Skip to content

Instantly share code, notes, and snippets.

@Zlatkovsky
Created August 3, 2019 00:32
Show Gist options
  • Save Zlatkovsky/17696c0c6cd37d09ad28231b42b20b8f to your computer and use it in GitHub Desktop.
Save Zlatkovsky/17696c0c6cd37d09ad28231b42b20b8f to your computer and use it in GitHub Desktop.
name: Add content controls
description: ''
host: WORD
api_set: {}
script:
content: >
$("#run").click(() => tryCatch(run));
async function run() {
// Gets the current selection and changes the font color to red.
await Word.run(async (context) => {
const range = context.document.getSelection();
range.insertOoxml(xml, "Replace");
await context.sync();
});
}
/** 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);
}
}
const xml = `<?xml version="1.0" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<pkg:package
xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<pkg:xmlData>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex"
xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"
xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex"
xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex"
xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex"
xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex"
xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex"
xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex"
xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink"
xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex"
xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid"
xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml"
xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex wp14">
<w:body>
<w:sdt>
<w:sdtPr>
<w:alias w:val="Location Analysis" />
<w:tag w:val="LocationAnalysis" />
<w:id w:val="625664391" />
<w:lock w:val="contentLocked" />
</w:sdtPr>
<w:sdtContent>
<w:p w:rsidR="00703BB6" w:rsidRDefault="00703BB6">
<w:r>
<w:t xml:space="preserve">The best place to live is in </w:t>
</w:r>
<w:sdt>
<w:sdtPr>
<w:alias w:val="Primary state" />
<w:tag w:val="PrimaryState" />
<w:id w:val="548262910" />
</w:sdtPr>
<w:sdtContent>
<w:r>
<w:t>Washington</w:t>
</w:r>
</w:sdtContent>
</w:sdt>
<w:r>
<w:t xml:space="preserve">. It has a temperature today of </w:t>
</w:r>
<w:sdt>
<w:sdtPr>
<w:alias w:val="Temperature" />
<w:tag w:val="Temp" />
<w:id w:val="-355656400" />
</w:sdtPr>
<w:sdtContent>
<w:r>
<w:t>80 degrees</w:t>
</w:r>
</w:sdtContent>
</w:sdt>
<w:r>
<w:t>.</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00703BB6" w:rsidRDefault="00703BB6">
<w:r>
<w:t xml:space="preserve">On the other hand, </w:t>
</w:r>
<w:sdt>
<w:sdtPr>
<w:alias w:val="Another state" />
<w:tag w:val="AnotherState" />
<w:id w:val="1814749353" />
</w:sdtPr>
<w:sdtContent>
<w:r>
<w:t>California</w:t>
</w:r>
</w:sdtContent>
</w:sdt>
<w:r>
<w:t xml:space="preserve"> probably isn’t bad either.</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00703BB6" w:rsidRDefault="00703BB6" />
</w:sdtContent>
</w:sdt>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
</pkg:package>`
language: typescript
template:
content: |-
<button id="run" class="ms-Button">
<span class="ms-Button-label">Add content controls</span>
</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/1/hosted/office.js
@types/office-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