Google apps script Google脚本自定义函数错误解决方法
我在各种操作的电子表格中使用自定义函数。 我使用了缓存值的属性服务。此外,我还向函数发送了一系列值,而不是数百次调用该函数 我正在为一个包含100行的列使用该函数,并且正在增长。我试着在一点上取两列,但它超过了30秒的自定义函数限制 尽管如此,我还是收到了一些错误消息: “错误:执行自定义函数时出现内部错误。” 因为同一个自定义函数同时50次计算这100行的答案 我收到这个消息大约是50次中的9次。这并不奇怪,因为函数正在计算很多东西 如果可以在不同的时间启动相同的自定义函数,或者如果自定义函数在错误消息后再次运行,问题将得到解决。也许还有别的办法 我尝试过应用指数退避,但我想用自定义函数是不可能做到这一点的 我是JavaScript新手,我试图找到解决方法,但没有成功 这是调用此自定义函数的电子表格的图像:Google apps script Google脚本自定义函数错误解决方法,google-apps-script,google-sheets,error-handling,google-sheets-custom-function,Google Apps Script,Google Sheets,Error Handling,Google Sheets Custom Function,我在各种操作的电子表格中使用自定义函数。 我使用了缓存值的属性服务。此外,我还向函数发送了一系列值,而不是数百次调用该函数 我正在为一个包含100行的列使用该函数,并且正在增长。我试着在一点上取两列,但它超过了30秒的自定义函数限制 尽管如此,我还是收到了一些错误消息: “错误:执行自定义函数时出现内部错误。” 因为同一个自定义函数同时50次计算这100行的答案 我收到这个消息大约是50次中的9次。这并不奇怪,因为函数正在计算很多东西 如果可以在不同的时间启动相同的自定义函数,或者如果自定义函数
// Names - range of names e.g. A4:A100
// function is returning number if it meets conditions
function VSApmokyti(Names, date, place ) {
// if date and place is not arrays
if ( !Array.isArray(date) ) {
return Names.map (function (d) {
return process (d[0], date, place)
})
}
// if date and place is arrays
else {
return Names.map (function (d) {
return date[0].map (function (k, h) {
return process (d[0], k, place[0][h])
})
})
}
// this function can calculate no matter if date or place is arrays or values
function process(teacher, Vdate, school) {
if (Vdate=="") {
return null;
}
if (teacher=="") {
return null;
}
// Taking from CACHE
var cache = CacheService.getScriptCache();
var teachersL = cache.get("TeachersL");
teachersL = JSON.parse(teachersL);
var teachers1 = cache.get("Teachers1");
teachers1 = JSON.parse(teachers1);
var teachers2 = cache.get("Teachers2");
teachers2 = JSON.parse(teachers2);
var teachers3 = cache.get("Teachers3");
teachers3 = JSON.parse(teachers3);
var teachers4 = cache.get("Teachers4");
teachers4 = JSON.parse(teachers4);
var dates = cache.get("Dates");
dates = JSON.parse(dates);
var Schools = cache.get("Schools");
Schools = JSON.parse(Schools);
var number = cache.get("NumberScholars");
number = JSON.parse(number);
if (!number) {
// WRITING to CACHE
var TeachersL = PropertiesService.getScriptProperties().getProperty('TeachersL');
cache.put('TeachersL', TeachersL);
teachersL = JSON.parse(TeachersL);
var Teachers1 = PropertiesService.getScriptProperties().getProperty('Teachers1');
cache.put('Teachers1', Teachers1);
teachers1 = JSON.parse(Teachers1);
var Teachers2 = PropertiesService.getScriptProperties().getProperty('Teachers2');
cache.put('Teachers2', Teachers2);
teachers2 = JSON.parse(Teachers2);
var Teachers3 = PropertiesService.getScriptProperties().getProperty('Teachers3');
cache.put('Teachers3', Teachers3);
teachers3 = JSON.parse(Teachers3);
var Teachers4 = PropertiesService.getScriptProperties().getProperty('Teachers4');
cache.put('Teachers4', Teachers4);
teachers4 = JSON.parse(Teachers4);
var Dates = PropertiesService.getScriptProperties().getProperty('Dates');
cache.put('Dates', Dates);
dates = JSON.parse(Dates);
var Schools = PropertiesService.getScriptProperties().getProperty('Schools');
cache.put('Schools', Schools);
Schools = JSON.parse(Schools);
var NumberScholars = PropertiesService.getScriptProperties().getProperty('NumberScholars');
cache.put('NumberScholars', NumberScholars);
number = JSON.parse(NumberScholars);
}
// converting date from spreadsheet cell to be able check if condition
Vdate = Vdate.toJSON();
for(var y = 0; y < Schools.length; y++) {
if(Vdate==dates[y] && school==Schools[y]) {
if ((teacher==teachersL[y]) || (teacher==teachers1[y]) || (teacher==teachers2[y]) || (teacher==teachers3[y]) || (teacher==teachers4[y])) {
return number[y];
}
}
}
}
}
这是我的自定义函数的代码:
// Names - range of names e.g. A4:A100
// function is returning number if it meets conditions
function VSApmokyti(Names, date, place ) {
// if date and place is not arrays
if ( !Array.isArray(date) ) {
return Names.map (function (d) {
return process (d[0], date, place)
})
}
// if date and place is arrays
else {
return Names.map (function (d) {
return date[0].map (function (k, h) {
return process (d[0], k, place[0][h])
})
})
}
// this function can calculate no matter if date or place is arrays or values
function process(teacher, Vdate, school) {
if (Vdate=="") {
return null;
}
if (teacher=="") {
return null;
}
// Taking from CACHE
var cache = CacheService.getScriptCache();
var teachersL = cache.get("TeachersL");
teachersL = JSON.parse(teachersL);
var teachers1 = cache.get("Teachers1");
teachers1 = JSON.parse(teachers1);
var teachers2 = cache.get("Teachers2");
teachers2 = JSON.parse(teachers2);
var teachers3 = cache.get("Teachers3");
teachers3 = JSON.parse(teachers3);
var teachers4 = cache.get("Teachers4");
teachers4 = JSON.parse(teachers4);
var dates = cache.get("Dates");
dates = JSON.parse(dates);
var Schools = cache.get("Schools");
Schools = JSON.parse(Schools);
var number = cache.get("NumberScholars");
number = JSON.parse(number);
if (!number) {
// WRITING to CACHE
var TeachersL = PropertiesService.getScriptProperties().getProperty('TeachersL');
cache.put('TeachersL', TeachersL);
teachersL = JSON.parse(TeachersL);
var Teachers1 = PropertiesService.getScriptProperties().getProperty('Teachers1');
cache.put('Teachers1', Teachers1);
teachers1 = JSON.parse(Teachers1);
var Teachers2 = PropertiesService.getScriptProperties().getProperty('Teachers2');
cache.put('Teachers2', Teachers2);
teachers2 = JSON.parse(Teachers2);
var Teachers3 = PropertiesService.getScriptProperties().getProperty('Teachers3');
cache.put('Teachers3', Teachers3);
teachers3 = JSON.parse(Teachers3);
var Teachers4 = PropertiesService.getScriptProperties().getProperty('Teachers4');
cache.put('Teachers4', Teachers4);
teachers4 = JSON.parse(Teachers4);
var Dates = PropertiesService.getScriptProperties().getProperty('Dates');
cache.put('Dates', Dates);
dates = JSON.parse(Dates);
var Schools = PropertiesService.getScriptProperties().getProperty('Schools');
cache.put('Schools', Schools);
Schools = JSON.parse(Schools);
var NumberScholars = PropertiesService.getScriptProperties().getProperty('NumberScholars');
cache.put('NumberScholars', NumberScholars);
number = JSON.parse(NumberScholars);
}
// converting date from spreadsheet cell to be able check if condition
Vdate = Vdate.toJSON();
for(var y = 0; y < Schools.length; y++) {
if(Vdate==dates[y] && school==Schools[y]) {
if ((teacher==teachersL[y]) || (teacher==teachers1[y]) || (teacher==teachers2[y]) || (teacher==teachers3[y]) || (teacher==teachers4[y])) {
return number[y];
}
}
}
}
}
//名称-名称范围,例如A4:A100
//函数在满足条件时返回数字
函数VSApmokyti(名称、日期、地点){
//如果日期和地点不是数组
如果(!Array.isArray(日期)){
返回Names.map(函数(d){
退货流程(d[0],日期,地点)
})
}
//如果日期和地点是数组
否则{
返回Names.map(函数(d){
返回日期[0]。映射(函数(k,h){
返回流程(d[0],k,位置[0][h])
})
})
}
//无论日期或地点是数组还是值,此函数都可以计算
功能流程(教师、Vdate、学校){
如果(Vdate==“”){
返回null;
}
如果(教师==“”){
返回null;
}
//从缓存中获取
var cache=CacheService.getScriptCache();
var teachersL=cache.get(“teachersL”);
teachersL=JSON.parse(teachersL);
var teachers1=cache.get(“teachers1”);
teachers1=JSON.parse(teachers1);
var teachers2=cache.get(“teachers2”);
teachers2=JSON.parse(teachers2);
var teachers3=cache.get(“teachers3”);
teachers3=JSON.parse(teachers3);
var teachers4=cache.get(“teachers4”);
teachers4=JSON.parse(teachers4);
var dates=cache.get(“日期”);
dates=JSON.parse(日期);
var Schools=cache.get(“学校”);
Schools=JSON.parse(Schools);
var number=cache.get(“NumberScholars”);
number=JSON.parse(number);
如果(!编号){
//写入缓存
var TeachersL=PropertiesService.getScriptProperties().getProperty('TeachersL');
cache.put('TeachersL',TeachersL);
teachersL=JSON.parse(teachersL);
var Teachers1=PropertiesService.getScriptProperties().getProperty('Teachers1');
cache.put('Teachers1',Teachers1);
teachers1=JSON.parse(teachers1);
var Teachers2=PropertiesService.getScriptProperties().getProperty('Teachers2');
cache.put('Teachers2',Teachers2);
teachers2=JSON.parse(teachers2);
var Teachers3=PropertiesService.getScriptProperties().getProperty('Teachers3');
cache.put('Teachers3',Teachers3);
teachers3=JSON.parse(teachers3);
var Teachers4=PropertiesService.getScriptProperties().getProperty('Teachers4');
cache.put('Teachers4',Teachers4);
teachers4=JSON.parse(teachers4);
var Dates=PropertiesService.getScriptProperties().getProperty('Dates');
cache.put('Dates',Dates);
dates=JSON.parse(日期);
var Schools=PropertiesService.getScriptProperties().getProperty(“学校”);
cache.put('学校',学校);
Schools=JSON.parse(Schools);
var NumberScholars=PropertiesService.getScriptProperties().getProperty('NumberScholars');
cache.put('NumberScholars',NumberScholars);
number=JSON.parse(NumberScholars);
}
//将电子表格单元格中的日期转换为能够检查条件
Vdate=Vdate.toJSON();
对于(var y=0;y
有几种方法可以处理应用程序脚本中的错误。一种不太难的方法是使用try-and-catch。例如,下面的代码会将函数运行时发生的任何错误的日志写入工作表。而且try-and-catch是无阻塞的,所以即使有错误,函数也不会停止。下面是一个将错误记录到Google工作表中的示例
function somefunction(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var logSheet = ss.getSheetByName('Sheet1');
try {
myFunctionThatMightHaveErrors()
}
catch(e){
var now = new Data();
var error = JSON.stringify(e);
logSheet.appendRow([now, error]);
}
}
仅供参考:应用程序脚本将运行任何功能长达6分钟,除非您的域具有Gsuite Buissness帐户和应用程序生成器。但是,如果需要的话,有办法绕过时间限制 简短回答
使其返回一个值数组,而不是返回单个值的自定义函数
解释
自定义函数可以返回单个值或值数组。如果要将自定义函数应用于连续单元格,则可以使用单个公式返回值数组,而不是将公式应用于每个单元格,从而导致多次计算,但请记住自定义函数的执行时间限制为30秒。看
如果您的函数超过了30秒的限制,则使用“常规”函数,该函数将由自定义菜单、对话框、侧边栏、触发器或Google应用程序脚本编辑器调用。这类函数对于普通帐户有6分钟的执行时间限制,对于注册的G套件帐户有30分钟的执行时间限制。自定义函数的作用是什么?你考虑过退货吗