I hired a Google Apps Script developer and they delivered unreadable code. What’s going on?

A few days ago I got an update from a Google Apps Script group that I subscribe to. The poster shared a jumble unreadable single-line mess of code with a message that read something similar to:

I hired a Google Apps Script developer and they shared me this. It works, but it is unreadable. What should I do?

Obfuscated Code Google Apps Script
Google Apps Script code that has been intentionally obfuscated. Click to expand!

It looked like the poster’s code has been deliberately obfuscated or obscured to make it difficult for someone to read. The code solves the problem for the client and runs as it should. It’s just near impossible to read or edit without seeing the original source code.

Picket lines were formed in the comments section of the post and salvos ensued. It wasn’t a Reddit-level skirmish, but it was getting there before it ran out of steam.

The post and the ensuing comments did raise some good points of view that are well worth considering as a freelance Google Apps Script developer or as someone who plans to hire a developer.

Continue reading “I hired a Google Apps Script developer and they delivered unreadable code. What’s going on?”

Why Object.create() doesn’t work in Google Apps Script and how to fix it.

If you have found this post while searching to try to figure out why your JavaScript Object.create() method is being a big stupid head isn’t working how you would expect it would in Google Apps Script, you have come to the right place.

You’ve probably used Object.create() in a JavaScript project in the past to create a new object based on a template object and then added another property or so to it.

As you can see in the example above, it works just dandy in JavaScript. The original object "a" is not affected if we add a new property to the "newObj" object.

However, if you try and do the same thing in Google Apps Script, as you have no doubt discovered, you will get the following results:

So what’s going on? What can I do to fix this? Read on or select from the menu to get to the bits you are interested in.

Some Solutions

Spread Syntax to the rescue

With the modernisation of Google Apps Script with their introduction of the V8 runtime back in February of 2020, we can now use the JavaScript Spread Syntax ({...obj})to create a shallow clone of the original object (a.k.a object literal).

This is my favourite approach. It is quite neat and elegant, but perhaps not as explicit in its meaning for someone reviewing your code.

If you create a new object and add a property to it

Dam it! Now I’ve got that Beyonce song in my head. ✔💍*

Let’s fix the original code with our spread syntax.

You can see on line 5 we have a nice neat spread syntax to add to your "newObj" object. This shallow clone of "a" allows us to keep the original object unchanged while importing the properties into the new object.

Just like on line 6, we can now add new properties to our new object without fear of updating the original source object, "a".

*Just when you thought the humour couldn’t set a lower standard, the goat’s gone subterranean.

Joining two objects together in Google Apps Script

Probably the most useful part of using the spread operator is that we can join or concatenate two objects together.

In our next example, we will join objects "a" and "b" together to form one new object with our spread syntax. To do this we simply separate the two objects by a comma and use the spread syntax on both.

As you can see, "c" has now combined "a" and "b" successfully while both "a" and "b" still maintain their individuality.

Adding an object to an existing object in Google Apps Script

One final thing you might find useful is to concatenate one or more objects to an existing object. We can do this with the Object. assign() method. This method takes a target object as its first parameter and then any number of objects as its subsequent parameters.

Adding one object to another.

Let’s say if I wanted to add all of "a"‘s properties to "b". It would look a little something like this:

As you can see, object "b" now has the properties of "a", but if we change the "name" property of "b" to something else, then "a" will not be affected.

Adding multiple objects to an existing object.

Likewise, we can add multiple objects to an existing object with the Object.assign() method.  In the next example, we will make a copy of "a", "b" and "c" objects and join it to object "d".

As you have probably guessed by now, objects, "a","b" and "c" are not affected by the Object.assign() but now "d" has assimilated a copy of the properties of the other objects.

Create and Publish a Google Workspace Add-on with Apps Script Course

Need help with Google Workspace development?

Go something to solve bigger than Chat GPT?

I can help you with all of your Google Workspace development needs, from custom app development to integrations and security. I have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.

Schedule a free consultation today to discuss your needs and get started or learn more about our services here.


The video.

What’s going on?

Well, I found a relatively obscure reference to the reason why Object.create() performs how we expect it to in StackOverflow that was answered by a former legendary member of the Google Apps Script team, Corey Goldfeder.

Corey informs us that in Google Apps Script when objects are passed to Object.create() method, the properties of the original object are non-enumerable by default. This means that they cannot be explicitly iterated through with a for…in loop inside the create() method.

Corey goes on to say that Object.create() uses the Object.defineProperties() syntax which, unless the enumerable property is explicitly defined previously, will be defined as non-enumerable and not be added to the newly created object.

You could resolve or test this by explicitly defining the enumerability of each property in "a" as true which would then give you the result you were expecting.

Note the changes to the object 'a' and the parameters in the create method (Line 6).

 

That’s it for creating new objects based on existing objects in Google Apps Script. I would love to know how you used this code or if you have another approach. Feel free to share your ideas or questions in the comments below.

If you found the tutorial, useful why not shout me a cuppa.

~Yagi