Last active
October 4, 2018 02:17
-
-
Save Zlatkovsky/522183067333a47d8ec4f7e8a4823c57 to your computer and use it in GitHub Desktop.
Shared with Script Lab
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
name: Stock Price | |
description: '' | |
author: Zlatkovsky | |
host: EXCEL | |
api_set: {} | |
script: | |
content: |- | |
/** @customfunction */ | |
function stockPrice(ticker: string): Promise<number> { | |
var url = "https://api.iextrading.com/1.0/stock/" + ticker + "/price"; | |
return fetch(url) | |
.then(function (response) { | |
return response.text(); | |
}) | |
.then(function (text) { | |
return parseFloat(text); | |
}); | |
// Note: in case of an error, the returned rejected Promise | |
// will be bubbled up to Excel to indicate an error. | |
} | |
/** @customfunction */ | |
function stockPriceStream(ticker: string, handler: CustomFunctions.StreamingHandler<number>) { | |
var updateFrequency = 10 /* milliseconds*/; | |
var isPending = false; | |
var timer = setInterval(function () { | |
// If there is already a pending request, skip this iteration: | |
if (isPending) { | |
return; | |
} | |
var url = "https://api.iextrading.com/1.0/stock/" + ticker + "/price"; | |
isPending = true; | |
fetch(url) | |
.then(function (response) { | |
return response.text(); | |
}) | |
.then(function (text) { | |
handler.setResult(parseFloat(text)); | |
}) | |
.catch(function (error) { | |
handler.setResult(new Error(error) as any); // FIXME | |
}) | |
.then(function () { | |
isPending = false; | |
}); | |
}, updateFrequency); | |
handler.onCanceled = () => { | |
clearInterval(timer); | |
}; | |
} | |
language: typescript | |
libraries: | | |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
@types/office-js | |
@types/custom-functions-runtime | |
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css | |
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css | |
core-js@2.4.1/client/core.min.js | |
@types/core-js | |
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js | |
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts | |
jquery@3.1.1 | |
@types/jquery |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment