axlsx 1.0.17 → 1.1.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 (167) hide show
  1. data/CHANGELOG.md +11 -3
  2. data/README.md +159 -51
  3. data/examples/example.csv +1000 -0
  4. data/examples/example.rb +255 -150
  5. data/examples/example.xlsx +0 -0
  6. data/examples/example_streamed.xlsx +0 -0
  7. data/examples/no-use_autowidth.xlsx +0 -0
  8. data/examples/shared_strings_example.xlsx +0 -0
  9. data/lib/axlsx/content_type/content_type.rb +12 -12
  10. data/lib/axlsx/content_type/default.rb +9 -6
  11. data/lib/axlsx/content_type/override.rb +12 -8
  12. data/lib/axlsx/doc_props/app.rb +37 -40
  13. data/lib/axlsx/doc_props/core.rb +12 -17
  14. data/lib/axlsx/drawing/axis.rb +38 -19
  15. data/lib/axlsx/drawing/bar_3D_chart.rb +33 -32
  16. data/lib/axlsx/drawing/bar_series.rb +13 -14
  17. data/lib/axlsx/drawing/cat_axis.rb +15 -14
  18. data/lib/axlsx/drawing/cat_axis_data.rb +16 -18
  19. data/lib/axlsx/drawing/chart.rb +39 -40
  20. data/lib/axlsx/drawing/drawing.rb +15 -12
  21. data/lib/axlsx/drawing/graphic_frame.rb +21 -21
  22. data/lib/axlsx/drawing/hyperlink.rb +12 -11
  23. data/lib/axlsx/drawing/line_3D_chart.rb +30 -28
  24. data/lib/axlsx/drawing/line_series.rb +11 -11
  25. data/lib/axlsx/drawing/marker.rb +10 -8
  26. data/lib/axlsx/drawing/named_axis_data.rb +36 -0
  27. data/lib/axlsx/drawing/one_cell_anchor.rb +17 -16
  28. data/lib/axlsx/drawing/pic.rb +25 -37
  29. data/lib/axlsx/drawing/picture_locking.rb +21 -18
  30. data/lib/axlsx/drawing/pie_3D_chart.rb +10 -8
  31. data/lib/axlsx/drawing/pie_series.rb +15 -12
  32. data/lib/axlsx/drawing/scaling.rb +10 -10
  33. data/lib/axlsx/drawing/scatter_chart.rb +69 -0
  34. data/lib/axlsx/drawing/scatter_series.rb +39 -0
  35. data/lib/axlsx/drawing/ser_axis.rb +10 -10
  36. data/lib/axlsx/drawing/series.rb +15 -15
  37. data/lib/axlsx/drawing/series_title.rb +14 -14
  38. data/lib/axlsx/drawing/title.rb +26 -26
  39. data/lib/axlsx/drawing/two_cell_anchor.rb +18 -20
  40. data/lib/axlsx/drawing/val_axis.rb +8 -7
  41. data/lib/axlsx/drawing/val_axis_data.rb +17 -17
  42. data/lib/axlsx/drawing/view_3D.rb +22 -20
  43. data/lib/axlsx/package.rb +60 -43
  44. data/lib/axlsx/rels/relationship.rb +11 -8
  45. data/lib/axlsx/rels/relationships.rb +7 -1
  46. data/lib/axlsx/stylesheet/#num_fmt.rb# +69 -0
  47. data/lib/axlsx/stylesheet/border.rb +27 -23
  48. data/lib/axlsx/stylesheet/border_pr.rb +16 -15
  49. data/lib/axlsx/stylesheet/cell_alignment.rb +23 -21
  50. data/lib/axlsx/stylesheet/cell_protection.rb +10 -7
  51. data/lib/axlsx/stylesheet/cell_style.rb +8 -5
  52. data/lib/axlsx/stylesheet/color.rb +20 -14
  53. data/lib/axlsx/stylesheet/fill.rb +7 -5
  54. data/lib/axlsx/stylesheet/font.rb +21 -14
  55. data/lib/axlsx/stylesheet/gradient_fill.rb +19 -16
  56. data/lib/axlsx/stylesheet/gradient_stop.rb +9 -5
  57. data/lib/axlsx/stylesheet/num_fmt.rb +12 -6
  58. data/lib/axlsx/stylesheet/pattern_fill.rb +25 -10
  59. data/lib/axlsx/stylesheet/styles.rb +47 -35
  60. data/lib/axlsx/stylesheet/table_style.rb +9 -4
  61. data/lib/axlsx/stylesheet/table_style_element.rb +10 -7
  62. data/lib/axlsx/stylesheet/table_styles.rb +11 -8
  63. data/lib/axlsx/stylesheet/xf.rb +29 -25
  64. data/lib/axlsx/util/constants.rb +20 -14
  65. data/lib/axlsx/util/simple_typed_list.rb +18 -9
  66. data/lib/axlsx/util/validators.rb +13 -6
  67. data/lib/axlsx/version.rb +1 -1
  68. data/lib/axlsx/workbook/shared_strings_table.rb +19 -21
  69. data/lib/axlsx/workbook/workbook.rb +58 -34
  70. data/lib/axlsx/workbook/worksheet/cell.rb +149 -132
  71. data/lib/axlsx/workbook/worksheet/col.rb +114 -0
  72. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  73. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +29 -0
  74. data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -0
  75. data/lib/axlsx/workbook/worksheet/row.rb +40 -23
  76. data/lib/axlsx/workbook/worksheet/table.rb +96 -0
  77. data/lib/axlsx/workbook/worksheet/table.rb~ +97 -0
  78. data/lib/axlsx/workbook/worksheet/worksheet.rb +243 -127
  79. data/lib/axlsx.rb +30 -9
  80. data/lib/schema/dc.xsd +5 -5
  81. data/lib/schema/dcmitype.xsd +5 -3
  82. data/lib/schema/dcterms.xsd +15 -15
  83. data/lib/schema/opc-coreProperties.xsd +6 -2
  84. data/lib/schema/xml.xsd +7 -8
  85. data/test/#benchmark.txt# +7 -0
  86. data/test/#tc_helper.rb# +3 -0
  87. data/test/benchmark.rb +81 -0
  88. data/test/benchmark.rb~ +0 -0
  89. data/test/benchmark.txt +6 -0
  90. data/test/benchmark.txt~ +6 -0
  91. data/test/content_type/tc_content_type.rb +30 -32
  92. data/test/content_type/tc_default.rb +8 -23
  93. data/test/content_type/tc_override.rb +7 -21
  94. data/test/doc_props/tc_app.rb +2 -8
  95. data/test/doc_props/tc_core.rb +6 -7
  96. data/test/drawing/tc_axis.rb +7 -3
  97. data/test/drawing/tc_bar_3D_chart.rb +6 -7
  98. data/test/drawing/tc_bar_series.rb +4 -5
  99. data/test/drawing/tc_cat_axis.rb +2 -3
  100. data/test/drawing/tc_cat_axis_data.rb +2 -3
  101. data/test/drawing/tc_chart.rb +13 -14
  102. data/test/drawing/tc_drawing.rb +7 -8
  103. data/test/drawing/tc_graphic_frame.rb +3 -4
  104. data/test/drawing/tc_hyperlink.rb +2 -3
  105. data/test/drawing/tc_line_3d_chart.rb +5 -6
  106. data/test/drawing/tc_line_series.rb +3 -4
  107. data/test/drawing/tc_marker.rb +3 -4
  108. data/test/drawing/tc_one_cell_anchor.rb +6 -7
  109. data/test/drawing/tc_pic.rb +8 -9
  110. data/test/drawing/tc_picture_locking.rb +2 -3
  111. data/test/drawing/tc_pie_3D_chart.rb +5 -6
  112. data/test/drawing/tc_pie_series.rb +4 -5
  113. data/test/drawing/tc_scaling.rb +3 -4
  114. data/test/drawing/tc_scatter_chart.rb +43 -0
  115. data/test/drawing/tc_scatter_series.rb +20 -0
  116. data/test/drawing/tc_ser_axis.rb +2 -3
  117. data/test/drawing/tc_series.rb +4 -5
  118. data/test/drawing/tc_series_title.rb +4 -5
  119. data/test/drawing/tc_title.rb +4 -5
  120. data/test/drawing/tc_two_cell_anchor.rb +4 -5
  121. data/test/drawing/tc_val_axis.rb +2 -3
  122. data/test/drawing/tc_val_axis_data.rb +2 -3
  123. data/test/drawing/tc_view_3D.rb +6 -7
  124. data/test/example.csv +1000 -0
  125. data/test/example.xlsx +0 -0
  126. data/test/example_streamed.xlsx +0 -0
  127. data/test/profile.rb +33 -0
  128. data/test/rels/tc_relationship.rb +5 -6
  129. data/test/rels/tc_relationships.rb +4 -5
  130. data/test/stylesheet/tc_border.rb +3 -4
  131. data/test/stylesheet/tc_border_pr.rb +3 -4
  132. data/test/stylesheet/tc_cell_alignment.rb +10 -6
  133. data/test/stylesheet/tc_cell_protection.rb +2 -3
  134. data/test/stylesheet/tc_cell_style.rb +2 -3
  135. data/test/stylesheet/tc_color.rb +2 -3
  136. data/test/stylesheet/tc_fill.rb +1 -2
  137. data/test/stylesheet/tc_font.rb +12 -5
  138. data/test/stylesheet/tc_gradient_fill.rb +1 -2
  139. data/test/stylesheet/tc_gradient_stop.rb +1 -2
  140. data/test/stylesheet/tc_num_fmt.rb +1 -2
  141. data/test/stylesheet/tc_pattern_fill.rb +3 -4
  142. data/test/stylesheet/tc_styles.rb +16 -6
  143. data/test/stylesheet/tc_table_style.rb +2 -3
  144. data/test/stylesheet/tc_table_style_element.rb +2 -3
  145. data/test/stylesheet/tc_table_styles.rb +3 -4
  146. data/test/stylesheet/tc_xf.rb +16 -17
  147. data/test/tc_axlsx.rb +39 -0
  148. data/test/tc_axlsx.rb~ +0 -0
  149. data/test/tc_helper.rb +3 -0
  150. data/test/tc_helper.rb~ +3 -0
  151. data/test/tc_package.rb +13 -10
  152. data/test/util/tc_simple_typed_list.rb +8 -9
  153. data/test/util/tc_validators.rb +7 -8
  154. data/test/workbook/tc_shared_strings_table.rb +5 -6
  155. data/test/workbook/tc_workbook.rb +24 -6
  156. data/test/workbook/worksheet/table/tc_table.rb +71 -0
  157. data/test/workbook/worksheet/table/tc_table.rb~ +72 -0
  158. data/test/workbook/worksheet/tc_cell.rb +62 -18
  159. data/test/workbook/worksheet/tc_col.rb +59 -0
  160. data/test/workbook/worksheet/tc_col.rb~ +10 -0
  161. data/test/workbook/worksheet/tc_date_time_converter.rb +126 -0
  162. data/test/workbook/worksheet/tc_page_margins.rb +97 -0
  163. data/test/workbook/worksheet/tc_row.rb +54 -4
  164. data/test/workbook/worksheet/tc_worksheet.rb +177 -34
  165. metadata +69 -41
  166. data/test/drawing/tc_hyperlink.rb~ +0 -71
  167. data/test/workbook/tc_shared_strings_table.rb~ +0 -8
