By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Enum in Google Scripts
JavaScript

Javascript – Enum in Google Scripts

Admin
Last updated: 2024/02/10 at 1:23 PM
Admin
Share
3 Min Read
Enum in Google Scripts

Problem:

If I understood correctly, there is no enum in Javascript/Google Scripts (I’m a Javascript novice, so I know just the basics of it). To overcome this, one can define a list of constants, like:

Contents
Problem:Solution:Sample script 1:Sample script 2:Sample script 3:
const COLUMNS_INDEXES
{
  FIRST_NAME: 1,
  LAST_NAME: 2,
  ADDRESS: 3
}

As suggested by this example, I need to define the columns indexes of various entries in a Spreadsheet, so I can access them via constants and have a more readable/editable code.

Needing to manually set every entry is quite annoying and, more important, prone to mistake. Also, when I need to insert a new column between two existing ones, I need to edit all the following entries (more annoying, more risk of errors). Example:

const COLUMNS_INDEXES
{
  FIRST_NAME: 1,
  NICKNAME: 2,
  LAST_NAME: 3,
  ADDRESS: 4
}

A good solution to this could have been to declare the list like this:

const COLUMNS_INDEXES
{
  FIRST_NAME: 1,
  LAST_NAME: FIRST_NAME + 1,
  ADDRESS: LAST_NAME + 1
}

This way when I insert a new entry I just need to edit a couple of lines, and all the following ones gets “automatically updated”:

const COLUMNS_INDEXES
{
  FIRST_NAME: 1,
  NICKNAME: FIRST_NAME + 1,
  LAST_NAME: NICKNAME+ 1,
  ADDRESS: LAST_NAME + 1
}

Awfully, this syntax generates an error… Same if I fully reference the entries, like LAST_NAME: COLUMNS_INDEXES.FIRST_NAME + 1.

So, the question is: Is there a smart way to declare enums/consts without needing to set the value of every entry?

Solution:

Consider the following sample script:

Sample script 1:

const COLUMNS_INDEXES = {
  FIRST_NAME: 1,
  get NICKNAME() { return this.FIRST_NAME + 1 },
  get LAST_NAME() { return this.NICKNAME + 1 },
  get ADDRESS() { return this.LAST_NAME + 1 },
};

console.log(COLUMNS_INDEXES.FIRST_NAME); // 1
console.log(COLUMNS_INDEXES.NICKNAME); // 2
console.log(COLUMNS_INDEXES.LAST_NAME); // 3
console.log(COLUMNS_INDEXES.ADDRESS); // 4
console.log(JSON.stringify(COLUMNS_INDEXES)); // {"FIRST_NAME":1,"NICKNAME":2,"LAST_NAME":3,"ADDRESS":4}

Expand snippet
  • In this case, the values of COLUMNS_INDEXES.NICKNAME, COLUMNS_INDEXES.LAST_NAME, COLUMNS_INDEXES.NICKNAME, and COLUMNS_INDEXES.ADDRESS are 2, 3, 4, and 5, respectively.

Sample script 2:

const keys = ["FIRST_NAME", "NICKNAME", "LAST_NAME", "ADDRESS"];
const COLUMNS_INDEXES = keys.reduce((o, e, i) => (o[e] = i + 1, o), {});
console.log(COLUMNS_INDEXES); // { FIRST_NAME: 1, NICKNAME: 2, LAST_NAME: 3, ADDRESS: 4 }

Expand snippet

Sample script 3:

const keys = ["FIRST_NAME", "NICKNAME", "LAST_NAME", "ADDRESS"];
const COLUMNS_INDEXES = Object.fromEntries(keys.map((e, i) => [e, i + 1]));
console.log(COLUMNS_INDEXES); // { FIRST_NAME: 1, NICKNAME: 2, LAST_NAME: 3, ADDRESS: 4 }

Expand snippet

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How to turn off time picker in tempusDominus Javascript – How to turn off time picker in tempusDominus
Next Article Connect HTMLMediaElement to Android playback controls Javascript – Connect HTMLMediaElement to Android playback controls
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?