Created
August 19, 2022 10:18
-
-
Save richie5um/0806850916575a66938f2323dcb3547d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Comments (Ooxml) | |
description: '' | |
host: WORD | |
api_set: {} | |
script: | |
content: > | |
$("#run").click(() => tryCatch(insertComment2)); | |
// async function insertComment3() { | |
// // get data as OOXML | |
// Office.context.document.getSelectedDataAsync(Office.CoercionType.Ooxml, | |
function (result) { | |
// if (result.status === "succeeded") { | |
// var selectionAsOOXML = result.value; | |
// console.log(selectionAsOOXML); | |
// var bodyContentAsOOXML = | |
selectionAsOOXML.match(/<w:body.*?>(.*?)<\/w:body>/)[1]; | |
// // perform manipulations to the body | |
// // it can be difficult to do to OOXML but with som regexps it should | |
be possible | |
// bodyContentAsOOXML = bodyContentAsOOXML.replace(/error/g, 'rorre'); | |
// reverse the word 'error' | |
// // insert the body back in to the OOXML | |
// selectionAsOOXML = | |
selectionAsOOXML.replace(/(<w:body.*?>)(.*?)<\/w:body>/, '$1' + | |
bodyContentAsOOXML + '<\/w:body>'); | |
// // replace the selected text with the new OOXML | |
// Office.context.document.setSelectedDataAsync(selectionAsOOXML, { | |
coercionType: Office.CoercionType.Ooxml }, function (asyncResult) { | |
// if (asyncResult.status === "failed") { | |
// console.log("Action failed with error: " + | |
asyncResult.error.message); | |
// } | |
// }); | |
// } | |
// }); | |
// } | |
async function insertComment2() { | |
Office.context.document.getSelectedDataAsync(Office.CoercionType.Ooxml, function(result) { | |
if (result.status == "succeeded") { | |
// Get the OOXML returned from the getSelectedDataAsync call. | |
var ooxml = result.value; | |
// console.log(ooxml); | |
$("#code").innerText(ooxml); | |
// var comment = getCommentAsOoxml(selectedText); | |
Office.context.document.setSelectedDataAsync(ooxml, { coercionType: Office.CoercionType.Ooxml }, function( | |
asyncResult | |
) { | |
if (result.status == "succeeded") { | |
console.debug("Set SelectedDataAsync"); | |
} else if (asyncResult.status == "failed") { | |
console.error("Action failed with error: " + asyncResult.error.message); | |
} | |
}); | |
} | |
}); | |
} | |
async function insertComment() { | |
await Word.run(async (context) => { | |
let range = context.document.getSelection(); | |
await context.sync(); | |
range.insertOoxml( | |
` | |
<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:p> | |
<w:r> | |
<w:t>Comment</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> | |
`, | |
"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); | |
} | |
} | |
language: typescript | |
template: | |
content: |- | |
<button id="run" class="ms-Button"> | |
<span class="ms-Button-label">Run</span> | |
</button> | |
<div> | |
<pre id="code"> | |
Hello | |
</pre> | |
</div> | |
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 | |
[email protected]/dist/css/fabric.min.css | |
[email protected]/dist/css/fabric.components.min.css | |
[email protected]/client/core.min.js | |
@types/core-js | |
[email protected] | |
@types/[email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment