相关疑难解决方法(0)

哪个版本的ECMA-262是否支持Google Apps脚本?

根据旧谷歌集团的这个帖子,Apps脚本基于ECMA-262第3版.

这似乎得到了编辑器中的自动完成显示第3版阵列功能这一事实的支持.

但是,以下代码运行得非常好,这使人对此事表示怀疑:

var array = [
  1,2,3,4,5
];

Logger.log("forEach:");
array.forEach(function (item,idx,arr) {
  Logger.log(item); 
});
Run Code Online (Sandbox Code Playgroud)

请注意每个使用ECMA-262第5版阵列功能.

有权威的人会给出明确的答案吗?为什么会这样?是否可以安全地依赖所有第5版功能或已经实施并且似乎有效的子集?

javascript ecma google-apps-script

29
推荐指数
1
解决办法
5855
查看次数

函数应用脚本的异步执行

我一直在挖掘,但找不到关于如何在 Google App Script 中使用异步函数的参考或文档,我发现人们提到这是可能的,但没有提到如何...

有人可以指出我正确的方向或为我提供一个例子吗?承诺、回调或其他可以帮助我解决这个问题的东西。

我有这个函数可以调用它foo需要一段时间才能执行(足够长的时间它可能会超时 HTTP 调用)。

我想要做的是重构它,它的工作方式是这样的:

function doPost(e) {
    // parsing and getting values from e
    var returnable = foo(par1, par2, par3);
      return ContentService
             .createTextOutput(JSON.stringify(returnable))
             .setMimeType(ContentService.MimeType.JSON);
}

function foo(par1, par2, par3) {
    var returnable = something(par1, par2, par3); // get the value I need to return;

    // continue in an Async way, or schedule execution for something else
    // and allow the function to continue its flow
    /* async bar(); */

    return returnable;
} …
Run Code Online (Sandbox Code Playgroud)

asynchronous callback promise google-apps-script

10
推荐指数
3
解决办法
9436
查看次数

尝试...捕获在 Google Apps 脚本中未按预期工作

[编辑以包含TJ Crowder建议的最小可重现示例。]

我正在使用 Google Apps 脚本开发一个简单的函数,该函数应该将日期错误字符串发布到电子表格的列中。该列已具有拒绝任何非有效日期值的数据验证规则。这一切都很好。

我的问题是这样的:

我尝试使用 try...catch 块来优雅地处理错误,并在值未通过数据验证时仅记录错误消息。try...catch 似乎根本不起作用。相反,脚本会抛出错误并中断,并且日志显示为空。

这是带有新代码的更新屏幕截图(抱歉,Sourabh Choraria,覆盖了您的更新)。令人惊讶的是,GAS 突出显示了错误发生位置上方的一行。

更新后的代码的屏幕截图

作为一点背景知识,此脚本获取存储在列中的各种其他电子表格的 ID,获取每个电子表格的最后更新时间戳,并将结果发布到结果列中。

这是我使用的代码。

function trackDocUpdates() {
    //Set the global variables
    var ss = SpreadsheetApp.getActive();
    var residentSheet = ss.getSheetByName("Resident Documents");
    var activeRange = residentSheet.getDataRange();
    var numRows = activeRange.getNumRows();
    var lastRevision = "No value yet.";

    //Loop through the rows of data to get the last updated timestamp of each document
    //The first data row is the 5th row …
Run Code Online (Sandbox Code Playgroud)

javascript validation try-catch google-sheets google-apps-script

4
推荐指数
1
解决办法
5488
查看次数

有没有办法同时(同时/异步)运行两个函数,一个函数作为无限循环?

我有两个要同时运行的函数,但我不能让它们单独运行,因为一个函数包含一个无限循环while(true)。JavaScript 的问题是,如果你在哪里运行两个函数,它会在运行下一个之前完成该函数的运行;所以如果我用while(true)循环运行一个函数,它永远不会移动到下一个函数。
如果你还不明白,这是我的代码:

function onOpen(){           // Google Apps Script trigger
    infLoop()                //how to run both of these functions at the same time?
    runScript()
}

function infLoop(){          //inf loop.

    while(True){
        Utilities.sleep(100)
        DocumentApp.getActiveDocument()
        .setname("dont change this name")
    }
}

function runScript(){
    //code...
}
Run Code Online (Sandbox Code Playgroud)

asynchronous function simultaneous infinite-loop google-apps-script

-1
推荐指数
1
解决办法
4426
查看次数