title: How do I use LockService properly every time in Google Apps Script?
body: <p>I am trying to avoid concurrent process with <code>LockService</code> on my project.</p>
  
  <p>In test runs, <code>LockService</code> works for me without any problems.</p>
  
  <p>But when I roll my project out, sometimes <code>LockService</code> doesn't seem to work properly and overwrite spreadsheet values.</p>
  
  <p>I used <code>tryLock()</code> and <code>hasLock()</code> methods to acquire a lock. </p>
  
  <pre><code>function test(array) {
    var lock = LockService.getScriptLock();
    if (lock.tryLock(500)) {
      if (!lock.hasLock()) {
        var message = "Another user is submitting form data. Please try again later.";
        return message;
      }
      var ss = SpreadsheetApp.openById(outputId);
      var sheet = ss.getSheetByName("sheet1");
      var lastRow = sheet.getLastRow() + 1;
      var range = sheet.getRange(lastRow, 1, 1, array[0].length);
      range.setValues(array);
      lock.releaseLock();
    }
    else {
      var message = "Another user is submitting form data. Please try again later.";
      return message;
    }
  }
  </code></pre>
  
  <p>I would like to avoid to set values in the same last row when users simultaneously submit data.
  But sometimes it seems to overwrite the values in the last row and one of the arrays' values are just gone.</p>
  
  <p>Could you point out my code's error?</p>
  
  <p>Thank you.</p>