Home > Net >  How to get Values from Yahoo Finance XHR's Complex Object in Google Apps Script
How to get Values from Yahoo Finance XHR's Complex Object in Google Apps Script

Time:12-26

I'm trying to pull historical quarterly revenue data from Yahoo Finance. Its default "Annual" enter image description here How can I do that? Thank you for any help!

function test() {
  var ticker = 'JPM';
  var url = 'https://query1.finance.yahoo.com/ws/fundamentals-timeseries/v1/finance/timeseries/'
      ticker   '?lang=en-US&region=US&symbol='   ticker   '&padTimeSeries=true&type='   ',quarterlyTotalRevenue'  
    ',&merge=false&period1=493590046&period2=1672016399&corsDomain=finance.yahoo.com';
  var obj = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
  var obj = JSON.parse(obj);  //Convert strings to object
  console.log(obj);
}

CodePudding user response:

When your script is modified, how about the following modification?

Modified script:

var obj = ### // Please set your value.

var valeus = [["asOfDate", "raw"], ...obj.timeseries.result[0].quarterlyTotalRevenue.map(({ asOfDate, reportedValue: { raw } }) => [asOfDate, raw])];
var res = valeus[0].map((_, c) => valeus.map(r => r[c]));

// return res; // If you want to use this as a custom function, please use this.

var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, res.length, 2).setValues(res);
  • In this modification, the values are put to the active sheet. If you want to use this script as a custom function, please return the values like return res;.

Reference:

  • Related