Ruby脚本读取列中最后一个填充的单元格

我需要一个ruby代码来读取列a,并find列最后填充的单元格在哪里结束。在上传的图像中,最后填充的数据是我在单元格“A21”。 我需要通过ruby代码来知道这个单元格地址。 在这里输入图像说明

我会使用Ruby stdlib WIN32OLE

 require 'win32ole' # create an instance of the Excel application object excel = WIN32OLE.new('Excel.Application') # make Excel visible excel.visible = true # open the excel from the desired path wb=excel.workbooks.open("C:\\Users\\test.xlsx") # get the first Worksheet wbs= wb.Worksheets(1) # value of the constants I picked up from # http://techsupt.winbatch.com/ts/T000001033005F9.html rng = wbs.range("1:1").SpecialCells(11) # value of 'xlCellTypeLastCell' is 11 rng.value # => "i" rng.address # => "$A$21" # to get the row and column number row,col = rng.row,rng.column [row,col] # => [21,1] 

看看SpecialCells的MSDN文档。

您可以从安装在您电脑中的MSExcel版本中MSExcel xlCellTypeLastCell的值。 只要做ALT+F11 – > F2 – >在那里search常量:

不变的价值

尝试使用“电子表格”gem

 book = Spreadsheet.open 'myexcel.xls'; sheet1 = book.worksheet 0 last_value = nil sheet1.each do |row| last_value = row[0].present? && row[0] end last_value => "i"