axlsx 1.1.6 → 1.2.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 (173) hide show
  1. data/.yardopts +5 -2
  2. data/CHANGELOG.md +39 -0
  3. data/README.md +97 -46
  4. data/Rakefile +3 -2
  5. data/examples/basic_charts.rb +54 -0
  6. data/examples/chart_colors.rb +18 -3
  7. data/examples/example.rb +118 -42
  8. data/examples/example.xlsx +0 -0
  9. data/examples/example_streamed.xlsx +0 -0
  10. data/examples/extractive.pdf +0 -0
  11. data/examples/no-use_autowidth.xlsx +0 -0
  12. data/examples/scraping_html.rb +91 -0
  13. data/examples/shared_strings_example.xlsx +0 -0
  14. data/examples/sheet_view.rb +34 -0
  15. data/lib/axlsx/content_type/content_type.rb +2 -0
  16. data/lib/axlsx/content_type/default.rb +21 -12
  17. data/lib/axlsx/content_type/override.rb +22 -11
  18. data/lib/axlsx/doc_props/app.rb +36 -32
  19. data/lib/axlsx/doc_props/core.rb +9 -5
  20. data/lib/axlsx/drawing/ax_data_source.rb +7 -6
  21. data/lib/axlsx/drawing/axis.rb +64 -25
  22. data/lib/axlsx/drawing/bar_3D_chart.rb +49 -45
  23. data/lib/axlsx/drawing/bar_series.rb +1 -0
  24. data/lib/axlsx/drawing/cat_axis.rb +43 -39
  25. data/lib/axlsx/drawing/chart.rb +65 -54
  26. data/lib/axlsx/drawing/d_lbls.rb +100 -0
  27. data/lib/axlsx/drawing/drawing.rb +7 -5
  28. data/lib/axlsx/drawing/graphic_frame.rb +2 -2
  29. data/lib/axlsx/drawing/line_3D_chart.rb +3 -10
  30. data/lib/axlsx/drawing/num_data.rb +2 -2
  31. data/lib/axlsx/drawing/num_data_source.rb +1 -1
  32. data/lib/axlsx/drawing/pie_3D_chart.rb +10 -7
  33. data/lib/axlsx/drawing/scatter_chart.rb +2 -8
  34. data/lib/axlsx/drawing/title.rb +21 -9
  35. data/lib/axlsx/drawing/two_cell_anchor.rb +41 -9
  36. data/lib/axlsx/drawing/view_3D.rb +41 -31
  37. data/lib/axlsx/drawing/vml_drawing.rb +1 -1
  38. data/lib/axlsx/package.rb +20 -16
  39. data/lib/axlsx/rels/relationship.rb +3 -3
  40. data/lib/axlsx/stylesheet/styles.rb +1 -1
  41. data/lib/axlsx/util/constants.rb +4 -0
  42. data/lib/axlsx/util/simple_typed_list.rb +15 -8
  43. data/lib/axlsx/util/validators.rb +28 -4
  44. data/lib/axlsx/version.rb +2 -7
  45. data/lib/axlsx/workbook/defined_name.rb +174 -0
  46. data/lib/axlsx/workbook/defined_names.rb +21 -0
  47. data/lib/axlsx/workbook/workbook.rb +46 -17
  48. data/lib/axlsx/workbook/worksheet/auto_filter.rb +34 -0
  49. data/lib/axlsx/workbook/worksheet/cell.rb +28 -1
  50. data/lib/axlsx/workbook/worksheet/col.rb +15 -0
  51. data/lib/axlsx/workbook/worksheet/cols.rb +20 -0
  52. data/lib/axlsx/workbook/worksheet/comments.rb +8 -0
  53. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +25 -0
  54. data/lib/axlsx/workbook/worksheet/data_bar.rb +1 -1
  55. data/lib/axlsx/workbook/worksheet/data_validations.rb +28 -0
  56. data/lib/axlsx/workbook/worksheet/dimension.rb +65 -0
  57. data/lib/axlsx/workbook/worksheet/merged_cells.rb +35 -0
  58. data/lib/axlsx/workbook/worksheet/page_setup.rb +10 -1
  59. data/lib/axlsx/workbook/worksheet/pane.rb +144 -0
  60. data/lib/axlsx/workbook/worksheet/protected_range.rb +46 -0
  61. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +34 -0
  62. data/lib/axlsx/workbook/worksheet/row.rb +1 -1
  63. data/lib/axlsx/workbook/worksheet/selection.rb +111 -0
  64. data/lib/axlsx/workbook/worksheet/sheet_data.rb +25 -0
  65. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +24 -0
  66. data/lib/axlsx/workbook/worksheet/sheet_view.rb +379 -0
  67. data/lib/axlsx/workbook/worksheet/tables.rb +31 -0
  68. data/lib/axlsx/workbook/worksheet/worksheet.rb +315 -291
  69. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +57 -0
  70. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +64 -0
  71. data/lib/axlsx.rb +24 -16
  72. data/test/content_type/tc_content_type.rb +1 -6
  73. data/test/doc_props/tc_core.rb +1 -1
  74. data/test/drawing/tc_axis.rb +27 -2
  75. data/test/drawing/tc_bar_3D_chart.rb +12 -12
  76. data/test/drawing/tc_bar_series.rb +1 -2
  77. data/test/drawing/tc_cat_axis.rb +3 -3
  78. data/test/drawing/tc_chart.rb +8 -6
  79. data/test/drawing/tc_d_lbls.rb +47 -0
  80. data/test/drawing/tc_drawing.rb +5 -4
  81. data/test/drawing/tc_line_series.rb +1 -1
  82. data/test/drawing/tc_num_data.rb +5 -1
  83. data/test/drawing/tc_pie_3D_chart.rb +4 -8
  84. data/test/drawing/tc_pie_series.rb +1 -1
  85. data/test/drawing/tc_scatter_series.rb +1 -1
  86. data/test/drawing/tc_series.rb +1 -1
  87. data/test/drawing/tc_title.rb +16 -0
  88. data/test/drawing/tc_view_3D.rb +18 -18
  89. data/test/tc_package.rb +41 -4
  90. data/test/util/tc_validators.rb +68 -11
  91. data/test/workbook/tc_defined_name.rb +41 -0
  92. data/test/workbook/tc_workbook.rb +5 -3
  93. data/test/workbook/worksheet/table/tc_table.rb +0 -8
  94. data/test/workbook/worksheet/tc_cell.rb +2 -4
  95. data/test/workbook/worksheet/tc_page_setup.rb +20 -3
  96. data/test/workbook/worksheet/tc_pane.rb +94 -0
  97. data/test/workbook/worksheet/tc_protected_range.rb +17 -0
  98. data/test/workbook/worksheet/tc_row.rb +2 -2
  99. data/test/workbook/worksheet/tc_selection.rb +94 -0
  100. data/test/workbook/worksheet/tc_sheet_view.rb +214 -0
  101. data/test/workbook/worksheet/tc_worksheet.rb +53 -41
  102. metadata +104 -97
  103. data/examples/chart_colors.xlsx +0 -0
  104. data/examples/conditional_formatting/getting_barred.xlsx +0 -0
  105. data/examples/doc/_index.html +0 -84
  106. data/examples/doc/class_list.html +0 -47
  107. data/examples/doc/css/common.css +0 -1
  108. data/examples/doc/css/full_list.css +0 -55
  109. data/examples/doc/css/style.css +0 -322
  110. data/examples/doc/file_list.html +0 -46
  111. data/examples/doc/frames.html +0 -13
  112. data/examples/doc/index.html +0 -84
  113. data/examples/doc/js/app.js +0 -205
  114. data/examples/doc/js/full_list.js +0 -173
  115. data/examples/doc/js/jquery.js +0 -16
  116. data/examples/doc/method_list.html +0 -46
  117. data/examples/doc/top-level-namespace.html +0 -95
  118. data/examples/examples_saved.xlsx +0 -0
  119. data/examples/fish.xlsx +0 -0
  120. data/examples/pareto.rb +0 -28
  121. data/examples/pareto.xlsx +0 -0
  122. data/examples/pie_chart.rb +0 -16
  123. data/examples/pie_chart.xlsx +0 -0
  124. data/examples/pie_chart_saved.xlsx +0 -0
  125. data/examples/sheet_protection.xlsx +0 -0
  126. data/examples/two_cell_anchor_image.xlsx +0 -0
  127. data/examples/~$pie_chart_saved.xlsx +0 -0
  128. data/lib/axlsx/drawing/ax_data_source.rb~ +0 -55
  129. data/lib/axlsx/drawing/data_source.rb~ +0 -51
  130. data/lib/axlsx/drawing/hlink_click.rb~ +0 -0
  131. data/lib/axlsx/drawing/hyperlink.rb~ +0 -64
  132. data/lib/axlsx/drawing/num_data.rb~ +0 -51
  133. data/lib/axlsx/drawing/num_data_source.rb~ +0 -54
  134. data/lib/axlsx/drawing/num_val.rb~ +0 -40
  135. data/lib/axlsx/drawing/picture_locking.rb~ +0 -36
  136. data/lib/axlsx/drawing/ref.rb~ +0 -41
  137. data/lib/axlsx/drawing/str_data.rb~ +0 -58
  138. data/lib/axlsx/drawing/str_val.rb~ +0 -35
  139. data/lib/axlsx/drawing/vml_drawing.rb~ +0 -6
  140. data/lib/axlsx/drawing/vml_shape.rb~ +0 -61
  141. data/lib/axlsx/util/cbf.rb +0 -333
  142. data/lib/axlsx/util/cfb.rb~ +0 -201
  143. data/lib/axlsx/util/font_tables.rb~ +0 -0
  144. data/lib/axlsx/util/ms_off_crypto.rb +0 -189
  145. data/lib/axlsx/util/ms_off_crypto.rb~ +0 -3
  146. data/lib/axlsx/util/ms_offcrypto.rb~ +0 -0
  147. data/lib/axlsx/util/parser.rb~ +0 -6
  148. data/lib/axlsx/util/storage.rb~ +0 -0
  149. data/lib/axlsx/workbook/shared_strings_table.rb~ +0 -69
  150. data/lib/axlsx/workbook/worksheet/cfvo.rb~ +0 -0
  151. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  152. data/lib/axlsx/workbook/worksheet/color_scale.rb~ +0 -46
  153. data/lib/axlsx/workbook/worksheet/comment.rb~ +0 -91
  154. data/lib/axlsx/workbook/worksheet/comments.rb~ +0 -86
  155. data/lib/axlsx/workbook/worksheet/data_bar.rb~ +0 -0
  156. data/lib/axlsx/workbook/worksheet/icon_set.rb~ +0 -95
  157. data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
  158. data/lib/axlsx/workbook/worksheet/table.rb~ +0 -97
  159. data/lib/schema/dc.xsd~ +0 -118
  160. data/lib/schema/dcterms.xsd~ +0 -331
  161. data/lib/schema/opc-coreProperties.xsd~ +0 -50
  162. data/test/drawing/tc_data_source.rb~ +0 -30
  163. data/test/drawing/tc_num_data.rb~ +0 -35
  164. data/test/drawing/tc_num_val.rb~ +0 -29
  165. data/test/drawing/tc_str_data.rb~ +0 -30
  166. data/test/drawing/tc_str_val.rb~ +0 -26
  167. data/test/drawing/tc_vml_drawing.rb~ +0 -0
  168. data/test/workbook/worksheet/table/tc_table.rb~ +0 -72
  169. data/test/workbook/worksheet/tc_cfvo.rb~ +0 -20
  170. data/test/workbook/worksheet/tc_col.rb~ +0 -10
  171. data/test/workbook/worksheet/tc_color_scale.rb~ +0 -0
  172. data/test/workbook/worksheet/tc_data_bar.rb~ +0 -0
  173. data/test/workbook/worksheet/tc_icon_set.rb~ +0 -0
@@ -4,9 +4,44 @@ module Axlsx
4
4
  # The Worksheet class represents a worksheet in the workbook.
5
5
  class Worksheet
6
6
 
7
+ # definition of characters which are less than the maximum width of 0-9 in the default font for use in String#count.
8
+ # This is used for autowidth calculations
9
+ # @return [String]
10
+ def self.thin_chars
11
+ @thin_chars ||= "^.acefijklrstxyzFIJL()-"
12
+ end
13
+
14
+ # Creates a new worksheet.
15
+ # @note the recommended way to manage worksheets is Workbook#add_worksheet
16
+ # @see Workbook#add_worksheet
17
+ # @option options [String] name The name of this worksheet.
18
+ # @option options [Hash] page_margins A hash containing page margins for this worksheet. @see PageMargins
19
+ # @option options [Hash] print_options A hash containing print options for this worksheet. @see PrintOptions
20
+ # @option options [Boolean] show_gridlines indicates if gridlines should be shown for this sheet.
21
+ def initialize(wb, options={})
22
+ self.workbook = wb
23
+ @workbook.worksheets << self
24
+ @sheet_protection = nil
25
+
26
+ initialize_page_options(options)
27
+ options.each do |o|
28
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
29
+ end
30
+ end
31
+
32
+ # Initalizes page margin, setup and print options
33
+ # @param [Hash] options Options passed in from the initializer
34
+ def initialize_page_options(options)
35
+ @page_margins = PageMargins.new options[:page_margins] if options[:page_margins]
36
+ @page_setup = PageSetup.new options[:page_setup] if options[:page_setup]
37
+ @print_options = PrintOptions.new options[:print_options] if options[:print_options]
38
+ end
39
+
7
40
  # The name of the worksheet
8
41
  # @return [String]
9
- attr_reader :name
42
+ def name
43
+ @name ||= "Sheet" + (index+1).to_s
44
+ end
10
45
 
11
46
  # The sheet protection object for this workbook
12
47
  # @return [SheetProtection]
@@ -17,62 +52,68 @@ module Axlsx
17
52
  @sheet_protection
18
53
  end
19
54
 
55
+ # The sheet view object for this worksheet
56
+ # @return [SheetView]
57
+ # @see [SheetView]
58
+ def sheet_view
59
+ @sheet_view ||= SheetView.new
60
+ yield @sheet_view if block_given?
61
+ @sheet_view
62
+ end
63
+
20
64
  # The workbook that owns this worksheet
21
65
  # @return [Workbook]
22
66
  attr_reader :workbook
23
67
 
24
68
  # The tables in this worksheet
25
69
  # @return [Array] of Table
26
- attr_reader :tables
27
-
28
- attr_reader :comments
70
+ def tables
71
+ @tables ||= Tables.new self
72
+ end
73
+
74
+ # The a shortcut to the worksheet_comments list of comments
75
+ # @return [Array|SimpleTypedList]
76
+ def comments
77
+ worksheet_comments.comments if worksheet_comments.has_comments?
78
+ end
29
79
 
30
80
  # The rows in this worksheet
31
81
  # @note The recommended way to manage rows is Worksheet#add_row
32
82
  # @return [SimpleTypedList]
33
83
  # @see Worksheet#add_row
34
- attr_reader :rows
35
-
36
- # An array of content based calculated column widths.
37
- # @note a single auto fit data item is a hash with :longest => [String] and :sz=> [Integer] members.
38
- # @return [Array] of Hash
39
- attr_reader :auto_fit_data
84
+ def rows
85
+ @rows ||= SimpleTypedList.new Row
86
+ end
40
87
 
41
- # An array of merged cell ranges e.d "A1:B3"
42
- # Content and formatting is read from the first cell.
43
- # @return Array
44
- attr_reader :merged_cells
88
+ # returns the sheet data as columnw
89
+ def cols
90
+ @rows.transpose
91
+ end
45
92
 
46
93
  # An range that excel will apply an autfilter to "A1:B3"
47
94
  # This will turn filtering on for the cells in the range.
48
95
  # The first row is considered the header, while subsequent rows are considerd to be data.
49
- # @return Array
50
- attr_reader :auto_filter
51
-
52
- # Indicates if the worksheet should show gridlines or not
53
- # @return Boolean
54
- attr_reader :show_gridlines
55
-
56
-
57
- # Indicates if the worksheet is selected in the workbook
58
- # It is possible to have more than one worksheet selected, however it might cause issues
59
- # in some older versions of excel when using copy and paste.
60
- # @return Boolean
61
- attr_reader :selected
96
+ # @return String
97
+ def auto_filter
98
+ @auto_filter ||= AutoFilter.new self
99
+ end
62
100
 
63
101
  # Indicates if the worksheet will be fit by witdh or height to a specific number of pages.
64
102
  # To alter the width or height for page fitting, please use page_setup.fit_to_widht or page_setup.fit_to_height.
65
103
  # If you want the worksheet to fit on more pages (e.g. 2x2), set {PageSetup#fit_to_width} and {PageSetup#fit_to_height} accordingly.
66
104
  # @return Boolean
67
105
  # @see #page_setup
68
- def fit_to_page
69
- (@page_setup != nil && (@page_setup.fit_to_width != nil || @page_setup.fit_to_height != nil))
106
+ def fit_to_page?
107
+ return false unless self.instance_values.keys.include?('page_setup')
108
+ page_setup.fit_to_page?
70
109
  end
71
110
 
72
111
 
73
112
  # Column info for the sheet
74
113
  # @return [SimpleTypedList]
75
- attr_reader :column_info
114
+ def column_info
115
+ @column_info ||= Cols.new self
116
+ end
76
117
 
77
118
  # Page margins for printing the worksheet.
78
119
  # @example
