Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hjkim-MSFT/3961ccc057286ca7c13833fa14f0cb29 to your computer and use it in GitHub Desktop.
Save hjkim-MSFT/3961ccc057286ca7c13833fa14f0cb29 to your computer and use it in GitHub Desktop.
A workaround for the issue that when importing content having sectPr in a cc already having a sectPr, the original sectPr does not get replaced
name: Replace cc content with sectPr issue - workaround
description: >-
A workaround for the issue that when importing content having sectPr in a cc
already having a sectPr, the original sectPr does not get replaced
host: WORD
api_set: {}
script:
content: >
$("#setup").click(() => tryCatch(setup));
$("#run").click(() => tryCatch(run));
var ooxml = `<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:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:pPr><w:sectPr><w:pgSz
w:w="12240" w:h="15840" /><w:pgMar w:top="1440" w:right="1440"
w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"
/><w:cols w:space="708" /><w:docGrid w:linePitch="360"
/></w:sectPr></w:pPr></w:p></w:body></w:document></pkg:xmlData></pkg:part></pkg:package>`;
async function run() {
await Word.run(async (context) => {
let controls = context.document.contentControls;
controls.load("items");
await context.sync();
controls.items[0].insertOoxml(ooxml, Word.InsertLocation.replace);
await context.sync();
});
}
async function setup() {
await clear();
await Word.run(async (context) => {
context.document.body.insertParagraph("Paragraph", Word.InsertLocation.start);
let p = context.document.body.insertParagraph("Content control content", Word.InsertLocation.end);
let cc = p.insertContentControl();
cc.title = "Content control";
cc.insertOoxml(ooxml, Word.InsertLocation.replace);
await context.sync();
context.document.body.paragraphs.getLast().select();
let s = context.document.getSelection();
s.delete();
await context.sync();
});
}
async function clear() {
await Word.run(async (context) => {
context.document.body.clear();
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);
}
}
language: typescript
template:
content: |-
<button id="setup" class="ms-Button">
<span class="ms-Button-label">Setup</span>
</button>
<button id="run" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
language: html
style:
content: |
/* Your style goes here */
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
https://appsforoffice.microsoft.com/lib/1/hosted/office.d.ts
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
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts
jquery@3.1.1
@types/jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment