I am trying to make the LockService work across different Spreadsheets. I do not even know if that is possible as I it seems to be used for users within the same function, right?
I made some example sheets to demonstrate what I mean. Currently there is a small possibility that my users generate the same number. Is there any way to alleviate this?
User Sheet 1 // Here the user shall press the button to make the number go up in the Database. https://docs.google.com/spreadsheets/d/1P0_UQc02MRxHq_yFegjCc2UGmzRbkn2gxR8dw6yKvLY/edit#gid=0
User Sheet 2 // Here the user shall press the button to make the number go up in the Database. https://docs.google.com/spreadsheets/d/1Ua8mdZ-IBwcAt2AO9yuoa8PHiXlIGWfxPRx7rP9OXXA/edit#gid=0
Database Sheet: Here the number shall go up by one if users from sheet 1 or 2 press their button. https://docs.google.com/spreadsheets/d/146OaFXGInuzMduvQDshh5ZDlsKf--eOqJUfa3Hrt_To/edit#gid=0
function Lock() {
// Generates a unique ticket number for every form submission.
var lock = LockService.getScriptLock();
var ratenprogramm = SpreadsheetApp.openByUrl(
'https://docs.google.com/spreadsheets/d/146OaFXGInuzMduvQDshh5ZDlsKf--eOqJUfa3Hrt_To/edit'); // Leads to Lockscript 3
var main = ratenprogramm.getSheetByName("Main");
// var raten = SpreadsheetApp.getActiveSpreadsheet();
var targetCell = main.getRange("A2").getValue();
// Get a script lock, because we're about to modify a shared resource.
Utilities.sleep(8000);
// Wait for up to 10 seconds for other processes to finish.
lock.waitLock(20000);
main.getRange("A2").setValue(targetCell + 1);
// Release the lock so that other processes can continue.
lock.releaseLock();
}