@@ -94,7 +135,6 @@ module Axlsx
94
135
  @page_margins ||= PageMargins.new
95
136
  yield @page_margins if block_given?
96
137
  @page_margins
97
-
98
138
  end
99
139
 
100
140
  # Page setup settings for printing the worksheet.
@@ -142,79 +182,12 @@ module Axlsx
142
182
  @print_options
143
183
  end
144
184
 
145
- # definition of characters which are less than the maximum width of 0-9 in the default font for use in String#count.
146
- # This is used for autowidth calculations
147
- # @return [String]
148
- def self.thin_chars
149
- @thin_chars ||= "^.acefijklrstxyzFIJL()-"
150
- end
151
-
152
- # Creates a new worksheet.
153
- # @note the recommended way to manage worksheets is Workbook#add_worksheet
154
- # @see Workbook#add_worksheet
155
- # @option options [String] name The name of this worksheet.
156
- # @option options [Hash] page_margins A hash containing page margins for this worksheet. @see PageMargins
157
- # @option options [Hash] print_options A hash containing print options for this worksheet. @see PrintOptions
158
- # @option options [Boolean] show_gridlines indicates if gridlines should be shown for this sheet.
159
- def initialize(wb, options={})
160
- self.workbook = wb
161
- @workbook.worksheets << self
162
- @page_marging = @page_setup = @print_options = nil
163
- @drawing = @page_margins = @auto_filter = @sheet_protection = nil
164
- @merged_cells = []
165
- @auto_fit_data = []
166
- @conditional_formattings = []
167
- @data_validations = []
168
- @comments = Comments.new(self)
169
- @selected = false
170
- @show_gridlines = true
171
- self.name = "Sheet" + (index+1).to_s
172
- @page_margins = PageMargins.new options[:page_margins] if options[:page_margins]
173
- @page_setup = PageSetup.new options[:page_setup] if options[:page_setup]
174
- @print_options = PrintOptions.new options[:print_options] if options[:print_options]
175
- @rows = SimpleTypedList.new Row
176
- @column_info = SimpleTypedList.new Col
177
- # @cols = SimpleTypedList.new Cell
178
- @tables = SimpleTypedList.new Table
179
-
180
- options.each do |o|
181
- self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
182
- end
183
- end
184
-
185
185
  # convinience method to access all cells in this worksheet
186
186
  # @return [Array] cells
187
187
  def cells
188
188
  rows.flatten
189
189
  end
190
190
 
191
- # Add conditional formatting to this worksheet.
192
- #
193
- # @param [String] cells The range to apply the formatting to
194
- # @param [Array|Hash] rules An array of hashes (or just one) to create Conditional formatting rules from.
195
- # @example This would format column A whenever it is FALSE.
196
- # # for a longer example, see examples/example_conditional_formatting.rb (link below)
197
- # worksheet.add_conditional_formatting( "A1:A1048576", { :type => :cellIs, :operator => :equal, :formula => "FALSE", :dxfId => 1, :priority => 1 }
198
- #
199
- # @see ConditionalFormattingRule#initialize
200
- # @see file:examples/example_conditional_formatting.rb
201
- def add_conditional_formatting(cells, rules)
202
- cf = ConditionalFormatting.new( :sqref => cells )
203
- cf.add_rules rules
204
- @conditional_formattings << cf
205
- end
206
-
207
- # Add data validation to this worksheet.
208
- #
209
- # @param [String] cells The cells the validation will apply to.
210
- # @param [hash] data_validation options defining the validation to apply.
211
- # @see examples/data_validation.rb for an example
212
- def add_data_validation(cells, data_validation)
213
- dv = DataValidation.new(data_validation)
214
- dv.sqref = cells
215
- @data_validations << dv
216
- end
217
-
218
191
  # Creates merge information for this worksheet.
219
192
  # Cells can be merged by calling the merge_cells method on a worksheet.
220
193
  # @example This would merge the three cells C1..E1 #
@@ -223,74 +196,83 @@ module Axlsx
223
196
  # worksheet.merge_cells worksheet.rows.first.cells[(2..4)]
224
197
  # #alternatively you can do it from a single cell
225
198
  # worksheet["C1"].merge worksheet["E1"]
226
- # @param [Array, string]
199
+ # @param [Array, string] cells
227
200
  def merge_cells(cells)
228
- @merged_cells << if cells.is_a?(String)
229
- cells
230
- elsif cells.is_a?(Array)
231
- cells = cells.sort { |x, y| [x.index, x.row.index] <=> [y.index, y.row.index] }
232
- "#{cells.first.r}:#{cells.last.r}"
233
- end
201
+ merged_cells.add cells
234
202
  end
235
203
 
204
+ # Adds a new protected cell range to the worksheet. Note that protected ranges are only in effect when sheet protection is enabled.
205
+ # @param [String|Array] cells The string reference for the cells to protect or an array of cells.
206
+ # @return [ProtectedRange]
207
+ # @note When using an array of cells, a contiguous range is created from the minimum top left to the maximum top bottom of the cells provided.
208
+ def protect_range(cells)
209
+ protected_ranges.add_range(cells)
210
+ end
236
211
 
237
- # The demensions of a worksheet. This is not actually a required element by the spec,
212
+ # The dimensions of a worksheet. This is not actually a required element by the spec,
238
213
  # but at least a few other document readers expect this for conversion
239
- # @return [String] the A1:B2 style reference for the first and last row column intersection in the workbook
214
+ # @return [Dimension]
240
215
  def dimension
241
- dim_start = rows.first.cells.first == nil ? 'A1' : rows.first.cells.first.r
242
- dim_end = rows.last.cells.last == nil ? 'AA:200' : rows.last.cells.last.r
243
- "#{dim_start}:#{dim_end}"
216
+ @dimension ||= Dimension.new self
217
+ end
218
+
219
+ # The sheet properties for this workbook.
220
+ # Currently only pageSetUpPr -> fitToPage is implemented
221
+ # @return [SheetPr]
222
+ def sheet_pr
223
+ @sheet_pr ||= SheetPr.new self
244
224
  end
