caxlsx 3.3.0 → 4.5.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 (155) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +87 -11
  3. data/README.md +43 -16
  4. data/Rakefile +5 -11
  5. data/examples/generate.rb +3 -1
  6. data/lib/axlsx/content_type/abstract_content_type.rb +15 -10
  7. data/lib/axlsx/content_type/content_type.rb +8 -10
  8. data/lib/axlsx/content_type/default.rb +7 -6
  9. data/lib/axlsx/content_type/override.rb +6 -5
  10. data/lib/axlsx/doc_props/app.rb +96 -32
  11. data/lib/axlsx/doc_props/core.rb +9 -12
  12. data/lib/axlsx/drawing/area_chart.rb +16 -17
  13. data/lib/axlsx/drawing/area_series.rb +23 -18
  14. data/lib/axlsx/drawing/ax_data_source.rb +4 -7
  15. data/lib/axlsx/drawing/axes.rb +11 -10
  16. data/lib/axlsx/drawing/axis.rb +83 -36
  17. data/lib/axlsx/drawing/bar_3D_chart.rb +30 -21
  18. data/lib/axlsx/drawing/bar_chart.rb +29 -20
  19. data/lib/axlsx/drawing/bar_series.rb +23 -18
  20. data/lib/axlsx/drawing/bubble_chart.rb +9 -10
  21. data/lib/axlsx/drawing/bubble_series.rb +11 -12
  22. data/lib/axlsx/drawing/cat_axis.rb +38 -28
  23. data/lib/axlsx/drawing/chart.rb +52 -30
  24. data/lib/axlsx/drawing/d_lbls.rb +36 -32
  25. data/lib/axlsx/drawing/drawing.rb +61 -62
  26. data/lib/axlsx/drawing/graphic_frame.rb +6 -7
  27. data/lib/axlsx/drawing/hyperlink.rb +22 -14
  28. data/lib/axlsx/drawing/line_3D_chart.rb +15 -17
  29. data/lib/axlsx/drawing/line_chart.rb +16 -17
  30. data/lib/axlsx/drawing/line_series.rb +23 -18
  31. data/lib/axlsx/drawing/marker.rb +29 -16
  32. data/lib/axlsx/drawing/num_data.rb +13 -16
  33. data/lib/axlsx/drawing/num_data_source.rb +11 -13
  34. data/lib/axlsx/drawing/num_val.rb +10 -11
  35. data/lib/axlsx/drawing/one_cell_anchor.rb +16 -10
  36. data/lib/axlsx/drawing/pic.rb +68 -24
  37. data/lib/axlsx/drawing/picture_locking.rb +6 -7
  38. data/lib/axlsx/drawing/pie_3D_chart.rb +9 -12
  39. data/lib/axlsx/drawing/pie_chart.rb +36 -0
  40. data/lib/axlsx/drawing/pie_series.rb +26 -17
  41. data/lib/axlsx/drawing/scaling.rb +26 -13
  42. data/lib/axlsx/drawing/scatter_chart.rb +10 -11
  43. data/lib/axlsx/drawing/scatter_series.rb +17 -18
  44. data/lib/axlsx/drawing/ser_axis.rb +22 -19
  45. data/lib/axlsx/drawing/series.rb +14 -8
  46. data/lib/axlsx/drawing/series_title.rb +6 -6
  47. data/lib/axlsx/drawing/str_data.rb +10 -13
  48. data/lib/axlsx/drawing/str_val.rb +8 -9
  49. data/lib/axlsx/drawing/title.rb +23 -27
  50. data/lib/axlsx/drawing/two_cell_anchor.rb +9 -9
  51. data/lib/axlsx/drawing/val_axis.rb +7 -8
  52. data/lib/axlsx/drawing/view_3D.rb +38 -30
  53. data/lib/axlsx/drawing/vml_drawing.rb +19 -20
  54. data/lib/axlsx/drawing/vml_shape.rb +25 -26
  55. data/lib/axlsx/package.rb +134 -99
  56. data/lib/axlsx/rels/relationship.rb +42 -30
  57. data/lib/axlsx/rels/relationships.rb +9 -10
  58. data/lib/axlsx/stylesheet/border.rb +16 -8
  59. data/lib/axlsx/stylesheet/border_pr.rb +24 -12
  60. data/lib/axlsx/stylesheet/cell_alignment.rb +44 -21
  61. data/lib/axlsx/stylesheet/cell_protection.rb +13 -7
  62. data/lib/axlsx/stylesheet/cell_style.rb +36 -15
  63. data/lib/axlsx/stylesheet/color.rb +23 -14
  64. data/lib/axlsx/stylesheet/dxf.rb +36 -15
  65. data/lib/axlsx/stylesheet/fill.rb +7 -6
  66. data/lib/axlsx/stylesheet/font.rb +71 -22
  67. data/lib/axlsx/stylesheet/gradient_fill.rb +16 -10
  68. data/lib/axlsx/stylesheet/gradient_stop.rb +14 -6
  69. data/lib/axlsx/stylesheet/num_fmt.rb +15 -14
  70. data/lib/axlsx/stylesheet/pattern_fill.rb +20 -8
  71. data/lib/axlsx/stylesheet/styles.rb +167 -118
  72. data/lib/axlsx/stylesheet/table_style.rb +20 -10
  73. data/lib/axlsx/stylesheet/table_style_element.rb +16 -8
  74. data/lib/axlsx/stylesheet/table_styles.rb +16 -11
  75. data/lib/axlsx/stylesheet/theme.rb +163 -0
  76. data/lib/axlsx/stylesheet/xf.rb +78 -25
  77. data/lib/axlsx/util/accessors.rb +13 -11
  78. data/lib/axlsx/util/buffered_zip_output_stream.rb +64 -0
  79. data/lib/axlsx/util/constants.rb +133 -110
  80. data/lib/axlsx/util/mime_type_utils.rb +75 -5
  81. data/lib/axlsx/util/options_parser.rb +4 -3
  82. data/lib/axlsx/util/serialized_attributes.rb +47 -23
  83. data/lib/axlsx/util/simple_typed_list.rb +77 -64
  84. data/lib/axlsx/util/storage.rb +38 -41
  85. data/lib/axlsx/util/uri_utils.rb +70 -0
  86. data/lib/axlsx/util/validators.rb +111 -48
  87. data/lib/axlsx/util/zip_command.rb +10 -12
  88. data/lib/axlsx/version.rb +3 -2
  89. data/lib/axlsx/workbook/defined_name.rb +13 -9
  90. data/lib/axlsx/workbook/defined_names.rb +5 -4
  91. data/lib/axlsx/workbook/shared_strings_table.rb +13 -14
  92. data/lib/axlsx/workbook/workbook.rb +152 -120
  93. data/lib/axlsx/workbook/workbook_view.rb +9 -12
  94. data/lib/axlsx/workbook/workbook_views.rb +5 -5
  95. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +87 -15
  96. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +16 -10
  97. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +28 -25
  98. data/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb +51 -0
  99. data/lib/axlsx/workbook/worksheet/auto_filter/sort_state.rb +54 -0
  100. data/lib/axlsx/workbook/worksheet/border_creator.rb +34 -29
  101. data/lib/axlsx/workbook/worksheet/break.rb +4 -5
  102. data/lib/axlsx/workbook/worksheet/cell.rb +125 -78
  103. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +33 -29
  104. data/lib/axlsx/workbook/worksheet/cfvo.rb +13 -5
  105. data/lib/axlsx/workbook/worksheet/cfvos.rb +5 -5
  106. data/lib/axlsx/workbook/worksheet/col.rb +32 -19
  107. data/lib/axlsx/workbook/worksheet/col_breaks.rb +9 -8
  108. data/lib/axlsx/workbook/worksheet/color_scale.rb +16 -16
  109. data/lib/axlsx/workbook/worksheet/cols.rb +10 -8
  110. data/lib/axlsx/workbook/worksheet/comment.rb +14 -13
  111. data/lib/axlsx/workbook/worksheet/comments.rb +11 -13
  112. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +19 -9
  113. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +75 -22
  114. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +8 -8
  115. data/lib/axlsx/workbook/worksheet/data_bar.rb +29 -30
  116. data/lib/axlsx/workbook/worksheet/data_validation.rb +73 -33
  117. data/lib/axlsx/workbook/worksheet/data_validations.rb +8 -9
  118. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +8 -8
  119. data/lib/axlsx/workbook/worksheet/dimension.rb +10 -7
  120. data/lib/axlsx/workbook/worksheet/header_footer.rb +5 -4
  121. data/lib/axlsx/workbook/worksheet/icon_set.rb +40 -12
  122. data/lib/axlsx/workbook/worksheet/merged_cells.rb +11 -11
  123. data/lib/axlsx/workbook/worksheet/outline_pr.rb +7 -4
  124. data/lib/axlsx/workbook/worksheet/page_margins.rb +41 -13
  125. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +7 -6
  126. data/lib/axlsx/workbook/worksheet/page_setup.rb +153 -131
  127. data/lib/axlsx/workbook/worksheet/pane.rb +34 -26
  128. data/lib/axlsx/workbook/worksheet/pivot_table.rb +78 -29
  129. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +12 -13
  130. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +6 -5
  131. data/lib/axlsx/workbook/worksheet/print_options.rb +4 -2
  132. data/lib/axlsx/workbook/worksheet/protected_range.rb +6 -5
  133. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +13 -11
  134. data/lib/axlsx/workbook/worksheet/rich_text.rb +6 -6
  135. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +66 -28
  136. data/lib/axlsx/workbook/worksheet/row.rb +34 -23
  137. data/lib/axlsx/workbook/worksheet/row_breaks.rb +9 -8
  138. data/lib/axlsx/workbook/worksheet/selection.rb +22 -16
  139. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +11 -7
  140. data/lib/axlsx/workbook/worksheet/sheet_data.rb +7 -7
  141. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +23 -19
  142. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +12 -8
  143. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +22 -21
  144. data/lib/axlsx/workbook/worksheet/sheet_view.rb +71 -55
  145. data/lib/axlsx/workbook/worksheet/table.rb +13 -13
  146. data/lib/axlsx/workbook/worksheet/table_style_info.rb +8 -6
  147. data/lib/axlsx/workbook/worksheet/tables.rb +8 -6
  148. data/lib/axlsx/workbook/worksheet/worksheet.rb +120 -79
  149. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +13 -11
  150. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +11 -4
  151. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +12 -10
  152. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +9 -7
  153. data/lib/axlsx.rb +120 -60
  154. data/lib/caxlsx.rb +3 -2
  155. metadata +42 -75
@@ -1,20 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Axlsx
2
4
  # Pane options for a worksheet.
3
5
  #
4
6
  # @note The recommended way to manage the pane options is via SheetView#pane
5
7
  # @see SheetView#pane
6
8
  class Pane
7
-
8
9
  include Axlsx::OptionsParser
9
10
  include Axlsx::SerializedAttributes
11
+
10
12
  # Creates a new {Pane} object
11
13
  # @option options [Symbol] active_pane Active Pane
12
14
  # @option options [Symbol] state Split State
13
15
  # @option options [Cell, String] top_left_cell Top Left Visible Cell
14
16
  # @option options [Integer] x_split Horizontal Split Position
15
17
  # @option options [Integer] y_split Vertical Split Position
16
- def initialize(options={})
17
- #defaults
18
+ def initialize(options = {})
19
+ # defaults
18
20
  @active_pane = @state = @top_left_cell = nil
19
21
  @x_split = @y_split = 0
20
22
  parse_options options
@@ -24,16 +26,16 @@ module Axlsx
24
26
 
25
27
  # Active Pane
26
28
  # The pane that is active.
27
- # Options are
29
+ # Options are
28
30
  # * bottom_left: Bottom left pane, when both vertical and horizontal
29
31
  # splits are applied. This value is also used when only
