Skip to content

Instantly share code, notes, and snippets.

@studermartin
Last active December 6, 2018 07:12
Show Gist options
  • Save studermartin/116f918b5bbda673f0647b28838869b4 to your computer and use it in GitHub Desktop.
Save studermartin/116f918b5bbda673f0647b28838869b4 to your computer and use it in GitHub Desktop.
Shared with Script Lab
name: Prepare forms
description: ''
author: MartinStuderHTWChur
host: WORD
api_set: {}
script:
content: |-
// Script to form processing.
//
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));
async function run() {
console.log("Starting ... ");
await Word.run(async (context) => {
let selection = context.document.getSelection();
context.load(selection, "text");
await context.sync();
console.log(selection.text);
context.load(selection, "contentControls");
await context.sync();
let contentControls = selection.contentControls;
contentControls.load();
await context.sync();
let removeContentControls: boolean = $("#tgl-remove-contentControls").children('label').hasClass('is-selected');
console.log("Found " + contentControls.items.length + " content controls.");
for (let i = 0; i < contentControls.items.length; i++) {
let contentControl:Word.ContentControl = contentControls.items[i];
if (removeContentControls) {
contentControl.delete(false);
console.log("Removed content control ");
}
}
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);
}
}
// 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 prepare forms 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-remove-contentControls\">\n\t<span class=\"ms-Toggle-description\">Remove content controls?</span>\n\t<input type=\"checkbox\" class=\"ms-Toggle-input\" />\n\t<label for=\"tgl-remove-contentControls\" class=\"ms-Toggle-field\" tabindex=\"0\">\n <span class=\"ms-Label ms-Label--off\">Do not remove</span> \n <span class=\"ms-Label ms-Label--on\">Remove</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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment