Office 加载项 PowerPoint - 将自定义数据存储到文件不起作用
Office add-in PowerPoint - store custom data to file not works
我想将自定义数据存储到 PowerPoint 演示文稿文件中。我用这个例子:https://github.com/OfficeDev/Excel-Add-in-JavaScript-PersistCustomSettings
但我想在 Office 加载项卸载期间存储我的数据(用于用户关闭演示文稿文件时的示例)。
所以我使用 window.onunload = function () {}...
来检测演示文档何时开始关闭。
并且在关闭 PowerPoint 时,方法:
Office.context.document.settings.saveAsync()
return 错误: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');
}
});
}
}
})();
遗憾的是,您不能以这种方式利用 onunload 事件。当 add-in 关闭时,它与文档的连接也关闭。
我建议从 saveToPropertyBag()
函数中调用 Office.context.document.settings.saveAsync()
。除非您经常对 属性 包进行更改,否则不会有太多开销。或者,如果您需要快速连续地进行多项更改,您可以立即调用 saveAsync()
。
如果对 closed/closing 事件感兴趣,目前 ]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)可以使用您的投票。 :)
我想将自定义数据存储到 PowerPoint 演示文稿文件中。我用这个例子:https://github.com/OfficeDev/Excel-Add-in-JavaScript-PersistCustomSettings
但我想在 Office 加载项卸载期间存储我的数据(用于用户关闭演示文稿文件时的示例)。
所以我使用 window.onunload = function () {}...
来检测演示文档何时开始关闭。
并且在关闭 PowerPoint 时,方法:
Office.context.document.settings.saveAsync()
return 错误: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');
}
});
}
}
})();
遗憾的是,您不能以这种方式利用 onunload 事件。当 add-in 关闭时,它与文档的连接也关闭。
我建议从 saveToPropertyBag()
函数中调用 Office.context.document.settings.saveAsync()
。除非您经常对 属性 包进行更改,否则不会有太多开销。或者,如果您需要快速连续地进行多项更改,您可以立即调用 saveAsync()
。
如果对 closed/closing 事件感兴趣,目前 ]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)可以使用您的投票。 :)