google-apps-script multiple criteria writing over headers <p>I have taken a bit of script from Serge which is great (original <a href="http://stackoverflow.com/questions/25038715/google-apps-script-copy-row-from-table-if-value-in-table-meets-condition">link here</a>. I have added in a second criteria to exclude certain rows and it works great except, if there is not header in the sheet being copied to, it will not work (error: "The coordinates or dimensions of the range are invalid.") and if I enter a header or some other data, it overwrites it. Can anyone assist please? I have also found that is there is no match to the criteria I get following message "TypeError: Cannot read property "length" from undefined."</p> <p>Also, what change would I need to make to change the cell 'dataSheetLog[i][12]' to the status variable, i.e. "COPIED" after I have copied it across. I have tried writing a setValue line but it is obviously the wrong instruction for that syntax.</p> <p>Code is:</p> <pre><code> { var Spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var sheetLog = Spreadsheet.getSheetByName("LOG"); var sheetMaint = Spreadsheet.getSheetByName("MAINTENANCE"); var Alast = sheetLog.getLastRow(); var criteria = "08 - Maintenance" var status = "COPIED" var dataSheetLog = sheetLog.getRange(2,1,Alast,sheetLog.getLastColumn()).getValues(); var outData = []; for (var i in dataSheetLog) { if (dataSheetLog[i][2]==criteria &amp;&amp; dataSheetLog[i][12]!=status){ outData.push(dataSheetLog[i]); } } sheetMaint.getRange(sheetMaint.getLastRow(),1,outData.length,outData[0].length).setValues(outData); } </code></pre>