To retrieve stock data from Yahoo Finance in OfficeScripts, you can make use of the UrlFetch
class to send HTTP requests and fetch the data. Here’s an example of a function that retrieves stock data from Yahoo Finance based on a provided link:
function getStockDataFromYahoo(link) { var url = "https://query1.finance.yahoo.com/v7/finance/download" + link; var requestOptions = { method: "GET" }; return ExcelScript.UrlFetch.fetch(url, requestOptions) .then(function (response) { if (response.status === 200) { var content = response.content; // Process the content as needed console.log("Stock data retrieved successfully: " + content); } else { console.log("Error occurred while retrieving stock data. Status: " + response.status); } }) .catch(function (error) { console.log("Error occurred: " + error); }); }
In this example, the getStockDataFromYahoo
function takes a link
parameter, which represents the link to the specific stock data you want to retrieve. The function constructs the URL using the provided link and sends a GET request to Yahoo Finance using ExcelScript.UrlFetch.fetch
.
Once the response is received, the function checks the status code of the response. If the status code is 200 (indicating a successful response), it processes the content of the response as needed. In this example, it logs the content to the console. If the status code is not 200, it logs an error message with the status code.
To use this function, you can call it with the desired link as an argument. For example:
getStockDataFromYahoo("/quotes.csv?s=MSFT") .catch(function (error) { console.log("Error occurred: " + error); });
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.