caxlsx 3.3.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +25 -7
  4. data/Rakefile +7 -6
  5. data/lib/axlsx/content_type/abstract_content_type.rb +11 -9
  6. data/lib/axlsx/content_type/content_type.rb +7 -9
  7. data/lib/axlsx/content_type/default.rb +4 -6
  8. data/lib/axlsx/content_type/override.rb +3 -5
  9. data/lib/axlsx/doc_props/app.rb +27 -30
  10. data/lib/axlsx/doc_props/core.rb +9 -12
  11. data/lib/axlsx/drawing/area_chart.rb +13 -14
  12. data/lib/axlsx/drawing/area_series.rb +13 -14
  13. data/lib/axlsx/drawing/ax_data_source.rb +3 -6
  14. data/lib/axlsx/drawing/axes.rb +10 -9
  15. data/lib/axlsx/drawing/axis.rb +27 -30
  16. data/lib/axlsx/drawing/bar_3D_chart.rb +17 -18
  17. data/lib/axlsx/drawing/bar_chart.rb +16 -17
  18. data/lib/axlsx/drawing/bar_series.rb +9 -13
  19. data/lib/axlsx/drawing/bubble_chart.rb +8 -9
  20. data/lib/axlsx/drawing/bubble_series.rb +9 -10
  21. data/lib/axlsx/drawing/cat_axis.rb +14 -17
  22. data/lib/axlsx/drawing/chart.rb +25 -28
  23. data/lib/axlsx/drawing/d_lbls.rb +29 -26
  24. data/lib/axlsx/drawing/drawing.rb +60 -62
  25. data/lib/axlsx/drawing/graphic_frame.rb +6 -7
  26. data/lib/axlsx/drawing/hyperlink.rb +12 -13
  27. data/lib/axlsx/drawing/line_3D_chart.rb +13 -15
  28. data/lib/axlsx/drawing/line_chart.rb +13 -14
  29. data/lib/axlsx/drawing/line_series.rb +13 -14
  30. data/lib/axlsx/drawing/marker.rb +14 -16
  31. data/lib/axlsx/drawing/num_data.rb +13 -16
  32. data/lib/axlsx/drawing/num_data_source.rb +11 -13
  33. data/lib/axlsx/drawing/num_val.rb +9 -10
  34. data/lib/axlsx/drawing/one_cell_anchor.rb +10 -10
  35. data/lib/axlsx/drawing/pic.rb +57 -22
  36. data/lib/axlsx/drawing/picture_locking.rb +6 -7
  37. data/lib/axlsx/drawing/pie_3D_chart.rb +6 -9
  38. data/lib/axlsx/drawing/pie_series.rb +9 -12
  39. data/lib/axlsx/drawing/scaling.rb +9 -10
  40. data/lib/axlsx/drawing/scatter_chart.rb +9 -10
  41. data/lib/axlsx/drawing/scatter_series.rb +15 -16
  42. data/lib/axlsx/drawing/ser_axis.rb +9 -11
  43. data/lib/axlsx/drawing/series.rb +8 -8
  44. data/lib/axlsx/drawing/series_title.rb +6 -6
  45. data/lib/axlsx/drawing/str_data.rb +10 -13
  46. data/lib/axlsx/drawing/str_val.rb +8 -9
  47. data/lib/axlsx/drawing/title.rb +23 -27
  48. data/lib/axlsx/drawing/two_cell_anchor.rb +8 -8
  49. data/lib/axlsx/drawing/val_axis.rb +5 -6
  50. data/lib/axlsx/drawing/view_3D.rb +32 -30
  51. data/lib/axlsx/drawing/vml_drawing.rb +19 -20
  52. data/lib/axlsx/drawing/vml_shape.rb +25 -26
  53. data/lib/axlsx/package.rb +81 -79
  54. data/lib/axlsx/rels/relationship.rb +30 -28
  55. data/lib/axlsx/rels/relationships.rb +7 -8
  56. data/lib/axlsx/stylesheet/border.rb +7 -8
  57. data/lib/axlsx/stylesheet/border_pr.rb +8 -8
  58. data/lib/axlsx/stylesheet/cell_alignment.rb +14 -20
  59. data/lib/axlsx/stylesheet/cell_protection.rb +6 -7
  60. data/lib/axlsx/stylesheet/cell_style.rb +12 -14
  61. data/lib/axlsx/stylesheet/color.rb +15 -12
  62. data/lib/axlsx/stylesheet/dxf.rb +7 -9
  63. data/lib/axlsx/stylesheet/fill.rb +3 -5
  64. data/lib/axlsx/stylesheet/font.rb +24 -21
  65. data/lib/axlsx/stylesheet/gradient_fill.rb +9 -9
  66. data/lib/axlsx/stylesheet/gradient_stop.rb +7 -6
  67. data/lib/axlsx/stylesheet/num_fmt.rb +9 -14
  68. data/lib/axlsx/stylesheet/pattern_fill.rb +8 -8
  69. data/lib/axlsx/stylesheet/styles.rb +104 -98
  70. data/lib/axlsx/stylesheet/table_style.rb +8 -9
  71. data/lib/axlsx/stylesheet/table_style_element.rb +7 -8
  72. data/lib/axlsx/stylesheet/table_styles.rb +8 -10
  73. data/lib/axlsx/stylesheet/xf.rb +21 -22
  74. data/lib/axlsx/util/accessors.rb +6 -6
  75. data/lib/axlsx/util/buffered_zip_output_stream.rb +60 -0
  76. data/lib/axlsx/util/constants.rb +119 -108
  77. data/lib/axlsx/util/mime_type_utils.rb +11 -0
  78. data/lib/axlsx/util/options_parser.rb +4 -3
  79. data/lib/axlsx/util/serialized_attributes.rb +45 -21
  80. data/lib/axlsx/util/simple_typed_list.rb +58 -57
  81. data/lib/axlsx/util/storage.rb +38 -41
  82. data/lib/axlsx/util/validators.rb +107 -44
  83. data/lib/axlsx/util/zip_command.rb +10 -12
  84. data/lib/axlsx/version.rb +3 -2
  85. data/lib/axlsx/workbook/defined_name.rb +11 -8
  86. data/lib/axlsx/workbook/defined_names.rb +4 -3
  87. data/lib/axlsx/workbook/shared_strings_table.rb +10 -11
  88. data/lib/axlsx/workbook/workbook.rb +121 -114
  89. data/lib/axlsx/workbook/workbook_view.rb +8 -11
  90. data/lib/axlsx/workbook/workbook_views.rb +4 -4
  91. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +72 -14
  92. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +11 -7
  93. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +24 -21
  94. data/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb +51 -0
  95. data/lib/axlsx/workbook/worksheet/auto_filter/sort_state.rb +56 -0
  96. data/lib/axlsx/workbook/worksheet/border_creator.rb +30 -25
  97. data/lib/axlsx/workbook/worksheet/break.rb +4 -5
  98. data/lib/axlsx/workbook/worksheet/cell.rb +92 -65
  99. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +32 -28
  100. data/lib/axlsx/workbook/worksheet/cfvo.rb +7 -5
  101. data/lib/axlsx/workbook/worksheet/cfvos.rb +5 -5
  102. data/lib/axlsx/workbook/worksheet/col.rb +9 -10
  103. data/lib/axlsx/workbook/worksheet/col_breaks.rb +8 -7
  104. data/lib/axlsx/workbook/worksheet/color_scale.rb +16 -16
  105. data/lib/axlsx/workbook/worksheet/cols.rb +9 -7
  106. data/lib/axlsx/workbook/worksheet/comment.rb +12 -11
  107. data/lib/axlsx/workbook/worksheet/comments.rb +10 -12
  108. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +12 -8
  109. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +19 -21
  110. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +5 -5
  111. data/lib/axlsx/workbook/worksheet/data_bar.rb +29 -30
  112. data/lib/axlsx/workbook/worksheet/data_validation.rb +34 -33
  113. data/lib/axlsx/workbook/worksheet/data_validations.rb +5 -6
  114. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +8 -8
  115. data/lib/axlsx/workbook/worksheet/dimension.rb +9 -6
  116. data/lib/axlsx/workbook/worksheet/header_footer.rb +4 -3
  117. data/lib/axlsx/workbook/worksheet/icon_set.rb +24 -8
  118. data/lib/axlsx/workbook/worksheet/merged_cells.rb +10 -10
  119. data/lib/axlsx/workbook/worksheet/outline_pr.rb +6 -3
  120. data/lib/axlsx/workbook/worksheet/page_margins.rb +17 -12
  121. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +6 -4
  122. data/lib/axlsx/workbook/worksheet/page_setup.rb +128 -129
  123. data/lib/axlsx/workbook/worksheet/pane.rb +27 -26
  124. data/lib/axlsx/workbook/worksheet/pivot_table.rb +23 -25
  125. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +12 -13
  126. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +5 -4
  127. data/lib/axlsx/workbook/worksheet/print_options.rb +3 -2
  128. data/lib/axlsx/workbook/worksheet/protected_range.rb +6 -5
  129. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +12 -10
  130. data/lib/axlsx/workbook/worksheet/rich_text.rb +6 -6
  131. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +35 -17
  132. data/lib/axlsx/workbook/worksheet/row.rb +30 -22
  133. data/lib/axlsx/workbook/worksheet/row_breaks.rb +8 -7
  134. data/lib/axlsx/workbook/worksheet/selection.rb +16 -16
  135. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +10 -7
  136. data/lib/axlsx/workbook/worksheet/sheet_data.rb +7 -7
  137. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +23 -19
  138. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +11 -7
  139. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +21 -20
  140. data/lib/axlsx/workbook/worksheet/sheet_view.rb +48 -53
  141. data/lib/axlsx/workbook/worksheet/table.rb +13 -13
  142. data/lib/axlsx/workbook/worksheet/table_style_info.rb +6 -5
  143. data/lib/axlsx/workbook/worksheet/tables.rb +7 -5
  144. data/lib/axlsx/workbook/worksheet/worksheet.rb +92 -63
  145. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +10 -8
  146. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +11 -4
  147. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +9 -8
  148. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +7 -5
  149. data/lib/axlsx.rb +75 -47
  150. data/lib/caxlsx.rb +3 -2
  151. metadata +50 -44
data/lib/axlsx/package.rb CHANGED
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
4
  # Package is responsible for managing all the bits and peices that Open Office XML requires to make a valid
4
5
  # xlsx document including validation and serialization.
@@ -20,7 +21,7 @@ module Axlsx
20
21
  # @option options [Time] :created_at Timestamp in the document properties (defaults to current time).
21
22
  # @option options [Boolean] :use_shared_strings This is passed to the workbook to specify that shared strings should be used when serializing the package.
22
23
  # @example Package.new :author => 'you!', :workbook => Workbook.new
23
- def initialize(options={})
24
+ def initialize(options = {})
24
25
  @workbook = nil
25
26
  @core, @app = Core.new, App.new
26
27
  @core.creator = options[:author] || @core.creator
@@ -32,12 +33,10 @@ module Axlsx
32
33
  # Shortcut to specify that the workbook should use autowidth
33
34
  # @see Workbook#use_autowidth
34
35
  def use_autowidth=(v)
35
- Axlsx::validate_boolean(v);
36
+ Axlsx.validate_boolean(v)
36
37
  workbook.use_autowidth = v
37
38
  end
38
39
 
39
-
40
-
41
40
  # Shortcut to determine if the workbook is configured to use shared strings
42
41
  # @see Workbook#use_shared_strings
43
42
  def use_shared_strings
@@ -47,9 +46,10 @@ module Axlsx
47
46
  # Shortcut to specify that the workbook should use shared strings
48
47
  # @see Workbook#use_shared_strings
49
48
  def use_shared_strings=(v)
50
- Axlsx::validate_boolean(v);
49
+ Axlsx.validate_boolean(v)
51
50
  workbook.use_shared_strings = v
52
51
  end
52
+
53
53
  # The workbook this package will serialize or validate.
54
54
  # @return [Workbook] If no workbook instance has been assigned with this package a new Workbook instance is returned.
55
55
  # @raise ArgumentError if workbook parameter is not a Workbook instance.
@@ -101,16 +101,17 @@ module Axlsx
101
101
  # s = p.to_stream()
102
102
  # File.open('example_streamed.xlsx', 'wb') { |f| f.write(s.read) }
103
103
  def serialize(output, options = {}, secondary_options = nil)
104
- if !workbook.styles_applied
105
- workbook.apply_styles
104
+ unless workbook.styles_applied
105
+ workbook.apply_styles
106
106
  end
107
107
 
108
108
  confirm_valid, zip_command = parse_serialize_options(options, secondary_options)
109
- return false unless !confirm_valid || self.validate.empty?
109
+ return false unless !confirm_valid || validate.empty?
110
+
110
111
  zip_provider = if zip_command
111
112
  ZipCommand.new(zip_command)
112
113
  else
113
- Zip::OutputStream
114
+ BufferedZipOutputStream
114
115
  end
115
116
  Relationship.initialize_ids_cache
116
117
  zip_provider.open(output) do |zip|
@@ -121,19 +122,20 @@ module Axlsx
121
122
  Relationship.clear_ids_cache
122
123
  end
123
124
 
124
-
125
125
  # Serialize your workbook to a StringIO instance
126
126
  # @param [Boolean] confirm_valid Validate the package prior to serialization.
127
127
  # @return [StringIO|Boolean] False if confirm_valid and validation errors exist. rewound string IO if not.
128
- def to_stream(confirm_valid=false)
129
- if !workbook.styles_applied
130
- workbook.apply_styles
128
+ def to_stream(confirm_valid = false)
129
+ unless workbook.styles_applied
130
+ workbook.apply_styles
131
131
  end
132
132
 
133
- return false unless !confirm_valid || self.validate.empty?
133
+ return false unless !confirm_valid || validate.empty?
134
+
134
135
  Relationship.initialize_ids_cache
135
- zip = write_parts(Zip::OutputStream.new(StringIO.new.binmode, true))
136
- stream = zip.close_buffer
136
+ stream = BufferedZipOutputStream.write_buffer do |zip|
137
+ write_parts(zip)
138
+ end
137
139
  stream.rewind
138
140
  stream
139
141
  ensure
@@ -143,7 +145,7 @@ module Axlsx
143
145
  # Encrypt the package into a CFB using the password provided
144
146
  # This is not ready yet
145
147
  def encrypt(file_name, password)
146
- return false
148
+ false
147
149
  # moc = MsOffCrypto.new(file_name, password)
148
150
  # moc.save
149
151
  end
@@ -157,7 +159,7 @@ module Axlsx
157
159
  #
158
160
  # If by chance you are able to create a package that does not validate it indicates that the internal
159
161
  # validation is not robust enough and needs to be improved. Please report your errors to the gem author.
160
- # @see http://www.ecma-international.org/publications/standards/Ecma-376.htm
162
+ # @see https://www.ecma-international.org/publications-and-standards/standards/ecma-376/
161
163
  # @example
162
164
  # # The following will output any error messages found in serialization.
163
165
  # p = Axlsx::Package.new
@@ -187,7 +189,7 @@ module Axlsx
187
189
  end
188
190
  unless part[:path].nil?
189
191
  zip.put_next_entry(zip_entry_for_part(part))
190
- zip.write IO.read(part[:path], mode: "rb")
192
+ zip.write File.read(part[:path], mode: "rb")
191
193
  end
192
194
  end
193
195
  zip
@@ -214,58 +216,57 @@ module Axlsx
214
216
  # @private
215
217
  def parts
216
218
  parts = [
217
- {:entry => "xl/#{STYLES_PN}", :doc => workbook.styles, :schema => SML_XSD},
218
- {:entry => CORE_PN, :doc => @core, :schema => CORE_XSD},
219
- {:entry => APP_PN, :doc => @app, :schema => APP_XSD},
220
- {:entry => WORKBOOK_RELS_PN, :doc => workbook.relationships, :schema => RELS_XSD},
221
- {:entry => WORKBOOK_PN, :doc => workbook, :schema => SML_XSD}
219
+ { entry: "xl/#{STYLES_PN}", doc: workbook.styles, schema: SML_XSD },
220
+ { entry: CORE_PN, doc: @core, schema: CORE_XSD },
221
+ { entry: APP_PN, doc: @app, schema: APP_XSD },
222
+ { entry: WORKBOOK_RELS_PN, doc: workbook.relationships, schema: RELS_XSD },
223
+ { entry: WORKBOOK_PN, doc: workbook, schema: SML_XSD }
222
224
  ]
223
225
 
224
226
  workbook.drawings.each do |drawing|
225
- parts << {:entry => "xl/#{drawing.rels_pn}", :doc => drawing.relationships, :schema => RELS_XSD}
226
- parts << {:entry => "xl/#{drawing.pn}", :doc => drawing, :schema => DRAWING_XSD}
227
+ parts << { entry: "xl/#{drawing.rels_pn}", doc: drawing.relationships, schema: RELS_XSD }
228
+ parts << { entry: "xl/#{drawing.pn}", doc: drawing, schema: DRAWING_XSD }
227
229
  end
228
230
 
229
-
230
231
  workbook.tables.each do |table|
231
- parts << {:entry => "xl/#{table.pn}", :doc => table, :schema => SML_XSD}
232
+ parts << { entry: "xl/#{table.pn}", doc: table, schema: SML_XSD }
232
233
  end
233
234
  workbook.pivot_tables.each do |pivot_table|
234
235
  cache_definition = pivot_table.cache_definition
235
- parts << {:entry => "xl/#{pivot_table.rels_pn}", :doc => pivot_table.relationships, :schema => RELS_XSD}
236
- parts << {:entry => "xl/#{pivot_table.pn}", :doc => pivot_table} #, :schema => SML_XSD}
237
- parts << {:entry => "xl/#{cache_definition.pn}", :doc => cache_definition} #, :schema => SML_XSD}
236
+ parts << { entry: "xl/#{pivot_table.rels_pn}", doc: pivot_table.relationships, schema: RELS_XSD }
237
+ parts << { entry: "xl/#{pivot_table.pn}", doc: pivot_table } # , :schema => SML_XSD}
238
+ parts << { entry: "xl/#{cache_definition.pn}", doc: cache_definition } # , :schema => SML_XSD}
238
239
  end
239
240
 
240
- workbook.comments.each do|comment|
241
- if comment.size > 0
242
- parts << { :entry => "xl/#{comment.pn}", :doc => comment, :schema => SML_XSD }
243
- parts << { :entry => "xl/#{comment.vml_drawing.pn}", :doc => comment.vml_drawing, :schema => nil }
241
+ workbook.comments.each do |comment|
242
+ unless comment.empty?
243
+ parts << { entry: "xl/#{comment.pn}", doc: comment, schema: SML_XSD }
244
+ parts << { entry: "xl/#{comment.vml_drawing.pn}", doc: comment.vml_drawing, schema: nil }
244
245
  end
245
246
  end
246
247
 
247
248
  workbook.charts.each do |chart|
248
- parts << {:entry => "xl/#{chart.pn}", :doc => chart, :schema => DRAWING_XSD}
249
+ parts << { entry: "xl/#{chart.pn}", doc: chart, schema: DRAWING_XSD }
249
250
  end
250
251
 
251
252
  workbook.images.each do |image|
252
- parts << {:entry => "xl/#{image.pn}", :path => image.image_src}
253
+ parts << { entry: "xl/#{image.pn}", path: image.image_src } unless image.remote?
253
254
  end
254
255
 
255
256
  if use_shared_strings
256
- parts << {:entry => "xl/#{SHARED_STRINGS_PN}", :doc => workbook.shared_strings, :schema => SML_XSD}
257
+ parts << { entry: "xl/#{SHARED_STRINGS_PN}", doc: workbook.shared_strings, schema: SML_XSD }
257
258
  end
258
259
 
259
260
  workbook.worksheets.each do |sheet|
260
- parts << {:entry => "xl/#{sheet.rels_pn}", :doc => sheet.relationships, :schema => RELS_XSD}
261
- parts << {:entry => "xl/#{sheet.pn}", :doc => sheet, :schema => SML_XSD}
261
+ parts << { entry: "xl/#{sheet.rels_pn}", doc: sheet.relationships, schema: RELS_XSD }
262
+ parts << { entry: "xl/#{sheet.pn}", doc: sheet, schema: SML_XSD }
262
263
  end
263
264
 
264
265
  # Sort parts for correct MIME detection
265
266
  [
266
- {:entry => CONTENT_TYPES_PN, :doc => content_types, :schema => CONTENT_TYPES_XSD},
267
- {:entry => RELS_PN, :doc => relationships, :schema => RELS_XSD},
268
- *(parts.sort_by { |part| part[:entry] }.reverse)
267
+ { entry: CONTENT_TYPES_PN, doc: content_types, schema: CONTENT_TYPES_XSD },
268
+ { entry: RELS_PN, doc: relationships, schema: RELS_XSD },
269
+ *parts.sort_by { |part| part[:entry] }.reverse
269
270
  ]
270
271
  end
271
272
 
@@ -291,56 +292,56 @@ module Axlsx
291
292
  def content_types
292
293
  c_types = base_content_types
293
294
  workbook.drawings.each do |drawing|
294
- c_types << Axlsx::Override.new(:PartName => "/xl/#{drawing.pn}",
295
- :ContentType => DRAWING_CT)
295
+ c_types << Axlsx::Override.new(PartName: "/xl/#{drawing.pn}",
296
+ ContentType: DRAWING_CT)
296
297
  end
297
298
 
298
299
  workbook.charts.each do |chart|
299
- c_types << Axlsx::Override.new(:PartName => "/xl/#{chart.pn}",
300
- :ContentType => CHART_CT)
300
+ c_types << Axlsx::Override.new(PartName: "/xl/#{chart.pn}",
301
+ ContentType: CHART_CT)
301
302
  end
302
303
 
303
304
  workbook.tables.each do |table|
304
- c_types << Axlsx::Override.new(:PartName => "/xl/#{table.pn}",
305
- :ContentType => TABLE_CT)
305
+ c_types << Axlsx::Override.new(PartName: "/xl/#{table.pn}",
306
+ ContentType: TABLE_CT)
306
307
  end
307
308
 
308
309
  workbook.pivot_tables.each do |pivot_table|
309
- c_types << Axlsx::Override.new(:PartName => "/xl/#{pivot_table.pn}",
310
- :ContentType => PIVOT_TABLE_CT)
311
- c_types << Axlsx::Override.new(:PartName => "/xl/#{pivot_table.cache_definition.pn}",
312
- :ContentType => PIVOT_TABLE_CACHE_DEFINITION_CT)
310
+ c_types << Axlsx::Override.new(PartName: "/xl/#{pivot_table.pn}",
311
+ ContentType: PIVOT_TABLE_CT)
312
+ c_types << Axlsx::Override.new(PartName: "/xl/#{pivot_table.cache_definition.pn}",
313
+ ContentType: PIVOT_TABLE_CACHE_DEFINITION_CT)
313
314
  end
314
315
 
315
316
  workbook.comments.each do |comment|
316
- if comment.size > 0
317
- c_types << Axlsx::Override.new(:PartName => "/xl/#{comment.pn}",
318
- :ContentType => COMMENT_CT)
317
+ unless comment.empty?
318
+ c_types << Axlsx::Override.new(PartName: "/xl/#{comment.pn}",
319
+ ContentType: COMMENT_CT)
319
320
  end
320
321
  end
321
322
 
322
- if workbook.comments.size > 0
323
- c_types << Axlsx::Default.new(:Extension => "vml", :ContentType => VML_DRAWING_CT)
323
+ unless workbook.comments.empty?
324
+ c_types << Axlsx::Default.new(Extension: "vml", ContentType: VML_DRAWING_CT)
324
325
  end
325
326
 
326
327
  workbook.worksheets.each do |sheet|
327
- c_types << Axlsx::Override.new(:PartName => "/xl/#{sheet.pn}",
328
- :ContentType => WORKSHEET_CT)
328
+ c_types << Axlsx::Override.new(PartName: "/xl/#{sheet.pn}",
329
+ ContentType: WORKSHEET_CT)
329
330
  end
330
331
  exts = workbook.images.map { |image| image.extname.downcase }
331
332
  exts.uniq.each do |ext|
332
- ct = if ['jpeg', 'jpg'].include?(ext)
333
+ ct = if JPEG_EXS.include?(ext)
333
334
  JPEG_CT
334
- elsif ext == 'gif'
335
+ elsif ext == GIF_EX
335
336
  GIF_CT
336
- elsif ext == 'png'
337
+ elsif ext == PNG_EX
337
338
  PNG_CT
338
339
  end
339
- c_types << Axlsx::Default.new(:ContentType => ct, :Extension => ext )
340
+ c_types << Axlsx::Default.new(ContentType: ct, Extension: ext)
340
341
  end
341
342
  if use_shared_strings
342
- c_types << Axlsx::Override.new(:PartName => "/xl/#{SHARED_STRINGS_PN}",
343
- :ContentType => SHARED_STRINGS_CT)
343
+ c_types << Axlsx::Override.new(PartName: "/xl/#{SHARED_STRINGS_PN}",
344
+ ContentType: SHARED_STRINGS_CT)
344
345
  end
345
346
  c_types
346
347
  end
@@ -349,13 +350,13 @@ module Axlsx
349
350
  # @return [ContentType]
350
351
  # @private
351
352
  def base_content_types
352
- c_types = ContentType.new()
353
- c_types << Default.new(:ContentType => RELS_CT, :Extension => RELS_EX)
354
- c_types << Default.new(:Extension => XML_EX, :ContentType => XML_CT)
355
- c_types << Override.new(:PartName => "/#{APP_PN}", :ContentType => APP_CT)
356
- c_types << Override.new(:PartName => "/#{CORE_PN}", :ContentType => CORE_CT)
357
- c_types << Override.new(:PartName => "/xl/#{STYLES_PN}", :ContentType => STYLES_CT)
358
- c_types << Axlsx::Override.new(:PartName => "/#{WORKBOOK_PN}", :ContentType => WORKBOOK_CT)
353
+ c_types = ContentType.new
354
+ c_types << Default.new(ContentType: RELS_CT, Extension: RELS_EX)
355
+ c_types << Default.new(Extension: XML_EX, ContentType: XML_CT)
356
+ c_types << Override.new(PartName: "/#{APP_PN}", ContentType: APP_CT)
357
+ c_types << Override.new(PartName: "/#{CORE_PN}", ContentType: CORE_CT)
358
+ c_types << Override.new(PartName: "/xl/#{STYLES_PN}", ContentType: STYLES_CT)
359
+ c_types << Axlsx::Override.new(PartName: "/#{WORKBOOK_PN}", ContentType: WORKBOOK_CT)
359
360
  c_types.lock
360
361
  c_types
361
362
  end
@@ -378,19 +379,20 @@ module Axlsx
378
379
  # @private
379
380
  def parse_serialize_options(options, secondary_options)
380
381
  if secondary_options
381
- warn "[DEPRECATION] Axlsx::Package#serialize with 3 arguments is deprecated. " +
382
- "Use keyword args instead e.g., package.serialize(output, confirm_valid: false, zip_command: 'zip')"
382
+ warn "[DEPRECATION] Axlsx::Package#serialize with 3 arguments is deprecated. " \
383
+ "Use keyword args instead e.g., package.serialize(output, confirm_valid: false, zip_command: 'zip')"
383
384
  end
384
385
  if options.is_a?(Hash)
385
386
  options.merge!(secondary_options || {})
386
387
  invalid_keys = options.keys - [:confirm_valid, :zip_command]
387
388
  if invalid_keys.any?
388
- raise ArgumentError.new("Invalid keyword arguments: #{invalid_keys}")
389
+ raise ArgumentError, "Invalid keyword arguments: #{invalid_keys}"
389
390
  end
391
+
390
392
  [options.fetch(:confirm_valid, false), options.fetch(:zip_command, nil)]
391
393
  else
392
- warn "[DEPRECATION] Axlsx::Package#serialize with confirm_valid as a boolean is deprecated. " +
393
- "Use keyword args instead e.g., package.serialize(output, confirm_valid: false)"
394
+ warn "[DEPRECATION] Axlsx::Package#serialize with confirm_valid as a boolean is deprecated. " \
395
+ "Use keyword args instead e.g., package.serialize(output, confirm_valid: false)"
394
396
  parse_serialize_options((secondary_options || {}).merge(confirm_valid: options), nil)
395
397
  end
396
398
  end
@@ -1,9 +1,9 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
4
  # A relationship defines a reference between package parts.
4
5
  # @note Packages automatically manage relationships.
5
6
  class Relationship
6
-
7
7
  class << self
8
8
  # Keeps track of relationship ids in use.
9
9
  # @return [Array]
@@ -12,27 +12,27 @@ module Axlsx
12
12
  end
13
13
 
14
14
  # Initialize cached ids.
15
- #
15
+ #
16
16
  # This should be called before serializing a package (see {Package#serialize} and
17
- # {Package#to_stream}) to make sure that serialization is idempotent (i.e.
17
+ # {Package#to_stream}) to make sure that serialization is idempotent (i.e.
18
18
  # Relationship instances are generated with the same IDs everytime the package
