javascriptgoogle-sheetsgoogle-apps-scriptweb-applications

Multiple submission at exact same time in Google App Script


I created a form using Google App Script and send all the data in Google Spreadsheet.

My GS CODE:

function doGet(e){
  return HtmlService.createTemplateFromFile("Form1");
}

my html code just a simple input tag and whenever users submit it it will append row in Google Spreadsheet.

Here is my code:

function executeProgram(agentSub){
  var ss = SpreadsheetApp.openByUrl(url);
  var sheet = ss.getSheetByName("Submission");

  var timeStamp = new Date();
  sheet.appendRow([timeStamp,name,age]);
}

Now the code working fine if there are only one people submit the form. But if 2 people submit it at the exact same time, it will only create 1 row instead of 2. Look like the second is overlapped with the first one. But if the second person submitted 1 second after the first person, the code running fine(created 2 row). What am I missing here, and how to solve it?

Thanks!


Solution

  • Answer

    Lock Service allows scripts to prevents concurrent access to sections of code. This can be useful when you have multiple users or processes modifying a shared resource and want to prevent collisions.

    Lock Service has three different methods depending on the restrictions, in this case you need to use getScriptLock() to make sure that the code section cannot be executed simultaneously regardless of the identity of the user.

    Step by step