Reputation: 3143
In GAS,
Code.gs
function myFunction() {
var sheet2 = SpreadsheetApp.getActivesheet();
var firstset = sheet2.getRange('B1:B10').getValues();
Logger.log(firstset)
var found = firstset.indexOf('[re]')
Logger.log(found)
}
In Google Spreadsheet Range B1:B10,
B1 we
B2 we
B3 re
B4 ef
B5 ge
B6 er
B7 fe
B8 th
B9 rg
B10 pq
The answer to the code should result 2, however, Log is showing -1 (not matched). Any help
Upvotes: 2
Views: 17645
Reputation: 1
Here is what works for me.
This is a revised method, the original was not giving me the proper results.
Array.prototype.findIndex = function(search)
{
if(search == "") return -1;
for(var t = 0; t < this.length; t++)
{
if(this[t].search(search) >=0)
{break}
}
return t-1;
}
USAGE
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var index = fruits.findIndex("Apple");
//"Apple" is located at position 2 in the array.
Logger.log("Index of 'Apple' is: " + index);
NOTE
When using this method to get the index of a sheets column by name, increment the index by 1 to use sheet.getRange(rowIndex, columnIndex).
var range = sheet.getRange(row, (index+1));
Upvotes: 0
Reputation: 344
If you would put your data in a row, not a column, the code could by used like this:
function myFunction() {
var sheet2 = SpreadsheetApp.getActivesheet();
var firstset = sheet2.getRange('A1:J1').getValues();
Logger.log(firstset)
var found = firstset[0].indexOf('[re]')
Logger.log(found)
}
indexOf
- can search only in a one dimensional array - therefore is used in my example firstset[0].indexOf('[re]')
.getValues()
- always returns 2 dimensional array, even there is only one row or column.
Upvotes: 0
Reputation: 46812
in a simple for loop :
function myFunction() {
var sheet2 = SpreadsheetApp.getActiveSheet();
var firstset = sheet2.getRange('B1:B10').getValues();
for(var n in firstset){
if(firstset[n][0]=='re'){break}
}
Logger.log(n);
}
Upvotes: 4