Created
May 31, 2024 10:52
-
-
Save richie5um/69509b092dd9b5a90ffb9c8b139ff99a to your computer and use it in GitHub Desktop.
This file contains hidden or 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: References | |
description: '' | |
host: WORD | |
api_set: {} | |
script: | |
content: | | |
$("#check-fields").on("click", () => tryCatch(checkFields)); | |
$("#get-fields").on("click", () => tryCatch(getFields)); | |
$("#get-bookmarks").on("click", () => tryCatch(getBookmarks)); | |
async function checkFields() { | |
await Word.run(async (context) => { | |
var fields = context.document.body.fields.load("items"); | |
fields.load(["code", "result", "locked", "type", "data", "kind"]); | |
await context.sync(); | |
if (fields.items.length > 0) { | |
for (let field of fields.items) { | |
if (field.isNullObject) { | |
} else { | |
let match = /(_Toc|_Ref)([^ ]+)/gi.exec(field.code); | |
if (match !== undefined && match !== null && match.length > 0) { | |
let bookmark = match[0]; | |
// console.log(`Field Code: ${field.code}`); | |
// console.log(`Finding Bookmark: ${bookmark}`); | |
var bookmarkRange = context.document.getBookmarkRangeOrNullObject(bookmark); | |
bookmarkRange.load("*"); | |
await context.sync(); | |
if (bookmarkRange === undefined || bookmarkRange === null || bookmarkRange.isNullObject) { | |
// console.log(`${bookmark} => Invalid`); | |
} else { | |
// console.log(`${bookmark} => ${JSON.stringify(bookmarkRange)}`); | |
// console.log(`${bookmark} => ${bookmarkRange.text}`); | |
let bookmarkCCs = bookmarkRange.contentControls; | |
bookmarkCCs.load("*"); | |
let fieldRange = field.result; | |
let fieldCCs = fieldRange.contentControls; | |
fieldCCs.load("*"); | |
await context.sync(); | |
let bookmarkSmzCCs = bookmarkCCs.items.filter((x) => x.tag.startsWith("smz-token:")); | |
let fieldSmzCCs = fieldCCs.items.filter((x) => x.tag.startsWith("smz-token:")); | |
let bookmarkTags = bookmarkSmzCCs.map((x) => x.tag); | |
let bsmz = bookmarkTags.map((x) => { | |
let a = /(\[.+\])$/gi.exec(x); | |
return a?.length > 0 ? a[1] : ""; | |
}); | |
let fieldTags = fieldSmzCCs.map((x) => x.tag); | |
let fsmz = fieldTags.map((x) => { | |
let a = /(\[.+\])$/gi.exec(x); | |
return a?.length > 0 ? a[1] : ""; | |
}); | |
bsmz = bsmz.sort(); | |
fsmz = fsmz.sort(); | |
let bsmzstr = JSON.stringify(bsmz); | |
let fsmzstr = JSON.stringify(fsmz); | |
if (bsmzstr !== fsmzstr) { | |
console.log("-----"); | |
console.log(`${bookmark} => ${bookmarkRange.text}`); | |
console.log(`${bookmark} Reference => ${JSON.stringify(fsmz)}`); | |
console.log(`${bookmark} Bookmarked => ${JSON.stringify(bsmz)}`); | |
} | |
} | |
} else { | |
// console.log('Ignoring Field'); | |
} | |
} | |
} | |
} | |
}); | |
} | |
async function getFields() { | |
await Word.run(async (context) => { | |
var fields = context.document.body.fields.load("items"); | |
fields.load(["code", "result", "locked", "type", "data", "kind", "parentBody/text"]); | |
await context.sync(); | |
if (fields.items.length > 0) { | |
for (let field of fields.items) { | |
console.log("-----"); | |
if (field.isNullObject) { | |
console.log("This document has no fields."); | |
} else { | |
console.log( | |
"Code of first field: " + field.code, | |
"Result of first field: " + JSON.stringify(field.result), | |
"Type of first field: " + field.type, | |
"Is the first field locked? " + field.locked, | |
"Kind of the first field: " + field.kind | |
// "ParentText: " + field.parentBody.text, | |
); | |
if (JSON.stringify(field.result).includes("Security")) { | |
field.select(); | |
} | |
} | |
} | |
} | |
}); | |
} | |
async function getBookmarks() { | |
await Word.run(async (context) => { | |
var bookmarks = context.document.body.getRange().getBookmarks(true, true); | |
// bookmarks.load('*'); | |
// fields.load(["code", "result", "locked", "type", "data", "kind", "parentBody/text"]); | |
await context.sync(); | |
// console.log(JSON.stringify(bookmarks.)); | |
for (let bookmark of bookmarks.value) { | |
var bookmarkRange = context.document.getBookmarkRangeOrNullObject(bookmark); | |
bookmarkRange.load("*"); | |
await context.sync(); | |
if (bookmarkRange.isNullObject) { | |
console.log("This bookmark is invalid"); | |
} else { | |
console.log(`${bookmark} => ${JSON.stringify(bookmarkRange)}`); | |
} | |
} | |
}); | |
} | |
// 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"> Smz</section><section class="setup ms-font-m"> <h3>Check Fields</h3> <button id="check-fields" class="ms-Button"> <span class="ms-Button-label">Check Fields</span> </button></section><section class="setup ms-font-m"> <h3>Fields</h3> <button id="get-fields" class="ms-Button"> <span class="ms-Button-label">Log Fields</span> </button></section><section class="setup ms-font-m"> <h3>Bookmarks</h3> <button id="get-bookmarks" class="ms-Button"> <span class="ms-Button-label">Log Bookmarks</span> </button></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/1/hosted/office.js | |
@types/office-js | |
[email protected]/dist/css/fabric.min.css | |
[email protected]/dist/css/fabric.components.min.css | |
[email protected]/client/core.min.js | |
@types/core-js | |
[email protected] | |
@types/[email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment