34

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?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Ada Lovelace
  • 835
  • 2
  • 8
  • 20

2 Answers2

51

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();
}
Kirby
  • 2,847
  • 2
  • 32
  • 42
Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32
  • 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? – Antonio Ooi Sep 29 '16 at 17:48
  • When referencing the class defined in this method as Library in apps script, the error says "ReferenceError: 'ClassName' is not defined", in this case: "Polygon" is not defined. Anything possibly wrong? – Antonio Ooi Sep 29 '16 at 18:17
  • 5
    **UPDATE:** It works now. When referencing the class through the Apps Script library, it has to be `var x = new LibraryName.ClassName();` – Antonio Ooi Sep 29 '16 at 18:49
  • 2
    what is LibraryName? Name of the file containing the class? – Ada Lovelace Jun 01 '17 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: https://developers.google.com/apps-script/guides/libraries – Cameron Roberts Jun 01 '17 at 17:06
  • This is great; I upvoted this and your helpful comments. In case anyone is wondering: all of the THIS variables/functions in the "class" can be accessed externally. For example you can call poly1.height externally and get the height. If you want privacy, you can declare the variables using var like "var asdf = 1234;" instead of "this.asdf = 1234;" - please see this link for more details: http://phrogz.net/js/classes/OOPinJS.html – Augustine C Jun 06 '17 at 23:12
  • @AntonioOoi - can you please add an answer and expand a little on your comment that it works now? – Ada Lovelace Jun 07 '17 at 15:29
  • @AdaLovelace Sorry for the late reply. Cameron Roberts was right, just follow his comment. – Antonio Ooi Aug 25 '17 at 08:32
  • 2
    @CameronRoberts : You can use classes now in Google app scripts since Chrome v8 is used : https://developers.google.com/apps-script/guides/v8-runtime – moudug Mar 28 '20 at 11:30
  • FYI. Syntax highlighting doesn't seem to work in v8 classes and it doesn't look you can assign a class to a global variable (but tbh I don't know if this normal js behaviour). – Baddie Nov 24 '20 at 01:51
  • It is partially true. A class definition in GAS is properly possible. See the answer https://stackoverflow.com/a/60459759/8383884 – Marcello Marino Jun 07 '21 at 14:51
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.

RetroCraft
  • 327
  • 1
  • 2
  • 13
Noel Yap
  • 18,822
  • 21
  • 92
  • 144