“无法从此上下文调用SpreadsheetApp.getUi()".在基于时间的触发器上不使用getUi()时出错 [英] "Cannot call SpreadsheetApp.getUi() from this context" error while not using getUi() on time-based trigger

查看:160
本文介绍了“无法从此上下文调用SpreadsheetApp.getUi()".在基于时间的触发器上不使用getUi()时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图每天晚上运行一个函数,该函数检查日期列表,并且如果发现日期已过并且该行中的所有复选框都被选中,则该函数可以正常工作.不过,每天我都会收到一封电子邮件,提示

I am trying to run a function every night that checks a list of dates and does work if it finds that a date has passed and all the checkboxes on that row are checked. Every day, though, I get an email saying

无法从此上下文中调用SpreadsheetApp.getUi().(第172行,文件代码")."

"Cannot call SpreadsheetApp.getUi() from this context. (line 172, file "Code")".

奇怪的是,我在CheckHireDates函数中的任何地方都没有使用getUi(),并且它指定的行甚至不在应该运行的函数中.第172行在我的onEdit函数中,该函数也不使用getUi().有人可以帮我理解为什么我会收到此错误吗?

The weird thing is that I don't use getUi() anywhere in my CheckHireDates function and the line that it specifies is not even in the function that is supposed to run. Line 172 is in my onEdit function which also doesn't use getUi(). Can anybody help me understand why I'm getting this error?

我确实在onOpen函数中使用了getUi,因此当我开始遇到此问题时就将其注释掉,但仍然无法解决任何问题.

I did use getUi in my onOpen function, so I commented it out when I started having this problem but it still didn't fix anything.

function CheckHireDates() {
var spreadsheet = SpreadsheetApp.getActive();
var PaycomSheet = spreadsheet.getSheetByName('Paycom');
var TechSheet = spreadsheet.getSheetByName("Tech Key");
var SoftwareTracker = spreadsheet.getSheetByName('Software Tracking');
var range = "R2:R";
var Hvals = PaycomSheet.getRange("H2:H").getValues();
var Hlast = Hvals.filter(String).length;
var data = PaycomSheet.getRange(range).getValues();
var today = new Date().toLocaleDateString();
for(var i = 0; i < Hlast;i++)
{
    Hvals[i].toLocaleString();
    if(Hvals[i] <= today && (PaycomSheet.getRange('R' + (i+2)).getValue() == true))
    {
        var fullName = PaycomSheet.getRange('D' + (i+2)).getValue();
        var techRow = findMatchingRow(fullName, "Tech Key");  
        var softwareRow = findMatchingRow(fullName, "Software Tracking");
        var position = PaycomSheet.getRange('G' + (i+2)).getValue();
        TechSheet.getRange('E' + techRow).setValue(1);
        SoftwareTracker.getRange('G' + softwareRow).setValue(1);
        if (position == "Pre-Employment, Initial") 
        {
            position = "Initial";
        }
        else if (position == "Pre-Employment, Ace") 
        {
            position = "Route";
        }
        else if (position == "Pre-Employment, Expert") 
        {
            position = "Route";
        }
        else
        {
            Logger.log("Position not known");
        }
        TechSheet.getRange('G' + techRow).setValue(position);
        SoftwareTracker.getRange('D' + softwareRow).setValue(position);
        SoftwareTracker.getRange('H' + softwareRow + ':M' + softwareRow).setValue(2);
        if (position !== "Initial") 
        {
            SoftwareTracker.getRange('N' + softwareRow).setValue(2);
        }  
    }
}

}

推荐答案

我遇到了这个问题,发现该问题涉及另一个用户设置的项目中的另一个google脚本.错误消息混合了我的脚本和其他脚本的详细信息:

I had that problem and found it involved another google script in the project set up by a different user. The error message had a mixture of details from my script and the other script:

myFunctionName 无法从此上下文调用SpreadsheetApp.getUi(). (第2行,文件" OtherScript ")

myFunctionName Cannot call SpreadsheetApp.getUi() from this context. (line 2, file "OtherScript")

other 脚本的

第2行确实使用了getUi()

Line 2 of the other script did use getUi()

var app = SpreadsheetApp.getUi();

看来,当我的脚本运行(由onChange事件触发)时,另一个脚本也将运行(可能也由change事件触发).另一个脚本设置了一些UI元素,一些简单的菜单.通常,这不会造成任何问题.但是,SpreadsheetApp.getUi()仅在存在打开的编辑器的当前实例的情况下才起作用(请参见 https://developers.google.com/apps-script/reference/base/ui ).因此,如果在没有打开编辑器的情况下发生了更改事件,那么它将失败,从而导致错误.

It seemed that when my script ran (triggered by onChange event) then the other script would also get run (maybe also triggered by a change event). The other script set up some UI elements, some simple menus. Normally that caused no problem. However, SpreadsheetApp.getUi() only works in the context of there being a current instance of an open editor (see https://developers.google.com/apps-script/reference/base/ui). So if a change event happened without an open editor then it fails, causing the error.

我通过稍微修改其他脚本来解决该问题来解决该问题:

I resolved the problem by slightly modifying the other script to catch the problem:

try {
  app = SpreadsheetApp.getUi();
} catch (error) {}
if (app != null) {
  //Use app...
}

也可能有用的另一种方法是与该人交谈以及如何触发他们的脚本,并查看他们是否将其更改为由仅在有人打开电子表格时才发生的onOpen事件触发.使用者介面.

A different approach that might also have worked is to speak with the person and how their script was triggered, and see if they'd change it to trigger by onOpen event that just occurs when someone opens a spreadsheet and hence there is a Ui context.

所以,我认为您的问题将来自项目中其他脚本中的SpreadsheetApp.getUi().查看错误消息是否像我的一样提到其他文件.当您的脚本在夜间运行时,将没有任何上下文,这可以解释当时为什么会发生错误.

So, I think your problem would be coming from SpreadsheetApp.getUi() in a different script in the project. See if the error message mentions a different file, like mine did. When your script runs in the night, there would be no context, which explains why the error occurs at that time.

这篇关于“无法从此上下文调用SpreadsheetApp.getUi()".在基于时间的触发器上不使用getUi()时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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