首页 > 解决方案 > 优化空间?

问题描述

我的代码作为单个数组自定义函数可以正常工作,但是当我尝试运行时

Arrayformula(如果 A2:A = true, nextyear(A2:A),nextmonth(A2:A)

它不起作用,它说内部错误。

从我所看到的看起来可能是因为我的功能花费了太长时间?

function NextMonth(input) {

   if(input.map) {
    return input.map(NextMonth);}

  else {
  var month = Utilities.formatDate(new Date(input), "GMT+0","MM")-1;
  var day = Utilities.formatDate(new Date(input), "GMT+0","dd");
  var year = Utilities.formatDate(new Date(input), "GMT+0","yyyy");

  var output = new Date(year,month,day,0,0,0,0);
  var now = new Date();

  while (output < new Date()) {
  var month = Utilities.formatDate(new Date(output), "GMT+0","MM")-1+1;
  var day = Utilities.formatDate(new Date(output), "GMT+0","dd");
  var year = Utilities.formatDate(new Date(output), "GMT+0","yyyy");
  var output = new Date(year,month,day,0,0,0,0);

  }

  return (output)
  }
} 

标签: google-apps-scriptgoogle-sheetsgoogle-sheets-formulaarray-formulascustom-function

解决方案


自定义函数不能像使用内置函数一样在 ARRAYFORMULAS 中使用。通常最好构建一个自定义函数来完成整个公式的工作。

为什么?

自定义函数有 30 秒的执行时间限制,而内置函数没有此功能。

另一个限制是自定义函数不能使用 NOW()、TODAY()、RAND()、RANDBETWEEN() 等易失性函数作为参数。

有关的


推荐阅读