我正在從 api 獲取資料到谷歌表。它的所有獲取都是單個單元格上的資料字串。如何將其格式化為帶有標題的表格。我得到的長字串的一部分,代碼如下。
字串 - {instantBuy=5679.95, pricechange=0, InstantSell=5623.39, 貨幣=AUD, 24hoursHigh=5848.86, market=5848.86, virtualCurrency=ETH, 24hoursLow=5848.86, sell=5848.87.84.1 交易量=5848.87.861. -澳元} ...
代碼:
function myQuotes() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainSheet = ss.getSheetByName('Sheet1');
var url = "https://www.zebapi.com/pro/v1/market/";
var response = UrlFetchApp.fetch(url);
var json = response.getContentText();
var data = JSON.parse(json);
mainSheet.getRange(4,2).setValue(data);
謝謝!
uj5u.com熱心網友回復:
使用Object.entries(),像這樣:
function myQuotes() {
const url = "https://www.zebapi.com/pro/v1/market/";
const response = UrlFetchApp.fetch(url);
const json = response.getContentText();
const data = transpose_(Object.entries(JSON.parse(json)));
const targetRange = SpreadsheetApp.getActive().getRange('Sheet1!B4');
targetRange.offset(0, 0, data.length, data[0].length).setValues(data);
}
function transpose_(a) {
// @see https://stackoverflow.com/a/13241545/1536038
return Object.keys(a[0]).map(c => a.map(r => r[c]));
}
您可能還想看看ImportJSON。
uj5u.com熱心網友回復:
我對 JavaScript 不是很熟悉,但是您可以嘗試 search() 方法,我相信它會回傳找到單詞的索引。因此,例如,如果您嘗試將來自“instantBuy”的資料放入其自己的行或列中,您可以執行 search("instantBuy"),獲取該字串和搜索將回傳的下一個索引之間的字串,搜索(“價格變化”)
對不起,如果這令人困惑!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/379138.html
下一篇:如何回圈回圈x次?
