Marvellanius
Marvellanius

Reputation: 11

Printing values in an object to columns in google script

I have an object filled via this piece of code:

for ( i = 1; i < array.length; i++){
  var num = array[i];
  result[num] = result[num] ? result[num]+1 : 1;
} 
Logger.log(result);

Now I want to print set te values of a column in my spreadsheet to the counted values. The data in the object looks like this:

{git-b-60=2.0, sax-c-12=6.0, ......}

And it continues on like this, and will get bigger in the future.

Now I also have a column with these course codes (the git-b-60 is a course code) in chronological order, and would love for the number values in the object to be printed in the column after the corresponding course code.

Is there a way to do it? Because I'm stumped. I can't seem to find the way to print values from an object to the columns.

Thanks in advance for any and all help!

Upvotes: 1

Views: 1365

Answers (1)

Alan Wells
Alan Wells

Reputation: 31300

You need to construct a 2 dimensional array and then use setValues(2D Array)

var outerArray = [],
    thisValue = "",
    innerArray = [];

for (var key in result) {
  innerArray = []; //Reset on every loop
  thisValue = result[key];
  innerArray.push(thisValue);
  outerArray.push(innerArray);
};

var numberOfRowsToWrite = outerArray.length;

SpreadsheetApp
  .getActiveSpreadsheet()
  .getSheetByName(name)
  .getRange(row to start at, column to start at, numberOfRowsToWrite, 1)
  .setValues(outerArray);

Upvotes: 1

Related Questions