簡體   English   中英

Apps 腳本 - 如何獲取正確的標題范圍以將值設置為單元格谷歌表格?

[英]Apps Script - how to get the correct headers range to set the value to a cell google sheets?

function response() {
  var url = 'https://api.zonda.exchange/rest/trading/ticker/BTC-PLN'
  var response = UrlFetchApp.fetch('https://api.zonda.exchange/rest/trading/ticker/BTC-PLN');
  Logger.log(response.getContentText());
  SpreadsheetApp.getActiveSheet().getRange('D4').setValue("hello");
}

日志:

{ "status": "Ok", "ticker": { "market": { "code": "BTC-PLN", "first": { "currency": "BTC", "minOffer": "0.0000432", "scale": 8 }, "second": { "currency": "PLN", "minOffer": "5", "scale": 2 }, "amountPrecision": 8, "pricePrecision": 2, "ratePrecision": 2 }, "time": "1663016303747", "highestBid": "104000.28", "lowestAsk": "104232.99", "rate": "104300.04", "previousRate": "104232" } }
function getData() {
  const json = '{ "status": "Ok", "ticker": { "market": { "code": "BTC-PLN", "first": { "currency": "BTC", "minOffer": "0.0000432", "scale": 8 }, "second": { "currency": "PLN", "minOffer": "5", "scale": 2 }, "amountPrecision": 8, "pricePrecision": 2, "ratePrecision": 2 }, "time": "1663016303747", "highestBid": "104000.28", "lowestAsk": "104232.99", "rate": "104300.04", "previousRate": "104232" } }';
  let obj = JSON.parse(json);
  Logger.log(obj.ticker.highestBid);
  Logger.log(obj.ticker.lowestAsk);
}

Execution log
3:14:07 PM  Notice  Execution started
3:14:05 PM  Info    104000.28
3:14:05 PM  Info    104232.99
3:14:08 PM  Notice  Execution completed

function getData() {
  const json = '{ "status": "Ok", "ticker": { "market": { "code": "BTC-PLN", "first": { "currency": "BTC", "minOffer": "0.0000432", "scale": 8 }, "second": { "currency": "PLN", "minOffer": "5", "scale": 2 }, "amountPrecision": 8, "pricePrecision": 2, "ratePrecision": 2 }, "time": "1663016303747", "highestBid": "104000.28", "lowestAsk": "104232.99", "rate": "104300.04", "previousRate": "104232" } }';
  let obj = JSON.parse(json);
  let o = Object.keys(obj.ticker).map(k => {
    if (typeof obj.ticker[k] !== 'object') {
      return [k, obj.ticker[k]];
    } else {
      return ["",""];
    }
  }).filter(e => e[0]);
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  sh.clearContents();
  o.unshift(["Key","Value"]);
  sh.getRange(1,1,o.length,o[0].length).setValues(o);
}

這就是我的表格 output 的樣子

鑰匙 價值
時間 1663016303747
最高出價 104000.28
最低要價 104232.99
速度 104300.04
上一個速率 104232

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM