Skip to content

Instantly share code, notes, and snippets.

@vikasg603
Created April 17, 2021 16:46
Show Gist options
  • Save vikasg603/5deceb3fafd52a4e3fce5d2cca31c745 to your computer and use it in GitHub Desktop.
Save vikasg603/5deceb3fafd52a4e3fce5d2cca31c745 to your computer and use it in GitHub Desktop.
Inserts, updates, and retrieves content controls.
name: Content control basics
description: 'Inserts, updates, and retrieves content controls.'
host: WORD
api_set: {}
script:
content: |
$("#setup").click(() => tryCatch(setup));
var book_string =
"Genesis|Gen?|Gn|Exodus|Exod?|Ex|Leviticus|Le?v|Numbers|Nu?m|Nu|Deuteronomy|Deut?|Dt|Josh?ua|Josh?|Jsh|Judges|Ju?dg|Jg|Ru(?:th)?|Ru?t|(?:1|i|2|ii) ?Samuel|(?:1|i|2|ii) ?S(?:a|m)|(?:1|i|2|ii) ?Sam|(?:1|i|2|ii) ?Kin(?:gs?)?|(?:1|i|2|ii) ?Kgs|(?:1|i|2|ii) ?Chronicles|(?:1|i|2|ii) ?Chr(?:o?n)?|(?:1|i|2|ii) ?Cr|Ezra?|Nehemiah|Neh?|Esther|Esth?|Jo?b|Psalms?|Psa?|Proverbs|Pro?v?|Ecclesiastes|Ec(?:cl?)?|Song (?:O|o)f Solomon|Song (?:O|o)f Songs?|Son(?:gs?)?|SS|Isaiah?|Isa?|Jeremiah|Je?r|Lamentations|La(?:me?)?|Ezekiel|Eze?k?|Daniel|Da?n|Da|Hosea|Hos?|Hs|Jo(?:el?)?|Am(?:os?)?|Obadiah|Ob(?:ad?)?|Jon(?:ah?)?|Jnh|Mic(?:ah?)?|Mi|Nah?um|Nah?|Habakkuk|Hab|Zephaniah|Ze?ph?|Haggai|Hagg?|Hg|Zechariah|Ze?ch?|Malachi|Ma?l|Matthew|Matt?|Mt|Mark|Ma(?:r|k)|M(?:r|k)|Luke?|Lk|Lu?c|John|Jn|Ac(?:ts?)?|Romans|Ro?m|(?:1|i|2|ii) ?Corinthians|(?:1|i|2|ii) ?C(?:or?)?|Galatians|Gal?|Gl|Ephesians|Eph?|Philippians|Phil|Colossians|Co?l|(?:1|i|2|ii) ?Thessalonians|(?:1|i|2|ii) ?Th(?:e(?:ss?)?)?|(?:1|i|2|ii) ?Timothy|(?:1|i|2|ii) ?Tim|(?:1|i|2|ii) ?T(?:i|m)|Ti(?:tus)?|Ti?t|Philemon|Phl?m|Hebrews|Heb?|Jam(?:es)?|Jms|Jas|(?:1|i|2|ii) ?Peter|(?:1|i|2|ii) ?Pe?t?|(?:1|i|2|ii|3|iii) ?J(?:oh)?n?|Jude?|Revelations?|Rev|R(?:e|v)";
var apoc_books =
"|Tobit?|To?b|Judi(?:th?)?|Jdt|(?:1|2) ?Mac(?:cabees)?|(?:1|2) ?Ma?|Wi(?:sdom)?|Wi?s|Sir(?:ach)?|Ba(?:ruc?h)?|Ba?r";
var unicode_space =
"[\\u0020\\u00a0\\u1680\\u2000-\\u200a\\u2028-\\u202f\\u205f\\u3000]";
//finds book and chapter for each verse that been separated by &,and,etc...
var book_chap =
"((?:(" +
book_string +
")(?:.)?" +
unicode_space +
"*?)?(?:(\\d*):)?(\\d+(?:(?:ff|f|\\w)|(?:\\s?(?:-|–|—)\\s?\\d+)?)))([^a-z0-9]*)";
var regex_string =
"(?:" +
book_string +
")(?:.)?" +
unicode_space +
"*?\\d+:\\d+(?:ff|f|\\w)?(?:\\s?(?:(?:(?:-|–|—)\\s?(?:(?:" +
book_string +
")(?:.)?\\s)?)|(?:(?:,|;|&|&|and|cf\\.|cf)))\\s?(?:(?:(?:vv.|vs.|vss.|v.) ?)?\\d+\\w?)(?::\\d+\\w?)?)*";
const regex = new RegExp(regex_string, "ig");
const book_chap_regex = new RegExp(book_chap, "gi");
const setup = async () => {
await Word.run(async (context) => {
try {
context.document.body.paragraphs.load("items");
await context.sync();
const paragraphs = context.document.body.paragraphs.items;
for (let paragraph of paragraphs) {
const item_string = paragraph.text.toString();
var verse_match = item_string.match(regex);
if (verse_match == null) {
continue;
} else {
let matched, book, chapter, verse;
for (let verse_matched of verse_match) {
while ((matched = book_chap_regex.exec(verse_matched))) {
try {
if (matched[2] != "" && matched[2] != null) {
book = matched[2];
}
if (matched[3] != "" && matched[3] != null) {
chapter = matched[3];
}
verse = matched[4];
var passage = book + " " + chapter + ":" + verse;
const hyperlink = `https://www.biblegateway.com/passage/?search=${passage}&version=NKJV&src=tools`;
const SearchItems = paragraph.search(matched[0]);
SearchItems.load('items');
await paragraph.context.sync();
SearchItems.items[0].hyperlink = hyperlink;
} catch (err) {
console.log(matched[0]);
console.log(err);
}
}
}
}
}
} catch(err) {
console.log(err);
}
});
};
/** 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">
This sample demonstrates how to insert and change content control properties.
</section>
<section class="setup ms-font-m">
<h3>Set up</h3>
<button id="setup" class="ms-Button">
<span class="ms-Button-label">Setup</span>
</button>
</section>
<section class="samples ms-font-m">
<h3>Try it out</h3>
<span class="ms-font-m">Insert content controls on each paragraph.</span>
<button id="insert-controls" class="ms-Button">
<span class="ms-Button-label">Insert</span>
</button><p>
<span class="ms-font-m">Modify content control appearance and content.</span>
<button id="change-controls" class="ms-Button">
<span class="ms-Button-label">Modify content controls</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
https://unpkg.com/xregexp/xregexp-all.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
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