简体   繁体   中英

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:

=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/ )

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)."

see picture

Is it possible to detect this error, and if there is an error, call this function instead:

=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.

See picture #2

You can do it wiht 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']"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM