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:
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?
2
Answers
Although I’m not sure whether I could correctly understand your expected result, how about the following sample script?
Sample script 1:
COLUMNS_INDEXES.NICKNAME
,COLUMNS_INDEXES.LAST_NAME
,COLUMNS_INDEXES.NICKNAME
, andCOLUMNS_INDEXES.ADDRESS
are 2, 3, 4, and 5, respectively.Sample script 2:
Sample script 3:
RECOMMENDED SOLUTION
Try reading through the different methods of array manipulation that’s available on the internet, as they can help you accomplish what you’d like. I also created a sample script using Google Apps Script for you to easily declare your objects in the order that you’ll put them.
SAMPLE SCRIPT
OUTPUT
UPDATE
This script is an example of how you can access the first name in another function. I also created dummy data.
OUTPUT