【问题标题】:Office task pane app: how to get the whole document in an OOXml string?Office 任务窗格应用程序:如何在 OOXml 字符串中获取整个文档?
【发布时间】:2016-07-04 16:16:51
【问题描述】:

我正在开发一个需要访问整个文档的 Office 任务窗格应用。我知道有一个 API getFileAsync() https://msdn.microsoft.com/en-us/library/office/jj220084.aspx

Office.context.document.getFileAsync(fileType [, options], callback);

但是,fileType 只能是三个值:compressedpdftext

compressed

以字节数组的形式返回 Office Open XML (OOXML) 格式的整个文档(.pptx 或 .docx)。

pdf

以字节数组的形式返回 PDF 格式的整个文档。

text

仅将文档的文本作为字符串返回。 (仅限文字)

当为compressed时,返回值为字节数组。 如何获取 OOXml 字符串?

或者是否有 API 可以选择文档中的所有内容,以便我可以使用 getSelectedDataAsync() API?

【问题讨论】:

    标签: ms-office office365


    【解决方案1】:

    如果有人找到这个帖子,我设法使用zip.js 解决了这个问题。

    var dataByteArray = [];
    
    function getDocumentAsOoxml() {
        Office.context.document.getFileAsync("compressed", { sliceSize: 100000 }, function (result) {
            if (result.status == Office.AsyncResultStatus.Succeeded) {
                // Get the File object from the result.
                var myFile = result.value;
                var state = {
                    file: myFile,
                    counter: 0,
                    sliceCount: myFile.sliceCount
                };
                getSlice(state);
            }
        });
    }
    
    function getSlice(state) {
        state.file.getSliceAsync(state.counter, function (result) {
            if (result.status == Office.AsyncResultStatus.Succeeded) {
                readSlice(result.value, state);
            }
        });
    }
    
    function readSlice(slice, state) {
        var data = slice.data;
        // If the slice contains data, create an HTTP request.
        if (data) {
            dataByteArray = dataByteArray.concat(data);
            state.counter++;
            if (state.counter < state.sliceCount) {
                getSlice(state);
            } else {
                closeFile(state);
            }
        }
    }
    
    function closeFile(state) {
        // Close the file when you're done with it.
        state.file.closeAsync(function (result) { });
    
        // convert from byte array to blob that can bre read by zip.js
        var byteArray = new Uint8Array(dataByteArray);
        var blob = new Blob([byteArray]);
    
        // Load zip.js library
        $.getScript("/Scripts/zip.js/zip.js", function () {
            zip.workerScriptsPath = "/Scripts/zip.js/";
            // use a BlobReader to read the zip from a Blob object
            zip.createReader(new zip.BlobReader(blob), function (reader) {
                // get all entries from the zip file
                reader.getEntries(function (entries) {
                    if (entries.length > 0) {
                        for (var i = 0; i < entries.length; i++) {
                            var entry = entries[i];
                            // find the file you are looking for
                            if (entry.filename == 'word/document.xml') {
                                entry.getData(new zip.TextWriter(), function (text) {
    
                                    // text contains the entry data as a String
                                    doSomethingWithText(text);
    
                                    // close the zip reader
                                    reader.close(function () {
                                        // onclose callback
                                    });
                                }, function (current, total) {
                                    // onprogress callback
                                });
                                break;
                            }
                        }
                    }
                });
            }, function (error) {
                // onerror callback
            });
        });
    }
    

    希望将来会有更简单的方法。

    【讨论】:

    • 将以下内容替换为 PowerPoint:entry.filename == 'ppt/presentation.xml'
    【解决方案2】:

    这有点晚了。

    我最近一直在使用任务窗格应用程序,事实证明,OOXML 是本机压缩的(除非我弄错了)。

    我最好的建议是找出字符串编码的编码,并使用该编码类型进行解码。我敢打赌它是 UTF-8。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多