[英]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®ion=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?在你的脚本中,如何进行以下修改?
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])
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);
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
将该值放入工作表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.