Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script - Find Row number based on a cell value

I'm looking for some assistance on find the row number for a cell that contains a specific value.

The spreadsheet has lots of data across multiple rows & columns. I'm looping over the .getDataRange and can located the cell containing the value I'm looking for. Once found I'd like to know the row that his cell is in, so that I can further grab additional cell values since I know exactly how many rows down and/or columns over the additional information is from the found cell.

Here is a bit of the code for finding the cell containing a specific string.

function findCell() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var dataRange = sheet.getDataRange();
  var values = dataRange.getValues();

  for (var i = 0; i < values.length; i++) {
    var row = "";
    for (var j = 0; j < values[i].length; j++) {     
      if (values[i][j] == "User") {
        row = values[i][j+1];
        Logger.log(row);
      }
    }    
  }  
}

When outputting Logger.log(row) it gives me the values I'm looking for. I want to determine what row each value is in, so I can then go down X number of rows and over X columns to get the contents of other cells.

like image 204
jjones312 Avatar asked Jul 16 '14 16:07

jjones312


People also ask

How do I get row numbers in Google script?

The ROW function in Google Sheets returns the row number of a given cell. For example, if you want to know what row number the cell A1 is in, you would use the function =ROW(A1). This function is especially useful when you're working with formulas and need to refer to specific rows or columns.

How do I get a value from a cell in a Google script?

Get selected cell value in your Google Sheet Script First, let us add a menu item to your Google sheet so that it is easy for us to try the functions. Now we have to add getCurrentCellValue() function. This function will get the value of the currently selected cell and then show it in a message box.

How do I reference a row in Google Sheets?

A reference to a single cell is a combination of a letter and a number. For example, A1, C5, and E9 are all references to a single cell. The letter indicates the column and the number indicates the row. A range is referenced by using two cell references separated by a colon.

Is there a way to emulate Vlookup in Google script?

Yes, it is possible to emulate many of the built-in functions by using Class Spreadsheet (SpreadsheetApp) and JavaScript Array Object and its methods, but "the emulations" usually will be slower than built-in functions.


2 Answers

Since 2018 this can be done without a loop using flat().

function findRow(searchVal) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var columnCount = sheet.getDataRange().getLastColumn();

  var i = data.flat().indexOf(searchVal); 
  var columnIndex = i % columnCount
  var rowIndex = ((i - columnIndex) / columnCount);

  Logger.log({columnIndex, rowIndex }); // zero based row and column indexes of searchVal

  return i >= 0 ? rowIndex + 1 : "searchVal not found";
}
like image 143
Vrienden van Andries Avatar answered Oct 20 '22 15:10

Vrienden van Andries


I don't know much about google apps but it looks like [i] is your row number in this circumstance.

So if you did something like:

function findCell() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var dataRange = sheet.getDataRange();
  var values = dataRange.getValues();

  for (var i = 0; i < values.length; i++) {
    var row = "";
    for (var j = 0; j < values[i].length; j++) {     
      if (values[i][j] == "User") {
        row = values[i][j+1];
        Logger.log(row);
        Logger.log(i); // This is your row number
      }
    }    
  }  
}
like image 24
visch Avatar answered Oct 20 '22 16:10

visch