spreadsheet 1.1.5 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -101,12 +101,12 @@ module Spreadsheet
101
101
  @pattern_fg_color = :border
102
102
  @pattern_bg_color = :pattern_bg
103
103
  @regexes = {
104
- :date => Regexp.new(client("[YMD]", 'UTF-8')),
104
+ :date => Regexp.new(client("[YMD]|d{2}|m{3}|y{2}", 'UTF-8')),
105
105
  :date_or_time => Regexp.new(client("[hmsYMD]", 'UTF-8')),
106
106
  :datetime => Regexp.new(client("([YMD].*[HS])|([HS].*[YMD])", 'UTF-8')),
107
107
  :time => Regexp.new(client("[hms]", 'UTF-8')),
108
108
  :number => Regexp.new(client("([\#]|0+)", 'UTF-8')),
109
- :locale => Regexp.new(client(/\A\[\$\-\d+\]/.to_s, 'UTF-8')),
109
+ :locale => Regexp.new(client(/\A\[\$\-\S+\]/.to_s, 'UTF-8')),
110
110
  }
111
111
 
112
112
  # Temp code to prevent merged formats in non-merged cells.
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spreadsheet
4
+ ##
5
+ # The version of Spreadsheet you are using.
6
+ VERSION = '1.3.0'
7
+ end
@@ -76,7 +76,7 @@ module Spreadsheet
76
76
  # Returns the count of total worksheets present.
77
77
  # Takes no arguments. Just returns the length of @worksheets array.
78
78
  def sheet_count
79
- @worksheets.length
79
+ @worksheets.length
80
80
  end
81
81
  ##
82
82
  # The Font at _idx_
@@ -49,7 +49,7 @@ module Spreadsheet
49
49
  :right => 0.75,
50
50
  :bottom => 1
51
51
  }
52
- @name = opts[:name] || 'Worksheet'
52
+ @name = sanitize_invalid_characters(opts[:name] || 'Worksheet')
53
53
  @workbook = opts[:workbook]
54
54
  @rows = []
55
55
  @columns = []
@@ -153,9 +153,13 @@ module Spreadsheet
153
153
  # If the argument skip is given, #each iterates from that row until but
154
154
  # omitting the first unused Row, effectively skipping the first _skip_ Rows
155
155
  # from the top of the Worksheet.
156
- def each skip=dimensions[0]
157
- skip.upto(dimensions[1] - 1) do |idx|
158
- yield row(idx)
156
+ def each(skip=dimensions[0], &block)
157
+ rows = skip.upto(dimensions[1] - 1).map { |index| row(index) }.to_enum
158
+
159
+ if block_given?
160
+ rows.each(&block)
161
+ else
162
+ rows
159
163
  end
160
164
  end
161
165
  def encoding # :nodoc:
@@ -228,6 +232,9 @@ module Spreadsheet
228
232
  updated_from idx
229
233
  res
230
234
  end
235
+ def << cells=[]
236
+ insert_row @rows.size, cells
237
+ end
231
238
  def inspect
232
239
  names = instance_variables
233
240
  names.delete '@rows'
@@ -314,7 +321,7 @@ module Spreadsheet
314
321
 
315
322
  def compact!
316
323
  recalculate_dimensions
317
-
324
+
318
325
  # detect first non-nil non-empty row if given first row is empty or nil
319
326
  if row(@dimensions[0]).empty? || row(@dimensions[0]).compact.join('').empty?
320
327
  (@dimensions[0]...@dimensions[1]).each do |i|
@@ -325,19 +332,19 @@ module Spreadsheet
325
332
 
326
333
  # detect last non-nil non-empty row if given last row is empty or nil
327
334
  if row(@dimensions[1] - 1).empty? || row(@dimensions[1] - 1).compact.join('').empty?
328
- i = @dimensions[1] - 1
335
+ row_size = @dimensions[1] - 1
329
336
  @dimensions[1] = @dimensions[0]
330
337
  # divide and conquer
331
- while(i - @dimensions[1] > 1) do
332
- if row(i).empty? || row(i).compact.join('').empty?
333
- i = @dimensions[1] + (((i - @dimensions[1]) + 1) / 2).to_i
338
+ while(row_size - @dimensions[1] > 1) do
339
+ if row(row_size).empty? || row(row_size).compact.join('').empty?
340
+ row_size = @dimensions[1] + (((row_size - @dimensions[1]) + 1) / 2).to_i
334
341
  else
335
- _i = ((i - @dimensions[1]) / 2).to_i + 1
336
- @dimensions[1] = i
337
- i = i + _i
342
+ _i = ((row_size - @dimensions[1]) / 2).to_i + 1
343
+ @dimensions[1] = row_size
344
+ row_size = row_size + _i
338
345
  end
339
346
  end
340
- @dimensions[1] = i + 1
347
+ @dimensions[1] = row_size + 1
341
348
  end
342
349
 
343
350
  # detect first non-empty non-nil column if first column is empty or nil
@@ -359,6 +366,9 @@ module Spreadsheet
359
366
  end
360
367
 
361
368
  private
369
+ def sanitize_invalid_characters(name) # :nodoc:
370
+ name.gsub(Regexp.new('[\\\/\*\?\:\[\]]'.encode(Spreadsheet.client_encoding)), '_')
371
+ end
362
372
  def index_of_first ary # :nodoc:
363
373
  return unless ary
364
374
  ary.index(ary.find do |elm| elm end)
data/lib/spreadsheet.rb CHANGED
@@ -25,6 +25,7 @@
25
25
  # 8006 Zürich
26
26
  ### Switzerland
27
27
 
28
+ require 'spreadsheet/version'
28
29
  require 'spreadsheet/errors'
29
30
 
30
31
  require 'spreadsheet/excel/workbook'
@@ -44,10 +45,6 @@ require 'spreadsheet/excel/rgb'
44
45
  # sheet.each do |row| puts row[0] end
45
46
  module Spreadsheet
46
47
 
47
- ##
48
- # The version of Spreadsheet you are using.
49
- VERSION = '1.1.5'
50
-
51
48
  ##
52
49
  # Default client Encoding. Change this value if your application uses a
53
50
  # different Encoding:
Binary file
Binary file
data/test/excel/reader.rb CHANGED
@@ -25,6 +25,19 @@ module Spreadsheet
25
25
  reader.setup not_empty_io
26
26
  end
27
27
  end
28
+
29
+ def test_not_frozen_stream_error_on_setup
30
+ return if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3.0')
31
+
32
+ reader = Spreadsheet::Excel::Reader.new
33
+ data = File.expand_path File.join('test', 'data')
34
+ path = File.join data, 'test_empty.xls'
35
+ content_string = File.read path
36
+ frozen_io = StringIO.new(content_string.freeze)
37
+ assert_nothing_thrown do
38
+ reader.setup(frozen_io)
39
+ end
40
+ end
28
41
  end
29
42
  end
30
43
  end
data/test/format.rb CHANGED
@@ -25,6 +25,8 @@ module Spreadsheet
25
25
  assert_equal false, @format.date?
26
26
  @format.number_format = "0.00;[RED]\\-0.00"
27
27
  assert_equal false, @format.date?
28
+ @format.number_format = "[$-C0A]dd\\-mmm\\-yy"
29
+ assert_equal true, @format.date?
28
30
  end
29
31
  def test_date_or_time?
30
32
  assert_equal false, @format.date_or_time?
data/test/integration.rb CHANGED
@@ -36,7 +36,7 @@ module Spreadsheet
36
36
  end
37
37
  def teardown
38
38
  Spreadsheet.client_encoding = 'UTF-8'
39
- FileUtils.rm_r @var
39
+ FileUtils.rm_rf @var
40
40
  end
41
41
  def test_copy__identical__file_paths
42
42
  path = File.join @data, 'test_copy.xls'
@@ -70,7 +70,7 @@ module Spreadsheet
70
70
  def test_missing_format
71
71
  path = File.join @data, 'test_missing_format.xls'
72
72
  assert_nothing_thrown do
73
- workbook = Spreadsheet.open(path, "rb")
73
+ Spreadsheet.open(path, "rb")
74
74
  end
75
75
  end
76
76
  def test_version_excel97__excel2010__utf16
@@ -506,7 +506,7 @@ module Spreadsheet
506
506
  assert_equal 3, sheets.size
507
507
  sheet = book.worksheet 0
508
508
  assert_instance_of Excel::Worksheet, sheet
509
- str = "S\000h\000e\000e\000t\0001\000"
509
+ str = "S\000h\000e\000e\000t\0001\000".dup
510
510
  if RUBY_VERSION >= '1.9'
511
511
  str.force_encoding 'UTF-16LE' if str.respond_to?(:force_encoding)
512
512
  end
@@ -730,7 +730,7 @@ module Spreadsheet
730
730
  book = Spreadsheet::Excel::Workbook.new
731
731
  sheet = book.create_worksheet :name => 'My Worksheet'
732
732
  sheet[0,0] = 'my cell'
733
- data = StringIO.new ''
733
+ data = StringIO.new ''.dup
734
734
  assert_nothing_raised do
735
735
  book.write data
736
736
  end
@@ -749,7 +749,7 @@ module Spreadsheet
749
749
  sheet1 = book.create_worksheet
750
750
  str1 = 'My Shared String'
751
751
  str2 = 'Another Shared String'
752
- assert_equal 1, (str1.size + str2.size) % 2,
752
+ assert_equal 1, (str1.size + str2.size) % 2,
753
753
  "str3 should start at an odd offset to test splitting of wide strings"
754
754
  str3 = '–––––––––– ' * 1000
755
755
  str4 = '1234567890 ' * 1000
@@ -829,7 +829,7 @@ module Spreadsheet
829
829
  assert_equal 2, book.worksheets.size
830
830
  sheet = book.worksheets.first
831
831
  assert_instance_of Spreadsheet::Excel::Worksheet, sheet
832
- name = "W\000o\000r\000k\000s\000h\000e\000e\000t\0001\000"
832
+ name = "W\000o\000r\000k\000s\000h\000e\000e\000t\0001\000".dup
833
833
  name.force_encoding 'UTF-16LE' if name.respond_to?(:force_encoding)
834
834
  assert_equal name, sheet.name
835
835
  assert_not_nil sheet.offset
@@ -926,7 +926,7 @@ module Spreadsheet
926
926
  assert_equal 40, sheet1.row(11).height
927
927
  assert_instance_of Spreadsheet::Excel::Worksheet, sheet
928
928
  sheet = book.worksheets.last
929
- name = "m\000y\000 \000n\000a\000m\000e\000"
929
+ name = "m\000y\000 \000n\000a\000m\000e\000".dup
930
930
  name.force_encoding 'UTF-16LE' if name.respond_to?(:force_encoding)
931
931
  assert_equal name, sheet.name
932
932
  assert_not_nil sheet.offset
@@ -1211,8 +1211,8 @@ module Spreadsheet
1211
1211
  book = Spreadsheet::Workbook.new
1212
1212
  sheet = book.create_worksheet
1213
1213
  (0..200).each { |i| sheet.row(i).push "ëçáéíóú" }
1214
- assert_nothing_raised do
1215
- book.write StringIO.new("", "w+")
1214
+ assert_nothing_raised do
1215
+ book.write StringIO.new("".dup, "w+")
1216
1216
  end
1217
1217
  end
1218
1218
 
@@ -1257,7 +1257,7 @@ module Spreadsheet
1257
1257
  str1 = "Frozen String.".freeze
1258
1258
  sheet1[0,0] = str1
1259
1259
  sheet1.row(0).push str1
1260
- assert_nothing_raised do
1260
+ assert_nothing_raised do
1261
1261
  book.write path
1262
1262
  end
1263
1263
  end
@@ -1290,7 +1290,7 @@ module Spreadsheet
1290
1290
  sheet1 = book.create_worksheet
1291
1291
  (sheet1.row(0).format 0).border = :hair
1292
1292
  (sheet1.row(0).format 0).border_color = :brown
1293
- assert_nothing_raised do
1293
+ assert_nothing_raised do
1294
1294
  book.write path
1295
1295
  end
1296
1296
  book2 = Spreadsheet.open path
@@ -1391,7 +1391,7 @@ module Spreadsheet
1391
1391
  path = File.join @var, 'test_write_worksheet_visibility.xls'
1392
1392
  sheet1 = book.create_worksheet
1393
1393
  sheet1.visibility = :hidden
1394
- sheet2 = book.create_worksheet
1394
+ _sheet2 = book.create_worksheet
1395
1395
  assert_nothing_raised do
1396
1396
  book.write path
1397
1397
  end
@@ -1401,6 +1401,30 @@ module Spreadsheet
1401
1401
  assert_equal(:visible, book2.worksheet(1).visibility)
1402
1402
  end
1403
1403
 
1404
+ def test_append_and_reopen
1405
+ filename = path = File.join @var, 'test.xls'
1406
+ sheet_name = 'Test Sheet'
1407
+
1408
+ # Create excel
1409
+ excel = Spreadsheet::Workbook.new(filename)
1410
+ sheet = excel.create_worksheet(name: sheet_name)
1411
+ sheet.row(1).replace ['Data']
1412
+ excel.write(filename)
1413
+
1414
+ # Append something
1415
+ excel = Spreadsheet.open(filename, 'a+')
1416
+ sheet = excel.worksheet(sheet_name)
1417
+ sheet.row(2).replace ['Data2']
1418
+ filename = "test2.xls"
1419
+ excel.write(filename)
1420
+
1421
+ # Reopen
1422
+ excel = Spreadsheet.open filename
1423
+ sheet = excel.worksheet sheet_name
1424
+ ensure
1425
+ File.delete filename if File.exist? filename
1426
+ end
1427
+
1404
1428
  def test_text_drawing
1405
1429
  path = File.join @data, 'test_text_drawing.xls'
1406
1430
  book = Spreadsheet.open path
data/test/suite.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require 'rubygems'
4
4
  require 'bundler'
5
5
  require 'find'
6
+ require 'simplecov'
7
+ SimpleCov.start
6
8
 
7
9
  $VERBOSE = true
8
10
 
data/test/worksheet.rb CHANGED
@@ -108,18 +108,40 @@ module Spreadsheet
108
108
  assert_equal 6, @book.formats.length
109
109
 
110
110
  end
111
-
111
+
112
112
  def test_freeze_panel!
113
113
  assert_equal 0, @sheet.froze_top
114
114
  assert_equal 0, @sheet.froze_left
115
115
  assert_equal false, @sheet.has_frozen_panel?
116
-
116
+
117
117
  @sheet.freeze!(2, 3)
118
118
  assert_equal 2, @sheet.froze_top
119
119
  assert_equal 3, @sheet.froze_left
120
120
  assert_equal true, @sheet.has_frozen_panel?
121
-
121
+
122
+ end
123
+
124
+ def test_each_with_skip
125
+ @sheet[0, 0] = 'foo'
126
+ @sheet[1, 0] = 'bar'
127
+
128
+ assert_equal @sheet.each(1).count, 1
129
+ assert_equal @sheet.each(1).first[0], 'bar'
122
130
  end
123
-
131
+
132
+ def test_each_with_index
133
+ @sheet[0, 0] = 'foo'
134
+ @sheet[1, 0] = 'bar'
135
+
136
+ @sheet.each.with_index do |row, index|
137
+ assert_equal row[0], @sheet[index, 0]
138
+ end
139
+ end
140
+
141
+ def test_name
142
+ worksheet = Worksheet.new(name: '\a/b*c?d:e[f]')
143
+ assert_equal '_a_b_c_d_e_f_', worksheet.name
144
+ end
145
+
124
146
  end
125
147
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spreadsheet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
- - Masaomi Hatakeyama, Zeno R.R. Davatz
7
+ - Hannes F. Wyss, Masaomi Hatakeyama, Zeno R.R. Davatz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-20 00:00:00.000000000 Z
11
+ date: 2021-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-ole
@@ -16,72 +16,72 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.0'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rdoc
28
+ name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: '4.0'
47
+ version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '4.0'
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: hoe
56
+ name: simplecov
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - "~>"
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
- version: '3.14'
61
+ version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - "~>"
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
- version: '3.14'
55
- description: |-
56
- The Spreadsheet Library is designed to read and write Spreadsheet Documents.
57
- As of version 0.6.0, only Microsoft Excel compatible spreadsheets are
58
- supported. Spreadsheet is a combination/complete rewrite of the
59
- Spreadsheet::Excel Library by Daniel J. Berger and the ParseExcel Library by
60
- Hannes Wyss. Spreadsheet can read, write and modify Spreadsheet Documents.
61
- email: zdavatz@ywesee.com
68
+ version: '0'
69
+ description: As of version 0.6.0, only Microsoft Excel compatible spreadsheets are
70
+ supported
71
+ email: hannes.wyss@gmail.com, mhatakeyama@ywesee.com, zdavatz@ywesee.com
62
72
  executables:
63
73
  - xlsopcodes
64
74
  extensions: []
65
- extra_rdoc_files:
66
- - GUIDE.md
67
- - History.md
68
- - LICENSE.txt
69
- - Manifest.txt
70
- - README.md
75
+ extra_rdoc_files: []
71
76
  files:
72
- - ".gitignore"
73
- - ".travis.yml"
74
- - Excel97-2007BinaryFileFormatSpecification.pdf
75
- - GUIDE.md
76
- - Gemfile
77
- - Gemfile.lock
78
- - History.md
79
77
  - LICENSE.txt
80
78
  - Manifest.txt
81
- - README.md
82
- - Rakefile
79
+ - bin/bundle
80
+ - bin/bundler
81
+ - bin/oletool
82
+ - bin/rake
83
+ - bin/sow
83
84
  - bin/xlsopcodes
84
- - excelfileformat.pdf
85
85
  - lib/parseexcel.rb
86
86
  - lib/parseexcel/parseexcel.rb
87
87
  - lib/parseexcel/parser.rb
@@ -120,14 +120,16 @@ files:
120
120
  - lib/spreadsheet/note.rb
121
121
  - lib/spreadsheet/noteObject.rb
122
122
  - lib/spreadsheet/row.rb
123
+ - lib/spreadsheet/version.rb
123
124
  - lib/spreadsheet/workbook.rb
124
125
  - lib/spreadsheet/worksheet.rb
125
126
  - lib/spreadsheet/writer.rb
126
- - spreadsheet.gemspec
127
127
  - test/data/test_adding_data_to_existing_file.xls
128
128
  - test/data/test_borders.xls
129
129
  - test/data/test_changes.xls
130
130
  - test/data/test_comment.xls
131
+ - test/data/test_compact_format_date.xls
132
+ - test/data/test_compact_many_rows.xls
131
133
  - test/data/test_copy.xls
132
134
  - test/data/test_datetime.xls
133
135
  - test/data/test_empty.xls
@@ -136,8 +138,10 @@ files:
136
138
  - test/data/test_margin.xls
137
139
  - test/data/test_merged_and_protected.xls
138
140
  - test/data/test_merged_cells.xls
141
+ - test/data/test_missing_format.xls
139
142
  - test/data/test_missing_row.xls
140
143
  - test/data/test_pagesetup.xls
144
+ - test/data/test_sizes.xls
141
145
  - test/data/test_text_drawing.xls
142
146
  - test/data/test_version_excel5.xls
143
147
  - test/data/test_version_excel95.xls
@@ -156,14 +160,12 @@ files:
156
160
  - test/workbook.rb
157
161
  - test/workbook_protection.rb
158
162
  - test/worksheet.rb
159
- homepage: https://github.com/zdavatz/spreadsheet
163
+ homepage: https://github.com/zdavatz/spreadsheet/
160
164
  licenses:
161
165
  - GPL-3.0
162
166
  metadata: {}
163
167
  post_install_message:
164
- rdoc_options:
165
- - "--main"
166
- - README.md
168
+ rdoc_options: []
167
169
  require_paths:
168
170
  - lib
169
171
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -177,9 +179,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
179
  - !ruby/object:Gem::Version
178
180
  version: '0'
179
181
  requirements: []
180
- rubyforge_project:
181
- rubygems_version: 2.6.8
182
+ rubygems_version: 3.2.3
182
183
  signing_key:
183
184
  specification_version: 4
184
185
  summary: The Spreadsheet Library is designed to read and write Spreadsheet Documents
185
- test_files: []
186
+ test_files:
187
+ - test/suite.rb
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- tags
2
- *.swp
data/.travis.yml DELETED
@@ -1,36 +0,0 @@
1
- language: ruby
2
- sudo: false
3
- cache: bundler
4
- before_install:
5
- - gem install bundler
6
- bundler_args: --binstubs
7
- script: "bundle exec ruby test/suite.rb"
8
- rvm:
9
- - ruby-head
10
- - 2.3.3
11
- - 2.2.6
12
- - 2.1.10
13
- - 2.0.0
14
- - 1.9.3
15
- - 1.9.2
16
- - 1.8.7
17
- - rbx-19mode
18
- - rbx-18mode
19
- - jruby-head
20
- - jruby-19mode
21
- - jruby-18mode
22
- - ree
23
- matrix:
24
- allow_failures:
25
- - rvm: ruby-head
26
- - rvm: rbx-19mode
27
- - rvm: rbx-18mode
28
- - rvm: jruby-head
29
- - rvm: jruby-19mode
30
- - rvm: jruby-18mode
31
- - rvm: ree
32
- notifications:
33
- email:
34
- recipients:
35
- - yasaka@ywesee.com
36
- - zdavatz@ywesee.com