GAS – make a variable from a variable

Clever little ditty this one. Somtimes you need to create a dynamic variable on the fly, perhaps using data pulled in from a sheet or elsewhere that goes into a variable, but you then need to use that value as a variable name and assign it a value. This is how it is done:

I want the value of the variable value01 to become a variable name, result. I want to give result the value green, which is currently in the variable output01. If using a string in output01 you have to have the extra “‘” in the eval(), otherwise a number will not need them.

Leave a Reply

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