class Ld::Excel
Attributes
excel[RW]
path[RW]
Public Class Methods
create(path, &block)
click to toggle source
new(path = nil)
click to toggle source
# File lib/ld/excel/excel.rb, line 7 def initialize path = nil if path if path.match(/.xls$/) if File::exist? path @excel = Spreadsheet.open path @path = path else raise "File does not exist: #{path}" end else raise "Can only read .xls!" end else @excel = Spreadsheet::Workbook.new end end
open(path)
click to toggle source
Public Instance Methods
flush()
click to toggle source
作用 如果xls文件内容有改变,可以刷新(会重新open一次,但这个方法不需要再传入参数了)
# File lib/ld/excel/excel.rb, line 67 def flush @excel = Ld::Excel.open @path end
new_sheet(name)
click to toggle source
# File lib/ld/excel/excel.rb, line 83 def new_sheet name Ld::Sheet.new @excel, name end
open_sheet(name)
click to toggle source
# File lib/ld/excel/excel.rb, line 87 def open_sheet name Ld::Sheet.open @excel, name end
read(params, show_location = false)
click to toggle source
作用 读xls文件中的内容,二维数组¶ ↑
示例 Ld::Excel.read
“Sheet1?A1:B2”¶ ↑
# File lib/ld/excel/excel.rb, line 46 def read params, show_location = false case params.class.to_s when 'String' shett_name, scope = params.split('?') @current_sheet = open_sheet shett_name @current_sheet.read scope, show_location when 'Hash' raise "Parameter error! \nnot find 'sheet'" if params[:sheet].nil? raise "Parameter error! \nnot find 'scope'" if params[:scope].nil? params[:location] = false if params[:location].nil? @current_sheet = open_sheet params[:sheet] @current_sheet.read params, params[:location] end end
read_with_location(params)
click to toggle source
save(path)
click to toggle source
作用 保存(真正执行io写入操作)
# File lib/ld/excel/excel.rb, line 72 def save path puts "Covers a file: #{path}" if File.exist? path @excel.write path puts "Excel save success!" self rescue puts $! puts $@ false end
write_sheet(sheet_name, &block)
click to toggle source
# File lib/ld/excel/excel.rb, line 91 def write_sheet sheet_name, &block sheet = new_sheet sheet_name block.call sheet sheet.save true rescue puts $! puts $@ false end