【问题标题】:Uncaught SyntaxError: Unexpected token '>' at $response lineUncaught SyntaxError: Unexpected token '>' at $response line
【发布时间】: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


【解决方案1】:

&lt;script&gt;&lt;/script&gt; 只能用于将javascript代码包含到html文件中

  • 因此,粘贴在这些标记中的任何代码都将被读取为 javascript。
  • -&gt; 不是有效的 javascript 运算符,这就是代码向您抛出(第一个)错误的原因

如果您想要实现的不是将 php 代码嵌入到您的 html 中 - 过程是不同的:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2019-02-10
    • 2014-07-08
    • 2011-03-09
    • 2014-01-06
    相关资源
    最近更新 更多