【问题标题】:OfficeJS Word Addin - Modify content control in the whole document ( including header/footer )OfficeJS Word Addin - 修改整个文档中的内容控件(包括页眉/页脚)
【发布时间】:2018-03-20 14:43:14
【问题描述】:

由于 Office API 限制(API 仅限于富文本内容控件)。我们必须将内容控件作为 OOXML 对象插入。它正确插入了内容控件,但 Office API 无法将其识别为内容控件。比如使用office API,如果调用方法获取所有的内容控件,上面的内容控件将不会返回。

如果你运行下面的代码,它不会返回那些像那样插入的内容控件。

// Run a batch operation against the Word object model.
Word.run(function (context) {  
     // Create a proxy object for the content controls collection.
     var contentControls = context.document.contentControls;
}

这里是 OOXML:

        <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/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" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml">
                <w:body>
                <w:sdt>
                    <w:sdtPr>
                    <w:alias w:val="[REPLACE_THIS]" />
                    <w:id w:val="1382295294" />
                    <w:placeholder>
                        <w:docPart w:val="4D926923E99F45DBAF2203E4FEA73047" />
                    </w:placeholder>
                    <w:dataBinding w:xpath="[REPLACE_THIS]" w:storeItemID="{AFE97E21-6B3F-435F-8566-BD38F346ABE4}" />
                    <w:showingPlcHdr />
                    </w:sdtPr>
                    <w:sdtContent>
                    <w:p>
                        <w:r>
                        <w:t>[REPLACE_THIS]</w:t>
                        </w:r>
                    </w:p>
                    </w:sdtContent>
                </w:sdt>
                </w:body>
            </w:document>
            </pkg:xmlData>
        </pkg:part>
        </pkg:package>

由于 Office API 没有任何内置方法来获取这些内容控件并对其进行修改。我必须阅读文档的每个部分,获取 OOXML ,对其进行修改并将其插入回去。有没有其他方法可以做到这一点?因为这是一个非常麻烦且容易出错的过程。

【问题讨论】:

    标签: api office-js office-addins word-2016


    【解决方案1】:

    我假设您正在使用 OOXML 插入其他类型的内容控件(即纯文本、组合、日期等),如果是这种情况,是的,在我们不支持的内容控件集合中,截至现在,除了富文本之外的其他类型的内容控件。这主要是因为 Word Online 的限制,它目前仅支持富文本,并且我们提供的 API 可以保证多平台行为。计划是当它最终支持其他类型时,它们将作为集合的一部分包含在内(因此是 type 属性)。 所以,是的,现在你唯一的出路是获取 OOXML 并通过 XML 处理其他类型的控制。如果您使用富文本内容控件包装它们并且仅获取包装内容控件的 OOXML,则您可能可以针对您获得的 OOXML。

    【讨论】:

    • 我也有同样的问题,出于性能和稳定性的原因,我真的想避免修补整个文档的完整 OOXML。包装内容控件对我来说不是选项,因为现有文档和其他软件预计没有任何包装。这种类型的内容控件无论如何都不能由用户在 Word Online 中进行编辑,因此与原生 Word 应用程序已经存在差异。为 Word for Mac 启用此功能真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多