【问题标题】:Failure using getActiveSpreadsheet with webapp bound to Sheets [duplicate]使用绑定到表格的 webapp 的 getActiveSpreadsheet 失败 [重复]
【发布时间】:2016-07-14 03:08:32
【问题描述】:

一件奇怪的事情正在发生。也许有人可以解释它,或者可能有一个错误。我正在编写一个从绑定到电子表格的应用程序脚本运行的 web 应用程序。当我使用 openById() 打开电子表格时,webapp 正常运行,但不是 getActiveSpreadsheet()。当我加载页面时,我收到此消息“TypeError:无法调用 null 的方法“getSheetByName”。(第 11 行,文件“代码”)”。它的行为就像它没有绑定到电子表格一样。

但是,如果我手动运行 doGet(),我不会收到错误消息。从日志表中成功提取数据。这是我的代码...当我注释掉第 1 行并取消注释第 2 行时,它工作得很好。提前致谢。

    function doGet() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Log');
      //  var sheet = SpreadsheetApp.openById('abc123def456').getSheetByName('Log');
      var data = sheet.getDataRange().getValues();
      var t = HtmlService.createTemplateFromFile('index');
      t.data = data;
      var evaluatedTemplate = t.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
      return evaluatedTemplate;
    }

【问题讨论】:

  • 是的,可能是重复的。我受到该线程的启发,并提出了一种不同的解决方法,似乎不需要最终用户的额外授权。
  • 那么请在答案中分享。
  • 没关系。解决方法失败

标签: google-apps-script google-sheets


【解决方案1】:

删除您的“doGet()”函数。如果您的脚本是附加组件,则不需要 doGet()。而是使用 onInstall、onOpen 然后使用 HTML 服务打开页面。

您正在向 Google 发送混合信号。

绑定脚本和 Webapp 不同。

https://developers.google.com/apps-script/guides/bound

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多