Skip to content Skip to sidebar Skip to footer

Dynamically Define Variable Name And Assigning Value In Google Apps Script

I`m writing a script to generate a form in Google Apps Script. The idea is, that the user should select his/her name from a dropdown list, and then be transferred to the question b

Solution 1:

As far as I read up so far, eval() is almost always a bad choice, should be handled with caution and mostly never used. Using dynamic variables in such way is also a bad programming logic. I've never seen a case where it is a must, and your case obviously isn't. You can easily get around it with an object, just define a generic object var myVariables = {}, and then start assigning its properties dynamically for your variables.

var myVariables = {};
myVariables[ "beginning" ] = 1;

for( i = 0; i < 10; i++){
  myVariables[ ("beginning" + i) ] = i;
}


Logger.log( myVariable[ "beginning5" ] ); //Loggs 5

Post a Comment for "Dynamically Define Variable Name And Assigning Value In Google Apps Script"