axlsx 1.0.16 → 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 (173) hide show
  1. data/.yardopts +1 -0
  2. data/CHANGELOG.md +18 -3
  3. data/README.md +189 -55
  4. data/examples/example.csv +1000 -0
  5. data/examples/example.rb +264 -142
  6. data/examples/example.xlsx +0 -0
  7. data/examples/example_streamed.xlsx +0 -0
  8. data/examples/no-use_autowidth.xlsx +0 -0
  9. data/examples/shared_strings_example.xlsx +0 -0
  10. data/lib/axlsx/content_type/content_type.rb +13 -12
  11. data/lib/axlsx/content_type/default.rb +10 -6
  12. data/lib/axlsx/content_type/override.rb +13 -8
  13. data/lib/axlsx/doc_props/app.rb +38 -41
  14. data/lib/axlsx/doc_props/core.rb +13 -17
  15. data/lib/axlsx/drawing/axis.rb +39 -19
  16. data/lib/axlsx/drawing/bar_3D_chart.rb +34 -32
  17. data/lib/axlsx/drawing/bar_series.rb +14 -14
  18. data/lib/axlsx/drawing/cat_axis.rb +16 -14
  19. data/lib/axlsx/drawing/cat_axis_data.rb +17 -18
  20. data/lib/axlsx/drawing/chart.rb +40 -41
  21. data/lib/axlsx/drawing/drawing.rb +16 -12
  22. data/lib/axlsx/drawing/graphic_frame.rb +22 -21
  23. data/lib/axlsx/drawing/hyperlink.rb +13 -12
  24. data/lib/axlsx/drawing/line_3D_chart.rb +31 -28
  25. data/lib/axlsx/drawing/line_series.rb +12 -11
  26. data/lib/axlsx/drawing/marker.rb +11 -8
  27. data/lib/axlsx/drawing/named_axis_data.rb +36 -0
  28. data/lib/axlsx/drawing/one_cell_anchor.rb +18 -16
  29. data/lib/axlsx/drawing/pic.rb +26 -38
  30. data/lib/axlsx/drawing/picture_locking.rb +22 -18
  31. data/lib/axlsx/drawing/pie_3D_chart.rb +11 -8
  32. data/lib/axlsx/drawing/pie_series.rb +16 -12
  33. data/lib/axlsx/drawing/scaling.rb +11 -10
  34. data/lib/axlsx/drawing/scatter_chart.rb +69 -0
  35. data/lib/axlsx/drawing/scatter_series.rb +39 -0
  36. data/lib/axlsx/drawing/ser_axis.rb +11 -10
  37. data/lib/axlsx/drawing/series.rb +16 -15
  38. data/lib/axlsx/drawing/series_title.rb +15 -14
  39. data/lib/axlsx/drawing/title.rb +27 -26
  40. data/lib/axlsx/drawing/two_cell_anchor.rb +19 -20
  41. data/lib/axlsx/drawing/val_axis.rb +9 -7
  42. data/lib/axlsx/drawing/val_axis_data.rb +18 -17
  43. data/lib/axlsx/drawing/view_3D.rb +23 -20
  44. data/lib/axlsx/package.rb +116 -51
  45. data/lib/axlsx/rels/relationship.rb +12 -8
  46. data/lib/axlsx/rels/relationships.rb +8 -1
  47. data/lib/axlsx/stylesheet/#num_fmt.rb# +69 -0
  48. data/lib/axlsx/stylesheet/border.rb +28 -23
  49. data/lib/axlsx/stylesheet/border_pr.rb +17 -15
  50. data/lib/axlsx/stylesheet/cell_alignment.rb +24 -21
  51. data/lib/axlsx/stylesheet/cell_protection.rb +11 -7
  52. data/lib/axlsx/stylesheet/cell_style.rb +9 -5
  53. data/lib/axlsx/stylesheet/color.rb +21 -14
  54. data/lib/axlsx/stylesheet/fill.rb +8 -5
  55. data/lib/axlsx/stylesheet/font.rb +22 -14
  56. data/lib/axlsx/stylesheet/gradient_fill.rb +20 -17
  57. data/lib/axlsx/stylesheet/gradient_stop.rb +10 -6
  58. data/lib/axlsx/stylesheet/num_fmt.rb +13 -6
  59. data/lib/axlsx/stylesheet/pattern_fill.rb +26 -10
  60. data/lib/axlsx/stylesheet/styles.rb +53 -41
  61. data/lib/axlsx/stylesheet/table_style.rb +10 -4
  62. data/lib/axlsx/stylesheet/table_style_element.rb +11 -7
  63. data/lib/axlsx/stylesheet/table_styles.rb +12 -8
  64. data/lib/axlsx/stylesheet/xf.rb +30 -25
  65. data/lib/axlsx/util/cbf.rb +1 -0
  66. data/lib/axlsx/util/constants.rb +27 -14
  67. data/lib/axlsx/util/ms_off_crypto.rb +1 -1
  68. data/lib/axlsx/util/parser.rb +1 -0
  69. data/lib/axlsx/util/simple_typed_list.rb +20 -10
  70. data/lib/axlsx/util/storage.rb +1 -0
  71. data/lib/axlsx/util/validators.rb +23 -7
  72. data/lib/axlsx/version.rb +2 -1
  73. data/lib/axlsx/workbook/shared_strings_table.rb +69 -0
  74. data/lib/axlsx/workbook/shared_strings_table.rb~ +69 -0
  75. data/lib/axlsx/workbook/workbook.rb +81 -35
  76. data/lib/axlsx/workbook/worksheet/cell.rb +172 -117
  77. data/lib/axlsx/workbook/worksheet/col.rb +114 -0
  78. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  79. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +29 -0
  80. data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -0
  81. data/lib/axlsx/workbook/worksheet/row.rb +53 -21
  82. data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
  83. data/lib/axlsx/workbook/worksheet/table.rb +96 -0
  84. data/lib/axlsx/workbook/worksheet/table.rb~ +97 -0
  85. data/lib/axlsx/workbook/worksheet/worksheet.rb +269 -121
  86. data/lib/axlsx.rb +41 -14
  87. data/lib/schema/dc.xsd +5 -5
  88. data/lib/schema/dcmitype.xsd +5 -3
  89. data/lib/schema/dcterms.xsd +15 -15
  90. data/lib/schema/opc-coreProperties.xsd +6 -2
  91. data/lib/schema/xml.xsd +7 -8
  92. data/test/#benchmark.txt# +7 -0
  93. data/test/#tc_helper.rb# +3 -0
  94. data/test/benchmark.rb +81 -0
  95. data/test/benchmark.rb~ +0 -0
  96. data/test/benchmark.txt +6 -0
  97. data/test/benchmark.txt~ +6 -0
  98. data/test/content_type/tc_content_type.rb +31 -31
  99. data/test/content_type/tc_default.rb +9 -22
  100. data/test/content_type/tc_override.rb +8 -21
  101. data/test/doc_props/tc_app.rb +2 -8
  102. data/test/doc_props/tc_core.rb +6 -7
  103. data/test/drawing/tc_axis.rb +7 -3
  104. data/test/drawing/tc_bar_3D_chart.rb +6 -7
  105. data/test/drawing/tc_bar_series.rb +4 -5
  106. data/test/drawing/tc_cat_axis.rb +2 -3
  107. data/test/drawing/tc_cat_axis_data.rb +2 -3
  108. data/test/drawing/tc_chart.rb +13 -14
  109. data/test/drawing/tc_drawing.rb +7 -8
  110. data/test/drawing/tc_graphic_frame.rb +3 -4
  111. data/test/drawing/tc_hyperlink.rb +2 -3
  112. data/test/drawing/tc_line_3d_chart.rb +5 -6
  113. data/test/drawing/tc_line_series.rb +3 -4
  114. data/test/drawing/tc_marker.rb +3 -4
  115. data/test/drawing/tc_one_cell_anchor.rb +6 -7
  116. data/test/drawing/tc_pic.rb +8 -9
  117. data/test/drawing/tc_picture_locking.rb +2 -3
  118. data/test/drawing/tc_pie_3D_chart.rb +5 -6
  119. data/test/drawing/tc_pie_series.rb +4 -5
  120. data/test/drawing/tc_scaling.rb +3 -4
  121. data/test/drawing/tc_scatter_chart.rb +43 -0
  122. data/test/drawing/tc_scatter_series.rb +20 -0
  123. data/test/drawing/tc_ser_axis.rb +2 -3
  124. data/test/drawing/tc_series.rb +4 -5
  125. data/test/drawing/tc_series_title.rb +4 -5
  126. data/test/drawing/tc_title.rb +4 -5
  127. data/test/drawing/tc_two_cell_anchor.rb +4 -5
  128. data/test/drawing/tc_val_axis.rb +2 -3
  129. data/test/drawing/tc_val_axis_data.rb +2 -3
  130. data/test/drawing/tc_view_3D.rb +6 -7
  131. data/test/example.csv +1000 -0
  132. data/test/example.xlsx +0 -0
  133. data/test/example_streamed.xlsx +0 -0
  134. data/test/profile.rb +33 -0
  135. data/test/rels/tc_relationship.rb +5 -6
  136. data/test/rels/tc_relationships.rb +4 -5
  137. data/test/stylesheet/tc_border.rb +3 -4
  138. data/test/stylesheet/tc_border_pr.rb +3 -4
  139. data/test/stylesheet/tc_cell_alignment.rb +10 -6
  140. data/test/stylesheet/tc_cell_protection.rb +2 -3
  141. data/test/stylesheet/tc_cell_style.rb +2 -3
  142. data/test/stylesheet/tc_color.rb +2 -3
  143. data/test/stylesheet/tc_fill.rb +1 -2
  144. data/test/stylesheet/tc_font.rb +12 -5
  145. data/test/stylesheet/tc_gradient_fill.rb +1 -2
  146. data/test/stylesheet/tc_gradient_stop.rb +1 -2
  147. data/test/stylesheet/tc_num_fmt.rb +1 -2
  148. data/test/stylesheet/tc_pattern_fill.rb +3 -4
  149. data/test/stylesheet/tc_styles.rb +16 -6
  150. data/test/stylesheet/tc_table_style.rb +2 -3
  151. data/test/stylesheet/tc_table_style_element.rb +2 -3
  152. data/test/stylesheet/tc_table_styles.rb +3 -4
  153. data/test/stylesheet/tc_xf.rb +16 -17
  154. data/test/tc_axlsx.rb +39 -0
  155. data/test/tc_axlsx.rb~ +0 -0
  156. data/test/tc_helper.rb +3 -0
  157. data/test/tc_helper.rb~ +3 -0
  158. data/test/tc_package.rb +43 -9
  159. data/test/util/tc_simple_typed_list.rb +8 -9
  160. data/test/util/tc_validators.rb +7 -8
  161. data/test/workbook/tc_shared_strings_table.rb +38 -0
  162. data/test/workbook/tc_workbook.rb +32 -6
  163. data/test/workbook/worksheet/table/tc_table.rb +71 -0
  164. data/test/workbook/worksheet/table/tc_table.rb~ +72 -0
  165. data/test/workbook/worksheet/tc_cell.rb +70 -13
  166. data/test/workbook/worksheet/tc_col.rb +59 -0
  167. data/test/workbook/worksheet/tc_col.rb~ +10 -0
  168. data/test/workbook/worksheet/tc_date_time_converter.rb +126 -0
  169. data/test/workbook/worksheet/tc_page_margins.rb +97 -0
  170. data/test/workbook/worksheet/tc_row.rb +54 -4
  171. data/test/workbook/worksheet/tc_worksheet.rb +191 -31
  172. metadata +162 -109
  173. data/test/drawing/tc_hyperlink.rb~ +0 -71
