Class: Ld::Excel
- Inherits:
-
Object
- Object
- Ld::Excel
- Defined in:
- lib/ld/excel/excel.rb
Instance Attribute Summary collapse
-
#excel ⇒ Object
Returns the value of attribute excel.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.create(path, &block) ⇒ Object
作用 write的同名方法.
-
.open(path) ⇒ Object
作用 打开一个xls文件,返回Ld::Excel实例.
-
.write(path, &block) ⇒ Object
作用 写excel(创建新的xls文件).
Instance Method Summary collapse
-
#flush ⇒ Object
作用 如果xls文件内容有改变,可以刷新(会重新open一次,但这个方法不需要再传入参数了).
-
#initialize(path = nil) ⇒ Excel
constructor
A new instance of Excel.
- #new_sheet(name) ⇒ Object
- #open_sheet(name) ⇒ Object
-
#read(params, show_location = false) ⇒ Object
作用 读xls文件中的内容,二维数组 = 示例 Ld::Excel.read “Sheet1?A1:B2”.
-
#read_with_location(params) ⇒ Object
作用 与read方法相同(但会多返回坐标数据).
-
#save(path) ⇒ Object
作用 保存(真正执行io写入操作).
- #write_sheet(sheet_name, &block) ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ Excel
Returns a new instance of Excel.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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 |
Instance Attribute Details
#excel ⇒ Object
Returns the value of attribute excel.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def excel @excel end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def path @path end |
Class Method Details
.create(path, &block) ⇒ Object
作用 write的同名方法
40 41 42 |
# File 'lib/ld/excel/excel.rb', line 40 def self.create path, &block self.write path, &block end |
.open(path) ⇒ Object
作用 打开一个xls文件,返回Ld::Excel实例
25 26 27 |
# File 'lib/ld/excel/excel.rb', line 25 def self.open path self.new path end |
.write(path, &block) ⇒ Object
作用 写excel(创建新的xls文件)
30 31 32 33 34 35 36 37 |
# File 'lib/ld/excel/excel.rb', line 30 def self.write path, &block if path.class == Hash path = path[:file_path] end excel = Ld::Excel.new block.call excel excel.save path end |
Instance Method Details
#flush ⇒ Object
作用 如果xls文件内容有改变,可以刷新(会重新open一次,但这个方法不需要再传入参数了)
67 68 69 |
# File 'lib/ld/excel/excel.rb', line 67 def flush @excel = Ld::Excel.open @path end |
#new_sheet(name) ⇒ Object
83 84 85 |
# File 'lib/ld/excel/excel.rb', line 83 def new_sheet name Ld::Sheet.new @excel, name end |
#open_sheet(name) ⇒ Object
87 88 89 |
# File 'lib/ld/excel/excel.rb', line 87 def open_sheet name Ld::Sheet.open @excel, name end |
#read(params, show_location = false) ⇒ Object
作用 读xls文件中的内容,二维数组
示例 Ld::Excel.read “Sheet1?A1:B2”
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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) ⇒ Object
作用 与read方法相同(但会多返回坐标数据)
62 63 64 |
# File 'lib/ld/excel/excel.rb', line 62 def read_with_location params read params, true end |
#save(path) ⇒ Object
作用 保存(真正执行io写入操作)
72 73 74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# 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 |