다른 2D 배열의 끝에 2D 배열을 추가하는 데 문제가 있습니다.다른 2D 배열에 2D 배열을 추가하면 대신에 단일 행으로 삽입됩니다.
array_from_sheet라는 첫 번째 배열 Google 시트의 셀 범위를 5 열 100 행의 범위에서 읽는 것으로 구성됩니다. 이 첫 번째 배열은 디버그 창에서 다음과 같습니다. [[ "2017-12-02T16 : 49 : 48.9Z", 1040036, 399.07, 0.01, "판매"], [ "2017-12-02T16 : 49 : 48.9Z" 트레이드라고하는 두 번째 배열은 내가 구문 분석 한 디버그 창 (100 개의 행과 5 개의 열을 가짐)에서 JSON 문자열로 표시됩니다. : [[ "2017-12-02T18 : 06 : 55.909Z", 1040574, "399.00000000", "1.12619681", "판매하다", [ "2017-12-02T18 : 06 : 55.829Z", 1040573, "399.00000000 ","1.31054161 ","sell "], [...
이것은 내 splice 명령 앞에 있습니다. trades.splice (1, 0, array_from_sheet);
내 스플 라이스 명령 후 거래 배열은 디버그 창에서 다음과 같이 표시됩니다. [[ "2017-12-02T18 : 06 : 55.909Z", 1040574, "399.00000000", "1.12619681", "sell"], [[ "2017-12-02T16 : 49 : 48.9Z", 1040036, 399.07, 0.01, "판매하다"] [...
이제 두 번째 줄이 이중 브래킷으로 시작하는 것을 볼 수 있습니다. .
디버그 창에 현재 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);