通过onOpen命令运行时SpreadsheetApp.openById显示错误 [英] SpreadsheetApp.openById displays error when run via the onOpen command

查看:84
本文介绍了通过onOpen命令运行时SpreadsheetApp.openById显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑这是一个简单的修复程序,但是我在Google App脚本中遇到了问题,我读了许多帖子,这些帖子表明这些功能在自定义函数中已被弃用,但我不知道如何解决。

I suspect this is a simple fix but I'm having problems in my Google App Script, I have read a number of posts that indicate these functions are deprecated in custom functions but I'm lost how to fix.

我有一个脚本,当使用onOpen()函数打开Goog​​le表格时,该脚本会自动运行。我试图在此函数内移动OpenByID()方法,但遇到相同的错误。

I have a script that is run automatically when the Google Sheet is opened using the onOpen() function. I tried to move the OpenByID() method inside this function but get the same error. Can someone tell me how to fix this specifically please.

function onOpen(e) {
//function test(e) {
  // Add a custom menu to the spreadsheet.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var masterSheet=ss.getSheetByName(masterTab);

  for(var xLOSName = 3; xLOSName <= 4; xLOSName++) 
  {
    Logger.log('Attempting to open file');
    var fileId = masterSheet.getRange(xLOSName,2).getValue();
    Logger.log('Opening File ID: ' + fileId);
    var xLOSSpreadsheet  = SpreadsheetApp.openById(masterSheet.getRange(xLOSName,2).getValue());

    //var xLOSSpreadsheet  = SpreadsheetApp.openById(masterSheet.getRange(xLOSName,2).getValue()).getSheets()[0];
    Logger.log(xLOSSpreadsheet.getName());
  }

注意:它永远无法记录文件名。愚蠢的是,这在从调试器运行时有效。
问候

NOTE: It never get to the point of logging the name of the file. Stupidly, this works when running from the debugger. Regards

推荐答案

您不能在自定义函数中使用SpreadsheetApp.openById()。

You cannot use SpreadsheetApp.openById() in custom functions. It's not just deprecated it's not allowed.

对自定义功能的限制

此外,像onOpen()这样的简单触发器也无法执行需要权限的操作。

Also simple triggers like onOpen() cannot perform operations that require permission.

简单触发器限制

如果需要需要权限的操作,您必须使用可安装的触发器。

If you require operations that require permission you must use installable triggers.

这篇关于通过onOpen命令运行时SpreadsheetApp.openById显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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