What functions do Arrays in Google Apps Script support?

前端 未结 3 1405
温柔的废话
温柔的废话 2020-12-23 18:14

I keep on finding that Array functions are missing in GAS, eg calling find gives the error: Cannot find function find in object

The only d

相关标签:
3条回答
  • 2020-12-23 18:17

    When in Doubt, Test Yourself!

    Check it out for yourself with:

    function es_feature_test(f) {
      var a = [1, 2, 3, 4];
    
      try {
        a[f].call(a, function () { return true; });
        Logger.log("+ %s", f);
      } catch (e) {
        Logger.log("- %s", f);
      }
    }
    
    function es_test() {
      [
        "any",
        "every",
        "fill",
        "filter",
        "find",
        "findIndex",
        "forEach",
        "includes",
        "indexOf",
        "join",
        "keys",
        "lastIndexOf",
        "map",
        "pop",
        "push",
        "reduce",
        "reduceRight",
        "reverse",
        "shift",
        "slice",
        "some",
        "sort",
        "splice"
      ].forEach(function (fName) {
        es_feature_test(fName);
      });
    }
    

    Note: this function list is not exhaustive and is only for example purposes.

    Which outputs something like:

    [16-09-05 14:48:38:843 CEST] - any
    [16-09-05 14:48:38:843 CEST] + every
    [16-09-05 14:48:38:844 CEST] - fill
    [16-09-05 14:48:38:844 CEST] + filter
    [16-09-05 14:48:38:845 CEST] - find
    [16-09-05 14:48:38:846 CEST] - findIndex
    [16-09-05 14:48:38:846 CEST] + forEach
    [16-09-05 14:48:38:847 CEST] - includes
    [16-09-05 14:48:38:847 CEST] + indexOf
    [16-09-05 14:48:38:848 CEST] + join
    [16-09-05 14:48:38:848 CEST] - keys
    [16-09-05 14:48:38:849 CEST] + lastIndexOf
    [16-09-05 14:48:38:849 CEST] + map
    [16-09-05 14:48:38:850 CEST] + pop
    [16-09-05 14:48:38:850 CEST] + push
    [16-09-05 14:48:38:851 CEST] + reduce
    [16-09-05 14:48:38:851 CEST] + reduceRight
    [16-09-05 14:48:38:851 CEST] + reverse
    [16-09-05 14:48:38:852 CEST] + shift
    [16-09-05 14:48:38:852 CEST] + slice
    [16-09-05 14:48:38:853 CEST] + some
    [16-09-05 14:48:38:853 CEST] + sort
    [16-09-05 14:48:38:854 CEST] + splice
    

    Alternatively, robd's method works fine too, except it only tells you the list of methods that are visible. It doesn't tell you:

    • whether they actually work (they could block on access),
    • which methods are not visible.

    So I prefer my slightly more explicit approach.

    An even better method than mine would be to actually check function test cases to make sure behaviors are correct, but... oh well...

    GAS Function Call Support Fun Fact

    Here's something weird: I first tested using an implementation with .apply() instead of .call() (out of habit), and strangely enough only the methods supported in the GAS editor appeared as supported. Works fine using .call() though. Rather odd.

    I Want my Functional Tools Back!

    Check out underscoreGS.

    0 讨论(0)
  • 2020-12-23 18:23

    Besides robd's answer there is also the 2d arrays library.
    According to the picture (but not the documentation) that one has a find function

    0 讨论(0)
  • 2020-12-23 18:31

    Logger.log(Object.getOwnPropertyNames(Array.prototype)) gives the following, which I think it is the correct list:

    [constructor, toString, toLocaleString, toSource, join, reverse, sort, push, pop, shift, unshift, splice, concat, slice, indexOf, lastIndexOf, every, filter, forEach, map, some, reduce, reduceRight, length]

    0 讨论(0)
提交回复
热议问题