1

I try to get all the sheet from a spreadsheet(ID) with the Google Sheets API, I haven't found the REST method to do that.

My code is

$range[]= 'sheet1!A:C'; $response = $service->spreadsheets_values->get($spreadsheetId, $range); $values = $response->getValues(); 

Array or string for $range works only if one value. Array with mlti value give a bad url in REST:

$range[]= 'sheet1!A:C'; $range[]= 'SHEET2!A:C'; $response = $service->spreadsheets_values->get($spreadsheetId, $range); 

Returns the following Error:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://sheets.googleapis.com/v4/spreadsheets/[spreadsheetID]/values/Config%21A%3AC,Carte%21A%3AC?key=[my api key]: (400) Unable to parse range: sheet1!A:C,SHEET2!A:C' in C:\Program Files\EasyPHP-12.1\www...\src\Google\Http\REST.php:110 Stack trace: #0 C:\Program Files\EasyPHP-12.1\www...\src\Google\Http\REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 C:\Program Files\EasyPHP-12.1\www...\src\Google\Task\Runner.php(174): call_user_func_array(Array, Array) #3 C:\Program Files\EasyPHP-12.1\www....\src\Google\Http\REST.php(46): Google_Task_Runner->run() #4 C:\Program Files\EasyPHP-12.1\www...\src\Google\Client.php(593): Google_Http_REST::execute(Object(Google_Client in C:\Program Files\EasyPHP-12.1\www...\src\Google\Http\REST.php on line 110

Thanks

1 Answer 1

1
$service->spreadsheets_values->get($spreadsheetId, $range); 

Takes a single range. You are getting an error because you are trying to send it two.

You should be calling spreadsheet.values.batchget

<?php /* * BEFORE RUNNING: * --------------- * 1. If not already done, enable the Google Sheets API * and check the quota for your project at * https://console.developers.google.com/apis/api/sheets * 2. Install the PHP client library with Composer. Check installation * instructions at https://github.com/google/google-api-php-client. */ // Autoload Composer. require_once __DIR__ . '/vendor/autoload.php'; $client = getClient(); $service = new Google_Service_Sheets($client); // The ID of the spreadsheet to retrieve data from. $spreadsheetId = ''; // TODO: Update placeholder value. $optParams = []; // The A1 notation of the values to retrieve. $optParams['ranges'] = []; // TODO: Update placeholder value. // How values should be represented in the output. // The default render option is ValueRenderOption.FORMATTED_VALUE. $optParams['valueRenderOption'] = ''; // TODO: Update placeholder value. // How dates, times, and durations should be represented in the output. // This is ignored if value_render_option is // FORMATTED_VALUE. // The default dateTime render option is [DateTimeRenderOption.SERIAL_NUMBER]. $optParams['dateTimeRenderOption'] = ''; // TODO: Update placeholder value. $response = $service->spreadsheets_values->batchGet($spreadsheetId, $optParams); // TODO: Change code below to process the `response` object: echo '<pre>', var_export($response, true), '</pre>', "\n"; function getClient() { // TODO: Change placeholder below to generate authentication credentials. See // https://developers.google.com/sheets/quickstart/php#step_3_set_up_the_sample // // Authorize using one of the following scopes: // 'https://www.googleapis.com/auth/drive' // 'https://www.googleapis.com/auth/drive.readonly' // 'https://www.googleapis.com/auth/spreadsheets' // 'https://www.googleapis.com/auth/spreadsheets.readonly' return null; } ?> 

Note code ripped directly from the documentation found here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.