Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ethanabrooks/886de3a2fc999cee6d0ccc0956cc6b53 to your computer and use it in GitHub Desktop.
Save ethanabrooks/886de3a2fc999cee6d0ccc0956cc6b53 to your computer and use it in GitHub Desktop.
Inserts slides from another PowerPoint file into the current presentation.
name: Insert slides from other presentation
description: Inserts slides from another PowerPoint file into the current presentation.
host: POWERPOINT
api_set: {}
script:
content: >
$("#insert-all-slides").click(() => tryCatch(insertAllSlides));
$("#insert-after-target-slide").click(() =>
tryCatch(insertAfterSelectedSlide));
$("#file").change(() => tryCatch(storeFileAsBase64));
let chosenFileBase64;
async function storeFileAsBase64() {
const reader = new FileReader();
reader.onload = async (event) => {
// Remove the metadata before the Base64-encoded string.
const startIndex = reader.result.toString().indexOf("base64,");
const copyBase64 = reader.result.toString().substr(startIndex + 7);
chosenFileBase64 = copyBase64;
};
// Read in the file and store a Base64-encoded copy as the reader.result
// property. This also triggers the onload event.
const myFile = document.getElementById("file") as HTMLInputElement;
reader.readAsDataURL(myFile.files[0]);
}
async function insertAllSlides() {
await PowerPoint.run(async function(context) {
context.presentation.insertSlidesFromBase64(chosenFileBase64);
await context.sync();
});
}
async function insertAfterSelectedSlide() {
await PowerPoint.run(async function(context) {
const selectedSlideID = await getSelectedSlideID();
context.presentation.insertSlidesFromBase64(chosenFileBase64, {
formatting: PowerPoint.InsertSlideFormatting.useDestinationTheme,
targetSlideId: selectedSlideID + "#"
});
await context.sync();
});
}
function getSelectedSlideID() {
// Wrap a call of one of the Common APIs in a Promise-returning
// function, so that it can be easily called within a run() function
// of an application-specific API.
return new OfficeExtension.Promise<string>(function(resolve, reject) {
Office.context.document.getSelectedDataAsync(Office.CoercionType.SlideRange, function(asyncResult) {
try {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
reject(console.error(asyncResult.error.message));
} else {
const result = asyncResult.value as any;
resolve(result.slides[0].id);
}
} catch (error) {
reject(console.log(error));
}
});
});
}
/** 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: "<html>\n\n<head>\n\t<style>\n\t\th1 {\n\t\t\tcolor: blue;\n\t\t\tfont-family: Arial;\n\t\t}\n\n\t\tp {\n\t\t\tcolor: green;\n\t\t\tfont-family: Verdana;\n\t\t}\n\t</style>\n</head>\n\n<body>\n\t<h1>Script Lab</h1>\n\t<p>This is a sample task pane for Script Lab in PowerPoint.</p>\n</body>\n\n</html>"
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