Created
March 29, 2023 02:35
-
-
Save xuruiyao-msft/1f438e3468c9bee98f765d0e4d63c7b5 to your computer and use it in GitHub Desktop.
GetStylesFromBase64AsJson
This file contains 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: GetStylesFromBase64AsJson | |
description: GetStylesFromBase64AsJson | |
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. | |
const json = context.application.retrieveStylesFromBase64(externalDoc); | |
await context.sync(); | |
console.log(json); | |
}); | |
} | |
/** 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: "\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<p>Select an Word document to get styles. as json</p>\n\t<form>\n\t\t<input type=\"file\" id=\"file\" />\n </form>\n\t\t<br>\n\t\t<p></p>\n\t\t<button id=\"insert-sheets\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Get Styles Json</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