Skip to content

Instantly share code, notes, and snippets.

@jipyua
Created April 3, 2023 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jipyua/53bff39daa9771d37f14074578bb2ad3 to your computer and use it in GitHub Desktop.
Save jipyua/53bff39daa9771d37f14074578bb2ad3 to your computer and use it in GitHub Desktop.
Performs a basic Word API call using TypeScript.
name: InsertFileFromBase64
description: Performs a basic Word API call using TypeScript.
host: WORD
api_set: {}
script:
content: |
$("#file").change(getBase64);
$("#insert-sheets").click(() => tryCatch(insertDocument));
let externalDoc;
async function getBase64() {
// Retrieve the file and set up an HTML FileReader element.
const myFile = <HTMLInputElement>document.getElementById("file");
const reader = new FileReader();
reader.onload = (event) => {
// Remove the metadata before the base64-encoded string.
const startIndex = reader.result.toString().indexOf("base64,");
externalDoc = reader.result.toString().substr(startIndex + 7);
};
// Read the file as a data URL so that we can parse the base64-encoded string.
reader.readAsDataURL(myFile.files[0]);
}
async function insertDocument() {
await Word.run(async (context) => {
// Retrieve the source workbook.
var option = {
importTheme: true,
importStyles: true,
importParagraphSpacing: true,
importPageColor: true,
importChangeTrackingMode: true
};
const document = context.document.body.insertFileFromBase64(externalDoc, "Replace");
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\">\n\t<p>This sample shows how to insert content from an existing Document into the current Document.</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<p>Select an Word document to copy its content into the current one.</p>\n\t<form>\n\t\t<input type=\"file\" id=\"file\" />\n </form>\n\t\t<br>\n\t\t<p>Insert the content from the selected Document.</p>\n\t\t<button id=\"insert-sheets\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Insert content</span>\n </button>\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