问题描述
我在寻找一种方法来存储Mac上office.js加载项的持久设置时遇到了麻烦.
I am having trouble finding a way to store persistent settings for an office.js add-in on Mac.
在Windows上,localStorage可以完美地工作,因为它可以保存通过关闭和打开Word保留的设置.
On windows localStorage works perfect as it saves settings that persist through closing and opening Word.
在Mac上,localStorage不会持久地关闭和打开Word,即使刷新或关闭并打开外接程序也是如此.
On Mac localStorage does not persist closing and opening Word, not even through a refresh or closing and opening of the add-in.
这是一个简单的代码示例:
Here is a simple code sample:
var settingString = 'mySetting';
var oldValue = localStorage.getItem(settingString);
write('oldValue: "' + oldValue + '"');
var d = new Date();
var newValue = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
localStorage.setItem(settingString, newValue);
write('newValue: "' + newValue + '"');
推荐答案
iOS当前存在一个错误,目前仍在阻止我们修复此localStorage问题.同时,您有两种可能的解决方法:
iOS currently has a bug that's preventing us from fixing this localStorage issue yet. In the meantime, you have two potential workarounds:
曲奇饼
如果您希望该设置在文档中保持不变,请使用JavaScript cookie( w3schools文档),直到修复该错误为止:
If you want the setting to be persisted across documents, use JavaScript cookies (w3schools doc) until the bug is fixed:
var settingString = 'mySetting';
var oldValue;
var myCookies = document.cookie.split(';');
for(var i = 0;i < myCookies.length;i++){
var myCookie = myCookies[i].trim();
if(myCookie.indexOf(settingString + "=") == 0){
oldValue = myCookie.substring(settingString + 1,myCookie.length);
}
}
write('oldValue: "' + oldValue + '"');
var d = new Date();
var newValue = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
var expiry = d.setTime(d.getTime() + (14 * 86400000)); // # milliseconds in a day
document.cookie = settingString + "=" + newValue + "; expires=" + expiry.toGMTString();
write('newValue: "' + newValue + '"');
设置
如果仅在当前文档中保留该值就足够了,则可以使用Office设置API( Office.js设置对象文档):
If it's sufficient for you to persist the value only in the current document, you can use the Office Settings API (Office.js Settings object doc):
var settingString = 'mySetting';
var oldValue = Office.context.Settings.get(settingString);
write('oldValue: "' + oldValue + '"');
var d = new Date();
var newValue = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
Office.context.Settings.set(settingString, newValue);
Office.context.Settings.saveAsync(function(asyncResult){
write('newValue: "' + newValue + '"');
});
-Michael Saunders,Office加载项的程序经理
-Michael Saunders, program manager for Office add-ins
这篇关于OSX/Mac中的加载项永久设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!