@@ -0,0 +1,97 @@
1
+ module Axlsx
2
+ # PageMargins specify the margins when printing a worksheet.
3
+ #
4
+ # For compatibility, PageMargins serialize to an empty string, unless at least one custom margin value
5
+ # has been specified. Otherwise, it serializes to a PageMargin element specifying all 6 margin values
6
+ # (using default values for margins that have not been specified explicitly).
7
+ #
8
+ # @note The recommended way to manage page margins is via Worksheet#page_margins
9
+ # @see Worksheet#page_margins
10
+ # @see Worksheet#initialize
11
+ class PageMargins
12
+
13
+ # Default left and right margin (in inches)
14
+ DEFAULT_LEFT_RIGHT = 0.75
15
+
16
+ # Default top and bottom margins (in inches)
17
+ DEFAULT_TOP_BOTTOM = 1.00
18
+
19
+ # Default header and footer margins (in inches)
20
+ DEFAULT_HEADER_FOOTER = 0.50
21
+
22
+ # Left margin (in inches)
23
+ # @return [Float]
24
+ attr_reader :left
25
+
26
+ # Right margin (in inches)
27
+ # @return [Float]
28
+ attr_reader :right
29
+
30
+ # Top margin (in inches)
31
+ # @return [Float]
32
+ attr_reader :top
33
+
34
+ # Bottom margin (in inches)
35
+ # @return [Float]
36
+ attr_reader :bottom
37
+
38
+ # Header margin (in inches)
39
+ # @return [Float]
40
+ attr_reader :header
41
+
42
+ # Footer margin (in inches)
43
+ # @return [Float]
44
+ attr_reader :footer
45
+
46
+ # Creates a new PageMargins object
47
+ # @option options [Numeric] left The left margin in inches
48
+ # @option options [Numeric] right The right margin in inches
49
+ # @option options [Numeric] bottom The bottom margin in inches
50
+ # @option options [Numeric] top The top margin in inches
51
+ # @option options [Numeric] header The header margin in inches
52
+ # @option options [Numeric] footer The footer margin in inches
53
+ def initialize(options={})
54
+ # Default values taken from MS Excel for Mac 2011
55
+ @left = @right = DEFAULT_LEFT_RIGHT
56
+ @top = @bottom = DEFAULT_TOP_BOTTOM
57
+ @header = @footer = DEFAULT_HEADER_FOOTER
58
+
59
+ options.each do |o|
60
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
61
+ end
62
+ end
63
+
64
+ # Set some or all margins at once.
65
+ # @param [Hash] margins the margins to set (possible keys are :left, :right, :top, :bottom, :header and :footer).
66
+ def set(margins)
67
+ margins.select do |k, v|
68
+ next unless [:left, :right, :top, :bottom, :header, :footer].include? k
69
+ send("#{k}=", v)
70
+ end
71
+ end
72
+
73
+ # @see left
74
+ def left=(v); Axlsx::validate_unsigned_numeric(v); @left = v end
75
+ # @see right
76
+ def right=(v); Axlsx::validate_unsigned_numeric(v); @right = v end
77
+ # @see top
78
+ def top=(v); Axlsx::validate_unsigned_numeric(v); @top = v end
79
+ # @see bottom
80
+ def bottom=(v); Axlsx::validate_unsigned_numeric(v); @bottom = v end
81
+ # @see header
82
+ def header=(v); Axlsx::validate_unsigned_numeric(v); @header = v end
83
+ # @see footer
84
+ def footer=(v); Axlsx::validate_unsigned_numeric(v); @footer = v end
85
+
86
+ # Serializes the page margins element
87
+ # @param [String] str
88
+ # @return [String]
89
+ # @note For compatibility, this is a noop unless custom margins have been specified.
90
+ # @see #custom_margins_specified?
91
+ def to_xml_string(str = '')
92
+ str << '<pageMargins '
93
+ str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' }.join(' ')
94
+ str << '/>'
95
+ end
96
+ end
97
+ end
@@ -13,12 +13,14 @@ module Axlsx
13
13
  # @return [SimpleTypedList]
14
14
  attr_reader :cells
15
15
 
16
+ # The height of this row in points, if set explicitly.
17
+ # @return [Float]
18
+ attr_reader :height
19
+
16
20
  # TODO 18.3.1.73
17
21
  # collapsed
18
22
  # customFormat
19
- # customHeight
20
23
  # hidden
21
- # ht (height)
22
24
  # outlineLevel
23
25
  # ph
24
26
  # s (style)
@@ -37,38 +39,50 @@ module Axlsx
37
39
  # If the style option is not defined, the default style (0) is applied to each cell.
38
40
  # @param [Worksheet] worksheet
39
41
  # @option options [Array] values
40
- # @option options [Array, Symbol] types
41
- # @option options [Array, Integer] style
42
+ # @option options [Array, Symbol] types
43
+ # @option options [Array, Integer] style
44
+ # @option options [Float] height the row's height (in points)
42
45
  # @see Row#array_to_cells
43
46
  # @see Cell
44
47
  def initialize(worksheet, values=[], options={})
48
+ @height = nil
45
49
  self.worksheet = worksheet
46
50
  @cells = SimpleTypedList.new Cell
47
51
  @worksheet.rows << self
52
+ self.height = options.delete(:height) if options[:height]
48
53
  array_to_cells(values, options)
49
54
  end
50
55
 
51
56
  # The index of this row in the worksheet
52
57
  # @return [Integer]
53
- def index
58
+ def index
54
59
  worksheet.rows.index(self)
55
60
  end
56
-
61
+
57
62
  # Serializes the row
58
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
63
+ # @param [Integer] r_index The row index, 0 based.
64
+ # @param [String] str The string this rows xml will be appended to.
59
65
  # @return [String]
60
- def to_xml(xml)
61
- xml.row(:r => index+1) { @cells.each { |cell| cell.to_xml(xml) } }
66
+ def to_xml_string(r_index, str = '')
67
+ str << '<row r="' << (r_index + 1 ).to_s << '" '
68
+ if custom_height?
69
+ str << 'customHeight="1" ht="' << height.to_s << '">'
70
+ else
71
+ str << '>'
72
+ end
73
+ @cells.each_with_index { |cell, c_index| cell.to_xml_string(r_index, c_index, str) }
74
+ str << '</row>'
75
+ str
62
76
  end
63
77
 
64
78
  # Adds a singel sell to the row based on the data provided and updates the worksheet's autofit data.
65
79
  # @return [Cell]
66
80
  def add_cell(value="", options={})
67
81
  c = Cell.new(self, value, options)
68
- update_auto_fit_data
82
+ worksheet.send(:update_column_info, self.cells, self.cells.map(&:style))
69
83
  c
70
84
  end
71
-
85
+
72
86
  # sets the style for every cell in this row
73
87
  def style=(style)
74
88
  cells.each_with_index do | cell, index |
@@ -78,23 +92,26 @@ module Axlsx
78
92
  end
79
93
 
80
94
  # returns the cells in this row as an array
81
- # This lets us transpose the rows into columns
95
+ # This lets us transpose the rows into columns
82
96
  # @return [Array]
83
97
  def to_ary
84
98
  @cells.to_ary
85
99
  end
86
100
 
101
+ # @see height
102
+ def height=(v); Axlsx::validate_unsigned_numeric(v) unless v.nil?; @height = v end
103
+
104
+ # true if the row height has been manually set
105
+ # @return [Boolean]
106
+ # @see #height
107
+ def custom_height?
108
+ @height != nil
109
+ end
110
+
87
111
  private
88
112
 
89
113
  # assigns the owning worksheet for this row
90
114
  def worksheet=(v) DataTypeValidator.validate "Row.worksheet", Worksheet, v; @worksheet=v; end
91
-
92
- # Tell the worksheet to update autofit data for the columns based on this row's cells.
93
- # @return [SimpleTypedList]
94
- def update_auto_fit_data
95
- worksheet.send(:update_auto_fit_data, self.cells)
96
- end
97
-
98
115
 
99
116
  # Converts values, types, and style options into cells and associates them with this row.
100
117
  # A new cell is created for each item in the values array.
@@ -103,13 +120,13 @@ module Axlsx
103
120
  # If the style option is defined and is an Integer, it is applied to all cells created.
104
121
  # If the style option is an array, style is applied by index for each cell.
105
122
  # @option options [Array] values
106
- # @option options [Array, Symbol] types
107
- # @option options [Array, Integer] style
123
+ # @option options [Array, Symbol] types
124
+ # @option options [Array, Integer] style
108
125
  def array_to_cells(values, options={})
109
126
  values = values
110
127
  DataTypeValidator.validate 'Row.array_to_cells', Array, values
111
128
  types, style = options.delete(:types), options.delete(:style)
112
- values.each_with_index do |value, index|
129
+ values.each_with_index do |value, index|
113
130
  cell_style = style.is_a?(Array) ? style[index] : style
114
131
  options[:style] = cell_style if cell_style
115
132
  cell_type = types.is_a?(Array)? types[index] : types
@@ -118,5 +135,5 @@ module Axlsx
118
135
  end
119
136
  end
120
137
  end
121
-
138
+
122
139
  end
@@ -0,0 +1,96 @@
1
+ # encoding: UTF-8
2
+ module Axlsx
3
+ # Table
4
+ # @note Worksheet#add_table is the recommended way to create tables for your worksheets.
5
+ # @see README for examples
6
+ class Table
7
+
8
+
9
+ # The reference to the table data
10
+ # @return [String]
11
+ attr_reader :ref
12
+
13
+ # The name of the table.
14
+ # @return [String]
15
+ attr_reader :name
16
+
17
+ # The style for the table.
18
+ # @return [TableStyle]
19
+ attr_reader :style
20
+
21
+ # Creates a new Table object
22
+ # @param [String] ref The reference to the table data.
23
+ # @param [Sheet] ref The sheet containing the table data.
24
+ # @option options [Cell, String] name
25
+ # @option options [TableStyle] style
26
+ def initialize(ref, sheet, options={})
27
+ @ref = ref
28
+ @sheet = sheet
29
+ @style = nil
30
+ @sheet.workbook.tables << self
31
+ @name = "Table#{index+1}"
32
+ options.each do |o|
33
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
34
+ end
35
+ yield self if block_given?
36
+ end
37
+
38
+ # The index of this chart in the workbooks charts collection
39
+ # @return [Integer]
40
+ def index
41
+ @sheet.workbook.tables.index(self)
42
+ end
43
+
44
+ # The part name for this table
45
+ # @return [String]
46
+ def pn
47
+ "#{TABLE_PN % (index+1)}"
48
+ end
49
+
50
+ # The relation reference id for this table
51
+ # @return [String]
52
+ def rId
53
+ "rId#{index+1}"
54
+ end
55
+
56
+ # The name of the Table.
57
+ # @param [String, Cell] v
58
+ # @return [Title]
59
+ def name=(v)
60
+ DataTypeValidator.validate "#{self.class}.name", [String], v
61
+ if v.is_a?(String)
62
+ @name = v
63
+ end
64
+ end
65
+
66
+ # Serializes the object
67
+ # @param [String] str
68
+ # @return [String]
69
+ def to_xml_string(str = '')
70
+ str << '<?xml version="1.0" encoding="UTF-8"?>'
71
+ str << '<table xmlns="' << XML_NS << '" id="' << (index+1).to_s << '" name="' << @name << '" displayName="' << @name.gsub(/\s/,'_') << '" '
72
+ str << 'ref="' << @ref << '" totalsRowShown="0">'
73
+ str << '<autoFilter ref="' << @ref << '"/>'
74
+ str << '<tableColumns count="' << header_cells.length.to_s << '">'
75
+ header_cells.each_with_index do |cell,index|
76
+ str << '<tableColumn id ="' << (index+1).to_s << '" name="' << cell.value << '"/>'
77
+ end
78
+ str << '</tableColumns>'
79
+ #TODO implement tableStyleInfo
80
+ str << '<tableStyleInfo showFirstColumn="0" showLastColumn="0" showRowStripes="1" showColumnStripes="0" name="TableStyleMedium9" />'
81
+ str << '</table>'
82
+ end
83
+
84
+ # The style for the table.
85
+ # TODO
86
+ # def style=(v) DataTypeValidator.validate "Table.style", Integer, v, lambda { |arg| arg >= 1 && arg <= 48 }; @style = v; end
87
+
88
+ private
89
+
90
+ # get the header cells (hackish)
91
+ def header_cells
92
+ header = @ref.gsub(/^(\w+)(\d+)\:(\w+)\d+$/, '\1\2:\3\2')
93
+ @sheet[header]
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,97 @@
1
+ # encoding: UTF-8
2
+ module Axlsx
3
+ # Table
4
+ # @note Worksheet#add_table is the recommended way to create charts for your worksheets.
5
+ # @see README for examples
6
+ class Table
7
+
8
+
9
+ # The reference to the table data
10
+ # @return [String]
11
+ attr_reader :ref
12
+
13
+ # The name of the table.
14
+ # @return [String]
15
+ attr_reader :name
16
+
17
+ # The style for the table.
18
+ # @return [TableStyle]
19
+ attr_reader :style
20
+
21
+ # Creates a new Table object
22
+ # @param [String] ref The reference to the table data.
23
+ # @param [Sheet] ref The sheet containing the table data.
24
+ # @option options [Cell, String] name
25
+ # @option options [TableStyle] style
26
+ def initialize(ref, sheet, options={})
27
+ @ref = ref
28
+ @sheet = sheet
29
+ @style = nil
30
+ @sheet.workbook.tables << self
31
+ @name = "Table#{index+1}"
32
+ options.each do |o|
33
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
34
+ end
35
+ yield self if block_given?
36
+ end
37
+
38
+ # The index of this chart in the workbooks charts collection
39
+ # @return [Integer]
40
+ def index
41
+ @sheet.workbook.tables.index(self)
42
+ end
43
+
44
+ # The part name for this table
45
+ # @return [String]
46
+ def pn
47
+ "#{TABLE_PN % (index+1)}"
48
+ end
49
+
50
+ # The relation reference id for this table
51
+ # @return [String]
52
+ def rId
53
+ "rId#{index+1}"
54
+ end
55
+
56
+ # The name of the Table.
57
+ # @param [String, Cell] v
58
+ # @return [Title]
59
+ def name=(v)
60
+ DataTypeValidator.validate "#{self.class}.name", [String], v
61
+ if v.is_a?(String)
62
+ @name = v
63
+ end
64
+ end
65
+
66
+
67
+ # The style for the table.
68
+ # TODO
69
+ # def style=(v) DataTypeValidator.validate "Chart.style", Integer, v, lambda { |arg| arg >= 1 && arg <= 48 }; @style = v; end
70
+
71
+ # Table Serialization
72
+ # serializes the table
73
+ def to_xml
74
+ builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
75
+ xml.table(:xmlns => XML_NS, :id => index+1, :name => @name, :displayName => @name.gsub(/\s/,'_'), :ref => @ref, :totalsRowShown => 0) {
76
+ xml.autoFilter :ref=>@ref
77
+ xml.tableColumns(:count => header_cells.length) {
78
+ header_cells.each_with_index do |cell,index|
79
+ xml.tableColumn :id => index+1, :name => cell.value
80
+ end
81
+ }
82
+ xml.tableStyleInfo :showFirstColumn=>"0", :showLastColumn=>"0", :showRowStripes=>"1", :showColumnStripes=>"0", :name=>"TableStyleMedium9"
83
+ #TODO implement tableStyleInfo
84
+ }
85
+ end
86
+ builder.to_xml(:save_with => 0)
87
+ end
88
+
89
+ private
90
+
91
+ # get the header cells (hackish)
92
+ def header_cells
93
+ header = @ref.gsub(/^(\w+)(\d+)\:(\w+)\d+$/, '\1\2:\3\2')
94
+ @sheet[header]
95
+ end
96
+ end
97
+ end