rspreadsheet 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e68b11b80d618d17de31bb79ff8723d7367dbbc
4
- data.tar.gz: 5cf05fdad441db2cea08b21784ce55cac716b149
3
+ metadata.gz: 1baffe562cc732b33a3919529ebac6eee2a4e337
4
+ data.tar.gz: 0405d76a41fecf70a3a131f0fcfdabd41877cb71
5
5
  SHA512:
6
- metadata.gz: 5c438393ef32daba2c6deb4d4d4b251125adc6322ad39297e1f6a4c9d08a7814ff76e5d8f40679e470ffb76fc295048e9da44b590f0561b93dce984617bbf137
7
- data.tar.gz: 0a125394984f40ae1aa8fe2db8c142f3a04e921b580714019ee4d29075f549d784c2773cd688b4482600a2eec83ef6ec406b2903d83f83a7c08ec79af470cb8a
6
+ metadata.gz: a60aa7bc6cbcd3af3d8de9b161b220d8f9f0db92279ee14715a107cd7bb0f902e27dfb87d4b3c42e86634f198c7163e18a01a8721b5a7501ade4d54246cd93a9
7
+ data.tar.gz: 7f44c68924b04255565aaf06e0ac77ba6b3e719a748630122f8f7fd2bff5fe563033321a1e42db3c1070a9164627dd373fec7cba42901b2e9fe4536ac5f3c7b1
@@ -10,8 +10,9 @@ script: 'bundle exec rake'
10
10
  matrix:
11
11
  include:
12
12
  - rvm: 2.4.0
13
- before_install:
13
+ xx_before_install:
14
14
  - gem uninstall libxml-ruby
15
+ - sudo apt-get install ruby-libxml
15
16
  - sudo apt-get install libxml2
16
17
  - gem update bundler
17
18
 
@@ -74,10 +74,7 @@ RSpreadsheet.generate('pricelist.ods') do
74
74
 
75
75
  ## Release notes
76
76
 
