Ich möchte meine benutzerdefinierten Daten in PowerPoint-Präsentationsdatei speichern. Ich verwende dieses Beispiel: https://github.com/OfficeDev/Excel-Add-in-JavaScript-PersistCustomSettingsOffice-Add-In PowerPoint - benutzerdefinierte Daten in Datei speichern funktioniert nicht
Aber ich möchte speichern meine Daten während Office-Add-in entladen (für Beispiel, wenn Benutzer eine Präsentationsdatei schließen).
So verwende ich window.onunload = function() {}...
für erkennen, wenn Präsentationsdokument zu schließen beginnen.
Und während Powerpoint zu schließen, wobei das Verfahren:
Office.context.document.settings.saveAsync()
Rückkehr Fehler: Saving failed. error: An internal error has occurred.
Codebeispiel
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');
}
});
}
}
})();
danke für die Klarstellung, onunload/onclose wäre gut funktional für den aktuellen App-Zustand zu speichern. – Jarikus
Die Herausforderung hier ist onunload/onclose in Ihrem Add-In auftreten, nachdem die Verbindung zwischen Ihrem Add-In und dem Dokument wurde abgerissen. Im Moment gibt es keine Möglichkeit, das Schließen zu erfassen, bevor das Abreißen stattfindet. –