我在另一个2D数组的末尾添加2D数组时遇到了问题。
第一个名为array_from_sheet的数组是通过读取Google表格中的一系列单元格构建的,范围为5列和100行。第一个数组在调试窗口中如下所示: [[" 2017-12-02T16:49:48.9Z",1040036,399.07,0.01," sell"],[" 2017-12-02T16:49:48.9 Z",1040037,399.08,1.12907707,"出售"],[....
第二个名为trades的数组来自我解析的JSON字符串,在调试窗口中看起来像这样(它有100行和5列): [[" 2017-12-02T18:06:55.909Z",1040574," 399.00000000"," 1.12619681"," sell" ],[" 2017-12-02T18:06:55.829Z",1040573," 399.00000000"," 1.31054161","出售&#34 ;],[...
这是在我的splice命令之前:trades.splice(1,0,array_from_sheet);
在我的splice命令之后,交易数组在调试窗口中如下所示: [[" 2017-12-02T18:06:55.909Z",1040574," 399.00000000"," 1.12619681"," sell" ],[[" 2017-12-02T16:49:48.9Z",1040036,399.07,0.01," sell"],[...
你可以看到第二行现在以双括号开头,我想这个问题。
调试窗口显示它现在有101行,而它应该有200行。新数组的第二行现在有100个元素,看起来像array_from_sheet被插入到这里而不是在同一行。
(我尝试过.push命令,但遇到同样的问题)
(array_from_sheet.length返回100,所以看起来是100行)
这是我的代码:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("trades");
first.activate();
//var sheet = SpreadsheetApp.openById('DEV_GDAX Order book and trades')
//var APIPullSheet = sheet.getSheetByName("APIPull");
//first.getRange('A2:D19999').clearContent();
// call the trades from GDAX into var parcedData- should add error handling here if server does not respond or return error code
var url = "https://api.gdax.com/products/ETH-EUR/trades"
var responseAPI = UrlFetchApp.fetch(url)
var parcedData = JSON.parse(responseAPI.getContentText());
// creates new empty array trades, adds headers on first row,
var trades = new Array ();
//trades.push(['time', 'trade_id', 'price', 'size','side']) hll: i removed this line as i don't need headers, i have them on row1 in sheet
// create temp array for a row, read from parsedData, push to temp, repeat, then push to temp (more or less)
var keys = ['time', 'trade_id', 'price', 'size','side'];
for (var i in parcedData) {
var temp = [];
for (var j in keys) {
temp.push(parcedData[i][keys[j]]);
}
trades.push(temp);
//Logger.log(trades)
}
//here i should remove duplicates first before coyping to sheet trades
//load current trades on sheet in temp array
var lastRow = first.getLastRow();
var array_from_sheet = first.getRange('A2:E' + lastRow).getValues();
Logger.log("array-from-sheet: ");
Logger.log(array_from_sheet);
//append new trades array to array_from_sheet
trades.splice(1, 0, array_from_sheet);
答案 0 :(得分:0)
解决:
我使用了这个命令:
trades_new = array_from_sheet.concat(交易);
而不是:
trades.splice(1,0,array_from_sheet);
现在trades_new包含端到端的2个数组。