Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hjkim-MSFT/500d6a42187d6d9936112427fc22be39 to your computer and use it in GitHub Desktop.
Save hjkim-MSFT/500d6a42187d6d9936112427fc22be39 to your computer and use it in GitHub Desktop.
Workaround for the issue that Word inserts an undesirable paragraph when replacing a content control content with a table.
name: Replace cc content with table issue - Workaround
description: >-
Workaround for the issue that Word inserts an undesirable paragraph when
replacing a content control content with a table.
host: WORD
api_set: {}
script:
content: >
$("#clear").click(() => tryCatch(clear));
$("#setup").click(() => tryCatch(setup));
$("#run").click(() => tryCatch(run));
const ooxml = `<pkg:package
xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"><pkg:part
pkg:name="/word/_rels/document.xml.rels"
pkg:contentType="application/vnd.openxmlformats-package.relationships+xml"
pkg:padding="256"><pkg:xmlData><Relationships
xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship
Id="rId1"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
Target="styles.xml"/></Relationships></pkg:xmlData></pkg:part><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:tbl><w:tblPr><w:tblStyle
w:val="TableGrid"/><w:tblW w:w="0" w:type="auto"/><w:tblLook w:val="04A0"
w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0"
w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid><w:gridCol
w:w="9350"/></w:tblGrid><w:tr><w:tc><w:tcPr><w:tcW w:w="9350"
w:type="dxa"/></w:tcPr><w:p><w:r><w:t>Table
content</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p/></w:body></w:document></pkg:xmlData></pkg:part><pkg:part
pkg:name="/word/styles.xml"
pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"><pkg:xmlData><w:styles
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
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:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid"
xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex"
mc:Ignorable="w14 w15 w16se w16cid"><w:style w:type="table" w:default="1"
w:styleId="TableNormal"><w:name w:val="Normal Table"/><w:uiPriority
w:val="99"/><w:semiHidden/><w:unhideWhenUsed/><w:tblPr><w:tblInd w:w="0"
w:type="dxa"/><w:tblCellMar><w:top w:w="0" w:type="dxa"/><w:left w:w="108"
w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:right w:w="108"
w:type="dxa"/></w:tblCellMar></w:tblPr></w:style><w:style w:type="table"
w:styleId="TableGrid"><w:name w:val="Table Grid"/><w:basedOn
w:val="TableNormal"/><w:uiPriority w:val="39"/><w:pPr><w:spacing w:after="0"
w:line="240" w:lineRule="auto"/></w:pPr><w:tblPr><w:tblBorders><w:top
w:val="single" w:sz="4" w:space="0" w:color="auto"/><w:left w:val="single"
w:sz="4" w:space="0" w:color="auto"/><w:bottom w:val="single" w:sz="4"
w:space="0" w:color="auto"/><w:right w:val="single" w:sz="4" w:space="0"
w:color="auto"/><w:insideH w:val="single" w:sz="4" w:space="0"
w:color="auto"/><w:insideV w:val="single" w:sz="4" w:space="0"
w:color="auto"/></w:tblBorders></w:tblPr></w:style></w:styles></pkg:xmlData></pkg:part></pkg:package>`;
async function run() {
OfficeExtension.config.extendedErrorLogging = true;
await Word.run(async (context) => {
context.document.body.tables.getFirst().select();
let sel = context.document.getSelection();
sel.delete();
await context.sync();
context.document.body.insertOoxml(ooxml, Word.InsertLocation.replace);
await context.sync();
});
}
async function setup() {
await clear();
await Word.run(async (context) => {
let t = context.document.body.insertTable(1, 1, Word.InsertLocation.start, [["Table content"]]);
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="clear" class="ms-Button">
<span class="ms-Button-label">Clear</span>
</button>
<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