3

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?

4

1 に答える 1

6

switch次のようなステートメントがある場合はいつでも:

switch ( someValue ) {
case "string1": doSomething( valueForString1 ); break;
case "string2": doSomething( valueForString2 ); break;
// ... 
case "stringN": doSomething( valueForStringN ); break;
}

それを次のように置き換えることができます:

var dict = {
  "string1": valueForString1,
  "string2": valueForString2,
  // ...
  "stringN": valueForStringN
};

doSomething( dict[ someValue ] );

もちろん、値は、文字列、数値、オブジェクト、関数など、何でもかまいません。また、ディクショナリに値が含まれていないかどうかを確認することもできます。

if (dict[someValue]) doSomething(dict[someValue]);
于 2012-12-28T18:53:50.450 に答える