axlsx 1.0.15 → 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.
- data/.yardopts +1 -0
- data/CHANGELOG.md +40 -2
- data/README.md +223 -92
- data/examples/example.csv +1000 -0
- data/examples/example.rb +266 -132
- data/examples/example.xlsx +0 -0
- data/examples/example_streamed.xlsx +0 -0
- data/examples/no-use_autowidth.xlsx +0 -0
- data/examples/shared_strings_example.xlsx +0 -0
- data/lib/axlsx/content_type/content_type.rb +13 -12
- data/lib/axlsx/content_type/default.rb +10 -6
- data/lib/axlsx/content_type/override.rb +13 -8
- data/lib/axlsx/doc_props/app.rb +38 -41
- data/lib/axlsx/doc_props/core.rb +13 -17
- data/lib/axlsx/drawing/axis.rb +39 -19
- data/lib/axlsx/drawing/bar_3D_chart.rb +34 -32
- data/lib/axlsx/drawing/bar_series.rb +14 -14
- data/lib/axlsx/drawing/cat_axis.rb +16 -14
- data/lib/axlsx/drawing/cat_axis_data.rb +17 -18
- data/lib/axlsx/drawing/chart.rb +40 -41
- data/lib/axlsx/drawing/drawing.rb +27 -12
- data/lib/axlsx/drawing/graphic_frame.rb +22 -21
- data/lib/axlsx/drawing/hlink_click.rb~ +0 -0
- data/lib/axlsx/drawing/hyperlink.rb +96 -0
- data/lib/axlsx/drawing/hyperlink.rb~ +64 -0
- data/lib/axlsx/drawing/line_3D_chart.rb +31 -28
- data/lib/axlsx/drawing/line_series.rb +12 -11
- data/lib/axlsx/drawing/marker.rb +11 -8
- data/lib/axlsx/drawing/named_axis_data.rb +36 -0
- data/lib/axlsx/drawing/one_cell_anchor.rb +18 -16
- data/lib/axlsx/drawing/pic.rb +47 -33
- data/lib/axlsx/drawing/picture_locking.rb +22 -18
- data/lib/axlsx/drawing/pie_3D_chart.rb +11 -8
- data/lib/axlsx/drawing/pie_series.rb +16 -12
- data/lib/axlsx/drawing/scaling.rb +11 -10
- data/lib/axlsx/drawing/scatter_chart.rb +69 -0
- data/lib/axlsx/drawing/scatter_series.rb +39 -0
- data/lib/axlsx/drawing/ser_axis.rb +11 -10
- data/lib/axlsx/drawing/series.rb +16 -15
- data/lib/axlsx/drawing/series_title.rb +15 -14
- data/lib/axlsx/drawing/title.rb +27 -26
- data/lib/axlsx/drawing/two_cell_anchor.rb +19 -20
- data/lib/axlsx/drawing/val_axis.rb +9 -7
- data/lib/axlsx/drawing/val_axis_data.rb +18 -17
- data/lib/axlsx/drawing/view_3D.rb +23 -20
- data/lib/axlsx/package.rb +117 -52
- data/lib/axlsx/rels/relationship.rb +27 -8
- data/lib/axlsx/rels/relationships.rb +9 -2
- data/lib/axlsx/stylesheet/#num_fmt.rb# +69 -0
- data/lib/axlsx/stylesheet/border.rb +28 -23
- data/lib/axlsx/stylesheet/border_pr.rb +17 -15
- data/lib/axlsx/stylesheet/cell_alignment.rb +24 -21
- data/lib/axlsx/stylesheet/cell_protection.rb +11 -7
- data/lib/axlsx/stylesheet/cell_style.rb +9 -5
- data/lib/axlsx/stylesheet/color.rb +21 -14
- data/lib/axlsx/stylesheet/fill.rb +8 -5
- data/lib/axlsx/stylesheet/font.rb +22 -14
- data/lib/axlsx/stylesheet/gradient_fill.rb +20 -17
- data/lib/axlsx/stylesheet/gradient_stop.rb +10 -6
- data/lib/axlsx/stylesheet/num_fmt.rb +13 -6
- data/lib/axlsx/stylesheet/pattern_fill.rb +26 -10
- data/lib/axlsx/stylesheet/styles.rb +53 -41
- data/lib/axlsx/stylesheet/table_style.rb +10 -4
- data/lib/axlsx/stylesheet/table_style_element.rb +11 -7
- data/lib/axlsx/stylesheet/table_styles.rb +12 -8
- data/lib/axlsx/stylesheet/xf.rb +30 -25
- data/lib/axlsx/util/cbf.rb +69 -33
- data/lib/axlsx/util/constants.rb +39 -20
- data/lib/axlsx/util/ms_off_crypto.rb +10 -10
- data/lib/axlsx/util/parser.rb +1 -0
- data/lib/axlsx/util/simple_typed_list.rb +20 -10
- data/lib/axlsx/util/storage.rb +1 -1
- data/lib/axlsx/util/validators.rb +23 -7
- data/lib/axlsx/version.rb +5 -2
- data/lib/axlsx/workbook/shared_strings_table.rb +69 -0
- data/lib/axlsx/workbook/shared_strings_table.rb~ +69 -0
- data/lib/axlsx/workbook/workbook.rb +82 -26
- data/lib/axlsx/workbook/worksheet/cell.rb +172 -117
- data/lib/axlsx/workbook/worksheet/col.rb +114 -0
- data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
- data/lib/axlsx/workbook/worksheet/date_time_converter.rb +29 -0
- data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -0
- data/lib/axlsx/workbook/worksheet/row.rb +53 -21
- data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
- data/lib/axlsx/workbook/worksheet/table.rb +96 -0
- data/lib/axlsx/workbook/worksheet/table.rb~ +97 -0
- data/lib/axlsx/workbook/worksheet/worksheet.rb +270 -121
- data/lib/axlsx.rb +41 -14
- data/lib/schema/dc.xsd +5 -5
- data/lib/schema/dcmitype.xsd +5 -3
- data/lib/schema/dcterms.xsd +15 -15
- data/lib/schema/opc-coreProperties.xsd +6 -2
- data/lib/schema/xml.xsd +7 -8
- data/test/#benchmark.txt# +7 -0
- data/test/#tc_helper.rb# +3 -0
- data/test/benchmark.rb +81 -0
- data/test/benchmark.rb~ +0 -0
- data/test/benchmark.txt +6 -0
- data/test/benchmark.txt~ +6 -0
- data/test/content_type/tc_content_type.rb +31 -31
- data/test/content_type/tc_default.rb +9 -22
- data/test/content_type/tc_override.rb +8 -21
- data/test/doc_props/tc_app.rb +2 -8
- data/test/doc_props/tc_core.rb +6 -7
- data/test/drawing/tc_axis.rb +7 -3
- data/test/drawing/tc_bar_3D_chart.rb +6 -7
- data/test/drawing/tc_bar_series.rb +4 -5
- data/test/drawing/tc_cat_axis.rb +2 -3
- data/test/drawing/tc_cat_axis_data.rb +2 -3
- data/test/drawing/tc_chart.rb +13 -14
- data/test/drawing/tc_drawing.rb +7 -8
- data/test/drawing/tc_graphic_frame.rb +3 -4
- data/test/drawing/tc_hyperlink.rb +69 -0
- data/test/drawing/tc_line_3d_chart.rb +5 -6
- data/test/drawing/tc_line_series.rb +3 -4
- data/test/drawing/tc_marker.rb +3 -4
- data/test/drawing/tc_one_cell_anchor.rb +6 -7
- data/test/drawing/tc_pic.rb +13 -8
- data/test/drawing/tc_picture_locking.rb +2 -3
- data/test/drawing/tc_pie_3D_chart.rb +5 -6
- data/test/drawing/tc_pie_series.rb +4 -5
- data/test/drawing/tc_scaling.rb +3 -4
- data/test/drawing/tc_scatter_chart.rb +43 -0
- data/test/drawing/tc_scatter_series.rb +20 -0
- data/test/drawing/tc_ser_axis.rb +2 -3
- data/test/drawing/tc_series.rb +4 -5
- data/test/drawing/tc_series_title.rb +4 -5
- data/test/drawing/tc_title.rb +4 -5
- data/test/drawing/tc_two_cell_anchor.rb +4 -5
- data/test/drawing/tc_val_axis.rb +2 -3
- data/test/drawing/tc_val_axis_data.rb +2 -3
- data/test/drawing/tc_view_3D.rb +6 -7
- data/test/example.csv +1000 -0
- data/test/example.xlsx +0 -0
- data/test/example_streamed.xlsx +0 -0
- data/test/profile.rb +33 -0
- data/test/rels/tc_relationship.rb +10 -6
- data/test/rels/tc_relationships.rb +4 -5
- data/test/stylesheet/tc_border.rb +3 -4
- data/test/stylesheet/tc_border_pr.rb +3 -4
- data/test/stylesheet/tc_cell_alignment.rb +10 -6
- data/test/stylesheet/tc_cell_protection.rb +2 -3
- data/test/stylesheet/tc_cell_style.rb +2 -3
- data/test/stylesheet/tc_color.rb +2 -3
- data/test/stylesheet/tc_fill.rb +1 -2
- data/test/stylesheet/tc_font.rb +12 -5
- data/test/stylesheet/tc_gradient_fill.rb +1 -2
- data/test/stylesheet/tc_gradient_stop.rb +1 -2
- data/test/stylesheet/tc_num_fmt.rb +1 -2
- data/test/stylesheet/tc_pattern_fill.rb +3 -4
- data/test/stylesheet/tc_styles.rb +16 -6
- data/test/stylesheet/tc_table_style.rb +2 -3
- data/test/stylesheet/tc_table_style_element.rb +2 -3
- data/test/stylesheet/tc_table_styles.rb +3 -4
- data/test/stylesheet/tc_xf.rb +16 -17
- data/test/tc_axlsx.rb +39 -0
- data/test/tc_axlsx.rb~ +0 -0
- data/test/tc_helper.rb +3 -0
- data/test/tc_helper.rb~ +3 -0
- data/test/tc_package.rb +43 -9
- data/test/util/tc_simple_typed_list.rb +8 -9
- data/test/util/tc_validators.rb +7 -8
- data/test/workbook/tc_shared_strings_table.rb +38 -0
- data/test/workbook/tc_workbook.rb +32 -6
- data/test/workbook/worksheet/table/tc_table.rb +71 -0
- data/test/workbook/worksheet/table/tc_table.rb~ +72 -0
- data/test/workbook/worksheet/tc_cell.rb +70 -13
- data/test/workbook/worksheet/tc_col.rb +59 -0
- data/test/workbook/worksheet/tc_col.rb~ +10 -0
- data/test/workbook/worksheet/tc_date_time_converter.rb +126 -0
- data/test/workbook/worksheet/tc_page_margins.rb +97 -0
- data/test/workbook/worksheet/tc_row.rb +54 -4
- data/test/workbook/worksheet/tc_worksheet.rb +198 -31
- metadata +167 -111
- data/test/drawing/tc_line_series.tc~ +0 -34
- data/test/drawing/tc_picture_locking.rb~ +0 -77
|
@@ -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 [
|
|
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
|
|
46
|
-
|
|
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
|
-
|
|
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
|
|
File without changes
|
|
@@ -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
|