Last active
October 21, 2018 10:15
-
-
Save lumine2008/12ed098d9fdbaceb2cf8945db102b941 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: CFDemo1 | |
description: '' | |
author: lumine2008 | |
host: EXCEL | |
api_set: {} | |
script: | |
content: |- | |
/** @customfunction */ | |
function getSalesData( | |
product: string, | |
callback: CustomFunctions.StreamingHandler<number> | |
): void { | |
let result = 0; | |
const timer = setInterval(() => { | |
result += Math.random() * 20; | |
callback.setResult(result); | |
}, 2000); | |
callback.onCanceled = () => { | |
clearInterval(timer); | |
}; | |
} | |
/** @CustomFunction */ | |
function | |
Contains(text: string, find_text: string): boolean | |
{ | |
//return TRUE if text contains find_text; else false | |
return text.includes(find_text); | |
} | |
/** @CustomFunction */ | |
function StockQuote(ticker:string): Promise<number> | |
{ | |
//1. If(web call), you should want to return a "Promise". | |
//2. This tells Excel to #GETTING_DATA, until the promise is 'resolved'. | |
//3. NEW: Custom Functions allow you the user to continue interaction | |
return new Promise(function (resolve) { | |
var xhr = new XMLHttpRequest(); | |
var url = "https://api.iextrading.com/1.0/stock/" + ticker + "/price"; | |
//add handler for xhr | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState == XMLHttpRequest.DONE) { | |
//return result back to Excel | |
var price = parseFloat(xhr.responseText); | |
resolve(price); | |
} | |
} | |
//make request | |
xhr.open('GET', url, true); | |
xhr.send(); | |
}); | |
} | |
/** @CustomFunction */ | |
function IMPORTHTML(url:string, tableIndex:number):Promise<string[][]> { | |
//make call w/ XHR to return the data | |
return new Promise(function (resolve) { | |
var xhr = new XMLHttpRequest(); | |
var apiUrl = "https://excelcf-demo-api.azurewebsites.net/api/ImportHtml?url=" + url + "&index=" + tableIndex; | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState == XMLHttpRequest.DONE) { | |
var result = xhr.responseText; | |
var jsonResult = JSON.parse(result); | |
if (jsonResult.length == 0) | |
jsonResult = [["Try again we couldn't find any results. Sample formulas:"], | |
["=SCRIPTLAB.DEMO.IMPORTHTML(\"https://en.wikipedia.org/wiki/India\",4)"] | |
] | |
resolve(jsonResult); | |
} | |
} | |
xhr.open('GET', apiUrl, true); | |
xhr.send(); | |
}); | |
} | |
/** @CustomFunction */ | |
function TRANSLATE(text: string, langLocale: string): Promise<string> { | |
return new Promise(function (resolve) { | |
var xhr = new XMLHttpRequest(); | |
var textStr = encodeURIComponent(text); | |
var localeStr = encodeURIComponent(langLocale); | |
var url = "https://excelcf-demo-api.azurewebsites.net/api/translate?code=F69Va5ojUPvfnat9udiM8OpEcScy/oK3bV8/wBYW8OXlypR3nyV/AA==&name=" + | |
textStr + "&locale=" + localeStr; | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState == XMLHttpRequest.DONE) { | |
resolve(xhr.responseText); | |
} | |
} | |
xhr.open('GET', url, true); | |
xhr.send(); | |
}); | |
} | |
language: typescript | |
libraries: | | |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
@types/office-js | |
[email protected]/dist/css/fabric.min.css | |
[email protected]/dist/css/fabric.components.min.css | |
[email protected]/client/core.min.js | |
@types/core-js | |
@microsoft/[email protected]/dist/office.helpers.min.js | |
@microsoft/[email protected]/dist/office.helpers.d.ts | |
[email protected] | |
@types/jquery |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment