title: Google Apps Script: create sequential ID
body: <p>I'm using Google Spreadsheet as a basic database. The one-to-many relationship in question here is a "Leads" sheet and a "Comms" sheet. Each lead can have a number of communications (emails, tweets, phone calls, etc.)</p>
  
  <p>When a user fills in a Google Form, I have a script that inserts some of the data from the associated form sheet into the "Leads" sheet, and also emails the user:</p>
  
  <pre><code>function formSubmitReply(e) {
   srcSheet.getRange(srcLastRow, 1).copyTo(leadsSheet.getRange(leadsLastRow + 1, 1));     // Copy timestamp
   srcSheet.getRange(srcLastRow, 6).copyTo(leadsSheet.getRange(leadsLastRow + 1, 5));     // Copy first name
   srcSheet.getRange(srcLastRow, 7).copyTo(leadsSheet.getRange(leadsLastRow + 1, 6));     // Copy surname
   leadsSheet.getRange(leadsLastRow + 1, 3).setValue("Web - Unknown");                    // Mark as WEB lead
   leadsSheet.getRange(leadsLastRow + 1, 4).setValue("New");                              // Set status to NEW
   // Email the user ...
  }
  </code></pre>
  
  <p>I also want to generate an ID so I can tie comms back to a given lead. What's the best way to do this? Ideas I've thought of so far include generating a very long GUID, or using a sequential ID, e.g. L1, L2, L3, etc.</p>
  
  <p>I prefer the latter idea for readability and to give a better insight into the data, but</p>
  
  <p>a) what code would I use to find the last ID with the L prefix, and increment this?</p>
  
  <p>b) what's the best way to prevent incrementing the ID incorrectly in the case where I re-sort the sheet?</p>