245
225
 
246
226
  # Indicates if gridlines should be shown in the sheet.
247
227
  # This is true by default.
248
228
  # @return [Boolean]
229
+ # @deprecated Use {SheetView#show_grid_lines=} instead.
249
230
  def show_gridlines=(v)
231
+ warn('axlsx::DEPRECIATED: Worksheet#show_gridlines= has been depreciated. This value can be set over SheetView#show_grid_lines=.')
250
232
  Axlsx::validate_boolean v
251
- @show_gridlines = v
233
+ sheet_view.show_grid_lines = v
252
234
  end
253
235
 
254
236
  # @see selected
255
237
  # @return [Boolean]
238
+ # @deprecated Use {SheetView#tab_selected=} instead.
256
239
  def selected=(v)
240
+ warn('axlsx::DEPRECIATED: Worksheet#selected= has been depreciated. This value can be set over SheetView#tab_selected=.')
257
241
  Axlsx::validate_boolean v
258
- @selected = v
242
+ sheet_view.tab_selected = v
259
243
  end
260
244
 
245
+ # Indicates if the worksheet should show gridlines or not
246
+ # @return Boolean
247
+ # @deprecated Use {SheetView#show_grid_lines} instead.
248
+ def show_gridlines
249
+ warn('axlsx::DEPRECIATED: Worksheet#show_gridlines has been depreciated. This value can get over SheetView#show_grid_lines.')
250
+ sheet_view.show_grid_lines
251
+ end
252
+
253
+ # Indicates if the worksheet is selected in the workbook
254
+ # It is possible to have more than one worksheet selected, however it might cause issues
255
+ # in some older versions of excel when using copy and paste.
256
+ # @return Boolean
257
+ # @deprecated Use {SheetView#tab_selected} instead.
258
+ def selected
259
+ warn('axlsx::DEPRECIATED: Worksheet#selected has been depreciated. This value can get over SheetView#tab_selected.')
260
+ sheet_view.tab_selected
261
+ end
261
262
 
262
263
  # (see #fit_to_page)
263
264
  # @return [Boolean]
264
265
  def fit_to_page=(v)
265
266
  warn('axlsx::DEPRECIATED: Worksheet#fit_to_page has been depreciated. This value will automatically be set for you when you use PageSetup#fit_to.')
266
- fit_to_page
267
- end
268
-
269
-
270
- # returns the column and row index for a named based cell
271
- # @param [String] name The cell or cell range to return. "A1" will return the first cell of the first row.
272
- # @return [Cell]
273
- def name_to_cell(name)
274
- col_index, row_index = *Axlsx::name_to_indices(name)
275
- r = rows[row_index]
276
- r.cells[col_index] if r
267
+ fit_to_page?
277
268
  end
278
269
 
279
270
  # The name of the worksheet
280
271
  # The name of a worksheet must be unique in the workbook, and must not exceed 31 characters
281
- # @param [String] v
282
- def name=(v)
283
- DataTypeValidator.validate "Worksheet.name", String, v
284
- raise ArgumentError, (ERR_SHEET_NAME_TOO_LONG % v) if v.size > 31
285
- sheet_names = @workbook.worksheets.map { |s| s.name }
286
- raise ArgumentError, (ERR_DUPLICATE_SHEET_NAME % v) if sheet_names.include?(v)
287
- @name=v
288
- end
289
-
290
- # The absolute auto filter range
291
- # @see auto_filter
292
- def abs_auto_filter
293
- Axlsx.cell_range(@auto_filter.split(':').collect { |name| name_to_cell(name)}) if @auto_filter
272
+ # @param [String] name
273
+ def name=(name)
274
+ validate_sheet_name name
275
+ @name=Axlsx::coder.encode(name)
294
276
  end
295
277
 
296
278
  # The auto filter range for the worksheet
@@ -298,7 +280,7 @@ module Axlsx
298
280
  # @see auto_filter
299
281
  def auto_filter=(v)
300
282
  DataTypeValidator.validate "Worksheet.auto_filter", String, v
301
- @auto_filter = v
283
+ auto_filter.range = v
302
284
  end
303
285
 
304
286
  # The part name of this worksheet
@@ -330,7 +312,7 @@ module Axlsx
330
312
  # @return [Drawing]
331
313
  # @see Worksheet#add_chart
332
314
  def drawing
333
- @drawing || @drawing = Axlsx::Drawing.new(self)
315
+ worksheet_drawing.drawing
334
316
  end
335
317
 
336
318
  # Adds a row to the worksheet and updates auto fit data.
@@ -374,71 +356,39 @@ module Axlsx
374
356
  # @option options [Float] height the row's height (in points)
375
357
  def add_row(values=[], options={})
376
358
  Row.new(self, values, options)
377
- update_column_info @rows.last.cells, options.delete(:widths) ||[], options.delete(:style) || []
378
- # update_auto_fit_data @rows.last.cells, options.delete(:widths) || []
359
+ update_column_info @rows.last.cells, options.delete(:widths) || []
379
360
  yield @rows.last if block_given?
380
361
  @rows.last
381
362
  end
382
363
 
383
364
  alias :<< :add_row
384
365
 
