【发布时间】:2021-02-13 00:55:47
【问题描述】:
我正在使用 JSON 从 API(FMP 云)中为我的 Google 表格电子表格检索股市数据,但不知道如何从 API 网页获取特定数据。以下是我试图从中提取的网页内容:
[ {
"symbol" : "AMC",
"name" : "AMC Entertainment Holdings, Inc.",
"price" : 5.60000000,
"changesPercentage" : -0.18000000,
"change" : -0.01000000,
"dayLow" : 5.55000000,
"dayHigh" : 5.97000000,
"yearHigh" : 20.36000000,
"yearLow" : 1.91000000,
"marketCap" : 1898808704.00000000,
"priceAvg50" : 4.95848460,
"priceAvg200" : 4.37841750,
"volume" : 39477325,
"avgVolume" : 120395740,
"exchange" : "NYSE",
"open" : 5.72000000,
"previousClose" : 5.61000000,
"eps" : -34.81800000,
"pe" : null,
"earningsAnnouncement" : "2020-11-02T16:15:00.000+0000",
"sharesOutstanding" : 339072983,
"timestamp" : 1613160110
} ]
如果我使用单元格脚本=IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "/price","")(我替换了我的真实 API 密钥),它会获取其中包含“价格”一词的所有数据,即价格、priceAvg50 和 priceAvg200。它看起来像这样:
Avg50 Avg200
5.59 4.9584846 4.3784175
如果我只想要价格,即第一条数据(5.59),我应该如何修改单元格中的脚本?希望这个问题是有道理的。感谢您的宝贵时间。
【问题讨论】:
-
虽然我不确定你使用的是
IMPORTJSON,但如果数据的结构始终相同,那么使用const IMPORTJSON = url => JSON.parse(UrlFetchApp.fetch(url).getContentText())[0].price;的脚本怎么样?在这种情况下,请将=IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey")的公式放入单元格中。 -
您好,感谢您的帮助。当我尝试这个时,它会将我可以获取的东西限制在价格上。所有带有脚本以获取其他内容的单元格都会出现错误。有没有办法绕过它?
-
感谢您的回复。我带来的不便表示歉意。在我的评论中,来自
If I just want price, which is the first piece of data (5.59), how should I modify the script in the cell?,我提出了它。关于When I try this, it restricts what I can fetch to just the price. All cells with script looking to fetch other things come up as an error. Is there a way to bypass that?在您的回复中,您能否提供您期望的示例输出?通过这个,我想考虑解决方案。对此我深表歉意。 -
没问题,感谢您抽出宝贵时间帮助我。样本输出将是两个单元格。在单元格 1 中,我会写
=IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "/price", "noHeaders"),它会向我显示股票的价格。在单元格 2 中,我会写=IMPORTJSON("https://fmpcloud.io/api/v3/quote/AMC/?apikey=examplekey", "/changesPercentage", "noHeaders"),这将显示股票价格的百分比变化。 -
感谢您的回复。从您的 2 个回复 cmets 中,我提出了一个答案。你能确认一下吗?如果这不是您期望的方向,我深表歉意。
标签: json google-apps-script google-sheets google-sheets-formula google-sheets-api