Skip to content

Instantly share code, notes, and snippets.

@jipyua
Created April 11, 2023 07:27
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/f00478681668a1376dcf650c0301d359 to your computer and use it in GitHub Desktop.
Save jipyua/f00478681668a1376dcf650c0301d359 to your computer and use it in GitHub Desktop.
Performs a basic Word API call using TypeScript.
name: InsertFileFromBase64 (1)
description: Performs a basic Word API call using TypeScript.
host: WORD
api_set: {}
script:
content: |
$("#file").change(getBase64);
$("#insert-sheets").click(() => tryCatch(insertDocument));
var 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 createDocument() {
await Word.run(async (context) => {
// Retrieve the source workbook.
const documentCreated = context.application.createDocument(externalDoc);
documentCreated.body.insertText("abc", "Start");
//documentCreated.save();
documentCreated.open();
//context.document.body.contentControls.getFirst().insertFileFromBase64(externalDoc, "Replace");
// context.document.body.getRange();
await context.sync();
});
}
async function insertDocument() {
await Word.run(async (context) => {
var option = {
importTheme: true,
importStyles: true,
importParagraphSpacing: true,
importPageColor: true,
importChangeTrackingMode: true
};
//context.document.body.insertFileFromBase64(externalDoc, "Replace")
//var sections = context.document.insertFileFromBase64(externalDoc, "Start", option);
console.log(externalDoc.length);
const field = context.document.getSelection().insertField("Start", Word.FieldType.addin);
field.data = externalDoc;
await context.sync();
const field2 = context.document.body.fields.getFirst();
field2.load();
await context.sync();
console.log(field2.data.length);
field2.delete();
await context.sync();
}).catch((e) => {
console.log(e);
});
}
/** 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