77
- 2017-01
78
- - file can be saced to any IO now, making it suitable for creating files on fly.
79
- - basic image handling implemented (issue [#24](https://github.com/gorn/rspreadsheet/issues/24))
80
- - bug corrected: inserted row was not empty, but rather copy of the row below.
77
+ Release notes are maintaned [on github](https://github.com/gorn/rspreadsheet/releases).
81
78
 
82
79
  ## Developing this gem
83
80
 
@@ -1,4 +1,4 @@
1
- if RUBY_VERSION > '2.1'
1
+ if RUBY_VERSION > '2.1'
2
2
 
3
3
  module ClassExtensions
4
4
 
@@ -10,6 +10,9 @@ if RUBY_VERSION > '2.1'
10
10
  inject(0){ |sum, element| sum.to_f + element.to_f } || identity
11
11
  end
12
12
  end
13
+ def self.marker
14
+ 'yes'
15
+ end
13
16
  end
14
17
 
15
18
  refine LibXML::XML::Node do
@@ -69,6 +72,9 @@ else # Monkeypatching
69
72
  inject(0){ |sum, element| sum.to_f + element.to_f } || identity
70
73
  end
71
74
  end
75
+ def self.marker
76
+ 'yes2'
77
+ end
72
78
  end
73
79
 
74
80
  class LibXML::XML::Node
@@ -46,6 +46,10 @@ class Cell < XMLTiedItem
46
46
  end
47
47
  def row; @worksheet.rows(rowi) end
48
48
  def coordinates; [rowi,coli] end
49
+ def address
50
+ Tools.convert_cell_coordinates_to_address(coordinates)
51
+ end
52
+
49
53
  def to_s; value.to_s end
50
54
  def valuexml; self.valuexmlnode.andand.inner_xml end
51
55
  def valuexmlnode; self.xmlnode.elements.first end
@@ -128,8 +132,9 @@ class Cell < XMLTiedItem
128
132
  when gt == Float then
129
133
  remove_all_value_attributes_and_content
130
134
  set_type_attribute('float')
131
- Tools.set_ns_attribute(xmlnode,'office','value', avalue.to_s)
132
- xmlnode << Tools.prepare_ns_node('text','p', avalue.to_f.to_s)
135
+ sav=avalue.to_f.to_s # to_f handles case when avalue is decimal number
136
+ Tools.set_ns_attribute(xmlnode,'office','value', sav)
137
+ xmlnode << Tools.prepare_ns_node('text','p', sav)
133
138
  when gt == String then
134
139
  remove_all_value_attributes_and_content
135
140
  set_type_attribute('string')
@@ -137,14 +142,18 @@ class Cell < XMLTiedItem
137
142
  when gt == :datetime then
138
143
  remove_all_value_attributes_and_content
139
144
  set_type_attribute('date')
140
- avalue = avalue.strftime(InternalDateTimeFormat)
141
- Tools.set_ns_attribute(xmlnode,'office','date-value', avalue)
142
- xmlnode << Tools.prepare_ns_node('text','p', avalue)
145
+ if avalue.kind_of?(DateTime) or avalue.kind_of?(Date) or avalue.kind_of?(Time)
146
+ avalue = avalue.strftime(InternalDateTimeFormat)
147
+ Tools.set_ns_attribute(xmlnode,'office','date-value', avalue)
148
+ xmlnode << Tools.prepare_ns_node('text','p', avalue)
149
+ end
143
150
  when gt == :time then
144
151
  remove_all_value_attributes_and_content
145
152
  set_type_attribute('time')
146
- Tools.set_ns_attribute(xmlnode,'office','time-value', avalue.strftime(InternalTimeFormat))
147
- xmlnode << Tools.prepare_ns_node('text','p', avalue.strftime('%H:%M'))
153
+ if avalue.kind_of?(DateTime) or avalue.kind_of?(Date) or avalue.kind_of?(Time)
154
+ Tools.set_ns_attribute(xmlnode,'office','time-value', avalue.strftime(InternalTimeFormat))
155
+ xmlnode << Tools.prepare_ns_node('text','p', avalue.strftime('%H:%M'))
156
+ end
148
157
  when gt == :percentage then
149
158
  remove_all_value_attributes_and_content
150
159
  set_type_attribute('percentage')
@@ -153,8 +162,10 @@ class Cell < XMLTiedItem
153
162
  when gt == :currency then
154
163
  remove_all_value_attributes_and_content
155
164
  set_type_attribute('currency')
156
- Tools.set_ns_attribute(xmlnode,'office','value', '%f' % avalue.to_d)
157
- xmlnode << Tools.prepare_ns_node('text','p', avalue.to_d.to_s+' '+self.format.currency)
165
+ unless avalue.nil?
166
+ Tools.set_ns_attribute(xmlnode,'office','value', '%f' % avalue.to_d)
167
+ xmlnode << Tools.prepare_ns_node('text','p', avalue.to_d.to_s+' '+self.format.currency)
168
+ end
158
169
  end
159
170
  else
160
171
  raise "Unknown cell mode #{self.mode}"
@@ -203,7 +214,7 @@ class Cell < XMLTiedItem
203
214
  else nil
204
215
  end
205
216
  result = valueguess
206
-
217
+
207
218
  if valueguess.nil? # valueguess is most important if not succesfull then try guessing by type from node xml
208
219
  typ = xmlnode.nil? ? 'N/A' : xmlnode.attributes['value-type']
209
220
  typeguess = case typ
@@ -256,9 +267,6 @@ class Cell < XMLTiedItem
256
267
  def format
257
268
  @format ||= CellFormat.new(self)
258
269
  end
259
- def address
260
- Tools.convert_cell_coordinates_to_address(coordinates)
261
- end
262
270
 
263
271
  def formula
264
272
  rawformula = Tools.get_ns_attribute(xmlnode,'table','formula',nil).andand.value
@@ -37,12 +37,18 @@ class Row < XMLTiedItem
37
37
  initialize_xml_tied_item(aworksheet,arowi)
38
38
  end
39
39
 
40
- # @!group Syntactic sugar
41
- def cells(*params); subitems(*params) end
40
+ # @!group Syntactic sugar
41
+ def cells(*params)
42
+ if params.length == 1
43
+ subitems(Tools.convert_column_name_to_index(params[0]))
44
+ else
45
+ subitems(*params)
46
+ end
47
+ end
42
48
  alias :cell :cells
43
49
 
44
50
  ## @return [String or Float or Date] value of the cell
45
- # @param coli [Integer] colum index of the cell
51
+ # @param coli [Integer ot String] colum index of the cell of colum letter
46
52
  # returns value of the cell at column `coli`.
47
53
  #
48
54
  # @row = @worksheet.rows(5) # with cells containing names of months
@@ -13,22 +13,22 @@ module Tools
13
13
  if addr.length == 1
14
14
  addr[0].to_s.match(/^([A-Za-z]{1,3})(\d{1,8})$/)
15
15
  colname = $~[1]
16
- rowname = $~[2].to_i
16
+ rowi = $~[2].to_i
17
17
  elsif addr.length == 2
18
18
  a = addr[0]; b = addr[1]
19
19
  if a.kind_of?(Integer) and b.kind_of?(Integer) # most common case first
20
- colname,rowname = b,a
20
+ colname,rowi = b,a
21
21
  elsif only_letters?(a)
22
22
  if kind_of_integer?(b)
23
- colname,rowname = a,b.to_i
23
+ colname,rowi = a,b.to_i
24
24
  else
25
25
  raise 'Wrong parameters - first is letters, but the seconds is not digits only'
26
26
  end
27
27
  elsif kind_of_integer?(a)
28
28
  if only_letters?(b)
29
- colname,rowname = b,a.to_i
29
+ colname,rowi = b,a.to_i
30
30
  elsif kind_of_integer?(b)
31
- colname,rowname = b.to_i,a.to_i
31
+ colname,rowi = b.to_i,a.to_i
32
32
  else
33
33
  raise 'Wrong second out of two paremeters - mix of digits and numbers'
34
34
  end
@@ -39,24 +39,23 @@ module Tools
39
39
  raise 'Wrong number of arguments'
40
40
  end
41
41
 
42
+ return [rowi,convert_column_name_to_index(colname)]
43
+ end
44
+
45
+ def self.convert_column_name_to_index(colname)
46
+ return colname if colname.kind_of?(Integer)
42
47
  ## first possibility how to implement it
43
48
  # colname=colname.rjust(3,'@')
44
- # col = (colname[-1].ord-64)+(colname[-2].ord-64)*26+(colname[-3].ord-64)*26*26
49
+ # return (colname[-1].ord-64)+(colname[-2].ord-64)*26+(colname[-3].ord-64)*26*26
45
50
 
46
51
  ## second possibility how to implement it
47
- # col=(colname.to_i(36)-('A'*colname.size).to_i(36)).to_s(36).to_i(26)+('1'*colname.size).to_i(26)
52
+ # return (colname.to_i(36)-('A'*colname.size).to_i(36)).to_s(36).to_i(26)+('1'*colname.size).to_i(26)
48
53
 
49
- if colname.kind_of?(Integer)
50
- col=colname
51
- else
52
54
  ## third possibility how to implement it (second one little shortened)
53
- s=colname.size
54
- col=(colname.upcase.to_i(36)-(36**s-1).div(3.5)).to_s(36).to_i(26)+(26**s-1)/25
55
- end
56
-
57
- row = rowname
58
- return [row,col]
55
+ s=colname.size
56
+ return (colname.to_s.upcase.to_i(36)-(36**s-1).div(3.5)).to_s(36).to_i(26)+(26**s-1)/25
59
57
  end
58
+
60
59
  def self.convert_cell_coordinates_to_address(*coords)
61
60
  coords = coords[0] if coords.length == 1
62
61
  raise 'Wrong number of arguments' if coords.length != 2
@@ -200,4 +199,4 @@ class Range
200
199
  res = self.end-self.begin+1
201
200
  res>0 ? res : 0
202
201
  end
203
- end
202
+ end
@@ -1,3 +1,3 @@
1
1
  module Rspreadsheet
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.5"
3
3
  end
@@ -106,7 +106,7 @@ class Worksheet
106
106
  when 1..2
107
107
  r,c = Rspreadsheet::Tools.a2c(*params)
108
108
  row(r).andand.cell(c)
109
- else raise Exception.new('Wrong number of arguments.')
109
+ else raise ArgumentError.new('Wrong number of arguments.')
110
110
  end
111
111
  end
112
112
  alias :cell :cells
@@ -128,8 +128,9 @@ class Worksheet
128
128
  super
129
129
  end
130
130
  end
131
+ alias :rowcount :size
131
132
  def used_rows_range
132
- 1..self.first_unused_row_index-1
133
+ 1..self.rowcount
133
134
  end
134
135
  end
135
136
 
@@ -74,7 +74,7 @@ module XMLTiedArray
74
74
  case params.length
75
75
  when 0 then subitems_array
76
76
  when 1 then subitem(params[0])
77
- else raise Exception.new('Wrong number of arguments.')
77
+ else raise ArgumentError.new('Wrong number of arguments.')
78
78
  end
79
79
  end
80
80
 
@@ -129,7 +129,6 @@ module XMLTiedArray
129
129
  # @!group inserting new subnodes TODO: refactor out repeatable connected code
130
130
  def insert_new_empty_subnode_before(aindex)
131
131
  node_after = my_subnode(aindex)
132
-
133
132
  if !node_after.nil?
134
133
  node_after.prev = prepare_empty_subnode
135
134
  return node_after.prev
@@ -162,7 +161,7 @@ module XMLTiedArray
162
161
  def prepare_empty_xmlnode
163
162
  raise 'xmlnode is empty and I do not know how to create empty xmlnode. Please provide prepare_empty_xmlnode method in your object.'
164
163
  end
165
-
164
+
166
165
  # @!group finding and accessing subnodes
167
166
  # array containing subnodes of xmlnode which represent subitems
168
167
  def xmlsubnodes
@@ -61,7 +61,7 @@ module XMLTiedArray_WithRepeatableItems
61
61
  node.remove! # remove the original node
62
62
  else # insert outbound xmlnode
63
63
  [index+1..aindex-1,aindex..aindex].reject {|range| range.size<1}.each do |range|
64
- axmlnode << XMLTiedArray_WithRepeatableItems.prepare_repeated_subnode(range.size, options)
64
+ axmlnode << prepare_repeated_subnode(range.size, options)
65
65
  end
66
66
  end #TODO: Out of bounds indexes handling
67
67
  return my_subnode(aindex)
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  # runtime dependencies
29
29
  unless package_natively_installed?('ruby-libxml')
30
- spec.add_runtime_dependency 'libxml-ruby', '2.8' # parsing XML files
30
+ spec.add_runtime_dependency 'libxml-ruby', '3.0' # parsing XML files
31
31
  end
32
32
  spec.add_runtime_dependency 'rubyzip', '~>1.1' # opening zip files
33
33
  spec.add_runtime_dependency 'andand', '~>1.3'
@@ -452,4 +452,25 @@ describe Rspreadsheet::Cell do
452
452
  it 'automatically creates new style, if a style is automatic, some of its attributes changes and there are several cells pointing to it', :penting=>'' do
453
453
 
454
454
  end
455
+
456
+ it 'gracedully accepts nil in assignement' do
457
+ expect {
458
+ @sheet2.cell('B1').value = nil
459
+ @sheet2.cell('B2').value = nil
460
+ @sheet2.cell('B3').value = nil
461
+ @sheet2.cell('B22').value = nil
462
+ @sheet2.cell('D22').value = nil
463
+ @sheet2.cell('F22').value = nil
464
+ }.not_to raise_error
465
+ end
466
+ it 'can be inserted before existing cell and this one is shifted right' do
467
+ @sheet1.B2 = 'test'
468
+ @sheet1.B2.should == 'test'
469
+ inscell = @sheet1.insert_cell_before(2,2)
470
+ inscell.value = 'new'
471
+ @sheet1.B2.should == 'new'
472
+ @sheet1.C2.should == 'test'
473
+ inscell = @sheet1.insert_cell_before(2,4) # should not move cells with data
474
+ @sheet1.C2.should == 'test'
475
+ end
455
476
  end
@@ -21,8 +21,8 @@ if RUBY_VERSION > '2.1'
21
21
  @m2 = LibXML::XML::Node.new('a')
22
22
  end
23
23
  it 'can compare nodes' do
24
- @n.should == @m
25
- @n.should_not == @m2
24
+ @n.to_s.should == @m.to_s
25
+ @n.to_s.should_not == @m2.to_s
26
26
  end
27
27
  it 'has correct elements' do
28
28
  # raise @n.first_diff(@m).inspect
@@ -46,4 +46,4 @@ if RUBY_VERSION > '2.1'
46
46
  end
47
47
  end
48
48
  end
49
- end
49
+ end
@@ -71,6 +71,5 @@ end
71
71
  def xmls_should_be_identical(xml1,xml2)
72
72
  xml2.root.first_diff(xml1.root).should be_nil
73
73
  xml1.root.first_diff(xml2.root).should be_nil
74
-
75
- xml1.root.should == xml2.root
74
+ xml1.root.to_s.should == xml2.root.to_s
76
75
  end
@@ -10,7 +10,7 @@ describe Rspreadsheet::Row do
10
10
  @row = @sheet2.rows(1)
11
11
  @c = @row.cells(1)
12
12
  @c.value = 3
13
- @c.value.should == 3
13
+ @c.value.should == 3
14
14
  end
15
15
  it 'allows access to cells using different syntax' do
16
16
  @row = @sheet1.rows(1)
@@ -48,8 +48,7 @@ describe Rspreadsheet do
48
48
 
49
49
  @content_xml2.root.first_diff(@content_xml1.root).should be_nil
50
50
  @content_xml1.root.first_diff(@content_xml2.root).should be_nil
51
-
52
- @content_xml1.root.should == @content_xml2.root
51
+ @content_xml1.root.to_s.should == @content_xml2.root.to_s
53
52
  end
54
53
 
55
54
  it 'when open and save file modified, than the file is different' do
@@ -8,6 +8,7 @@ describe Rspreadsheet::Worksheet do
8
8
  end
9
9
  it 'contains nonempty xml in rows for testfile' do
10
10
  @sheet.rows(1).xmlnode.elements.size.should be >1
11
+ @sheet.rows(1).xml.index '<text:p>text</text:p>'
11
12
  end
12
13
  it 'uses detach_my_subnode_respect_repeated well' do
13
14
  @sheet.cell(50,12).mode.should_not == :regular
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+ require 'rspreadsheet/xml_tied_array'
3
+
4
+ describe Rspreadsheet::XMLTiedArray do
5
+ before do
6
+ @book = Rspreadsheet.new
7
+ @sheet = @book.create_worksheet
8
+ class TestXMLTiedArray
9
+ include Rspreadsheet::XMLTiedArray
10
+ end
11
+ end
12
+ it 'method subitems does not accept 2 parameteres' do
13
+ expect {@sheet.subitems(1,2)}.to raise_error ArgumentError
14
+ end
15
+ it 'does not have xmlnode method by default' do
16
+ tx = TestXMLTiedArray.new
17
+ expect {tx.xmlnode}.to raise_error
18
+ end
19
+
20
+ it 'raises when prepare_empty_xmlnode fails in insert_new_empty_subnode_before' do
21
+ class TestXMLTiedArray
22
+ def subitem_xml_options; {} end
23
+ def xmlnode; nil end
24
+ end
25
+
26
+ tx = TestXMLTiedArray.new
27
+ expect {tx.insert_new_empty_subnode_before(0)}.to raise_error IndexError
28
+ expect {tx.insert_new_empty_subnode_before(1)}.to raise_error /create empty xmlnode/
29
+ end
30
+ end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspreadsheet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub A.Těšínský
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-19 00:00:00.000000000 Z
11
+ date: 2017-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: libxml-ruby
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '='
18
- - !ruby/object:Gem::Version
19
- version: '2.8'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '='
25
- - !ruby/object:Gem::Version
26
- version: '2.8'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rubyzip
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -122,34 +108,6 @@ dependencies:
122
108
  - - "~>"
123
109
  - !ruby/object:Gem::Version
124
110
  version: '0.7'
125
- - !ruby/object:Gem::Dependency
126
- name: guard
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '2.13'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
137
- - !ruby/object:Gem::Version
138
- version: '2.13'
139
- - !ruby/object:Gem::Dependency
140
- name: guard-rspec
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - "~>"
144
- - !ruby/object:Gem::Version
145
- version: '4.6'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - "~>"
151
- - !ruby/object:Gem::Version
152
- version: '4.6'
153
111
  description: Manipulating OpenDocument spreadsheets with Ruby. This gem can create
154
112
  new, read existing files abd modify them. When modyfying files, it tries to change
155
113
  as little as possible, making it as much forward compatifle as possible.
@@ -208,6 +166,7 @@ files:
208
166
  - spec/tools_spec.rb
209
167
  - spec/workbook_spec.rb
210
168
  - spec/worksheet_spec.rb
169
+ - spec/xml_tied_spec.rb
211
170
  homepage: https://github.com/gorn/rspreadsheet
212
171
  licenses:
213
172
  - GPL
@@ -228,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
187
  version: '0'
229
188
  requirements: []
230
189
  rubyforge_project:
231
- rubygems_version: 2.5.2
190
+ rubygems_version: 2.2.2
232
191
  signing_key:
233
192
  specification_version: 4
234
193
  summary: Manipulating spreadsheets with Ruby (read / create / modify OpenDocument
@@ -250,3 +209,4 @@ test_files:
250
209
  - spec/tools_spec.rb
251
210
  - spec/workbook_spec.rb
252
211
  - spec/worksheet_spec.rb
212
+ - spec/xml_tied_spec.rb