简体   繁体   English

Google Apps 脚本:如何从 Yahoo Finance 检索动态命名对象的值

[英]Google Apps Script: How to retrieve values of dynamically named objects from Yahoo Finance

I'm trying to pull raw data from dynamically named objects from Yahoo Finance.我正在尝试从 Yahoo Finance 的动态命名对象中提取原始数据。 In the following simplified example codes, I hard coded its object key name and index after manually checking its object structure to retrieve raw data.在下面的简化示例代码中,在手动检查其 object 结构以检索原始数据后,我对其 object 键名和索引进行了硬编码。 Instead of such a hard coding which will be really difficult if more than 100 keys, I'm looking for a smart solution that checks all the object keys and pull their raw data from them directly without needing to write each key names manually.如果超过 100 个键,那么硬编码将非常困难,我正在寻找一种智能解决方案来检查所有 object 键并直接从中提取原始数据,而无需手动编写每个键名。 How can I do that?我怎样才能做到这一点? Thanks!谢谢!

My expected output:我预期的 output:

在此处输入图像描述

function test() {

  var url = 'https://query2.finance.yahoo.com/ws/fundamentals-timeseries/v1/finance/timeseries/AVGO?lang=en-US&region=US&symbol=AVGO&padTimeSeries=true&type=annualInterestExpense,trailingInterestExpense&merge=false&period1=493590046&period2=1672807102&corsDomain=finance.yahoo.com';
  var obj = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
  obj = JSON.parse(obj);  //Convert strings to object

  var type = obj.timeseries.result.map(({ meta: { type } }) => type).flat();
  console.log(type)

  var trailingInterestExpenseRAW = obj.timeseries.result[0].trailingInterestExpense.map(({ reportedValue: { raw } }) => raw);
  var annualInterestExpenseRAW = obj.timeseries.result[1].annualInterestExpense.map(({ reportedValue: { raw } }) => raw);
  console.log(['trailingInterestExpense', ...trailingInterestExpenseRAW],
    ['annualInterestExpense', ...annualInterestExpenseRAW])
}

In your script, how about the following modification?在你的脚本中,如何进行以下修改?

From:从:

var type = obj.timeseries.result.map(({ meta: { type } }) => type).flat();
console.log(type)

var trailingInterestExpenseRAW = obj.timeseries.result[0].trailingInterestExpense.map(({ reportedValue: { raw } }) => raw);
var annualInterestExpenseRAW = obj.timeseries.result[1].annualInterestExpense.map(({ reportedValue: { raw } }) => raw);
console.log(['trailingInterestExpense', ...trailingInterestExpenseRAW],
  ['annualInterestExpense', ...annualInterestExpenseRAW])

To:到:

var types = obj.timeseries.result.flatMap(({ meta: { type } }) => type);
var array = obj.timeseries.result.map(o => types.flatMap(type => o[type] ? [type, ...o[type].map(({ reportedValue: { raw } }) => raw)] : []));
var maxLen = Math.max(...array.map(r => r.length));
var values = array.map(r => [...r, ...Array(maxLen - r.length).fill(null)]);
console.log(values); // You can see the values in the log.

// In this sample, the values are put to the active sheet.
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
  • When this script is run, values is [["trailingInterestExpense",1737000000,null,null,null],["annualInterestExpense",1444000000,1777000000,1885000000,1737000000]] .运行此脚本时, values [["trailingInterestExpense",1737000000,null,null,null],["annualInterestExpense",1444000000,1777000000,1885000000,1737000000]] By this, this value can be put to the sheet using setValues .这样,可以使用setValues将该值放入工作表。

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Google Apps 脚本中从 Yahoo Finance XHR 的 Complex Object 获取值 - How to get Values from Yahoo Finance XHR's Complex Object in Google Apps Script Google Apps Script 停止从 Yahoo Finance 抓取数据 - Google Apps Script stopped scraping data from Yahoo Finance 如何使用 Google Apps 脚本从其对象中提取 Yahoo Finance 历史价格数据? - How to pull Yahoo Finance Historical Price Data from its Object with Google Apps Script? 适用于雅虎财经的 Google Apps 脚本返回空单元格 - Google Apps Script for Yahoo Finance Returns Empty Cell 在应用程序脚本中从 yahoo Finance API 返回历史价格数组 - Returning array of historical prices from yahoo finance API in apps script 用于抓取 WSJ 和 Yahoo Finance 的 Google App 脚本 - Google App script for scraping WSJ and Yahoo Finance 如何从Google Apps脚本项目属性存储和检索对象? - How can I store and retrieve objects from Google Apps Script Project Properties? Google Apps 脚本:如何从具有 null 和空元素的深层嵌套 object 中检索值? - Google Apps Script: How to retrieve values from deeply nested object having nulls and empty elements? 触发运行 Google Sheets 脚本以批量从 Yahoo Finance 获取 URL 数据 - Trigger-run a Google Sheets script to fetch URL data from Yahoo Finance in batches 如何从谷歌应用程序脚本中的数组中检索特定行? - How to retrieve a particular row from an array in google apps script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM