wrap_excel 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -74,14 +74,14 @@ rangeオブジェクトからのアクセス。
74
74
 
75
75
  既存のファイルは保存可能です。
76
76
 
77
- WrapExcel::Book.open('./sample.xls') do |book|
77
+ WrapExcel::Book.open('./sample.xls', :read_only => false) do |book|
78
78
  # do something
79
79
  book.save
80
80
  end
81
81
 
82
82
  もしくは
83
83
 
84
- book = WrapExcel::Book.open('./sample.xls')
84
+ book = WrapExcel::Book.open('./sample.xls', :read_only => false)
85
85
  book.save
86
86
  book.close
87
87
 
@@ -74,14 +74,14 @@ Read from range object
74
74
 
75
75
  Can save an existing file.
76
76
 
77
- WrapExcel::Book.open('./sample.xls') do |book|
77
+ WrapExcel::Book.open('./sample.xls', :read_only => false) do |book|
78
78
  # do something
79
79
  book.save
80
80
  end
81
81
 
82
82
  or
83
83
 
84
- book = WrapExcel::Book.open('./sample.xls')
84
+ book = WrapExcel::Book.open('./sample.xls', :read_only => false)
85
85
  book.save
86
86
  book.close
87
87
 
@@ -5,7 +5,7 @@ module WrapExcel
5
5
  attr_reader :book
6
6
 
7
7
  def initialize(file, options={ }, &block)
8
- options = {
8
+ @options = {
9
9
  :read_only => true,
10
10
  :displayalerts => false,
11
11
  :visible => false
@@ -13,10 +13,10 @@ module WrapExcel
13
13
  file = WrapExcel::Cygwin.cygpath('-w', file) if RUBY_PLATFORM =~ /cygwin/
14
14
  file = WIN32OLE.new('Scripting.FileSystemObject').GetAbsolutePathName(file)
15
15
  @winapp = WIN32OLE.new('Excel.Application')
16
- @winapp.DisplayAlerts = options[:displayalerts]
17
- @winapp.Visible = options[:visible]
16
+ @winapp.DisplayAlerts = @options[:displayalerts]
17
+ @winapp.Visible = @options[:visible]
18
18
  WIN32OLE.const_load(@winapp, WrapExcel) unless WrapExcel.const_defined?(:CONSTANTS)
19
- @book = @winapp.Workbooks.Open(file,{ 'ReadOnly' => options[:read_only] })
19
+ @book = @winapp.Workbooks.Open(file,{ 'ReadOnly' => @options[:read_only] })
20
20
 
21
21
  if block
22
22
  begin
@@ -35,6 +35,7 @@ module WrapExcel
35
35
  end
36
36
 
37
37
  def save
38
+ raise IOError, "Not opened for writing(open with :read_only option)" if @options[:read_only]
38
39
  @book.save
39
40
  end
40
41
 
@@ -49,6 +50,23 @@ module WrapExcel
49
50
  end
50
51
  end
51
52
 
53
+ def add_sheet(sheet = nil, options = { })
54
+ if sheet.is_a? Hash
55
+ options = sheet
56
+ sheet = nil
57
+ end
58
+
59
+ new_sheet_name = options.delete(:as)
60
+
61
+ after_or_before, base_sheet = options.first || [:after, WrapExcel::Sheet.new(@book.Worksheets.Item(@book.Worksheets.Count))]
62
+ base_sheet = base_sheet.sheet
63
+ sheet ? sheet.Copy({ after_or_before => base_sheet }) : @book.WorkSheets.Add({ after_or_before => base_sheet })
64
+
65
+ new_sheet = WrapExcel::Sheet.new(@winapp.Activesheet)
66
+ new_sheet.name = new_sheet_name if new_sheet_name
67
+ new_sheet
68
+ end
69
+
52
70
  def self.open(file, options={ }, &block)
53
71
  new(file, options, &block)
54
72
  end
@@ -1,3 +1,3 @@
1
1
  module WrapExcel
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -76,4 +76,104 @@ describe WrapExcel::Book do
76
76
  }
77
77
  end
78
78
  end
79
+
80
+ describe "#add_sheet" do
81
+ before do
82
+ @book = WrapExcel::Book.open(@simple_file)
83
+ @sheet = @book[0]
84
+ end
85
+
86
+ after do
87
+ @book.close
88
+ end
89
+
90
+ context "only first argument" do
91
+ it "should add worksheet" do
92
+ expect { @book.add_sheet @sheet }.to change{ @book.book.Worksheets.Count }.from(3).to(4)
93
+ end
94
+
95
+ it "should return copyed sheet" do
96
+ sheet = @book.add_sheet @sheet
97
+ copyed_sheet = @book.book.Worksheets.Item(@book.book.Worksheets.Count)
98
+ sheet.name.should eq copyed_sheet.name
99
+ end
100
+ end
101
+
102
+ context "with first argument" do
103
+ context "with second argument is {:as => 'copyed_name'}" do
104
+ it "copyed sheet name should be 'copyed_name'" do
105
+ @book.add_sheet(@sheet, :as => 'copyed_name').name.should eq 'copyed_name'
106
+ end
107
+ end
108
+
109
+ context "with second argument is {:before => @sheet}" do
110
+ it "should add the first sheet" do
111
+ @book.add_sheet(@sheet, :before => @sheet).name.should eq @book[0].name
112
+ end
113
+ end
114
+
115
+ context "with second argument is {:after => @sheet}" do
116
+ it "should add the first sheet" do
117
+ @book.add_sheet(@sheet, :after => @sheet).name.should eq @book[1].name
118
+ end
119
+ end
120
+
121
+ context "with second argument is {:before => @book[2], :after => @sheet}" do
122
+ it "should arguments in the first is given priority" do
123
+ @book.add_sheet(@sheet, :before => @book[2], :after => @sheet).name.should eq @book[2].name
124
+ end
125
+ end
126
+
127
+ end
128
+
129
+ context "without first argument" do
130
+ context "second argument is {:as => 'new sheet'}" do
131
+ it "should return new sheet" do
132
+ @book.add_sheet(:as => 'new sheet').name.should eq 'new sheet'
133
+ end
134
+ end
135
+
136
+ context "second argument is {:before => @sheet}" do
137
+ it "should add the first sheet" do
138
+ @book.add_sheet(:before => @sheet).name.should eq @book[0].name
139
+ end
140
+ end
141
+
142
+ context "second argument is {:after => @sheet}" do
143
+ it "should add the second sheet" do
144
+ @book.add_sheet(:after => @sheet).name.should eq @book[1].name
145
+ end
146
+ end
147
+
148
+ end
149
+
150
+ context "without argument" do
151
+ it "should add empty sheet" do
152
+ expect { @book.add_sheet }.to change{ @book.book.Worksheets.Count }.from(3).to(4)
153
+ end
154
+
155
+ it "shoule return copyed sheet" do
156
+ sheet = @book.add_sheet
157
+ copyed_sheet = @book.book.Worksheets.Item(@book.book.Worksheets.Count)
158
+ sheet.name.should eq copyed_sheet.name
159
+ end
160
+ end
161
+ end
162
+
163
+ describe ".save" do
164
+ context "when open with read only" do
165
+ before do
166
+ @book = WrapExcel::Book.open(@simple_file)
167
+ end
168
+
169
+ it {
170
+ expect {
171
+ @book.save
172
+ }.to raise_error(IOError,
173
+ "Not opened for writing(open with :read_only option)")
174
+ }
175
+ end
176
+
177
+ end
178
+
79
179
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrap_excel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-08 00:00:00.000000000 Z
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &21104316 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 0.9.2
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *21104316
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.2
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec
27
- requirement: &21103752 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: 2.6.0
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *21103752
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.6.0
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rb-fchange
38
- requirement: &21103380 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *21103380
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: rb-notifu
49
- requirement: &21102828 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *21102828
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: win32console
60
- requirement: &21102420 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ! '>='
@@ -65,10 +85,15 @@ dependencies:
65
85
  version: '0'
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *21102420
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: guard-rspec
71
- requirement: &21102036 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
99
  - - ! '>='
@@ -76,7 +101,12 @@ dependencies:
76
101
  version: '0'
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *21102036
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
80
110
  description: WrapExcel is to wrap the win32ole, and easy to use Excel operations with
81
111
  ruby. Detailed description please see the README.
82
112
  email:
@@ -132,7 +162,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
162
  version: '0'
133
163
  segments:
134
164
  - 0
135
- hash: 767876733
165
+ hash: 109616829
136
166
  required_rubygems_version: !ruby/object:Gem::Requirement
137
167
  none: false
138
168
  requirements:
@@ -141,10 +171,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
171
  version: '0'
142
172
  segments:
143
173
  - 0
144
- hash: 767876733
174
+ hash: 109616829
145
175
  requirements: []
146
176
  rubyforge_project: wrap_excel
147
- rubygems_version: 1.8.16
177
+ rubygems_version: 1.8.23
148
178
  signing_key:
149
179
  specification_version: 3
150
180
  summary: WrapExcel is a wrapper library that specializes in the operation of Excel