Migrating user and script properties

The old UserProperties and ScriptProperties services have been deprecated and replaced by the new PropertiesService. In the old service, a user property was available from all scripts to a particular user, and a script property was available to all users of a script. 

In this new service, the getUserProperties() are available to a particular user running the current script, and getScriptProperties() are available to everyone running this script

At some point you'll need to migrate your existing properties to this new PropertiesService. Here's a handy script to do that

function mergeProperties (from, to, replace) {

  var props = from.getProperties();
  var existingProps = to.getProperties();
  
  Object.keys(props).forEach ( function (k) {
    if (replace || !existingProps.hasOwnProperty(k)) {
      existingProps[k] = props[k];
    }
  });
  
  to.setProperties(existingProps);
  return existingProps;
}

function migrate() {

  mergeProperties ( ScriptProperties , PropertiesService.getScriptProperties());
  mergeProperties ( UserProperties , PropertiesService.getUserProperties());

  // check
  Logger.log("script");
  Logger.log( PropertiesService.getScriptProperties().getProperties());
  
  Logger.log("user");
  Logger.log( PropertiesService.getUserProperties().getProperties());
  
}

For help and more information join our forumfollow the blogfollow me on twitter

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.



Comments