Site owners

  • Romain Vialard
  • James Ferreira

Get the Books!

Page authors

  • James Ferreira
    May 21, 2012

Recent site activity

USEFUL LIBRARIES‎ > ‎

underscoreGS

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


Report problems in the Issue Tracker

Please Contact us if you need help writing scripts.





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. 






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);
}





Contents

  1. 1 _each(Object obj, Iterator iterator, context)
    1. 1.1 Arguments:
    2. 1.2 _any(Object obj, Iterator iterator, context)
    3. 1.3 _map(Object obj, Iterator iterator, context)
    4. 1.4 _find(Array obj, Iterator iterator, context)
    5. 1.5 _filter(Array obj, Iterator iterator, context)
    6. 1.6 _reject(Array obj, Iterator iterator, context)
    7. 1.7 _all(Array obj, Iterator iterator, context)
    8. 1.8 _contains(Array obj, Target target)
    9. 1.9 _pluck(Object obj, String key)
    10. 1.10 _sortBy(Array obj, Iterator iterator, context)
    11. 1.11 _max( List, Iterator iterator, context)
    12. 1.12 _min(Object obj, Iterator iterator, context)
    13. 1.13 _shuffle(Array obj)
    14. 1.14 _groupBy(Array obj, Iterator val)
    15. 1.15 _sortedIndex( array, Object obj, Iterator iterator)
    16. 1.16 _toArray(Iterable iterable)
    17. 1.17 _size(Object obj)
    18. 1.18 _first(Array array, Integer n, guard)
    19. 1.19 _initial(Array array, Integer n, guard)
    20. 1.20 _last(Array array, Integer n, guard)
    21. 1.21 _tail( array, index, guard)
    22. 1.22 _indexOf(Array array, Item item, Boolean isSorted)
    23. 1.23 _lastIndexOf(Array array, Item item)
    24. 1.24 _keys(Object obj)
    25. 1.25 _values(Object obj)
    26. 1.26 _methods(Object obj)
    27. 1.27 _isEmpty(Object obj)
    28. 1.28 _isElement(Object obj)
    29. 1.29 _isArray(Object obj)
    30. 1.30 _isObject(Object obj)
    31. 1.31 _isFunction(Object obj)
    32. 1.32 _isString(Object obj)
    33. 1.33 _has( obj, key)
    34. 1.34 _isNumber(Object obj)
    35. 1.35 _isBoolean(Object obj)
    36. 1.36 _isDate(Object obj)
    37. 1.37 _isRegExp(Object obj)
    38. 1.38 _isNull(Object obj)
    39. 1.39 _isUndefined(Object obj)
    40. 1.40 _identity( value)
    41. 1.41 _randomString(Integer length)
    42. 1.42 _trim(String str)
    43. 1.43 _recurseJSON(Object json)


_each(Object obj, Iterator iterator, context)

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

Arguments:

Name Type Description
obj Object
iterator Iterator
context

_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:

Name Type Description
obj Object
iterator Iterator
context

_map(Object obj, Iterator iterator, context)

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

Arguments:

Name Type Description
obj Object
iterator Iterator
context

_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:

Name Type Description
obj Array
iterator Iterator
context

_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:

Name Type Description
obj Array
iterator Iterator
context

_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:

Name Type Description
obj Array
iterator Iterator
context

_all(Array obj, Iterator iterator, context)

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

Arguments:

Name Type Description
obj Array
iterator Iterator
context

_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:

Name Type Description
obj Array
target Target

_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:

Name Type Description
obj Object
key String

_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:

Name Type Description
obj Array
iterator Iterator
context

_max( List, Iterator iterator, context)

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

Arguments:

Name Type Description
List
iterator Iterator
context

_min(Object obj, Iterator iterator, context)

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

Arguments:

Name Type Description
obj Object
iterator Iterator
context

_shuffle(Array obj)

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

Arguments:

Name Type Description
obj Array

_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:

Name Type Description
obj Array
val Iterator

_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:

Name Type Description
array
obj Object
iterator Iterator

_toArray(Iterable iterable)

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

Arguments:

Name Type Description
iterable Iterable

_size(Object obj)

Return the number of values in the list.

Arguments:

Name Type Description
obj Object

_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:

Name Type Description
array Array
n Integer
guard

_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:

Name Type Description
array Array
n Integer
guard

_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:

Name Type Description
array Array
n Integer
guard

_tail( array, index, guard)

Arguments:

Name Type Description
array
index
guard

_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:

Name Type Description
array Array
item Item
isSorted Boolean

Return Values:

Type Description
Integer

_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:

Name Type Description
array Array
item Item

Return Values:

Type Description
Object

_keys(Object obj)

Retrieve all the names of the object's properties.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
String[]

_values(Object obj)

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

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Object[]

_methods(Object obj)

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

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isEmpty(Object obj)

Returns true if object contains no values.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isElement(Object obj)

Returns true if object is a DOM element.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isArray(Object obj)

Returns true if object is an Array.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isObject(Object obj)

Returns true if object is an Object.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isFunction(Object obj)

Returns true if object is a Function.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isString(Object obj)

Returns true if object is a String.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_has( obj, key)

Arguments:

Name Type Description
obj
key

_isNumber(Object obj)

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

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isBoolean(Object obj)

Returns true if object is a Boolean.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isDate(Object obj)

Returns true if object is a Date.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isRegExp(Object obj)

Returns true if object is a RegExp.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isNull(Object obj)

Returns true if object is null.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_isUndefined(Object obj)

Returns true if object is undefined.

Arguments:

Name Type Description
obj Object

Return Values:

Type Description
Boolean

_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 Type Description
value

_randomString(Integer length)

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

Arguments:

Name Type Description
length Integer

Return Values:

Type Description
String

_trim(String str)

Removes whitespace around a string

Arguments:

Name Type Description
str String

Return Values:

Type Description
String

_recurseJSON(Object json)

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

Arguments:

Name Type Description
json Object A JSON Object

Return Values:

Type Description
String keys in object