【问题标题】:Is there an JS API for Office 365 to operate paragraphs, tables or text in a Word document?Office 365 是否有 JS API 来操作 Word 文档中的段落、表格或文本?
【发布时间】:2015-04-30 02:28:51
【问题描述】:

是否有用于 Office 365 的 Javascript API 来操作 Word 文档中的段落、表格或文本?

我想使用 API 搜索文档中的某些特定文本/字段并替换它们。

【问题讨论】:

  • 在 Stack Overflow 中搜索 API / 库是题外话。试试谷歌/MSDN。
  • 已经完成但效果不佳。@Raptor

标签: office365


【解决方案1】:

一种选择是获取文档为ooxml,然后使用openXml 遍历并替换对象。伪代码:

    function getParas() {
    var documentText = null;
    Office.context.document.getFileAsync("compressed", function (result) {
        if (result.status == "succeeded") {
            //Get the file
            var myFile = result.value;
            myFile.getSliceAsync(0, function (resultSlice) {
                if (result.status == "succeeded") {
                    //We got the file slice. Now we will encode the data and post to a service
                    documentText = OSF.OUtil.encodeBase64(resultSlice.value.data);
                    var doc = new openXml.OpenXmlPackage(documentText);

                    // Replace the first paragraph in the document with the new paragraph.
                    var allParas = doc.mainDocumentPart().getXDocument().descendants(W.p);
                    // .firstOrDefault().toString();

                    for (var i = 0; i < allParas.count(); i++)
                    {
                        var Para = allParas.elementAt(i);
                        var ParaText = Para.toString();
                        app.showNotification(ParaText);
                    }

                }
            });
            myFile.closeAsync();
        }
    })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    相关资源
    最近更新 更多