【问题标题】:Retrieve entire Word document in task pane app / office.js在任务窗格 app / office.js 中检索整个 Word 文档
【发布时间】:2015-01-19 19:57:07
【问题描述】:

在 Word 2013(桌面)和 office.js 中工作时,我们会看到一些围绕用户选择的功能(GetSelectedDataAsyncSetSelectedDataAsync),但没有任何东西可以让您查看整个 (OpenXML) 文档。我错过了什么吗?

【问题讨论】:

  • 我有同样的请求,API 似乎不允许这样做。

标签: ms-office openxml apps-for-office


【解决方案1】:

Office.context.document.getFileAsync 将让您以 3 种格式选择整个文档:

  • 压缩:以字节数组的形式返回 Office Open XML (OOXML) 格式的整个文档(.pptx 或 .docx)
  • pdf:以字节数组的形式返回 PDF 格式的整个文档
  • text:仅将文档的文本作为字符串返回。 (仅限 Word)

这是取自MSDN的示例:

var i = 0;
var slices = 0;

function getDocumentAsPDF() {

    Office.context.document.getFileAsync("pdf", { sliceSize: 2097152 }, function (result) {
        if (result.status == "succeeded") {
            // If the getFileAsync call succeeded, then
            // result.value will return a valid File Object.
            myFile = result.value;
            slices = myFile.sliceCount;
            document.getElementById("result").innerText = " File size:" + myFile.size + " #Slices: " + slices;

            // Iterate over the file slices.
            for (i = 0; i < slices; i++) {
                var slice = myFile.getSliceAsync(i, function (result) {
                    if (result.status == "succeeded") {
                        doSomethingWithChunk(result.value.data);
                        if (slices == i) // Means it's done traversing...
                        {
                            SendFileComplete();
                        }
                    }
                    else
                        document.getElementById("result").innerText = result.error.message;
                });
            }
            myFile.closeAsync();
        }
        else
            document.getElementById("result2").innerText = result.error.message;
    });
}

【讨论】:

    【解决方案2】:

    这不完全是您所要求的(它只是文档的正文),但它帮助了我......所以我把它贴在这里,因为它是我在谷歌上搜索我的问题时登陆的地方。

    此处的文档:https://dev.office.com/reference/add-ins/word/body 建议 getOoxml() 将为您获取文档的正文。还有一个属性 text 会返回纯文本内容。

    此 API 的工作方式并不太直接 - 但是在线文档中的示例确实有助于入门。

    一切顺利,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      相关资源
      最近更新 更多