Skip to content

Instantly share code, notes, and snippets.

@studermartin
Last active July 15, 2018 16:22
Show Gist options
  • Save studermartin/6b8297a494bb998d44731f84069f50de to your computer and use it in GitHub Desktop.
Save studermartin/6b8297a494bb998d44731f84069f50de to your computer and use it in GitHub Desktop.
Shared with Script Lab
name: Fix Confluence2Word export
description: ''
author: MartinStuderHTWChur
host: WORD
api_set: {}
script:
content: |-
// Script to adapt formating in Microsoft Word documents exported from Atlassian Confluence.
//
// Links
// - https://github.com/OfficeDev/office-js-snippets/tree/master/samples/word/99-fabric: example using fabric components for the UI
initializeUI();
async function initializeUI() {
const toggleElements = document.querySelectorAll(".ms-Toggle");
for (let i = 0; i < toggleElements.length; i++) {
new fabric['Toggle'](toggleElements[i]);
}
}
$("#run").click(() => tryCatch(run));
function replaceListStyle13(paragraph: Word.Paragraph): void {
if (paragraph.isListItem) {
if (paragraph.leftIndent == 36) {
setAndLogStyle(paragraph, "List Bullet");
}
if (paragraph.leftIndent == 72) {
setAndLogStyle(paragraph, "List Bullet 2");
}
}
}
function replaceHeadingStyle13(paragraph: Word.Paragraph): void {
const heading: string = "Heading";
let replaceStyles: Map<string, string> = new Map<string, string>();
replaceStyles[`${heading}2`] = `${heading}1`;
replaceStyles[`${heading}3`] = `${heading}2`;
replaceStyles[`${heading}4`] = `${heading}3`;
if (paragraph.styleBuiltIn in replaceStyles) {
setAndLogStyleBuiltIn(paragraph, replaceStyles[paragraph.styleBuiltIn]);
}
}
function replaceHeadingStyle(paragraph: Word.Paragraph): void {
const heading: string = "\u00dcberschrift";
let replaceStyles: Map<string, string> = new Map<string, string>();
replaceStyles[`${heading} 2`] = `${heading} 1`;
replaceStyles[`${heading} 3`] = `${heading} 2`;
replaceStyles[`${heading} 4`] = `${heading} 3`;
if (paragraph.style in replaceStyles) {
setAndLogStyle(paragraph, replaceStyles[paragraph.style]);
}
}
async function run() {
await Word.run(async (context) => {
let wordApi1_3: boolean = Office.context.requirements.isSetSupported('WordApi', 1.3);
/* if (!Office.context.requirements.isSetSupported('WordApi', 1.3)) {
console.log('Sorry. The tutorial add-in uses Word.js APIs that are not available in your version of Office.');
}
*/
// only using WordApi 1.1
const heading: string = "\u00dcberschrift";
let replaceStyles: Map<string,string> = new Map<string,string>();
replaceStyles[`${heading} 2`] = `${heading} 1`;
replaceStyles[`${heading} 3`] = `${heading} 2`;
replaceStyles[`${heading} 4`] = `${heading} 3`;
let increaseHeading:boolean = $("#tgl-increase-heading").children('label').hasClass('is-selected');
let selection = context.document.getSelection();
await context.sync();
let paragraphs = selection.paragraphs;
// var paragraphs = context.document.body.paragraphs;
paragraphs.load();
await context.sync();
for (let i = 0; i < paragraphs.items.length; i++) {
let paragraph = paragraphs.items[i];
paragraph.load("style");
await context.sync();
//
if (paragraph.style == "Standard (Web)") {
setAndLogStyle(paragraph, "Standard");
}
if (increaseHeading) {
if (wordApi1_3) {
replaceHeadingStyle13(paragraph);
} else {
replaceHeadingStyle(paragraph);
}
}
// Failed attemps to reliably check for list items
// Attempt 1: isListItem
// Failed because list related functions are only available in WordApi 1.3
// console.log(paragraph.isListItem)
// Attempt 2: HTML
// Aproach: Check the html code for <li>s
// Failed because the html code does not contain any sign of lists
// var html = paragraph.getHtml();
// await context.sync();
// console.log(html.value);
// Heuristially check for list item
// Approach: Check for indentation
// Drawback: Cannot distinguish between list types (ordered, numbered ...)
if (wordApi1_3) {
replaceListStyle13(paragraph);
} else {
if (paragraph.leftIndent == 36) {
setAndLogStyle(paragraph, "Aufz\u00E4hlungszeichen");
}
if (paragraph.leftIndent == 72) {
setAndLogStyle(paragraph, "Aufz\u00E4hlungszeichen 2");
}
}
}
OfficeHelpers.UI.notify("Finished");
await context.sync();
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
}
catch (error) {
OfficeHelpers.UI.notify(error);
OfficeHelpers.Utilities.log(error);
}
}
/** Helper */
function logParagraph(paragraph: Word.Paragraph):void {
console.log("Paragraph <" + paragraph.text.substr(0, 20) + "...>");
}
function setAndLogStyle(paragraph: Word.Paragraph, style: string): void {
paragraph.style = style;
logParagraph(paragraph);
console.log(` style = ${style}`);
}
function setAndLogStyleBuiltIn(paragraph: Word.Paragraph, style: string): void {
paragraph.styleBuiltIn = style;
logParagraph(paragraph);
console.log(` styleBuiltIn = ${style}`);
}
// To avoid compile errors for the undeclared "fabric" object, declare it to the type-system here.
declare var fabric;
language: typescript
template:
content: "<section class=\"ms-font-m\">\n\t<p>Select range to fix styles. If there is no selection, the paragraph where the cursor is placed is the selected.</p>\n</section>\n\n<div class=\"ms-Toggle ms-Toggle--textRight\" id=\"tgl-increase-heading\">\n\t<span class=\"ms-Toggle-description\">Increase headings?</span>\n\t<input type=\"checkbox\" class=\"ms-Toggle-input\" />\n\t<label for=\"tgl-increase-heading\" class=\"ms-Toggle-field\" tabindex=\"0\">\n <span class=\"ms-Label ms-Label--off\">Do not increase</span> \n <span class=\"ms-Label ms-Label--on\">Increase</span> \n </label>\n</div>\n\n\n<button id=\"run\" class=\"ms-Button\">\n<span class=\"ms-Button-label\">Run</span>\n</button>\n<label class=\"ms-Label\">See log for changes.</label>\n"
language: html
style:
content: |
/* Your style goes here */
language: css
libraries: |-
// Office.js
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
// NPM libraries
jquery@3.1.1
office-ui-fabric-js@1.4.0/dist/js/fabric.min.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
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js
core-js@2.4.1/client/core.min.js
// IntelliSense: Use dt~library_name for DefinitelyTyped or URLs to d.ts files
@types/office-js
@types/jquery
@types/core-js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts
@studermartin
Copy link
Author

Correct name and description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment