title: Google Apps Script Spreadsheet Auto Increment
body: <p>In Spreadsheet, If I select ST on 'Trial' column C0001-ST1 will appear in 'Code' column then if I select SP in 'Trial' column C0002-SP1 will appear in 'Code' column then if I again select ST in 'Trial' column C0003-ST2 will appear in 'Code' column.</p>
  
  <p><strong>Step 1:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/vSUQn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vSUQn.png" alt="Step 1"></a></p>
  
  <p><strong>Step 2</strong></p>
  
  <p><a href="https://i.stack.imgur.com/lPHhS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lPHhS.png" alt="Step 2"></a></p>
  
  <p>This is the code that I've now to achieve this.  </p>
  
  <pre><code>function incTrialCode(sectionCode) { 
      if (sectionCode == ""){ return null; } 
  
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getActiveSheet();
      var range = sheet.getDataRange();
      var values = range.getValues();
      for (i=1; i&lt;values.length; i++)
      { 
          trial = values[i][1];
      } 
      return sectionCode + values;
   }
  </code></pre>