Powerpoint 加载项 - 在 Office.initialize 外部调用时,saveAsync 不会保存设置 [英] Powerpoint Add-In - saveAsync does not save settings when called outside of Office.initialize

查看:65
本文介绍了Powerpoint 加载项 - 在 Office.initialize 外部调用时,saveAsync 不会保存设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 PowerPoint 开发一个内容加载项,当用户使用 Office.context.document.settings 函数(getsetremovesaveAsync).

但是,在 Office.initialize 函数之外进行的 saveAsync 调用似乎并没有实际保存当前设置,因为在重新加载演示文稿时,只有设置初始化时保存的数据被持久化.

为了演示,下面是一个计数器初始化为 1 并在每次加载页面或用户单击增量按钮时递增的最小示例:

index.html

<头><meta charset="UTF-8"/><meta http-equiv="X-UA-Compatible" content="IE=Edge"/><meta name="viewport" content="width=device-width, initial-scale=1"><title>计数器插件</title><!-- Office JavaScript API --><script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.debug.js"></script><身体><div><h1>计数器插件</h1><span id="test-message"></span><button class="ms-Button" id="increment-button">Increment</button>

<script type="text/javascript" src="node_modules/core-js/client/core.js"></script><script type="text/javascript" src="node_modules/jquery/dist/jquery.js"></script><script type="text/javascript" src="app.js"></script></html>

app.js

