从 Values.get 请求多个范围会导致:无法解析范围错误
Posted
技术标签:
【中文标题】从 Values.get 请求多个范围会导致:无法解析范围错误【英文标题】:Requesting multiple ranges from Values.get results in: Unable to parse range error 【发布时间】:2017-08-16 19:01:48 【问题描述】:我尝试使用 Google Sheets API 从电子表格 (ID) 中获取所有工作表, 我还没有找到这样做的 REST 方法。
我的代码是
$range[]= 'sheet1!A:C';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
$range 的数组或字符串仅在一个值时有效。 具有 mlti 值的数组在 REST 中给出了错误的 url:
$range[]= 'sheet1!A:C';
$range[]= 'SHEET2!A:C';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
返回以下错误:
致命错误:未捕获的异常“Google_Service_Exception”与 message '调用 GET 时出错 https://sheets.googleapis.com/v4/spreadsheets/[spreadsheetID]/values/Config%21A%3AC,Carte%21A%3AC?key=[my api key]: (400) 无法解析范围:sheet1!A:C,SHEET2!A:C' in C:\Program Files\Easyphp-12.1\www...\src\Google\Http\REST.php:110 堆栈跟踪:#0 C:\Program 文件\EasyPHP-12.1\www...\src\Google\Http\REST.php(62): Google_Http_REST::decodeHttpResponse(对象(Google_Http_Request), 对象(Google_Client))#1 [内部函数]: Google_Http_REST::doExecute(Object(Google_Client), 对象(Google_Http_Request))#2 C:\Program 文件\EasyPHP-12.1\www...\src\Google\Task\Runner.php(174): call_user_func_array(Array, Array) #3 C:\Program 文件\EasyPHP-12.1\www....\src\Google\Http\REST.php(46): Google_Task_Runner->run() #4 C:\Program 文件\EasyPHP-12.1\www...\src\Google\Client.php(593): Google_Http_REST::execute(Object(C:\Program 中的 Google_Client Files\EasyPHP-12.1\www...\src\Google\Http\REST.php 在第 110 行
谢谢
【问题讨论】:
【参考方案1】:$service->spreadsheets_values->get($spreadsheetId, $range);
采用单个范围。您收到错误是因为您尝试发送两个。
你应该打电话给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;
?>
注释代码直接从here找到的文档中提取
【讨论】:
以上是关于从 Values.get 请求多个范围会导致:无法解析范围错误的主要内容,如果未能解决你的问题,请参考以下文章