Google张自定义函数内置函数

Google sheets custom function built-in function

我在 google 工作表中有以下自定义函数,我试图在我的自定义函数中调用内置函数 "TEXT" 但它没有成功。 Google张会提示"unknown"函数"TEXT"。有解决办法吗?

function NextMonth(StockTradeDate) {
  var DeltaDate;
  if (**TEXT**(StockTradeDate,"mmm") = "JAN" ) {
  DeltaDate = 30;
      }
  return DATEVALUE(StockTradeDate) + 31;    
}

尝试使用 javascript 日期方法(如下所示)来驱动您需要的日期和条件。无法找到支持从应用程序脚本函数中调用表格内置函数的文档。 Javascript 支持。

function NEXTMONTH(StockTradeDate) {

  var DeltaDate

    if (StockTradeDate.getMonth() === 0 ) { //Jan:0  Feb:1 ... Dec:11 these will need more conditionals. 
    DeltaDate = 30;
    }

  var date2 = new Date(StockTradeDate.valueOf()+ DeltaDate*24*60*60*1000)
                                   // valueOf() is millisec time since 1/1/1970
  return date2
}

如果您需要有关日期方法和实现的更多信息,w3schools 有一个有效的参考。

Google Apps 脚本具有包含 formatDate 方法的实用程序库

Utilities.formatDate(date, timeZone, format)

详情见https://developers.google.com/apps-script/reference/utilities/utilities#formatdatedate-timezone-format

值得一提的是,在 Google 表格中,无法在脚本中调用内置函数。如果像 Utilities 这样的服务不包含您正在寻找的功能,那么另一种方法是构建您自己的版本,从图书馆、开源项目或任何其他地方借来它。

我尝试使用 Ethercalc 的电子表格函数库并在我的 answer to Is there a way to evaluate a formula that is stored in a cell?

上分享了这个