我需要一个ruby代码来读取列a并找到列中最后一个填充单元格的结束位置。在上传的图像中,最后填充的数据是单元格“A21”中的i。我需要通过ruby代码知道这个单元格地址。
答案 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
- >在那里搜索常数:
答案 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"