56 lines
1.9 KiB
JavaScript
56 lines
1.9 KiB
JavaScript
|
|
// ONLYOFFICE Custom Function
|
||
|
|
// Location: View -> Macros -> Custom Functions
|
||
|
|
// Used on: "Dashboard" tab (regular equities only -- Finnhub does NOT support mutual funds)
|
||
|
|
// Data source: Finnhub (https://finnhub.io) -- free tier, real-time US equity quotes
|
||
|
|
//
|
||
|
|
// Real API key lives in Vaultwarden -- never commit the real key to this file.
|
||
|
|
|
||
|
|
(function () {
|
||
|
|
/**
|
||
|
|
* Latest stock price for a ticker, via Finnhub.
|
||
|
|
* @customfunction
|
||
|
|
* @param {string} ticker Stock ticker symbol, e.g. "AAPL"
|
||
|
|
* @returns {any} Latest price, or an error string
|
||
|
|
*/
|
||
|
|
async function STOCKPRICE(ticker) {
|
||
|
|
var apiKey = "YOUR_FINNHUB_API_KEY";
|
||
|
|
var url = "https://finnhub.io/api/v1/quote?symbol=" +
|
||
|
|
encodeURIComponent(ticker) + "&token=" + apiKey;
|
||
|
|
try {
|
||
|
|
var r = await fetch(url);
|
||
|
|
var data = await r.json();
|
||
|
|
if (data && typeof data.c === "number" && data.c !== 0) {
|
||
|
|
return data.c;
|
||
|
|
}
|
||
|
|
return "#N/A";
|
||
|
|
} catch (e) {
|
||
|
|
return "#ERROR";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Exchange-reported timestamp of the last quote for a ticker, via Finnhub.
|
||
|
|
* @customfunction
|
||
|
|
* @param {string} ticker Stock ticker symbol, e.g. "AAPL"
|
||
|
|
* @returns {any} Last-updated time, or an error string
|
||
|
|
*/
|
||
|
|
async function STOCKTIME(ticker) {
|
||
|
|
var apiKey = "YOUR_FINNHUB_API_KEY";
|
||
|
|
var url = "https://finnhub.io/api/v1/quote?symbol=" +
|
||
|
|
encodeURIComponent(ticker) + "&token=" + apiKey;
|
||
|
|
try {
|
||
|
|
var r = await fetch(url);
|
||
|
|
var data = await r.json();
|
||
|
|
if (data && data.t) {
|
||
|
|
return new Date(data.t * 1000).toLocaleString();
|
||
|
|
}
|
||
|
|
return "#N/A";
|
||
|
|
} catch (e) {
|
||
|
|
return "#ERROR";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Api.AddCustomFunction(STOCKPRICE);
|
||
|
|
Api.AddCustomFunction(STOCKTIME);
|
||
|
|
})();
|