根据旧谷歌集团的这个帖子,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版功能或已经实施并且似乎有效的子集?
我一直在挖掘,但找不到关于如何在 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) [编辑以包含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
我有两个要同时运行的函数,但我不能让它们单独运行,因为一个函数包含一个无限循环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
asynchronous ×2
javascript ×2
callback ×1
ecma ×1
function ×1
promise ×1
simultaneous ×1
try-catch ×1
validation ×1