Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abusch419/357e06a8c2fda077a06be2be0885cfb2 to your computer and use it in GitHub Desktop.
Save abusch419/357e06a8c2fda077a06be2be0885cfb2 to your computer and use it in GitHub Desktop.
Minimal reproduction example for stack overflow
name: Tracking a range object in a loop (1)
description: 'Minimal reproduction example for stack overflow '
host: WORD
api_set: {}
script:
content: >
OfficeExtension.config.extendedErrorLogging = true;
$("#run").click(() => tryCatch(main));
async function main() {
if (ranges.length > 0) {
await Word.run(ranges[0], removeComments);
}
// This works.
await Word.run(insertFirstComment);
// This does not work. If you comment out line 10
// and uncomment line 13 you'll see an error in the console.
// await Word.run(insertCommentsInLoop);
}
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
console.error(error);
}
}
let ranges = [];
const searchQuery = "foo";
async function removeComments(context) {
for (const range of ranges) {
range.insertOoxml(ooxmlWithoutComment, "Replace");
range.untrack();
}
await context.sync();
ranges = [];
}
async function insertCommentsInLoop(context) {
const rangeCollection = context.document.body.search(searchQuery);
rangeCollection.load("length");
await context.sync();
for (const range of rangeCollection.items) {
range.insertOoxml(commentOoxml, "Replace");
range.track();
ranges.push(range);
}
await context.sync();
}
async function insertFirstComment(context) {
const rangeCollection = context.document.body.search(searchQuery);
rangeCollection.load('length');
await context.sync();
const range = rangeCollection.getFirstOrNullObject();
range.load('isNullObject');
await context.sync();
if (!range.isNullObject) {
range.insertOoxml(commentOoxml, "Replace");
range.track();
ranges.push(range);
await context.sync();
} else {
console.log(`Did not find ${searchQuery} in the document`);
}
}
const commentOoxml = `<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/_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/comments" Target="comments.xml"
xmlns="http://schemas.openxmlformats.org/package/2006/relationships" />
</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:commentRangeStart w:id="0" />
<w:r>
<w:t>It was selected text!</w:t>
</w:r>
<w:commentRangeEnd w:id="0"/>
<w:r>
<w:commentReference w:id="0"/>
</w:r>
</w:p>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
<pkg:part xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" pkg:name="/word/comments.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml">
<pkg:xmlData>
<w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:comment xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:id="0" w:author="Patent Theory" w:initials="PT">
<w:p>
<w:r>
<w:t>COMMENT_TEXT</w:t>
</w:r>
</w:p>
</w:comment>
</w:comments>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/_rels/comments.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships>
</pkg:xmlData>
</pkg:part>
</pkg:package>`;
const ooxmlWithoutComment = `<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/_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/comments" Target="comments.xml"
xmlns="http://schemas.openxmlformats.org/package/2006/relationships" />
</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:r>
<w:t>It was selected text!</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
<pkg:part xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" pkg:name="/word/comments.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml">
<pkg:xmlData>
<w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
</w:comments>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/_rels/comments.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships>
</pkg:xmlData>
</pkg:part>
</pkg:package>`;
language: typescript
template:
content: "<p>\n To see the expected behavior, type <code>foo</code> into the document then click the run button. Click the run button a second time to remove the comment. This is the expected behavior.\n</p>\n<p>\n\tTo trigger the error, open the script and comment out line 10, then uncomment line 13. Then type <code>foo</code> into the document and click the run button. Clicking the run button a second time will cause the error. \n</p>\n<button id=\"run\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Run</span>\n</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