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

Ms office Office加载项PowerPoint-将自定义数据存储到文件不起作用,ms-office,powerpoint,office-js,office-addins,Ms Office,Powerpoint,Office Js,Office Addins,我想将自定义数据存储到PowerPoint演示文稿文件中。我用这个例子: 但我希望在Office加载项卸载期间存储数据(对于用户关闭演示文件时的示例) 所以我使用window.onunload=function(){}…来检测演示文档何时开始关闭 在关闭PowerPoint时,方法: Office.context.document.settings.saveAsync() 返回错误:保存失败。错误:发生内部错误。 代码示例 Home.html: <html> <head>

我想将自定义数据存储到PowerPoint演示文稿文件中。我用这个例子:

但我希望在Office加载项卸载期间存储数据(对于用户关闭演示文件时的示例)

所以我使用
window.onunload=function(){}…
来检测演示文档何时开始关闭

在关闭PowerPoint时,方法:

Office.context.document.settings.saveAsync()
返回错误:
保存失败。错误:发生内部错误。

代码示例

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

})();

不幸的是,您不能以这种方式利用onunload事件。当外接程序关闭时,它与文档的连接也将关闭

我建议从
saveToPropertyBag()
函数中调用
Office.context.document.settings.saveAsync()
。除非您经常对属性包进行更改,否则不会有太多开销。或者,如果需要快速连续地进行几次更改,您可以立即调用
saveAsync()


如果对关闭/关闭事件感兴趣,则[Office Developer Platform UserVoice]()中当前有一个功能请求,可以使用您的投票:

感谢您的澄清,onunload/onclose对于存储当前的应用程序状态来说是很好的功能。这里的挑战是,在您的外接程序和文档之间的连接被破坏之后,您的外接程序中的onunload/onclose是否会出现。目前,还没有一种方法可以在拆卸发生之前捕捉到关闭。