Last active
August 7, 2024 13:36
-
-
Save lopes/75bc6b4f7b084517596c to your computer and use it in GitHub Desktop.
Retrieves a quote from YAHOO! Finance and puts into the spreadsheet's cell. #javascript #js #sheets #yahoo #finance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_quote() { | |
/* get_quote | |
* Retrieves a quote from YAHOO! Finance and puts into the spreadsheet's cell. | |
* | |
* It's a pretty simple script that access YAHOO! Finance and get the price of | |
* previous close price of symbol in the first column cell of actual row. | |
* | |
* Usage: Start a spreadsheet where the symbols are disposed in lines and are | |
* in the first column. Use only symbols without those ".SA"s in the end. | |
* Access Tools > Script editor... and replace the default code for this | |
* function. In each price line, add this formula to get the current price for | |
* the corresponding symbol: =get_quote() | |
* | |
* Example: | |
* | A | ... | N | |
* -+--------+-----+-------------- | |
* 1| SYMBOL | ... | PRICE | |
* -+--------+-----+-------------- | |
* 2| PETR4 | ... | =get_quote() | |
* 3| GGBR4 | ... | =get_quote() | |
* 4| CMIG4 | ... | =get_quote() | |
* -+--------+-----+-------------- | |
* | |
* If you made everything as described, Google Spreadsheet should display the | |
* current quote in that line. | |
* | |
* For further information about YAHOO! Finance data download, please read | |
* this document: http://www.gummy-stuff.org/Yahoo-data.htm | |
* If you want to know more about Google Apps Script (this language) refer to | |
* the official documentation page: | |
* http://code.google.com/googleapps/appsscript/guide.html | |
* | |
* IMPORTANT: This function retrieves the previous close price. If you want | |
* to get the "real time" quote, you should use "b" special tag instead of | |
* "p". But it is not going to work if markets are about to open --the hours | |
* between the beggining of the day and the early trading. See YAHOO! Finance | |
* documentation --link above. | |
* | |
* Author.: José Lopes de Oliveira Jr. <http://about.me/joselopes> | |
* Licence: GPLv3+ | |
* Date...: 2012-01-09 | |
* | |
*/ | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveCell(); | |
var symbol = sheet.getRange(range.getRow(), 1, 1, 1).getValues() + ".SA"; | |
return parseFloat(UrlFetchApp.fetch("http://finance.yahoo.com/d/quotes.csv?s=" + | |
symbol + "&f=p").getContentText()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment