I am trying to make and use a Dictionary object in Google Apps Script. Before, I used a very long switch statement for my script the first time around, and when I submitted it to the Gallery, the person who approved it asked why I didn't use a Javascript Dictionary object instead. I looked into how to use Dictionary objects, but now my script wont' work because Google Apps Script doesn't understand the command:
Components.utils.import("resource://gre/modules/Dict.jsm");
This 'import' line of code was copied straight from this Javascript Reference webpage: http://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Dict.jsm
How do I include this javascript library needed to make it work, or what is the Google Apps Script alternative to a javascript Dictionary object?
Any time you've got a switch
statement that looks something like:
switch ( someValue ) {
case "string1": doSomething( valueForString1 ); break;
case "string2": doSomething( valueForString2 ); break;
// ...
case "stringN": doSomething( valueForStringN ); break;
}
you can replace that with:
var dict = {
"string1": valueForString1,
"string2": valueForString2,
// ...
"stringN": valueForStringN
};
doSomething( dict[ someValue ] );
The values can be anything, of course: strings, numbers, objects, functions, whatever. And you probably would want to check for a value being missing from the dictionary:
if (dict[someValue]) doSomething(dict[someValue]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With