how to get specific value cell index using rubyXL gem

467 Views Asked by At

I have a static header in my excel template and I want to find each column cell index using cell value.

Example:

Row1: cell1, cell2, cell3

I want to find the cell index which has a value [cell3].

1

There are 1 best solutions below

0
On

It's possible to iterate over the cells and find the cell that equal some content

So as idea

def find_cell_by_content(worksheet, content)
  worksheet.each do |row|
    row.cells.each do |cell|
      return [cell.row, cell.column] if cell.value == content
    end
  end

  nil
end

This method will return row and column indices if such cell exists otherwise nil