Class: Ld::Sheet
- Inherits:
-
Object
- Object
- Ld::Sheet
- Defined in:
- lib/ld/excel/sheet.rb
Constant Summary collapse
- ABSCISSA =
{}
Instance Attribute Summary collapse
-
#excel ⇒ Object
Returns the value of attribute excel.
-
#sheet ⇒ Object
Returns the value of attribute sheet.
Class Method Summary collapse
-
.create(excel, name) ⇒ Object
作用 创建一个sheet.
-
.open(excel, name) ⇒ Object
作用 打开一个sheet.
Instance Method Summary collapse
-
#delete_maps(rows, cols, deletes) ⇒ Object
作用 少读一些行或列.
-
#initialize(excel, name, type = 'new') ⇒ Sheet
constructor
A new instance of Sheet.
-
#insert_maps(rows, cols, inserts) ⇒ Object
作用 多读一些行或列.
-
#insert_row(row) ⇒ Object
作用 在当前sheet的主体内容末尾添加一行数据(传入一维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io).
-
#is_row?(row) ⇒ Boolean
作用 判断要添加或要移除的是一行还是一列.
- #parse_point(point) ⇒ Object
-
#parse_string_scope(scope) ⇒ Object
作用 解析范围参数.
-
#read(scope, show_location = false) ⇒ Object
作用 读sheet页数据,返回二维数组.
-
#read_arrs(map_arrs, show_location) ⇒ Object
作用 读二维数据(使用maps).
-
#read_scope_to_map(scope) ⇒ Object
作用 使用范围参数构建maps(预读).
-
#read_unit_by_xy(x, y, parse) ⇒ Object
作用 通过x,y坐标获取一个单元格的内容.
-
#save ⇒ Object
作用 将数据写入sheet.
-
#set_color(color) ⇒ Object
作用 设置当前sheet页的字体颜色.
-
#set_font(font) ⇒ Object
作用 设置当前sheet页的字体.
-
#set_font_size(size) ⇒ Object
作用 设置当前sheet页的字体大小.
- #set_format(hash) ⇒ Object
-
#set_headings(headings) ⇒ Object
作用 在当前sheet的主体内容顶上方添加一个表头(传入二维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io).
-
#set_point(point) ⇒ Object
作用 设置当前sheet页的字体颜色.
-
#set_rows(rows) ⇒ Object
作用 在当前sheet中添加主体数据(传入二维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io).
-
#set_weight(weight) ⇒ Object
作用 设置当前sheet页的单元格宽度(暂时无效).
-
#write_unit_by_xy(x, y, unit) ⇒ Object
作用 通过x,y坐标往一个单元格中写入数据,但不写入(只有调用Ld::Excel的实例方法save才会写入io).
Constructor Details
#initialize(excel, name, type = 'new') ⇒ Sheet
Returns a new instance of Sheet.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ld/excel/sheet.rb', line 11 def initialize excel, name, type = 'new' raise "sheet name is nil" if !name @excel = excel @name = name case type when 'new' @sheet = excel.create_worksheet :name => name @point = 'a1' @headings = nil @rows = [] when 'open' @sheet = excel.worksheet name raise "sheet '#{name}' not found!" if !@sheet end @format = @sheet.default_format end |
Instance Attribute Details
#excel ⇒ Object
Returns the value of attribute excel.
2 3 4 |
# File 'lib/ld/excel/sheet.rb', line 2 def excel @excel end |
#sheet ⇒ Object
Returns the value of attribute sheet.
2 3 4 |
# File 'lib/ld/excel/sheet.rb', line 2 def sheet @sheet end |
Class Method Details
.create(excel, name) ⇒ Object
作用 创建一个sheet
172 173 174 |
# File 'lib/ld/excel/sheet.rb', line 172 def self.create excel, name self.new excel, name, 'new' end |
.open(excel, name) ⇒ Object
作用 打开一个sheet
167 168 169 |
# File 'lib/ld/excel/sheet.rb', line 167 def self.open excel, name self.new excel, name, 'open' end |
Instance Method Details
#delete_maps(rows, cols, deletes) ⇒ Object
作用 少读一些行或列
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ld/excel/sheet.rb', line 112 def delete_maps rows, cols, deletes raise "deletes 参数只能是 String" if deletes.class != String del_arr = deletes.split(',').map do |del| if del.match(/:/) raise "del params syntax error! \n'#{del}'" if del.split(':').size > 2 a, b = del.split(':') (a..b).to_a else del end end del_arr.flatten.each do |del| if is_row? del rows.delete del.to_i else cols.delete del.upcase end end end |
#insert_maps(rows, cols, inserts) ⇒ Object
作用 多读一些行或列
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ld/excel/sheet.rb', line 91 def insert_maps rows, cols, inserts raise "inserts 参数只能是 String" if inserts.class != String insert_arr = inserts.split(',').map do |insert| if insert.match(/:/) raise "insert params syntax error! \n'#{insert}'" if insert.split(':').size > 2 a, b = insert.split(':') (a..b).to_a else insert end end insert_arr.flatten.each do |insert| if is_row? insert rows << insert.to_i else cols << insert.upcase end end end |
#insert_row(row) ⇒ Object
作用 在当前sheet的主体内容末尾添加一行数据(传入一维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io)
221 222 223 224 |
# File 'lib/ld/excel/sheet.rb', line 221 def insert_row row raise 'insert_row 传入的必须是一个数组' if row.class != Array @rows << row end |
#is_row?(row) ⇒ Boolean
作用 判断要添加或要移除的是一行还是一列
159 160 161 162 163 164 |
# File 'lib/ld/excel/sheet.rb', line 159 def is_row? row if row.to_i.to_s == row.to_s return true end false end |
#parse_point(point) ⇒ Object
194 195 196 197 198 199 200 201 202 |
# File 'lib/ld/excel/sheet.rb', line 194 def parse_point point raise "无法解析excel坐标,坐标需要是String,不能是#{point.class.to_s}" if point.class != String point.upcase! characters = point.scan(/[A-Z]+/) raise "parse point error! \n'#{point}'" if characters.size != 1 numbers = point.scan(/[0-9]+/) raise "parse point error! \n'#{point}'" if numbers.size != 1 {:character => characters[0], :number => numbers[0].to_i} end |
#parse_string_scope(scope) ⇒ Object
作用 解析范围参数
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ld/excel/sheet.rb', line 36 def parse_string_scope scope PARAMETER_ERROR.hint_and_raise :scope, "'+' or '-' 只能存在1个" if scope.split('+').size > 2 or scope.split('-').size > 2 hash = {} scope.upcase! if scope.include? '+' hash[:scope], other = scope.split('+') if other.include? '-' hash[:insert], hash[:delete] = other.split('-') else hash[:insert] = other end else if scope.include? '-' hash[:scope], hash[:delete] = scope.split('-') else hash[:scope] = scope end end hash end |
#read(scope, show_location = false) ⇒ Object
作用 读sheet页数据,返回二维数组
29 30 31 32 33 |
# File 'lib/ld/excel/sheet.rb', line 29 def read scope, show_location = false raise "scope params is nil" if !scope map = read_scope_to_map scope read_arrs map, show_location end |
#read_arrs(map_arrs, show_location) ⇒ Object
作用 读二维数据(使用maps)
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ld/excel/sheet.rb', line 133 def read_arrs map_arrs, show_location map_arrs.map do |map_arr| map_arr.map do |map| value = read_unit_by_xy map[:col], map[:row], true if show_location {map[:location] => value} else value end end end end |
#read_scope_to_map(scope) ⇒ Object
作用 使用范围参数构建maps(预读)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ld/excel/sheet.rb', line 58 def read_scope_to_map scope scope = parse_string_scope scope if scope.class == String PARAMETER_ERROR.hint_and_raise :scope, "缺少scope参数,或':',或':'存在多个" if !scope[:scope] or !scope[:scope].match(/:/) or scope[:scope].split(':').size > 2 a, b = scope[:scope].split(':').map{|point| parse_point point} cols = (a[:character]..b[:character]).to_a rows = (a[:number]..b[:number]).to_a insert_maps rows, cols, scope[:insert].upcase if scope[:insert] delete_maps rows, cols, scope[:delete].upcase if scope[:delete] if scope[:delete] raise "delete 参数只能是 String" if scope[:delete].class != String end rows = rows.uniq.sort cols = cols.uniq.sort maps = rows.map do |row| cols.map do |col| col_i = ABSCISSA[col] raise "不存在这个列 \n'#{col}'" if !col_i { location:"#{col}#{row}", row:row - 1, col:col_i } end end # 调试 # maps.each do |arr| # puts arr.map{|a| "#{a[:location]}(#{a[:row]}_#{a[:col]})"}.to_s # end maps end |
#read_unit_by_xy(x, y, parse) ⇒ Object
作用 通过x,y坐标获取一个单元格的内容
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ld/excel/sheet.rb', line 147 def read_unit_by_xy x, y, parse # puts "x: #{x}\ty: #{y}" unit = @sheet.row(y)[x] if unit.instance_of? Spreadsheet::Formula if parse return unit.value end end return unit end |
#save ⇒ Object
作用 将数据写入sheet
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/ld/excel/sheet.rb', line 177 def save point = parse_point @point raise '保存sheet必须要有内容,请 set_rows' if !@rows raise '保存sheet必须要有name,请 set_rows' if !@name @rows.unshift @headings if @headings @sheet.default_format = @format @rows.each_with_index do |row, r| row.each_with_index do |unit, c| row = point[:number] + r - 1 col = ABSCISSA[point[:character]] + c write_unit_by_xy row, col, unit end end self end |
#set_color(color) ⇒ Object
作用 设置当前sheet页的字体颜色
236 237 238 |
# File 'lib/ld/excel/sheet.rb', line 236 def set_color color @format.font.color = color end |
#set_font(font) ⇒ Object
作用 设置当前sheet页的字体
247 248 249 |
# File 'lib/ld/excel/sheet.rb', line 247 def set_font font @format.font.name = font end |
#set_font_size(size) ⇒ Object
作用 设置当前sheet页的字体大小
241 242 243 244 |
# File 'lib/ld/excel/sheet.rb', line 241 def set_font_size size raise 'size 必须是一个整数' if size.class != Fixnum @format.font.size = size end |
#set_format(hash) ⇒ Object
261 262 263 264 265 |
# File 'lib/ld/excel/sheet.rb', line 261 def set_format hash set_color hash[:color] set_font_size hash[:font_size] set_font hash[:font] end |
#set_headings(headings) ⇒ Object
作用 在当前sheet的主体内容顶上方添加一个表头(传入二维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io)
211 212 213 214 215 216 217 218 |
# File 'lib/ld/excel/sheet.rb', line 211 def set_headings headings if headings raise 'headings 必须是一个数组' if headings.class != Array @headings = headings else @headings = nil end end |
#set_point(point) ⇒ Object
作用 设置当前sheet页的字体颜色
257 258 259 |
# File 'lib/ld/excel/sheet.rb', line 257 def set_point point @point = point end |
#set_rows(rows) ⇒ Object
作用 在当前sheet中添加主体数据(传入二维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io)
205 206 207 208 |
# File 'lib/ld/excel/sheet.rb', line 205 def set_rows rows raise '必须是一个数组且是一个二维数组' if rows.class != Array && rows.first.class != Array @rows = rows end |
#set_weight(weight) ⇒ Object
作用 设置当前sheet页的单元格宽度(暂时无效)
252 253 254 |
# File 'lib/ld/excel/sheet.rb', line 252 def set_weight weight @format end |
#write_unit_by_xy(x, y, unit) ⇒ Object
作用 通过x,y坐标往一个单元格中写入数据,但不写入(只有调用Ld::Excel的实例方法save才会写入io)
227 228 229 230 231 232 233 |
# File 'lib/ld/excel/sheet.rb', line 227 def write_unit_by_xy x, y, unit if unit.class == Array unit = unit.to_s puts '提示: 有一个单元格的内容是Array, 它被当成字符串写入' end @sheet.row(x)[y] = unit end |