write_xlsx 1.09.5 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -0
  3. data/Changes +5 -0
  4. data/LICENSE.txt +1 -1
  5. data/examples/autofilter.rb +38 -9
  6. data/examples/dynamic_arrays.rb +247 -0
  7. data/examples/lambda.rb +43 -0
  8. data/examples/watermark.png +0 -0
  9. data/examples/watermark.rb +26 -0
  10. data/lib/write_xlsx/chart.rb +1 -0
  11. data/lib/write_xlsx/chartsheet.rb +1 -0
  12. data/lib/write_xlsx/col_name.rb +1 -0
  13. data/lib/write_xlsx/colors.rb +1 -0
  14. data/lib/write_xlsx/compatibility.rb +1 -0
  15. data/lib/write_xlsx/drawing.rb +20 -10
  16. data/lib/write_xlsx/format.rb +5 -0
  17. data/lib/write_xlsx/formats.rb +1 -0
  18. data/lib/write_xlsx/gradient.rb +2 -0
  19. data/lib/write_xlsx/package/app.rb +1 -0
  20. data/lib/write_xlsx/package/button.rb +6 -2
  21. data/lib/write_xlsx/package/comments.rb +3 -1
  22. data/lib/write_xlsx/package/conditional_format.rb +1 -0
  23. data/lib/write_xlsx/package/content_types.rb +1 -0
  24. data/lib/write_xlsx/package/core.rb +1 -0
  25. data/lib/write_xlsx/package/custom.rb +1 -0
  26. data/lib/write_xlsx/package/metadata.rb +1 -0
  27. data/lib/write_xlsx/package/packager.rb +1 -0
  28. data/lib/write_xlsx/package/relationships.rb +1 -0
  29. data/lib/write_xlsx/package/shared_strings.rb +1 -1
  30. data/lib/write_xlsx/package/styles.rb +1 -0
  31. data/lib/write_xlsx/package/table.rb +1 -0
  32. data/lib/write_xlsx/package/theme.rb +1 -0
  33. data/lib/write_xlsx/package/vml.rb +1 -0
  34. data/lib/write_xlsx/package/xml_writer_simple.rb +21 -2
  35. data/lib/write_xlsx/shape.rb +1 -0
  36. data/lib/write_xlsx/sheets.rb +1 -0
  37. data/lib/write_xlsx/sparkline.rb +1 -0
  38. data/lib/write_xlsx/utility.rb +2 -3
  39. data/lib/write_xlsx/version.rb +3 -1
  40. data/lib/write_xlsx/worksheet/cell_data.rb +52 -62
  41. data/lib/write_xlsx/worksheet/data_validation.rb +1 -0
  42. data/lib/write_xlsx/worksheet/hyperlink.rb +3 -2
  43. data/lib/write_xlsx/worksheet/page_setup.rb +1 -0
  44. data/lib/write_xlsx/worksheet.rb +260 -47
  45. data/lib/write_xlsx/zip_file_utils.rb +1 -0
  46. data/lib/write_xlsx.rb +1 -0
  47. data/write_xlsx.gemspec +1 -0
  48. metadata +8 -4
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'write_xlsx/package/xml_writer_simple'
4
5
  require 'write_xlsx/utility'
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'write_xlsx/package/xml_writer_simple'
4
5
  require 'write_xlsx/utility'
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'write_xlsx/package/xml_writer_simple'
4
5
  require 'write_xlsx/utility'
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'write_xlsx/package/xml_writer_simple'
4
5
  require 'write_xlsx/utility'
@@ -27,7 +27,7 @@ module Writexlsx
27
27
  unless @strings_index[string]
28
28
  # Only first time the string will be append to list
29
29
  # next time we only check and not #dup it
30
- str = string.dup.freeze
30
+ str = string.frozen? ? string : string.freeze
31
31
  @strings << str
32
32
  @strings_index[str] = @strings.size - 1
33
33
  end
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'write_xlsx/package/xml_writer_simple'
4
5
  require 'write_xlsx/utility'
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'write_xlsx/package/xml_writer_simple'
4
5
  require 'write_xlsx/utility'
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'write_xlsx/package/xml_writer_simple'
4
5
  require 'write_xlsx/utility'
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'write_xlsx/package/xml_writer_simple'
4
5
  require 'write_xlsx/utility'
@@ -13,6 +13,11 @@ module Writexlsx
13
13
 
14
14
  def initialize
15
15
  @io = StringIO.new
16
+ # Will allocate new string once, then use allocated string
17
+ # Key is tag name
18
+ # Only tags without attributes will be cached
19
+ @tag_start_cache = {}
20
+ @tag_end_cache = {}
16
21
  end
17
22
 
18
23
  def set_xml_writer(filename = nil)
@@ -41,7 +46,16 @@ module Writexlsx
41
46
  end
42
47
 
43
48
  def start_tag_str(tag, attr = [])
44
- "<#{tag}#{key_vals(attr)}>"
49
+ if attr.empty?
50
+ result = @tag_start_cache[tag]
51
+ unless result
52
+ result = "<#{tag}>"
53
+ @tag_start_cache[tag] = result
54
+ end
55
+ else
56
+ result = "<#{tag}#{key_vals(attr)}>"
57
+ end
58
+ result
45
59
  end
46
60
 
47
61
  def end_tag(tag)
@@ -49,7 +63,12 @@ module Writexlsx
49
63
  end
50
64
 
51
65
  def end_tag_str(tag)
52
- "</#{tag}>"
66
+ result = @tag_end_cache[tag]
67
+ unless result
68
+ result = "</#{tag}>"
69
+ @tag_end_cache[tag] = result
70
+ end
71
+ result
53
72
  end
54
73
 
55
74
  def empty_tag(tag, attr = [])
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  module Writexlsx
4
5
  ###############################################################################
@@ -1,4 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'delegate'
4
5
  require 'write_xlsx/package/xml_writer_simple'
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'write_xlsx/utility'
4
5
 
@@ -458,11 +458,10 @@ module Writexlsx
458
458
  [left, top, width, height]
459
459
  end
460
460
 
461
- def v_shape_attributes_base(id, z_index)
461
+ def v_shape_attributes_base(id)
462
462
  [
463
463
  ['id', "_x0000_s#{id}"],
464
- ['type', type],
465
- ['style', (v_shape_style_base(z_index, vertices) + style_addition).join]
464
+ ['type', type]
466
465
  ]
467
466
  end
468
467
 
@@ -1 +1,3 @@
1
- WriteXLSX_VERSION = "1.09.5"
1
+ # frozen_string_literal: true
2
+
3
+ WriteXLSX_VERSION = "1.10.0"
@@ -6,14 +6,13 @@ module Writexlsx
6
6
  class CellData # :nodoc:
7
7
  include Writexlsx::Utility
8
8
 
9
- attr_reader :row, :col, :token, :xf
10
- attr_reader :result, :range, :link_type, :url, :tip
9
+ attr_reader :xf
11
10
 
12
11
  #
13
12
  # attributes for the <cell> element. This is the innermost loop so efficiency is
14
13
  # important where possible.
15
14
  #
16
- def cell_attributes # :nodoc:
15
+ def cell_attributes(worksheet, row, col) # :nodoc:
17
16
  xf_index = xf ? xf.get_xf_index : 0
18
17
  attributes = [
19
18
  ['r', xl_rowcol_to_cell(row, col)]
@@ -22,11 +21,11 @@ module Writexlsx
22
21
  # Add the cell format index.
23
22
  if xf_index != 0
24
23
  attributes << ['s', xf_index]
25
- elsif @worksheet.set_rows[row] && @worksheet.set_rows[row][1]
26
- row_xf = @worksheet.set_rows[row][1]
24
+ elsif worksheet.set_rows[row] && worksheet.set_rows[row][1]
25
+ row_xf = worksheet.set_rows[row][1]
27
26
  attributes << ['s', row_xf.get_xf_index]
28
- elsif @worksheet.col_formats[col]
29
- col_xf = @worksheet.col_formats[col]
27
+ elsif worksheet.col_formats[col]
28
+ col_xf = worksheet.col_formats[col]
30
29
  attributes << ['s', col_xf.get_xf_index]
31
30
  end
32
31
  attributes
@@ -38,10 +37,9 @@ module Writexlsx
38
37
  end
39
38
 
40
39
  class NumberCellData < CellData # :nodoc:
41
- def initialize(worksheet, row, col, num, xf)
42
- @worksheet = worksheet
43
- @row = row
44
- @col = col
40
+ attr_reader :token
41
+
42
+ def initialize(num, xf)
45
43
  @token = num
46
44
  @xf = xf
47
45
  end
@@ -50,18 +48,17 @@ module Writexlsx
50
48
  @token
51
49
  end
52
50
 
53
- def write_cell
54
- @worksheet.writer.tag_elements('c', cell_attributes) do
55
- @worksheet.write_cell_value(token)
51
+ def write_cell(worksheet, row, col)
52
+ worksheet.writer.tag_elements('c', cell_attributes(worksheet, row, col)) do
53
+ worksheet.write_cell_value(token)
56
54
  end
57
55
  end
58
56
  end
59
57
 
60
58
  class StringCellData < CellData # :nodoc:
61
- def initialize(worksheet, row, col, index, xf)
62
- @worksheet = worksheet
63
- @row = row
64
- @col = col
59
+ attr_reader :token
60
+
61
+ def initialize(index, xf)
65
62
  @token = index
66
63
  @xf = xf
67
64
  end
@@ -71,11 +68,11 @@ module Writexlsx
71
68
  end
72
69
 
73
70
  TYPE_STR_ATTRS = %w[t s].freeze
74
- def write_cell
75
- attributes = cell_attributes
71
+ def write_cell(worksheet, row, col)
72
+ attributes = cell_attributes(worksheet, row, col)
76
73
  attributes << TYPE_STR_ATTRS
77
- @worksheet.writer.tag_elements('c', attributes) do
78
- @worksheet.write_cell_value(token)
74
+ worksheet.writer.tag_elements('c', attributes) do
75
+ worksheet.write_cell_value(token)
79
76
  end
80
77
  end
81
78
 
@@ -85,10 +82,9 @@ module Writexlsx
85
82
  end
86
83
 
87
84
  class FormulaCellData < CellData # :nodoc:
88
- def initialize(worksheet, row, col, formula, xf, result)
89
- @worksheet = worksheet
90
- @row = row
91
- @col = col
85
+ attr_reader :token, :result, :range, :link_type, :url
86
+
87
+ def initialize(formula, xf, result)
92
88
  @token = formula
93
89
  @xf = xf
94
90
  @result = result
@@ -98,11 +94,11 @@ module Writexlsx
98
94
  @result || 0
99
95
  end
100
96
 
101
- def write_cell
97
+ def write_cell(worksheet, row, col)
102
98
  truefalse = { 'TRUE' => 1, 'FALSE' => 0 }
103
99
  error_code = ['#DIV/0!', '#N/A', '#NAME?', '#NULL!', '#NUM!', '#REF!', '#VALUE!']
104
100
 
105
- attributes = cell_attributes
101
+ attributes = cell_attributes(worksheet, row, col)
106
102
  if @result && !(@result.to_s =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
107
103
  if truefalse[@result]
108
104
  attributes << %w[t b]
@@ -113,18 +109,17 @@ module Writexlsx
113
109
  attributes << %w[t str]
114
110
  end
115
111
  end
116
- @worksheet.writer.tag_elements('c', attributes) do
117
- @worksheet.write_cell_formula(token)
118
- @worksheet.write_cell_value(result || 0)
112
+ worksheet.writer.tag_elements('c', attributes) do
113
+ worksheet.write_cell_formula(token)
114
+ worksheet.write_cell_value(result || 0)
119
115
  end
120
116
  end
121
117
  end
122
118
 
123
119
  class FormulaArrayCellData < CellData # :nodoc:
124
- def initialize(worksheet, row, col, formula, xf, range, result)
125
- @worksheet = worksheet
126
- @row = row
127
- @col = col
120
+ attr_reader :token, :result, :range, :link_type, :url
121
+
122
+ def initialize(formula, xf, range, result)
128
123
  @token = formula
129
124
  @xf = xf
130
125
  @range = range
@@ -135,19 +130,18 @@ module Writexlsx
135
130
  @result || 0
136
131
  end
137
132
 
138
- def write_cell
139
- @worksheet.writer.tag_elements('c', cell_attributes) do
140
- @worksheet.write_cell_array_formula(token, range)
141
- @worksheet.write_cell_value(result)
133
+ def write_cell(worksheet, row, col)
134
+ worksheet.writer.tag_elements('c', cell_attributes(worksheet, row, col)) do
135
+ worksheet.write_cell_array_formula(token, range)
136
+ worksheet.write_cell_value(result)
142
137
  end
143
138
  end
144
139
  end
145
140
 
146
141
  class DynamicFormulaArrayCellData < CellData # :nodoc:
147
- def initialize(worksheet, row, col, formula, xf, range, result)
148
- @worksheet = worksheet
149
- @row = row
150
- @col = col
142
+ attr_reader :token, :result, :range, :link_type, :url
143
+
144
+ def initialize(formula, xf, range, result)
151
145
  @token = formula
152
146
  @xf = xf
153
147
  @range = range
@@ -158,23 +152,22 @@ module Writexlsx
158
152
  @result || 0
159
153
  end
160
154
 
161
- def write_cell
155
+ def write_cell(worksheet, row, col)
162
156
  # Add metadata linkage for dynamic array formulas.
163
- attributes = cell_attributes
157
+ attributes = cell_attributes(worksheet, row, col)
164
158
  attributes << %w[cm 1]
165
159
 
166
- @worksheet.writer.tag_elements('c', attributes) do
167
- @worksheet.write_cell_array_formula(token, range)
168
- @worksheet.write_cell_value(result)
160
+ worksheet.writer.tag_elements('c', attributes) do
161
+ worksheet.write_cell_array_formula(token, range)
162
+ worksheet.write_cell_value(result)
169
163
  end
170
164
  end
171
165
  end
172
166
 
173
167
  class BooleanCellData < CellData # :nodoc:
174
- def initialize(worksheet, row, col, val, xf)
175
- @worksheet = worksheet
176
- @row = row
177
- @col = col
168
+ attr_reader :token
169
+
170
+ def initialize(val, xf)
178
171
  @token = val
179
172
  @xf = xf
180
173
  end
@@ -183,21 +176,18 @@ module Writexlsx
183
176
  @token
184
177
  end
185
178
 
186
- def write_cell
187
- attributes = cell_attributes
179
+ def write_cell(worksheet, row, col)
180
+ attributes = cell_attributes(worksheet, row, col)
188
181
 
189
182
  attributes << %w[t b]
190
- @worksheet.writer.tag_elements('c', attributes) do
191
- @worksheet.write_cell_value(token)
183
+ worksheet.writer.tag_elements('c', attributes) do
184
+ worksheet.write_cell_value(token)
192
185
  end
193
186
  end
194
187
  end
195
188
 
196
189
  class BlankCellData < CellData # :nodoc:
197
- def initialize(worksheet, row, col, xf)
198
- @worksheet = worksheet
199
- @row = row
200
- @col = col
190
+ def initialize(xf)
201
191
  @xf = xf
202
192
  end
203
193
 
@@ -205,8 +195,8 @@ module Writexlsx
205
195
  ''
206
196
  end
207
197
 
208
- def write_cell
209
- @worksheet.writer.empty_tag('c', cell_attributes)
198
+ def write_cell(worksheet, row, col)
199
+ worksheet.writer.empty_tag('c', cell_attributes(worksheet, row, col))
210
200
  end
211
201
  end
212
202
  end
@@ -1,4 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  module Writexlsx
4
5
  class Worksheet
@@ -1,4 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  module Writexlsx
4
5
  class Worksheet
@@ -99,10 +100,10 @@ module Writexlsx
99
100
 
100
101
  # For external links change the directory separator from Unix to Dos.
101
102
  url = url.gsub(%r{/}, '\\')
102
- str.gsub!(%r{/}, '\\')
103
+ str = str.gsub(%r{/}, '\\')
103
104
 
104
105
  # Strip the mailto header.
105
- str.sub!(/^mailto:/, '')
106
+ str = str.sub(/^mailto:/, '')
106
107
 
107
108
  # Split url into the link and optional anchor/location.
108
109
  url, url_str = url.split(/#/, 2)
@@ -1,4 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  module Writexlsx
4
5
  class Worksheet