shaiss
shaiss

Reputation: 2010

Google Sheets: Multidimension Array to rows in Custom Function

I have a json response like the below. I'd like to take this output and put it into two columns. The Coin name in column A and percentage in column B. I can't figure out how to loop through the array and output it into two columns:

[
    {
        "Coin": "ETH",
        "PortfolioPercentage": "23.87%"
    },
    {
        "Coin": "BTC",
        "PortfolioPercentage": "11.86%"
    },
    {
        "Coin": "BNB",
        "PortfolioPercentage": "9.92%"
    },
    {
        "Coin": "FET",
        "PortfolioPercentage": "3.82%"
    }
]

Any suggestions on how to accomplish this? I can step through the array and pull the coin out into one column, just not both. Ty!

here's what I have that works for the coin only

  for(var i = 1; i <= 50; i++){
    var CoinPortfolio = parseData[i].Coin;
    var CoinPercentage = parseData[i].PortfolioPercentage;
    
    //console.log(CoinPortfolio);
    //console.log(CoinPercentage);
    
    output.push(CoinPortfolio);
  }

  //var portfolioData = parseData.data[coin_token].circulating_supply;
  return output;

Upvotes: 0

Views: 40

Answers (2)

Mike Steelson
Mike Steelson

Reputation: 15318

Assuming your json is in a cell

function extractData(json) {
  var result = []
  var data = JSON.parse(json)
  for (var i=0;i<data.length;i++){
    result.push([data[i]['Coin'],data[i]['PortfolioPercentage']])
  }
  return result
}

Upvotes: 2

JohnA
JohnA

Reputation: 1107

No looping required, just set the range and write the array

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.getRange(1, 1, CoinPortfolio.length(), 2).setValues(CoinPortfolio);

Upvotes: 0

Related Questions