(function() {Office.initialize = 函数(原因){$(document).ready(function() {切换(原因){案例插入":console.log('加载项刚刚插入.');setState('计数器', 1);案例'documentOpened':console.log('加载项已经是文档的一部分.');setState('counter', getState('counter') + 1);休息;}$('#test-message').text(getState('counter') || 'NOT FOUND');保存状态();//按钮的回调函数$('#increment-button').click(incrementCounter);});};//设置/状态助手函数 getState(name) {返回 Office.context.document.settings.get(name);}函数 setState(name, val) {console.log('Setting', name, 'to', val);Office.context.document.settings.set(name, val);}函数保存状态(){Office.context.document.settings.saveAsync(function(asyncResult) {if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {console.log('设置已保存.');} 别的 {console.warn('设置保存失败', asyncResult.status, asyncResult.error.message);}});}函数增量计数器(){var counter = getState('counter');setState('计数器', 计数器 + 1);$('#test-message').text(getState('counter'));保存状态();}})();

counter-manifest.xml

<Id>71a1a0ed-6cd0-4a6e-ad2d-015b8a8b43cb</Id><版本>1.0.0.0</版本><ProviderName>测试</ProviderName><DefaultLocale>en-US</DefaultLocale><DisplayName DefaultValue="DisplayName"/><Description DefaultValue="Description"/><IconUrl DefaultValue="https://localhost:3000/assets/icon-32.png"/><HighResolutionIconUrl DefaultValue="https://localhost:3000/assets/hi-res-icon.png"/><AppDomains></AppDomains><主机><主机名="演示文稿"/></主机><默认设置><SourceLocation DefaultValue="https://localhost:3000/index.html"/></默认设置><权限>ReadWriteDocument</Permissions><版本覆盖xmlns="http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type="VersionOverridesV1_0"><主机><Host xsi:type="Presentation"></Host></主机><资源></资源></VersionOverrides></OfficeApp>

当插入到演示文稿中时,此加载项首先显示行为正确(计数器按预期递增并且 设置已保存. 每次都显示在控制台日志中).但是,当重新加载演示文稿并查看控制台日志时,我们可以看到 incrementCounter 中的 saveAsync 调用没有效果,即按钮点击的增量不是已注册,但重新加载页面的增量是.

这是一个错误还是我遗漏了什么?

编辑 2018/01/19

不确定您是否更改了某些内容,但今天它似乎运行良好(重新加载不会更改计数器值)

编辑 2018/05/23

错误又回来了,我的结局没有任何变化.

解决方案

您需要调用 refreshAsync 在尝试读取给定值之前.这会将当前设置从文档中传入内存.

试试这个:

function getState(name) {Office.context.document.settings.refreshAsync(function(){返回 Office.context.document.settings.get(name);});}

I'm developing a content add-in for PowerPoint and I'm trying to persist its state when the user reloads the presentation using the Office.context.document.settings functions (get, set, remove and saveAsync).

However, it appears that saveAsync calls made outside of the Office.initialize function do not actually save the current settings, as upon reloading of the presentation, only the settings saved during the initialization are persisted.

For demonstration, here is a minimal example of a counter initialized at 1 and incremented each time the page is loaded, or when the user clicks an Increment button:

index.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Counter Add-In</title>

    <!-- Office JavaScript API -->
    <script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.debug.js"></script>
</head>

<body>
    <div>
        <h1>Counter Add-In</h1>
        <span id="test-message"></span>
        <button class="ms-Button" id="increment-button">Increment</button>
    </div>

    <script type="text/javascript" src="node_modules/core-js/client/core.js"></script>
    <script type="text/javascript" src="node_modules/jquery/dist/jquery.js"></script>
    <script type="text/javascript" src="app.js"></script>
</body>

</html>

app.js

(function() {

  Office.initialize = function(reason) {
    $(document).ready(function() {
      switch (reason) {
        case 'inserted':
          console.log('The add-in was just inserted.');
          setState('counter', 1);
        case 'documentOpened':
          console.log('The add-in is already part of the document.');
          setState('counter', getState('counter') + 1);
          break;
      }
      $('#test-message').text(getState('counter') || 'NOT FOUND');
      saveState();

      // Callback functions for buttons
      $('#increment-button').click(incrementCounter);
    });
  };

  // Settings/State helpers
  function getState(name) {
    return Office.context.document.settings.get(name);
  }

  function setState(name, val) {
    console.log('Setting', name, 'to', val);
    Office.context.document.settings.set(name, val);
  }

  function saveState() {
    Office.context.document.settings.saveAsync(function(asyncResult) {
      if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
        console.log('Settings saved.');
      } else {
        console.warn('Settings save failed', asyncResult.status, asyncResult.error.message);
      }
    });
  }

  function incrementCounter() {
    var counter = getState('counter');
    setState('counter', counter + 1);
    $('#test-message').text(getState('counter'));
    saveState();
  }

})();

counter-manifest.xml

<?xml version="1.0" encoding="UTF-8"?>
<OfficeApp 
    xmlns="http://schemas.microsoft.com/office/appforoffice/1.1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0" 
    xmlns:ov="http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type="ContentApp">
    <Id>71a1a0ed-6cd0-4a6e-ad2d-015b8a8b43cb</Id>
    <Version>1.0.0.0</Version>
    <ProviderName>Test</ProviderName>
    <DefaultLocale>en-US</DefaultLocale>
    <DisplayName DefaultValue="DisplayName" />
    <Description DefaultValue="Description"/>
    <IconUrl DefaultValue="https://localhost:3000/assets/icon-32.png" />
    <HighResolutionIconUrl DefaultValue="https://localhost:3000/assets/hi-res-icon.png"/>
    <AppDomains></AppDomains>
    <Hosts>
        <Host Name="Presentation" />
    </Hosts>
    <DefaultSettings>
        <SourceLocation DefaultValue="https://localhost:3000/index.html" />
    </DefaultSettings>
    <Permissions>ReadWriteDocument</Permissions>
    <VersionOverrides 
        xmlns="http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type="VersionOverridesV1_0">
        <Hosts>
            <Host xsi:type="Presentation"></Host>
        </Hosts>
        <Resources></Resources>
    </VersionOverrides>
</OfficeApp>

When inserted into a presentation, this add-in first appear to behave correctly (the counter is incremented as expected and Settings saved. is displayed in the console log each time). However when reloading the presentation, and looking at the console log, we can see that the saveAsync call in the incrementCounter has no effect, i.e. the increments from the button clicking are not registered, but the increments from reloading the page are.

Is this a bug or am I missing something?

Edit 2018/01/19

Not sure if you changed something on your end, but today it seems to work perfectly (reloading does not change the counter value)

Edit 2018/05/23

The bug came back again without any change from my end.

解决方案

You need to call refreshAsync prior to attempting to read a given value. This will reach the current settings from the document into memory.

Try this:

function getState(name) {
  Office.context.document.settings.refreshAsync(function(){
     return Office.context.document.settings.get(name);
  });
}

这篇关于Powerpoint 加载项 - 在 Office.initialize 外部调用时,saveAsync 不会保存设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