Created
October 27, 2022 12:20
-
-
Save theboyknowsclass/503a2ee64fe15abc78efb08f5ed414c4 to your computer and use it in GitHub Desktop.
Performs a basic PowerPoint API call using TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Basic API call (TypeScript) | |
| description: Performs a basic PowerPoint API call using TypeScript | |
| host: POWERPOINT | |
| api_set: {} | |
| script: | |
| content: > | |
| $("#tagged").click(() => tryCatch(tagged)); | |
| $("#refresh").click(() => tryCatch(refresh)); | |
| const data = ["VALUE_1", "VALUE_2", "VALUE_3"]; | |
| const refreshData = ["VALUE_1 (Refreshed)", "VALUE_2 (Refreshed)", "VALUE_3 | |
| (Refreshed)"]; | |
| const refresh: Function = async () => { | |
| // This function gets the collection of shapes on the first slide, | |
| // and adds a text box to the collection, while specifying its text, | |
| const replacementTags = data.map((_e, i) => { | |
| return "<" + (i + 1) + ">"; | |
| }); | |
| await PowerPoint.run(async (context) => { | |
| const slides = context.presentation.slides; | |
| slides.load( | |
| "items, items/shapes, items/shapes/items/id, items/shapes/items/top, items/shapes/items/left, items/shapes/items/width, items/shapes/items/height, items/shapes/items/tags, items/shapes/items/tags/items, items/shapes/items/tags/items/key, items/shapes/items/tags/items/value" | |
| ); | |
| await context.sync(); | |
| for (const slide of slides.items) { | |
| for (const shape of slide.shapes.items) { | |
| for (const tag of shape.tags.items) { | |
| if (tag.key == "ANAPLAN") { | |
| let inputString = tag.value; | |
| for (let i = 0; i < replacementTags.length; i++) { | |
| const tag = replacementTags[i]; | |
| inputString = inputString.replace(tag, refreshData[i]); | |
| } | |
| shape.load("textFrame, textFrame/hasText") | |
| await context.sync(); | |
| console.log(shape.textFrame.hasText) | |
| shape.textFrame.load("textRange, textRange/text") | |
| await context.sync(); | |
| shape.textFrame.textRange.text = inputString; | |
| await context.sync(); | |
| } | |
| } | |
| await context.sync(); | |
| } | |
| } | |
| }); | |
| }; | |
| const getTaggedShapes = async () => { | |
| let props = []; | |
| await PowerPoint.run(async (context) => { | |
| const slides = context.presentation.slides; | |
| slides.load( | |
| "items, items/shapes, items/shapes/items/id, items/shapes/items/top, items/shapes/items/left, items/shapes/items/width, items/shapes/items/height, items/shapes/items/tags, items/shapes/items/tags/items, items/shapes/items/tags/items/key, items/shapes/items/tags/items/value" | |
| ); | |
| await context.sync(); | |
| for (const slide of slides.items) { | |
| for (const shape of slide.shapes.items) { | |
| for (const tag of shape.tags.items) { | |
| if (tag.key == "ANAPLAN") { | |
| props.push({ | |
| tag: tag.value, | |
| slideId: slide.id, | |
| shapeId: shape.id | |
| }); | |
| } | |
| } | |
| } | |
| await context.sync(); | |
| } | |
| }); | |
| return props; | |
| }; | |
| const tagged: Function = async () => { | |
| // This function gets the collection of shapes on the first slide, | |
| // and adds a text box to the collection, while specifying its text, | |
| await PowerPoint.run(async (context) => { | |
| const shapes: PowerPoint.ShapeCollection = context.presentation.slides.getItemAt(0).shapes; | |
| shapes.load("items"); | |
| await context.sync(); | |
| const replacementTags = data.map((_e, i) => { | |
| return "<" + (i + 1) + ">"; | |
| }); | |
| const inputString = $("#input").val() as string; | |
| let replaced = inputString | |
| for (let i = 0; i < replacementTags.length; i++) { | |
| const tag = replacementTags[i]; | |
| replaced = replaced.replace(tag, data[i]); | |
| } | |
| const shapeOptions: PowerPoint.ShapeAddOptions = { | |
| left: 100, | |
| top: 300, | |
| height: 300, | |
| width: 450 | |
| }; | |
| const textbox: PowerPoint.Shape = shapes.addTextBox(replaced, shapeOptions); | |
| textbox.load("tags, tags/items"); | |
| await context.sync(); | |
| textbox.tags.add("ANAPLAN", inputString); | |
| await context.sync(); | |
| textbox.load("tags, tags/items, tags/items/key, tags/items/value"); | |
| await context.sync(); | |
| console.log(textbox.tags.items[0].value); | |
| return; | |
| }); | |
| }; | |
| /** Default helper for invoking an action and handling errors. */ | |
| const tryCatch: (callback: Function) => void = async (callback: Function) => | |
| { | |
| 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\tThis sample executes a code snippet that adds a text box to the first slide in the presentation.\n</section>\n<div>\n\t<input id=\"input\">\n\t<button id=\"tagged\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Change TextBox to with input</span>\n</button>\n</div>\n<div>\n\t<button id=\"refresh\" class=\"ms-Button\">\n\t <span class=\"ms-Button-label\">Refresh Text Shapes</span>\n\t</button>\n\t</div>" | |
| 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 | |
| [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