将收到的信息解析为列/行 [英] Parse received information into columns/rows

查看:56
本文介绍了将收到的信息解析为列/行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以从Binance中获取所有数据并推出解析后的信息.这是我的脚本:

I have a script to get all the data from a Binance and push out parsed information. Here's my script:

function myFunction() {
  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getActiveSheet();

  var data = UrlFetchApp.fetch("https://www.binance.com/assetWithdraw/getAllAsset.html");
  var first = JSON.parse(data)[0];
  var out = JSON.stringify(first).split(",");

  for(var i =0;i<out.length;i++){
    Logger.log(i);
    Logger.log(out[i]);
    ss.getRange(1,i+1).setValue(out[i]);
  }
}

我收到了看起来像标题":值"的信息.我需要获取所有"title"值作为列标题,然后将这些值作为行.我不确定从这里要去哪里.

I received information that looks like "title":"value". I need to get all the "title" values as column headers and then the values as the rows. I'm not sure where to go from here.

推荐答案

此修改如何?

  • 通过分隔标题"和值"来检索每个数据.
  • 创建一个数组,用于将数据导入电子表格并使用setValues()导入.
  • Retrieve each data by separating "title" and "values".
  • Create an array for importing data to Spreadsheet and import it using setValues().
function myFunction() {
  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getActiveSheet();
  var data = UrlFetchApp.fetch("https://www.binance.com/assetWithdraw/getAllAsset.html");

  // Modified
  var out = JSON.parse(data.getContentText());
  var title = [];
  for (var i in out[0]) {
    title.push(i);
  }
  var res = [];
  res.push(title);
  for (var i in out) {
    var values = [];
    for (var j in out[i]) {
      values.push(out[i][j]);
    }
    res.push(values);
  }
  ss.getRange(ss.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
}

参考:

  • setValues()
  • Reference :

    • setValues()
    • 如果我误解了您的问题,请告诉我.我想修改它.

      If I misunderstand your question, please tell me. I would like to modify it.

      为了从out[0]对象检索键,我使用了for (var i in out[0]) {.请参阅以下示例.

      In order to retrieve keys from object which is out[0], I used for (var i in out[0]) {. Please see the following samples.

      var sample = [{key1: "value1", key2: "value2"}];
      for (var i in sample[0]) {
        Logger.log("%s, %s", i, sample[0][i])
      }
      

      结果

      key1, value1
      key2, value2
      

      在此脚本中,sample[0]{key1: "value1", key2: "value2"}的对象. for (var i in sample[0]) {可以检索key1key2的键.使用检索的键,sample[0][i]可以检索value1value2.

      In this script, sample[0] is an object of {key1: "value1", key2: "value2"}. for (var i in sample[0]) { can retrieve keys which are key1 and key2. Using retrieved keys, value1 and value2 can be retrieved by sample[0][i].

      另一方面,请参阅下一个示例脚本.

      On the other hand, please see the next sample script.

      var sample = [{key1: "value1", key2: "value2"}];
      for (var i = 0; i < sample[0].length; i++) {
        Logger.log("%s, %s", i, sample[0][i])
      }
      

      在此脚本中,sample[0].length变为undefined.因为object.length无法检索长度.因此,此for循环不起作用.

      In this script, sample[0].length becomes undefined. Because object.length cannot retrieve the length. So this for loop doesn't work.

      如果要像示例2一样使用for循环检索键和值,请使用下一个示例脚本.

      If you want to retrieve keys and values using for loop like the sample 2. Please use the next sample script.

      var sample = [{key1: "value1", key2: "value2"}];
      var keys = Object.keys(sample[0]);
      for (var i=0; i<keys.length; i++) {
        Logger.log("%s, %s", keys[i], sample[0][keys[i]])
      }
      

      结果

      key1, value1
      key2, value2
      

      在此脚本中,对象的键可以通过Object.keys()检索.使用此方法,可以检索键和值.

      In this script, keys from object can be retrieved by Object.keys(). Using this, keys and values are retrieved.

      • for...in
      • Object.keys()
      function myFunction() {
        var sh = SpreadsheetApp.getActiveSpreadsheet();
        var ss = sh.getActiveSheet();
        var data = UrlFetchApp.fetch("https://www.binance.com/assetWithdraw/getAllAsset.html");
      
        // Modified
        var out = JSON.parse(data.getContentText());
        var title = [];
        for (var i in out[0]) {
          if (i == "assetCode" || i == "transactionFee") {
            title.push(i);
          }
        }
        var res = [];
        res.push(title);
        for (var i in out) {
          var values = [];
          for (var j in out[i]) {
            if (j == "assetCode" || j == "transactionFee") {
              values.push(out[i][j]);
            }
          }
          res.push(values);
        }
        ss.getRange(ss.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
      }
      

      这篇关于将收到的信息解析为列/行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