caxlsx 2.0.2 → 3.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 (181) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +41 -33
  4. data/Rakefile +9 -11
  5. data/examples/auto_filter.rb +10 -1
  6. data/examples/conditional_formatting/example_conditional_formatting.rb +18 -3
  7. data/examples/data_validation.rb +57 -40
  8. data/examples/example.rb +115 -7
  9. data/examples/merge_cells.rb +17 -0
  10. data/examples/no_grid_with_borders.rb +18 -0
  11. data/examples/pivot_test.rb +63 -0
  12. data/examples/split.rb +16 -0
  13. data/lib/axlsx.rb +34 -15
  14. data/lib/axlsx/content_type/abstract_content_type.rb +1 -1
  15. data/lib/axlsx/content_type/content_type.rb +1 -1
  16. data/lib/axlsx/doc_props/app.rb +1 -1
  17. data/lib/axlsx/doc_props/core.rb +5 -5
  18. data/lib/axlsx/drawing/area_chart.rb +99 -0
  19. data/lib/axlsx/drawing/area_series.rb +110 -0
  20. data/lib/axlsx/drawing/axes.rb +1 -1
  21. data/lib/axlsx/drawing/axis.rb +12 -9
  22. data/lib/axlsx/drawing/bar_3D_chart.rb +13 -13
  23. data/lib/axlsx/drawing/bar_chart.rb +143 -0
  24. data/lib/axlsx/drawing/bar_series.rb +9 -9
  25. data/lib/axlsx/drawing/bubble_chart.rb +59 -0
  26. data/lib/axlsx/drawing/bubble_series.rb +63 -0
  27. data/lib/axlsx/drawing/cat_axis.rb +5 -5
  28. data/lib/axlsx/drawing/chart.rb +52 -8
  29. data/lib/axlsx/drawing/d_lbls.rb +3 -3
  30. data/lib/axlsx/drawing/drawing.rb +6 -1
  31. data/lib/axlsx/drawing/graphic_frame.rb +3 -3
  32. data/lib/axlsx/drawing/hyperlink.rb +1 -3
  33. data/lib/axlsx/drawing/line_3D_chart.rb +2 -2
  34. data/lib/axlsx/drawing/line_chart.rb +10 -10
  35. data/lib/axlsx/drawing/line_series.rb +32 -3
  36. data/lib/axlsx/drawing/marker.rb +1 -1
  37. data/lib/axlsx/drawing/num_data.rb +4 -4
  38. data/lib/axlsx/drawing/num_data_source.rb +6 -6
  39. data/lib/axlsx/drawing/num_val.rb +3 -1
  40. data/lib/axlsx/drawing/one_cell_anchor.rb +3 -2
  41. data/lib/axlsx/drawing/pic.rb +25 -19
  42. data/lib/axlsx/drawing/picture_locking.rb +1 -3
  43. data/lib/axlsx/drawing/pie_3D_chart.rb +5 -6
  44. data/lib/axlsx/drawing/pie_series.rb +6 -6
  45. data/lib/axlsx/drawing/scaling.rb +6 -6
  46. data/lib/axlsx/drawing/scatter_chart.rb +10 -10
  47. data/lib/axlsx/drawing/scatter_series.rb +40 -7
  48. data/lib/axlsx/drawing/ser_axis.rb +2 -2
  49. data/lib/axlsx/drawing/series.rb +3 -3
  50. data/lib/axlsx/drawing/series_title.rb +2 -2
  51. data/lib/axlsx/drawing/str_data.rb +3 -3
  52. data/lib/axlsx/drawing/str_val.rb +3 -1
  53. data/lib/axlsx/drawing/title.rb +22 -4
  54. data/lib/axlsx/drawing/two_cell_anchor.rb +6 -1
  55. data/lib/axlsx/drawing/val_axis.rb +1 -1
  56. data/lib/axlsx/drawing/view_3D.rb +2 -2
  57. data/lib/axlsx/drawing/vml_drawing.rb +1 -1
  58. data/lib/axlsx/package.rb +33 -31
  59. data/lib/axlsx/rels/relationship.rb +1 -1
  60. data/lib/axlsx/rels/relationships.rb +7 -4
  61. data/lib/axlsx/stylesheet/border_pr.rb +2 -2
  62. data/lib/axlsx/stylesheet/cell_alignment.rb +1 -3
  63. data/lib/axlsx/stylesheet/cell_protection.rb +1 -3
  64. data/lib/axlsx/stylesheet/cell_style.rb +1 -3
  65. data/lib/axlsx/stylesheet/color.rb +1 -3
  66. data/lib/axlsx/stylesheet/font.rb +1 -1
  67. data/lib/axlsx/stylesheet/gradient_stop.rb +1 -1
  68. data/lib/axlsx/stylesheet/num_fmt.rb +10 -3
  69. data/lib/axlsx/stylesheet/pattern_fill.rb +1 -1
  70. data/lib/axlsx/stylesheet/styles.rb +7 -7
  71. data/lib/axlsx/stylesheet/table_style_element.rb +1 -3
  72. data/lib/axlsx/util/accessors.rb +6 -6
  73. data/lib/axlsx/util/constants.rb +107 -99
  74. data/lib/axlsx/util/mime_type_utils.rb +11 -0
  75. data/lib/axlsx/util/options_parser.rb +2 -1
  76. data/lib/axlsx/util/parser.rb +4 -4
  77. data/lib/axlsx/util/serialized_attributes.rb +16 -6
  78. data/lib/axlsx/util/simple_typed_list.rb +28 -52
  79. data/lib/axlsx/util/storage.rb +4 -4
  80. data/lib/axlsx/util/validators.rb +29 -17
  81. data/lib/axlsx/version.rb +1 -1
  82. data/lib/axlsx/workbook/defined_name.rb +11 -12
  83. data/lib/axlsx/workbook/defined_names.rb +2 -2
  84. data/lib/axlsx/workbook/shared_strings_table.rb +5 -5
  85. data/lib/axlsx/workbook/workbook.rb +36 -11
  86. data/lib/axlsx/workbook/workbook_view.rb +80 -0
  87. data/lib/axlsx/workbook/workbook_views.rb +22 -0
  88. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +2 -2
  89. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +1 -3
  90. data/lib/axlsx/workbook/worksheet/break.rb +1 -3
  91. data/lib/axlsx/workbook/worksheet/cell.rb +136 -74
  92. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +63 -43
  93. data/lib/axlsx/workbook/worksheet/cfvo.rb +1 -3
  94. data/lib/axlsx/workbook/worksheet/cfvos.rb +4 -1
  95. data/lib/axlsx/workbook/worksheet/col.rb +7 -10
  96. data/lib/axlsx/workbook/worksheet/col_breaks.rb +2 -2
  97. data/lib/axlsx/workbook/worksheet/cols.rb +5 -2
  98. data/lib/axlsx/workbook/worksheet/comment.rb +5 -6
  99. data/lib/axlsx/workbook/worksheet/comments.rb +9 -12
  100. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +1 -1
  101. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +1 -1
  102. data/lib/axlsx/workbook/worksheet/data_bar.rb +4 -6
  103. data/lib/axlsx/workbook/worksheet/data_validation.rb +6 -4
  104. data/lib/axlsx/workbook/worksheet/dimension.rb +2 -2
  105. data/lib/axlsx/workbook/worksheet/header_footer.rb +6 -8
  106. data/lib/axlsx/workbook/worksheet/icon_set.rb +3 -5
  107. data/lib/axlsx/workbook/worksheet/merged_cells.rb +4 -2
  108. data/lib/axlsx/workbook/worksheet/outline_pr.rb +33 -0
  109. data/lib/axlsx/workbook/worksheet/page_margins.rb +1 -3
  110. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +1 -1
  111. data/lib/axlsx/workbook/worksheet/page_setup.rb +21 -23
  112. data/lib/axlsx/workbook/worksheet/pane.rb +1 -3
  113. data/lib/axlsx/workbook/worksheet/pivot_table.rb +44 -28
  114. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +4 -4
  115. data/lib/axlsx/workbook/worksheet/print_options.rb +1 -3
  116. data/lib/axlsx/workbook/worksheet/protected_range.rb +1 -3
  117. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +5 -2
  118. data/lib/axlsx/workbook/worksheet/rich_text.rb +55 -0
  119. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +250 -0
  120. data/lib/axlsx/workbook/worksheet/row.rb +40 -51
  121. data/lib/axlsx/workbook/worksheet/row_breaks.rb +2 -2
  122. data/lib/axlsx/workbook/worksheet/selection.rb +1 -3
  123. data/lib/axlsx/workbook/worksheet/sheet_data.rb +3 -1
  124. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +21 -3
  125. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +1 -3
  126. data/lib/axlsx/workbook/worksheet/table.rb +6 -6
  127. data/lib/axlsx/workbook/worksheet/table_style_info.rb +1 -3
  128. data/lib/axlsx/workbook/worksheet/tables.rb +4 -1
  129. data/lib/axlsx/workbook/worksheet/worksheet.rb +64 -78
  130. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +10 -10
  131. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +3 -3
  132. data/test/drawing/tc_area_chart.rb +39 -0
  133. data/test/drawing/tc_area_series.rb +71 -0
  134. data/test/drawing/tc_axis.rb +27 -0
  135. data/test/drawing/tc_bar_chart.rb +71 -0
  136. data/test/drawing/tc_bubble_chart.rb +44 -0
  137. data/test/drawing/tc_bubble_series.rb +21 -0
  138. data/test/drawing/tc_chart.rb +23 -10
  139. data/test/drawing/tc_data_source.rb +6 -0
  140. data/test/drawing/tc_drawing.rb +2 -2
  141. data/test/drawing/tc_line_chart.rb +5 -5
  142. data/test/drawing/tc_line_series.rb +47 -6
  143. data/test/drawing/tc_pic.rb +11 -15
  144. data/test/drawing/tc_scatter_series.rb +36 -1
  145. data/test/drawing/tc_str_val.rb +9 -0
  146. data/test/drawing/tc_title.rb +5 -0
  147. data/test/stylesheet/tc_styles.rb +2 -2
  148. data/test/tc_axlsx.rb +31 -0
  149. data/test/tc_helper.rb +2 -0
  150. data/test/tc_package.rb +19 -1
  151. data/test/util/tc_mime_type_utils.rb +13 -0
  152. data/test/util/tc_simple_typed_list.rb +2 -3
  153. data/test/util/tc_validators.rb +34 -10
  154. data/test/workbook/tc_defined_name.rb +12 -4
  155. data/test/workbook/tc_shared_strings_table.rb +16 -1
  156. data/test/workbook/tc_workbook.rb +38 -3
  157. data/test/workbook/tc_workbook_view.rb +50 -0
  158. data/test/workbook/worksheet/auto_filter/tc_filters.rb +1 -1
  159. data/test/workbook/worksheet/tc_break.rb +1 -1
  160. data/test/workbook/worksheet/tc_cell.rb +76 -8
  161. data/test/workbook/worksheet/tc_col.rb +2 -2
  162. data/test/workbook/worksheet/tc_conditional_formatting.rb +2 -2
  163. data/test/workbook/worksheet/tc_data_bar.rb +1 -1
  164. data/test/workbook/worksheet/tc_data_validation.rb +11 -11
  165. data/test/workbook/worksheet/tc_header_footer.rb +2 -2
  166. data/test/workbook/worksheet/tc_icon_set.rb +1 -1
  167. data/test/workbook/worksheet/tc_outline_pr.rb +19 -0
  168. data/test/workbook/worksheet/tc_page_setup.rb +3 -3
  169. data/test/workbook/worksheet/tc_pivot_table.rb +21 -6
  170. data/test/workbook/worksheet/tc_print_options.rb +1 -1
  171. data/test/workbook/worksheet/tc_rich_text.rb +44 -0
  172. data/test/workbook/worksheet/tc_rich_text_run.rb +172 -0
  173. data/test/workbook/worksheet/tc_row.rb +7 -2
  174. data/test/workbook/worksheet/tc_sheet_calc_pr.rb +1 -1
  175. data/test/workbook/worksheet/tc_sheet_format_pr.rb +4 -4
  176. data/test/workbook/worksheet/tc_sheet_pr.rb +26 -4
  177. data/test/workbook/worksheet/tc_sheet_protection.rb +5 -5
  178. data/test/workbook/worksheet/tc_sheet_view.rb +4 -4
  179. data/test/workbook/worksheet/tc_table.rb +2 -3
  180. data/test/workbook/worksheet/tc_worksheet.rb +99 -45
  181. metadata +89 -16
@@ -14,7 +14,7 @@ module Axlsx
14
14
  # @see Break
15
15
  def add_break(options)
16
16
  # force feed the excel default
17
- @list << Break.new(options.merge(:max => 16383, :man => true))
17
+ self << Break.new(options.merge(:max => 16383, :man => true))
18
18
  last
19
19
  end
20
20
 
@@ -25,7 +25,7 @@ module Axlsx
25
25
  # </rowBreaks>
26
26
  def to_xml_string(str='')
27
27
  return if empty?
28
- str << '<rowBreaks count="' << @list.size.to_s << '" manualBreakCount="' << @list.size.to_s << '">'
28
+ str << ('<rowBreaks count="' << self.size.to_s << '" manualBreakCount="' << self.size.to_s << '">')
29
29
  each { |brk| brk.to_xml_string(str) }
30
30
  str << '</rowBreaks>'
31
31
  end
@@ -95,9 +95,7 @@ module Axlsx
95
95
  # @param [String] str
96
96
  # @return [String]
97
97
  def to_xml_string(str = '')
98
- str << '<selection '
99
- serialized_attributes str
100
- str << '/>'
98
+ serialized_tag 'selection', str
101
99
  end
102
100
  end
103
101
  end
@@ -17,7 +17,9 @@ module Axlsx
17
17
  # @return [String]
18
18
  def to_xml_string(str = '')
19
19
  str << '<sheetData>'
20
- worksheet.rows.each_with_index{ |row, index| row.to_xml_string(index, str) }
20
+ worksheet.rows.each_with_index do |row, index|
21
+ row.to_xml_string(index, str)
22
+ end
21
23
  str << '</sheetData>'
22
24
  end
23
25
 
@@ -1,6 +1,6 @@
1
1
  module Axlsx
2
2
 
3
- # The SheetPr class manages serialization fo a worksheet's sheetPr element.
3
+ # The SheetPr class manages serialization of a worksheet's sheetPr element.
4
4
  class SheetPr
5
5
  include Axlsx::OptionsParser
6
6
  include Axlsx::Accessors
@@ -8,7 +8,7 @@ module Axlsx
8
8
 
9
9
  serializable_attributes :sync_horizontal,
10
10
  :sync_vertical,
11
- :transtion_evaluation,
11
+ :transition_evaluation,
12
12
  :transition_entry,
13
13
  :published,
14
14
  :filter_mode,
@@ -20,7 +20,7 @@ module Axlsx
20
20
  # waving magic show to set up the attriubte accessors
21
21
  boolean_attr_accessor :sync_horizontal,
22
22
  :sync_vertical,
23
- :transtion_evaluation,
23
+ :transition_evaluation,
24
24
  :transition_entry,
25
25
  :published,
26
26
  :filter_mode,
@@ -33,6 +33,7 @@ module Axlsx
33
33
  def initialize(worksheet, options={})
34
34
  raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
35
35
  @worksheet = worksheet
36
+ @outline_pr = nil
36
37
  parse_options options
37
38
  end
38
39
 
@@ -40,12 +41,18 @@ module Axlsx
40
41
  # @return [Worksheet]
41
42
  attr_reader :worksheet
42
43
 
44
+ # The tab color of the sheet.
45
+ # @return [Color]
46
+ attr_reader :tab_color
47
+
43
48
  # Serialize the object
44
49
  # @param [String] str serialized output will be appended to this object if provided.
45
50
  # @return [String]
46
51
  def to_xml_string(str = '')
47
52
  update_properties
48
53
  str << "<sheetPr #{serialized_attributes}>"
54
+ tab_color.to_xml_string(str, 'tabColor') if tab_color
55
+ outline_pr.to_xml_string(str) if @outline_pr
49
56
  page_setup_pr.to_xml_string(str)
50
57
  str << "</sheetPr>"
51
58
  end
@@ -55,6 +62,17 @@ module Axlsx
55
62
  def page_setup_pr
56
63
  @page_setup_pr ||= PageSetUpPr.new
57
64
  end
65
+
66
+ # The OutlinePr for this sheet pr object
67
+ # @return [OutlinePr]
68
+ def outline_pr
69
+ @outline_pr ||= OutlinePr.new
70
+ end
71
+
72
+ # @see tab_color
73
+ def tab_color=(v)
74
+ @tab_color ||= Color.new(:rgb => v)
75
+ end
58
76
 
59
77
  private
60
78
 
@@ -76,9 +76,7 @@ module Axlsx
76
76
  # @param [String] str
77
77
  # @return [String]
78
78
  def to_xml_string(str = '')
79
- str << '<sheetProtection '
80
- serialized_attributes str
81
- str << '/>'
79
+ serialized_tag('sheetProtection', str)
82
80
  end
83
81
 
84
82
  private
@@ -58,7 +58,7 @@ module Axlsx
58
58
  # @param [String, Cell] v
59
59
  # @return [Title]
60
60
  def name=(v)
61
- DataTypeValidator.validate "#{self.class}.name", [String], v
61
+ DataTypeValidator.validate :table_name, [String], v
62
62
  if v.is_a?(String)
63
63
  @name = v
64
64
  end
@@ -75,12 +75,12 @@ module Axlsx
75
75
  # @return [String]
76
76
  def to_xml_string(str = '')
77
77
  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/,'_') << '" '
79
- str << 'ref="' << @ref << '" totalsRowShown="0">'
80
- str << '<autoFilter ref="' << @ref << '"/>'
81
- str << '<tableColumns count="' << header_cells.length.to_s << '">'
78
+ str << ('<table xmlns="' << XML_NS << '" id="' << (index+1).to_s << '" name="' << @name << '" displayName="' << @name.gsub(/\s/,'_') << '" ')
79
+ str << ('ref="' << @ref << '" totalsRowShown="0">')
80
+ str << ('<autoFilter ref="' << @ref << '"/>')
81
+ str << ('<tableColumns count="' << header_cells.length.to_s << '">')
82
82
  header_cells.each_with_index do |cell,index|
83
- str << '<tableColumn id ="' << (index+1).to_s << '" name="' << cell.value << '"/>'
83
+ str << ('<tableColumn id ="' << (index+1).to_s << '" name="' << cell.value << '"/>')
84
84
  end
85
85
  str << '</tableColumns>'
86
86
  table_style_info.to_xml_string(str)
@@ -43,9 +43,7 @@ module Axlsx
43
43
  # seralizes this object to an xml string
44
44
  # @param [String] str the string to contact this objects serialization to.
45
45
  def to_xml_string(str = '')
46
- str << '<tableStyleInfo '
47
- serialized_attributes str
48
- str << '/>'
46
+ serialized_tag('tableStyleInfo', str)
49
47
  end
50
48
  end
51
49
  end
@@ -20,10 +20,13 @@ module Axlsx
20
20
  map{ |table| Relationship.new(table, TABLE_R, "../#{table.pn}") }
21
21
  end
22
22
 
23
+ # renders the tables xml
24
+ # @param [String] str
25
+ # @return [String]
23
26
  def to_xml_string(str = "")
24
27
  return if empty?
25
28
  str << "<tableParts count='#{size}'>"
26
- @list.each { |table| str << "<tablePart r:id='#{table.rId}'/>" }
29
+ each { |table| str << "<tablePart r:id='#{table.rId}'/>" }
27
30
  str << '</tableParts>'
28
31
  end
29
32
  end
@@ -4,14 +4,10 @@ module Axlsx
4
4
  # The Worksheet class represents a worksheet in the workbook.
5
5
  class Worksheet
6
6
  include Axlsx::OptionsParser
7
-
7
+ include Axlsx::SerializedAttributes
8
8
  # definition of characters which are less than the maximum width of 0-9 in the default font for use in String#count.
9
9
  # This is used for autowidth calculations
10
- # @return [String]
11
- def self.thin_chars
12
- # removed 'e' and 'y' from this list - as a GUESS
13
- @thin_chars ||= "^.acfijklrstxzFIJL()-"
14
- end
10
+ THIN_CHARS = '^.acfijklrstxzFIJL()-'.freeze
15
11
 
16
12
  # Creates a new worksheet.
17
13
  # @note the recommended way to manage worksheets is Workbook#add_worksheet
@@ -24,12 +20,15 @@ module Axlsx
24
20
  def initialize(wb, options={})
25
21
  self.workbook = wb
26
22
  @sheet_protection = nil
27
-
28
23
  initialize_page_options(options)
29
24
  parse_options options
30
25
  @workbook.worksheets << self
26
+ @sheet_id = index + 1
27
+ yield self if block_given?
31
28
  end
32
29
 
30
+ serializable_attributes :sheet_id, :state
31
+
33
32
  # Initalizes page margin, setup and print options
34
33
  # @param [Hash] options Options passed in from the initializer
35
34
  def initialize_page_options(options)
@@ -44,7 +43,23 @@ module Axlsx
44
43
  # The name of the worksheet
45
44
  # @return [String]
46
45
  def name
