Hello, I’ve been trying to replace the content inside an existing content control using the Plugin API.
Here’s an example:
I’d like to replace the content control’s content with the actual employee name.
I have 2 cases I’d like to cover - one where I have to insert content from a remote URL docx document (essentially copying the content from one document into a specific place inside the document, marked using a content control) and another one where I simply replace the text inside.
The 1st case I’ve “hacked” together like
window.Asc.plugin.executeMethod(
"GetAllContentControls",
[],
(returnValue) => {
returnValue
.forEach((control) => {
const id = control.InternalId
const key = control.Tag.slice(tagPrefix.length)
const value = values[key]
if (!value) return
const cc = {
Props: {
Id: control.Id,
Lock: 3,
Tag: control.Tag,
},
Url: "http://my-documents.com/document.docx",
}
window.Asc.plugin.executeMethod("InsertAndReplaceContentControls", [
[cc],
])
})
This however doesn’t do what I want - it instead creates a new content control, which is not what I want. I tried passing in an InternalId
to the Props
like so
...
const cc = {
Props: {
Id: control.Id,
Lock: 3,
Tag: control.Tag,
InternalId: control.InternalId
},
Url: "http://my-documents.com/document.docx",
}
window.Asc.plugin.executeMethod("InsertAndReplaceContentControls", [
[cc],
])
I thought that it would solve it, but instead I get a cryptic error
1st question - What’s the recommended way to replace content controls with content from another document?
The 2nd case uses a similar approach
window.Asc.plugin.executeMethod(
"GetAllContentControls",
[],
(returnValue) => {
returnValue
.forEach((control) => {
const id = control.InternalId
const key = control.Tag.slice(tagPrefix.length)
const value = values[key]
if (!value) return
window.Asc.plugin.executeMethod(
"SelectContentControl",
[id],
() => {
window.Asc.plugin.executeMethod("PasteText", [`${value}`])
}
)
})
This approach seems hacky to me because of the selection & pasting – the replacing doesn’t happen atomically and can for example get messy if the editor gets closed.
2nd question - What other ways are available for replacing content control’s content without selecting and pasting text inside them?
3rd question - Can this be achieved using the Asc.plugin.callCommand
method & the Document Builder API?
4th question - What are the differences between the Plugin API and the Document Builder API w/ the callCommand
method?
5th question - How can I wrap a selection into a content control using Plugin API or the Document Builder API?