我正在开发PowerPoint的内容加载项,并且当用户使用Office.context.document.settings
函数重新加载演示文稿时,我试图保持其状态(get
,{{ 1}},set
和remove
)。
但是,saveAsync
函数之外的saveAsync
调用似乎并未实际保存当前设置,因为重新加载演示文稿时,只会保留初始化期间保存的设置。< / p>
为了演示,这里是一个初始化为1的计数器的最小示例,每次加载页面或用户单击“增量”按钮时都会递增:
的index.html
Office.initialize
app.js
<!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>
反的manifest.xml
(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();
}
})();
当插入到演示文稿中时,此加载项首先看起来表现正常(计数器按预期递增,并且每次都在控制台日志中显示<?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>
)。但是,当重新加载演示文稿并查看控制台日志时,我们可以看到Settings saved.
中的saveAsync
调用无效,即按钮点击的增量未注册,但增量来自重新加载页面。
这是一个错误还是我错过了什么?
编辑2018/01/19
不确定你是否改变了一些东西,但今天看起来效果很好(重新加载不会改变计数器值)
编辑2018/05/23
这个错误再次回来,没有任何改变。
答案 0 :(得分:1)
您需要在尝试读取给定值之前调用refreshAsync
。这将从文档到内存达到当前设置。
试试这个:
function getState(name) {
Office.context.document.settings.refreshAsync(function(){
return Office.context.document.settings.get(name);
});
}