为什么ContentControl.insertOoxml(original_content_control_ooxml)似乎创建了新的不可见内容控件?

时间:2019-07-05 14:57:31

标签: office-js

我希望能够隐藏/取消隐藏某些内容控件的内容。为此,我获得了内容的ooxml,使用customXmlParts保存并加载,然后返回insertOoxml()

目前,我可以隐藏和取消隐藏一次。在第二次隐藏尝试中,context.document.contentControls.getByTitle()给了我两倍于我在屏幕上看到的内容控件的数量。 ID的一半是新的。当我尝试其中的getOoxml()时,系统会产生errorLocation的{​​{1}}错误。

我承诺Office 2013 api如下:

ContentControlCollection.getItem

并且以下函数尝试隐藏/取消隐藏两次。在第二次迭代中,我在function cxpAddCustomXmlPartAsync(xml): Promise<Office.AsyncResult<Office.CustomXmlPart>>{ return new Promise((resolve)=>{ Office.context.document.customXmlParts.addAsync(xml, (asyncResult)=>{ resolve(asyncResult); }) }); } function cxpGetCustomXmlPartByIdAsync(id): Promise<Office.AsyncResult<Office.CustomXmlPart>> { return new Promise((resolve)=>{ Office.context.document.customXmlParts.getByIdAsync(id, (result: Office.AsyncResult<Office.CustomXmlPart>)=>{ resolve(result) }) }); } function cxpDeleteXmlPartAsync(part:Office.CustomXmlPart){ return new Promise((resolve)=>{ part.deleteAsync((result: Office.AsyncResult<void>)=>{ resolve(result) }); }); } function cxpGetXmlAsync(part: Office.CustomXmlPart): Promise<Office.AsyncResult<string>> { return new Promise((resolve)=>{ part.getXmlAsync((result)=>{ resolve(result); }); }); } 的行上得到了错误。

//*

1 个答案:

答案 0 :(得分:1)

这是一个有趣的案例,在某种程度上,您所描述的内容是设计使然,没有在代码中正确处理。实际上,我怀疑那是您想要做的。让我解释。

问题的根源在于,通过执行ccs.items [i] .getOoxml(),您实际上正在获得内容控件的OOXML,包括实际的内容控件。因此,当您保存cc内容并将其替换为OOXML时,实际上是在内容控件中插入内容控件,这就是为什么每次都要将内容控件的大小加倍! :)希望这不会引起混淆...

我认为您真正想要的是OOXML-ize,它是内容控件中的内容,对吗?我假设根据您的代码示例,隐藏在此上下文中并不意味着整个内容控件,而是您想要一个空的内容控件(带有“-”),对吗?

如果是这种情况,则需要获取内容控件“内容”的OOXML。这是您的操作方式:

 const ooxmlObj = ccs.items[i]
        .getRange("start")
        .expandTo(ccs.items[i].getRange("end"))
        .getOoxml(); 

该简单的指令将使您获得内容控件的内部,那就是您需要存储然后替换的OOXML。

请在此处(脚本实验室)https://gist.github.com/JuaneloJuanelo/4a78ab47b7df9594bc7d097842166cbf

上查看工作示例。

我也不确定为什么要将它另存为XML部件...我在示例中使用了一个数组,然后可以使用settings对象保留该数组。由您决定要存储临时OOXML的位置。

快乐的编码!