I'm trying to search a google sheet for a specific key word with this code:
var completeSearch = dataSheetName.createTextFinder(searchTerm).findAll();
When I log it, the variable returns [Range, Range, Range, Range].
Is there a way to have it return the cell values for each occurrence of this key word?
Edit: I didn't mean I needed it to return the cell VALUES, as in the contents of the cell, but rather the cell name itself, like (A4).
var completeSearch = dataSheetName.createTextFinder(searchTerm).findAll();
dataSheetName
is a sheet in a Spreadsheet.
dataSheetName
.If my understanding is correct, how about this modification?
findAll()
is [Range, Range,,]
. In order to retrieve values from Range
, in this case, it uses getValue()
and getValues
.Range
are the same with the value of searchTerm
. So in this sample script, both the value of the searched range and the values of the row of searched range are retrieved.Please think of this as just one of several answers.
Before you use this script, please set the variables of searchTerm
and sheetName
.
function myFunction() {
var searchTerm = "sample"; // Please set this.
var sheetName = "Sheet1"; // Please set this.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheetName = ss.getSheetByName(sheetName);
var completeSearch = dataSheetName.createTextFinder(searchTerm).findAll();
for (var i = 0; i < completeSearch.length; i++) {
var range = completeSearch[i];
var value = range.getValue();
var rowValue = dataSheetName.getRange(range.getRow(), 1, 1, dataSheetName.getLastColumn()).getValues();
Logger.log(value) // Value of the searched range
Logger.log(rowValue) // Values of the row of searched range
}
}
If I misunderstood your question and this was not the result you want, I apologize.
From your replying, I could understand like above. If my understanding is correct, how about this sample script?
function myFunction() {
var searchTerm = "sample"; // Please set this.
var sheetName = "Sheet1"; // Please set this.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheetName = ss.getSheetByName(sheetName);
var completeSearch = dataSheetName.createTextFinder(searchTerm).findAll();
for (var i = 0; i < completeSearch.length; i++) {
var range = completeSearch[i];
Logger.log(range.getA1Notation())
}
}