将getActiveSpreadsheet与Webapp绑定到Sheets失败 [英] Failure using getActiveSpreadsheet with webapp bound to Sheets

查看:43
本文介绍了将getActiveSpreadsheet与Webapp绑定到Sheets失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

An odd thing is happening. Maybe someone can explain it or maybe there's a bug. I'm writing a webapp that runs from an Apps Script bound to a spreadsheet. The webapp runs normally when I open the spreadsheet using openById(), but not getActiveSpreadsheet(). When I load the page I get this message, "TypeError: Cannot call method "getSheetByName" of null. (line 11, file "Code")". It behaves like it's not bound to the spreadsheet.

但是,如果我手动运行doGet(),则不会收到该错误.已成功从日志表中提取数据.这是我的代码...当我注释掉第1行和取消注释第2行时,它可以正常工作.提前致谢.

However, if I run doGet() manually I do not get the error. Data is pulled successfully from the Log sheet. Here's my code... when I comment out line 1 and uncomment line 2 it works perfectly. Thanks in advance.

    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;
    }

推荐答案

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

Remove your 'doGet()' function. If your script is an add-on, it doesn't need a doGet(). Instead use onInstall, onOpen and then use HTML service to open pages.

您正在发送Google混合信号.

You're sending google mixed signals.

绑定脚本和Webapp不同.

Bound scripts and Webapps are different.

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

这篇关于将getActiveSpreadsheet与Webapp绑定到Sheets失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