function onFormSubmit(e) {
// Multiple scripts can run this at the same time since they're not modifying any shared resource
var username = e.values[0];
var date = e.values[1];
var sheet = SpreadsheetApp.openById('id of my target spreadsheet');
// get the lock, because we're now modifying the shared resource
var lock = LockService.getPublicLock();
lock.waitLock(30000);
var dataRange = sheet.getDataRange();
var lastRow = dataRange.getLastRow();
var newRow = sheet.getRange(lastRow + 1, 1);
newRow.setValue("foo " + username + " " + date);
// clean up and release the lock
lock.release();
}
// Get a public lock, try to get exclusion, waiting a maximum of 10 seconds (10000ms)
var lock = LockService.getPublicLock();
var hasMutex = lock.tryLock(10000);
// Get a public lock, try to get exclusion, waiting a maximum of 10 seconds (10000ms)
var lock = LockService.getPublicLock();
lock.waitLock(10000); // throws an error if lock could not be obtained
hasLock()
ロックオブジェクトが現在ロックを保持しているかをテストする
戻り値 :
Type
説明
Boolean
ロックオブジェクトが相互排他ロックを保持している場合はtrue
サンプル :
var lock = LockService.getPublicLock();
if (lock.hasLock()) {
// opportunistically do stuff here because I have it
// or acquire the lock if necessary
}
// Get a public lock, read a spreadsheet value, increment it, write out the new value, and then release the lock
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange('a1');
var lock = LockService.getPublicLock();
lock.waitLock(20000);
cell.setValue(Number(cell.getValue()) + 1);
SpreadsheetApp.flush();
lock.releaseLock();