Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lumine2008/604300d1c0c0a130279cb52fa41a2470 to your computer and use it in GitHub Desktop.
Save lumine2008/604300d1c0c0a130279cb52fa41a2470 to your computer and use it in GitHub Desktop.
Creates a few different geometric shapes and deletes them from the worksheet.
name: Create Shapes and remove colors
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));
$("#createTriangle").click(() => tryCatch(createTriangle));
$("#createSmileyFace").click(() => tryCatch(createSmileyFace));
$("#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 = 5;
shape.top = 5;
shape.height = 175;
shape.width = 200;
//shape.fill.setSolidColor("yellow");
await context.sync();
});
}
async function createTriangle() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Shapes");
const shape = sheet.shapes.addGeometricShape(Excel.GeometricShapeType.triangle);
shape.left = 100;
shape.top = 300;
shape.height = 150;
shape.width = 200;
shape.rotation = 45;
shape.fill.clear();
await context.sync();
});
}
async function createSmileyFace() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Shapes");
const shape = sheet.shapes.addGeometricShape(Excel.GeometricShapeType.smileyFace);
shape.left = 300;
shape.top = 100;
shape.height = 100;
shape.width = 100;
shape.fill.foregroundColor = "yellow";
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) => {
const sheet = context.workbook.worksheets.getItem("Shapes");
const shapes = sheet.shapes;
shapes.load();
await context.sync();
for (var i = 0; i < shapes.items.length; i++) {
shapes.getItemAt(i).lineFormat.transparency = 1;
shapes.getItemAt(i).fill.clear();
}
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="createTriangle" class="ms-Button">
<span class="ms-Button-label">Create triangle</span>
</button><p/>
<button id="createSmileyFace" class="ms-Button">
<span class="ms-Button-label">Create smiley face</span>
</button><p/>
<button id="removeAll" class="ms-Button">
<span class="ms-Button-label">Remove all Colors</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