19
19
  # is serialized).
20
20
  def initialize_ids_cache
21
21
  Thread.current[:axlsx_relationship_ids_cache] = {}
22
22
  end
23
-
23
+
24
24
  # Clear cached ids.
25
- #
25
+ #
26
26
  # This should be called after serializing a package (see {Package#serialize} and
27
27
  # {Package#to_stream}) to free the memory allocated for cache.
28
- #
29
- # Also, calling this avoids memory leaks (cached ids lingering around
30
- # forever).
28
+ #
29
+ # Also, calling this avoids memory leaks (cached ids lingering around
30
+ # forever).
31
31
  def clear_ids_cache
32
32
  Thread.current[:axlsx_relationship_ids_cache] = nil
33
33
  end
34
-
35
- # Generate and return a unique id (eg. `rId123`) Used for setting {#Id}.
34
+
35
+ # Generate and return a unique id (eg. `rId123`) Used for setting {#Id}.
36
36
  #
37
37
  # The generated id depends on the number of previously cached ids, so using
38
38
  # {clear_ids_cache} will automatically reset the generated ids, too.
@@ -42,12 +42,12 @@ module Axlsx
42
42
  end
43
43
  end
44
44
 
45
- # The id of the relationship (eg. "rId123"). Most instances get their own unique id.
46
- # However, some instances need to share the same id – see {#should_use_same_id_as?}
45
+ # The id of the relationship (eg. "rId123"). Most instances get their own unique id.
46
+ # However, some instances need to share the same id – see {#ids_cache_key}
47
47
  # for details.
48
48
  # @return [String]
49
49
  attr_reader :Id
50
-
50
+
51
51
  # The location of the relationship target
52
52
  # @return [String]
53
53
  attr_reader :Target
@@ -77,24 +77,24 @@ module Axlsx
77
77
  # The source object the relations belongs to (e.g. a hyperlink, drawing, ...). Needed when
78
78
  # looking up the relationship for a specific object (see {Relationships#for}).
79
79
  attr_reader :source_obj
80
-
81
- # Initializes a new relationship.
80
+
81
+ # Initializes a new relationship.
82
82
  # @param [Object] source_obj see {#source_obj}
83
83
  # @param [String] type The type of the relationship
84
84
  # @param [String] target The target for the relationship
85
85
  # @option [Symbol] :target_mode only accepts :external.
86
- def initialize(source_obj, type, target, options={})
86
+ def initialize(source_obj, type, target, options = {})
87
87
  @source_obj = source_obj
88
- self.Target=target
89
- self.Type=type
88
+ self.Target = target
89
+ self.Type = type
90
90
  self.TargetMode = options[:target_mode] if options[:target_mode]
91
91
  @Id = (self.class.ids_cache[ids_cache_key] ||= self.class.next_free_id)
92
92
  end
93
93
 
94
94
  # @see Target
95
- def Target=(v) Axlsx::validate_string v; @Target = v end
95
+ def Target=(v) Axlsx.validate_string v; @Target = v end
96
96
  # @see Type
97
- def Type=(v) Axlsx::validate_relationship_type v; @Type = v end
97
+ def Type=(v) Axlsx.validate_relationship_type v; @Type = v end
98
98
 
99
99
  # @see TargetMode
100
100
  def TargetMode=(v) RestrictionValidator.validate 'Relationship.TargetMode', [:External, :Internal], v; @TargetMode = v; end
@@ -102,18 +102,21 @@ module Axlsx
102
102
  # serialize relationship
103
103
  # @param [String] str
104
104
  # @return [String]
105
- def to_xml_string(str = '')
106
- h = Axlsx.instance_values_for(self).reject{|k, _| k == "source_obj"}
105
+ def to_xml_string(str = +'')
106
+ h = Axlsx.instance_values_for(self).reject { |k, _| k == "source_obj" }
107
107
  str << '<Relationship '
108
- str << (h.map { |key, value| '' << key.to_s << '="' << Axlsx::coder.encode(value.to_s) << '"'}.join(' '))
108
+ h.each_with_index do |key_value, index|
109
+ str << ' ' unless index.zero?
110
+ str << key_value.first.to_s << '="' << Axlsx.coder.encode(key_value.last.to_s) << '"'
111
+ end
109
112
  str << '/>'
110
113
  end
111
-
114
+
112
115
  # A key that determines whether this relationship should use already generated id.
113
116
  #
114
117
  # Instances designating the same relationship need to use the same id. We can not simply
115
- # compare the {#Target} attribute, though: `foo/bar.xml`, `../foo/bar.xml`,
116
- # `../../foo/bar.xml` etc. are all different but probably mean the same file (this
118
+ # compare the {#Target} attribute, though: `foo/bar.xml`, `../foo/bar.xml`,
119
+ # `../../foo/bar.xml` etc. are all different but probably mean the same file (this
117
120
  # is especially an issue for relationships in the context of pivot tables). So lets
