Created
December 2, 2019 11:29
-
-
Save anmarant/3cbe9f461e1107f1c779662a51cab49d to your computer and use it in GitHub Desktop.
Demo Excel 365 API call
This file contains hidden or 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: Demo Excel 365 -v3 | |
| description: Demo Excel 365 API call | |
| host: EXCEL | |
| api_set: {} | |
| script: | |
| content: | | |
| //$("#run").click(() => tryCatch(run)); | |
| $("#submitStockData").click(() => tryCatch(getStockData)); | |
| function getStockData() { | |
| var stockId = $("#stockId").val(); | |
| var dataUrl = | |
| "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=" + | |
| stockId + | |
| "&interval=5min&apikey=CFB1YOLQLQ996EJO"; | |
| console.log(dataUrl); | |
| $.ajax({ | |
| type: "GET", | |
| url: dataUrl, | |
| async: false, | |
| success: function(responseData) { | |
| var timeSeriesData = responseData["Time Series (5min)"]; | |
| writeAPIDataToExcel(timeSeriesData); //aggiunta io | |
| }, | |
| error: function printError(errorMessage) { | |
| console.log(errorMessage); | |
| } | |
| }); | |
| console.log("Your code goes here"); | |
| } | |
| async function writeAPIDataToExcel(timeSeriesData) { | |
| await Excel.run(async (context) => { | |
| var excelDataArray = []; | |
| Object.keys(timeSeriesData).forEach(function(item) { | |
| console.log(item); | |
| var excelRowItem = []; | |
| excelRowItem[0] = item; | |
| excelRowItem[1] = timeSeriesData[item]["1. open"]; | |
| excelRowItem[2] = timeSeriesData[item]["2. high"]; | |
| excelRowItem[3] = timeSeriesData[item]["3. low"]; | |
| excelRowItem[4] = timeSeriesData[item]["4. close"]; | |
| excelRowItem[5] = timeSeriesData[item]["5. volume"]; | |
| excelDataArray.push(excelRowItem); | |
| }); | |
| var shareOutputRange = context.workbook.worksheets.getItem('Sheet1').getRange('A1').getResizedRange(excelDataArray.length - 1, excelDataArray[0].length - 1).getOffsetRange(1, 0); | |
| var shareHeadingRange = context.workbook.worksheets.getItem('Sheet1').getRange('A1: F1'); | |
| shareHeadingRange.values = [['Date', 'Open', 'High', 'Low', 'Close', 'Volume']]; | |
| shareHeadingRange.format.font.bold = true; | |
| shareOutputRange.values = excelDataArray; | |
| await context.sync(); | |
| }); | |
| } | |
| /** Default helper for invoking an action and handling errors. */ | |
| async function tryCatch(callback) { | |
| try { | |
| await callback(); | |
| } catch (error) { | |
| // Note: In a production add-in, you'd want to notify the user through your add-in's UI. | |
| console.error(error); | |
| } | |
| } | |
| language: typescript | |
| template: | |
| content: "<div class=\"jumbotron\">\n\t<div id=\"content-main\">\n\t\t<div class=\"padding\">\n\t\t\t<h1> Share Price Data </h1>\n\t\t\t<hr />\n\t\t\t<form>\n\t\t\t\t<div class=\"form-group\">\n\t\t\t\t\t<label for=\"exampleInputEmail1\">Stock Ticker</label>\n\t\t\t\t\t<input type=\"text\" class=\"form-control\" id=\"stockId\" placeholder=\"Inserisci stock ticker\">\n </div>\n\t\t\t\t\t<button id=\"submitStockData\" type=\"submit\" class=\"btn btn-primary\">submit</button>\n\t\t\t</form>\n\t\t</div>\n\t</div>\n</div>" | |
| language: html | |
| style: | |
| content: |- | |
| section.samples { | |
| margin-top: 20px; | |
| } | |
| section.samples .ms-Button, section.setup .ms-Button { | |
| display: block; | |
| margin-bottom: 5px; | |
| margin-left: 20px; | |
| min-width: 80px; | |
| } | |
| language: css | |
| libraries: | | |
| https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
| @types/office-js | |
| 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 | |
| jquery@3.1.1 | |
| @types/jquery@3.3.1 | |
| https://unpkg.com/bootstrap@4.1.0/dist/css/bootstrap.min.css |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment