Skip to content

Instantly share code, notes, and snippets.

@jiju-MS
Created October 7, 2021 11:31
Show Gist options
  • Save jiju-MS/d80b70bd038d1268bdf992948af0b2a1 to your computer and use it in GitHub Desktop.
Save jiju-MS/d80b70bd038d1268bdf992948af0b2a1 to your computer and use it in GitHub Desktop.
Performs a basic Excel API call using TypeScript.
name: Basic API call (TypeScript)
description: Performs a basic Excel API call using TypeScript.
host: EXCEL
api_set: {}
script:
content: |
$("#get").click(() => tryCatch(getYellow));
$("#set").click(() => tryCatch(setYellow));
async function getYellow() {
await Excel.run(async (context) => {
const range = context.workbook.getSelectedRange();
range.load(["values", "valuesAsJSON"]);
await context.sync();
console.log(JSON.stringify(range.valuesAsJSON));
//range.values = [[ range.values[0][0] + 1]];
// range.valuesAsJSON = [
// [{
// type: "Error",
// basicValue: "#N/A",
// basicType: "Error",
// errorSubType: "HlookupValueNotFound",
// errorType: "Na"
// }]
// ];
// await context.sync();
// const range2 = context.workbook.getSelectedRange();
// range2.load(["values", "valuesAsJSON"]);
// await context.sync();
// console.log(JSON.stringify(range2.valuesAsJSON));
});
}
async function setYellow() {
await Excel.run(async (context) => {
const range = context.workbook.getSelectedRange();
range.load(["values", "valuesAsJSON"]);
await context.sync();
// console.log(JSON.stringify(range.valuesAsJSON));
// range.values = [[range.values[0][0]]];
range.valuesAsJSON = [[{
"type": "Entity",
"basicType": "Error",
"basicValue": "#VALUE!",
"text": "Entity With API",
"properties": {
"TestDouble": {
"type": "Double",
"basicType": "Double",
"basicValue": 1,
propertyMetadata: {
excludeFrom: {
"autoComplete": false,
"calcCompare": false,
"dotNotation": false,
"cardView": true
}
}
},
"TestString": {
"type": "String",
"basicType": "String",
"basicValue": "Test"
}
}
}]];
await context.sync();
// const range2 = context.workbook.getSelectedRange();
// range2.load(["values", "valuesAsJSON"]);
// await context.sync();
// console.log(JSON.stringify(range2.valuesAsJSON));
});
}
/** 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">
<p class="ms-font-m">This sample demonstrates basic Excel API calls.</p>
</section>
<section class="samples ms-font-m">
<h3>Try it out</h3>
<p class="ms-font-m">Select some cells in the worksheet, then press <b>Highlight selected range</b>.</p>
<button id="get" class="ms-Button">
<span class="ms-Button-label">get</span>
</button>
<button id="set" class="ms-Button">
<span class="ms-Button-label">set</span>
</button>
</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://unpkg.com/@microsoft/[email protected]/dist/office.debug.js
https://unpkg.com/@microsoft/[email protected]/dist/office-with-first-party.d.ts
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
[email protected]/client/core.min.js
@types/core-js
[email protected]
@types/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment