Home > OS >  How to list desired excel row and column with Ruby
How to list desired excel row and column with Ruby

Time:07-01

Friends I'm not able to get only the first Invisible value from the Status column, or only the 'No. Hydrometer' value from the Elemento column, or all the values. Friends my spreadsheet is configured like this.

| Elemento | Status | | Nº Hidrômetro |Invisível | | Leitura |Invisível | | Diâmetro do HD |Invisível | | Filtro |Invisível |

I need to get the following values:
-- Only the Status column with the value in the first row equal to 'Invisible'
-- All values ​​in the Status column only
-- All values ​​in Element column only
-- All values ​​in rows and columns

require 'spreadsheet'
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet.open('C:/temp/pasta1.xls', "r")
sheet = book.worksheet 0
pp sheet.row(0)

CodePudding user response:

element status
1 Visible
2 Invisible
>> book = Spreadsheet.open("excel.xls", "r")
>> sheet = book.worksheet(0)
>> element_column = sheet.first.find_index("element")
=> 0
>> status_column = sheet.first.find_index("status")
=> 1

Only the Status column with the value in the first row equal to 'Invisible'

>> sheet.detect { |row| row[status_column] =~ /invisible/i }&.fetch(status_column)
=> "Invisible"

All values ​​in the Status column only

>> sheet.column(status_column).to_a[1..]
=> ["Visible", "Invisible"]

All values ​​in Element column only

>> sheet.column(element_column).to_a[1..]
=> [1, 2]

All values ​​in rows and columns

>> sheet.rows[1..]
=> [[1, "Visible"], [2, "Invisible"]]

https://github.com/zdavatz/spreadsheet/blob/master/GUIDE.md#reading-is-easy

  •  Tags:  
  • ruby
  • Related