'Sets and data structures in Google Apps Script

Google Sheets's script editor seems to be a variant of JavaScript, but I can't figure out how to use it to handle Sets, dictionaries, etc. I already know how to use JS Arrays, but Arrays are not sufficient for my task.

Declaring a Set through the normal way (i.e. var categoryOptions = new Set(String); gives me a 'Set is not defined' error.

Using var categoryOptions = {}; does not allow me to use any built-in Set functions, such as add. Instead, I get: "TypeError: Cannot find function add in object [object Object]."

Any pro tips?



Solution 1:[1]

Some news from the AppScript runtime overview https://developers.google.com/apps-script/guides/v8-runtime

Historically, Apps Script has been powered by Mozilla's Rhino JavaScript interpreter. While Rhino provided a convenient way for Apps Script to execute developer scripts, it also tied Apps Script to a specific JavaScript version (ES5). Apps Script developers can't use more modern JavaScript syntax and features in scripts using the Rhino runtime.

To address this concern, Apps Script is now supported by the V8 runtime that powers Chrome and Node.js. You can migrate existing scripts to V8 in order to take advantage of the modern JavaScript syntax and features.

Solution 2:[2]

It looks like Apps Script is running on Rhino 1.7r3. Here's a write up on what is supported and what is not.

http://ramblings.mcpher.com/Home/excelquirks/gassnips/rhinoversion

Solution 3:[3]

While Appscript itself is still way behind the ES times, using the Google maintained Clasp tool does let you now develop app script using Typescript in an officially supported way which will let you use Set() and all the other language features.

https://github.com/google/clasp/blob/master/docs/typescript.md

Note the big constraint here, once you shift to using Typescript you can no longer develop/debug your code in the app script script editor as you will be looking at the transpiled code.

Similarly there are also non-google projects to transpile to appscript code from ESx using webpack and babel.

eg https://github.com/labnol/apps-script-starter

Solution 4:[4]

try using this code:

function test() {
let search_from_data = ['a', 'b', 'c', 1, 2];
let search_data = ['c', 'a', 'd', 'z', 2, 3, 4, 5];
let search_set = new Set();
for ( let i = 0 ; i < search_from_data.length ; i++ )
    search_set.add(search_from_data[i]);
for ( let i = 0 ; i < search_data.length ; i++)
    Logger.log([search_data[i], "Is present?", search_set.has(search_data[i])]);
}

This Image is the output for the above code

Apps Script is now supported by the V8 runtime that powers Chrome and Node.js. You can migrate existing scripts to V8 in order to take advantage of the modern JavaScript syntax and features. Reference: https://developers.google.com/apps-script/guides/v8-runtime

Solution 5:[5]

Here's an ES6 shim for Apps Script, so you can use things like Set and Map. The IDE of course won't support arrow functions or anything else that might break the 5.1 syntax.

http://ramblings.mcpher.com/Home/excelquirks/gassnips/es6shim

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sonavolob
Solution 2 bruce
Solution 3 benz001
Solution 4
Solution 5 bruce