title: Last column in a specific row with the error "The coordinates of the target range are outside the dimensions of the sheet."
body: <p>As the title says I want to get the last column in a specific row. I tried that with my function <code>lastColumnForRow(sheet, row)</code> which is pretty similar to my function <code>lastRowForColumn(sheet, column)</code>. However I get an error with <code>getLastColumn()</code> in <code>lastColumnForRow(sheet, row)</code> and <code>lastRowForColumn(sheet, column)</code> is working as intended. Does somebody know a better way or how to fix this problem?</p>
  
  <p>In my sheet there was a value in the last column, so I added a new column without values, but that didn't fix the issue. Also I've tried <code>getLastColumn()-1</code> but here's the same problem.</p>
  
  <pre><code>function lastRowForColumn(sheet, column){
    // Get the last row with data for the whole sheet.
  
    // Get all data for the given column
  
    // Iterate backwards and find first non empty cell
  
  }
  
  function lastColumnForRow(sheet, row){
    // Get the last column with data for the whole sheet.
    var numColu = sheet.getLastColumn();
  
    // Get all data for the given row
    var data2 = sheet.getRange(row, 1, 1, numColu).getValues();
  
    // Iterate backwards and find first non empty cell
    for(var j = data2.length - 1 ; j &gt;= 0 ; j--){
      if (data[0][j] != null &amp;&amp; data[0][j] != ""){
        return j + 1;
      }
    }
  }
  
  
  function Missionen() {
   var gros = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Großaufträge");
    var schab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Schablonen");
    var name = gros.getRange(9, 3).getValue();
    gros.getRange(12, lastColumnForRow(gros, 12)+1).setValue("Auftrag "+name);
  }
  </code></pre>
  
  <p>I tried to get the maximum amount of columns in a row and then iterate backwards to find the first non-empty cell. If I try to execute the code there always occurs the issue</p>
  
  <blockquote>
    <p>"The coordinates of the target range are outside the dimensions of the sheet. (line 18, file "Code")"</p>
  </blockquote>
  
  <p>based on the call <code>getLastColumn()</code>.</p>