One challenge I'm facing is with a javascript function that generates an array filled with objects. Each object contains various attributes such as id, colour, and size. After the array is created, it is sent to a Google Apps Script for further processing.
Upon receiving the array in the Apps script function, my goal is to iterate through each object using a for loop and append the object's attributes to a spreadsheet by adding new rows. However, I'm unsure of how to reference the attributes of an object within an array while inside the for loop.
Below is a snippet of the code:
/*
the array is structured like this
arrayOfObjects = [
object1
object1.id = 1
object1.colour = 'red'
object1.size = 5
,
object2
object2.id = 2
object2.colour = 'blue'
object2.size = 2
and so on...
]
*/
function appendRowsToSS(arrayOfObjects){
var url = "myspreadsheetID";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("mysheet");
for (i = 0; i < arrayOfObjects.length; i++) {
//Here I want to get the attributes of each object in the array and append them to a new row
ws.appendRow([ arrayOfObjects[i].id , arrayOfObjects[i].colour , arrayOfObjects[i].size ]); //Is this how I reference them?
}
}
//I hope that the first row appended to the ss will be (1 , red , 5) and the next row will be (2 , blue , 2)
Thank you in advance for your assistance!