Code Summary
I have a Google Apps Script project that is used by approximately 80 users in a specific domain, however the application is executed by me (i.e Publish. Deploy as web app>> Execute the app as:) Me.
One of the functions of the Script is to fill out a Google Sheet from a user form (using HTML Service), and then notify yourself and the sender (who is identified using a simple login system and cookies).
It has been working fine for about 6 months, but a notification email has been sent 1-2 times, but the record in Google Sheet has not appeared.
I think this may be due to the simultaneous use of Script (since two email notifications had the same timestamp) and recently learned about the Lock Service .
I use this post to make sure that I have the correct understanding Lockand how to implement it, to prevent the appearance of entries that do not appear in the Google Sheet due to the simultaneous use of Script.
Implementation
Pseudocode of my script:
Code.gs
var active_spreadsheet = SpreadsheetApp.openById("bbb");
var lock = LockService.getScriptLock();
try {
lock.waitLock(30000);
} catch (e) {
Logger.log('Could not obtain lock after 30 seconds.');
}
var active_sheet = active_spreadsheet.getSheetByName("ENTRIES");
var new_start_row = active_sheet.getLastRow() + 1;
SpreadsheetApp.flush();
lock.releaseLock();
return;
Questions
01) whether implementation are correct LockService, getScriptLock(), waitLock()and releaseLock()?
02) Is it recommended to use SpreadsheetApp.flush(), and if this is true, then the correct implementation?
Terminology (for reference)
from: https://developers.google.com/apps-script/reference/lock
Lock:
.
LockService:
.
Lock 4 :
hasLock()
Boolean, true, .
releaseLock()
void, , , .
tryLock (timeoutInMillis)
Boolean, , - .
waitLock (timeoutInMillis)
void, , - .
LockService 3 :
getDocumentLock()
, , .
getScriptLock()
, , .
getUserLock()
Lock, , .