App script code to access google finance stock data.

4,085 views
Skip to first unread message

Lalit Kumar

unread,
Apr 2, 2023, 1:36:55 PM4/2/23
to Google Apps Script Community
I am using = GOOGLEFINANCE() function to access stocks data in Google sheets..


For some analysis and data extraction, i need to get google finance stock price using Google Apps Scripts but i am not able to find any similar function or options in Google Apps Scripts to do so, need urgent help on this.


Tanaike

unread,
Apr 2, 2023, 8:57:48 PM4/2/23
to Google Apps Script Community

Nerio Villalobos

unread,
Apr 11, 2023, 4:20:42 AM4/11/23
to google-apps-sc...@googlegroups.com
To retrieve stock price using Google Apps Script, you can use the UrlFetchApp service to make a request to the Google Finance API and parse the JSON response to extract the price.

Here's an example code snippet to get the stock price of Apple:

function getStockPrice() {
  var symbol = "AAPL";
  var url = "https://finance.google.com/finance/quote/" + symbol + ":NASDAQ?output=json";
  var response = UrlFetchApp.fetch(url);
  var content = response.getContentText();
  var json = JSON.parse(content.substring(6));
  var price = json[0].l;
  return price;
}

In this example, the symbol variable is set to "AAPL" for Apple, but you can change it to the stock symbol of your choice. The url variable is constructed with the symbol and the exchange (in this case, NASDAQ), and the UrlFetchApp.fetch() method is used to make a GET request to the URL.

The response content is then parsed as JSON using JSON.parse() method, and the price is extracted from the parsed JSON object using the l property.

You can then call the getStockPrice() function in your Google Apps Script to retrieve the stock price.

El dom, 2 abr 2023 a la(s) 14:36, Lalit Kumar (idl...@gmail.com) escribió:
I am using = GOOGLEFINANCE() function to access stocks data in Google sheets..


For some analysis and data extraction, i need to get google finance stock price using Google Apps Scripts but i am not able to find any similar function or options in Google Apps Scripts to do so, need urgent help on this.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/2aa25e6b-b768-4b17-b50f-2ecea73ee503n%40googlegroups.com.


--
__________________________
Nerio Enrique Villalobos Morillo
Buenos Aires, Argentina
Reply all
Reply to author
Forward
0 new messages