118
121
  # just ignore this attribute for now (except when {#TargetMode} is set to `:External` –
119
122
  # then {#Target} will be an absolute URL and thus can safely be compared).
@@ -125,6 +128,5 @@ module Axlsx
125
128
  key << self.Target if self.TargetMode == :External
126
129
  key
127
130
  end
128
-
129
131
  end
130
132
  end
@@ -1,11 +1,11 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
- require 'axlsx/rels/relationship.rb'
4
+ require 'axlsx/rels/relationship'
4
5
 
5
6
  # Relationships are a collection of Relations that define how package parts are related.
6
7
  # @note The package automatically manages releationships.
7
8
  class Relationships < SimpleTypedList
8
-
9
9
  # Creates a new Relationships collection based on SimpleTypedList
10
10
  def initialize
11
11
  super Relationship
@@ -15,18 +15,17 @@ require 'axlsx/rels/relationship.rb'
15
15
  # @see Relationship#source_obj
16
16
  # @return [Relationship]
17
17
  def for(source_obj)
18
- find{ |rel| rel.source_obj == source_obj }
18
+ find { |rel| rel.source_obj == source_obj }
19
19
  end
20
20
 
21
21
  # serialize relationships
22
22
  # @param [String] str
23
23
  # @return [String]
24
- def to_xml_string(str = '')
24
+ def to_xml_string(str = +'')
25
25
  str << '<?xml version="1.0" encoding="UTF-8"?>'
26
- str << ('<Relationships xmlns="' << RELS_R << '">')
27
- each{ |rel| rel.to_xml_string(str) }
26
+ str << '<Relationships xmlns="' << RELS_R << '">'
27
+ each { |rel| rel.to_xml_string(str) }
28
28
  str << '</Relationships>'
29
29
  end
30
-
31
30
  end
32
31
  end
@@ -1,8 +1,8 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
4
  # This class details a border used in Office Open XML spreadsheet styles.
4
5
  class Border
5
-
6
6
  include Axlsx::SerializedAttributes
7
7
  include Axlsx::OptionsParser
8
8
 
@@ -21,7 +21,7 @@ module Axlsx
21
21
  #
22
22
  # @note The recommended way to manage borders is with Style#add_style
23
23
  # @see Style#add_style
24
- def initialize(options={})
24
+ def initialize(options = {})
25
25
  @prs = SimpleTypedList.new BorderPr
26
26
  parse_options options
27
27
  end
@@ -43,20 +43,20 @@ module Axlsx
43
43
  attr_reader :prs
44
44
 
45
45
  # @see diagonalUp
46
- def diagonal_up=(v) Axlsx::validate_boolean v; @diagonal_up = v end
46
+ def diagonal_up=(v) Axlsx.validate_boolean v; @diagonal_up = v end
47
47
  alias :diagonalUp= :diagonal_up=
48
48
 
49
49
  # @see diagonalDown
50
- def diagonal_down=(v) Axlsx::validate_boolean v; @diagonal_down = v end
50
+ def diagonal_down=(v) Axlsx.validate_boolean v; @diagonal_down = v end
51
51
  alias :diagonalDown= :diagonal_down=
52
52
 
53
53
  # @see outline
54
- def outline=(v) Axlsx::validate_boolean v; @outline = v end
54
+ def outline=(v) Axlsx.validate_boolean v; @outline = v end
55
55
 
56
56
  # Serializes the object
57
57
  # @param [String] str
58
58
  # @return [String]
59
- def to_xml_string(str = '')
59
+ def to_xml_string(str = +'')
60
60
  str << '<border '
61
61
  serialized_attributes str
62
62
  str << '>'
@@ -68,6 +68,5 @@ module Axlsx
68
68
  end
69
69
  str << '</border>'
70
70
  end
71
-
72
71
  end
73
72
  end
@@ -1,4 +1,5 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
4
  # A border part.
4
5
  class BorderPr
@@ -44,11 +45,11 @@ module Axlsx
44
45
  # @option options [Symbol] name
45
46
  # @option options [Symbol] style
46
47
  # @see Axlsx::Border
47
- def initialize(options={})
48
+ def initialize(options = {})
48
49
  parse_options(options)
49
- #options.each do |o|
50
+ # options.each do |o|
50
51
  # self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
51
- #end
52
+ # end
52
53
  end
53
54
 
54
55
  # @see name
@@ -61,11 +62,10 @@ module Axlsx
61
62
  # Serializes the object
62
63
  # @param [String] str
63
64
  # @return [String]
64
- def to_xml_string(str = '')
65
- str << ('<' << @name.to_s << ' style="' << @style.to_s << '">')
65
+ def to_xml_string(str = +'')
66
+ str << '<' << @name.to_s << ' style="' << @style.to_s << '">'
66
67
  @color.to_xml_string(str) if @color.is_a?(Color)
67
- str << ('</' << @name.to_s << '>')
68
+ str << '</' << @name.to_s << '>'
68
69
  end
69
-
70
70
  end
71
71
  end