Office加载项PowerPoint - 将自定义数据存储到文件不起作用

时间:2017-01-27 19:03:00

标签: ms-office powerpoint office-js office-addins

我想将自定义数据存储到PowerPoint演示文稿文件中。我使用这个例子:https://github.com/OfficeDev/Excel-Add-in-JavaScript-PersistCustomSettings

但我想在Office加载项中存储我的数据正在卸载(用户关闭演示文件时的示例)。

所以我在演示文稿开始关闭时使用window.onunload = function () {}...进行检测。

在关闭PowerPoint时,方法:

Office.context.document.settings.saveAsync()

返回错误:Saving failed. error: An internal error has occurred.

代码示例

Home.html中

<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />

    <script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>

    <script src="Home.js" type="text/javascript"></script>
</head>
<body>
    <button id="setData">Set data</button>
</body>
</html>

Home.js

(function () {
    "use strict";

   Office.initialize = function (reason) {
       $(document).ready(function () {

           console.log('>>> Office.initialize()');

            // TODO: If you wanted to save the settings stored in the app's property 
            // bag before the app is closed - for instance, for saving app state - 
            // add a handler to the Internet Explorer window.onunload event.
           window.onunload = function ()
           {
               saveSettingsToFile();
           };

           $('#setData').click(SetData);
        });
   };

   function SetData()
   {
       saveToPropertyBag('dataKey', 'myData');
   }

    // Stores the settings in the JavaScript APIs for Office property bag.
   function saveToPropertyBag(key, value)
   {
       // Note that Project does not support the settings object.
       // Need to check that the settings object is available before setting.
       if (Office.context.document.settings)
       {
           Office.context.document.settings.set(key, value);
       }
       else
       {
           console.log('Error: Feature not supported: the settings object is not supported in this host application');
       }
   }

   function saveSettingsToFile()
   {
       if (Office.context.document.settings) {
           Office.context.document.settings.saveAsync(
           function (asyncResult) {
               if (asyncResult.status == Office.AsyncResultStatus.Failed)
               {
                   console.log('Saving failed. error: ' + asyncResult.error.message);
               }
               else {
                   console.log('Saving success');
               }
           });
       }
   }

})();

1 个答案:

答案 0 :(得分:1)

很遗憾,您无法以这种方式利用onunload事件。当加载项关闭时,它与文档的连接也是如此。

我建议在Office.context.document.settings.saveAsync()函数中调用saveToPropertyBag()。除非你不断对财产包进行更改,否则不应该有太大的开销。或者,如果您需要快速连续进行多项更改,可以立即致电saveAsync()

如果感兴趣的是关闭/关闭事件,则Office Developer Platform UserVoice中的功能请求(https://officespdev.uservoice.com/forums/224641-feature-requests-and-feedback/suggestions/17572624-need-to-have-an-onclose-event-for-the-task-pane-or)可以使用您的投票。 :)