Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save areed1192/70a3178078880128ff85e8305eaa3884 to your computer and use it in GitHub Desktop.
Save areed1192/70a3178078880128ff85e8305eaa3884 to your computer and use it in GitHub Desktop.
Performs a basic Excel API call using JavaScript with the "common API" syntax (compatible with Office 2013).
name: Basic API call (Office 2013)
description: >-
Performs a basic Excel API call using JavaScript with the "common API" syntax
(compatible with Office 2013).
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(showAsync);
// $("#setup").click(() => tryCatch(grabTable));
function showAsync() {
// define some variables
let a = 100;
let b = 200;
let c = 300;
// define some functions
let funcOne = function(num) {return 5 * num}
let funcTwo = function() {return 10}
// print the function
console.log(funcOne(funcTwo()));
// print the variables
console.log(a);
console.log(b);
// Let's see what happen when we try to print this.
setTimeout(function () { c++; }, 0);
console.log(c)
// Let's see if this fixes the problem
setTimeout(function () { console.log(c) }, 0);
}
async function grabTable() {
await Excel.run(async (context) => {
let myWorkbook = context.workbook;
myWorkbook.tables
.getItem("Table1")
.getHeaderRowRange()
.select();
// load all the properties related to table.
myWorkbook.load(["tables/*", "dataConnections"]);
await context.sync();
// Alternatively I could have done the following
// let myTbl = myWorkbook.tables;
// myTbl.load("count");
// Display count to user.
console.log(`The table count is '${myWorkbook.tables.count}'.`);
});
}
/** 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: "<section class=\"ms-font-m\">\n\t<p>This sample uses the Common APIs compatible with Office 2013.</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<p>Select a cell in the worksheet and press <b>Write to console</b> to see the contents of that cell in the console.\n\t</p>\n\t<button id=\"run\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Write to console</span>\n </button>\n\t<button id=\"setup\" class=\"ms-Button\">\n\t <span class=\"ms-Button-label\">Grab table</span>\n\t </button>\n\t<p>\n\t\t<p><i>Be sure to exit cell-editing context before pressing the button.</i></p>\n</section>"
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
[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/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment