我一直在尝试调试一个问题,我正在使用 GAS 将多个新行(通过多个 HTTP GET)写入 google 电子表格,但在写入其中一些行后,它们似乎无法获得锁定。我最初的超时时间为 5000 毫秒(5 秒),这对于单个调用来说应该有足够的时间,但我注意到,如果我要添加 20 行,该超时会导致随机行更新失败。 当我将时间改为 30 秒时,20 行就可以正常工作了。但如果我转到 60 行,我会再次遇到随机失败。改为 60 秒超时似乎有效。
这个超时似乎需要考虑写出所有行所需的累积时间。我想得越多,我认为这是有道理的,因为我是通过异步方法从 flutter/web 项目进行这些调用的。下面的 HTTP GET 的 dart 代码...
void submitOrder(String orderParms) async {
try {
String fullURL = "$URL$orderParms";
await http.get(fullURL).then((response) {
callback(convert.jsonDecode(response.body)['status']);
});
} catch (e) {
print(e);
}
}
下面的GAS doGet方法...
function doGet(request){
var prodID = "1qNt0Q-not-K37U-the-Ea8Fa-real-ynUF-url-xwskYfmDgnMEw2lX_LFblSuDw";
var result = {"status": "SUCCESS"};
try {
// Get all Parameters
var lastName = request.parameter.lastName;
var firstName = request.parameter.firstName;
var lock = LockService.getUserLock();
if (lock.tryLock(60000)) { // currently using 60 sec to be safe??
// Open the correct sheetGoogle Sheet using ID
var sheet_prod = SpreadsheetApp.openById(prodID);
var rowData = sheet_prod.appendRow([lastName, firstName]);
// Need to flush these updates so they fully take before releasing the lock
SpreadsheetApp.flush();
// ok, done important spreadsheet stuff, release the lock
lock.releaseLock();
// else did not get a lock
} else {
result = {"status": "FAILED - No Lock!", "message": "Lock timed out"};
}
} catch(exc){
result = {"status": "Exception! FAILED", "message": exc.message};
}
// Return result
return ContentService
.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}
我目前将超时设置为 60 秒,以确保我可以处理 30-40 行而不会失败获取锁。将超时设置得这么高有什么坏处吗?任何见解都非常感谢!!
官方文档没有提到使用“大”
milliseconds
数字有任何“危害”,因此这完全取决于这是否会影响您的网络应用程序和/或电子表格的用途。
旁注:由于您的代码在发布文档后不会对电子表格进行任何更改,因此无需包含
SpreadsheetApp.flush()
。
正如 Rubén 提到的,Google 官方文档没有提到锁定超时的最大值。但请确保您的整个运行时执行时间不超过本页中定义的脚本运行时限制https://developers.google.com/apps-script/guides/services/quotas#current_limitations
当前限制为 6 分钟/执行