Is it possible to call Lock in two unrelated functions without locking affecting the other?

If I have two unrelated functions in Code.gs

(each interacting with a different google sheet), can I call lock

in both functions without one call acting on the other?

For future reference, an application that has about 80 users within the same domain works like Me, however, it is possible that:

  • User A calls myFunction_01()

    at the same time
  • User B calls myFunction_02()

In this case, will the locks work independently?

Is the following implementation possible 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;

}

      

+3


source to share





All Articles