@@ -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
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # A Row is a single row in a worksheet.
3
4
  # @note The recommended way to manage rows and cells is to use Worksheet#add_row
@@ -12,6 +13,22 @@ module Axlsx
12
13
  # @return [SimpleTypedList]
13
14
  attr_reader :cells
14
15
 
16
+ # The height of this row in points, if set explicitly.
17
+ # @return [Float]
18
+ attr_reader :height
19
+
20
+ # TODO 18.3.1.73
21
+ # collapsed
22
+ # customFormat
23
+ # hidden
24
+ # outlineLevel
25
+ # ph
26
+ # s (style)
27
+ # spans
28
+ # thickTop
29
+ # thickBottom
30
+
31
+
15
32
  # Creates a new row. New Cell objects are created based on the values, types and style options.
16
33
  # A new cell is created for each item in the values array. style and types options are applied as follows:
17
34
  # If the types option is defined and is a symbol it is applied to all the cells created.
@@ -22,38 +39,50 @@ module Axlsx
22
39
  # If the style option is not defined, the default style (0) is applied to each cell.
23
40
  # @param [Worksheet] worksheet
24
41
  # @option options [Array] values
25
- # @option options [Array, Symbol] types
26
- # @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)
27
45
  # @see Row#array_to_cells
28
46
  # @see Cell
29
47
  def initialize(worksheet, values=[], options={})
48
+ @height = nil
30
49
  self.worksheet = worksheet
31
50
  @cells = SimpleTypedList.new Cell
32
51
  @worksheet.rows << self
52
+ self.height = options.delete(:height) if options[:height]
33
53
  array_to_cells(values, options)
34
54
  end
35
55
 
36
56
  # The index of this row in the worksheet
37
57
  # @return [Integer]
38
- def index
58
+ def index
39
59
  worksheet.rows.index(self)
40
60
  end
41
-
61
+
42
62
  # Serializes the row
43
- # @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.
44
65
  # @return [String]
45
- def to_xml(xml)
46
- 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
47
76
  end
48
77
 
49
78
  # Adds a singel sell to the row based on the data provided and updates the worksheet's autofit data.
50
79
  # @return [Cell]
51
80
  def add_cell(value="", options={})
52
81
  c = Cell.new(self, value, options)
53
- update_auto_fit_data
82
+ worksheet.send(:update_column_info, self.cells, self.cells.map(&:style))
54
83
  c
55
84
  end
56
-
85
+
57
86
  # sets the style for every cell in this row
58
87
  def style=(style)
59
88
  cells.each_with_index do | cell, index |
@@ -63,23 +92,26 @@ module Axlsx
63
92
  end
