Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Rick-Kirkham/764e00fd24384407a859dcd5ca2e7cd1 to your computer and use it in GitHub Desktop.
Save Rick-Kirkham/764e00fd24384407a859dcd5ca2e7cd1 to your computer and use it in GitHub Desktop.
Inserts slides from another PowerPoint file into the current presentation.
name: 'Insert shape, line, and textbox to PPT'
description: Inserts slides from another PowerPoint file into the current presentation.
host: POWERPOINT
api_set: {}
script:
content: |
$("#createHexagon").click(() => tryCatch(createHexagon));
$("#createLine").click(() => tryCatch(createLine));
$("#createTextBox").click(() => tryCatch(createTextBox));
$("#createShapeWithText").click(() => tryCatch(createShapeWithText));
$("#removeAll").click(() => tryCatch(removeAll));
async function createHexagon() {
await PowerPoint.run(async (context) => {
var shapes = context.presentation.slides.getItemAt(0).shapes;
var rectangle = shapes.addGeometricShape(PowerPoint.GeometricShapeType.hexagon);
rectangle.left = 100;
rectangle.top = 100;
rectangle.height = 150;
rectangle.width = 150;
rectangle.name = "Square";
await context.sync();
});
}
async function createLine() {
await PowerPoint.run(async (context) => {
var shapes = context.presentation.slides.getItemAt(0).shapes;
var line = shapes.addLine(PowerPoint.ConnectorType.straight, { left: 100, top: -100, height: 20, width: 150 });
line.name = "StraightLine";
await context.sync();
});
}
async function removeAll() {
await PowerPoint.run(async (context) => {
const slide = context.presentation.slides.getItemAt(0);
const shapes = slide.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();
});
}
async function createTextBox() {
await PowerPoint.run(async (context) => {
var shapes = context.presentation.slides.getItemAt(0).shapes;
var textbox = shapes.addTextBox("Hello!");
textbox.left = 100;
textbox.top = 100;
textbox.height = 300;
textbox.width = 450;
textbox.name = "Textbox";
return context.sync();
});
}
async function createShapeWithText() {
await PowerPoint.run(async (context) => {
var shapes = context.presentation.slides.getItemAt(0).shapes;
var wave = shapes.addGeometricShape(PowerPoint.GeometricShapeType.bracePair);
wave.left = 100;
wave.top = 400;
wave.height = 50;
wave.width = 150;
wave.name = "Wave";
wave.fill.setSolidColor("lightblue");
// wave.textFrame.textRange.text = "Shape text";
wave.textFrame.textRange.font.color = "purple";
// wave.textFrame.verticalAlignment = PowerPoint.TextVerticalAlignment.middleCentered;
return 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\">\n\t<p>This sample shows how to create different shapes, then delete them.</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<button id=\"createHexagon\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Create hexagon</span>\n </button>\n\t<p />\n\t<button id=\"createLine\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Create line</span>\n </button>\n\t<p />\n\n\t<button id=\"createTextBox\" class=\"ms-Button\">\n\t\t <span class=\"ms-Button-label\">Create text box</span>\n\t\t </button>\n\t<p />\n\n\t<button id=\"createShapeWithText\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Create shape with text</span>\n </button>\n\t<p />\n\n\t<button id=\"removeAll\" class=\"ms-Button\">\n\t <span class=\"ms-Button-label\">Remove all shapes</span>\n\t </button>\n\t<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/beta/hosted/office.js
@types/office-js-preview
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