caxlsx 3.3.0 → 3.4.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 (148) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/README.md +24 -5
  4. data/Rakefile +0 -1
  5. data/lib/axlsx/content_type/abstract_content_type.rb +2 -5
  6. data/lib/axlsx/content_type/content_type.rb +0 -4
  7. data/lib/axlsx/content_type/default.rb +1 -5
  8. data/lib/axlsx/content_type/override.rb +0 -4
  9. data/lib/axlsx/doc_props/app.rb +1 -6
  10. data/lib/axlsx/doc_props/core.rb +1 -6
  11. data/lib/axlsx/drawing/area_chart.rb +3 -6
  12. data/lib/axlsx/drawing/area_series.rb +1 -4
  13. data/lib/axlsx/drawing/ax_data_source.rb +1 -6
  14. data/lib/axlsx/drawing/axes.rb +5 -6
  15. data/lib/axlsx/drawing/axis.rb +7 -12
  16. data/lib/axlsx/drawing/bar_3D_chart.rb +7 -10
  17. data/lib/axlsx/drawing/bar_chart.rb +6 -9
  18. data/lib/axlsx/drawing/bar_series.rb +2 -8
  19. data/lib/axlsx/drawing/bubble_chart.rb +2 -5
  20. data/lib/axlsx/drawing/bubble_series.rb +3 -6
  21. data/lib/axlsx/drawing/cat_axis.rb +2 -7
  22. data/lib/axlsx/drawing/chart.rb +7 -12
  23. data/lib/axlsx/drawing/d_lbls.rb +23 -22
  24. data/lib/axlsx/drawing/drawing.rb +4 -8
  25. data/lib/axlsx/drawing/graphic_frame.rb +0 -3
  26. data/lib/axlsx/drawing/hyperlink.rb +4 -7
  27. data/lib/axlsx/drawing/line_3D_chart.rb +10 -14
  28. data/lib/axlsx/drawing/line_chart.rb +3 -6
  29. data/lib/axlsx/drawing/line_series.rb +1 -4
  30. data/lib/axlsx/drawing/marker.rb +5 -9
  31. data/lib/axlsx/drawing/num_data.rb +4 -9
  32. data/lib/axlsx/drawing/num_data_source.rb +3 -7
  33. data/lib/axlsx/drawing/num_val.rb +2 -5
  34. data/lib/axlsx/drawing/one_cell_anchor.rb +4 -6
  35. data/lib/axlsx/drawing/pic.rb +46 -13
  36. data/lib/axlsx/drawing/picture_locking.rb +3 -6
  37. data/lib/axlsx/drawing/pie_3D_chart.rb +2 -7
  38. data/lib/axlsx/drawing/pie_series.rb +1 -6
  39. data/lib/axlsx/drawing/scaling.rb +2 -5
  40. data/lib/axlsx/drawing/scatter_chart.rb +2 -5
  41. data/lib/axlsx/drawing/scatter_series.rb +2 -5
  42. data/lib/axlsx/drawing/ser_axis.rb +2 -6
  43. data/lib/axlsx/drawing/series.rb +3 -5
  44. data/lib/axlsx/drawing/series_title.rb +0 -2
  45. data/lib/axlsx/drawing/str_data.rb +3 -8
  46. data/lib/axlsx/drawing/str_val.rb +2 -5
  47. data/lib/axlsx/drawing/title.rb +12 -15
  48. data/lib/axlsx/drawing/two_cell_anchor.rb +5 -7
  49. data/lib/axlsx/drawing/val_axis.rb +1 -4
  50. data/lib/axlsx/drawing/view_3D.rb +24 -24
  51. data/lib/axlsx/drawing/vml_drawing.rb +2 -5
  52. data/lib/axlsx/drawing/vml_shape.rb +4 -7
  53. data/lib/axlsx/package.rb +35 -36
  54. data/lib/axlsx/rels/relationship.rb +21 -24
  55. data/lib/axlsx/rels/relationships.rb +3 -6
  56. data/lib/axlsx/stylesheet/border.rb +1 -4
  57. data/lib/axlsx/stylesheet/border_pr.rb +3 -5
  58. data/lib/axlsx/stylesheet/cell_alignment.rb +2 -10
  59. data/lib/axlsx/stylesheet/cell_protection.rb +1 -4
  60. data/lib/axlsx/stylesheet/cell_style.rb +3 -7
  61. data/lib/axlsx/stylesheet/color.rb +6 -5
  62. data/lib/axlsx/stylesheet/dxf.rb +2 -6
  63. data/lib/axlsx/stylesheet/fill.rb +0 -4
  64. data/lib/axlsx/stylesheet/font.rb +7 -6
  65. data/lib/axlsx/stylesheet/gradient_fill.rb +4 -6
  66. data/lib/axlsx/stylesheet/gradient_stop.rb +2 -3
  67. data/lib/axlsx/stylesheet/num_fmt.rb +1 -4
  68. data/lib/axlsx/stylesheet/pattern_fill.rb +3 -5
  69. data/lib/axlsx/stylesheet/styles.rb +53 -45
  70. data/lib/axlsx/stylesheet/table_style.rb +5 -8
  71. data/lib/axlsx/stylesheet/table_style_element.rb +1 -4
  72. data/lib/axlsx/stylesheet/table_styles.rb +3 -7
  73. data/lib/axlsx/stylesheet/xf.rb +3 -6
  74. data/lib/axlsx/util/accessors.rb +2 -4
  75. data/lib/axlsx/util/constants.rb +3 -5
  76. data/lib/axlsx/util/mime_type_utils.rb +13 -0
  77. data/lib/axlsx/util/options_parser.rb +1 -2
  78. data/lib/axlsx/util/serialized_attributes.rb +4 -5
  79. data/lib/axlsx/util/simple_typed_list.rb +18 -17
  80. data/lib/axlsx/util/storage.rb +32 -36
  81. data/lib/axlsx/util/validators.rb +7 -4
  82. data/lib/axlsx/util/zip_command.rb +0 -2
  83. data/lib/axlsx/version.rb +1 -2
  84. data/lib/axlsx/workbook/defined_name.rb +6 -5
  85. data/lib/axlsx/workbook/defined_names.rb +1 -2
  86. data/lib/axlsx/workbook/shared_strings_table.rb +3 -6
  87. data/lib/axlsx/workbook/workbook.rb +96 -91
  88. data/lib/axlsx/workbook/workbook_view.rb +5 -10
  89. data/lib/axlsx/workbook/workbook_views.rb +1 -3
  90. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +9 -8
  91. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +6 -6
  92. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +13 -14
  93. data/lib/axlsx/workbook/worksheet/border_creator.rb +29 -26
  94. data/lib/axlsx/workbook/worksheet/break.rb +2 -5
  95. data/lib/axlsx/workbook/worksheet/cell.rb +46 -26
  96. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +11 -11
  97. data/lib/axlsx/workbook/worksheet/cfvo.rb +2 -2
  98. data/lib/axlsx/workbook/worksheet/cfvos.rb +3 -5
  99. data/lib/axlsx/workbook/worksheet/col.rb +4 -7
  100. data/lib/axlsx/workbook/worksheet/col_breaks.rb +4 -5
  101. data/lib/axlsx/workbook/worksheet/color_scale.rb +10 -12
  102. data/lib/axlsx/workbook/worksheet/cols.rb +6 -6
  103. data/lib/axlsx/workbook/worksheet/comment.rb +4 -5
  104. data/lib/axlsx/workbook/worksheet/comments.rb +6 -10
  105. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +4 -5
  106. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +2 -6
  107. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +2 -4
  108. data/lib/axlsx/workbook/worksheet/data_bar.rb +20 -22
  109. data/lib/axlsx/workbook/worksheet/data_validation.rb +4 -5
  110. data/lib/axlsx/workbook/worksheet/data_validations.rb +2 -5
  111. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +1 -3
  112. data/lib/axlsx/workbook/worksheet/dimension.rb +5 -4
  113. data/lib/axlsx/workbook/worksheet/header_footer.rb +0 -1
  114. data/lib/axlsx/workbook/worksheet/icon_set.rb +2 -3
  115. data/lib/axlsx/workbook/worksheet/merged_cells.rb +8 -8
  116. data/lib/axlsx/workbook/worksheet/outline_pr.rb +0 -1
  117. data/lib/axlsx/workbook/worksheet/page_margins.rb +2 -2
  118. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +1 -3
  119. data/lib/axlsx/workbook/worksheet/page_setup.rb +117 -120
  120. data/lib/axlsx/workbook/worksheet/pane.rb +16 -17
  121. data/lib/axlsx/workbook/worksheet/pivot_table.rb +8 -11
  122. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +8 -11
  123. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +3 -4
  124. data/lib/axlsx/workbook/worksheet/print_options.rb +0 -1
  125. data/lib/axlsx/workbook/worksheet/protected_range.rb +4 -5
  126. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +9 -9
  127. data/lib/axlsx/workbook/worksheet/rich_text.rb +4 -6
  128. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +21 -5
  129. data/lib/axlsx/workbook/worksheet/row.rb +24 -15
  130. data/lib/axlsx/workbook/worksheet/row_breaks.rb +3 -4
  131. data/lib/axlsx/workbook/worksheet/selection.rb +7 -9
  132. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +5 -6
  133. data/lib/axlsx/workbook/worksheet/sheet_data.rb +4 -6
  134. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +18 -18
  135. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +4 -4
  136. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +12 -13
  137. data/lib/axlsx/workbook/worksheet/sheet_view.rb +35 -42
  138. data/lib/axlsx/workbook/worksheet/table.rb +7 -9
  139. data/lib/axlsx/workbook/worksheet/table_style_info.rb +2 -3
  140. data/lib/axlsx/workbook/worksheet/tables.rb +4 -4
  141. data/lib/axlsx/workbook/worksheet/worksheet.rb +63 -35
  142. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +6 -6
  143. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +3 -2
  144. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +5 -6
  145. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +4 -4
  146. data/lib/axlsx.rb +23 -9
  147. data/lib/caxlsx.rb +0 -1
  148. metadata +3 -3
