【问题标题】:Room For Optimization?优化空间?
【发布时间】:2019-11-24 16:24:40
【问题描述】:

我的代码作为单个数组自定义函数运行良好,但是当我尝试运行时

数组公式(如果 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-script google-sheets google-sheets-formula array-formulas custom-function


【解决方案1】:

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

为什么?

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

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

相关

【讨论】:

  • 确认不能使用是什么意思?它在我做一个数组时有效,但在较长的 if 语句中它最终不起作用。
  • @PatrickDufresne 我编辑了我的答案(更改了措辞并添加了一些“为什么”和相关问题的链接。
猜你喜欢
  • 1970-01-01
  • 2012-08-17
  • 2012-08-23
  • 2011-05-23
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多