title: setValues after the last non-empty row of a specific column in all possible scenarios
body: <p>Let's say the column of interest is Column <code>A</code> and I always want to add values in the first row after the last row with values or in the first row if there are no values in the column of interest..</p>
  <p>Spreadsheets can deliver several scenarios, they are (if there are others that I haven't listed, remind me in the comments):</p>
  <ol>
  <li>No values in any column (all empty cells):<br />
  <a href="https://i.stack.imgur.com/G90es.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G90es.png" alt="enter image description here" /></a></li>
  <li>Non-empty cells in other columns but not in the column that interest you:<br />
  <a href="https://i.stack.imgur.com/UpFu3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UpFu3.png" alt="enter image description here" /></a></li>
  <li>Non-empty cells in other columns and in the column that interest you:<br />
  <a href="https://i.stack.imgur.com/wCujH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wCujH.png" alt="enter image description here" /></a></li>
  <li>Non-empty cells in sequence from the first row in the column you are interested in:<br />
  <a href="https://i.stack.imgur.com/wObTt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wObTt.png" alt="enter image description here" /></a></li>
  <li>Non-empty cells from the first row in the column you are interested in but with empty cells between the non-empty cells:<br />
  <a href="https://i.stack.imgur.com/Q0BuT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q0BuT.png" alt="enter image description here" /></a></li>
  <li>First cell of the column that interests you being empty but other non-empty cells existing in that same column:<br />
  <a href="https://i.stack.imgur.com/7FUEz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7FUEz.png" alt="enter image description here" /></a></li>
  </ol>
  <p>To avoid the crashes that happen in each of these scenarios I've put together these two models that work correctly.</p>
  <h1>Using the Google Sheets API:</h1>
  <p>(<a href="https://i.stack.imgur.com/wCujH.png" rel="nofollow noreferrer">Need enable the Google Sheets API advanced service</a>)</p>
  <pre class="lang-javascript prettyprint-override"><code>function gsapi() {
    var sheet_id = 'XXXXXXXXXX';
    var sheet = SpreadsheetApp.openById(sheet_id);
    var sheet_page = sheet.getSheetByName('STACKTEST');
  
    first_row = 1;
    
    var Avals = Sheets.Spreadsheets.Values.get(sheet_id, 'STACKTEST!A' + first_row + ':A').values;
    if (Avals != undefined) {
      var Alast = Avals.length + 1;
    } else {
      var Alast = first_row;
    }
    var to_sheet = [
      ['A','B','C','D','E']
    ];
    sheet_page.getRange(Alast, 1, to_sheet.length, to_sheet[0].length).setValues(to_sheet);
  }
  </code></pre>
  <h1>Using Basic Google Apps Script:</h1>
  <pre class="lang-javascript prettyprint-override"><code>function gasbasic() {
    var sheet_id = 'XXXXXXXXXX';
    var sheet = SpreadsheetApp.openById(sheet_id);
    var sheet_page = sheet.getSheetByName('STACKTEST');
  
    first_row = 1;
    
    const lrow = sheet_page.getLastRow();
  
    if (lrow &gt; 0) {
      var last = lrow;
    } else {
      var last = 1;
    }
    
    const Avals = sheet_page.getRange(&quot;A&quot; + first_row + &quot;:A&quot; + last).getValues();
    const reverse = Avals.reverse().findIndex(c=&gt;c[0]!='');
    
    if (reverse != -1) {
      var Alast  = last - reverse + first_row;
    } else {
      var Alast  = first_row;
    }
  
    var to_sheet = [
      ['A','B','C','D','E']
    ];
    sheet_page.getRange(Alast, 1, to_sheet.length, to_sheet[0].length).setValues(to_sheet);
  }
  </code></pre>
  <p>Is there a method that manages to circumvent all these possible scenarios in a more professional way?</p>
  <p>It seems a bit archaic to need <code>if</code> or <code>try + catch</code> to work around failures.</p>