object programming in app script

998 views
Skip to first unread message

whands

unread,
Sep 21, 2023, 6:36:12 AM9/21/23
to Google Apps Script Community
Hi,

I'm currently moving to oop and i have issues working with Libraries.

-----------------
Here is the class i have declared in a project called "Library"
-----------------

class MyApp {
constructor(sheetName) {
this.sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
this.lastRow = 11; // Update this as needed
this.iCol = 9; // Update this as needed
this.xCol = 24; // Update this as needed
}

replaceCharacters() {
const originalCampaignName = this.sheet.getRange(2, this.iCol, this.lastRow - 1).getValues();

for (let i = 0; i < originalCampaignName.length; i++) {
if (originalCampaignName[i][0].length > 0) {
try {
const campaignNameCleaned = originalCampaignName[i][0].replace(/[^a-zA-Z0-9&_-]/g, '_');
this.sheet.getRange(i + 2, this.xCol).setValue(campaignNameCleaned.toUpperCase());
} catch (error) {
this.sheet
.getRange(i + 2, this.xCol).setValue('').setFontColor('red').setFontSize(11);
continue;
}
}
}
}
}



-----------------
and here is my main function
-----------------

function main() {
// Initialize your app with the sheet name
const sheetName = "Result 1";
// Create an instance of the MyApp class from the library
const myApp = Library.MyApp(sheetName);

// Call the replaceCharacters method
myApp.replaceCharacters();
}

-----------------
i have correctly linked my the Library in my project but i still have the following error when executed:
TypeError: Library.MyApp is not a function

Thanks for your help

Tanaike

unread,
Sep 21, 2023, 8:22:50 AM9/21/23
to Google Apps Script Community
In your showing script, please modify as follows.

Library side:
From

class MyApp {

To

var MyApp = class MyApp {

Client side:
From

const myApp = Library.MyApp(sheetName);

To

const myApp = new Library.MyApp(sheetName);


whands

unread,
Sep 21, 2023, 9:40:46 AM9/21/23
to Google Apps Script Community
Thanks Tanaike

it's working now.
But why do i need to declare the class as a variable ?

thanks
david

Tanaike

unread,
Sep 21, 2023, 8:55:52 PM9/21/23
to Google Apps Script Community
Thank you for replying.

About "it's working now.", I'm glad your issue was resolved.

About "But why do i need to declare the class as a variable ?", in the current stage, it seems that the Class object cannot be directly used as the Google Apps Script. The Class object cannot be seen at the client side. I believe that this will be resolved in the future update. So, as the current workaround, in order to use the Class object as the library, I proposed the above modification. I apologize for this situation.

whands

unread,
Oct 2, 2023, 12:30:11 PM10/2/23
to Google Apps Script Community
Hi Tanaike,

Thanks again for your help, much appreciated !
I have an other question regarding the Class declaration.

I've tried to create a GlobalParameters class.
My aim was to manage efficiently in one location all my parameters used in all my other classes.
Here an example : 

var GlobalParameters = class GlobalParameters {
constructor(){
this.param1 : param1;
this.param2 : param2;
this.param3 : param3;
}

var MyOtherClass = class MyOtherClass extends  GlobalParameters {
constructor(){
super();
} 
myFunction(){
this.param1... is processed
this.param2... is processed
etc...
}

Unfortunately, it's not working that way. After few research it looks like i should do like this :
class A
constructor() { 
this.data = data;
 } 
class B
constructor() { 
this.instanceOfA = new A(); 
console.log(this.instanceOfA.data); 
 }
 } 

What do you think ?
Do you have a more efficient option ?

Many thanks for your help
David

Tanaike

unread,
Oct 2, 2023, 8:59:49 PM10/2/23
to Google Apps Script Community
About your new question, I cannot understand the relationship between your new question and the library. I apologize for my poor English skill. Can I ask you about the details of it?

Rafael Brum

unread,
May 28, 2025, 3:16:41 AMMay 28
to Google Apps Script Community
Hi everyone, I believe this is relevant and also related to the original question.

I got everything working, I can instantiate classes and they work as expected. However, I can't make the autocomplete work. 
On run time I can access methods and props, but not on the IDE.

Don't know if someone could hop in and shine some light.

Jonathan Butler

unread,
May 28, 2025, 9:29:01 AMMay 28
to google-apps-sc...@googlegroups.com
I think the factory pattern is the best design pattern to use when creating libraries in Google Apps Script. Instead of creating a class and assign it to a variable to be accessed, create a function that always returns a new instance of that class. This should fix your autocomplete issue.

So Instead of

let RectangleClass = class Rectangle {}

do

function rectangle (myVar1,myVar2)
{
      return new Rectangle(myVar1,myVar2);
}


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/61a0b0ff-d8eb-4f38-ab17-39cc67ebc93fn%40googlegroups.com.
Message has been deleted

DimuDesigns

unread,
May 28, 2025, 1:10:09 PMMay 28
to Google Apps Script Community

To make autocomplete work you'll need to add JSDoc comments to your code.

And using the factory pattern also helps as pointed out by Jonathan.

One thing you can do is study how well-established libraries incorporate JSDoc comments.

Check out Google's OAuth2 library, you can find the GAS project linked below:
That library uses prototypal inheritance instead of classes (but classes are just syntantic sugar anyway - its all prototypal under the hood) but the factory methods all have JSDoc comments that allow for code completion.
Reply all
Reply to author
Forward
0 new messages