I'm finding it really hard to word my question correctly into google.
I'm in the process of writing a Google Apps script that will search through a column of data to see if there's a match.
When I put the values into an array, the return is
[ 'data' ]
rather than an actual string. This means that I'll never find a match.
I thought it was because I was searching through a list of numbers stored as text (all Australian mobile phone numbers have a leading "0"), so I tried different columns with the same result.
I don't know what I'm doing wrong
Code excerpt:
//Global variables - spreadsheet name, sheet names
const This_File = SpreadsheetApp.getActiveSpreadsheet();
const Code_Sheet = This_File.getSheetByName("Button");
const Data_Sheet = This_File.getSheetByName("Current_Members");
function search_ph_num() {
//find last row of data
var last_row = Data_Sheet.getLastRow()
var rng_map = "C2:C"+last_row
//var rng_map = "A2:A"+last_row;
console.log(rng_map)
//testing
//SpreadsheetApp.getUi().alert(rng_map);
//testing, input dummy data - will be read from email list
var dummy_array = ["0412345678", "033999779", "0455667"];
// first number should be found value
//set search range to phone number column
var this_range = Data_Sheet.getRange(rng_map);
1.
1. var Skt_Number = this_range.getValues();
1.
1. //search phone numbers for existing data. Assume phone number is unique... pretty valid assumption.
1.
1. for (var i = 0; i <= last_row; i = i + 1){
1.
1. var isMatched = dummy_array.indexOf(Skt_Number[i]);
if (isMatched !== -1) {
//SNIP actual code goes here
SpreadsheetApp.getUi().alert(Skt_Number[i]);
SpreadsheetApp.getUi().alert(i + 2);
}
//debug
if (Skt_Number[i] == "0412345678"){
var insert_dummy = Data_Sheet.getRange("I3");
insert_dummy.setValue(Skt_Number[i]);
console.log(Skt_Number[i])
var insert_dummy = Data_Sheet.getRange("J3");
insert_dummy.setValue(dummy_array[0]);
console.log(dummy_array[0])
}
}
}
Log output:
2:08:04 PM Notice Execution started
2:08:05 PM Info C2:C11
2:08:05 PM Info [ '0412345678' ]
2:08:05 PM Info 0412345678
2:08:05 PM Notice Execution completed
So, [ '0412345678' ] will never match 0412345678.
What am I doing wrong?
Input cell data into an array. Tried to search array using indexOf method. Expected to find match. No match returned.
From your showing script, I guessed that the reason for your current issue is due to Skt_Number
of var Skt_Number = this_range.getValues();
is a 2-dimensional array.
About So, [ '0412345678' ] will never match 0412345678.
, I think that [ '0412345678' ]
is a 1-dimensional array. And, 0412345678
is a string value. And, Array.prototype.indexOf()
can be used for a 1-dimensional array. If you want to compare them, how about modifying as follows.
var isMatched = dummy_array.indexOf(Skt_Number[i]);
var isMatched = dummy_array.indexOf(Skt_Number[i][0]);
var rng_map = "C2:C"+last_row
. So, Skt_Number
is like [["value1"],["value2"],,,]
. So, I modified Skt_Number[i]
to Skt_Number[i][0]
.