简体   繁体   English

如何运行 Google 表格应用脚本,如果出现错误,则调用电子表格 function IMPORTXML() 来填充单元格?

[英]How to run a Google Sheets App Script, and if there is an error, then call Spreadsheet function IMPORTXML() instead to populate the cell?

To get the price of bitcoin and other cryptocurrencies into my Google Sheets spreadsheet, I use this function:为了将比特币和其他加密货币的价格输入我的 Google 表格电子表格,我使用了这个 function:

=importJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin","0.current_price")

where importJSON is a Google App Script provided by coingecko (see https://blog.coingecko.com/import-coingecko-cryptocurrency-data-into-google-sheets/ )其中 importJSON 是 coingecko 提供的 Google App Script(参见https://blog.coingecko.com/import-coingecko-cryptocurrency-data-into-google-sheets/

This works fine for a few lookups, but if the spreadsheet has many cryptocurrencies, a whole bunch of them will say "Error: Exceed maximum execution time (line 0)."这适用于一些查找,但如果电子表格有很多加密货币,一大堆会说“错误:超过最大执行时间(第 0 行)”。

see picture看图片

Is it possible to detect this error, and if there is an error, call this function instead:是否可以检测到这个错误,如果有错误,调用这个 function 代替:

=IMPORTXML("https://coinmarketcap.com/currencies/bitcoin","//div[@class='priceValue___11gHJ']")

The problem is that this is a Google Sheets function and I don't know how to call it from inside the Google Apps script inside the catch(err) code block.问题是这是一个 Google 表格 function,我不知道如何从 catch(err) 代码块内的 Google Apps 脚本中调用它。

See picture #2见图片#2

You can do it wiht IFERROR你可以用IFERROR做到这一点

Sample formula:示例公式:

=IFERROR(importJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin","0.current_price"),IMPORTXML("https://coinmarketcap.com/currencies/bitcoin","//div[@class='priceValue___11gHJ']"))

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

相关问题 Google App-使用Debug运行脚本时不会填充电子表格单元格 - Google App - Doesn't Populate Spreadsheet Cell During Script Run using Debug 如何将应用程序脚本 function 值(字符串)作为 importXML function 直接传递给谷歌工作表单元格? - How can I pass an app script function value (a string) to a google sheet cell as an importXML function directly? 使用Google表格,我如何每小时运行一个脚本并填充列中的每个单元格? - Using Google Sheets, how do I run a script every hour and populate each cell in a column? Google电子表格脚本函数调用 - Google spreadsheet script function call 从外部应用程序调用Google Spreadsheet脚本函数 - Call Google Spreadsheet script function from external app Google 表格应用程序脚本 - 将 function 转换为特定单元格的 onEdit - Google Sheets App Script - Convert a function to an onEdit of a specific cell Google 表格应用程序脚本编辑受保护的单元格或 Object 错误 - Google Sheets App Script Edit Protected Cell or Object Error Google电子表格:在单元格值更改上运行脚本 - Google Spreadsheet: run script on cell value change 如何一直在Google Spreadsheet单元格中调用函数? - How can I call a function all the time in a Google Spreadsheet cell? 加速电子表格的脚本(GAS - Google App Script & Google Sheets) - Speeding up a script for a spreadsheet (GAS - Google App Script & Google Sheets)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM