我有一个javascript函数,它创建一个数组并用对象填充它。每个对象都有多个属性(id、颜色、大小等)。在函数的末尾,它将这个对象数组发送到Google Apps脚本。
当Apps脚本函数接收到这个数组时,我想使用一个for循环来遍历该数组,并通过附加新的行为每个对象添加对象的属性到电子表格中。我只是不知道如何在for循环中引用数组中的对象的属性。
代码示例:
/*
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)
提前感谢!
转载请注明出处:http://www.hanxingera.com/article/20230526/936209.html