How can I eliminate the exception errors when conditions aren't met below.
My script to move a row from one tab to another works correctly when the condition is met, but when it's not met I get the following error. "Exception: The number of rows in the range must be at least 1. CIO2365 @ Code.gs:6". I have the script set up to a trigger, so would like to eliminate the error if possible.
function CIO2CIO(){
const sh = SpreadsheetApp.getActive();
const ss = sh.getSheetByName("Response Landing");
const outSheet = sh.getSheetByName('CIO to CIO');
let data = ss.getRange(2,1,ss.getLastRow()-1,21).getValues();
let out = [];
for (let i = 0; i < data.length; i ){
if (data[i][2] == 'CIO to CIO'){
out.push(data[i]);
ss.deleteRow(i 2);
data.splice(i,1);
i--;
}else{return}
}
outSheet.getRange(outSheet.getLastRow() 1,1,out.length,21).setValues(out);
}
CodePudding user response:
When I saw your script and error message, I thought that the reason for your error might be due to ss.getLastRow()
being smaller than 1. If my understanding is correct, how about the following modification?
From:
let data = ss.getRange(2,1,ss.getLastRow()-1,21).getValues();
To:
const lastRow = ss.getLastRow();
if (lastRow <= 1) return;
let data = ss.getRange(2, 1, lastRow - 1, 21).getValues();