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文件 = 参数 xls文件的全路径或相对路径的字符串 = 示例 Ld::Excel.open ‘project.xls’ = 返回 Ld::Excel实例.
-
.write(path, &block) ⇒ Object
作用 写xls文件(会创建一个新的xls文件) = 返回 如果创建成功,返回Ld::Excel实例.如果创建失败会返回false(不会导致程序终止) = 参数1 path 字符串,指定要写的文件路径(最好使用全路径).
Instance Method Summary collapse
-
#flush ⇒ Object
作用 如果xls文件内容有改变,可以刷新(会重新open一次,但这个方法不需要再传入参数了) = 参数 无需参数 = 返回 Ld::Excel实例.
-
#initialize(path = nil) ⇒ Excel
constructor
参数 path(可以为空).
- #new_sheet(name) ⇒ Object
- #open_sheet(name) ⇒ Object
-
#read(params, show_location = false) ⇒ Object
作用 读xls文件中的内容 = 示例 Ld::Excel.read “Sheet1?A1:B2” = 示例 Ld::Excel.read “Sheet1?A1:B2+C,D” = 示例 Ld::Excel.read “Sheet1?A1:B2+C,D,1,2” = 返回 二维数组.
-
#read_with_location(params) ⇒ Object
作用 与read方法相同(但会多返回坐标数据) = 返回 与read方法相同(但会多返回坐标数据) = 示例 与read方法相同(但会多返回坐标数据).
-
#save(path) ⇒ Object
作用 保存数据到一个路径 = 返回 如果创建成功,返回Ld::Excel实例.如果创建失败会返回false(不会导致程序终止) = 参数1 path 字符串,指定要写的文件路径(最好使用全路径).
- #write_sheet(sheet_name, &block) ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ Excel
参数 path(可以为空)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ld/excel/excel.rb', line 8 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的同名方法,作用和使用方法完全一样
46 47 48 |
# File 'lib/ld/excel/excel.rb', line 46 def self.create path, &block self.write path, &block end |
.open(path) ⇒ Object
作用 打开一个xls文件
参数 xls文件的全路径或相对路径的字符串
示例 Ld::Excel.open ‘project.xls’
返回 Ld::Excel实例
29 30 31 |
# File 'lib/ld/excel/excel.rb', line 29 def self.open path self.new path end |
.write(path, &block) ⇒ Object
作用 写xls文件(会创建一个新的xls文件)
返回 如果创建成功,返回Ld::Excel实例.如果创建失败会返回false(不会导致程序终止)
参数1 path 字符串,指定要写的文件路径(最好使用全路径)
36 37 38 39 40 41 42 43 |
# File 'lib/ld/excel/excel.rb', line 36 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一次,但这个方法不需要再传入参数了)
参数 无需参数
返回 Ld::Excel实例
80 81 82 |
# File 'lib/ld/excel/excel.rb', line 80 def flush @excel = Ld::Excel.open @path end |
#new_sheet(name) ⇒ Object
98 99 100 |
# File 'lib/ld/excel/excel.rb', line 98 def new_sheet name Ld::Sheet.new @excel, name end |
#open_sheet(name) ⇒ Object
102 103 104 |
# File 'lib/ld/excel/excel.rb', line 102 def open_sheet name Ld::Sheet.open @excel, name end |
#read(params, show_location = false) ⇒ Object
作用 读xls文件中的内容
示例 Ld::Excel.read “Sheet1?A1:B2”
示例 Ld::Excel.read “Sheet1?A1:B2+C,D”
示例 Ld::Excel.read “Sheet1?A1:B2+C,D,1,2”
返回 二维数组
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ld/excel/excel.rb', line 55 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方法相同(但会多返回坐标数据)
返回 与read方法相同(但会多返回坐标数据)
示例 与read方法相同(但会多返回坐标数据)
73 74 75 |
# File 'lib/ld/excel/excel.rb', line 73 def read_with_location params read params, true end |
#save(path) ⇒ Object
作用 保存数据到一个路径
返回 如果创建成功,返回Ld::Excel实例.如果创建失败会返回false(不会导致程序终止)
参数1 path 字符串,指定要写的文件路径(最好使用全路径)
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ld/excel/excel.rb', line 87 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
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ld/excel/excel.rb', line 106 def write_sheet sheet_name, &block sheet = new_sheet sheet_name block.call sheet sheet.save true rescue puts $! puts $@ false end |