ld 0.1.2 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +31 -4
- data/ld.gemspec +4 -2
- data/lib/ld/excel.rb +34 -213
- data/lib/ld/file.rb +26 -3
- data/lib/ld/project.rb +59 -0
- data/lib/ld/sheet.rb +140 -0
- data/lib/ld/table.rb +25 -0
- data/lib/ld/tree.rb +1 -1
- data/lib/ld/version.rb +1 -1
- data/lib/ld.rb +3 -22
- metadata +35 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fca7404f3d9c8f44c54e0dafc99a7ac32d64d9c
|
4
|
+
data.tar.gz: c8df603095e2cac129223acca4e171d42e0b179c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f742f74988299f3de9cb03df2120f3ea7239156e3e25fa9ab45b49d876eeb7b3648aaffeb0327530ab126c1a8f2987ca6b2ff89ba5c24e5de5353973664e0090
|
7
|
+
data.tar.gz: 933d4982483354e85039712b7c7437758208da0ed54f29cc041269e1fa91fbaed7a88aec6a0c6364f8e03e54f668cdcbd65f67154e636e28f973a270e56ddb24
|
data/README.md
CHANGED
@@ -1,8 +1,19 @@
|
|
1
1
|
# Ld
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
提供开发基础功能,旨在提高日常工作的开发效率
|
4
|
+
主要有以下类:
|
5
|
+
```ruby
|
6
|
+
module Ld
|
7
|
+
class excel
|
8
|
+
end
|
9
|
+
class file
|
10
|
+
end
|
11
|
+
class table
|
12
|
+
end
|
13
|
+
class project
|
14
|
+
end
|
15
|
+
end
|
16
|
+
```
|
6
17
|
|
7
18
|
## Installation
|
8
19
|
|
@@ -22,7 +33,22 @@ Or install it yourself as:
|
|
22
33
|
|
23
34
|
## Usage
|
24
35
|
|
25
|
-
|
36
|
+
```ruby
|
37
|
+
Ld::Table.p User.all, 'id ,name , created_at'
|
38
|
+
Ld::Excel.open('/Users/liudong/Desktop/abss.xls').read('sh1?a1:c5')
|
39
|
+
Ld::Excel.create '/Users/liudong/Desktop/abss.xls' do |excel|
|
40
|
+
['sh1','sh2','发有3'].each do |sheet_name|
|
41
|
+
excel.write_sheet sheet_name do |sheet|
|
42
|
+
sheet.set_format({color: :red, font_size: 22, font: '宋体'})
|
43
|
+
sheet.set_headings ['a','b']
|
44
|
+
sheet.set_point 'c5'
|
45
|
+
(5..22).to_a.each do |i|
|
46
|
+
sheet.add_row i.times.map{|j| '村腰里 是'}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
26
52
|
|
27
53
|
## Development
|
28
54
|
|
@@ -39,3 +65,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
39
65
|
|
40
66
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
67
|
|
68
|
+
##
|
data/ld.gemspec
CHANGED
@@ -9,11 +9,13 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Liu Dong"]
|
10
10
|
spec.email = ["chuangye201012@163.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.description = %q{
|
12
|
+
spec.summary = %q{我的工具箱}
|
13
|
+
spec.description = %q{我的工具箱}
|
14
14
|
spec.homepage = "https://github.com/18810625123/ld"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
|
+
spec.add_dependency 'terminal-table', '~> 1.8'
|
18
|
+
spec.add_dependency 'spreadsheet'
|
17
19
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
20
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
21
|
if spec.respond_to?(:metadata)
|
data/lib/ld/excel.rb
CHANGED
@@ -60,16 +60,9 @@ class Ld::Excel
|
|
60
60
|
self
|
61
61
|
end
|
62
62
|
|
63
|
-
# 创建新页(先查有没有该页)
|
64
|
-
def new_sheet(sheet_name)
|
65
|
-
@sheet = @excel.create_worksheet(:name => sheet_name)
|
66
|
-
# puts "创建了一页 #{sheet_name}"
|
67
|
-
self
|
68
|
-
end
|
69
|
-
|
70
63
|
# 读一个单元格
|
71
64
|
def read_location(location,parse = true)
|
72
|
-
l = parse_location(location)
|
65
|
+
l = Ld::Excel.parse_location(location)
|
73
66
|
unit = read_unit_by_xy(l[:r],l[:c],parse)
|
74
67
|
# puts ""
|
75
68
|
end
|
@@ -83,7 +76,7 @@ class Ld::Excel
|
|
83
76
|
|
84
77
|
# 刷新excel中的sheet
|
85
78
|
def open_new
|
86
|
-
excel_new = Excel.open @path
|
79
|
+
excel_new = Ld::Excel.open @path
|
87
80
|
end
|
88
81
|
|
89
82
|
# 读很多个location链,返回二维数组
|
@@ -101,18 +94,14 @@ class Ld::Excel
|
|
101
94
|
unit_list = []
|
102
95
|
open_sheet locations_config.split('?')[0]
|
103
96
|
locations_config.split('?')[1].split('.').each do |location|
|
104
|
-
l = parse_location(location)
|
97
|
+
l = Ld::Excel.parse_location(location)
|
105
98
|
unit = read_unit_by_xy(l[:r],l[:c],parse)
|
106
99
|
unit_list << unit
|
107
100
|
end
|
108
101
|
unit_list
|
109
102
|
end
|
110
103
|
|
111
|
-
|
112
|
-
def write_unit_by_xy x, y, unit
|
113
|
-
unit = unit.to_s if unit.class == Array
|
114
|
-
@sheet.row(x)[y] = unit
|
115
|
-
end
|
104
|
+
|
116
105
|
|
117
106
|
# 通过x,y坐标获取unit内容
|
118
107
|
def read_unit_by_xy x, y, parse
|
@@ -127,14 +116,14 @@ class Ld::Excel
|
|
127
116
|
end
|
128
117
|
|
129
118
|
def flush
|
130
|
-
Excel.new self.path
|
119
|
+
Ld::Excel.new self.path
|
131
120
|
end
|
132
121
|
|
133
122
|
def parse_del_to_hash address, scope
|
134
123
|
arr = address.split '-'
|
135
124
|
arr = arr[1..arr.size-1]
|
136
125
|
start_row_num = scope.scan(/\d/).join[0..1]# 首行行号
|
137
|
-
location = parse_location(scope.split(':')[0])
|
126
|
+
location = Ld::Excel.parse_location(scope.split(':')[0])
|
138
127
|
hash = {}
|
139
128
|
del_rows = []
|
140
129
|
address.each do |del_row_num|# 去除行行号
|
@@ -145,7 +134,7 @@ class Ld::Excel
|
|
145
134
|
end
|
146
135
|
|
147
136
|
# 解析一个excel location
|
148
|
-
def parse_location location
|
137
|
+
def self.parse_location location
|
149
138
|
if location and location.class == String
|
150
139
|
location.upcase!
|
151
140
|
{
|
@@ -335,7 +324,7 @@ class Ld::Excel
|
|
335
324
|
|
336
325
|
# 先打开一个sheet页,再读scope范围数据
|
337
326
|
# params?b13:m27-g.j.k.(14:18)
|
338
|
-
def
|
327
|
+
def read full_scope, simple = true, filter_nil = false
|
339
328
|
if full_scope.include?('?')
|
340
329
|
sheet_name = full_scope.split('?')[0]
|
341
330
|
if sheet_name
|
@@ -409,215 +398,47 @@ class Ld::Excel
|
|
409
398
|
arr
|
410
399
|
end
|
411
400
|
|
412
|
-
# 将二维数组写到表中
|
413
|
-
def write_arrs_to_point(arrs,point = "a1")
|
414
|
-
l = parse_location(point)
|
415
|
-
arrs.each_with_index do |arr,r|
|
416
|
-
arr.each_with_index do |data,c|
|
417
|
-
write_unit_by_xy(r+l[:x],c+l[:y],data)
|
418
|
-
end
|
419
|
-
end
|
420
|
-
self
|
421
|
-
end
|
422
|
-
|
423
|
-
# 将一维数组写到表中,可写成列,也可以写成行
|
424
|
-
def write_arr_to_point(arr, rank = '|', point = "a1")
|
425
|
-
l = parse_location(point)
|
426
|
-
if rank == '|' or rank == 'col'
|
427
|
-
arr.each_with_index do |data,r|
|
428
|
-
# 坚写,行动列不动
|
429
|
-
write_unit_by_xy(l[:r]+r,l[:c],data)
|
430
|
-
end
|
431
|
-
elsif rank == '-' or rank == 'row'
|
432
|
-
arr.each_with_index do |data,c|
|
433
|
-
# 横写,列动行不动
|
434
|
-
write_unit_by_xy(l[:r],l[:c]+c,data)
|
435
|
-
end
|
436
|
-
else
|
437
|
-
raise "横写rank | 竖写rank - 无法识别#{rank}"
|
438
|
-
end
|
439
|
-
self
|
440
|
-
end
|
441
|
-
|
442
401
|
# 保存文件
|
443
|
-
def save
|
444
|
-
|
445
|
-
if path==nil
|
446
|
-
if File.exist? @path
|
447
|
-
@excel.write @path
|
448
|
-
puts "保存覆盖了一个同名文件 #{@path}"
|
449
|
-
else
|
450
|
-
@excel.write @path
|
451
|
-
puts "保存到: #{@path}"
|
452
|
-
end
|
453
|
-
else
|
402
|
+
def save path
|
403
|
+
if File.exist? path
|
454
404
|
@excel.write path
|
455
|
-
|
456
|
-
puts "保存到: #{path}"
|
457
|
-
else
|
458
|
-
raise "保存失败!"
|
459
|
-
end
|
460
|
-
end
|
461
|
-
self
|
462
|
-
end
|
463
|
-
|
464
|
-
def save_to_source(basename)
|
465
|
-
if basename.match(/.xls$/)!=nil
|
466
|
-
save(@@base_path + "source/" + basename)
|
467
|
-
else
|
468
|
-
raise "要以.xls结尾"
|
469
|
-
end
|
470
|
-
self
|
471
|
-
end
|
472
|
-
|
473
|
-
def save_to_complete(basename)
|
474
|
-
if basename.match(/.xls$/)!=nil
|
475
|
-
save(@@base_path + "complete/" + basename)
|
405
|
+
puts "保存覆盖了一个同名文件 #{@path}"
|
476
406
|
else
|
477
|
-
|
407
|
+
@excel.write @path
|
408
|
+
puts "保存到: #{@path}"
|
478
409
|
end
|
479
410
|
self
|
480
411
|
end
|
481
412
|
|
482
|
-
def
|
483
|
-
|
484
|
-
save(path_name)
|
485
|
-
else
|
486
|
-
raise "要以.xls结尾"
|
487
|
-
end
|
488
|
-
self
|
413
|
+
def new_sheet name
|
414
|
+
Ld::Sheet.new @excel, name
|
489
415
|
end
|
490
416
|
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
@sheets.each_with_index do |sheet,i|
|
496
|
-
puts "第#{i}个sheet,name:#{sheet.name}"
|
497
|
-
end
|
498
|
-
puts "Excel文件size:#{File.size @path},sheet数:#{@sheets.size},文件path:#{@path}"
|
499
|
-
else
|
500
|
-
puts "不存在的文件,#{@path}"
|
501
|
-
end
|
502
|
-
self
|
417
|
+
def write_sheet sheet_name, &block
|
418
|
+
@sheet = new_sheet sheet_name
|
419
|
+
block.call @sheet
|
420
|
+
@sheet.save
|
503
421
|
end
|
504
422
|
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
cols.each do |col|
|
510
|
-
puts col
|
511
|
-
end
|
423
|
+
def self.create path, &block
|
424
|
+
excel = Ld::Excel.new
|
425
|
+
block.call excel
|
426
|
+
excel.save path
|
512
427
|
end
|
513
428
|
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
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
|
525
440
|
end
|
526
|
-
|
527
|
-
else
|
528
|
-
raise "@path==nil,无法reload"
|
529
441
|
end
|
530
|
-
self
|
531
442
|
end
|
532
|
-
|
533
|
-
def set_default_format(font_color = :black)
|
534
|
-
if @sheet!=nil
|
535
|
-
@format = Spreadsheet::Format.new(
|
536
|
-
:color => :blue,
|
537
|
-
:weight => :bold,
|
538
|
-
:size => 11
|
539
|
-
)
|
540
|
-
@format.font.color = font_color
|
541
|
-
@format.font.name = "微软雅黑"
|
542
|
-
@format.font.size = 11
|
543
|
-
@sheet.default_format = @format
|
544
|
-
# puts "设置默认格式成功 #{@sheet.name}"
|
545
|
-
else
|
546
|
-
raise "@sheet==nil,无法获取默认格式,无法设置默认格式"
|
547
|
-
end
|
548
|
-
@format
|
549
|
-
end
|
550
|
-
|
551
|
-
def write_sheet(sheet)
|
552
|
-
new_sheet sheet[:name]
|
553
|
-
set_default_format(sheet[:color].nil? ? 'black' : sheet[:color])
|
554
|
-
write_arrs_to_point(sheet[:arrs], sheet[:point].nil? ? "a1" : sheet[:point])
|
555
|
-
sheet
|
556
|
-
end
|
557
|
-
|
558
|
-
def self.write_excel(arrs_list,excel_name)
|
559
|
-
new_excel = Excel.new
|
560
|
-
arrs_list.each_with_index do |arrs,i|
|
561
|
-
new_excel.write_sheet arrs,'sheet'+(i+1).to_s,:red
|
562
|
-
end
|
563
|
-
new_excel.save_to_complete excel_name
|
564
|
-
end
|
565
|
-
|
566
|
-
def self.create(path, sheets)
|
567
|
-
e = Excel.new
|
568
|
-
sheets.each do |sheet|
|
569
|
-
e.write_sheet sheet
|
570
|
-
end
|
571
|
-
e.save path
|
572
|
-
end
|
573
|
-
|
574
|
-
def self.write_excel2(arrs,excel_name)
|
575
|
-
new_excel = Excel.new
|
576
|
-
new_excel.write_sheet arrs,'sheet1',:red
|
577
|
-
new_excel.save_to excel_name
|
578
|
-
rescue
|
579
|
-
puts $!
|
580
|
-
puts $@
|
581
|
-
end
|
582
|
-
|
583
443
|
end
|
584
444
|
|
585
|
-
=begin
|
586
|
-
|
587
|
-
# <Spreadsheet::Format:0x007fe8297dba40
|
588
|
-
@bottom=:none,
|
589
|
-
@bottom_color=:builtin_black,
|
590
|
-
@cross_down=false,
|
591
|
-
@cross_up=false,
|
592
|
-
@diagonal_color=:builtin_black,
|
593
|
-
@font=
|
594
|
-
# <Spreadsheet::Font:0x007fe8285948a0
|
595
|
-
@color=:black,
|
596
|
-
@encoding=:iso_latin1,
|
597
|
-
@escapement=:normal,
|
598
|
-
@family=:none,
|
599
|
-
@italic=false,
|
600
|
-
@name="仿宋",
|
601
|
-
@outline=false,
|
602
|
-
@previous_fast_key=nil,
|
603
|
-
@shadow=false,
|
604
|
-
@size=11,
|
605
|
-
@strikeout=false,
|
606
|
-
@underline=:none,
|
607
|
-
@weight=400>,
|
608
|
-
@horizontal_align=:center,
|
609
|
-
@indent_level=0,
|
610
|
-
@left=:none,
|
611
|
-
@left_color=:builtin_black,
|
612
|
-
@number_format="GENERAL",
|
613
|
-
@pattern=1,
|
614
|
-
@pattern_bg_color=:border,
|
615
|
-
@pattern_fg_color=:red,
|
616
|
-
@regexes=
|
617
|
-
{:date=>/[YMD]/,
|
618
|
-
:date_or_time=>/[hmsYMD]/,
|
619
|
-
:datetime=>/([YMD].*[HS])|([HS].*[YMD])/,
|
620
|
-
:time=>/[hms]/,
|
621
|
-
:number=>/([# ]|0+)/,
|
622
|
-
:locale=>/(?-mix:\A\[\$\-\d+\])/},
|
623
|
-
=end
|
data/lib/ld/file.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
class Ld::File
|
2
2
|
|
3
|
-
attr_accessor :path, :name, :type
|
3
|
+
attr_accessor :path, :base_name, :name, :type
|
4
4
|
|
5
5
|
def initialize path
|
6
6
|
@path = path
|
7
7
|
@name = File.basename @path
|
8
|
+
@base_name = name.split('.')[0]
|
8
9
|
@type = File.directory?(@path) ? 1 : 0
|
9
10
|
end
|
10
11
|
|
@@ -12,10 +13,12 @@ class Ld::File
|
|
12
13
|
father.children
|
13
14
|
end
|
14
15
|
|
15
|
-
def children
|
16
|
+
def children(remove = nil)
|
16
17
|
arr = []
|
17
18
|
Dir.foreach(@path)do |p|
|
18
|
-
|
19
|
+
removes = ['.','..','.DS_Store']
|
20
|
+
removes << remove if remove
|
21
|
+
if !removes.include?(p)
|
19
22
|
arr << Ld::File.new("#{@path}/#{p}")
|
20
23
|
end
|
21
24
|
end
|
@@ -53,6 +56,12 @@ class Ld::File
|
|
53
56
|
arr
|
54
57
|
end
|
55
58
|
|
59
|
+
def search regexp
|
60
|
+
arr = []
|
61
|
+
iter_search regexp, arr
|
62
|
+
arr
|
63
|
+
end
|
64
|
+
|
56
65
|
def iter_search regexp, arr
|
57
66
|
children.each do |f|
|
58
67
|
if f.type == 1
|
@@ -75,6 +84,20 @@ class Ld::File
|
|
75
84
|
self
|
76
85
|
end
|
77
86
|
|
87
|
+
def size
|
88
|
+
File.size path
|
89
|
+
end
|
90
|
+
|
91
|
+
def lines
|
92
|
+
arr = []
|
93
|
+
File.new(path).each_line{|l| arr << l }
|
94
|
+
arr
|
95
|
+
end
|
96
|
+
|
97
|
+
def exist?
|
98
|
+
File.exist? path
|
99
|
+
end
|
100
|
+
|
78
101
|
def method_missing name
|
79
102
|
find name
|
80
103
|
end
|
data/lib/ld/project.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
class Ld::Project
|
2
|
+
|
3
|
+
attr_accessor :root, :app, :name, :path, :models, :views_dir, :views, :controllers, :routes
|
4
|
+
|
5
|
+
def initialize path
|
6
|
+
@root = Ld::File.new(path)
|
7
|
+
@app = @root.app
|
8
|
+
@models = @app.models.search(/.rb$/)
|
9
|
+
@views_dir = @app.views.children
|
10
|
+
@views = @app.views.search(/.html/)
|
11
|
+
@controllers = @app.controllers.search(/_controller.rb$/)
|
12
|
+
@routes = @root.config.find('routes.rb')
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.p model = :all
|
16
|
+
@@p ||= Ld::Project.new(Rails.root.to_s)
|
17
|
+
|
18
|
+
t = Terminal::Table.new
|
19
|
+
case model.to_s
|
20
|
+
when 'all'
|
21
|
+
t.title = "project:#{@@p.root.name}"
|
22
|
+
t.headings = ['models', 'views', 'controllers', 'routes']
|
23
|
+
t.add_row [@@p.models.size, @@p.views.size, @@p.controllers.size, @@p.routes.lines.size]
|
24
|
+
when 'models'
|
25
|
+
t.title = 'models'
|
26
|
+
t.headings = ['name', 'action-size', 'line-size', 'routes']
|
27
|
+
@@p.models.map{|f| [f.name.split('.')[0], f.lines.map{|l| l if l.match(/def /)}.compact.size, f.lines.size, nil] }
|
28
|
+
.sort{|a,b| b[1]-a[1]}.each{|i| t.add_row i}
|
29
|
+
when 'controllers'
|
30
|
+
t.title = 'controllers'
|
31
|
+
t.headings = ['name', 'action-size', 'line-size']
|
32
|
+
@@p.controllers.map{|f| [f.name.split('.')[0], f.lines.map{|l| l if l.match(/def /)}.compact.size, f.lines.size] }
|
33
|
+
.sort{|a,b| b[1]-a[1]}.each{|i| t.add_row i}
|
34
|
+
when 'views'
|
35
|
+
t.title = 'views'
|
36
|
+
t.headings = ['name', 'file-size', 'html']
|
37
|
+
@@p.app.views.children('shared')
|
38
|
+
.map{|f| htmls = f.search(/.html/);[f.name, htmls.size, htmls.map{|f2| f2.name.split('.')[0]}.join(' ')]}
|
39
|
+
.sort{|a,b| b[1]-a[1]}
|
40
|
+
.each{|arr| t.add_row arr}
|
41
|
+
when 'routes'
|
42
|
+
file = Ld::File.new @@p.root.path + '/routes.txt'
|
43
|
+
if !file.exist?
|
44
|
+
system "rake routes > #{@@p.root.path + '/routes.txt'}"
|
45
|
+
end
|
46
|
+
arrs = file.lines.map{|l| lines = l.split(' '); lines.size == 3 ? lines.unshift(nil) : lines}
|
47
|
+
arrs.delete_at 0
|
48
|
+
t.title = 'routes'
|
49
|
+
t.headings = ['controller', 'action', 'type']
|
50
|
+
arrs.map{|arr| controller,action = arr[3].split('#'); [controller, action, arr[1]]}
|
51
|
+
.each{|arr| t.add_row arr}
|
52
|
+
else
|
53
|
+
puts '(models/controllers/views)'
|
54
|
+
return
|
55
|
+
end
|
56
|
+
puts t
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/lib/ld/sheet.rb
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
class Ld::Sheet
|
2
|
+
|
3
|
+
def initialize excel, name
|
4
|
+
@excel = excel
|
5
|
+
@name = name
|
6
|
+
@point = 'a1'
|
7
|
+
@rows = []
|
8
|
+
@headings = nil
|
9
|
+
@sheet = excel.create_worksheet :name => name
|
10
|
+
@format = @sheet.default_format
|
11
|
+
end
|
12
|
+
|
13
|
+
def save
|
14
|
+
l = Ld::Excel.parse_location @point
|
15
|
+
raise '保存sheet必须要有内容,请 set_rows' if !@rows
|
16
|
+
raise '保存sheet必须要有name,请 set_rows' if !@name
|
17
|
+
@rows.unshift @headings if @headings
|
18
|
+
@sheet.default_format = @format
|
19
|
+
@rows.each_with_index do |row,r|
|
20
|
+
row.each_with_index do |data,c|
|
21
|
+
write_unit_by_xy(r+l[:y],c+l[:x],data)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def set_rows rows
|
28
|
+
raise '必须是一个数组且是一个二维数组' if rows.class != Array && rows.first.class != Array
|
29
|
+
@rows = rows
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_headings headings
|
33
|
+
if headings
|
34
|
+
raise 'headings 必须是一个数组' if headings.class != Array
|
35
|
+
@headings = headings
|
36
|
+
else
|
37
|
+
@headings = nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_row row
|
42
|
+
raise 'add_row 传入的必须是一个数组' if row.class != Array
|
43
|
+
@rows << row
|
44
|
+
end
|
45
|
+
|
46
|
+
# 通过xy坐标往unit写内容
|
47
|
+
def write_unit_by_xy x, y, unit
|
48
|
+
if unit.class == Array
|
49
|
+
unit = unit.to_s
|
50
|
+
puts '有一个单元格是数组格式,已经转化成字符串'
|
51
|
+
end
|
52
|
+
@sheet.row(x)[y] = unit
|
53
|
+
end
|
54
|
+
|
55
|
+
# 将一维数组写到表中,可写成列,也可以写成行
|
56
|
+
def write_arr_to_point(arr, rank = '|', point = "a1")
|
57
|
+
l = Ld::Excel.parse_location(point)
|
58
|
+
if rank == '|' or rank == 'col'
|
59
|
+
arr.each_with_index do |data,r|
|
60
|
+
# 坚写,行动列不动
|
61
|
+
write_unit_by_xy(l[:r]+r,l[:c],data)
|
62
|
+
end
|
63
|
+
elsif rank == '-' or rank == 'row'
|
64
|
+
arr.each_with_index do |data,c|
|
65
|
+
# 横写,列动行不动
|
66
|
+
write_unit_by_xy(l[:r],l[:c]+c,data)
|
67
|
+
end
|
68
|
+
else
|
69
|
+
raise "横写rank | 竖写rank - 无法识别#{rank}"
|
70
|
+
end
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
def set_color color
|
75
|
+
@format.font.color = color
|
76
|
+
end
|
77
|
+
|
78
|
+
def set_font_size size
|
79
|
+
raise 'size 必须是一个整数' if size.class != Fixnum
|
80
|
+
@format.font.size = size
|
81
|
+
end
|
82
|
+
|
83
|
+
def set_font font
|
84
|
+
@format.font.name = font
|
85
|
+
end
|
86
|
+
|
87
|
+
def set_weight weight
|
88
|
+
@format
|
89
|
+
end
|
90
|
+
|
91
|
+
def set_point point
|
92
|
+
@point = point
|
93
|
+
end
|
94
|
+
|
95
|
+
def set_format hash
|
96
|
+
set_color hash[:color]
|
97
|
+
set_font_size hash[:font_size]
|
98
|
+
set_font hash[:font]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
=begin
|
103
|
+
|
104
|
+
# <Spreadsheet::Format:0x007fe8297dba40
|
105
|
+
@bottom=:none,
|
106
|
+
@bottom_color=:builtin_black,
|
107
|
+
@cross_down=false,
|
108
|
+
@cross_up=false,
|
109
|
+
@diagonal_color=:builtin_black,
|
110
|
+
@font=
|
111
|
+
# <Spreadsheet::Font:0x007fe8285948a0
|
112
|
+
@color=:black,
|
113
|
+
@encoding=:iso_latin1,
|
114
|
+
@escapement=:normal,
|
115
|
+
@family=:none,
|
116
|
+
@italic=false,
|
117
|
+
@name="仿宋",
|
118
|
+
@outline=false,
|
119
|
+
@previous_fast_key=nil,
|
120
|
+
@shadow=false,
|
121
|
+
@size=11,
|
122
|
+
@strikeout=false,
|
123
|
+
@underline=:none,
|
124
|
+
@weight=400>,
|
125
|
+
@horizontal_align=:center,
|
126
|
+
@indent_level=0,
|
127
|
+
@left=:none,
|
128
|
+
@left_color=:builtin_black,
|
129
|
+
@number_format="GENERAL",
|
130
|
+
@pattern=1,
|
131
|
+
@pattern_bg_color=:border,
|
132
|
+
@pattern_fg_color=:red,
|
133
|
+
@regexes=
|
134
|
+
{:date=>/[YMD]/,
|
135
|
+
:date_or_time=>/[hmsYMD]/,
|
136
|
+
:datetime=>/([YMD].*[HS])|([HS].*[YMD])/,
|
137
|
+
:time=>/[hms]/,
|
138
|
+
:number=>/([# ]|0+)/,
|
139
|
+
:locale=>/(?-mix:\A\[\$\-\d+\])/},
|
140
|
+
=end
|
data/lib/ld/table.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'terminal-table'
|
2
|
+
|
3
|
+
class Ld::Table
|
4
|
+
|
5
|
+
def initialize models
|
6
|
+
@models = models
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.p models,fields
|
10
|
+
t = Terminal::Table.new
|
11
|
+
t.title = models.first.class.to_s
|
12
|
+
fields = (fields.class == Array ? fields : fields.split(',')).map{|f| f.rstrip.lstrip}
|
13
|
+
t.headings = fields
|
14
|
+
models.map { |model|
|
15
|
+
fields.map { |field|
|
16
|
+
value = model.send field
|
17
|
+
value = value.strftime("%Y/%m/%d %H:%M:%S") if [Date, Time, DateTime, ActiveSupport::TimeWithZone].include? value.class
|
18
|
+
value
|
19
|
+
}
|
20
|
+
}#.sort{|a,b| a[2] <=> b[2]}
|
21
|
+
.each{|row| t.add_row row}
|
22
|
+
puts t
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/lib/ld/tree.rb
CHANGED
data/lib/ld/version.rb
CHANGED
data/lib/ld.rb
CHANGED
@@ -1,29 +1,10 @@
|
|
1
1
|
require "ld/version"
|
2
2
|
|
3
3
|
module Ld
|
4
|
-
class Excel
|
5
|
-
end
|
6
|
-
|
7
|
-
class File
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
class Dir
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
class Node
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
class Nodes
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
class Tree
|
24
|
-
|
25
|
-
end
|
26
4
|
|
27
5
|
end
|
6
|
+
|
28
7
|
require "ld/excel"
|
8
|
+
require "ld/sheet"
|
29
9
|
require "ld/file"
|
10
|
+
require "ld/project"
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ld
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
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-05-
|
11
|
+
date: 2017-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: terminal-table
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: spreadsheet
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
- !ruby/object:Gem::Dependency
|
14
42
|
name: bundler
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,7 +66,7 @@ dependencies:
|
|
38
66
|
- - "~>"
|
39
67
|
- !ruby/object:Gem::Version
|
40
68
|
version: '10.0'
|
41
|
-
description:
|
69
|
+
description: 我的工具箱
|
42
70
|
email:
|
43
71
|
- chuangye201012@163.com
|
44
72
|
executables: []
|
@@ -68,6 +96,9 @@ files:
|
|
68
96
|
- lib/ld/file.rb
|
69
97
|
- lib/ld/node.rb
|
70
98
|
- lib/ld/nodes.rb
|
99
|
+
- lib/ld/project.rb
|
100
|
+
- lib/ld/sheet.rb
|
101
|
+
- lib/ld/table.rb
|
71
102
|
- lib/ld/tree.rb
|
72
103
|
- lib/ld/version.rb
|
73
104
|
homepage: https://github.com/18810625123/ld
|
@@ -94,5 +125,5 @@ rubyforge_project:
|
|
94
125
|
rubygems_version: 2.6.7
|
95
126
|
signing_key:
|
96
127
|
specification_version: 4
|
97
|
-
summary:
|
128
|
+
summary: 我的工具箱
|
98
129
|
test_files: []
|