Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save lumine2008/69b3d60184382b00b35ef2850b160a78 to your computer and use it in GitHub Desktop.

Select an option

Save lumine2008/69b3d60184382b00b35ef2850b160a78 to your computer and use it in GitHub Desktop.
Creates a few different geometric shapes and deletes them from the worksheet.
name: Create and delete geometric shapes
description: Creates a few different geometric shapes and deletes them from the worksheet.
host: EXCEL
api_set: {}
script:
content: |
$("#setup").click(() => tryCatch(setup));
$("#createHexagon").click(() => tryCatch(createHexagon));
$("#removeAll").click(() => tryCatch(removeAll));
async function createHexagon() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Shapes");
const shape = sheet.shapes.addGeometricShape(Excel.GeometricShapeType.hexagon);
shape.left = 30;
shape.top = 5;
shape.height = 175;
shape.width = 200;
const range = sheet.getRange("A1");
range.select();
await context.sync();
});
}
async function setup() {
await Excel.run(async (context) => {
context.workbook.worksheets.getItemOrNullObject("Shapes").delete();
const sheet = context.workbook.worksheets.add("Shapes");
sheet.activate();
await context.sync();
});
}
async function removeAll() {
await Excel.run(async (context) => {
context.workbook.worksheets.getItemOrNullObject("Shapes").delete();
const sheet = context.workbook.worksheets.add("Shapes");
const shapes = sheet.shapes;
// load all the shapes in the collection without loading their properties
shapes.load("items/$none");
await context.sync();
shapes.items.forEach((shape) => shape.delete());
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: |-
<section class="ms-font-m">
<p>This sample shows how to create different shapes, then delele them.</p>
</section>
<section class="setup ms-font-m">
<h3>Setup</h3>
<button id="setup" class="ms-Button">
<span class="ms-Button-label">Create new worksheet</span>
</button>
</section>
<section class="samples ms-font-m">
<h3>Try it out</h3>
<button id="createHexagon" class="ms-Button">
<span class="ms-Button-label">Create hexagon</span>
</button><p/>
<button id="removeAll" class="ms-Button">
<span class="ms-Button-label">Remove all shapes</span>
</button><p/>
</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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment