title: LockService: How do I lock a Google sheet from within a script?
body: <p><br>
  I'm using a Google spreadsheet to store contact info I collect from a website (GDPR compliant, don't worry) and I synchronize this sheet with Mailchimp using a custom App script, which runs in the background.</p>
  
  <p>More in detail, I'm using a temporary sheet as a buffer which is filled with the data users send via the site's forms (I'm using Contact Form 7 Google Sheets Connector). My script runs each time the event INSERT_ROW is triggered in the "buffer" sheet. It analyzes the data, stores them in the final sheet and, eventually, it emptys the "buffer".<br>
  Now, aware of potential concurrency issues I've written the code in the following form:</p>
  
  <pre><code>function onChange(e) {
    // Script initializaztion
  
    if (!formNames.includes(formName) || e.changeType != "INSERT_ROW") return;
  
    let lock = LockService.getScriptLock();
  
    try {
      lock.waitLock(60000);
  
      // Get new stuff from Form sheet
      .
      .
      .
  
      // Get existing data and compare with new
      compareData();
  
      // New entries
      insertNewDataIntoFinalSheet();
  
      // Updates
      updatesExistingDataInFinalSheet();
  
      // Clean up the buffer sheet
      deleteRowsFromSiteForms();
  
      console.log(formName);
  
    } catch(e) {
      console.log(e);
    }
  
    lock.releaseLock();
  }
  </code></pre>
  
  <p>But I'm not sure the Lock is doing what I'm expecting it to do. And Google Docs are not so clear about its use.<br>
  If I'm not understanding Google docs wrong, any Lock is </p>
  
  <blockquote>
    <p>preventing sections of code from running concurrently.
    This 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 (Google docs).</p>
  </blockquote>
  
  <p>Now Imagine the following scenario: User A in his browser fills in the form. At the very same time (well, almost) User B is doing the same in another browser somewhere else in the world. The CF7 connector of User A's browser sends data to my "buffer" sheet and it gets the lock so that my script begins its work. A nanosecond later, users B's form inserts other data into the "buffer" triggering thus a script execution. This new script instance tries to acquire the lock but is blocked by user A's lock.</p>
  
  <p>In the mean time, User A's script instance is finishing its job and cleans up everything, <em>deleting also the data input by User B</em>! When user A's script instance releases the lock, user B's script kicks in, but it finds its data swept away by user A's script instance.</p>
  
  <p>Ideally, what I need is a way to "freeze" any modification to the "buffer" sheet (including insertion of new rows) until each script instance has finished its job.</p>
  
  <p>But as far as I understand, and also judging by some strange errors I have in my logs, this is not what getScriptlock() is doing (and getUserlock() or getDocumentlock() neither).</p>
  
  <p>Can anyone help me understanding how I should use Google LockService for that?</p>
  
  <p>Thank you in advance </p>
  
  <h2>EDIT</h2>
  
  <p>Those who have commented my post (@TheMaster in particular) have caught the very core of the problem, so let me add some further information/thought.  This is the the error I'm getting (sometimes) in my Stackdriver Log</p>
  
  <p><a href="https://i.stack.imgur.com/gWlSG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gWlSG.png" alt="Error log from Stackdrive"></a></p>
  
  <p>Apparently, my script is trying to access data when it is not supposed to. I suspect it is the scenario I've depicted: the second instance of the script finds the "buffer" emptied by the first instance.</p>
  
  <p>As @TheMaster says, the problem is that there's no way to really "lock" a document if the document is accessed via some external resource, like Zapier, for example, or, as in my case, a Wordpress connector, or any API request (Google developers, shouldn't you address this issue?).   </p>