在任务窗格app / office.js中检索整个Word文档

时间:2015-01-19 19:57:08

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

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

1 个答案:

答案 0 :(得分:3)

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;
    });
}