How to check if the value is exist in google spreadsheet or not using apps script
I want to check if the Sam
exist in the entire spreadsheet or not using apps s
Depending on if you want to select the range or just always use the whole A:A column. In the former case, do this:
// this gets the range
var range = SpreadsheetApp.getActiveRange().getValues();
// this is what you are searching for
var searchString = "Sam";
// this is whether what you are searching for exists
var isSearchStringInRange = range.some( function(row){
return row[0] === searchString
});
// then you can proceed to do something like
if( isSearchStringInRange ){
// do something
}
You can define a textFinder
and run it over your data range.
function findSam() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var range = sheet.getDataRange();
var textFinder = range.createTextFinder('Sam');
var locations = [];
var occurrences = textFinder.findAll().map(x => x.getA1Notation());
if (occurrences == []) {
// do something if "Sam" not in sheet
}
else {
// do stuff with each range:
}
}
This code will:
Range
object that contains "Sam" to an array of rangesFrom here you can do what you wish with the ranges. If "Sam" is not in the sheet then occurrences
will be an empty array and you can do here what you wish.
I hope this is helpful to you!