2016-06-28 39 views
1

我有一個.NET在線的.NET MVC項目。加載項啓動成功,但不加載「Home.js」,它調用Office.initialize將數據插入詞的主體。Office.js函數不適用於Word Online的MVC應用程序

這裏是_Layout.cshtml:

<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title></title> 
    @Styles.Render("~/bundles/css") 
    @Scripts.Render("~/bundles/modernizr") 
</head> 
<body ng-app=""> 
    <div class="container"> 
     @RenderBody() 
    </div> 
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/Scripts/Office/1/office.js") 
    @Scripts.Render("~/Scripts/Home.js") 
    @Scripts.Render("~/bundles/bootstrap") 
    @Scripts.Render("~/Scripts/angular.min.js") 
</body> 
</html> 

而且Home.js:

(function() { 
    "use strict"; 

    Office.initialize = function (reason) { 
     $(document).ready(function() { 
      if (!Office.context.requirements.isSetSupported('WordApi', '1.1')) { 
       return; 
      } 
      loadSampleData(); 
     }); 
    }; 

    function loadSampleData() { 

     // Run a batch operation against the Word object model. 
     Word.run(function (context) { 
      // Create a proxy object for the document body. 
      var body = context.document.body; 
      // Queue a commmand to clear the contents of the body. 
      body.clear(); 
      // Queue a command to insert text into the end of the Word document body. 
      body.insertText("This is a sample text inserted in the document", 
          Word.InsertLocation.end); 

      // Synchronize the document state by executing the queued commands, and return a promise to indicate task completion. 
      return context.sync(); 
     }) 
     .catch(errorHandler); 
    } 

    function errorHandler(error) { 
     console.log("Error: " + error); 
     if (error instanceof OfficeExtension.Error) { 
      console.log("Debug info: " + JSON.stringify(error.debugInfo)); 
     } 
    } 
})(); 

謝謝。

+0

我也想評論角引導問題。我試圖徹底清除應用程序的角色,問題依然存在。 – michaelmilone

+0

home.js是否加載?也就是說,如果你在頂部輸入console.log,它會打印嗎?如果是的話,如果你把一個console.log作爲Office.initialze的第一行,它會打印嗎? –

+0

感謝您的回覆。是的,它確實打印。 – michaelmilone

回答

0

只有在WordAPI v1.1可用時才執行。該API僅在Word 2016 for Windows, Mac and iPad中受支持。如果您使用Word Online或Word 2013,則此代碼將退出(return)而不執行任何操作。

另一個注意事項是,您在<body>的底部引用了Office.js和Home.js。您只需5秒即可完成Office.initialize()並將其放在頁面底部,在瀏覽器執行該功能之前,需要加載所有內容。雖然這對於網站來說絕對是正確的做法,但會導致加載項出現超時錯誤。這些參考文獻should always be placed within the <header>

+0

上返回true,非常好,謝謝marc! – michaelmilone

相關問題