How do I use setValues to paste the data I got from getValues? Google Spreadsheet

How do I use setValues to paste the data I got from getValues? Google Spreadsheet

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 dont want the number of columns to be anything other than 1, then theres 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);

Short answer

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); 

Explanation

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);

How do I use setValues to paste the data I got from getValues? Google Spreadsheet

Leave a Reply

Your email address will not be published.