Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Dictionary javascript class in Google Apps Script?

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?

like image 295
user1935166 Avatar asked Sep 11 '25 12:09

user1935166


1 Answers

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]);
like image 143
Pointy Avatar answered Sep 14 '25 01:09

Pointy