I am trying to loop through a range of data, look for a certain value in the first column of data and then copy all rows with that value into another sheet called "Closed Requests"
function copyToClosed(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Open Requests");
var range = sheet.getActiveCell();
var lastrow = sheet.getLastRow();
var datarange = sheet.getRange(10,2,lastrow-1, 50).getValues();
var row = 10;
var x = [];
var sheetNameToMoveTheRowTo = "Closed Requests"
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
for (i=0;i<datarange.length;i++) {
if(i[0] == "CC") {
x.push([datarange[i]])
}
var targetRange =
targetSheet.getRange
(targetSheet.getLastRow()+1,2,datarange.length,datarange[0].length)
targetRange.setValues(x)
}
}
As you can see I first define the datarange and create an array "x". Then I loop through the data to find the text "CC" in the first column of the range. Then I push those rows into the array and copy them onto the sheet "Closed Requests" by using .setValues(). I get the error message at .setValues()
Browsing through other occurrences of this error I figured that there must be a problem with the dimension of my array and that I incorrectly defined either the array or the range. However I cannot figure out what exactly there seems to be the issue.
Any help would be appreciated.
The errors are in the for loop. Try this:
function copyToClosed(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Open Requests");
var range = sheet.getActiveCell();
var lastrow = sheet.getLastRow();
var datarange = sheet.getRange(10,2,lastrow-1, 50).getValues();
var row = 10;
var x = [];
var sheetNameToMoveTheRowTo = "Closed Requests"
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
for (i=0;i<datarange.length;i++) {
if(datarange[i][0] == "CC") {
x.push(datarange[i])
}}
targetSheet.getRange(targetSheet.getLastRow()+1,2,x.length,x[0].length).setValues(x)
}