In this article we will show how to import Yahoo Finance data to Google Sheets using a custom apps script. Simply follow the steps below.
We will use a custom script to import Yahoo Finance data to Google Sheets. Click Extensions in the main menu, then select Apps Script.
A new tab will appear where the Apps Script will be loaded.
As you notice, the text area of the script holds some lines of placeholder code. Delete it by selecting them all and then pressing the Delete key in your keyboard. Next, copy and paste the following code:
function yahooF(ticker) {
const url = `https://finance.yahoo.com/quote/${ticker}?p=${ticker}`;
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const contentText = res.getContentText();
const price = contentText.match(/<fin-streamer(?:.*?)active="">(\d+[,]?[\d\.]+?)<\/fin-streamer>/); console.log(price[1]); return price[1];
}
The code is from Jason Hee’s article in Medium. Thank you!
You can rename the project by clicking the box containing the words Untitled project. When you hover your cursor over it, a Rename label will appear. Click it.
A small pop-up box will appear where you can set the Project title. Once done, click Rename.
After copying the script to Apps Script, save the code by clicking the Save project icon along the main toolbar. The icon is on the left of the Run button.
After saving the code, the save icon will turn to gray instead of black.
Go back to the sheet. Type the yahooF formula with the ticker (TSLA, AAPL, etc) in double quotes. Google Sheets will initially notify you that yahooF is an unknown function.
Once you have set up the formula, click Enter. The script works! Our formula now displays the current price of the Nasdaq 100 (NQ=F).
If you enjoyed this article, you might also like our article on how to import CoinMarketCap data into Google Sheets or our article on how to import Binance API in Google Sheets.
If you want to learn how to extract domains from emails in Google Sheets, we also suggest checking out our detailed guide.