Apps Script Course: Objects and Classes

Objects and classes, you will probably not create them often in Apps Script. However, here is an overview to understand what they are about.

Especially since you have already used many objects since the beginning of this course.


Objects

An object is a structure that can contain different types of data, here is an example with 3 properties:

const user = {
  name: 'Example',
  firstName: 'Test',
  numPosts: 512
};

You can access these properties as you have done so far:

google apps script object objects classes

And modify them like this:

const user = {
  name: 'Example',
  firstName: 'Test',
  numPosts: 512
};

user.name = 'Preview';
user['firstName'] = 'Tester';
user.numPosts++;

console.log(user); // Displays: { name: 'Preview', firstName: 'Tester', numPosts: 513 }

Or add them like this:

const user = {};

user.username = 'Test';
user['city'] = 'Example';

console.log(user); // Displays: { username: 'Test', city: 'Example' }

Classes

A class is a template of an object, with its properties and/or methods from which you can create as many instances as necessary.

Here is an example of a class with 2 properties and a method:

class User {

  constructor(username, numPosts) {
    this.username = username;
    this.numPosts = numPosts;
  }

  displayInfo() {
    console.log(this.username + ' has written ' + this.numPosts + ' posts.');
  }
}

To create an instance of this class, enter new followed by the name of the class and the constructor arguments:

const user1 = new User('Test', 32);

When you create an instance, the constructor is executed (with the specified arguments).

If you want to be able to use the passed arguments, you must assign them to properties (with this):

this.username = username;
this.numPosts = numPosts;

You can modify a property like this:

const user1 = new User('Test', 32);

user1.username = 'Tester';

And you can also modify a property from a method (with this):

incrementNumPosts() {
  this.numPosts++;
}

Finally, here is the complete code for this example:

class User {

  constructor(username, numPosts) {
    this.username = username;
    this.numPosts = numPosts;
  }

  displayInfo() {
    console.log(this.username + ' has written ' + this.numPosts + ' posts.');
  }

  incrementNumPosts() {
    this.numPosts++;
  }
}

function example() {

  // Creating users
  const user1 = new User('Test', 32);
  const user2 = new User('Example', 512);

  // Changing the username of user 1
  user1.username = 'Tester';

  // Increasing the number of posts of user 2 by 1
  user2.incrementNumPosts();

  // Displaying information for both users
  user1.displayInfo(); // Displays: Tester has written 32 posts.
  user2.displayInfo(); // Displays: Example has written 513 posts.
}

End

The Apps Script course ends here, hoping it will help you in your future developments.

If you enjoyed this course, you have the option to download the PDFs of the entire course. This is a completely optional paid option that contributes to supporting the site and the development of new content.

You will also find other examples of Apps Script codes in the Apps Script Codes section of the site.