我不断发现数组函数在GAS中缺失,例如调用find
给出错误:Cannot find function find in object
我能找到的关于这个的唯一文档有些模棱两可:https://developers.google.com/apps-script/guides/services/basic_javascript_features
Apps Script基于JavaScript 1.6,加上1.7和1.8的一些特性。因此,除了内置的和高级的Google服务之外,还可以使用许多基本的JavaScript特性:您可以使用Array、Date、RegExp等常见对象,以及Math和Object全局对象。然而,由于Apps Script代码运行在Google的服务器上(不是客户端,除了html服务页面),基于浏览器的功能,如DOM操作或windows API是不可用的。
如何查看Array上可用的确切方法?
Logger.log(Object.getOwnPropertyNames(Array.prototype))
给出了以下,我认为这是正确的列表:
[constructor, toString, toLocaleString, toSource, join, reverse, sort, push, pop, shift, unshift, splice, concat, slice, indexOf, lastIndexOf, every, filter, forEach, map, some, reduce, reduceRight, length]
当你有疑问时,测试你自己!
自己查看:
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);
});
}
注意:此函数列表并不详尽,仅供示例使用。
输出如下:
[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
或者,robd的方法也工作得很好,除了它只告诉你可见的方法列表。它没有告诉你:
- 它们是否实际工作(它们可以阻止访问),
- 哪些方法不可见
所以我更喜欢我稍微更明确的方法。
一个比我的更好的方法是检查功能测试用例,以确保行为是正确的,但是……哦…
GAS函数调用支持趣事
这里有一些奇怪的事情:我首先使用.apply()
而不是.call()
的实现进行测试(出于习惯),奇怪的是,只有GAS编辑器中支持的方法显示为支持。使用.call()
可以很好地工作。相当奇怪的。
我要回我的功能工具!
查看下划线
除了robd的答案,还有2d数组库。
根据图片(而不是文档),一个人有一个查找函数