I keep getting an error message when using the setValues() function to export an array to Google sheets. I have tried many different methods for creating my 2D array, but I still keep getting the same errors. Sometimes my code will run (the array will export to the spreadsheet) but I will still get an error.
I originally used the setValue() function in a for loop but the code would time out because it ran too long. So I tried dumping all my data into a 2D Array and feeding that to the spreadsheet all at once.
Tried creating the Array as an empty 2D array
var outputArray = [[]]
and using .push to populate the data into it
Tried creating the empty Array using the function:
function create2DArray(rows) {
var arr = [];
for (var i=0;i<rows;i++) {
arr[i] = [];
}
return arr;
}
and adding the data in by rows (inside of a for loop that iterates by rowNumber)
outputArray[rowNumber] = [data1, data2, data3,...]
Used the same function above for creating empty array and created intermediate array and then put that into output array
outputArrayIntermediate[0] = data1;
outputArrayIntermediate[1] = data2;
outputArrayIntermediate[2] = data3;
outputArrayIntermediate[3] = data4;...
outputArray[rowNumber] = outputArrayIntermediate;
Here is where the error keeps happening
var setRows = outputArray.length;
var setColumns = outputArray[0].length
revenueSheet.getRange(2,1,setRows,setColumns).setValues(outputArray);
When I include the setColumns variable I get the error: "The number of columns in the data does not match the number of columns in the range. The data has 0 but the range has 11." This will still populate the data to the spreadsheet.
When I do not include the setColumns variable I get the error: "The number of columns in the data does not match the number of columns in the range. The data has 11 but the range has 1."
Logger.log(outputArray);Menu 'View' > Logs - Chris