@@ -1,11 +1,9 @@
1
- # encoding: UTF-8
2
1
  module Axlsx
3
2
  # View options for a worksheet.
4
3
  #
5
4
  # @note The recommended way to manage the sheet view is via Worksheet#sheet_view
6
5
  # @see Worksheet#sheet_view
7
6
  class SheetView
8
-
9
7
  include Axlsx::OptionsParser
10
8
  include Axlsx::Accessors
11
9
  include Axlsx::SerializedAttributes
@@ -30,8 +28,8 @@ module Axlsx
30
28
  # @option options [Integer] zoom_scale_normal Zoom Scale Normal View
31
29
  # @option options [Integer] zoom_scale_page_layout_view Zoom Scale Page Layout View
32
30
  # @option options [Integer] zoom_scale_sheet_layout_view Zoom Scale Page Break Preview
33
- def initialize(options={})
34
- #defaults
31
+ def initialize(options = {})
32
+ # defaults
35
33
  @color_id = @top_left_cell = @pane = nil
36
34
  @right_to_left = @show_formulas = @show_outline_symbols = @show_white_space = @tab_selected = @window_protection = false
37
35
  @default_grid_color = @show_grid_lines = @show_row_col_headers = @show_ruler = @show_zeros = true
@@ -42,16 +40,15 @@ module Axlsx
42
40
  end
43
41
 
44
42
  boolean_attr_accessor :default_grid_color, :right_to_left, :show_formulas, :show_grid_lines,
45
- :show_row_col_headers, :show_ruler, :show_white_space, :show_zeros, :tab_selected, :window_protection, :show_outline_symbols
43
+ :show_row_col_headers, :show_ruler, :show_white_space, :show_zeros, :tab_selected, :window_protection, :show_outline_symbols
46
44
 
47
45
  serializable_attributes :default_grid_color, :right_to_left, :show_formulas, :show_grid_lines,
48
- :show_row_col_headers, :show_ruler, :show_white_space, :show_zeros, :tab_selected, :window_protection, :show_outline_symbols,
49
- :zoom_scale_sheet_layout_view, :zoom_scale_page_layout_view, :zoom_scale_normal, :workbook_view_id,
50
- :view, :top_left_cell, :color_id, :zoom_scale
51
-
46
+ :show_row_col_headers, :show_ruler, :show_white_space, :show_zeros, :tab_selected, :window_protection, :show_outline_symbols,
47
+ :zoom_scale_sheet_layout_view, :zoom_scale_page_layout_view, :zoom_scale_normal, :workbook_view_id,
48
+ :view, :top_left_cell, :color_id, :zoom_scale
52
49
 
53
50
  # instance values that must be serialized as their own elements - e.g. not attributes.
54
- CHILD_ELEMENTS = [ :pane, :selections ]
51
+ CHILD_ELEMENTS = [:pane, :selections]
55
52
 
56
53
  # The pane object for the sheet view
57
54
  # @return [Pane]
@@ -66,11 +63,11 @@ module Axlsx
66
63
  # @return [Hash]
67
64
  attr_reader :selections
68
65
 
69
- #
66
+ #
70
67
  # Color Id
71
68
  # Index to the color value for row/column
72
- # text headings and gridlines. This is an
73
- # 'index color value' (ICV) rather than
69
+ # text headings and gridlines. This is an
70
+ # 'index color value' (ICV) rather than
74
71
  # rgb value.
75
72
  # @see type
76
73
  # @return [Integer]
@@ -78,18 +75,17 @@ module Axlsx
78
75
  attr_reader :color_id
79
76
 
80
77
  # Top Left Visible Cell
81
- # Location of the top left visible cell Location
78
+ # Location of the top left visible cell Location
82
79
  # of the top left visible cell in the bottom right
83
80
  # pane (when in Left-to-Right mode).
84
81
  # @see type
85
82
  # @return [String]
86
83
  # default nil
87
84
  attr_reader :top_left_cell
88
-
89
-
85
+
90
86
  # View Type
91
87
  # Indicates the view type.
92
- # Options are
88
+ # Options are
93
89
  # * normal: Normal view
94
90
  # * page_break_preview: Page break preview
95
91
  # * page_layout: Page Layout View
@@ -99,62 +95,59 @@ module Axlsx
99
95
  attr_reader :view
100
96
 
101
97
  # Workbook View Index
102
- # Zero-based index of this workbook view, pointing
98
+ # Zero-based index of this workbook view, pointing
103
99
  # to a workbookView element in the bookViews collection.
104
100
  # @see type
105
- # @return [Integer]
101
+ # @return [Integer]
106
102
  # default 0
107
103
  attr_reader :workbook_view_id
108
104
 
109
105
  # Zoom Scale
110
- # Window zoom magnification for current view
106
+ # Window zoom magnification for current view
111
107
  # representing percent values. This attribute
112
- # is restricted to values ranging from 10 to 400.
108
+ # is restricted to values ranging from 10 to 400.
113
109
  # Horizontal & Vertical scale together.
114
- # Current view can be Normal, Page Layout, or
110
+ # Current view can be Normal, Page Layout, or
115
111
  # Page Break Preview.
116
112
  # @see type
117
- # @return [Integer]
113
+ # @return [Integer]
118
114
  # default 100
119
115
  attr_reader :zoom_scale
120
116
 
121
-
122
117
  # Zoom Scale Normal View
123
- # Zoom magnification to use when in normal view,
124
- # representing percent values. This attribute is
125
- # restricted to values ranging from 10 to 400.
118
+ # Zoom magnification to use when in normal view,
119
+ # representing percent values. This attribute is
120
+ # restricted to values ranging from 10 to 400.
126
121
  # Horizontal & Vertical scale together.
127
- # Applies for worksheets only; zero implies the
122
+ # Applies for worksheets only; zero implies the
128
123
  # automatic setting.
129
124
  # @see type
130
- # @return [Integer]
125
+ # @return [Integer]
131
126
  # default 0
132
127
  attr_reader :zoom_scale_normal
133
128
 
134
-
135
129
  # Zoom Scale Page Layout View
136
- # Zoom magnification to use when in page layout
137
- # view, representing percent values. This attribute
138
- # is restricted to values ranging from 10 to 400.
130
+ # Zoom magnification to use when in page layout
131
+ # view, representing percent values. This attribute
132
+ # is restricted to values ranging from 10 to 400.
139
133
  # Horizontal & Vertical scale together.
140
- # Applies for worksheets only; zero implies
134
+ # Applies for worksheets only; zero implies
141
135
  # the automatic setting.
142
136
  # @see type
143
- # @return [Integer]
137
+ # @return [Integer]
144
138
  # default 0
145
139
  attr_reader :zoom_scale_page_layout_view
146
140
 
147
-
148
141
  # Zoom Scale Page Break Preview
149
- # Zoom magnification to use when in page break
150
- # preview, representing percent values. This
151
- # attribute is restricted to values ranging
142
+ # Zoom magnification to use when in page break
143
+ # preview, representing percent values. This
144
+ # attribute is restricted to values ranging
152
145
  # from 10 to 400. Horizontal & Vertical scale
153
146
  # together.
154
- # Applies for worksheet only; zero implies
147
+ # Applies for worksheet only; zero implies
155
148
  # the automatic setting.
156
149
  # @see type
157
- # @return [Integer]
150
+ # @return [Integer]
158
151
  # default 0
159
152
  attr_reader :zoom_scale_sheet_layout_view
160
153
 
@@ -172,7 +165,7 @@ module Axlsx
172
165
  # @see top_left_cell
173
166
  def top_left_cell=(v)
174
167
  cell = (v.class == Axlsx::Cell ? v.r_abs : v)
175
- Axlsx::validate_string(cell)
168
+ Axlsx::validate_string(cell)
176
169
  @top_left_cell = cell
177
170
  end
178
171
 
@@ -1,10 +1,8 @@
1
- # encoding: UTF-8
2
1
  module Axlsx
3
2
  # Table
4
3
  # @note Worksheet#add_table is the recommended way to create tables for your worksheets.
5
4
  # @see README for examples
6
5
  class Table
7
-
8
6
  include Axlsx::OptionsParser
9
7
 
10
8
  # Creates a new Table object
@@ -12,13 +10,13 @@ module Axlsx
12
10
  # @param [Worksheet] sheet The sheet containing the table data.
13
11
  # @option options [Cell, String] name
14
12
  # @option options [TableStyle] style
15
- def initialize(ref, sheet, options={})
13
+ def initialize(ref, sheet, options = {})
16
14
  @ref = ref
17
15
  @sheet = sheet
18
16
  @style = nil
19
17
  @sheet.workbook.tables << self
20
18
  @table_style_info = TableStyleInfo.new(options[:style_info]) if options[:style_info]
21
- @name = "Table#{index+1}"
19
+ @name = "Table#{index + 1}"
22
20
  parse_options options
23
21
  yield self if block_given?
24
22
  end
@@ -44,7 +42,7 @@ module Axlsx
44
42
  # The part name for this table
45
43
  # @return [String]
46
44
  def pn
47
- "#{TABLE_PN % (index+1)}"
45
+ "#{TABLE_PN % (index + 1)}"
48
46
  end
49
47
 
50
48
  # The relationship id for this table.
@@ -64,7 +62,7 @@ module Axlsx
64
62
  end
65
63
  end
66
64
 
67
- # TableStyleInfo for the table.
65
+ # TableStyleInfo for the table.
68
66
  # initialization can be fed via the :style_info option
69
67
  def table_style_info
70
68
  @table_style_info ||= TableStyleInfo.new
@@ -75,12 +73,12 @@ module Axlsx
75
73
  # @return [String]
76
74
  def to_xml_string(str = '')
77
75
  str << '<?xml version="1.0" encoding="UTF-8"?>'
78
- str << ('<table xmlns="' << XML_NS << '" id="' << (index+1).to_s << '" name="' << @name << '" displayName="' << @name.gsub(/\s/,'_') << '" ')
76
+ str << ('<table xmlns="' << XML_NS << '" id="' << (index + 1).to_s << '" name="' << @name << '" displayName="' << @name.gsub(/\s/, '_') << '" ')
79
77
  str << ('ref="' << @ref << '" totalsRowShown="0">')
80
78
  str << ('<autoFilter ref="' << @ref << '"/>')
81
79
  str << ('<tableColumns count="' << header_cells.length.to_s << '">')
82
- header_cells.each_with_index do |cell,index|
83
- str << ('<tableColumn id ="' << (index+1).to_s << '" name="' << cell.clean_value << '"/>')
80
+ header_cells.each_with_index do |cell, index|
81
+ str << ('<tableColumn id ="' << (index + 1).to_s << '" name="' << cell.clean_value << '"/>')
84
82
  end
85
83
  str << '</tableColumns>'
86
84
  table_style_info.to_xml_string(str)
@@ -1,5 +1,4 @@
1
1
  module Axlsx
2
-
3
2
  # The table style info class manages style attributes for defined tables in
4
3
  # a worksheet
5
4
  class TableStyleInfo
@@ -15,7 +14,7 @@ module Axlsx
15
14
  # @option [Boolean] show_column_stripes indicates if column stripes should
16
15
  # be shown
17
16
  # @option [Boolean] show_row_stripes indicates if row stripes should be shown
18
- # @option [String] name The name of the style to apply to your table.
17
+ # @option [String] name The name of the style to apply to your table.
19
18
  # Only predefined styles are currently supported.
20
19
  # @see Annex G. (normative) Predefined SpreadsheetML Style Definitions in part 1 of the specification.
21
20
  def initialize(options = {})
@@ -27,7 +26,7 @@ module Axlsx
27
26
  # boolean attributes for this object
28
27
  boolean_attr_accessor :show_first_column, :show_last_column, :show_row_stripes, :show_column_stripes
29
28
  serializable_attributes :show_first_column, :show_last_column, :show_row_stripes, :show_column_stripes,
30
- :name
29
+ :name
31
30
 
32
31
  # Initialize all the values to false as Excel requires them to
33
32
  # explicitly be disabled or all will show.
@@ -1,11 +1,10 @@
1
1
  module Axlsx
2
-
3
2
  # A simple, self serializing class for storing tables
4
3
  class Tables < SimpleTypedList
5
-
6
4
  # creates a new Tables object
7
5
  def initialize(worksheet)
8
6
  raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
7
+
9
8
  super Table
10
9
  @worksheet = worksheet
11
10
  end
@@ -17,7 +16,8 @@ module Axlsx
17
16
  # returns the relationships required by this collection
18
17
  def relationships
19
18
  return [] if empty?
20
- map{ |table| Relationship.new(table, TABLE_R, "../#{table.pn}") }
19
+
20
+ map { |table| Relationship.new(table, TABLE_R, "../#{table.pn}") }
21
21
  end
22
22
 
23
23
  # renders the tables xml
@@ -25,10 +25,10 @@ module Axlsx
25
25
  # @return [String]
26
26
  def to_xml_string(str = "")
27
27
  return if empty?
28
+
28
29
  str << "<tableParts count='#{size}'>"
29
30
  each { |table| str << "<tablePart r:id='#{table.rId}'/>" }
30
31
  str << '</tableParts>'
31
32
  end
32
33
  end
33
-
34
34
  end
@@ -1,9 +1,6 @@
1
- # encoding: UTF-8
2
-
3
1
  require_relative "border_creator"
4
2
 
5
3
  module Axlsx
6
-
7
4
  # The Worksheet class represents a worksheet in the workbook.
8
5
  class Worksheet
9
6
  include Axlsx::OptionsParser
@@ -16,12 +13,15 @@ module Axlsx
16
13
  # @option options [Hash] page_margins A hash containing page margins for this worksheet. @see PageMargins
17
14
  # @option options [Hash] print_options A hash containing print options for this worksheet. @see PrintOptions
18
15
  # @option options [Hash] header_footer A hash containing header/footer options for this worksheet. @see HeaderFooter
19
- # @option options [Boolean] show_gridlines indicates if gridlines should be shown for this sheet.
20
- def initialize(wb, options={})
16
+ # @option options [Boolean] show_gridlines Whether gridlines should be shown for this sheet.
17
+ # @option options [Boolean] escape_formulas Whether formulas should be escaped by default. Can be overridden at a
18
+ # row/cell level.
19
+ def initialize(wb, options = {})
21
20
  self.workbook = wb
22
21
  @sheet_protection = nil
23
22
  initialize_page_options(options)
24
23
  parse_options options
24
+ self.escape_formulas = wb.escape_formulas unless defined? @escape_formulas
25
25
  @workbook.worksheets << self
26
26
  @sheet_id = index + 1
27
27
  yield self if block_given?
@@ -43,7 +43,21 @@ module Axlsx
43
43
  # The name of the worksheet
44
44
  # @return [String]
45
45
  def name
46
- @name ||= "Sheet" + (index+1).to_s
46
+ @name ||= "Sheet" + (index + 1).to_s
47
+ end
48
+
49
+ # Whether to treat values starting with an equals sign as formulas or as literal strings.
50
+ # Allowing user-generated data to be interpreted as formulas is a security risk.
51
+ # See https://www.owasp.org/index.php/CSV_Injection for details.
52
+ # @return [Boolean]
53
+ attr_reader :escape_formulas
54
+
55
+ # Sets whether to treat values starting with an equals sign as formulas or as literal strings.
56
+ # @param [Boolean] value The value to set.
57
+ # @return [Boolean]
58
+ def escape_formulas=(value)
59
+ Axlsx.validate_boolean(value)
60
+ @escape_formulas = value
47
61
  end
48
62
 
49
63
  # Specifies the visible state of this sheet. Allowed states are
@@ -102,13 +116,13 @@ module Axlsx
102
116
  # The tables in this worksheet
103
117
  # @return [Array] of Table
104
118
  def tables
105
- @tables ||= Tables.new self
119
+ @tables ||= Tables.new self
106
120
  end
107
121
 
108
122
  # The pivot tables in this worksheet
109
123
  # @return [Array] of Table
110
124
  def pivot_tables
111
- @pivot_tables ||= PivotTables.new self
125
+ @pivot_tables ||= PivotTables.new self
112
126
  end
113
127
 
114
128
  # A collection of column breaks added to this worksheet
@@ -172,10 +186,10 @@ module Axlsx
172
186
  # @see #page_setup
173
187
  def fit_to_page?
174
188
  return false unless Axlsx.instance_values_for(self).keys.include?('page_setup')
189
+
175
190
  page_setup.fit_to_page?
176
191
  end
177
192
 
178
-
179
193
  # Column info for the sheet
180
194
  # @return [SimpleTypedList]
181
195
  def column_info
@@ -311,7 +325,7 @@ module Axlsx
311
325
  # @param [String] name
312
326
  def name=(name)
313
327
  validate_sheet_name name
314
- @name=Axlsx::coder.encode(name)
328
+ @name = Axlsx::coder.encode(name)
315
329
  end
316
330
 
317
331
  # The auto filter range for the worksheet
@@ -329,13 +343,13 @@ module Axlsx
329
343
  # The part name of this worksheet
330
344
  # @return [String]
331
345
  def pn
332
- "#{WORKSHEET_PN % (index+1)}"
346
+ "#{WORKSHEET_PN % (index + 1)}"
333
347
  end
334
348
 
335
349
  # The relationship part name of this worksheet
336
350
  # @return [String]
337
351
  def rels_pn
338
- "#{WORKSHEET_RELS_PN % (index+1)}"
352
+ "#{WORKSHEET_RELS_PN % (index + 1)}"
339
353
  end
340
354
 
341
355
  # The relationship id of this worksheet.
@@ -412,7 +426,8 @@ module Axlsx
412
426
  # sign as formula (default) or as simple string.
413
427
  # Allowing user generated data to be interpreted as formulas can be dangerous
414
428
  # (see https://www.owasp.org/index.php/CSV_Injection for details).
415
- def add_row(values=[], options={})
429
+ def add_row(values = [], options = {})
430
+ options[:escape_formulas] = escape_formulas if options[:escape_formulas].nil?
416
431
  row = Row.new(self, values, options)
417
432
  update_column_info row, options.delete(:widths)
418
433
  yield row if block_given?
@@ -432,7 +447,7 @@ module Axlsx
432
447
  # @see ConditionalFormattingRule#initialize
433
448
  # @see file:examples/example_conditional_formatting.rb
434
449
  def add_conditional_formatting(cells, rules)
435
- cf = ConditionalFormatting.new( :sqref => cells )
450
+ cf = ConditionalFormatting.new(:sqref => cells)
436
451
  cf.add_rules rules
437
452
  conditional_formattings << cf
438
453
  conditional_formattings
@@ -453,7 +468,7 @@ module Axlsx
453
468
  # @param [Hash] options for the hyperlink
454
469
  # @see WorksheetHyperlink for a list of options
455
470
  # @return [WorksheetHyperlink]
456
- def add_hyperlink(options={})
471
+ def add_hyperlink(options = {})
457
472
  hyperlinks.add(options)
458
473
  end
459
474
 
@@ -470,33 +485,33 @@ module Axlsx
470
485
  # @see Bar3DChart
471
486
  # @see Line3DChart
472
487
  # @see README for examples
473
- def add_chart(chart_type, options={})
488
+ def add_chart(chart_type, options = {})
474
489
  chart = worksheet_drawing.add_chart(chart_type, options)
475
490
  yield chart if block_given?
476
491
  chart
477
492
  end
478
493
 
479
494
  # needs documentation
480
- def add_table(ref, options={})
495
+ def add_table(ref, options = {})
481
496
  tables << Table.new(ref, self, options)
482
497
  yield tables.last if block_given?
483
498
  tables.last
484
499
  end
485
500
 
486
- def add_pivot_table(ref, range, options={})
501
+ def add_pivot_table(ref, range, options = {})
487
502
  pivot_tables << PivotTable.new(ref, range, self, options)
488
503
  yield pivot_tables.last if block_given?
489
504
  pivot_tables.last
490
505
  end
491
506
 
492
507
  # Shortcut to worsksheet_comments#add_comment
493
- def add_comment(options={})
508
+ def add_comment(options = {})
494
509
  worksheet_comments.add_comment(options)
495
510
  end
496
511
 
497
512
  # Adds a media item to the worksheets drawing
498
513
  # @option [Hash] options options passed to drawing.add_image
499
- def add_image(options={})
514
+ def add_image(options = {})
500
515
  image = worksheet_drawing.add_image(options)
501
516
  yield image if block_given?
502
517
  image
@@ -510,10 +525,10 @@ module Axlsx
510
525
  def add_page_break(cell)
511
526
  DataTypeValidator.validate :worksheet_page_break, [String, Cell], cell
512
527
  column_index, row_index = if cell.is_a?(String)
513
- Axlsx.name_to_indices(cell)
514
- else
515
- cell.pos
516
- end
528
+ Axlsx.name_to_indices(cell)
529
+ else
530
+ cell.pos
531
+ end
517
532
  if column_index > 0
518
533
  col_breaks.add_break(:id => column_index)
519
534
  end
@@ -530,6 +545,7 @@ module Axlsx
530
545
  def column_widths(*widths)
531
546
  widths.each_with_index do |value, index|
532
547
  next if value == nil
548
+
533
549
  Axlsx::validate_unsigned_numeric(value) unless value == nil
534
550
  find_or_create_column_info(index).width = value
535
551
  end
@@ -543,7 +559,7 @@ module Axlsx
543
559
  # @note You can also specify the style for specific columns in the call to add_row by using an array for the :styles option
544
560
  # @see Worksheet#add_row
545
561
  # @see README.md for an example
546
- def col_style(index, style, options={})
562
+ def col_style(index, style, options = {})
547
563
  offset = options.delete(:row_offset) || 0
548
564
  cells = @rows[(offset..-1)].map { |row| row[index] }.flatten.compact
549
565
  cells.each { |cell| cell.style = style }
@@ -557,7 +573,7 @@ module Axlsx
557
573
  # @note You can also specify the style in the add_row call
558
574
  # @see Worksheet#add_row
559
575
  # @see README.md for an example
560
- def row_style(index, style, options={})
576
+ def row_style(index, style, options = {})
561
577
  offset = options.delete(:col_offset) || 0
562
578
  cells = cols[(offset..-1)].map { |column| column[index] }.flatten.compact
563
579
  cells.each { |cell| cell.style = style }
@@ -587,19 +603,30 @@ module Axlsx
587
603
  # Set the style for cells in a specific column
588
604
  # @param [String|Array] cell references
589
605
  # @param [Hash|Array|Symbol] border options
590
- def add_border(cell_refs, options = Axlsx::Border::EDGES)
606
+ def add_border(cell_refs, options = nil)
607
+ if options.is_a?(Hash)
608
+ border_edges = options[:edges]
609
+ border_style = options[:style]
610
+ border_color = options[:color]
611
+ else
612
+ border_edges = options
613
+ end
614
+
591
615
  if !cell_refs.is_a?(Array)
592
616
  cell_refs = [cell_refs]
593
617
  end
594
618
 
595
619
  cell_refs.each do |cell_ref|
596
- cells = self[cell_ref]
597
- Axlsx::BorderCreator.new(self, cells, options).draw
620
+ item = self[cell_ref]
621
+
622
+ cells = item.is_a?(Array) ? item : [item]
623
+
624
+ Axlsx::BorderCreator.new(worksheet: self, cells: cells, edges: border_edges, style: border_style, color: border_color).draw
598
625
  end
599
626
  end
600
627
 
601
628
  # Returns a sheet node serialization for this sheet in the workbook.
602
- def to_sheet_node_xml_string(str='')
629
+ def to_sheet_node_xml_string(str = '')
603
630
  add_autofilter_defined_name_to_workbook
604
631
  str << '<sheet '
605
632
  serialized_attributes str
@@ -610,7 +637,7 @@ module Axlsx
610
637
  # Serializes the worksheet object to an xml string
611
638
  # This intentionally does not use nokogiri for performance reasons
612
639
  # @return [String]
613
- def to_xml_string str=''
640
+ def to_xml_string str = ''
614
641
  add_autofilter_defined_name_to_workbook
615
642
  auto_filter.apply if auto_filter.range
616
643
  str << '<?xml version="1.0" encoding="UTF-8"?>'
@@ -639,7 +666,7 @@ module Axlsx
639
666
  def [](cell_def)
640
667
  return rows[cell_def] if cell_def.is_a?(Integer)
641
668
 
642
- parts = cell_def.split(':').map{ |part| name_to_cell part }
669
+ parts = cell_def.split(':').map { |part| name_to_cell part }
643
670
 
644
671
  if parts.size == 1
645
672
  parts.first
@@ -713,7 +740,7 @@ module Axlsx
713
740
  end
714
741
 
715
742
  def outline(collection, range, level = 1, collapsed = true)
716
- range.each do |index|
743
+ range.each do |index|
717
744
  unless (item = collection[index]).nil?
718
745
  item.outline_level = level
719
746
  item.hidden = collapsed
@@ -726,9 +753,11 @@ module Axlsx
726
753
  DataTypeValidator.validate :worksheet_name, String, name
727
754
  # ignore first character (BOM) after encoding to utf16 because Excel does so, too.
728
755
  raise ArgumentError, (ERR_SHEET_NAME_EMPTY) if name.empty?
756
+
729
757
  character_length = name.encode("utf-16")[1..-1].encode("utf-16").bytesize / 2
730
758
  raise ArgumentError, (ERR_SHEET_NAME_TOO_LONG % name) if character_length > WORKSHEET_MAX_NAME_LENGTH
731
759
  raise ArgumentError, (ERR_SHEET_NAME_CHARACTER_FORBIDDEN % name) if WORKSHEET_NAME_FORBIDDEN_CHARS.any? { |char| name.include? char }
760
+
732
761
  name = Axlsx::coder.encode(name)
733
762
  sheet_names = @workbook.worksheets.reject { |s| s == self }.map { |s| s.name }
734
763
  raise ArgumentError, (ERR_DUPLICATE_SHEET_NAME % name) if sheet_names.include?(name)
@@ -783,7 +812,6 @@ module Axlsx
783
812
  @merged_cells ||= MergedCells.new self
784
813
  end
785
814
 
786
-
787
815
  # Helper method for parsingout the root node for worksheet
788
816
  # @return [String]
789
817
  def worksheet_node
@@ -822,8 +850,8 @@ module Axlsx
822
850
 
823
851
  def add_autofilter_defined_name_to_workbook
824
852
  return if !auto_filter.range
853
+
825
854
  workbook.add_defined_name auto_filter.defined_name, name: '_xlnm._FilterDatabase', local_sheet_id: index, hidden: 1
826
855
  end
827
-
828
856
  end
829
857
  end
@@ -1,13 +1,12 @@
1
1
  module Axlsx
2
-
3
2
  # A wraper class for comments that defines its on worksheet
4
3
  # serailization
5
4
  class WorksheetComments
6
-
7
5
  # Creates a new WorksheetComments object
8
6
  # param [Worksheet] worksheet The worksheet comments in thes object belong to
9
7
  def initialize(worksheet)
10
8
  raise ArugumentError, 'You must provide a worksheet' unless worksheet.is_a?(Worksheet)
9
+
11
10
  @worksheet = worksheet
12
11
  end
13
12
 
@@ -22,18 +21,18 @@ module Axlsx
22
21
  # Adds a comment
23
22
  # @param [Hash] options
24
23
  # @see Comments#add_comment
25
- def add_comment(options={})
24
+ def add_comment(options = {})
26
25
  comments.add_comment(options)
27
- end
26
+ end
28
27
 
29
28
  # The relationships defined by this objects comments collection
30
29
  # @return [Relationships]
31
30
  def relationships
32
31
  return [] unless has_comments?
32
+
33
33
  comments.relationships
34
34
  end
35
35
 
36
-
37
36
  # Helper method to tell us if there are comments in the comments collection
38
37
  # @return [Boolean]
39
38
  def has_comments?
@@ -44,7 +43,7 @@ module Axlsx
44
43
  # @see Relationship#Id
45
44
  # @return [String]
46
45
  def drawing_rId
47
- comments.relationships.find{ |r| r.Type == VML_DRAWING_R }.Id
46
+ comments.relationships.find { |r| r.Type == VML_DRAWING_R }.Id
48
47
  end
49
48
 
50
49
  # Seraalize the object
@@ -52,6 +51,7 @@ module Axlsx
52
51
  # @return [String]
53
52
  def to_xml_string(str = '')
54
53
  return unless has_comments?
54
+
55
55
  str << "<legacyDrawing r:id='#{drawing_rId}' />"
56
56
  end
57
57
  end
@@ -1,15 +1,14 @@
1
1
  module Axlsx
2
-
3
2
  # This is a utility class for serialing the drawing node in a
4
3
  # worksheet. Drawing objects have their own serialization that exports
5
4
  # a drawing document. This is only for the single node in the
6
5
  # worksheet
7
6
  class WorksheetDrawing
8
-
9
7
  # Creates a new WorksheetDrawing
10
8
  # @param [Worksheet] worksheet
11
9
  def initialize(worksheet)
12
10
  raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet)
11
+
13
12
  @worksheet = worksheet
14
13
  @drawing = nil
15
14
  end
@@ -45,6 +44,7 @@ module Axlsx
45
44
  # @return [Relationship]
46
45
  def relationship
47
46
  return unless has_drawing?
47
+
48
48
  Relationship.new(self, DRAWING_R, "../#{drawing.pn}")
49
49
  end
50
50
 
@@ -52,6 +52,7 @@ module Axlsx
52
52
  # @param [String] str
53
53
  def to_xml_string(str = '')
54
54
  return unless has_drawing?
55
+
55
56
  str << "<drawing r:id='#{relationship.Id}'/>"
56
57
  end
57
58
  end