35

I want to create a class inside my script.

Google Apps Script language is based on javaScript, so I took an example from a javaScript manual:

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

However, this doesn't work. I get this error message:

Missing ; before statement. (line 1, file "Code")

Does that mean it's not possible to create new classes in google scripts?

Or is there a different syntax I'm supposed to use?

2 Answers 2

53

Update:

As of spring 2020 Google has introduced a new runtime for Apps Script which supports Classes.
New scripts use this runtime by default, while older scripts must be converted. A prompt is displayed in the script editor, from which you can convert your scripts.

// V8 runtime
class Rectangle {
  constructor(width, height) { // class constructor
    this.width = width;
    this.height = height;
  }

  logToConsole() { // class method
    console.log(`Rectangle(width=${this.width}, height=${this.height})`);
  }
}

const r = new Rectangle(10, 20);
r.logToConsole();  // Outputs Rectangle(width=10, height=20)

Original (old) answer:

Historically Javascript is a "classless" language, classes are a newer feature which haven't been widely adopted yet, and apparently are not yet supported by Apps Script.

Here's an example of how you can imitate class behaviour in Apps Script:

var Polygon = function(height, width){
  this.height = height;
  this.width = width;
  
  this.logDimension = function(){
    Logger.log(this.height);
    Logger.log(this.width);
  }
};

function testPoly(){
  var poly1 = new Polygon(1,2);
  var poly2 = new Polygon(3,4);
  
  Logger.log(poly1);
  Logger.log(poly2);
  poly2.logDimension();
}
11
  • Then how about the JSDoc for this case? I can't make the autocomplete works for classes defined in this method. Any idea how to do that? Sep 29, 2016 at 17:48
  • 5
    UPDATE: It works now. When referencing the class through the Apps Script library, it has to be var x = new LibraryName.ClassName(); Sep 29, 2016 at 18:49
  • 2
    what is LibraryName? Name of the file containing the class? Jun 1, 2017 at 16:58
  • 4
    @AdaLovelace Antonio was referring to a situation where you import an external Apps Script project into another as a Library, in which case you assign the LibraryName when you set it up. If the class is defined within the same project (regardless of the .gs file), you just use the class name directly as in my example above. Here is the documentation on libraries: developers.google.com/apps-script/guides/libraries Jun 1, 2017 at 17:06
  • 2
    @CameronRoberts : You can use classes now in Google app scripts since Chrome v8 is used : developers.google.com/apps-script/guides/v8-runtime
    – moudug
    Mar 28, 2020 at 11:30
7

TypeScript can now be used with GAS. To use it, download the script files using clasp then convert them to TypeScript by changing the file suffixes to .ts. More info can be found at https://developers.google.com/apps-script/guides/typescript. Once this is done, class can be used.

An example of TypeScript being used to implement Google Sheets custom functions can be found at https://github.com/november-yankee/science-test-grading.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.