Javascript google API快速入门错误

时间:2018-01-23 01:36:07

标签: google-apps-script google-apps-script-api

我正在从这里完成教程:https://developers.google.com/apps-script/api/quickstart/js。当我尝试使用客户端ID和API密钥插入在窗口平台上本地运行quickstart.html时,它会在第144行引发错误:

  

Uncaught SyntaxError:无效或意外的令牌。
  未捕获的ReferenceError:未在HTMLScriptElement.onload

中定义handleClientLoad

在完成此快速入门之前,我是否遗漏了其他需要启用的内容?

1 个答案:

答案 0 :(得分:0)

我认为Google示例脚本中存在一些问题。 我发现和修改的是,

  1. 脚本标记上的async和defer属性
  2. 您可能希望使用async属性,但快速修复是删除异步并添加第一个和第二个脚本标记的延迟。

        <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. callAppsScript函数内的字符串语法错误。
    2. 这似乎是转义语法错误和网页中示例代码的换行符处理的组合。以下是我修改的代码的工作片段。

          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. 调用不存在的功能
      2. 示例代码在callScriptFunction内调用不存在的updateSigninStatus。它必须是callAppsScript,但后者需要一个参数。

        我将调用callScriptFunction();替换为以下内容,并且有效。

        callAppsScript(gapi.auth2.getAuthInstance());
        

        通过进行上述更改,示例可以在服务器端创建新脚本,但在更新时会返回错误。 因此,示例代码中似乎存在更多潜在问题,但这是另一个问题,我认为与原始问题无关。