64
93
 
65
94
  # returns the cells in this row as an array
66
- # This lets us transpose the rows into columns
95
+ # This lets us transpose the rows into columns
67
96
  # @return [Array]
68
97
  def to_ary
69
98
  @cells.to_ary
70
99
  end
71
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
+
72
111
  private
73
112
 
74
113
  # assigns the owning worksheet for this row
75
114
  def worksheet=(v) DataTypeValidator.validate "Row.worksheet", Worksheet, v; @worksheet=v; end
76
-
77
- # Tell the worksheet to update autofit data for the columns based on this row's cells.
78
- # @return [SimpleTypedList]
79
- def update_auto_fit_data
80
- worksheet.send(:update_auto_fit_data, self.cells)
81
- end
82
-
83
115
 
84
116
  # Converts values, types, and style options into cells and associates them with this row.
85
117
  # A new cell is created for each item in the values array.
@@ -88,13 +120,13 @@ module Axlsx
88
120
  # If the style option is defined and is an Integer, it is applied to all cells created.
89
121
  # If the style option is an array, style is applied by index for each cell.
90
122
  # @option options [Array] values
91
- # @option options [Array, Symbol] types
92
- # @option options [Array, Integer] style
123
+ # @option options [Array, Symbol] types
124
+ # @option options [Array, Integer] style
93
125
  def array_to_cells(values, options={})
94
126
  values = values
95
127
  DataTypeValidator.validate 'Row.array_to_cells', Array, values
96
128
  types, style = options.delete(:types), options.delete(:style)
97
- values.each_with_index do |value, index|
129
+ values.each_with_index do |value, index|
98
130
  cell_style = style.is_a?(Array) ? style[index] : style
99
131
  options[:style] = cell_style if cell_style
100
132
  cell_type = types.is_a?(Array)? types[index] : types
@@ -103,5 +135,5 @@ module Axlsx
103
135
  end
104
136
  end
105
137
  end
106
-
138
+
107
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