When working on Sheets API – Developer Metadata it becomes clear that some of the request objects can go to quite a depth, so you end up doing something like this.

var r = {a:{b:{c:{d:{e:true}}}};

This becomes more complicated when say, a.b.c already exists, and has already some other stuff in it. One way round this is to use an extend type function – there is one available in my cUseful.Utils library so you could do this to merge the new data into the old object.

var r = cUseful.Utils.vanExtend ( r ,  a:{b:{c:{d:{e:true}}}} );

But – you still have to construct that complex object.

Here’s an alternative way – You can get this in the cUseful library


or on github


The idea is to be able to specify the parent properties you need in the resulting object as a string

var r = propify ( "a.b.c.d", r); 

will create an object of the required structure, so you can just do this. Any existing objects in the heirarchy will be preserved, and any missing ones will be added

r.a.b.c.d.e = true;

or even this

propify ( "a.b.c.d", r).e=true; 

Here’s some examples and results

function testPropify() {

  // get from library
  var propify = cUseful.Utils.propify;
  Logger.log (propify("details")); // {details={}}
  Logger.log (propify("details.profile")); //{details={profile={}}}
  var card = propify("contact.details.profile", {contact:{}}); 
  Logger.log (card);    // {contact={details={profile={}}}}
  propify ("contact.details.profile.telephones", card);
  Logger.log (card);    // {contact={details={profile={telephones={}}}}}
  // can use a branch of the object to propify it, and will return the branch
  var profile = propify ("telephones", card.contact.details.profile); 
  Logger.log (card);    // {contact={details={profile={telephones={}}}}}
  Logger.log (profile); // {telephones={}}
  // add another couple of branches 
  propify ("emails" , profile);
  propify ("urls", card.contact.details);
  Logger.log (card);   // {contact={details={urls={}, profile={emails={}, telephones={}}}}}
  // we can use the branch
  // or the full object
  Logger.log (card); // {contact={details={urls={}, profile={emails={}, telephones={mobile=1234, home=1234}}}}}
  // should fail because mobile is not an object
  propify ("contact.details.profile.telephones.mobile", card);

The code

Propify code is the library, but also below for convenience. There’s not much to it, but it’s pretty handy.

   * a short cut to add nested properties to a an object
   * @param {object} [base] the base object
   * @param {string} propertyScheme something like "a.b.c" will extend as necessary
   * @return {object} base updated
   ns.propify = function (propertyScheme ,base) {
    // if base not specified, create it
    if (typeof base === typeof undefined) base = {};
    // make sure its an object
    if (typeof base !== typeof {} ) throw 'propify:base needs to be an object';
    // work through the scheme
    (propertyScheme || "").split (".")
      .reduce (function (p,c) {
        // add a branch if not already existing
        if (typeof p === typeof undefined) p = {};
        // make sure we're not overwriting anything
        if (typeof p !== typeof {}) throw 'propify:branch ' + c + ' not an object in ' + propertyScheme;
        // move down the branch
        return p;
      } , base);
    // now should have the required shape
    return base;
For more like this see Google Apps Scripts Snippets
Why not join our community , follow the blog or following me on Twitter