I can't seem to wrap my head around 2D arrays. I have set up the following code. but I'm unable to get setValues to work (the end of the code.)
I'm pretty sure I need to set it up as an array, but every way I've tried to set it up has resulted in a different error message. How do I properly set up the array so that all of the students' data for each class (k) gets sent to each class tab?
function onSearch() {
var sheetID = DriveApp.getFilesByName('Dash').next().getId();
var ss = SpreadsheetApp.openById(sheetID);
var sheet = ss.getSheetByName("Master List");
//Loop through Class List, get Course Codes
var classes = ss
.getSheetByName("sheetInfo")
.getRange(1, 2, 3)
.getValues();
//Loop through courses
for (var k = 0; k < classes.length; k++) {
var classCode = classes [k];
var searchVal = classCode;
var column = 18;
//Get Course Sheets
var classSheetArray = ss.getSheetByName("sheetInfo")
.getRange(1, 1, 3)
.getValues();
var classSheetName = classSheetArray [k];
var classSheet = ss.getSheetByName(classSheetName);
//Insert headers into Class sheets
sheet.getRange(1, 1, 1, 17).copyTo(classSheet.getRange(1, 1, 1, 17));
//Loop through 9 different course codes from student timetable
for (var j = 0; j < 9; j++) {
var searchCol = sheet.getRange(2, column+j, sheet.getLastRow())
.getValues();
for (var i = 0, len = searchCol.length; i < len; i++) {
//Take the data from the search and place it in the corresponding class tab.
//This data will have 17 columns and the number of rows is dependent on the number of returned students.
if (searchCol[i][0] == searchVal) {
var lastRow = classSheet.getLastRow();
var sourceInfo = sheet.getRange(i+2, 1, 1, 17)
.getValues();
tempArray = [];
tempArray.push(sourceInfo[i]);
Logger.log(tempArray); // WHEN I LOG THE ARRAY THE DATA LOOKS LIKE [[col 1, col 2 ...]][[col 1, col 2 ...]]...
classSheet.getRange(lastRow+1,1,1,sourceInfo.length) //DEFINITELY KNOW THIS TO BE WRONG...
.setValues(tempArray[0][i]);
}
}
}
}
}
Currently, in your code, the length of sourceInfo
is always 1:
var sourceInfo = sheet.getRange(i+2, 1, 1, 17)
.getValues();
The syntax for getRange
with 4 arguments is:
getRange(start row, start column, number of rows, number of columns)
The number of rows that you are getting is 1. You are getting 17 columns, but the outer array length is 1. The inner array length is 17.
So, you could use:
classSheet.getRange(lastRow+1,1,1,sourceInfo[0].length)
Note the index of [0]
after sourceInfo
: sourceInfo[0]
That will return a length of 17 for the number of columns.
If you don't want the number of columns to be anything other than 1, then there's no point of using arrayName.length
in the parameter.
Your are using sourceInfo
to set the number of columns, but then writing tempArray
. You should use tempArray
to set the parameter values. Use what ever array that is has the data to set the parameters.
classSheet.getRange(lastRow+1,1,tempArray.length,tempArray[0].length)
.setValues(tempArray);
Instead of
classSheet.getRange(lastRow+1,1,1,sourceInfo.length).setValues(tempArray[0][i]);
try
classSheet.getRange(lastRow+1,1,1,sourceInfo.length).setValues(tempArray);
IF tempArray
is a 2D array, then tempArray[0][i]
will return a array element instead of a 2D array and setValues(values)
requires a 2D array.
From https://developers.google.com/apps-script/reference/spreadsheet/range#setvaluesvalues
setValues(values)
Sets a rectangular grid of values (must match dimensions of this range).
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; // The size of the two-dimensional array must match the size of the range. var values = [ [ "2.000", "1,000,000", "$2.99" ] ]; var range = sheet.getRange("B2:D2"); range.setValues(values);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With