30
- # a horizontal split has been applied, dividing the pane
31
- # into upper and lower regions. In that case, this value
32
+ # a horizontal split has been applied, dividing the pane
33
+ # into upper and lower regions. In that case, this value
32
34
  # specifies the bottom pane.
33
35
  # * bottom_right: Bottom right pane, when both vertical and horizontal
34
36
  # splits are applied.
35
37
  # * top_left: Top left pane, when both vertical and horizontal splits
36
- # are applied. This value is also used when only a horizontal
38
+ # are applied. This value is also used when only a horizontal
37
39
  # split has been applied, dividing the pane into upper and lower
38
40
  # regions. In that case, this value specifies the top pane.
39
41
  # This value is also used when only a vertical split has
@@ -41,22 +43,21 @@ module Axlsx
41
43
  # regions. In that case, this value specifies the left pane
42
44
  # * top_right: Top right pane, when both vertical and horizontal
43
45
  # splits are applied. This value is also used when only
44
- # a vertical split has been applied, dividing the pane
45
- # into right and left regions. In that case, this value
46
+ # a vertical split has been applied, dividing the pane
47
+ # into right and left regions. In that case, this value
46
48
  # specifies the right pane.
47
49
  # @see type
48
50
  # @return [Symbol]
49
51
  # default nil
50
52
  attr_reader :active_pane
51
53
 
52
-
53
54
  # Split State
54
- # Indicates whether the pane has horizontal / vertical
55
+ # Indicates whether the pane has horizontal / vertical
55
56
  # splits, and whether those splits are frozen.
56
- # Options are
57
+ # Options are
57
58
  # * frozen: Panes are frozen, but were not split being frozen. In
58
59
  # this state, when the panes are unfrozen again, a single
59
- # pane results, with no split. In this state, the split
60
+ # pane results, with no split. In this state, the split
60
61
  # bars are not adjustable.
61
62
  # * frozen_split: Panes are frozen and were split before being frozen. In
62
63
  # this state, when the panes are unfrozen again, the split
@@ -69,7 +70,7 @@ module Axlsx
69
70
  attr_reader :state
70
71
 
71
72
  # Top Left Visible Cell
72
- # Location of the top left visible cell in the bottom
73
+ # Location of the top left visible cell in the bottom
73
74
  # right pane (when in Left-To-Right mode).
74
75
  # @see type
75
76
  # @return [String]
@@ -78,7 +79,7 @@ module Axlsx
78
79
 
79
80
  # Horizontal Split Position
80
81
  # Horizontal position of the split, in 1/20th of a point; 0 (zero)
81
- # if none. If the pane is frozen, this value indicates the number
82
+ # if none. If the pane is frozen, this value indicates the number
82
83
  # of columns visible in the top pane.
83
84
  # @see type
84
85
  # @return [Integer]
@@ -86,7 +87,7 @@ module Axlsx
86
87
  attr_reader :x_split
87
88
 
88
89
  # Vertical Split Position
89
- # Vertical position of the split, in 1/20th of a point; 0 (zero)
90
+ # Vertical position of the split, in 1/20th of a point; 0 (zero)
90
91
  # if none. If the pane is frozen, this value indicates the number
91
92
  # of rows visible in the left pane.
92
93
  # @see type
@@ -96,43 +97,50 @@ module Axlsx
96
97
 
97
98
  # @see active_pane
98
99
  def active_pane=(v)
99
- Axlsx::validate_pane_type(v)
100
- @active_pane = Axlsx::camel(v.to_s, false)
100
+ Axlsx.validate_pane_type(v)
101
+ @active_pane = Axlsx.camel(v.to_s, false)
101
102
  end
102
103
 
103
104
  # @see state
104
105
  def state=(v)
105
- Axlsx::validate_split_state_type(v)
106
- @state = Axlsx::camel(v.to_s, false)
106
+ Axlsx.validate_split_state_type(v)
107
+ @state = Axlsx.camel(v.to_s, false)
107
108
  end
108
109
 
109
110
  # @see top_left_cell
110
111
  def top_left_cell=(v)
111
- cell = (v.class == Axlsx::Cell ? v.r_abs : v)
112
- Axlsx::validate_string(cell)
112
+ cell = (v.instance_of?(Axlsx::Cell) ? v.r_abs : v)
113
+ Axlsx.validate_string(cell)
113
114
  @top_left_cell = cell
114
115
  end
115
116
 
116
117
  # @see x_split
117
- def x_split=(v); Axlsx::validate_unsigned_int(v); @x_split = v end
118
+ def x_split=(v)
119
+ Axlsx.validate_unsigned_int(v)
120
+ @x_split = v
121
+ end
118
122
 
119
123
  # @see y_split
120
- def y_split=(v); Axlsx::validate_unsigned_int(v); @y_split = v end
124
+ def y_split=(v)
125
+ Axlsx.validate_unsigned_int(v)
126
+ @y_split = v
127
+ end
121
128
 
122
129
  # Serializes the data validation
123
130
  # @param [String] str
124
131
  # @return [String]
125
- def to_xml_string(str = '')
132
+ def to_xml_string(str = +'')
126
133
  finalize
127
134
  serialized_tag 'pane', str
128
135
  end
136
+
129
137
  private
130
138
 
131
139
  def finalize
132
140
  if @state == 'frozen' && @top_left_cell.nil?
133
141
  row = @y_split || 0
134
142
  column = @x_split || 0
135
- @top_left_cell = "#{('A'..'ZZ').to_a[column]}#{row+1}"
143
+ @top_left_cell = "#{('A'..'ZZ').to_a[column]}#{row + 1}"
136
144
  end
137
145
  end
138
146
  end
@@ -1,10 +1,10 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
4
  # Table
4
5
  # @note Worksheet#add_pivot_table is the recommended way to create tables for your worksheets.
5
6
  # @see README for examples
6
7
  class PivotTable
7
-
8
8
  include Axlsx::OptionsParser
9
9
 
10
10
  # Creates a new PivotTable object
@@ -13,12 +13,12 @@ module Axlsx
13
13
  # @param [Worksheet] sheet The sheet containing the table data.
14
14
  # @option options [Cell, String] name
15
15
  # @option options [TableStyle] style
16
- def initialize(ref, range, sheet, options={})
16
+ def initialize(ref, range, sheet, options = {})
17
17
  @ref = ref
18
18
  self.range = range
19
19
  @sheet = sheet
20
20
  @sheet.workbook.pivot_tables << self
21
- @name = "PivotTable#{index+1}"
21
+ @name = "PivotTable#{index + 1}"
22
22
  @data_sheet = nil
23
23
  @rows = []
24
24
  @columns = []
@@ -26,8 +26,11 @@ module Axlsx
26
26
  @pages = []
27
27
  @subtotal = nil
28
28
  @no_subtotals_on_headers = []
29
+ @grand_totals = :both
29
30
  @sort_on_headers = {}
30
31
  @style_info = {}
32
+ @use_auto_formatting = true
33
+ @apply_width_height_formats = true
31
34
  parse_options options
32
35
  yield self if block_given?
33
36
  end
@@ -51,6 +54,19 @@ module Axlsx
51
54
  @sort_on_headers = headers
52
55
  end
53
56
 
57
+ # Defines which Grand Totals are to be shown.
58
+ # @return [Symbol] The row and/or column Grand Totals that are to be shown.
59
+ # Defaults to `:both` to show both row & column grand totals.
60
+ # Set to `:row_only`, `:col_only`, or `:none` to hide one or both Grand Totals.
61
+ attr_reader :grand_totals
62
+
63
+ # (see #grand_totals)
64
+ def grand_totals=(value)
65
+ RestrictionValidator.validate "PivotTable.grand_totals", [:both, :row_only, :col_only, :none], value
66
+
67
+ @grand_totals = value
68
+ end
69
+
54
70
  # Style info for the pivot table
55
71
  # @return [Hash]
56
72
  attr_accessor :style_info
@@ -71,6 +87,14 @@ module Axlsx
71
87
  # @return [Worksheet]
72
88
  attr_writer :data_sheet
73
89
 
90
+ # Whether to apply auto formatting to the pivot table.
91
+ # @return [Boolean]
92
+ attr_accessor :use_auto_formatting
93
+
94
+ # Whether to apply width/height formats to the pivot table.
95
+ # @return [Boolean]
96
+ attr_accessor :apply_width_height_formats
97
+
74
98
  # @see #data_sheet
75
99
  def data_sheet
76
100
  @data_sheet || @sheet
@@ -92,7 +116,6 @@ module Axlsx
92
116
  # @return [Array]
93
117
  attr_reader :rows
94
118
 
95
-
96
119
  # (see #rows)
97
120
  def rows=(v)
98
121
  DataTypeValidator.validate "#{self.class}.rows", [Array], v
@@ -115,7 +138,8 @@ module Axlsx
115
138
  @columns = v
116
139
  end
117
140
 
118
- # The data
141
+ # The data as an array of either headers (String) or hashes or mix of the two.
142
+ # Hash in format of { ref: header, num_fmt: numFmts, subtotal: subtotal }, where header is String, numFmts is Integer, and subtotal one of %w[sum count average max min product countNums stdDev stdDevp var varp]; leave subtotal blank to sum values
119
143
  # @return [Array]
120
144
  attr_reader :data
121
145
 
@@ -125,7 +149,7 @@ module Axlsx
125
149
  @data = []
126
150
  v.each do |data_field|
127
151
  if data_field.is_a? String
128
- data_field = {:ref => data_field}
152
+ data_field = { ref: data_field }
129
153
  end
130
154
  data_field.each do |key, value|
131
155
  if key == :num_fmt
@@ -136,7 +160,6 @@ module Axlsx
136
160
  end
137
161
  @data << data_field
138
162
  end
139
- @data
140
163
  end
141
164
 
142
165
  # The pages
@@ -161,13 +184,13 @@ module Axlsx
161
184
  # The part name for this table
162
185
  # @return [String]
163
186
  def pn
164
- "#{PIVOT_TABLE_PN % (index+1)}"
187
+ format(PIVOT_TABLE_PN, index + 1)
165
188
  end
166
189
 
167
190
  # The relationship part name of this pivot table
168
191
  # @return [String]
169
192
  def rels_pn
170
- "#{PIVOT_TABLE_RELS_PN % (index+1)}"
193
+ format(PIVOT_TABLE_RELS_PN, index + 1)
171
194
  end
172
195
 
173
196
  # The cache_definition for this pivot table
@@ -187,13 +210,21 @@ module Axlsx
187
210
  # Serializes the object
188
211
  # @param [String] str
189
212
  # @return [String]
190
- def to_xml_string(str = '')
213
+ def to_xml_string(str = +'')
191
214
  str << '<?xml version="1.0" encoding="UTF-8"?>'
192
215
 
193
- str << ('<pivotTableDefinition xmlns="' << XML_NS << '" name="' << name << '" cacheId="' << cache_definition.cache_id.to_s << '"' << (data.size <= 1 ? ' dataOnRows="1"' : '') << ' applyNumberFormats="0" applyBorderFormats="0" applyFontFormats="0" applyPatternFormats="0" applyAlignmentFormats="0" applyWidthHeightFormats="1" dataCaption="Data" showMultipleLabel="0" showMemberPropertyTips="0" useAutoFormatting="1" indent="0" compact="0" compactData="0" gridDropZones="1" multipleFieldFilters="0">')
216
+ str << '<pivotTableDefinition xmlns="' << XML_NS << '" name="' << name << '" cacheId="' << cache_definition.cache_id.to_s << '"'
217
+ str << ' dataOnRows="1"' if data.size <= 1
218
+ str << ' rowGrandTotals="0"' if grand_totals == :col_only || grand_totals == :none
219
+ str << ' colGrandTotals="0"' if grand_totals == :row_only || grand_totals == :none
220
+ str << ' applyNumberFormats="0" applyBorderFormats="0" applyFontFormats="0" applyPatternFormats="0" applyAlignmentFormats="0"'
221
+ str << ' applyWidthHeightFormats="' << (@apply_width_height_formats ? '1' : '0') << '"'
222
+ str << ' dataCaption="Data" showMultipleLabel="0" showMemberPropertyTips="0"'
223
+ str << ' useAutoFormatting="' << (@use_auto_formatting ? '1' : '0') << '"'
224
+ str << ' indent="0" compact="0" compactData="0" gridDropZones="1" multipleFieldFilters="0">'
194
225
 
195
- str << ('<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="' << ref << '"/>')
196
- str << ('<pivotFields count="' << header_cells_count.to_s << '">')
226
+ str << '<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="' << ref << '"/>'
227
+ str << '<pivotFields count="' << header_cells_count.to_s << '">'
197
228
 
198
229
  header_cell_values.each do |cell_value|
199
230
  subtotal = !no_subtotals_on_headers.include?(cell_value)
@@ -206,13 +237,13 @@ module Axlsx
206
237
  str << '<rowFields count="1"><field x="-2"/></rowFields>'
207
238
  str << '<rowItems count="2"><i><x/></i> <i i="1"><x v="1"/></i></rowItems>'
208
239
  else
209
- str << ('<rowFields count="' << rows.size.to_s << '">')
240
+ str << '<rowFields count="' << rows.size.to_s << '">'
210
241
  rows.each do |row_value|
211
- str << ('<field x="' << header_index_of(row_value).to_s << '"/>')
242
+ str << '<field x="' << header_index_of(row_value).to_s << '"/>'
212
243
  end
213
244
  str << '</rowFields>'
214
- str << ('<rowItems count="' << rows.size.to_s << '">')
215
- rows.size.times do |i|
245
+ str << '<rowItems count="' << rows.size.to_s << '">'
246
+ rows.size.times do
216
247
  str << '<i/>'
217
248
  end
218
249
  str << '</rowItems>'
@@ -222,32 +253,46 @@ module Axlsx
222
253
  str << '<colFields count="1"><field x="-2"/></colFields>'
223
254
  str << "<colItems count=\"#{data.size}\">"
224
255
  str << '<i><x/></i>'
225
- data[1..-1].each_with_index do |datum_value,i|
256
+ (data.size - 1).times do |i|
226
257
  str << "<i i=\"#{i + 1}\"><x v=\"#{i + 1}\"/></i>"
227
258
  end
228
259
  str << '</colItems>'
229
260
  else
230
261
  str << '<colItems count="1"><i/></colItems>'
231
262
  end
263
+ elsif data.size > 1
264
+ str << '<colFields count="' << (columns.size + 1).to_s << '">'
265
+ columns.each do |column_value|
266
+ str << '<field x="' << header_index_of(column_value).to_s << '"/>'
267
+ end
268
+ str << '<field x="-2"/></colFields>'
269
+ str << "<colItems count=\"#{data.size}\">"
270
+ str << '<i><x/></i>'
271
+ (data.size - 1).times do |i|
272
+ str << "<i i=\"#{i + 1}\"><x v=\"#{i + 1}\"/></i>"
273
+ end
274
+ str << '</colItems>'
232
275
  else
233
- str << ('<colFields count="' << columns.size.to_s << '">')
276
+ str << '<colFields count="' << columns.size.to_s << '">'
234
277
  columns.each do |column_value|
235
- str << ('<field x="' << header_index_of(column_value).to_s << '"/>')
278
+ str << '<field x="' << header_index_of(column_value).to_s << '"/>'
236
279
  end
237
280
  str << '</colFields>'
238
281
  end
239
282
  unless pages.empty?
240
- str << ('<pageFields count="' << pages.size.to_s << '">')
283
+ str << '<pageFields count="' << pages.size.to_s << '">'
241
284
  pages.each do |page_value|
242
- str << ('<pageField fld="' << header_index_of(page_value).to_s << '"/>')
285
+ str << '<pageField fld="' << header_index_of(page_value).to_s << '"/>'
243
286
  end
244
287
  str << '</pageFields>'
245
288
  end
246
289
  unless data.empty?
247
290
  str << "<dataFields count=\"#{data.size}\">"
248
291
  data.each do |datum_value|
249
- # The correct name prefix in ["Sum","Average", etc...]
250
- str << "<dataField name='#{(datum_value[:subtotal]||'')} of #{datum_value[:ref]}' fld='#{header_index_of(datum_value[:ref])}' baseField='0' baseItem='0'"
292
+ subtotal_name = datum_value[:subtotal] || 'sum'
293
+ subtotal_name = 'count' if datum_value[:subtotal] == 'countNums' # both count & countNums are labelled as count
294
+ field_name = datum_value[:name] || "#{subtotal_name.capitalize} of #{datum_value[:ref]}"
295
+ str << "<dataField name='#{field_name}' fld='#{header_index_of(datum_value[:ref])}' baseField='0' baseItem='0'"
251
296
  str << " numFmtId='#{datum_value[:num_fmt]}'" if datum_value[:num_fmt]
252
297
  str << " subtotal='#{datum_value[:subtotal]}' " if datum_value[:subtotal]
253
298
  str << "/>"
@@ -257,7 +302,7 @@ module Axlsx
257
302
  # custom pivot table style
258
303
  unless style_info.empty?
259
304
  str << '<pivotTableStyleInfo'
260
- style_info.each do |k,v|
305
+ style_info.each do |k, v|
261
306
  str << ' ' << k.to_s << '="' << v.to_s << '"'
262
307
  end
263
308
  str << ' />'
@@ -268,7 +313,7 @@ module Axlsx
268
313
  # References for header cells
269
314
  # @return [Array]
270
315
  def header_cell_refs
271
- Axlsx::range_to_a(header_range).first
316
+ Axlsx.range_to_a(header_range).first
272
317
  end
273
318
 
274
319
  # The header cells for the pivot table
@@ -313,7 +358,11 @@ module Axlsx
313
358
  elsif columns.include? cell_ref
314
359
  attributes << 'axis="axisCol"'
315
360
  attributes << "sortType=\"#{sorttype == :descending ? 'descending' : 'ascending'}\"" if sorttype
316
- include_items_tag = true
361
+ if subtotal
362
+ include_items_tag = true
363
+ else
364
+ attributes << 'defaultSubtotal="0"'
365
+ end
317
366
  elsif pages.include? cell_ref
318
367
  attributes << 'axis="axisPage"'
319
368
  include_items_tag = true
@@ -321,7 +370,7 @@ module Axlsx
321
370
  attributes << 'dataField="1"'
322
371
  end
323
372
 
324
- "<pivotField #{attributes.join(' ')}>#{include_items_tag ? items_tag : nil}</pivotField>"
373
+ "<pivotField #{attributes.join(' ')}>#{items_tag if include_items_tag}</pivotField>"
325
374
  end
326
375
 
327
376
  def data_refs
@@ -1,10 +1,10 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
4
  # Table
4
5
  # @note Worksheet#add_pivot_table is the recommended way to create tables for your worksheets.
5
6
  # @see README for examples
6
7
  class PivotTableCacheDefinition
7
-
8
8
  include Axlsx::OptionsParser
9
9
 
10
10
  # Creates a new PivotTable object
@@ -26,7 +26,7 @@ module Axlsx
26
26
  # The part name for this table
27
27
  # @return [String]
28
28
  def pn
29
- "#{PIVOT_TABLE_CACHE_DEFINITION_PN % (index+1)}"
29
+ format(PIVOT_TABLE_CACHE_DEFINITION_PN, index + 1)
30
30
  end
31
31
 
32
32
  # The identifier for this cache
@@ -45,22 +45,21 @@ module Axlsx
45
45
  # Serializes the object
46
46
  # @param [String] str
47
47
  # @return [String]
48
- def to_xml_string(str = '')
48
+ def to_xml_string(str = +'')
49
49
  str << '<?xml version="1.0" encoding="UTF-8"?>'
50
- str << ('<pivotCacheDefinition xmlns="' << XML_NS << '" xmlns:r="' << XML_NS_R << '" invalid="1" refreshOnLoad="1" recordCount="0">')
51
- str << '<cacheSource type="worksheet">'
52
- str << ( '<worksheetSource ref="' << pivot_table.range << '" sheet="' << pivot_table.data_sheet.name << '"/>')
53
- str << '</cacheSource>'
54
- str << ( '<cacheFields count="' << pivot_table.header_cells_count.to_s << '">')
50
+ str << '<pivotCacheDefinition xmlns="' << XML_NS << '" xmlns:r="' << XML_NS_R << '" invalid="1" refreshOnLoad="1" recordCount="0">'
51
+ str << '<cacheSource type="worksheet">'
52
+ str << '<worksheetSource ref="' << pivot_table.range << '" sheet="' << pivot_table.data_sheet.name << '"/>'
53
+ str << '</cacheSource>'
54
+ str << '<cacheFields count="' << pivot_table.header_cells_count.to_s << '">'
55
55
  pivot_table.header_cells.each do |cell|
56
- str << ( '<cacheField name="' << cell.clean_value << '" numFmtId="0">')
56
+ str << '<cacheField name="' << cell.clean_value << '" numFmtId="0">'
57
57
  str << '<sharedItems count="0">'
58
58
  str << '</sharedItems>'
59
- str << '</cacheField>'
59
+ str << '</cacheField>'
60
60
  end
61
- str << '</cacheFields>'
61
+ str << '</cacheFields>'
62
62
  str << '</pivotCacheDefinition>'
63
63
  end
64
-
65
64
  end
66
65
  end
@@ -1,12 +1,13 @@
1
- module Axlsx
1
+ # frozen_string_literal: true
2
2
 
3
+ module Axlsx
3
4
  # A simple, self serializing class for storing pivot tables
4
5
  class PivotTables < SimpleTypedList
5
-
6
6
  # creates a new Tables object
7
7
  def initialize(worksheet)
8
8
  raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
9
- super PivotTable
9
+
10
+ super(PivotTable)
10
11
  @worksheet = worksheet
11
12
  end
12
13
 
@@ -17,8 +18,8 @@ module Axlsx
17
18
  # returns the relationships required by this collection
18
19
  def relationships
19
20
  return [] if empty?
20
- map{ |pivot_table| Relationship.new(pivot_table, PIVOT_TABLE_R, "../#{pivot_table.pn}") }
21
+
22
+ map { |pivot_table| Relationship.new(pivot_table, PIVOT_TABLE_R, "../#{pivot_table.pn}") }
21
23
  end
22
24
  end
23
-
24
25
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Axlsx
2
4
  # Options for printing a worksheet. All options are boolean and false by default.
3
5
  #
@@ -5,10 +7,10 @@ module Axlsx
5
7
  # @see Worksheet#print_options
6
8
  # @see Worksheet#initialize
7
9
  class PrintOptions
8
-
9
10
  include Axlsx::OptionsParser
10
11
  include Axlsx::SerializedAttributes
11
12
  include Axlsx::Accessors
13
+
12
14
  # Creates a new PrintOptions object
13
15
  # @option options [Boolean] grid_lines Whether grid lines should be printed
14
16
  # @option options [Boolean] headings Whether row and column headings should be printed
@@ -32,7 +34,7 @@ module Axlsx
32
34
  # @note As all attributes default to "false" according to the xml schema definition, the generated xml includes only those attributes that are set to true.
33
35
  # @param [String] str
34
36
  # @return [String]
35
- def to_xml_string(str = '')
37
+ def to_xml_string(str = +'')
36
38
  serialized_tag 'printOptions', str
37
39
  end
38
40
  end
@@ -1,16 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Axlsx
2
4
  # The Protected Range class represents a set of cells in the worksheet
3
5
  # @note the recommended way to manage protected ranges with via Worksheet#protect_range
4
6
  # @see Worksheet#protect_range
5
7
  class ProtectedRange
6
-
7
8
  include Axlsx::OptionsParser
8
9
  include Axlsx::SerializedAttributes
9
10
 
10
- # Initializes a new protected range object
11
+ # Initializes a new protected range object
11
12
  # @option [String] sqref The cell range reference to protect. This can be an absolute or a relateve range however, it only applies to the current sheet.
12
13
  # @option [String] name An optional name for the protected name.
13
- def initialize(options={})
14
+ def initialize(options = {})
14
15
  parse_options options
15
16
  yield self if block_given?
16
17
  end
@@ -40,8 +41,8 @@ module Axlsx
40
41
  # @param [String] str if this string object is provided we append
41
42
  # our output to that object. Use this - it helps limit the number of
42
43
  # objects created during serialization
43
- def to_xml_string(str="")
44
+ def to_xml_string(str = +'')
44
45
  serialized_tag 'protectedRange', str
45
- end
46
+ end
46
47
  end
47
48
  end
@@ -1,34 +1,36 @@
1
- module Axlsx
1
+ # frozen_string_literal: true
2
2
 
3
+ module Axlsx
3
4
  # A self serializing collection of ranges that should be protected in
4
5
  # the worksheet
5
6
  class ProtectedRanges < SimpleTypedList
6
-
7
7
  attr_reader :worksheet
8
8
 
9
9
  def initialize(worksheet)
10
10
  raise ArgumentError, 'You must provide a worksheet' unless worksheet.is_a?(Worksheet)
11
- super ProtectedRange
11
+
12
+ super(ProtectedRange)
12
13
  @worksheet = worksheet
13
14
  end
14
15
 
15
16
  # Adds a protected range
16
17
  # @param [Array|String] cells A string range reference or array of cells that will be protected
17
18
  def add_range(cells)
18
- sqref = if cells.is_a?(String)
19
- cells
20
- elsif cells.is_a?(SimpleTypedList) || cells.is_a?(Array)
21
- Axlsx::cell_range(cells, false)
22
- end
23
- self << ProtectedRange.new(:sqref => sqref, :name => "Range#{size}")
24
- last
19
+ sqref = if cells.is_a?(String)
20
+ cells
21
+ elsif cells.is_a?(SimpleTypedList) || cells.is_a?(Array)
22
+ Axlsx.cell_range(cells, false)
23
+ end
24
+ self << ProtectedRange.new(sqref: sqref, name: "Range#{size}")
25
+ last
25
26
  end
26
27
 
27
28
  # Serializes the protected ranges
28
29
  # @param [String] str
29
30
  # @return [String]
30
- def to_xml_string(str = '')
31
+ def to_xml_string(str = +'')
31
32
  return if empty?
33
+
32
34
  str << '<protectedRanges>'
33
35
  each { |range| range.to_xml_string(str) }
34
36
  str << '</protectedRanges>'
@@ -1,13 +1,13 @@
1
- module Axlsx
1
+ # frozen_string_literal: true
2
2
 
3
+ module Axlsx
3
4
  # A simple, self serializing class for storing TextRuns
4
5
  class RichText < SimpleTypedList
5
-
6
6
  # creates a new RichText collection
7
7
  # @param [String] text -optional The text to use in creating the first RichTextRun
8
8
  # @param [Object] options -optional The options to use in creating the first RichTextRun
9
9
  # @yield [RichText] self
10
- def initialize(text = nil, options={})
10
+ def initialize(text = nil, options = {})
11
11
  super(RichTextRun)
12
12
  add_run(text, options) unless text.nil?
13
13
  yield self if block_given?
@@ -34,7 +34,7 @@ module Axlsx
34
34
  # Creates and adds a RichTextRun to this collectino
35
35
  # @param [String] text The text to use in creating a new RichTextRun
36
36
  # @param [Object] options The options to use in creating the new RichTextRun
37
- def add_run(text, options={})
37
+ def add_run(text, options = {})
38
38
  self << RichTextRun.new(text, options)
39
39
  end
40
40
 
@@ -47,8 +47,8 @@ module Axlsx
47
47
  # renders the RichTextRuns in this collection
48
48
  # @param [String] str
49
49
  # @return [String]
50
- def to_xml_string(str='')
51
- each{ |run| run.to_xml_string(str) }
50
+ def to_xml_string(str = +'')
51
+ each { |run| run.to_xml_string(str) }
52
52
  str
53
53
  end
54
54
  end