问题
I'm trying to use findIndex() on a object in google script but it does not work. Here is a exemple:
function myFunction() {
var searchingJohn = "John";
var DATA = {namesList: []};
DATA.namesList.push({name: "John", otherDataForThisName: []});
var result = DATA.namesList.findIndex(function (element)
{return element.name == this;}, searchingJohn);
return result;
}
this work well in a javascript consol but google script return me a "TypeError: Fonction findIndex not found in object....."
回答1:
You can't.
You are trying to use findIndex()
on a object, but findIndex()
is for arrays.
From the MDN documentation on findIndex()
:
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.
The reason is that objects don't hold key-value pairs in any particular order. For example,
{key1:value1, key2:value2}
and
{key2:value2, key1:value1}
are exactly the same object. So there is no index to be found.
回答2:
This is the example of using findIndex()
function for searching string in object in google script. Example:
function myFunction() {
var DATA = {namesList: []};
DATA.namesList.push({name: "John", otherDataForThisName: []});
DATA.namesList.push({name: "Mark", otherDataForThisName: []});
DATA.namesList.push({name: "Twin", otherDataForThisName: []});
const searching = "Mark";
var result = DATA.namesList.findIndex(function (element){
return element.name.toLowerCase() == searching.toLowerCase();
});
return result;
}
findIndex()
function is also use for 2 dimensional array.
I got solution from this link: http://one7techlab.com/post/findindex-function-in-google-script-4
回答3:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cellRange = ss.getRange("B5:Q10").getValues();
var someString = "John";
if (cellRange == "John")
{
//Do this code.
}
This is the only way I can think to pull out the information you're trying to get. IndexOf(); pulls from an index ergo if "John" was in an array it would look like this ["J", "o", "h", "n"] which is why it can't be found that way.
The code above will find it in a range of cells and you can do the whole sheet but the more "Empty" you add to it the slower it will run. Another nested if loop could clean that up for you if you need a massive sheet checked.
回答4:
I have the same issue with many basic javascript functionalities working with gscript, I added the polyfill at the top of my script and it worked with many functionalities.
Paste this code on the top of your script, it will support indexOf.
// This version tries to optimize by only checking for "in" when looking for undefined and
// skipping the definitely fruitless NaN search. Other parts are merely cosmetic conciseness.
// Whether it is actually faster remains to be seen.
if (!Array.prototype.indexOf)
Array.prototype.indexOf = (function(Object, max, min) {
"use strict"
return function indexOf(member, fromIndex) {
if (this === null || this === undefined)
throw TypeError("Array.prototype.indexOf called on null or undefined")
var that = Object(this), Len = that.length >>> 0, i = min(fromIndex | 0, Len)
if (i < 0) i = max(0, Len + i)
else if (i >= Len) return -1
if (member === void 0) { // undefined
for (; i !== Len; ++i) if (that[i] === void 0 && i in that) return i
} else if (member !== member) { // NaN
return -1 // Since NaN !== NaN, it will never be found. Fast-path it.
} else // all else
for (; i !== Len; ++i) if (that[i] === member) return i
return -1 // if the value was not found, then return -1
}
})(Object, Math.max, Math.min)
reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill
来源:https://stackoverflow.com/questions/47586105/indexof-in-google-script