47
- @name ||= "Sheet" + (index+1).to_s
46
+ @name ||= "Sheet" + (index+1).to_s
47
+ end
48
+
49
+ # Specifies the visible state of this sheet. Allowed states are
50
+ # :visible, :hidden or :very_hidden. The default value is :visible.
51
+ #
52
+ # Worksheets in the :hidden state can be shown using the sheet formatting properties in excel.
53
+ # :very_hidden sheets should be inaccessible to end users.
54
+ # @param [Symbol] sheet_state The visible state for this sheet.
55
+ def state=(sheet_state)
56
+ RestrictionValidator.validate :worksheet_state, [:visible, :hidden, :very_hidden], sheet_state
57
+ @state = sheet_state
58
+ end
59
+
60
+ # The visibility of this sheet
61
+ def state
62
+ @state ||= :visible
48
63
  end
49
64
 
50
65
  # The sheet calculation properties
@@ -76,7 +91,7 @@ module Axlsx
76
91
  # @see [SheetFormatPr]
77
92
  def sheet_format_pr
78
93
  @sheet_format_pr ||= SheetFormatPr.new
79
- yeild @sheet_format_pr if block_given?
94
+ yield @sheet_format_pr if block_given?
80
95
  @sheet_format_pr
81
96
  end
82
97
 
@@ -129,7 +144,7 @@ module Axlsx
129
144
  # @return [SimpleTypedList]
130
145
  # @see Worksheet#add_row
131
146
  def rows
132
- @rows ||= SimpleTypedList.new Row
147
+ @rows ||= SimpleTypedList.new Row
133
148
  end
134
149
 
135
150
  # returns the sheet data as columns
@@ -137,12 +152,12 @@ module Axlsx
137
152
  # cell at a specific index. The block will be called with the row and column
138
153
  # index in the missing cell was found.
139
154
  # @example
140
- # cols { |row_index, column_index| p "warn - row #{row_index} is does not have a cell at #{column_index}
155
+ # cols { |row_index, column_index| puts "warn - row #{row_index} does not have a cell at #{column_index}" }
141
156
  def cols(&block)
142
157
  @rows.transpose(&block)
143
158
  end
144
159
 
145
- # An range that excel will apply an auto-filter to "A1:B3"
160
+ # A range that excel will apply an auto-filter to "A1:B3"
146
161
  # This will turn filtering on for the cells in the range.
147
162
  # The first row is considered the header, while subsequent rows are considered to be data.
148
163
  # @return String
@@ -249,7 +264,7 @@ module Axlsx
249
264
  @header_footer
250
265
  end
251
266
 
252
- # convinience method to access all cells in this worksheet
267
+ # convenience method to access all cells in this worksheet
253
268
  # @return [Array] cells
254
269
  def cells
255
270
  rows.flatten
@@ -290,50 +305,6 @@ module Axlsx
290
305
  @sheet_pr ||= SheetPr.new self
291
306
  end
292
307
 
293
- # Indicates if gridlines should be shown in the sheet.
294
- # This is true by default.
295
- # @return [Boolean]
296
- # @deprecated Use SheetView#show_grid_lines= instead.
297
- def show_gridlines=(v)
298
- warn('axlsx::DEPRECIATED: Worksheet#show_gridlines= has been depreciated. This value can be set over SheetView#show_grid_lines=.')
299
- Axlsx::validate_boolean v
300
- sheet_view.show_grid_lines = v
301
- end
302
-
303
- # @see selected
304
- # @return [Boolean]
305
- # @deprecated Use SheetView#tab_selected= instead.
306
- def selected=(v)
307
- warn('axlsx::DEPRECIATED: Worksheet#selected= has been depreciated. This value can be set over SheetView#tab_selected=.')
308
- Axlsx::validate_boolean v
309
- sheet_view.tab_selected = v
310
- end
311
-
312
- # Indicates if the worksheet should show gridlines or not
313
- # @return Boolean
314
- # @deprecated Use SheetView#show_grid_lines instead.
315
- def show_gridlines
316
- warn('axlsx::DEPRECIATED: Worksheet#show_gridlines has been depreciated. This value can get over SheetView#show_grid_lines.')
317
- sheet_view.show_grid_lines
318
- end
319
-
320
- # Indicates if the worksheet is selected in the workbook
321
- # It is possible to have more than one worksheet selected, however it might cause issues
322
- # in some older versions of excel when using copy and paste.
323
- # @return Boolean
324
- # @deprecated Use SheetView#tab_selected instead.
325
- def selected
326
- warn('axlsx::DEPRECIATED: Worksheet#selected has been depreciated. This value can get over SheetView#tab_selected.')
327
- sheet_view.tab_selected
328
- end
329
-
330
- # (see #fit_to_page)
331
- # @return [Boolean]
332
- def fit_to_page=(v)
333
- warn('axlsx::DEPRECIATED: Worksheet#fit_to_page has been depreciated. This value will automatically be set for you when you use PageSetup#fit_to.')
334
- fit_to_page?
335
- end
336
-
337
308
  # The name of the worksheet
