ld 0.2.14 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -9
- data/lib/ld/excel/excel.rb +31 -394
- data/lib/ld/excel/sheet.rb +253 -7
- data/lib/ld/project/project.rb +2 -2
- data/lib/ld/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d473f30220597956292a6941e19ba858049e6397
|
4
|
+
data.tar.gz: faf55229c3191ccf2c0d49723d4f6d9e048d95b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 284fe0a7d62770499a25364635848437f3b2c07349972e00826258c8a2c86ce57ccbc9eaba89670a38170b6c5ec6f71600196bfa8e1a3ce7c8ae806f0808bac3
|
7
|
+
data.tar.gz: 783c3e70507df5011990945ed8aabe7dc7bb34e5dcf76de3ba0e1718fbbec17c3bd11642214a3da965baef7f6c85c1720480e6da82cbb285ebe1e15c754ef95c
|
data/README.md
CHANGED
@@ -12,13 +12,13 @@ end
|
|
12
12
|
```
|
13
13
|
## Introduction to the
|
14
14
|
|
15
|
-
设计这个gem,我希望可以帮助大家在开发简单rails应用时,可以帮助大家完成50%以上的简单而重复的工作
|
16
|
-
我会提供一些类与方法,在console中使用,调用它们会生成项目结构xls文件,生成的这个xls文件中的数据,类似于一个小型文件数据库
|
17
|
-
然后我们可以以此为基础,查询项目中的一些信息.我想这样会有助于我们快速理解一个项目的基本结构,与重要文件在哪
|
18
|
-
也可以在修复bug时对bug相关文件与方法,起到快速定位的作用
|
19
|
-
这是我设计本gem的初衷,未来应该会持续更新这个gem,让它变得更加强大与方便
|
20
|
-
最终的目的是,希望这个gem可以起到快速搭建简单rails应用的作用,提升工作效率,节省时间
|
21
|
-
比如我们可以集成一些常用的模块到这个gem中,在搭建项目时只需要执行一条简单的命令就可以创建
|
15
|
+
设计这个gem,我希望可以帮助大家在开发简单rails应用时,可以帮助大家完成50%以上的简单而重复的工作
|
16
|
+
我会提供一些类与方法,在console中使用,调用它们会生成项目结构xls文件,生成的这个xls文件中的数据,类似于一个小型文件数据库
|
17
|
+
然后我们可以以此为基础,查询项目中的一些信息.我想这样会有助于我们快速理解一个项目的基本结构,与重要文件在哪
|
18
|
+
也可以在修复bug时对bug相关文件与方法,起到快速定位的作用
|
19
|
+
这是我设计本gem的初衷,未来应该会持续更新这个gem,让它变得更加强大与方便
|
20
|
+
最终的目的是,希望这个gem可以起到快速搭建简单rails应用的作用,提升工作效率,节省时间
|
21
|
+
比如我们可以集成一些常用的模块到这个gem中,在搭建项目时只需要执行一条简单的命令就可以创建
|
22
22
|
|
23
23
|
## Installation
|
24
24
|
|
@@ -53,7 +53,7 @@ Ld::Excel.open('project.xls').read('models?a1:j100-f,h,i')
|
|
53
53
|
Ld::Excel.open('project.xls').read('tables?a1:i300')
|
54
54
|
|
55
55
|
# Create xls, Need to change the file path to your own, and then run
|
56
|
-
Ld::Excel.create 'excel_test.xls' do |excel|
|
56
|
+
Ld::Excel.create :file_path =>'excel_test.xls' do |excel|
|
57
57
|
excel.write_sheet 'sheet1' do |sheet|
|
58
58
|
sheet.set_format({color: :red, font_size: 11, font: '宋体'})
|
59
59
|
sheet.set_headings ['title1','title2','title3']
|
@@ -94,4 +94,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
94
94
|
|
95
95
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
96
96
|
|
97
|
-
##
|
97
|
+
##
|
data/lib/ld/excel/excel.rb
CHANGED
@@ -2,24 +2,12 @@ require 'spreadsheet'
|
|
2
2
|
Spreadsheet.client_encoding = 'UTF-8'
|
3
3
|
|
4
4
|
class Ld::Excel
|
5
|
-
attr_accessor :excel
|
6
|
-
attr_accessor :format
|
7
|
-
|
8
|
-
@@hz ||= ".xls"
|
9
|
-
ZIMU ||= {}
|
10
|
-
|
11
|
-
if ZIMU.empty?
|
12
|
-
flag = 'A'
|
13
|
-
0.upto(9999) do |i|
|
14
|
-
ZIMU.store(flag,i)
|
15
|
-
flag = flag.succ
|
16
|
-
end
|
17
|
-
end
|
5
|
+
attr_accessor :excel, :path
|
18
6
|
|
19
7
|
# 构造函数,如果未传path则是创建一个新的excel, 如果传了path,则打开这个excel,不过打开前会验证后缀与是否存在
|
20
|
-
def initialize
|
21
|
-
if path
|
22
|
-
if path.match(/.xls$/)
|
8
|
+
def initialize path = nil
|
9
|
+
if path
|
10
|
+
if path.match(/.xls$/)
|
23
11
|
if File::exist? path
|
24
12
|
@excel = Spreadsheet.open path
|
25
13
|
@path = path
|
@@ -34,379 +22,42 @@ class Ld::Excel
|
|
34
22
|
@excel = Spreadsheet::Workbook.new
|
35
23
|
puts "创建新的Excel实例"
|
36
24
|
end
|
37
|
-
@sheets = {}
|
38
|
-
@sheet = nil
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.open(path)
|
42
|
-
self.new(path)
|
43
25
|
end
|
44
26
|
|
45
27
|
# 获取一页
|
46
|
-
def open_sheet
|
47
|
-
|
48
|
-
if @sheet == nil
|
49
|
-
raise "未找到 sheet #{sheet_name}"
|
50
|
-
else
|
51
|
-
# puts "sheet #{sheet_name}"
|
52
|
-
end
|
53
|
-
self
|
54
|
-
end
|
55
|
-
|
56
|
-
# 获取所有页
|
57
|
-
def get_sheets
|
58
|
-
@sheets = @excel.worksheets
|
59
|
-
puts "返回 #{@sheets.size} 页"
|
60
|
-
self
|
61
|
-
end
|
62
|
-
|
63
|
-
# 读一个单元格
|
64
|
-
def read_location(location,parse = true)
|
65
|
-
l = Ld::Excel.parse_location(location)
|
66
|
-
unit = read_unit_by_xy(l[:r],l[:c],parse)
|
67
|
-
# puts ""
|
68
|
-
end
|
69
|
-
|
70
|
-
# 读一个单元格2
|
71
|
-
def read_sheet_location(location,parse = true)
|
72
|
-
open_sheet location.split('?')[0]
|
73
|
-
read_location location.split('?')[1]
|
74
|
-
end
|
75
|
-
|
76
|
-
|
77
|
-
# 刷新excel中的sheet
|
78
|
-
def open_new
|
79
|
-
excel_new = Ld::Excel.open @path
|
80
|
-
end
|
81
|
-
|
82
|
-
# 读很多个location链,返回二维数组
|
83
|
-
def read_location_list_arr(location_list_arr_str)
|
84
|
-
units_arr = []
|
85
|
-
location_list_arr_str.split(',').each do |location_list|
|
86
|
-
units = read_location_list(location_list)
|
87
|
-
units_arr << units
|
88
|
-
end
|
89
|
-
units_arr
|
90
|
-
end
|
91
|
-
|
92
|
-
# 读取一个location链 ,返回一维数组
|
93
|
-
def read_sheet_locations(locations_config,parse = true)
|
94
|
-
unit_list = []
|
95
|
-
open_sheet locations_config.split('?')[0]
|
96
|
-
locations_config.split('?')[1].split('.').each do |location|
|
97
|
-
l = Ld::Excel.parse_location(location)
|
98
|
-
unit = read_unit_by_xy(l[:r],l[:c],parse)
|
99
|
-
unit_list << unit
|
100
|
-
end
|
101
|
-
unit_list
|
102
|
-
end
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
# 通过x,y坐标获取unit内容
|
107
|
-
def read_unit_by_xy x, y, parse
|
108
|
-
# puts "x: #{x}\ty: #{y}"
|
109
|
-
unit = @sheet.row(y)[x]
|
110
|
-
if unit.instance_of? Spreadsheet::Formula
|
111
|
-
if parse
|
112
|
-
return unit.value
|
113
|
-
end
|
114
|
-
end
|
115
|
-
return unit
|
28
|
+
def open_sheet name
|
29
|
+
Ld::Sheet.open @excel, name
|
116
30
|
end
|
117
31
|
|
32
|
+
# 刷新 重读一次
|
118
33
|
def flush
|
119
|
-
Ld::Excel.
|
120
|
-
end
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
end
|
135
|
-
|
136
|
-
# 解析一个excel location
|
137
|
-
def self.parse_location location
|
138
|
-
if location and location.class == String
|
139
|
-
location.upcase!
|
140
|
-
{
|
141
|
-
:x => ZIMU[location.scan(/[A-Z]+/).join].to_i,
|
142
|
-
:y => (location.scan(/[0-9]+/).join.to_i - 1)
|
143
|
-
}
|
144
|
-
else
|
145
|
-
ms.puts_fail "location为空或不是String类型,无法解析"
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
def ab_to a, b
|
150
|
-
type = nil
|
151
|
-
if is_number?(a) == true and is_number?(b) == true
|
152
|
-
type = 'y'
|
153
|
-
case a.to_i <=> b.to_i
|
154
|
-
when 1
|
155
|
-
return [type, (b..a).to_a]
|
156
|
-
when -1
|
157
|
-
return [type, (a..b).to_a]
|
158
|
-
when 0
|
159
|
-
return [type, [a]]
|
160
|
-
end
|
161
|
-
elsif is_number?(a) == false and is_number?(b) == false
|
162
|
-
type = 'x'
|
163
|
-
case a <=> b
|
164
|
-
when 1
|
165
|
-
return [type, (b..a).to_a]
|
166
|
-
when -1
|
167
|
-
return [type, (a..b).to_a]
|
168
|
-
when 0
|
169
|
-
return [type, [a]]
|
170
|
-
end
|
171
|
-
else
|
172
|
-
raise "解析excel配置范围时,':'两边必须要么都是字母,要么都是数字!"
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
def map_adds map, adds
|
177
|
-
case adds[0]
|
178
|
-
when 'x'
|
179
|
-
adds[1].each do |add|
|
180
|
-
map[:x] << add
|
181
|
-
end
|
182
|
-
when 'y'
|
183
|
-
adds[1].each do |add|
|
184
|
-
map[:y] << add
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
def map_add map, add
|
190
|
-
if is_number? add
|
191
|
-
map[:y] << add
|
192
|
-
else
|
193
|
-
map[:x] << add
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
def map_mins map, mins
|
198
|
-
case mins[0]
|
199
|
-
when 'x'
|
200
|
-
mins[1].each do |min|
|
201
|
-
map[:x].delete min
|
202
|
-
end
|
203
|
-
when 'y'
|
204
|
-
mins[1].each do |min|
|
205
|
-
map[:y].delete min
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
def map_min map, min
|
211
|
-
if is_number? min
|
212
|
-
map[:y].delete min
|
213
|
-
else
|
214
|
-
map[:x].delete min
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
def is_number? str
|
219
|
-
if str.to_i.to_s == str.to_s
|
220
|
-
return true
|
221
|
-
end
|
222
|
-
false
|
223
|
-
end
|
224
|
-
|
225
|
-
# 用坐标解析一个excel scope
|
226
|
-
def generate_map address_str
|
227
|
-
map = {:x => [], :y => []}
|
228
|
-
config = parse_address address_str
|
229
|
-
if config[:scope]
|
230
|
-
if config[:scope].include? ':'
|
231
|
-
# map初始化
|
232
|
-
arr = config[:scope].split(':')
|
233
|
-
if config[:scope].scan(/[0-9]+/).join == ''
|
234
|
-
map_adds(map, ab_to(arr[0].scan(/[A-Z]+/).join, arr[1].scan(/[A-Z]+/).join))
|
235
|
-
elsif config[:scope].scan(/[A-Z]+/).join == ''
|
236
|
-
map_adds(map, ab_to(arr[0].scan(/[0-9]+/).join, arr[1].scan(/[0-9]+/).join))
|
237
|
-
else
|
238
|
-
map_adds(map, ab_to(arr[0].scan(/[0-9]+/).join, arr[1].scan(/[0-9]+/).join))
|
239
|
-
map_adds(map, ab_to(arr[0].scan(/[A-Z]+/).join, arr[1].scan(/[A-Z]+/).join))
|
240
|
-
end
|
241
|
-
# map 添加
|
242
|
-
if config[:add_str]
|
243
|
-
config[:add_str].split(',').each do |add|
|
244
|
-
if add.include? ":"
|
245
|
-
map_adds(map, ab_to(add.split(':')[0], add.split(':')[1]))
|
246
|
-
else
|
247
|
-
map_add map, add
|
248
|
-
end
|
249
|
-
end
|
250
|
-
end
|
251
|
-
# map 减小
|
252
|
-
if config[:min_str]
|
253
|
-
config[:min_str].split(',').each do |min|
|
254
|
-
if min.include? ":"
|
255
|
-
map_mins(map, ab_to(min.split(':')[0], min.split(':')[1]))
|
256
|
-
else
|
257
|
-
map_min map, min
|
258
|
-
end
|
259
|
-
end
|
260
|
-
end
|
261
|
-
else
|
262
|
-
raise "scope 没有 ':' 无法解析"
|
263
|
-
end
|
264
|
-
else
|
265
|
-
raise "scope == nil"
|
266
|
-
end
|
267
|
-
map[:x].uniq!
|
268
|
-
map[:y].uniq!
|
269
|
-
arrs = []
|
270
|
-
map[:y].each do |y|
|
271
|
-
rows = []
|
272
|
-
map[:x].each do |x|
|
273
|
-
rows << ["#{x}_#{y}", ZIMU[x], y.to_i - 1]
|
274
|
-
end
|
275
|
-
arrs << rows
|
276
|
-
end
|
277
|
-
return arrs
|
278
|
-
rescue
|
279
|
-
puts "生成map时发生错误: #{$!}"
|
280
|
-
puts $@
|
281
|
-
end
|
282
|
-
|
283
|
-
|
284
|
-
# 解析范围配置
|
285
|
-
def parse_address address
|
286
|
-
hash = {}
|
287
|
-
if address
|
288
|
-
address.upcase!
|
289
|
-
else
|
290
|
-
raise "address 为 nil"
|
291
|
-
end
|
292
|
-
if address.split('+').size > 2
|
293
|
-
raise "'+'号只能有1个"
|
294
|
-
end
|
295
|
-
if address.split('-').size > 2
|
296
|
-
raise "'-'号只能有1个"
|
297
|
-
end
|
298
|
-
if address.include?('+')
|
299
|
-
a = address.split('+')[0]
|
300
|
-
b = address.split('+')[1]
|
301
|
-
if a.include?('-')
|
302
|
-
hash.store :scope, a.split('-')[0]
|
303
|
-
hash.store :min_str, a.split('-')[1]
|
304
|
-
hash.store :add_str, b
|
305
|
-
else
|
306
|
-
hash.store :scope, a
|
307
|
-
if b.include?('-')
|
308
|
-
hash.store :min_str, b.split('-')[1]
|
309
|
-
hash.store :add_str, b.split('-')[0]
|
310
|
-
else
|
311
|
-
hash.store :add_str, b
|
312
|
-
end
|
313
|
-
end
|
314
|
-
else
|
315
|
-
if address.include?('-')
|
316
|
-
hash.store :scope, address.split('-')[0]
|
317
|
-
hash.store :min_str, address.split('-')[1]
|
318
|
-
else
|
319
|
-
hash.store :scope, address
|
320
|
-
end
|
321
|
-
end
|
322
|
-
hash
|
323
|
-
end
|
324
|
-
|
325
|
-
# 先打开一个sheet页,再读scope范围数据
|
326
|
-
# params?b13:m27-g.j.k.(14:18)
|
327
|
-
def read full_scope, simple = true, filter_nil = false
|
328
|
-
if full_scope.include?('?')
|
329
|
-
sheet_name = full_scope.split('?')[0]
|
330
|
-
if sheet_name
|
331
|
-
open_sheet sheet_name
|
332
|
-
else
|
333
|
-
raise "sheetname为nil"
|
334
|
-
end
|
335
|
-
address_str = full_scope.split('?')[1]
|
336
|
-
map = generate_map address_str
|
337
|
-
data_arrs = read_map map, simple
|
338
|
-
if data_arrs.size == 0
|
339
|
-
puts "没有任何内容的区域! #{full_scope}"
|
340
|
-
else
|
341
|
-
puts "#{full_scope}"
|
342
|
-
end
|
343
|
-
# 除去不完整数据
|
344
|
-
if filter_nil == true
|
345
|
-
(data_arrs.size - 1).downto(0) do |i|
|
346
|
-
arr = data_arrs[i]
|
347
|
-
if arr[0] == nil or arr[1] == nil
|
348
|
-
data_arrs.delete_at i
|
349
|
-
end
|
350
|
-
end
|
351
|
-
end
|
352
|
-
return data_arrs
|
353
|
-
else
|
354
|
-
raise "缺少?,需要在'?'左边指定sheet的名称"
|
355
|
-
end
|
356
|
-
end
|
357
|
-
|
358
|
-
def read_map arrs, simple = true
|
359
|
-
@scope_arrs = []
|
360
|
-
arrs.each do |arr|
|
361
|
-
rows = []
|
362
|
-
arr.each do |a|
|
363
|
-
if simple
|
364
|
-
rows << read_unit_by_xy(a[1], a[2], true)
|
365
|
-
else
|
366
|
-
rows << {:index => a[0], :value => read_unit_by_xy(a[1], a[2], true)}
|
34
|
+
@excel = Ld::Excel.open @path
|
35
|
+
end
|
36
|
+
|
37
|
+
# content_url = "Sheet1?b13:m27-g.j.k.(14:18)"
|
38
|
+
def read address_path_full, simple = false, filter_nil = false
|
39
|
+
raise "缺少?, 需要在'?'左边指定sheet的名称" if !address_path_full.match(/\?/)
|
40
|
+
sheet_name, address_path = address_path_full.split('?')
|
41
|
+
@current_sheet = open_sheet sheet_name
|
42
|
+
arrs = @current_sheet.read address_path, simple
|
43
|
+
# 除去不完整数据
|
44
|
+
if filter_nil
|
45
|
+
(arrs.size - 1).downto(0) do |i|
|
46
|
+
arr = arrs[i]
|
47
|
+
if arr[0] == nil or arr[1] == nil
|
48
|
+
arrs.delete_at i
|
367
49
|
end
|
368
50
|
end
|
369
|
-
@scope_arrs << rows
|
370
51
|
end
|
371
|
-
@scope_arrs
|
372
|
-
end
|
373
52
|
|
374
|
-
|
375
|
-
def jiang arr
|
376
|
-
0.upto(arr.size - 2) do |i|
|
377
|
-
(i+1).upto(arr.size - 1) do |j|
|
378
|
-
if arr[i] < arr[j]
|
379
|
-
arr[i] = arr[i] + arr[j]
|
380
|
-
arr[j] = arr[i] - arr[j]
|
381
|
-
arr[i] = arr[i] - arr[j]
|
382
|
-
end
|
383
|
-
end
|
384
|
-
end
|
385
|
-
arr
|
386
|
-
end
|
387
|
-
|
388
|
-
def sheng arr
|
389
|
-
0.upto(arr.size - 2) do |i|
|
390
|
-
(i+1).upto(arr.size - 1) do |j|
|
391
|
-
if arr[i] > arr[j]
|
392
|
-
arr[i] = arr[i] + arr[j]
|
393
|
-
arr[j] = arr[i] - arr[j]
|
394
|
-
arr[i] = arr[i] - arr[j]
|
395
|
-
end
|
396
|
-
end
|
397
|
-
end
|
398
|
-
arr
|
53
|
+
arrs
|
399
54
|
end
|
400
55
|
|
401
56
|
# 保存文件
|
402
57
|
def save path
|
403
|
-
if File.exist? path
|
404
|
-
|
405
|
-
|
406
|
-
else
|
407
|
-
@excel.write path
|
408
|
-
puts "保存到: #{path}"
|
409
|
-
end
|
58
|
+
puts "这个操作将会覆盖了这个文件:#{path}" if File.exist? path
|
59
|
+
@excel.write path
|
60
|
+
puts "保存excel成功:#{path}"
|
410
61
|
self
|
411
62
|
end
|
412
63
|
|
@@ -415,30 +66,16 @@ class Ld::Excel
|
|
415
66
|
end
|
416
67
|
|
417
68
|
def write_sheet sheet_name, &block
|
418
|
-
|
419
|
-
block.call
|
420
|
-
|
69
|
+
sheet = new_sheet sheet_name
|
70
|
+
block.call sheet
|
71
|
+
sheet.save
|
421
72
|
end
|
422
73
|
|
423
|
-
def self.create
|
74
|
+
def self.create hash, &block
|
424
75
|
excel = Ld::Excel.new
|
425
76
|
block.call excel
|
426
|
-
excel.save
|
77
|
+
excel.save hash[:file_path]
|
427
78
|
end
|
428
79
|
|
429
|
-
def self.test
|
430
|
-
Ld::Excel.create '/Users/liudong/Desktop/abss.xls' do |excel|
|
431
|
-
['sh1','sh2','发有3'].each do |sheet_name|
|
432
|
-
excel.write_sheet sheet_name do |sheet|
|
433
|
-
sheet.set_format({color: :green, font_size: 22, font: '宋体'})
|
434
|
-
sheet.set_headings ['a','b']
|
435
|
-
sheet.set_point 'c5'
|
436
|
-
(5..22).to_a.each do |i|
|
437
|
-
sheet.add_row i.times.map{|j| '村腰里 是'}
|
438
|
-
end
|
439
|
-
end
|
440
|
-
end
|
441
|
-
end
|
442
|
-
end
|
443
80
|
end
|
444
81
|
|
data/lib/ld/excel/sheet.rb
CHANGED
@@ -1,17 +1,255 @@
|
|
1
1
|
class Ld::Sheet
|
2
|
+
attr_accessor :excel, :sheet
|
2
3
|
|
3
|
-
|
4
|
+
ABSCISSA = {}
|
5
|
+
if ABSCISSA.empty?
|
6
|
+
zm = 'A'
|
7
|
+
ABSCISSA[zm] = 0
|
8
|
+
19999.times{|i| ABSCISSA[zm.succ!] = i+1}
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize excel, name, type = 'new'
|
12
|
+
raise "name为 nil" if !name
|
4
13
|
@excel = excel
|
5
14
|
@name = name
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
15
|
+
case type
|
16
|
+
when 'new'
|
17
|
+
@sheet = excel.create_worksheet :name => name
|
18
|
+
@point = 'a1'
|
19
|
+
@headings = nil
|
20
|
+
@rows = []
|
21
|
+
when 'open'
|
22
|
+
@sheet = excel.worksheet name
|
23
|
+
raise "#{name} 不存在" if !@sheet
|
24
|
+
end
|
10
25
|
@format = @sheet.default_format
|
11
26
|
end
|
12
27
|
|
28
|
+
def read address_str, simple = false
|
29
|
+
map = generate_map address_str
|
30
|
+
arrs = read_map map, simple
|
31
|
+
if arrs.size == 0
|
32
|
+
puts "没有任何内容的区域! #{address_str}"
|
33
|
+
else
|
34
|
+
puts "#{address_str}"
|
35
|
+
end
|
36
|
+
arrs
|
37
|
+
end
|
38
|
+
|
39
|
+
# simple 带不带坐标index数据
|
40
|
+
def read_map arrs, simple
|
41
|
+
@scope_arrs = []
|
42
|
+
arrs.each do |arr|
|
43
|
+
rows = []
|
44
|
+
arr.each do |a|
|
45
|
+
if simple
|
46
|
+
rows << {:index => a[0], :value => read_unit_by_xy(a[1], a[2], true)}
|
47
|
+
else
|
48
|
+
rows << read_unit_by_xy(a[1], a[2], true)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
@scope_arrs << rows
|
52
|
+
end
|
53
|
+
@scope_arrs
|
54
|
+
end
|
55
|
+
|
56
|
+
# 通过x,y坐标获取unit内容
|
57
|
+
def read_unit_by_xy x, y, parse
|
58
|
+
# puts "x: #{x}\ty: #{y}"
|
59
|
+
unit = @sheet.row(y)[x]
|
60
|
+
if unit.instance_of? Spreadsheet::Formula
|
61
|
+
if parse
|
62
|
+
return unit.value
|
63
|
+
end
|
64
|
+
end
|
65
|
+
return unit
|
66
|
+
end
|
67
|
+
|
68
|
+
# 用坐标解析一个excel scope
|
69
|
+
def generate_map address_str
|
70
|
+
map = {:x => [], :y => []}
|
71
|
+
config = parse_address address_str
|
72
|
+
if config[:scope]
|
73
|
+
if config[:scope].include? ':'
|
74
|
+
# map初始化
|
75
|
+
arr = config[:scope].split(':')
|
76
|
+
if config[:scope].scan(/[0-9]+/).join == ''
|
77
|
+
map_adds(map, ab_to(arr[0].scan(/[A-Z]+/).join, arr[1].scan(/[A-Z]+/).join))
|
78
|
+
elsif config[:scope].scan(/[A-Z]+/).join == ''
|
79
|
+
map_adds(map, ab_to(arr[0].scan(/[0-9]+/).join, arr[1].scan(/[0-9]+/).join))
|
80
|
+
else
|
81
|
+
map_adds(map, ab_to(arr[0].scan(/[0-9]+/).join, arr[1].scan(/[0-9]+/).join))
|
82
|
+
map_adds(map, ab_to(arr[0].scan(/[A-Z]+/).join, arr[1].scan(/[A-Z]+/).join))
|
83
|
+
end
|
84
|
+
# map 添加
|
85
|
+
if config[:add_str]
|
86
|
+
config[:add_str].split(',').each do |add|
|
87
|
+
if add.include? ":"
|
88
|
+
map_adds(map, ab_to(add.split(':')[0], add.split(':')[1]))
|
89
|
+
else
|
90
|
+
map_add map, add
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
# map 减小
|
95
|
+
if config[:min_str]
|
96
|
+
config[:min_str].split(',').each do |min|
|
97
|
+
if min.include? ":"
|
98
|
+
map_mins(map, ab_to(min.split(':')[0], min.split(':')[1]))
|
99
|
+
else
|
100
|
+
map_min map, min
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
else
|
105
|
+
raise "scope 没有 ':' 无法解析"
|
106
|
+
end
|
107
|
+
else
|
108
|
+
raise "scope == nil"
|
109
|
+
end
|
110
|
+
map[:x].uniq!
|
111
|
+
map[:y].uniq!
|
112
|
+
arrs = []
|
113
|
+
map[:y].each do |y|
|
114
|
+
rows = []
|
115
|
+
map[:x].each do |x|
|
116
|
+
rows << ["#{x}_#{y}", ABSCISSA[x], y.to_i - 1]
|
117
|
+
end
|
118
|
+
arrs << rows
|
119
|
+
end
|
120
|
+
return arrs
|
121
|
+
rescue
|
122
|
+
puts "生成map时发生错误: #{$!}"
|
123
|
+
puts $@
|
124
|
+
end
|
125
|
+
|
126
|
+
def ab_to a, b
|
127
|
+
type = nil
|
128
|
+
if is_number?(a) == true and is_number?(b) == true
|
129
|
+
type = 'y'
|
130
|
+
case a.to_i <=> b.to_i
|
131
|
+
when 1
|
132
|
+
return [type, (b..a).to_a]
|
133
|
+
when -1
|
134
|
+
return [type, (a..b).to_a]
|
135
|
+
when 0
|
136
|
+
return [type, [a]]
|
137
|
+
end
|
138
|
+
elsif is_number?(a) == false and is_number?(b) == false
|
139
|
+
type = 'x'
|
140
|
+
case a <=> b
|
141
|
+
when 1
|
142
|
+
return [type, (b..a).to_a]
|
143
|
+
when -1
|
144
|
+
return [type, (a..b).to_a]
|
145
|
+
when 0
|
146
|
+
return [type, [a]]
|
147
|
+
end
|
148
|
+
else
|
149
|
+
raise "解析excel配置范围时,':'两边必须要么都是字母,要么都是数字!"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def map_mins map, mins
|
154
|
+
case mins[0]
|
155
|
+
when 'x'
|
156
|
+
mins[1].each do |min|
|
157
|
+
map[:x].delete min
|
158
|
+
end
|
159
|
+
when 'y'
|
160
|
+
mins[1].each do |min|
|
161
|
+
map[:y].delete min
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def map_min map, min
|
167
|
+
if is_number? min
|
168
|
+
map[:y].delete min
|
169
|
+
else
|
170
|
+
map[:x].delete min
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def map_adds map, adds
|
175
|
+
case adds[0]
|
176
|
+
when 'x'
|
177
|
+
adds[1].each do |add|
|
178
|
+
map[:x] << add
|
179
|
+
end
|
180
|
+
when 'y'
|
181
|
+
adds[1].each do |add|
|
182
|
+
map[:y] << add
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def is_number? str
|
188
|
+
if str.to_i.to_s == str.to_s
|
189
|
+
return true
|
190
|
+
end
|
191
|
+
false
|
192
|
+
end
|
193
|
+
|
194
|
+
def map_add map, add
|
195
|
+
if is_number? add
|
196
|
+
map[:y] << add
|
197
|
+
else
|
198
|
+
map[:x] << add
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
# 解析范围配置
|
203
|
+
def parse_address address
|
204
|
+
hash = {}
|
205
|
+
if address
|
206
|
+
address.upcase!
|
207
|
+
else
|
208
|
+
raise "address 为 nil"
|
209
|
+
end
|
210
|
+
if address.split('+').size > 2
|
211
|
+
raise "'+'号只能有1个"
|
212
|
+
end
|
213
|
+
if address.split('-').size > 2
|
214
|
+
raise "'-'号只能有1个"
|
215
|
+
end
|
216
|
+
if address.include?('+')
|
217
|
+
a = address.split('+')[0]
|
218
|
+
b = address.split('+')[1]
|
219
|
+
if a.include?('-')
|
220
|
+
hash.store :scope, a.split('-')[0]
|
221
|
+
hash.store :min_str, a.split('-')[1]
|
222
|
+
hash.store :add_str, b
|
223
|
+
else
|
224
|
+
hash.store :scope, a
|
225
|
+
if b.include?('-')
|
226
|
+
hash.store :min_str, b.split('-')[1]
|
227
|
+
hash.store :add_str, b.split('-')[0]
|
228
|
+
else
|
229
|
+
hash.store :add_str, b
|
230
|
+
end
|
231
|
+
end
|
232
|
+
else
|
233
|
+
if address.include?('-')
|
234
|
+
hash.store :scope, address.split('-')[0]
|
235
|
+
hash.store :min_str, address.split('-')[1]
|
236
|
+
else
|
237
|
+
hash.store :scope, address
|
238
|
+
end
|
239
|
+
end
|
240
|
+
hash
|
241
|
+
end
|
242
|
+
|
243
|
+
def self.open excel, name
|
244
|
+
self.new excel, name, 'open'
|
245
|
+
end
|
246
|
+
|
247
|
+
def self.create excel, name
|
248
|
+
self.new excel, name, 'new'
|
249
|
+
end
|
250
|
+
|
13
251
|
def save
|
14
|
-
l =
|
252
|
+
l = parse_location @point
|
15
253
|
raise '保存sheet必须要有内容,请 set_rows' if !@rows
|
16
254
|
raise '保存sheet必须要有name,请 set_rows' if !@name
|
17
255
|
@rows.unshift @headings if @headings
|
@@ -24,6 +262,13 @@ class Ld::Sheet
|
|
24
262
|
self
|
25
263
|
end
|
26
264
|
|
265
|
+
# 解析一个 content_url
|
266
|
+
def parse_location location_str
|
267
|
+
raise "无法解析excel坐标,坐标需要是String,不能是#{location_str.class.to_s}" if location_str and location_str.class != String
|
268
|
+
location_str.upcase!
|
269
|
+
return {:x => ABSCISSA[location_str.scan(/[A-Z]+/).join].to_i, :y => (location_str.scan(/[0-9]+/).join.to_i - 1)}
|
270
|
+
end
|
271
|
+
|
27
272
|
def set_rows rows
|
28
273
|
raise '必须是一个数组且是一个二维数组' if rows.class != Array && rows.first.class != Array
|
29
274
|
@rows = rows
|
@@ -47,7 +292,7 @@ class Ld::Sheet
|
|
47
292
|
def write_unit_by_xy x, y, unit
|
48
293
|
if unit.class == Array
|
49
294
|
unit = unit.to_s
|
50
|
-
puts '
|
295
|
+
puts '提示: 有一个单元格的内容是Array, 它被当成字符串写入'
|
51
296
|
end
|
52
297
|
@sheet.row(x)[y] = unit
|
53
298
|
end
|
@@ -97,6 +342,7 @@ class Ld::Sheet
|
|
97
342
|
set_font_size hash[:font_size]
|
98
343
|
set_font hash[:font]
|
99
344
|
end
|
345
|
+
|
100
346
|
end
|
101
347
|
|
102
348
|
=begin
|
data/lib/ld/project/project.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Ld::Project
|
2
2
|
|
3
|
-
attr_accessor :root, :tables, :models, :controllers, :views, :routes
|
3
|
+
attr_accessor :root, :tables, :models, :controllers, :views, :routes, :table_hash
|
4
4
|
|
5
5
|
def initialize table_hash = {}, project_root_path = Rails.root.to_s
|
6
6
|
@root = Ld::File.new project_root_path
|
@@ -97,7 +97,7 @@ class Ld::Project
|
|
97
97
|
def to_xls path = "#{@root.path}/project.xls"
|
98
98
|
Ld::Excel.create path do |excel|
|
99
99
|
excel.write_sheet 'routes' do |sheet|
|
100
|
-
sheet.set_format({color: :
|
100
|
+
sheet.set_format({color: :red, font_size: 14, font: '微软雅黑'})
|
101
101
|
sheet.set_headings @routes.headings
|
102
102
|
sheet.set_rows @routes.rows
|
103
103
|
end
|
data/lib/ld/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ld
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Liu Dong
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: terminal-table
|