Skip to content

Instantly share code, notes, and snippets.

@MiaofeiWang
Last active July 23, 2018 05:59
Show Gist options
  • Save MiaofeiWang/3bfb2dd7b40bb40c31e96c1bc8ae2114 to your computer and use it in GitHub Desktop.
Save MiaofeiWang/3bfb2dd7b40bb40c31e96c1bc8ae2114 to your computer and use it in GitHub Desktop.
Executes a basic Excel API call - Shared with Script Lab
name: Shape - Text
description: Executes a basic Excel API call
author: MiaofeiWang
host: EXCEL
api_set: {}
script:
content: |-
$("#run").click(() => tryCatch(run));
async function run() {
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getActiveWorksheet();
let shapes = sheet.shapes;
let count = shapes.getCount();
shapes.load("items");
await context.sync();
let shape;
if (count.value > 0) {
shape = shapes.items[0];
}
else {
shape = shapes.addGeometricShape(Excel.GeometricShapeType.ribbon, 100, 100, 100, 100);
await context.sync();
}
let txfm = shape.textFrame;
txfm.leftMargin = 3.312345;
txfm.topMargin = 2.34234;
txfm.rightMargin = 0;
txfm.bottomMargin = -1;
txfm.load();
let range = txfm.textRange;
range.text = "ABCDE";
await context.sync();
let subRange = range.getCharacters(0,0);
subRange.text = "1ST";
await context.sync();
//subRange.text = "2ND";
range.load();
subRange.load("text");
await context.sync();
console.log(`TextFrame Margin: ${txfm.leftMargin}, ${txfm.topMargin}, ${txfm.rightMargin}, ${txfm.bottomMargin}`);
console.log(`Range Text: ${range.text}`);
console.log(`SubRange text: ${subRange.text}`);
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
}
catch (error) {
OfficeHelpers.UI.notify(error);
OfficeHelpers.Utilities.log(error);
}
}
language: typescript
template:
content: |
<h2>Shape API Demo</h2>
<button id="run" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
language: html
style:
content: ''
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
https://appsforoffice.microsoft.com/lib/1/hosted/office.d.ts
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
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts
jquery@3.1.1
@types/jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment