setValues doesn't iterate through the arrays
电子表格的 Google 应用脚本
我有一个可变长度的对象数组。
我使用 for loop
然后将其推送到一个空数组 -
当它跳转到下一个
Logger.log 惊人地按预期记录:
1 2 3 4 5 6 7 8 9 10 11 12 | [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 数组中只有一个 Object 项。
我做错了什么?这是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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");} |
更新:
执行记录:
1 2 3 4 5 6 7 | [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] |
看来您的问题实际上是如何将对象数组转换为正确的二维数组以传递给
我会更像这样的结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function convertToArray(objectArray) { var result = []; for(var o in objectArray){ var row = []; for(var property in objectArray[o]){ row.push(objectArray[o][property]); } result.push(row); } return result; } |
然后:
1 2 | var data = convertToArray(Obj); sheet.getRange(1,1,data.length, numberOfColumns).setValues(data); |
检查了执行记录,发现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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]); nextRow = sheet.getLastRow()+1; //Answer to this. row=[]; Logger.log(row); pushed(); } } function pushed(){Logger.log("pushed");} |
但仍然不确定为什么它会一直记录两次而不是一次,因为它每次都调用相同的行号。无论如何,不??再是我关心的问题了。