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

时间:2014-03-05 07:04:13

标签: ruby excel win32ole

我需要一个ruby代码来读取列a并找到列中最后一个填充单元格的结束位置。在上传的图像中,最后填充的数据是单元格“A21”中的i。我需要通过ruby代码知道这个单元格地址。 enter image description here

2 个答案:

答案 0 :(得分:4)

我会使用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文档。

您可以从电脑中安装的xlCellTypeLastCell版本中获取MSExcel的值。只做ALT+F11 - > F2 - >在那里搜索常数:

constant value

答案 1 :(得分:0)

尝试使用'电子表格'宝石

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"
相关问题