Daeker Poland
Daeker Poland

Reputation: 3

Making a list using api in google sheets

I am attempting to pull faction information from torns api but it puts all data into a single cell rather than listing. heres what ive got so far.

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Faction members')
      .addItem('Pulls faction member info','callNumbers')
      .addToUi();
}

function callNumbers() {

  var response = UrlFetchApp.fetch("https://api.torn.com/faction/42911?selections=basic&key=xFtPCG2ygjbhmKWI");
  Logger.log(response.getContentText());

  var fact = response.getContentText();
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1,2).setValue([fact]);

}```

Upvotes: 0

Views: 75

Answers (1)

Edvott
Edvott

Reputation: 46

You could try to parse the information using JSON.parse. Something like:

function callNumbers() {

  var response = UrlFetchApp.fetch("https://api.torn.com/faction/42911?selections=basic&key=xFtPCG2ygjbhmKWI");
  Logger.log(response.getContentText());

  var fact = response.getContentText();

  var myObject = JSON.parse(fact);

  // define an array of all the object keys
  var headerRow = Object.keys(myObject);

  // define an array of all the object values
  var row = headerRow.map(function(key){ return myObject[key]});

  // define the contents of the range
  var contents = [
    headerRow,
    row
  ];

  // select the range and set its values
  var ss = SpreadsheetApp.getActive();
  var rng = ss.getActiveSheet().getRange(1, 1, contents.length, headerRow.length )
  rng.setValues(contents)
}

Upvotes: 1

Related Questions