Is it possible to utilize Class objects within Google Apps Script Libraries?

Recently, I've been working on a JavaScript class that looks like this:

class MyObject {
  constructor(arg1, arg2, arg3) {
    this.field1 = arg1;
    this.field2 = arg2;
    this.field3 = arg3;
  }

  myMethod(param1, param2) {
    return param + param2;
  }
}

Now, I'm considering adding this class to a Google Apps Script library so I can easily reuse it in other projects. Do you think this idea is feasible?

Answer №1

According to the latest v8 runtime update, Google Apps Script now supports the use of classes, whether in libraries or standalone scripts. An example of this implementation can be found in the following documentation:

Classes

Classes offer a way to organize code in a more structured manner with the concept of inheritance. In V8, classes act as a facade for the traditional JavaScript prototype-based inheritance.

To utilize classes, define your class within a script project and deploy it as a Library. Make sure to keep note of the script ID.

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})`);
  }
}

function newRectangle(width, height) {
  return new Rectangle(width, height)
}

Next, in your main application, include the library using the previously noted script ID, create an instance, and invoke the method:

function myFunction() {
  const r = RectangleLibrary.newRectangle(10, 20);
  r.logToConsole();
}

Sample Output:

https://i.sstatic.net/ea0eX.png

https://i.sstatic.net/2llUp.png

Answer №2

contemplate

let MyObject = classs MyObject { ...

in the collection titled LIB

this method will enable you to customize MyObject in the associated endeavor

class SpecialMyObject extends LIB.MyObject { ...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Toggle the visibility of a div by clicking on another div in a

I have created a unique design where a div features a background image of a face, along with a paragraph, two buttons, and an input box inside it. While I know this question has been asked before, my scenario is slightly different. I want the div with the ...

What is the Best Way to Ask Users Not to Delete Local Storage Information?

As I work on developing an application using angularJS, the need to save data locally has arisen. To achieve this, I am utilizing HTML5 local storage. One challenge faced with HTML5 local storage is that when a user clears their browsing data, all stored ...

Discovering and updating a DOM element with a rejuvenating touch: Angular JS

Although not very experienced with Angular JS, here's what I currently have and what I aim to achieve: <div ng-app="MyApp" ng-controller="appController"> <div class="input-group"> <input class="form-control enableEnter" type=" ...

The Vue v-model-bound HTML element is unable to update the Vue instance when a different JavaScript entity binds to it and modifies the data

In my project, I have implemented Vue.js for two-way data binding on a remote control alarm clock. The main code can be found here. You can access the running instance of the server here. While most of the page uses Vue and JavaScript for rendering, I de ...

I am facing difficulties with invoking the popOpen() function within my parameters in JS, HTML, and CSS

I'm currently facing an issue with my code. I am attempting to invoke my openPop() function with the input parameter 'pop' within some of my sensor code, but no pop-up is appearing even though I believe I am calling the popup correctly. If a ...

Issue encountered while designing a 3-column card grid with Bulma and Nextjs while fetching data

Trying to implement a 3 Column Card Grid using Bulma in Nextjs with data fetched from a JSON Endpoint. The data retrieval is successful and working perfectly, but struggling to identify the issue. Is there a way to limit the columns? In Bootstrap, used C ...

Show images exclusively on screens with a smaller resolution

Looking for assistance in modifying the code below to only display the image in mobile view, instead of constantly appearing. <style type="text/javascript> .icon-xyz {float: left; width: 22px;cursor: pointer;background: none;} @me ...

Javascript code for scrolling to the top isn't functioning as expected

I found a helpful code snippet for adding a scroll to top button on my webpage at the following link: http://jsfiddle.net/neeklamy/RpPEe/ Although I implemented the code provided, unfortunately, the scroll to top button is not displaying correctly. Here ...

Ensure that you do not display the result until all Ajax calls have finished processing

When faced with a challenging problem, I persist through multiple attempts and seek your assistance in finding a solution. My current goal is to populate a table with the number of viewers for a specific streamer using the Twitch API. To achieve this, I ...

Modify the base URL with JavaScript

Is it possible to dynamically change the href using JavaScript? I attempted to make this change with the code below in my HTML file, but unfortunately, it didn't work: <base href="/" /> <script type="text/javascript"> function setbasehr ...

Add the div id to the script

Currently, I have a script set up where by clicking on a specific div (for example id=packtoa), it will add a class 'show' to listview items that have a matching class with the clicked div's id. While this system works well, I find myself n ...

APNS functionality is supported by APN providers, but it is not compatible with NodeJS in a production

I've set up a nodeJS script to send APNs. In development, it always works flawlessly. However, when I switch to production, the notifications never go through. Oddly enough, when I use the same notification ID with my production certificate in Easy Ap ...

Incorporate different courses tailored to the specific job role

https://i.stack.imgur.com/iGwxB.jpg I am interested in dynamically applying different classes to elements based on their position within a list. To elaborate: I have a list containing six elements, and the third element in this list is assigned the class ...

Can you please explain the distinction between the statements var a = b = 2 and var a = 2; var b = 2;

Whenever I try to declare a variable within a function, I encounter an issue. var b = 44; function test(){ var a = b = 2; } However, the following code works without any problems: var b = 44; function test(){ var a; var b = 2; } The global ...

Harnessing the power of external Javascript functions within an Angular 2 template

Within the component, I have a template containing 4 div tags. The goal is to use a JavaScript function named changeValue() to update the content of the first div from 1 to Yes!. Since I am new to TypeScript and Angular 2, I am unsure how to establish comm ...

Is it possible to utilize $.each() in combination with $.ajax() to query an API?

I am dealing with an array containing 2 values, and for each value, I need to make an AJAX query to an API to check the stock availability. If there is stock for both values, a certain message should be executed, otherwise, a different message. This check ...

Dynamic Binding of ng-model to DOM Element in AngularJS

I am facing a challenge with my web page where I need to dynamically attach ng-model attributes to some HTML elements that I don't have the ability to edit. What I want to achieve is to have AngularJS re-bind these attributes to the scope. You can fin ...

When an image in AngularJS is clicked, it should display a corresponding list

I'm brand new to Angular development. This is my very first solo project. My goal is to design a page where clicking on an image will display a list of items below it. For example, check out this link for reference: I've searched online for so ...

Using this.setState in ReactJS removes filters

Hey everyone, I've been struggling with a technical issue for the past few days and would really appreciate any hints or solutions. The problem lies in creating a table using the material-table library. Specifically, I need to extract the docID and do ...

Expressing the assignment of arrays inside the req.body object to separate variables

I've been facing some challenges trying to get Express and body-parser to properly interpret the JSON object sent from the Angular side of my app. It seems like there might be an issue with how I'm assigning variables in my syntax. Despite trying ...