385
- # Set the style for cells in a specific row
386
- # @param [Integer] index or range of indexes in the table
387
- # @param [Integer] the cellXfs index
388
- # @option options [Integer] col_offset only cells after this column will be updated.
389
- # @note You can also specify the style in the add_row call
390
- # @see Worksheet#add_row
391
- # @see README.md for an example
392
- def row_style(index, style, options={})
393
- offset = options.delete(:col_offset) || 0
394
- rs = @rows[index]
395
- if rs.is_a?(Array)
396
- rs.each { |r| r.cells[(offset..-1)].each { |c| c.style = style } }
397
- else
398
- rs.cells[(offset..-1)].each { |c| c.style = style }
399
- end
400
- end
401
-
402
- # returns the sheet data as columnw
403
- def cols
404
- @rows.transpose
405
- end
406
-
407
-
408
- # Set the style for cells in a specific column
409
- # @param [Integer] index the index of the column
410
- # @param [Integer] the cellXfs index
411
- # @option options [Integer] row_offset only cells after this column will be updated.
412
- # @note You can also specify the style for specific columns in the call to add_row by using an array for the :styles option
413
- # @see Worksheet#add_row
414
- # @see README.md for an example
415
- def col_style(index, style, options={})
416
- offset = options.delete(:row_offset) || 0
417
- @rows[(offset..-1)].each do |r|
418
- cells = r.cells[index]
419
- next unless cells
420
- if cells.is_a?(Array)
421
- cells.each { |c| c.style = style }
422
- else
423
- cells.style = style
424
- end
425
- end
366
+ # Add conditional formatting to this worksheet.
367
+ #
368
+ # @param [String] cells The range to apply the formatting to
369
+ # @param [Array|Hash] rules An array of hashes (or just one) to create Conditional formatting rules from.
370
+ # @example This would format column A whenever it is FALSE.
371
+ # # for a longer example, see examples/example_conditional_formatting.rb (link below)
372
+ # worksheet.add_conditional_formatting( "A1:A1048576", { :type => :cellIs, :operator => :equal, :formula => "FALSE", :dxfId => 1, :priority => 1 }
373
+ #
374
+ # @see ConditionalFormattingRule#initialize
375
+ # @see file:examples/example_conditional_formatting.rb
376
+ def add_conditional_formatting(cells, rules)
377
+ cf = ConditionalFormatting.new( :sqref => cells )
378
+ cf.add_rules rules
379
+ conditional_formattings << cf
380
+ conditional_formattings
426
381
  end
427
382
 
428
- # This is a helper method that Lets you specify a fixed width for multiple columns in a worksheet in one go.
429
- # Axlsx is sparse, so if you have not set data for a column, you cannot set the width.
430
- # Setting a fixed column width to nil will revert the behaviour back to calculating the width for you.
431
- # @example This would set the first and third column widhts but leave the second column in autofit state.
432
- # ws.column_widths 7.2, nil, 3
433
- # @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
434
- # @param [Integer|Float|Fixnum|nil] values
435
- def column_widths(*args)
436
- args.each_with_index do |value, index|
437
- next if value == nil
438
- Axlsx::validate_unsigned_numeric(value) unless value == nil
439
- @column_info[index] ||= Col.new index+1, index+1
440
- @column_info[index].width = value
441
- end
383
+ # Add data validation to this worksheet.
384
+ #
385
+ # @param [String] cells The cells the validation will apply to.
386
+ # @param [hash] data_validation options defining the validation to apply.
387
+ # @see examples/data_validation.rb for an example
388
+ def add_data_validation(cells, data_validation)
389
+ dv = DataValidation.new(data_validation)
390
+ dv.sqref = cells
391
+ data_validations << dv
442
392
  end
443
393
 
444
394
  # Adds a chart to this worksheets drawing. This is the recommended way to create charts for your worksheet. This method wraps the complexity of dealing with ooxml drawing, anchors, markers graphic frames chart objects and all the other dirty details.
@@ -455,91 +405,94 @@ module Axlsx
455
405
  # @see Line3DChart
456
406
  # @see README for examples
457
407
  def add_chart(chart_type, options={})
458
- chart = drawing.add_chart(chart_type, options)
408
+ chart = worksheet_drawing.add_chart(chart_type, options)
459
409
  yield chart if block_given?
460
410
  chart
461
411
  end
462
412
 
463
413
  # needs documentation
464
414
  def add_table(ref, options={})
465
- table = Table.new(ref, self, options)
466
- @tables << table
467
- yield table if block_given?
468
- table
415
+ tables << Table.new(ref, self, options)
416
+ yield tables.last if block_given?
417
+ tables.last
469
418
  end
470
419
 
471
-
472
- # Shortcut to comments#add_comment
420
+ # Shortcut to worsksheet_comments#add_comment
473
421
  def add_comment(options={})
474
- @comments.add_comment(options)
422
+ worksheet_comments.add_comment(options)
475
423
  end
476
424
 
477
425
  # Adds a media item to the worksheets drawing
478
- # @param [Class] media_type
479
- # @option options [] unknown
426
+ # @option [Hash] options options passed to drawing.add_image
480
427
  def add_image(options={})
481
- image = drawing.add_image(options)
428
+ image = worksheet_drawing.add_image(options)
482
429
  yield image if block_given?
483
430
  image
484
431
  end
485
432
 
486
- # Serializes the object
487
- # @param [String] str
433
+ # This is a helper method that Lets you specify a fixed width for multiple columns in a worksheet in one go.
434
+ # Axlsx is sparse, so if you have not set data for a column, you cannot set the width.
435
+ # 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.
436
+ # @example This would set the first and third column widhts but leave the second column in autofit state.
437
+ # ws.column_widths 7.2, nil, 3
438
+ # @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
439
+ # @param [Integer|Float|Fixnum|nil] widths
440
+ def column_widths(*widths)
441
+ widths.each_with_index do |value, index|
442
+ next if value == nil
443
+ Axlsx::validate_unsigned_numeric(value) unless value == nil
444
+ find_or_create_column_info(index).width = value
445
+ end
446
+ end
447
+
448
+ # Set the style for cells in a specific column
449
+ # @param [Integer] index the index of the column
450
+ # @param [Integer] style the cellXfs index
451
+ # @param [Hash] options
452
+ # @option [Integer] :row_offset only cells after this column will be updated.
453
+ # @note You can also specify the style for specific columns in the call to add_row by using an array for the :styles option
454
+ # @see Worksheet#add_row
455
+ # @see README.md for an example
456
+ def col_style(index, style, options={})
457
+ offset = options.delete(:row_offset) || 0
458
+ cells = @rows[(offset..-1)].map { |row| row.cells[index] }.flatten.compact
459
+ cells.each { |cell| cell.style = style }
460
+ end
461
+
462
+ # Set the style for cells in a specific row
463
+ # @param [Integer] index or range of indexes in the table
464
+ # @param [Integer] style the cellXfs index
465
+ # @param [Hash] options the options used when applying the style
466
+ # @option [Integer] :col_offset only cells after this column will be updated.
467
+ # @note You can also specify the style in the add_row call
468
+ # @see Worksheet#add_row
469
+ # @see README.md for an example
470
+ def row_style(index, style, options={})
471
+ offset = options.delete(:col_offset) || 0
472
+ cells = cols[(offset..-1)].map { |column| column[index] }.flatten.compact
473
+ cells.each { |cell| cell.style = style }
474
+ end
475
+
476
+ # Serializes the worksheet object to an xml string
477
+ # This intentionally does not use nokogiri for performance reasons
488
478
  # @return [String]
489
479
  def to_xml_string
490
- rels = relationships
491
480
  str = '<?xml version="1.0" encoding="UTF-8"?>'
492
- str.concat "<worksheet xmlns=\"%s\" xmlns:r=\"%s\">" % [XML_NS, XML_NS_R]
493
- str.concat "<sheetPr><pageSetUpPr fitToPage=\"%s\"></pageSetUpPr></sheetPr>" % fit_to_page if fit_to_page
494
- str.concat "<dimension ref=\"%s\"></dimension>" % dimension unless rows.size == 0
495
- str.concat "<sheetViews><sheetView tabSelected='%s' workbookViewId='0' showGridLines='%s'><selection activeCell=\"A1\" sqref=\"A1\"/></sheetView></sheetViews>" % [@selected, show_gridlines]
496
-
497
- if @column_info.size > 0
498
- str << "<cols>"
499
- @column_info.each { |col| col.to_xml_string(str) }
500
- str.concat '</cols>'
481
+ str << worksheet_node
482
+ serializable_parts.each do |item|
483
+ item.to_xml_string(str) if item
501
484
  end
502
- str.concat '<sheetData>'
503
- @rows.each_with_index { |row, index| row.to_xml_string(index, str) }
504
- str.concat '</sheetData>'
505
- str.concat "<autoFilter ref='%s'></autoFilter>" % @auto_filter if @auto_filter
506
- @sheet_protection.to_xml_string(str) if @sheet_protection
507
- str.concat "<mergeCells count='%s'>%s</mergeCells>" % [@merged_cells.size, @merged_cells.reduce('') { |memo, obj| memo += "<mergeCell ref='%s'></mergeCell>" % obj } ] unless @merged_cells.empty?
508
- print_options.to_xml_string(str) if @print_options
509
- page_margins.to_xml_string(str) if @page_margins
510
- page_setup.to_xml_string(str) if @page_setup
511
- str << "<drawing r:id='rId" << (rels.index{ |r| r.Type == DRAWING_R } + 1).to_s << "'/>" if @drawing
512
- str << "<legacyDrawing r:id='rId" << (rels.index{ |r| r.Type == VML_DRAWING_R } + 1).to_s << "'/>" if @comments.size > 0
513
- unless @tables.empty?
514
- str.concat "<tableParts count='%s'>%s</tableParts>" % [@tables.size, @tables.reduce('') { |memo, obj| memo += "<tablePart r:id='%s'/>" % obj.rId }]
515
- end
516
- @conditional_formattings.each do |cf|
517
- str.concat cf.to_xml_string
518
- end
519
-
520
- unless @data_validations.empty?
521
- str.concat "<dataValidations count=\"#{@data_validations.size}\">"
522
- @data_validations.each do |df|
523
- str.concat df.to_xml_string
524
- end
525
- str.concat '</dataValidations>'
526
- end
527
- str + '</worksheet>'
485
+ str << '</worksheet>'
486
+ str.gsub(/[[:cntrl:]]/,'')
528
487
  end
529
488
 
530
489
  # The worksheet relationships. This is managed automatically by the worksheet
531
490
  # @return [Relationships]
532
491
  def relationships
533
492
  r = Relationships.new
534
- @tables.each do |table|
535
- r << Relationship.new(TABLE_R, "../#{table.pn}")
536
- end
537
-
538
- r << Relationship.new(VML_DRAWING_R, "../#{@comments.vml_drawing.pn}") if @comments.size > 0
539
- r << Relationship.new(COMMENT_R, "../#{@comments.pn}") if @comments.size > 0
540
- r << Relationship.new(COMMENT_R_NULL, "NULL") if @comments.size > 0
541
-
542
- r << Relationship.new(DRAWING_R, "../#{@drawing.pn}") if @drawing
493
+ r + [tables.relationships,
494
+ worksheet_comments.relationships,
495
+ worksheet_drawing.relationship].flatten.compact || []
543
496
  r
544
497
  end
545
498
 
@@ -548,59 +501,130 @@ module Axlsx
548
501
  # @return [Cell, Array]
549
502
  def [] (cell_def)
550
503
  return rows[cell_def] if cell_def.is_a?(Integer)
551
-
552
- parts = cell_def.split(':')
553
- first = name_to_cell parts[0]
504
+ parts = cell_def.split(':').map{ |part| name_to_cell part }
554
505
  if parts.size == 1
555
- first
506
+ parts.first
556
507
  else
557
- cells = []
558
- last = name_to_cell(parts[1])
559
- rows[(first.row.index..last.row.index)].each do |r|
560
- r.cells[(first.index..last.index)].each do |c|
561
- cells << c
562
- end
563
- end
564
- cells
508
+ range(*parts)
565
509
  end
566
510
  end
567
511
 
