2016-12-09 3 views
1

these instructionsに基づいてYeoman Office generatorを使用して、オフィスアドインを作成しています。Wordアドインフルテキストを取得するには?

デフォルトのアドインには、ワードドキュメントで選択されたテキストを取得する機能があります。私は関数を変更して、代わりにドキュメントのフルテキストを取得しようとしています。次のように

My機能コードは次のとおりです。

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固有のAPIを使用した場合

+1

のタグ 'hackproductivity'は何ですか? – j08691

+1

@ j08691削除しました。ここでの議論はhttp://meta.stackoverflow.com/questions/339308/i-was-told-by-someone-associated-with-microsoft-to-create-a-tag –

答えて

1

、あなたのコードがに簡素化することができます。

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

関連する問題