获取整个文档,但File.closeAsync方法不起作用

时间:2016-12-06 19:29:54

标签: javascript office365 office-js microsoft-graph

我正在创建一个Office 365Addin,它可以获取整个打开文档的数据。我使用this作为参考。

当我第一次和第二次单击“#submit”按钮时,一切正常,但之后就失败了。

根据我的阅读,Document.getFileAsync仅允许将2个文档存储在内存中。 File.closeAsync关闭内存上的文档以允许打开其他文档,在我的情况下,它不会返回sliceCount 0和size 0。

File.closeAsync是否存在错误,或者我是否遗漏了文档中的内容?

感谢任何帮助。

谢谢,

function getDocumentAsCompressed() {
Office.context.document.getFileAsync(Office.FileType.Compressed, { sliceSize: 65536 /*64 KB*/ }, 
    function (result) {
        if (result.status == "succeeded") {
        // If the getFileAsync call succeeded, then
        // result.value will return a valid File Object.
        var myFile = result.value;
        var sliceCount = myFile.sliceCount;
        var slicesReceived = 0, gotAllSlices = true, docdataSlices = [];
        app.showNotification("File size:" + myFile.size + " #Slices: " + sliceCount);

        // Get the file slices.
        getSliceAsync(myFile, 0, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
        }
        else {
        app.showNotification("Error:", result.error.message);
        }
});}



function getSliceAsync(file, nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived) {
file.getSliceAsync(nextSlice, function (sliceResult) {
    if (sliceResult.status == "succeeded") {
        if (!gotAllSlices) { // Failed to get all slices, no need to continue.
            return;
        }

        // Got one slice, store it in a temporary array.
        // (Or you can do something else, such as
        // send it to a third-party server.)
        docdataSlices[sliceResult.value.index] = sliceResult.value.data;
        if (++slicesReceived == sliceCount) {
           // All slices have been received.
           file.closeAsync();
           onGotAllSlices(docdataSlices);
        }
        else {
            getSliceAsync(file, ++nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
        }
    }
        else {
            gotAllSlices = false;
            file.closeAsync();
            app.showNotification("getSliceAsync Error:", sliceResult.error.message);
        }
});}


function onGotAllSlices(docdataSlices) {
var docdata = [];
for (var i = 0; i < docdataSlices.length; i++) {
    docdata = docdata.concat(docdataSlices[i]);
}

var fileContent = new String();
for (var j = 0; j < docdata.length; j++) {
    fileContent += String.fromCharCode(docdata[j]);
}}

0 个答案:

没有答案