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
PropertiesService.getDocumentProperties().setProperty(key,value)
.then(function(result) {
/// it definitely worked....
})
.catch(function(err) {
// it failed...
});
var value = PropertiesService.getDocumentProperties().getProperty (key);
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 || {});
- Add-on for decrypting columns in Google Sheets
- Chord Snip
- Color Arranger
- Debugging Office JavaScript API add-ins
- Dicers
- Dicers Pro and advanced features
- Import, export and mix container bound and standalone Apps Script projects
- Inline libraries in an Apps Script project
- Measure round trip and execution time from add-ons
- Merging sheets from multiple sources and encrypting selected columns
- 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
- Refreshing an oauth token in add-on
- 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