如果我在Code.gs
中有两个不相关的功能(每个功能都与不同的Google表格相互作用),我是否可以在两个功能中调用lock
而不会有一个呼叫影响另一个功能?
对于更多上下文,该应用程序在同一个域中拥有大约80个用户,正在以Me身份运行,但有可能:
myFunction_01()
myFunction_02()
在这种情况下,锁会独立运行吗?
lock
的以下实施是否可以接受?
function myFunction_01() {
var my_spreadsheet_01 = SpreadsheetApp.openById("YYY");
var lock = LockService.getScriptLock();
try {
lock.waitLock(30000);
} catch (e) {
Logger.log('Could not obtain lock after 30 seconds.');
var returnObjectCatchError = {};
returnObjectCatchError['LockErrorFlag'] = "An exception occurred when waiting for lock";
return returnObjectCatchError;
}
// do things with my_spreadsheet_01 here
var return_object = {};
// apply all pending spreadsheet changes
SpreadsheetApp.flush();
//release lock
lock.releaseLock();
return return_object;
}
function myFunction_02() {
var my_spreadsheet_02 = SpreadsheetApp.openById("ZZZ");
var lock = LockService.getScriptLock();
try {
lock.waitLock(30000);
} catch (e) {
Logger.log('Could not obtain lock after 30 seconds.');
var returnObjectCatchError = {};
returnObjectCatchError['LockErrorFlag'] = "An exception occurred when waiting for lock";
return returnObjectCatchError;
}
// do things with my_spreadsheet_02 here
var return_object = {};
// apply all pending spreadsheet changes
SpreadsheetApp.flush();
//release lock
lock.releaseLock();
return return_object;
}