Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting request back instead of appending rows into a google sheet? #2600

Open
rajklakhani opened this issue Jun 2, 2024 · 1 comment
Open

Comments

@rajklakhani
Copy link

function insert_data_to_google_sheet($data): void {
    $client = getGoogleClient();
    $service = new \Google_Service_Sheets($client);
    $spreadsheetId = '1BgVqitbiMSP2sNQNy1hk0PCoItxacbyeZvjH8Zc2qu0';
     $range = 'Sheet1';
    $values = [
        $data
    ];

  $valueRange = new \Google_Service_Sheets_ValueRange();
  $valueRange->setValues($values);
  // I have tried the following statement as well
  // $body = new \Google_Service_Sheets_ValueRange([
  //     "values" => $values
  // ]); 


  $params = [
      'valueInputOption' => 'RAW'
  ];

  // appending record to sheet
  $result = $service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $params);

  echo '<pre>';
  print_r($result);
  echo '</pre>';
  // return $result;
}

$result is showing the following output instead of inserting the data:

GuzzleHttp\Psr7\Request Object
(
    [method:GuzzleHttp\Psr7\Request:private] => POST
    [requestTarget:GuzzleHttp\Psr7\Request:private] => 
    [uri:GuzzleHttp\Psr7\Request:private] => GuzzleHttp\Psr7\Uri Object
        (
            [scheme:GuzzleHttp\Psr7\Uri:private] => https
            [userInfo:GuzzleHttp\Psr7\Uri:private] => 
            [host:GuzzleHttp\Psr7\Uri:private] => sheets.googleapis.com
            [port:GuzzleHttp\Psr7\Uri:private] => 
            [path:GuzzleHttp\Psr7\Uri:private] => /v4/spreadsheets/1BgVqitbiMSP2sNQNy1hk0PCoItxacbyeZvjH8Zc2qu0/values/Sheet1:append
            [query:GuzzleHttp\Psr7\Uri:private] => valueInputOption=RAW
            [fragment:GuzzleHttp\Psr7\Uri:private] => 
        )

    [headers:GuzzleHttp\Psr7\Request:private] => Array
        (
            [host] => Array
                (
                    [0] => sheets.googleapis.com
                )

            [content-type] => Array
                (
                    [0] => application/json
                )

            [x-php-expected-class] => Array
                (
                    [0] => Google\Service\Sheets\AppendValuesResponse
                )

        )

    [headerLines:GuzzleHttp\Psr7\Request:private] => Array
        (
            [Host] => Array
                (
                    [0] => sheets.googleapis.com
                )

            [content-type] => Array
                (
                    [0] => application/json
                )

            [X-Php-Expected-Class] => Array
                (
                    [0] => Google\Service\Sheets\AppendValuesResponse
                )
        )

    [protocol:GuzzleHttp\Psr7\Request:private] => 1.1
    [stream:GuzzleHttp\Psr7\Request:private] => GuzzleHttp\Psr7\Stream Object
        (
            [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #148
            [size:GuzzleHttp\Psr7\Stream:private] => 
            [seekable:GuzzleHttp\Psr7\Stream:private] => 1
            [readable:GuzzleHttp\Psr7\Stream:private] => 1
            [writable:GuzzleHttp\Psr7\Stream:private] => 1
            [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
            [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                (
                )
        )
)

I need urgent help, please connect with someone who has already faced an issue like this.

Thanks

image

@Hectorhammett
Copy link
Contributor

Hello @rajklakhani! Thanks for the message!

I recommend taking a look at this snippet:
https://github.com/googleworkspace/php-samples/blob/main/sheets/snippets/src/SpreadsheetAppendValues.php

Also got a question, have you used the setUseBatch method on the client? code reference as that is the expected behaviour when batch mode is set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants