Class: Ld::Excel
- Inherits:
-
Object
- Object
- Ld::Excel
- Defined in:
- lib/ld/excel/excel.rb
Instance Attribute Summary collapse
-
#basename ⇒ Object
Returns the value of attribute basename.
-
#bean ⇒ Object
Returns the value of attribute bean.
-
#beans ⇒ Object
Returns the value of attribute beans.
-
#excel ⇒ Object
Returns the value of attribute excel.
-
#format ⇒ Object
Returns the value of attribute format.
-
#mappings ⇒ Object
Returns the value of attribute mappings.
-
#path ⇒ Object
Returns the value of attribute path.
-
#scope_arrs ⇒ Object
Returns the value of attribute scope_arrs.
-
#sheet ⇒ Object
Returns the value of attribute sheet.
-
#sheets ⇒ Object
Returns the value of attribute sheets.
Class Method Summary collapse
- .create(path, &block) ⇒ Object
- .open(path) ⇒ Object
-
.parse_location(location) ⇒ Object
解析一个excel location.
- .test ⇒ Object
Instance Method Summary collapse
- #ab_to(a, b) ⇒ Object
- #flush ⇒ Object
-
#generate_map(address_str) ⇒ Object
用坐标解析一个excel scope.
-
#get_sheets ⇒ Object
获取所有页.
-
#initialize(path = nil) ⇒ Excel
constructor
构造函数,如果未传path则是创建一个新的excel, 如果传了path,则打开这个excel,不过打开前会验证后缀与是否存在.
- #is_number?(str) ⇒ Boolean
-
#jiang(arr) ⇒ Object
排序.
- #map_add(map, add) ⇒ Object
- #map_adds(map, adds) ⇒ Object
- #map_min(map, min) ⇒ Object
- #map_mins(map, mins) ⇒ Object
- #new_sheet(name) ⇒ Object
-
#open_new ⇒ Object
刷新excel中的sheet.
-
#open_sheet(sheet_name) ⇒ Object
获取一页.
-
#parse_address(address) ⇒ Object
解析范围配置.
- #parse_del_to_hash(address, scope) ⇒ Object
-
#read(full_scope, simple = true, filter_nil = false) ⇒ Object
先打开一个sheet页,再读scope范围数据 params?b13:m27-g.j.k.(14:18).
-
#read_location(location, parse = true) ⇒ Object
读一个单元格.
-
#read_location_list_arr(location_list_arr_str) ⇒ Object
读很多个location链,返回二维数组.
- #read_map(arrs, simple = true) ⇒ Object
-
#read_sheet_location(location, parse = true) ⇒ Object
读一个单元格2.
-
#read_sheet_locations(locations_config, parse = true) ⇒ Object
读取一个location链 ,返回一维数组.
-
#read_unit_by_xy(x, y, parse) ⇒ Object
通过x,y坐标获取unit内容.
-
#save(path) ⇒ Object
保存文件.
- #sheng(arr) ⇒ Object
- #write_sheet(sheet_name, &block) ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ Excel
构造函数,如果未传path则是创建一个新的excel, 如果传了path,则打开这个excel,不过打开前会验证后缀与是否存在
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ld/excel/excel.rb', line 20 def initialize(path = nil) if path!=nil if path.match(/.xls$/)!=nil if File::exist? path @excel = Spreadsheet.open path @path = path puts "打开文件: #{path}" else raise "文件不存在: #{path}" end else raise "只能打开.xls结尾的文件" end else @excel = Spreadsheet::Workbook.new puts "创建新的Excel实例" end @sheets = {} @sheet = nil end |
Instance Attribute Details
#basename ⇒ Object
Returns the value of attribute basename.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def basename @basename end |
#bean ⇒ Object
Returns the value of attribute bean.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def bean @bean end |
#beans ⇒ Object
Returns the value of attribute beans.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def beans @beans end |
#excel ⇒ Object
Returns the value of attribute excel.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def excel @excel end |
#format ⇒ Object
Returns the value of attribute format.
6 7 8 |
# File 'lib/ld/excel/excel.rb', line 6 def format @format end |
#mappings ⇒ Object
Returns the value of attribute mappings.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def mappings @mappings end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def path @path end |
#scope_arrs ⇒ Object
Returns the value of attribute scope_arrs.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def scope_arrs @scope_arrs end |
#sheet ⇒ Object
Returns the value of attribute sheet.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def sheet @sheet end |
#sheets ⇒ Object
Returns the value of attribute sheets.
5 6 7 |
# File 'lib/ld/excel/excel.rb', line 5 def sheets @sheets end |
Class Method Details
.create(path, &block) ⇒ Object
423 424 425 426 427 |
# File 'lib/ld/excel/excel.rb', line 423 def self.create path, &block excel = Ld::Excel.new block.call excel excel.save path end |
.open(path) ⇒ Object
41 42 43 |
# File 'lib/ld/excel/excel.rb', line 41 def self.open(path) self.new(path) end |
.parse_location(location) ⇒ Object
解析一个excel location
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ld/excel/excel.rb', line 137 def self.parse_location location if location and location.class == String location.upcase! { :x => ZIMU[location.scan(/[A-Z]+/).join].to_i, :y => (location.scan(/[0-9]+/).join.to_i - 1) } else ms.puts_fail "location为空或不是String类型,无法解析" end end |
.test ⇒ Object
429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/ld/excel/excel.rb', line 429 def self.test Ld::Excel.create '/Users/liudong/Desktop/abss.xls' do |excel| ['sh1','sh2','发有3'].each do |sheet_name| excel.write_sheet sheet_name do |sheet| sheet.set_format({color: :green, font_size: 22, font: '宋体'}) sheet.set_headings ['a','b'] sheet.set_point 'c5' (5..22).to_a.each do |i| sheet.add_row i.times.map{|j| '村腰里 是'} end end end end end |
Instance Method Details
#ab_to(a, b) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/ld/excel/excel.rb', line 149 def ab_to a, b type = nil if is_number?(a) == true and is_number?(b) == true type = 'y' case a.to_i <=> b.to_i when 1 return [type, (b..a).to_a] when -1 return [type, (a..b).to_a] when 0 return [type, [a]] end elsif is_number?(a) == false and is_number?(b) == false type = 'x' case a <=> b when 1 return [type, (b..a).to_a] when -1 return [type, (a..b).to_a] when 0 return [type, [a]] end else raise "解析excel配置范围时,':'两边必须要么都是字母,要么都是数字!" end end |
#flush ⇒ Object
118 119 120 |
# File 'lib/ld/excel/excel.rb', line 118 def flush Ld::Excel.new self.path end |
#generate_map(address_str) ⇒ Object
用坐标解析一个excel scope
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/ld/excel/excel.rb', line 226 def generate_map address_str map = {:x => [], :y => []} config = parse_address address_str if config[:scope] if config[:scope].include? ':' # map初始化 arr = config[:scope].split(':') if config[:scope].scan(/[0-9]+/).join == '' map_adds(map, ab_to(arr[0].scan(/[A-Z]+/).join, arr[1].scan(/[A-Z]+/).join)) elsif config[:scope].scan(/[A-Z]+/).join == '' map_adds(map, ab_to(arr[0].scan(/[0-9]+/).join, arr[1].scan(/[0-9]+/).join)) else map_adds(map, ab_to(arr[0].scan(/[0-9]+/).join, arr[1].scan(/[0-9]+/).join)) map_adds(map, ab_to(arr[0].scan(/[A-Z]+/).join, arr[1].scan(/[A-Z]+/).join)) end # map 添加 if config[:add_str] config[:add_str].split(',').each do |add| if add.include? ":" map_adds(map, ab_to(add.split(':')[0], add.split(':')[1])) else map_add map, add end end end # map 减小 if config[:min_str] config[:min_str].split(',').each do |min| if min.include? ":" map_mins(map, ab_to(min.split(':')[0], min.split(':')[1])) else map_min map, min end end end else raise "scope 没有 ':' 无法解析" end else raise "scope == nil" end map[:x].uniq! map[:y].uniq! arrs = [] map[:y].each do |y| rows = [] map[:x].each do |x| rows << ["#{x}_#{y}", ZIMU[x], y.to_i - 1] end arrs << rows end return arrs rescue puts "生成map时发生错误: #{$!}" puts $@ end |
#get_sheets ⇒ Object
获取所有页
57 58 59 60 61 |
# File 'lib/ld/excel/excel.rb', line 57 def get_sheets @sheets = @excel.worksheets puts "返回 #{@sheets.size} 页" self end |
#is_number?(str) ⇒ Boolean
218 219 220 221 222 223 |
# File 'lib/ld/excel/excel.rb', line 218 def is_number? str if str.to_i.to_s == str.to_s return true end false end |
#jiang(arr) ⇒ Object
排序
375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/ld/excel/excel.rb', line 375 def jiang arr 0.upto(arr.size - 2) do |i| (i+1).upto(arr.size - 1) do |j| if arr[i] < arr[j] arr[i] = arr[i] + arr[j] arr[j] = arr[i] - arr[j] arr[i] = arr[i] - arr[j] end end end arr end |
#map_add(map, add) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/ld/excel/excel.rb', line 189 def map_add map, add if is_number? add map[:y] << add else map[:x] << add end end |
#map_adds(map, adds) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/ld/excel/excel.rb', line 176 def map_adds map, adds case adds[0] when 'x' adds[1].each do |add| map[:x] << add end when 'y' adds[1].each do |add| map[:y] << add end end end |
#map_min(map, min) ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/ld/excel/excel.rb', line 210 def map_min map, min if is_number? min map[:y].delete min else map[:x].delete min end end |
#map_mins(map, mins) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/ld/excel/excel.rb', line 197 def map_mins map, mins case mins[0] when 'x' mins[1].each do |min| map[:x].delete min end when 'y' mins[1].each do |min| map[:y].delete min end end end |
#new_sheet(name) ⇒ Object
413 414 415 |
# File 'lib/ld/excel/excel.rb', line 413 def new_sheet name Ld::Sheet.new @excel, name end |
#open_new ⇒ Object
刷新excel中的sheet
78 79 80 |
# File 'lib/ld/excel/excel.rb', line 78 def open_new excel_new = Ld::Excel.open @path end |
#open_sheet(sheet_name) ⇒ Object
获取一页
46 47 48 49 50 51 52 53 54 |
# File 'lib/ld/excel/excel.rb', line 46 def open_sheet sheet_name @sheet = @excel.worksheet sheet_name if @sheet == nil raise "未找到 sheet #{sheet_name}" else # puts "sheet #{sheet_name}" end self end |
#parse_address(address) ⇒ Object
解析范围配置
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/ld/excel/excel.rb', line 285 def parse_address address hash = {} if address address.upcase! else raise "address 为 nil" end if address.split('+').size > 2 raise "'+'号只能有1个" end if address.split('-').size > 2 raise "'-'号只能有1个" end if address.include?('+') a = address.split('+')[0] b = address.split('+')[1] if a.include?('-') hash.store :scope, a.split('-')[0] hash.store :min_str, a.split('-')[1] hash.store :add_str, b else hash.store :scope, a if b.include?('-') hash.store :min_str, b.split('-')[1] hash.store :add_str, b.split('-')[0] else hash.store :add_str, b end end else if address.include?('-') hash.store :scope, address.split('-')[0] hash.store :min_str, address.split('-')[1] else hash.store :scope, address end end hash end |
#parse_del_to_hash(address, scope) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ld/excel/excel.rb', line 122 def parse_del_to_hash address, scope arr = address.split '-' arr = arr[1..arr.size-1] start_row_num = scope.scan(/\d/).join[0..1]# 首行行号 location = Ld::Excel.parse_location(scope.split(':')[0]) hash = {} del_rows = [] address.each do |del_row_num|# 去除行行号 rows << del_row_num.to_i - start_row_num.to_i# 去除行行号 - 首行行号 = 数组角标位置 end hash.store(:rows,jiang(rows)) hash end |
#read(full_scope, simple = true, filter_nil = false) ⇒ Object
先打开一个sheet页,再读scope范围数据params?b13:m27-g.j.k.(14:18)
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/ld/excel/excel.rb', line 327 def read full_scope, simple = true, filter_nil = false if full_scope.include?('?') sheet_name = full_scope.split('?')[0] if sheet_name open_sheet sheet_name else raise "sheetname为nil" end address_str = full_scope.split('?')[1] map = generate_map address_str data_arrs = read_map map, simple if data_arrs.size == 0 puts "没有任何内容的区域! #{full_scope}" else puts "#{full_scope}" end # 除去不完整数据 if filter_nil == true (data_arrs.size - 1).downto(0) do |i| arr = data_arrs[i] if arr[0] == nil or arr[1] == nil data_arrs.delete_at i end end end return data_arrs else raise "缺少?,需要在'?'左边指定sheet的名称" end end |
#read_location(location, parse = true) ⇒ Object
读一个单元格
64 65 66 67 68 |
# File 'lib/ld/excel/excel.rb', line 64 def read_location(location,parse = true) l = Ld::Excel.parse_location(location) unit = read_unit_by_xy(l[:r],l[:c],parse) # puts "" end |
#read_location_list_arr(location_list_arr_str) ⇒ Object
读很多个location链,返回二维数组
83 84 85 86 87 88 89 90 |
# File 'lib/ld/excel/excel.rb', line 83 def read_location_list_arr(location_list_arr_str) units_arr = [] location_list_arr_str.split(',').each do |location_list| units = read_location_list(location_list) units_arr << units end units_arr end |
#read_map(arrs, simple = true) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/ld/excel/excel.rb', line 358 def read_map arrs, simple = true @scope_arrs = [] arrs.each do |arr| rows = [] arr.each do |a| if simple rows << read_unit_by_xy(a[1], a[2], true) else rows << {:index => a[0], :value => read_unit_by_xy(a[1], a[2], true)} end end @scope_arrs << rows end @scope_arrs end |
#read_sheet_location(location, parse = true) ⇒ Object
读一个单元格2
71 72 73 74 |
# File 'lib/ld/excel/excel.rb', line 71 def read_sheet_location(location,parse = true) open_sheet location.split('?')[0] read_location location.split('?')[1] end |
#read_sheet_locations(locations_config, parse = true) ⇒ Object
读取一个location链 ,返回一维数组
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ld/excel/excel.rb', line 93 def read_sheet_locations(locations_config,parse = true) unit_list = [] open_sheet locations_config.split('?')[0] locations_config.split('?')[1].split('.').each do |location| l = Ld::Excel.parse_location(location) unit = read_unit_by_xy(l[:r],l[:c],parse) unit_list << unit end unit_list end |
#read_unit_by_xy(x, y, parse) ⇒ Object
通过x,y坐标获取unit内容
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ld/excel/excel.rb', line 107 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(path) ⇒ Object
保存文件
402 403 404 405 406 407 408 409 410 411 |
# File 'lib/ld/excel/excel.rb', line 402 def save path if File.exist? path @excel.write path puts "保存覆盖了一个同名文件 #{@path}" else @excel.write path puts "保存到: #{path}" end self end |
#sheng(arr) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/ld/excel/excel.rb', line 388 def sheng arr 0.upto(arr.size - 2) do |i| (i+1).upto(arr.size - 1) do |j| if arr[i] > arr[j] arr[i] = arr[i] + arr[j] arr[j] = arr[i] - arr[j] arr[i] = arr[i] - arr[j] end end end arr end |
#write_sheet(sheet_name, &block) ⇒ Object
417 418 419 420 421 |
# File 'lib/ld/excel/excel.rb', line 417 def write_sheet sheet_name, &block @sheet = new_sheet sheet_name block.call @sheet @sheet.save end |