Skip to content

Instantly share code, notes, and snippets.

@xuruiyao-msft
Created March 2, 2023 17:17
Show Gist options
  • Save xuruiyao-msft/a6fc984a697c86c1f19afd007ec9e26c to your computer and use it in GitHub Desktop.
Save xuruiyao-msft/a6fc984a697c86c1f19afd007ec9e26c to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
name: OfficeAtWorkDemo
description: Create a new snippet from a blank template.
host: WORD
api_set: {}
script:
content: |-
OfficeExtension.config.extendedErrorLogging = true;
$("#file").change(getBase64);
$("#ok").click(() => tryCatch(run));
let externalWorkbook;
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) => {
const startIndex = reader.result.toString().indexOf("base64,");
externalWorkbook = 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 run() {
await Word.run(async (context) => {
// console.log(externalWorkbook);
const styles = context.application.retrieveStylesFromBase64(externalWorkbook);
await context.sync();
const jsonObject = JSON.parse(styles.value);
console.log(jsonObject['Styles'][2].Font.Color);
// add a new style with the same font.color as the one
// imported from the selected file
const newlyAddedStyle = context.document.addStyle("OfficeAtWork_NEW_STYLE",Word.StyleType.paragraph);
newlyAddedStyle.font.color = jsonObject['Styles'][1].Font.Color;
await context.sync();
// apply the imported style to curret selected range
const range = context.document.getSelection();
range.style = "OfficeAtWork_NEW_STYLE";
await context.sync();
// check if the range is the same style as the imported one just now
const range2 = context.document.getSelection();
range2.load();
await context.sync();
console.log(range2.style);
});
}
/** 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);
}
}
interface Style {
BaseStyle: string;
BuiltIn: boolean;
InUse: boolean;
Linked: boolean;
NextParagraphStyle: string;
}
language: typescript
template:
content: "<h1>Select a file to retrieve styles from</h1>\n<form>\n\t<input type=\"file\" id=\"file\"/>\n\t</form>\n\t<br>\n\t<button id=\"ok\" class=\"ms-Button\">\n\t\t\t<span class=\"ms-Button-label\">OK</span>\n\t\t</button>"
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