512
+ # returns the column and row index for a named based cell
513
+ # @param [String] name The cell or cell range to return. "A1" will return the first cell of the first row.
514
+ # @return [Cell]
515
+ def name_to_cell(name)
516
+ col_index, row_index = *Axlsx::name_to_indices(name)
517
+ r = rows[row_index]
518
+ r.cells[col_index] if r
519
+ end
520
+
521
+ # shortcut method to access styles direclty from the worksheet
522
+ # This lets us do stuff like:
523
+ # @example
524
+ # p = Axlsx::Package.new
525
+ # p.workbook.add_worksheet(:name => 'foo') do |sheet|
526
+ # my_style = sheet.styles.add_style { :bg_color => "FF0000" }
527
+ # sheet.add_row ['Oh No!'], :styles => my_style
528
+ # end
529
+ # p.serialize 'foo.xlsx'
530
+ def styles
531
+ @styles ||= self.workbook.styles
532
+ end
533
+
534
+
568
535
  private
569
536
 
570
- # assigns the owner workbook for this worksheet
571
- def workbook=(v) DataTypeValidator.validate "Worksheet.workbook", Workbook, v; @workbook = v; end
537
+ def validate_sheet_name(name)
538
+ DataTypeValidator.validate "Worksheet.name", String, name
539
+ raise ArgumentError, (ERR_SHEET_NAME_TOO_LONG % name) if name.size > 31
540
+ raise ArgumentError, (ERR_SHEET_NAME_COLON_FORBIDDEN % name) if name.include? ':'
541
+ name = Axlsx::coder.encode(name)
542
+ sheet_names = @workbook.worksheets.map { |s| s.name }
543
+ raise ArgumentError, (ERR_DUPLICATE_SHEET_NAME % name) if sheet_names.include?(name)
544
+ end
572
545
 
573
546
 
574
- # TODO this needs cleanup!
575
- def update_column_info(cells, widths=[], style=[])
576
- styles = self.workbook.styles
577
- cellXfs, fonts = styles.cellXfs, styles.fonts
578
- sz = 11
547
+ def serializable_parts
548
+ [sheet_pr, dimension, sheet_view, column_info,
549
+ sheet_data, @sheet_protection, protected_ranges,
550
+ auto_filter, merged_cells, conditional_formattings,
551
+ data_validations, print_options, page_margins,
552
+ page_setup, worksheet_drawing, worksheet_comments,
553
+ tables]
554
+ end
579
555
 
580
- cells.each_with_index do |cell, index|
581
- @column_info[index] ||= Col.new index+1, index+1
582
- col = @column_info[index]
583
- width = widths[index]
584
- col.width = width if [Integer, Float, Fixnum].include?(width.class)
585
- c_style = style[index] if [Integer, Fixnum].include?(style[index].class)
586
- next if width == :ignore || (cell.value.is_a?(String) && cell.value.start_with?('=') || cell.value == nil)
587
- if self.workbook.use_autowidth
588
- cell_xf = cellXfs[(c_style || 0)]
589
- font = fonts[(cell_xf.fontId || 0)]
590
- sz = cell.sz || font.sz || sz
591
- col.width = [(col.width || 0), calculate_width(cell.value.to_s, sz)].max
556
+ def range(*cell_def)
557
+ first, last = cell_def
558
+ cells = []
559
+ rows[(first.row.index..last.row.index)].each do |r|
560
+ r.cells[(first.index..last.index)].each do |c|
561
+ cells << c
592
562
  end
593
563
  end
564
+ cells
565
+ end
566
+
567
+ # A collection of protected ranges in the worksheet
568
+ # @note The recommended way to manage protected ranges is with Worksheet#protect_range
569
+ # @see Worksheet#protect_range
570
+ # @return [SimpleTypedList] The protected ranges for this worksheet
571
+ def protected_ranges
572
+ @protected_ranges ||= ProtectedRanges.new self
573
+ # SimpleTypedList.new ProtectedRange
574
+ end
575
+
576
+ # conditional formattings
577
+ # @return [Array]
578
+ def conditional_formattings
579
+ @conditional_formattings ||= ConditionalFormattings.new self
580
+ end
581
+
582
+ # data validations array
583
+ # @return [Array]
584
+ def data_validations
585
+ @data_validations ||= DataValidations.new self
586
+ end
587
+
588
+ # merged cells array
589
+ # @return [Array]
590
+ def merged_cells
591
+ @merged_cells ||= MergedCells.new self
592
+ end
593
+
594
+
595
+ # Helper method for parsingout the root node for worksheet
596
+ # @return [String]
597
+ def worksheet_node
598
+ "<worksheet xmlns=\"%s\" xmlns:r=\"%s\">" % [XML_NS, XML_NS_R]
599
+ end
600
+
601
+ def sheet_data
602
+ @sheet_data ||= SheetData.new self
603
+ end
604
+
605
+ def worksheet_drawing
606
+ @worksheet_drawing ||= WorksheetDrawing.new self
607
+ end
608
+
609
+ # The comments associated with this worksheet
610
+ # @return [SimpleTypedList]
611
+ def worksheet_comments
612
+ @worksheet_comments ||= WorksheetComments.new self
594
613
  end
595
614
 
615
+ def workbook=(v) DataTypeValidator.validate "Worksheet.workbook", Workbook, v; @workbook = v; end
616
+
617
+ def update_column_info(cells, widths=[])
618
+ cells.each_with_index do |cell, index|
619
+ col = find_or_create_column_info(index)
620
+ next if widths[index] == :ignore
621
+ col.update_width(cell, widths[index], workbook.use_autowidth)
622
+ end
623
+ end
596
624
 
597
- # This is still not perfect...
598
- # - scaling is not linear as font sizes increst
599
- # - different fonts have different mdw and char widths
600
- def calculate_width(text, sz)
601
- mdw = 1.78 #This is the widest width of 0..9 in arial@10px)
602
- font_scale = (sz/10.0).to_f
603
- ((text.count(Worksheet.thin_chars) * mdw + 5) / mdw * 256) / 256.0 * font_scale
625
+ def find_or_create_column_info(index)
626
+ column_info[index] ||= Col.new(index + 1, index + 1)
604
627
  end
628
+
605
629
  end
606
630
  end