Skip to content

Instantly share code, notes, and snippets.

@ShellyShulei
Last active December 10, 2019 18:49
Show Gist options
  • Select an option

  • Save ShellyShulei/0ff1f6852acae08eaa31d03237c9dc0d to your computer and use it in GitHub Desktop.

Select an option

Save ShellyShulei/0ff1f6852acae08eaa31d03237c9dc0d to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
name: URL
description: Create a new snippet from a blank template.
host: EXCEL
api_set: {}
script:
content: >
$("#submitStockData").click(() => tryCatch(getStockData));
$("#COPY").click(() => tryCatch(writeAPIDataToExcel("2019-12-10
13:30:00")));
function getStockData() {
var stockId = $("#stockId").val();
var dataUrl =
"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=" +
stockId +
"&interval=5min&apikey=";
console.log(dataUrl);
}
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=\"Enter 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\t\t\t<button id=\"COPY\" type=\"submit\" class=\"btn btn- primary\">Su</button>\n\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