Word加载项获取完整的文档文本?

时间:2016-12-09 15:10:10

标签: javascript ms-word ms-office office-js

我根据Yeoman Office generator使用these instructions撰写办公室附加内容。

默认加载项有一个函数,可以在word文档中选择文本。我试图修改函数,而不是获取文档的全文。

我的功能代码如下:

function getDataFromDoc(){
  Office.context.document.getFileAsync(Office.CoercionType.Text,
  function(result){
    jQuery('#get-data-from-selection').click(getDataFromSelection);
      if (result.status === Office.AsyncResultStatus.Succeeded) {
        console.dir(result);
        app.showNotification('The selected text is:', '"' + result.value + '"');
      } else {
        app.showNotification('Error:', result.error.message);
      }
    }
  );
}

返回一个对象,但当我使用console.dir(result)并查看对象时,我无法在任何地方看到文档文本。

如何修改此功能以便我获取word文档的全部内容?

1 个答案:

答案 0 :(得分:1)

如果您使用了特定于Word的API,则可以将代码简化为:

Word.run(function(context) {
    // Insert your code here. For example:
    var documentBody = context.document.body;
    context.load(documentBody);
    return context.sync()
    .then(function(){
        console.log(documentBody.text);
    })
});

我认为这更方便。无论如何,getFileAsync方法只为您提供文件的处理程序,然后您需要对其进行切片以获取内容。看看这个例子:

function getFile(){
        Office.context.document.getFileAsync(Office.FileType.Text, { sliceSize: 4194304  /*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();
                    console.log(docdataSlices); // docDataSlices contains all the text....
                }
                else {
                    getSliceAsync(file, ++nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
                }
            }
            else {
                gotAllSlices = false;
                file.closeAsync();
                app.showNotification("getSliceAsync Error:", sliceResult.error.message);
            }
        });
}