In Google Apps Script there are 3 PropertiesServices. Document, User and Script. Office doesn’t have a direct equivalent, but we can create something for the Document and Script/User properties fairly easily. It’s not as good, but it’s a start.
PropertiesService.getDocumentProperties
Each Office document can have some properties stored alongside them. This is pretty much directly equivalent to the DocumentProperties service (except it would be visible to multiple scripts accessing the same document). The polyfill below sets up a PropertiesService namespace, and can be used just like Apps Script. For example
PropertiesService.getDocumentProperties().setProperty(key, value);
PropertiesService.getUserProperties
Behind the scenes, it uses localstorage for UserProperties (it would only be visible if using the same machine). You can use it just like from Apps Script.
PropertiesService.getUserProperties().setProperty(key, value);
Promises
The DocumentProperties service is asynchronous for writing or removing, and return Promises. Not something we need to worry about in Apps Script – so if you want to check for errors, or you need the operation to have finished before continuing .. you can do this PropertiesService.getDocumentProperties().setProperty(key,value)
.then(function(result) {
/// it definitely worked....
})
.catch(function(err) {
// it failed...
});
The get method is synchronous, so this is just fine.var value = PropertiesService.getDocumentProperties().getProperty (key);
The UserProperties service methods are all synchronous.
The code
/** polyfill for Apps Script Properties service
* only does user and document properties
* @namespace PropertiesService
*/
var PropertiesService = (function (ps) {
// uses for document properties
ps.getDocumentProperties = function () {
return {
setProperty: function (key, value) {
Office.context.document.settings.set(key, value);
return ps.flushDocumentProperties();
},
getProperty: function (key) {
return Office.context.document.settings.get(key);
},
deleteProperty: function (key) {
Office.context.document.settings.remove(key);
return ps.flushDocumentProperties();
}
}
};
// the settings only write to in memory copy
// need to write async to doc for permamnence
ps.flushDocumentProperties = function () {
return new Promise(function (resolve, reject) {
Office.context.document.settings.saveAsync(function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
reject(asyncResult);
}
else {
resolve(asyncResult);
}
});
});
}
// uses for user properties
ps.getUserProperties = function () {
return {
setProperty: function (key, value) {
return localStorage.setItem(key, value);
},
getProperty: function (key) {
return localStorage.getItem(key);
},
deleteProperty: function (key) {
return localStorage.removeItem(key);
}
}
};
return ps;
})(PropertiesService || {});
The code for this on Github.
Subpages
- Chord Snip
- Color Arranger
- Debugging Office JavaScript API add-ins
- Dicers
- Dicers Pro and advanced features
- Measure round trip and execution time from add-ons
- Merging slide templates with tabular data
- Office Add-ins – first attempt
- Orchestrating competing google and Office framework loads
- Plotting maps with overlays Sheets add-on starter
- Promise implementation for Apps Script Stripe payments
- Repeatable add-on settings layouts and style
- Sheets API – Developer Metadata
- SlidesMerge add-on
- Unpicking the Google Picker
- Watching for changes in an Office add-in
- When test add-ons doesn’t work
- Sankey Snip