【发布时间】:2021-06-08 09:43:40
【问题描述】:
我正在编写代码以使用 Google Sheets API 和 PHP 从我的 google 表格中获取数据,但我的数据未显示,并且出现运行错误:
Uncaught SyntaxError: Unexpected token '>'
这是我的代码:
<script>
function makeApiCall() {
// The ID of the spreadsheet to retrieve data from.
$spreadsheetId: '1wcNmi2hgz30gx1zjrKDgMNrxf1mPPFXbp7g_WRpK9Hw'; // TODO: Update placeholder value.
// The A1 notation of the values to retrieve.
$range: 'Data1!A2:H'; // TODO: Update placeholder value.
// How values should be represented in the output.
// The default render option is ValueRenderOption.FORMATTED_VALUE.
// $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].
// $dateTimeRenderOption: ''; // TODO: Update placeholder value.
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (empty($values)) {
print "No data found.\n";
} else {
foreach ($values as $row) {
// Print columns A and E, which correspond to indices 0 and 4.
printf("%s %s %s %s %s %s %s\n", $row[0], $row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7]);
}
}
}
function initClient() {
var API_KEY = 'AIzaSyAOFUNhuXlOxPBTmkqUEyxnEkG8mnR9N7k'; // TODO: Update placeholder with desired API key.
var CLIENT_ID = '289763823878-c2pfkf7g9plaup99v4p42fncnm3on188.apps.googleusercontent.com'; // TODO: Update placeholder with desired client ID.
// TODO: Authorize using one of the following scopes:
// 'https://www.googleapis.com/auth/drive'
// 'https://www.googleapis.com/auth/drive.file'
// 'https://www.googleapis.com/auth/drive.readonly'
// 'https://www.googleapis.com/auth/spreadsheets'
// 'https://www.googleapis.com/auth/spreadsheets.readonly'
var SCOPE = 'https://www.googleapis.com/auth/spreadsheets.readonly';
gapi.client.init({
'apiKey': API_KEY,
'clientId': CLIENT_ID,
'scope': SCOPE,
'discoveryDocs': ['https://sheets.googleapis.com/$discovery/rest?version=v4'],
}).then(function() {
makeApiCall();
});
}
</script>
我还是初学者,我遇到这个问题已经一周了,有人可以帮助我吗?
【问题讨论】:
-
良好的代码缩进将帮助我们阅读代码,更重要的是,它将帮助您调试代码Take a quick look at a coding standard 为您自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我的。
-
错误消息通常包含一个线索,指出错误在哪里
-
为什么在javascript代码中使用php代码?
-
你的
printf()比占位符有更多的价值:) -
谢谢你,我没有意识到这一点..
标签: php google-sheets-api