338
309
  # The name of a worksheet must be unique in the workbook, and must not exceed 31 characters
339
310
  # @param [String] name
@@ -346,7 +317,7 @@ module Axlsx
346
317
  # @param [String] v
347
318
  # @see auto_filter
348
319
  def auto_filter=(v)
349
- DataTypeValidator.validate "Worksheet.auto_filter", String, v
320
+ DataTypeValidator.validate :worksheet_auto_filter, String, v
350
321
  auto_filter.range = v
351
322
  end
352
323
 
@@ -427,10 +398,10 @@ module Axlsx
427
398
  # @option options [Array] widths each member of the widths array will affect how auto_fit behavies.
428
399
  # @option options [Float] height the row's height (in points)
429
400
  def add_row(values=[], options={})
430
- Row.new(self, values, options)
431
- update_column_info @rows.last.cells, options.delete(:widths) || []
432
- yield @rows.last if block_given?
433
- @rows.last
401
+ row = Row.new(self, values, options)
402
+ update_column_info row, options.delete(:widths)
403
+ yield row if block_given?
404
+ row
434
405
  end
435
406
 
436
407
  alias :<< :add_row
@@ -522,7 +493,7 @@ module Axlsx
522
493
  # @example
523
494
  # ws.add_page_break("A4")
524
495
  def add_page_break(cell)
525
- DataTypeValidator.validate "Worksheet#add_page_break cell", [String, Cell], cell
496
+ DataTypeValidator.validate :worksheet_page_break, [String, Cell], cell
526
497
  column_index, row_index = if cell.is_a?(String)
527
498
  Axlsx.name_to_indices(cell)
528
499
  else
@@ -535,12 +506,12 @@ module Axlsx
535
506
  end
536
507
 
537
508
  # This is a helper method that Lets you specify a fixed width for multiple columns in a worksheet in one go.
538
- # Axlsx is sparse, so if you have not set data for a column, you cannot set the width.
509
+ # Note that you must call column_widths AFTER adding data, otherwise the width will not be set successfully.
539
510
  # Setting a fixed column width to nil will revert the behaviour back to calculating the width for you on the next call to add_row.
540
511
  # @example This would set the first and third column widhts but leave the second column in autofit state.
541
512
  # ws.column_widths 7.2, nil, 3
542
513
  # @note For updating only a single column it is probably easier to just set the width of the ws.column_info[col_index].width directly
543
- # @param [Integer|Float|Fixnum|nil] widths
514
+ # @param [Integer|Float|nil] widths
544
515
  def column_widths(*widths)
545
516
  widths.each_with_index do |value, index|
546
517
  next if value == nil
@@ -559,7 +530,7 @@ module Axlsx
559
530
  # @see README.md for an example
560
531
  def col_style(index, style, options={})
561
532
  offset = options.delete(:row_offset) || 0
562
- cells = @rows[(offset..-1)].map { |row| row.cells[index] }.flatten.compact
533
+ cells = @rows[(offset..-1)].map { |row| row[index] }.flatten.compact
563
534
  cells.each { |cell| cell.style = style }
564
535
  end
565
536
 
@@ -577,18 +548,27 @@ module Axlsx
577
548
  cells.each { |cell| cell.style = style }
578
549
  end
579
550
 
551
+ # Returns a sheet node serialization for this sheet in the workbook.
552
+ def to_sheet_node_xml_string(str='')
553
+ add_autofilter_defined_name_to_workbook
554
+ str << '<sheet '
555
+ serialized_attributes str
556
+ str << ('name="' << name << '" ')
557
+ str << ('r:id="' << rId << '"></sheet>')
558
+ end
559
+
580
560
  # Serializes the worksheet object to an xml string
581
561
  # This intentionally does not use nokogiri for performance reasons
582
562
  # @return [String]
583
- def to_xml_string
563
+ def to_xml_string str=''
564
+ add_autofilter_defined_name_to_workbook
584
565
  auto_filter.apply if auto_filter.range
585
- str = '<?xml version="1.0" encoding="UTF-8"?>'
566
+ str << '<?xml version="1.0" encoding="UTF-8"?>'
586
567
  str << worksheet_node
587
568
  serializable_parts.each do |item|
588
569
  item.to_xml_string(str) if item
589
570
  end
590
571
  str << '</worksheet>'
591
- Axlsx::sanitize(str)
592
572
  end
593
573
 
594
574
  # The worksheet relationships. This is managed automatically by the worksheet
@@ -606,7 +586,7 @@ module Axlsx
606
586
  # Returns the cell or cells defined using excel style A1:B3 references.
607
587
  # @param [String|Integer] cell_def the string defining the cell or range of cells, or the rownumber
608
588
  # @return [Cell, Array]
609
- def [] (cell_def)
589
+ def [](cell_def)
610
590
  return rows[cell_def] if cell_def.is_a?(Integer)
611
591
  parts = cell_def.split(':').map{ |part| name_to_cell part }
612
592
  if parts.size == 1
@@ -622,7 +602,7 @@ module Axlsx
622
602
  def name_to_cell(name)
623
603
  col_index, row_index = *Axlsx::name_to_indices(name)
624
604
  r = rows[row_index]
625
- r.cells[col_index] if r
605
+ r[col_index] if r
626
606
  end
627
607
 
628
608
  # shortcut method to access styles direclty from the worksheet
@@ -675,7 +655,7 @@ module Axlsx
675
655
  end
676
656
 
677
657
  def validate_sheet_name(name)
678
- DataTypeValidator.validate "Worksheet.name", String, name
658
+ DataTypeValidator.validate :worksheet_name, String, name
679
659
  raise ArgumentError, (ERR_SHEET_NAME_TOO_LONG % name) if name.size > 31
680
660
  raise ArgumentError, (ERR_SHEET_NAME_CHARACTER_FORBIDDEN % name) if '[]*/\?:'.chars.any? { |char| name.include? char }
681
661
  name = Axlsx::coder.encode(name)
@@ -695,8 +675,8 @@ module Axlsx
695
675
  def range(*cell_def)
696
676
  first, last = cell_def
697
677
  cells = []
698
- rows[(first.row.index..last.row.index)].each do |r|
699
- r.cells[(first.index..last.index)].each do |c|
678
+ rows[(first.row.row_index..last.row.row_index)].each do |r|
679
+ r[(first.index..last.index)].each do |c|
700
680
  cells << c
701
681
  end
702
682
  end
@@ -734,7 +714,7 @@ module Axlsx
734
714
  # Helper method for parsingout the root node for worksheet
735
715
  # @return [String]
736
716
  def worksheet_node
737
- "<worksheet xmlns=\"%s\" xmlns:r=\"%s\" xml:space=\"#{xml_space}\">" % [XML_NS, XML_NS_R]
717
+ "<worksheet xmlns=\"#{XML_NS}\" xmlns:r=\"#{XML_NS_R}\" xml:space=\"#{xml_space}\">"
738
718
  end
739
719
 
740
720
  def sheet_data
@@ -753,11 +733,12 @@ module Axlsx
753
733
 
754
734
  def workbook=(v) DataTypeValidator.validate "Worksheet.workbook", Workbook, v; @workbook = v; end
755
735
 
756
- def update_column_info(cells, widths=[])
736
+ def update_column_info(cells, widths=nil)
757
737
  cells.each_with_index do |cell, index|
738
+ width = widths ? widths[index] : nil
758
739
  col = find_or_create_column_info(index)
759
- next if widths[index] == :ignore
760
- col.update_width(cell, widths[index], workbook.use_autowidth)
740
+ next if width == :ignore
741
+ col.update_width(cell, width, workbook.use_autowidth)
761
742
  end
762
743
  end
763
744
 
@@ -765,5 +746,10 @@ module Axlsx
765
746
  column_info[index] ||= Col.new(index + 1, index + 1)
766
747
  end
767
748
 
749
+ def add_autofilter_defined_name_to_workbook
750
+ return if !auto_filter.range
751
+ workbook.add_defined_name auto_filter.defined_name, name: '_xlnm._FilterDatabase', local_sheet_id: index, hidden: 1
752
+ end
753
+
768
754
  end
769
755
  end