简体   繁体   中英

Javascript google API quickstart error

I'm working through the tutorial from here : https://developers.google.com/apps-script/api/quickstart/js . When i try to run the quickstart.html locally on window platform with client ID and API key insert, it throws an error at line 144 saying:

Uncaught SyntaxError: Invalid or unexpected token.
Uncaught ReferenceError: handleClientLoad is not defined at HTMLScriptElement.onload

Did i miss something else that need to be enable before going through this quickstart?

I think there are some issues in the Google sample script. What I found and modified are,

  1. async and defer attributes on script tag

You may want to use async attribute, but a quick fix is to remove async and add defer to both the 1st and 2nd script tags.

    <pre id="content"></pre>
    <!-- add defer to the first script tag -->
    <script defer type="text/javascript">
    ...
    <!-- remove async from the 2nd tag -->
    <script defer src="https://apis.google.com/js/api.js"
  1. Syntax Error on Strings inside callAppsScript function.

This seems that the combination of escaping syntax error and the newline handling of the sample code in the web page. The following is the working fragment of the code I modified.

    resource: {
          files: [{
            name: 'hello',
            type: 'SERVER_JS',
            source: 'function helloWorld() {\n  console.log("Hello, world!");\n}'
          }, {
            name: 'appsscript',
            type: 'JSON',
            source: "{\"timeZone\":\"America/New_York\",\"exceptionLogging\":\"CLOUD\"}"
          }]
  1. Calling non existing function

The sample code calls callScriptFunction inside updateSigninStatus which does not exist. It must be callAppsScript but the latter requires a parameter.

I replaced the calling callScriptFunction(); to the following, and it worked.

callAppsScript(gapi.auth2.getAuthInstance());

By making above changes, the sample can create a new script on the server-side, but error is returned upon updating it. So it seems that there are some more potential issues in the sample code, but it is another problem, and nothing to do with the original question, I guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM