underscoreGS

Your Google Script tool kit with more than 40 methods. This library helps you write less code by adding tons of helper methods.

Installing

Using

Reference Docs

Installing the underscoreGS Library

1. In the script editor, click on Resources > Manage librairies...

2. In the Included Libraries window, insert the project key into the Find a Library textbox and click on the Select button.

Project Key: MiC3qjLYVUjCCUQpMqPPTWUF7jOZt2NQ8

3. Select the Stable version from the drop down list.

*Note that the Identifier is how you call the library.

Clicking the blue "Title" link will bring up the documentation window.

4. In your code type: UnderscoreGS.

You will see the available choices in the autocomplete box.

Using the Library

Lets say you have a list of numbers that you would like to randomize [1,2,3,4,5,6,7,8,9].

The _shuffle method will work perfectly for this task.

Simply pass the original array through the method.

The resulting array will be randomized: [4, 1, 8, 5, 2, 9, 7, 3, 6]

EXAMPLE

function myFunction() {

var sortedOut = [1,2,3,4,5,6,7,8,9];

var mixedUp = UnderscoreGS._shuffle(sortedOut);

Logger.log(mixedUp);

}

Reference Documentation

_each(Object obj, Iterator iterator, context)

Iterates over a list of elements, yielding each in turn to an iterator function.

Arguments:

_any(Object obj, Iterator iterator, context)

Determine if at least one element in the object matches a truth test. Delegates to ECMAScript 5's native some if available. Aliased as any.

Arguments:

_map(Object obj, Iterator iterator, context)

Produces a new array of values by mapping each value in list through a transformation function (iterator)

Arguments:

_find(Array obj, Iterator iterator, context)

Looks through each value in the list, returning the first one that passes a truth test (iterator). The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.

Arguments:

_filter(Array obj, Iterator iterator, context)

Looks through each value in the list, returning an array of all the values that pass a truth test (iterator)

Arguments:

_reject(Array obj, Iterator iterator, context)

Returns the values in list without the elements that the truth test (iterator) passes. The opposite of filter.

Arguments:

_all(Array obj, Iterator iterator, context)

Returns true if all of the values in the list pass the iterator truth test.

Arguments:

_contains(Array obj, Target target)

Returns true if the value is present in the list, using === to test equality. Uses indexOf internally, if list is an Array.

Arguments:

_pluck(Object obj, String key)

A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.

Arguments:

_sortBy(Array obj, Iterator iterator, context)

Returns a sorted copy of list, ranked in ascending order by the results of running each value through iterator.

Arguments:

_max( List, Iterator iterator, context)

Return the maximum element (or element-based computation).

Arguments:

_min(Object obj, Iterator iterator, context)

Return the minimum element (or element-based computation).

Arguments:

_shuffle(Array obj)

Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle

Arguments:

Name

obj

Type

Array

Description

_groupBy(Array obj, Iterator val)

Splits a collection into sets, grouped by the result of running each value through iterator. If iterator is a string instead of a function, groups by the property named by iterator on each of the values.

Arguments:

_sortedIndex( array, Object obj, Iterator iterator)

Uses a binary search to determine the index at which the value should be inserted into the list in order to maintain the list's sorted order. If an iterator is passed, it will be used to compute the sort ranking of each value.

Arguments:

_toArray(Iterable iterable)

Converts the list (anything that can be iterated over), into a real Array. Useful for transmuting the arguments object.

Arguments:

Name

iterable

Type

Iterable

Description

_size(Object obj)

Return the number of values in the list.

Arguments:

Name

obj

Type

Object

Description

_first(Array array, Integer n, guard)

Get the first element of an array. Passing n will return the first N values in the array.

Arguments:

_initial(Array array, Integer n, guard)

Returns everything but the last entry of the array. Especially useful on the arguments object. Pass n to exclude the last n elements from the result.

Arguments:

_last(Array array, Integer n, guard)

Get the Last element of an array. Passing n will return the Last N values in the array.

Arguments:

_tail( array, index, guard)

Arguments:

_indexOf(Array array, Item item, Boolean isSorted)

Returns the index at which value can be found in the array, or -1 if value is not present in the array. Uses the native indexOf function unless it's missing. If you're working with a large array, and you know that the array is already sorted, pass true for isSorted to use a faster binary search.

Arguments:

Return Values:

Type

Integer

Description

_lastIndexOf(Array array, Item item)

Returns the index of the last occurrence of value in the array, or -1 if value is not present. Uses the native lastIndexOf function if possible.

Arguments:

Return Values:

Type

Object

Description

_keys(Object obj)

Retrieve all the names of the object's properties.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

String[]

Description

_values(Object obj)

Return all of the values of the object's properties.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Object[]

Description

_methods(Object obj)

Return all of the values of the object's methods.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isEmpty(Object obj)

Returns true if object contains no values.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isElement(Object obj)

Returns true if object is a DOM element.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isArray(Object obj)

Returns true if object is an Array.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isObject(Object obj)

Returns true if object is an Object.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isFunction(Object obj)

Returns true if object is a Function.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isString(Object obj)

Returns true if object is a String.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_has( obj, key)

Arguments:

_isNumber(Object obj)

Returns true if object is a Number (including NaN).

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isBoolean(Object obj)

Returns true if object is a Boolean.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isDate(Object obj)

Returns true if object is a Date.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isRegExp(Object obj)

Returns true if object is a RegExp.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isNull(Object obj)

Returns true if object is null.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_isUndefined(Object obj)

Returns true if object is undefined.

Arguments:

Name

obj

Type

Object

Description

Return Values:

Type

Boolean

Description

_identity( value)

Returns the same value that is used as the argument. In math: f(x) = x This function looks useless, but is used throughout Underscore as a default iterator.

Arguments:

Name

value

Type

Description

_randomString(Integer length)

Returns a random string of upper and lower case letters and numbers

Arguments:

Name

length

Type

Integer

Description

Return Values:

Type

String

Description

_trim(String str)

Removes whitespace around a string

Arguments:

Name

str

Type

String

Description

Return Values:

Type

String

Description

_recurseJSON(Object json)

Returns all the keys in a given JSON object. Will recurse through all Arrays and Objects

Arguments:

Name

json

Type

Object

Description

A JSON Object

Return Values:

Type

String

Description

keys in object