function onFormSubmit(e) { // Grab the e.values as soon as they arrive var submittedDataLocal = []; var submittedDataLocal = e.values; // set up the response and test sheet objects and find the last used rows in each var shtResp = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form responses 1"); var shtTest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test"); var LastRowResp = shtResp.getLastRow(); var LastRowTest = shtTest.getLastRow(); // get the first seven cells of the row that's just been submitted from the response sheet var tmpRespLine = shtResp.getRange(LastRowResp,1,1,7).getValues(); // write it to the next available line in the test sheet and make it pink shtTest.getRange(LastRowTest+1,1,1,7).setValues(tmpRespLine); shtTest.getRange(LastRowTest+1,1,1,7).setBackground("pink"); // Now write the values found in e.values var submittedDataLocallength = submittedDataLocal.length; shtTest.getRange(LastRowTest+2,1,1,submittedDataLocallength).setValues([submittedDataLocal]);function onFormSubmit(e) {
// Grab the e.values as soon as they arrive var submittedDataLocal = []; var submittedDataLocal = e.values;
if (e.values[1] == "") { // This must be a spuriouse trigger // set up the sheet objects and find the last used row var shtTest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Spurious onFormSubmit Triggers"); var LastRowTest = shtTest.getLastRow(); // Now write the values found in e.values - nothing except for the timestamp var submittedDataLocallength = submittedDataLocal.length; var submittedDataLocallength = submittedDataLocal.length; shtTest.getRange(LastRowTest+1,1,1,submittedDataLocallength).setValues([submittedDataLocal]); return; } // Set lock so that another onFormSubmit can't fire until we are finished // Log the time it takes to get a lock var lock = LockService.getScriptLock(); var success = lock.tryLock(30000); var nowTimeStamp = new Date(); if (!success) { Logger.log('Could not obtain lock after 30 seconds.'); UpdateHistorySheet(nowTimeStamp,"onSubmit","Unable to lock","","", "",""); sendAdminEmail("LockTimeout ", 0, submittedDataLocal[0], ""); } else { UpdateHistorySheet(nowTimeStamp,"onSubmit","Time to get lock",nowTimeStamp - startTime ,"", "",""); }
// processing here...
//Tidy up... SpreadsheetApp.flush(); lock.releaseLock();)function onFormSubmit(e) { // Set lock so that another onFormSubmit can't fire until we are finished var lock = LockService.getScriptLock(); var success = lock.tryLock(30000); var nowTimeStamp = new Date(); if (!success) { // Could not obtain lock after 30 seconds UpdateHistorySheet(nowTimeStamp,"onSubmit","Unable to lock","","", "",""); sendAdminEmail("LockTimeout ", 0, e.values[0], ""); } else { // Grab the e.values as soon as they arrive var submittedDataLocal = []; var submittedDataLocal = e.values; if (e.values[1] == "") { // This must be a spurious trigger - log it in spreadsheet and send an Admin email // No further processing done // set up the test sheet object and find the last used row var shtSoT = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Spurious onFormSubmit Triggers"); var LastRowSOT = shtSoT.getLastRow(); // Now write the values found in e.values - that will be nothing except for the timestamp var submittedDataLocallength = submittedDataLocal.length; shtSoT.getRange(LastRowSOT+1,1,1,submittedDataLocallength).setValues([submittedDataLocal]); sendAdminEmail("SpuriousTrigger", 0, submittedDataLocal[0], ""); } else { // Now we can process the data } } //Tidy up... SpreadsheetApp.flush(); lock.releaseLock(); } // End of onFormSubmitfunction onFormSubmit(e) { // Set lock so that another onFormSubmit can't fire until we are finished var lock = LockService.getScriptLock(); var success = lock.tryLock(30000); var nowTimeStamp = new Date(); if (!success) { // Could not obtain lock after 30 seconds UpdateHistorySheet(nowTimeStamp,"onSubmit","Unable to lock","","", "",""); sendAdminEmail("LockTimeout ", 0, e.values[0], ""); } else { // Grab the e.values as soon as they arrive var submittedDataLocal = []; var submittedDataLocal = e.values; if (e.values[1] == "") { // This must be a spurious trigger - log it in spreadsheet and send an Admin email // No further processing done // set up the test sheet object and find the last used row var shtSoT = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Spurious onFormSubmit Triggers"); var LastRowSOT = shtSoT.getLastRow(); // Now write the values found in e.values - that will be nothing except for the timestamp var submittedDataLocallength = submittedDataLocal.length; shtSoT.getRange(LastRowSOT+1,1,1,submittedDataLocallength).setValues([submittedDataLocal]); sendAdminEmail("SpuriousTrigger", 0, submittedDataLocal[0], ""); } else { try { // Now we can process the data } catch (catchError) { // do some logging about the error //Tidy up... SpreadsheetApp.flush(); lock.releaseLock(); return;function onFormSubmit(e) { try {
// Set lock so that another onFormSubmit can't fire until we are finished var lock = LockService.getScriptLock(); var success = lock.tryLock(30000); var nowTimeStamp = new Date(); if (!success) { // Could not obtain lock after 30 seconds UpdateHistorySheet(nowTimeStamp,"onSubmit","Unable to lock","","", "",""); sendAdminEmail("LockTimeout ", 0, e.values[0], ""); } else { // Grab the e.values as soon as they arrive var submittedDataLocal = []; var submittedDataLocal = e.values; if (e.values[1] == "") { // This must be a spurious trigger - log it in spreadsheet and send an Admin email // No further processing done // set up the test sheet object and find the last used row var shtSoT = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Spurious onFormSubmit Triggers"); var LastRowSOT = shtSoT.getLastRow(); // Now write the values found in e.values - that will be nothing except for the timestamp var submittedDataLocallength = submittedDataLocal.length; shtSoT.getRange(LastRowSOT+1,1,1,submittedDataLocallength).setValues([submittedDataLocal]); sendAdminEmail("SpuriousTrigger", 0, submittedDataLocal[0], ""); } else { // Now we can process the data } }
} catch (catchError) {
// call error logging function function onFormSubmit(e) { try {
if (!LockService.getScriptLock().tryLock(2000)) {//Try to get a lock and if not successful it returns false
Utilities.sleep(1000);//Wait x seconds
if (!LockService.getScriptLock().tryLock(2000)) {//Try to get a lock and if not successful it returns false
//Decide what you want to do - do nothing / let it run - There is a risk but maybe the code will be successful - or stop the process, and notify someone that there is a problem
}
} var nowTimeStamp = new Date(); if (!success) { // Could not obtain lock after 30 seconds UpdateHistorySheet(nowTimeStamp,"onSubmit","Unable to lock","","", "",""); sendAdminEmail("LockTimeout ", 0, e.values[0], ""); } else { // Grab the e.values as soon as they arrive var submittedDataLocal = []; var submittedDataLocal = e.values; if (e.values[1] == "") { // This must be a spurious trigger - log it in spreadsheet and send an Admin email // No further processing done // set up the test sheet object and find the last used row var shtSoT = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Spurious onFormSubmit Triggers"); var LastRowSOT = shtSoT.getLastRow(); // Now write the values found in e.values - that will be nothing except for the timestamp var submittedDataLocallength = submittedDataLocal.length; shtSoT.getRange(LastRowSOT+1,1,1,submittedDataLocallength).setValues([submittedDataLocal]); sendAdminEmail("SpuriousTrigger", 0, submittedDataLocal[0], ""); } else { // Now we can process the data } }
//Tidy up... SpreadsheetApp.flush(); LockService.getScriptLock().releaseLock(); } catch (errorObject) { LockService.getScriptLock().releaseLock();//Make sure the lock gets released if there is an error
// call error logging function
}} // End of onFormSubmit