mymatrix 0.1.0 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ = mymatrix
2
+
3
+ == DESCRIPTION:
4
+
5
+ mymatrix is a handling library for MS Excel and csv/tsv text.
6
+
7
+ == FEATURES/PROBLEMS:
8
+
9
+ Support filetypes: .xls, .tsv, .csv
10
+
11
+
12
+ == SYNOPSIS:
13
+
14
+
15
+
16
+ == REQUIREMENTS:
17
+
18
+ Support ruby versions are:
19
+ ruby 1.8.7,
20
+ ruby 1.9.2 or higher
21
+
22
+ == INSTALL:
23
+ gem install mymatrix
24
+ (It's hosted on gemcutter.org)
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009-2013 zucay
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49
+
50
+ == HOW TO USE:
51
+ load and wright file
52
+ --------------------
53
+
54
+ ```ruby
55
+ require 'mymatrix'
56
+ mx = MyMatrix.new('path/to/xlsfile.xls')
57
+ mx.each_with_index do |row, i|
58
+ the_column = 'sample_column_name'
59
+
60
+ # print value of the cell.
61
+ p mx.val(row, the_column)
62
+
63
+ # write value of the cell
64
+ mx.setValue(row, the_column, i) # write "i" value to the cell
65
+
66
+ # text_output(default is csv)
67
+ mx.to_t('path/to/text.txt')
68
+ # csv_output
69
+ mx.to_csv('path/to/csv.csv')
70
+ end
71
+ ```
@@ -1,31 +1,39 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  class FileIO
3
- # ファイルオープン時、パス文字列のエンコードを変換してシステムに返却するためのメソッド
3
+ # ファイル書き込み時、パス文字列のエンコードを変換してシステムに返却するためのメソッド
4
4
  def self.encodePath(path)
5
5
  case self.filesystem
6
- when 'u'
6
+ when 'u' # Linux Utf-8
7
7
  #utf8=>utf8なので何もしない
8
- #path = MyMatrix.toutf8(path)
9
- #path.encode('UTF-8')
10
8
  path
11
- when 's'
9
+ when 's' # Windows Shift-JIS(CP932)
12
10
  path = MyMatrix.tosjis(path)
13
- #path.encode('Windows-31J')
14
- when 'w'
15
- path = MyMatrix.tosjis(path)
16
- #path.encode('Windows-31J')
17
- when 'm'
11
+ when 'm' # Mac utf8(UTF8-Mac)
18
12
  path = MyMatrix.toUtf8Mac(path)
13
+ else
14
+ path
19
15
  end
20
16
  end
17
+ # ファイル読み込み時、パス文字列のエンコードをUTF8に変換して内部保持する為のメソッド。
18
+ # Windowsからファイルを受け取る場合、ShiftJisで文字列が渡ってくるため。
19
+ def self.readPath(path)
20
+ case self.filesystem
21
+ when 's'
22
+ MyMatrix.toutf8(path)
23
+ else
24
+ path
25
+ end
26
+ end
27
+
28
+ # ファイルシステムを判定するメソッド。
21
29
  def self.filesystem
22
30
  #platform check
23
31
  if(RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/)
24
- filesystem = 's'
32
+ filesystem = 's' # Windows Shift-JIS(CP932)
25
33
  elsif(RUBY_PLATFORM.downcase =~ /darwin/)
26
- filesystem = 'm'
34
+ filesystem = 'm' # Mac utf8(UTF8-Mac)
27
35
  elsif(RUBY_PLATFORM.downcase =~ /linux/)
28
- filesystem = 'u'
36
+ filesystem = 'u' # Linux Utf-8
29
37
  else
30
38
  filesystem = 'u'
31
39
  end
@@ -39,8 +39,11 @@ class MyMatrix
39
39
  retry
40
40
  end
41
41
  @log.level = Logger::DEBUG
42
- @file = file
43
-
42
+ if(FileIO.filesystem == 's')
43
+ @file = MyMatrix.toutf8(file.to_s)
44
+ else
45
+ @file = file
46
+ end
44
47
 
45
48
  @mx = LoaderFactory.load(@file, opts)
46
49
 
@@ -407,7 +410,7 @@ class MyMatrix
407
410
  end
408
411
  fo.close
409
412
  end
410
- # テキスト出力する
413
+ # テキスト出力する。outFileにxlsが指定された場合は、xls出力する。
411
414
  def to_t(outFile=nil, opts={})
412
415
  if(!outFile)
413
416
  outFile = @file
@@ -420,8 +423,8 @@ class MyMatrix
420
423
  opts[:separator] ||= ','
421
424
  opts[:escape] ||= true
422
425
  when '.xls'
423
- p 'use Tab-Separated-Value text format.'
424
- outFile = outFile + '.txt'
426
+ to_xls(outFile)
427
+ return
425
428
  when '.xlsx'
426
429
  p 'use Tab-Separated-Value text format.'
427
430
  outFile = outFile + '.txt'
@@ -478,10 +481,45 @@ class MyMatrix
478
481
  end
479
482
 
480
483
  #CSV出力する。ダブルクオーテーションやカンマをエスケープする。
481
- def to_csv(outFile)
482
- to_t(outFile, {:separator=>',', :escape=>true})
484
+ def to_csv(outFile=nil, opts={ })
485
+ to_t(outFile, opts.merge({:separator=>',', :escape=>true}))
483
486
  end
484
-
487
+
488
+ #xlsにて出力する。
489
+ def to_xls(outFile=nil, opts={ })
490
+ default_sheet_name = 'Sheet1'
491
+ if(outFile =~ /.xls$/)
492
+ else
493
+ raise "output file is not xls. set .xls file"
494
+ end
495
+
496
+ infile =opts[:template]
497
+ inFile ||= self.file
498
+
499
+ begin
500
+ xl = Spreadsheet.open(inFile)
501
+ sheet_str = opts[:sheet]
502
+ sheet_str ||= default_sheet_name
503
+ sheet = xl.worksheet(sheet_str)
504
+ rescue
505
+ xl = Spreadsheet::Workbook.new
506
+ sheet = book.create_worksheet
507
+ sheet.name = default_sheet_name
508
+ end
509
+
510
+ @headers.each_with_index do |head, i|
511
+ sheet[0, i] = head
512
+ end
513
+ self.each_with_index do |row, i|
514
+ n = i+1 # line number
515
+ row.each_with_index do |cell, j|
516
+ sheet[n, j] = cell
517
+ end
518
+ end
519
+ xl.write(outFile)
520
+ end
521
+
522
+
485
523
  #ヘッダのコピーを返却する
486
524
  def getHeaders
487
525
  out = @headers.dup
@@ -748,7 +786,7 @@ class MyMatrix
748
786
  out = 0
749
787
  arr = getColumn(header)
750
788
  arr.each do |ele|
751
- if(ele =~ /#{value}/)
789
+ if(ele == value)
752
790
  out += 1
753
791
  end
754
792
  end
@@ -1031,7 +1069,7 @@ class MyMatrix
1031
1069
  end
1032
1070
  #num文字以上の項目をnum文字に丸める
1033
1071
  def cutOff(head, num)
1034
- self.each do |row|
1072
+ self.each do |row|
1035
1073
  v = self.val(row, head)
1036
1074
  if(v =~ /(.{#{num}})/)
1037
1075
  self.setValue(row, head, $1)
@@ -1049,3 +1087,8 @@ class MyMatrix
1049
1087
  opath = (FileIO.encodePath("#{dir}/#{basename}_#{postfix}#{ext}"))
1050
1088
  end
1051
1089
  end
1090
+
1091
+ if(__FILE__ == $0)
1092
+ mx = MyMatrix.new('/Users/zuka/Dropbox/lib/mymatrix/spec/std_shoshiki.xls')
1093
+ mx.to_xls('hoge.xls')
1094
+ end
@@ -1,3 +1,3 @@
1
1
  class MyMatrix
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.6"
3
3
  end
Binary file
Binary file
@@ -32,7 +32,7 @@ describe MyMatrix do
32
32
  end
33
33
  =begin
34
34
  it 'sortされること' do
35
- @mx = MyMatrix.new('spec/110619.xls')
35
+ @mx = MyMatrix.new('spnc/110619.xls')
36
36
  @mx = @mx.sortBy('要確認').reverse
37
37
  @mx.each do |row|
38
38
  #p @mx.val(row, '要確認')
@@ -136,11 +136,22 @@ describe MyMatrix do
136
136
  pending('必要になったら実装する')
137
137
  mx = makeSample()
138
138
  mx.to_xls({:template => 'spec/template.xls', :out=>'spec/out.xls', :offset_r => 3, :offset_c =>1})
139
+
139
140
  mx = MyMatrix.new('spec/out.xls')
140
141
  mx.val(mx[0], 'P-a').should == 'iti'
141
142
  mx[3][1].should == 'a'
142
143
  mx[4][1].should == '4'
143
144
  end
145
+ it 'xls形式で書き出せること' do
146
+ mx = MyMatrix.new('spec/template.xls')
147
+ mx.setValue(mx[0], 'P-b', 'foo')
148
+
149
+ mx.to_xls('spec/out.xls')
150
+
151
+ mx = MyMatrix.new('spec/out.xls')
152
+ mx.val(mx[0], 'P-a').should == 'iti'
153
+ mx.val(mx[0], 'P-b').should == 'foo'
154
+ end
144
155
  it '同じシーケンスIDに同一の値をsetできること' do
145
156
  mx = MyMatrix.new()
146
157
  mx.addHeaders(%w[シーケンス 名称 情報])
@@ -283,8 +294,11 @@ describe MyMatrix do
283
294
  Proc {
284
295
  mx.to_t('test.txt')
285
296
  }.should raise_error
286
- end
287
-
297
+ end
298
+ it 'ヘッダが半角でも対応できること' do
299
+ mx = makeHankakuSample
300
+ mx.val(mx[0], '削除フラグ').should == '4'
301
+ end
288
302
  end
289
303
  def translationCheck(testcases)
290
304
  mx = MyMatrix.new
@@ -330,6 +344,14 @@ def makeEmptySample
330
344
  out << ['', '', '']
331
345
  return out
332
346
  end
347
+ def makeHankakuSample
348
+ out = MyMatrix.new()
349
+ out.addHeaders(['削除フラグ', 'b', 'c'])
350
+ out << ['4', '5', '8']
351
+ out << ['', '', '']
352
+ out << ['', '', '']
353
+ return out
354
+ end
333
355
 
334
356
  def makecsv
335
357
  open('spec/csv.csv', 'w') do |fo|
@@ -358,3 +380,13 @@ def makecsv_norow
358
380
  mx = MyMatrix.new('spec/csv.csv')
359
381
  return mx
360
382
  end
383
+
384
+
385
+ if(__FILE__ == $0)
386
+ p ARGV[0]
387
+ mx = MyMatrix.new(ARGV[0])
388
+ p mx.getHeaders.join("\t")
389
+ mx.each do |row|
390
+ p row.join("\t")
391
+ end
392
+ end
Binary file
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mymatrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-30 00:00:00.000000000 Z
12
+ date: 2013-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -66,46 +66,92 @@ executables: []
66
66
  extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
- - Gemfile
70
- - Rakefile
71
- - lib/file_io.rb
72
- - lib/loader_csv.rb
73
- - lib/loader_factory.rb
74
- - lib/loader_txt.rb
75
- - lib/loader_xls.rb
76
- - lib/mymatrix.rb
77
- - lib/mymatrix/version.rb
78
- - mymatrix.gemspec
79
- - pkg/mymatrix-0.0.1.gem
80
- - pkg/mymatrix-0.0.2.gem
81
- - pkg/mymatrix-0.0.3.gem
82
- - pkg/mymatrix-0.0.4.gem
83
- - pkg/mymatrix-0.0.5.gem
84
- - pkg/mymatrix-0.0.8.gem
85
- - pkg/mymatrix-0.0.9.gem
86
- - spec/csv.csv
87
- - spec/csv.csv.txt
88
- - spec/csv_test.csv
89
- - spec/for_concat/con1.xls
90
- - spec/for_concat/con2.xls
91
- - spec/for_concat/con3.xls
92
- - spec/for_concat/con4.xls
93
- - spec/info_list.txt
94
- - spec/jptest.txt
95
- - spec/line4.txt
96
- - spec/line4.xls
97
- - spec/mymatrix_spec.rb
98
- - spec/offset.txt
99
- - spec/out.xls
100
- - spec/std_shoshiki.xls
101
- - spec/template.xls
102
- - spec/test.csv
103
- - spec/test.txt
104
- - spec/test_helper.rb
105
- - spec/test_mymatrix.rb
106
- - spec/words/001_sheet.doc
107
- - spec/words/002_sheet.doc
108
- - spec/words/003_sheet.doc
69
+ - !binary |-
70
+ R2VtZmlsZQ==
71
+ - !binary |-
72
+ UkVBRE1FLnJkb2M=
73
+ - !binary |-
74
+ UmFrZWZpbGU=
75
+ - !binary |-
76
+ bGliL2ZpbGVfaW8ucmI=
77
+ - !binary |-
78
+ bGliL2xvYWRlcl9jc3YucmI=
79
+ - !binary |-
80
+ bGliL2xvYWRlcl9mYWN0b3J5LnJi
81
+ - !binary |-
82
+ bGliL2xvYWRlcl90eHQucmI=
83
+ - !binary |-
84
+ bGliL2xvYWRlcl94bHMucmI=
85
+ - !binary |-
86
+ bGliL215bWF0cml4LnJi
87
+ - !binary |-
88
+ bGliL215bWF0cml4L3ZlcnNpb24ucmI=
89
+ - !binary |-
90
+ bXltYXRyaXguZ2Vtc3BlYw==
91
+ - !binary |-
92
+ cGtnL215bWF0cml4LTAuMC4xLmdlbQ==
93
+ - !binary |-
94
+ cGtnL215bWF0cml4LTAuMC4yLmdlbQ==
95
+ - !binary |-
96
+ cGtnL215bWF0cml4LTAuMC4zLmdlbQ==
97
+ - !binary |-
98
+ cGtnL215bWF0cml4LTAuMC40LmdlbQ==
99
+ - !binary |-
100
+ cGtnL215bWF0cml4LTAuMC41LmdlbQ==
101
+ - !binary |-
102
+ cGtnL215bWF0cml4LTAuMC44LmdlbQ==
103
+ - !binary |-
104
+ cGtnL215bWF0cml4LTAuMC45LmdlbQ==
105
+ - !binary |-
106
+ cGtnL215bWF0cml4LTAuMS4wLmdlbQ==
107
+ - !binary |-
108
+ cGtnL215bWF0cml4LTAuMS42LmdlbQ==
109
+ - !binary |-
110
+ c3BlYy9jc3YuY3N2
111
+ - !binary |-
112
+ c3BlYy9jc3YuY3N2LnR4dA==
113
+ - !binary |-
114
+ c3BlYy9jc3ZfdGVzdC5jc3Y=
115
+ - !binary |-
116
+ c3BlYy9mb3JfY29uY2F0L2NvbjEueGxz
117
+ - !binary |-
118
+ c3BlYy9mb3JfY29uY2F0L2NvbjIueGxz
119
+ - !binary |-
120
+ c3BlYy9mb3JfY29uY2F0L2NvbjMueGxz
121
+ - !binary |-
122
+ c3BlYy9mb3JfY29uY2F0L2NvbjQueGxz
123
+ - !binary |-
124
+ c3BlYy9pbmZvX2xpc3QudHh0
125
+ - !binary |-
126
+ c3BlYy9qcHRlc3QudHh0
127
+ - !binary |-
128
+ c3BlYy9saW5lNC50eHQ=
129
+ - !binary |-
130
+ c3BlYy9saW5lNC54bHM=
131
+ - !binary |-
132
+ c3BlYy9teW1hdHJpeF9zcGVjLnJi
133
+ - !binary |-
134
+ c3BlYy9vZmZzZXQudHh0
135
+ - !binary |-
136
+ c3BlYy9vdXQueGxz
137
+ - !binary |-
138
+ c3BlYy9zdGRfc2hvc2hpa2kueGxz
139
+ - !binary |-
140
+ c3BlYy90ZW1wbGF0ZS54bHM=
141
+ - !binary |-
142
+ c3BlYy90ZXN0LmNzdg==
143
+ - !binary |-
144
+ c3BlYy90ZXN0LnR4dA==
145
+ - !binary |-
146
+ c3BlYy90ZXN0X2hlbHBlci5yYg==
147
+ - !binary |-
148
+ c3BlYy90ZXN0X215bWF0cml4LnJi
149
+ - !binary |-
150
+ c3BlYy93b3Jkcy8wMDFfc2hlZXQuZG9j
151
+ - !binary |-
152
+ c3BlYy93b3Jkcy8wMDJfc2hlZXQuZG9j
153
+ - !binary |-
154
+ c3BlYy93b3Jkcy8wMDNfc2hlZXQuZG9j
109
155
  homepage: ''
110
156
  licenses: []
111
157
  post_install_message:
@@ -126,31 +172,55 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
172
  version: '0'
127
173
  requirements: []
128
174
  rubyforge_project: mymatrix
129
- rubygems_version: 1.8.21
175
+ rubygems_version: 1.8.24
130
176
  signing_key:
131
177
  specification_version: 3
132
178
  summary: MS Excel and csv/tsv text handling library
133
179
  test_files:
134
- - spec/csv.csv
135
- - spec/csv.csv.txt
136
- - spec/csv_test.csv
137
- - spec/for_concat/con1.xls
138
- - spec/for_concat/con2.xls
139
- - spec/for_concat/con3.xls
140
- - spec/for_concat/con4.xls
141
- - spec/info_list.txt
142
- - spec/jptest.txt
143
- - spec/line4.txt
144
- - spec/line4.xls
145
- - spec/mymatrix_spec.rb
146
- - spec/offset.txt
147
- - spec/out.xls
148
- - spec/std_shoshiki.xls
149
- - spec/template.xls
150
- - spec/test.csv
151
- - spec/test.txt
152
- - spec/test_helper.rb
153
- - spec/test_mymatrix.rb
154
- - spec/words/001_sheet.doc
155
- - spec/words/002_sheet.doc
156
- - spec/words/003_sheet.doc
180
+ - !binary |-
181
+ c3BlYy9jc3YuY3N2
182
+ - !binary |-
183
+ c3BlYy9jc3YuY3N2LnR4dA==
184
+ - !binary |-
185
+ c3BlYy9jc3ZfdGVzdC5jc3Y=
186
+ - !binary |-
187
+ c3BlYy9mb3JfY29uY2F0L2NvbjEueGxz
188
+ - !binary |-
189
+ c3BlYy9mb3JfY29uY2F0L2NvbjIueGxz
190
+ - !binary |-
191
+ c3BlYy9mb3JfY29uY2F0L2NvbjMueGxz
192
+ - !binary |-
193
+ c3BlYy9mb3JfY29uY2F0L2NvbjQueGxz
194
+ - !binary |-
195
+ c3BlYy9pbmZvX2xpc3QudHh0
196
+ - !binary |-
197
+ c3BlYy9qcHRlc3QudHh0
198
+ - !binary |-
199
+ c3BlYy9saW5lNC50eHQ=
200
+ - !binary |-
201
+ c3BlYy9saW5lNC54bHM=
202
+ - !binary |-
203
+ c3BlYy9teW1hdHJpeF9zcGVjLnJi
204
+ - !binary |-
205
+ c3BlYy9vZmZzZXQudHh0
206
+ - !binary |-
207
+ c3BlYy9vdXQueGxz
208
+ - !binary |-
209
+ c3BlYy9zdGRfc2hvc2hpa2kueGxz
210
+ - !binary |-
211
+ c3BlYy90ZW1wbGF0ZS54bHM=
212
+ - !binary |-
213
+ c3BlYy90ZXN0LmNzdg==
214
+ - !binary |-
215
+ c3BlYy90ZXN0LnR4dA==
216
+ - !binary |-
217
+ c3BlYy90ZXN0X2hlbHBlci5yYg==
218
+ - !binary |-
219
+ c3BlYy90ZXN0X215bWF0cml4LnJi
220
+ - !binary |-
221
+ c3BlYy93b3Jkcy8wMDFfc2hlZXQuZG9j
222
+ - !binary |-
223
+ c3BlYy93b3Jkcy8wMDJfc2hlZXQuZG9j
224
+ - !binary |-
225
+ c3BlYy93b3Jkcy8wMDNfc2hlZXQuZG9j
226
+ has_rdoc: