我LockService在發送電子郵件時使用,但是當多個用戶同時運行它時,它會拋出錯誤:Service invoked too many times...
這是一些日志,顯示失敗:

function sendEmail() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
var lock = LockService.getScriptLock();
try {
lock.waitLock(3000); // wait 03 seconds for others' use of the code section and lock to stop and then proceed
} catch (e) {
Logger.log('Someone has just sent an Email. Try it again 3 seconds later.');
return HtmlService.createHtmlOutput("<b> Server Busy please try after some time <p>")
// In case this a server side code called asynchronously you return a error code and display the appropriate message on the client side
//return "Error: Server busy try again later... Sorry :("
}
/*
Gets certain data from current row. These are used as criteria
*/
GmailApp.sendEmail(email, "Text", name " BODY.", { name: 'Diplay Custom Name as Sender' });
//Looks for a matching record in another sheet to mark its adjacent column as sent ("Yes")
for (var n = 0; n < formRespValues.length; n ) {
if (formRespValues[n][1] == testNo) {
formRespSheet.getRange('M' (2 n)).setValue('Yes');
}
}
}
SpreadsheetApp.flush(); // applies all pending spreadsheet changes
lock.releaseLock();
}
uj5u.com熱心網友回復:
正如@Tanaike在評論中指出的那樣,在短時間內waitLock(timeoutInMillis)或tryLock(timeoutInMillis)可能會阻止鎖定服務按預期作業。
為確保正常運行,請嘗試設定一個遠大于腳本執行時間的時間。如果小于此值,鎖將過早“打開”,并導致問題中概述的錯誤。
檔案:
- 鎖服務
- 類鎖
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/426844.html
