如何在电子表格中从雅虎财经导入历史数据 (CSV) 示例脚本:结果:参考:

问题描述

我尝试为 APPLE 导入历史数据 (CSV)。我在 Google Sheet 中使用 ImportData 函数

https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1577982443&period2=1609604843&interval=1d&events=history&includeAdjustedClose=true

但结果是“#N/A”。

我想得到 CSV,因为有 3 位小数。并且只有 2 个,在网站上。

=IMPORTXML("https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1577982443&period2=1609604843&interval=1d&events=history&includeAdjustedClose=true")

获取文件后有一个脚本:AAPL.csv。

你能帮我吗?

解决方法

不幸的是,在 URL 的情况下,似乎不能使用 IMPORTDATAIMPORTXML。但是,幸运的是,我确认可以检索 Google Apps 脚本的 UrlFetchApp CSV 数据。因此,在这个答案中,我想建议使用 Google Apps Script 来实现您的目标。

示例脚本:

请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中,并将 =SAMPLE("https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1577982443&period2=1609604843&interval=1d&events=history&includeAdjustedClose=true") 放在单元格中。此脚本用作自定义函数。这样,可以在单元格中检索 CSV 数据。

const SAMPLE = url => Utilities.parseCsv(UrlFetchApp.fetch(url).getContentText());

结果:

当使用上述脚本时,得到如下结果。

enter image description here

参考: