gpt4 book ai didi

javascript - setValues 不迭代数组

转载 作者:行者123 更新时间:2023-12-02 15:10:06 27 4
gpt4 key购买 nike

电子表格的 Google-apps 脚本

我有一个可变长度的对象数组。 [{}, {}]我使用 for 循环 (i=0; i<Obj.length; i++)迭代每个对象项并使用另一个 for 循环从键中获取值 (var p in Obj[i])对于对象。然后将其推送到一个空数组 - row =[] - 用迭代值填充 Obj[i][p] .

当它跳到下一个时Obj[i] ,行数组应该被推送到谷歌工作表,然后用 - row = [] 清空自身初始化 - 然后迭代下一个 Obj[i] .

Logger.log 出色地按预期记录:

[16-01-18 07:39:56:418 AEDT] [4 secs, , Mailbox, 7:05:47 AM, finished,     7:05:51 AM, 2016-01-17T20:05:47.142Z, 2016-01-17T20:05:51.891Z, MAILBOX]
[16-01-18 07:39:56:419 AEDT] []
[16-01-18 07:39:56:420 AEDT] pushed
[16-01-18 07:39:56:420 AEDT] [5 secs, , Adhoc, 7:05:51 AM, finished, 7:05:56 AM, 2016-01-17T20:05:51.892Z, 2016-01-17T20:05:56.896Z, ADHOC TASKS]
[16-01-18 07:39:56:421 AEDT] []
[16-01-18 07:39:56:422 AEDT] pushed
[16-01-18 07:39:56:422 AEDT] [3 secs, , Huddle, 7:05:56 AM, finished, 7:06:00 AM, 2016-01-17T20:05:56.897Z, 2016-01-17T20:06:00.187Z, HUDDLE]
[16-01-18 07:39:56:423 AEDT] []
[16-01-18 07:39:56:424 AEDT] pushed
[16-01-18 07:39:56:424 AEDT] [13 mins 3 secs, , Audits, 7:06:00 AM, finished, 7:19:03 AM, 2016-01-17T20:06:00.189Z, 2016-01-17T20:19:03.384Z, AUDITS]
[16-01-18 07:39:56:426 AEDT] []
[16-01-18 07:39:56:426 AEDT] pushed

但是在工作表中,它仅记录最后推送的行数组,并且始终记录两次,除非主 Obj 数组中只有一个对象项。

我做错了什么?代码如下:

function getThisToTrix(Obj) {
// Logger.log(Obj);
var doc = SpreadsheetApp.openById("_id"),
sheetName="abc",
sheet = doc.getSheetByName(sheetName),
col = [], row = [],
headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0],
nextRow = sheet.getLastRow()+1
;

for(var i = 0; i<Obj.length; i++){
for(var p in Obj[i]){
col.push(p);
row.push(Obj[i][p]);
}

Logger.log(row);
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);

row=[];
Logger.log(row);
pushed();
}
}

function pushed(){Logger.log("pushed");}

更新:执行记录:

[16-01-23 00:20:46:140 HKT] Sheet.getLastColumn() [0.02 seconds]
[16-01-23 00:20:46:141 HKT] Sheet.getRange([1, 1, 1, 9]) [0 seconds]
[16-01-23 00:20:46:165 HKT] Range.getValues() [0.023 seconds]
[16-01-23 00:20:46:263 HKT] Sheet.getLastRow() [0.097 seconds]
[16-01-23 00:20:46:264 HKT] Sheet.getRange([88, 1, 3, 9]) [0 seconds]
[16-01-23 00:20:46:265 HKT] Range.setValues([[[987 ms, , Mailbox, 12:20:38 AM, finished, 12:20:39 AM, 2016-01-22T16:20:38.351Z, 2016-01-22T16:20:39.338Z, MAILBOX], [862 ms, , Audits, 12:20:39 AM, finished, 12:20:40 AM, 2016-01-22T16:20:39.352...) [0 seconds]
[16-01-23 00:20:46:458 HKT] Execution succeeded [0.243 seconds total runtime]

最佳答案

nextRow 永远不会在您的 for 循环中更新,是吗?因此,您不断更改电子表格中的同一行。

关于javascript - setValues 不迭代数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34843695/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com