axlsx 1.0.15 → 1.1.1

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 (184) hide show
  1. data/.yardopts +1 -0
  2. data/CHANGELOG.md +58 -2
  3. data/README.md +425 -207
  4. data/Rakefile +6 -2
  5. data/examples/axlsx.xlsx +0 -0
  6. data/examples/example.csv +1000 -0
  7. data/examples/example.rb +266 -132
  8. data/examples/example.xlsx +0 -0
  9. data/examples/example_streamed.xlsx +0 -0
  10. data/examples/no-use_autowidth.xlsx +0 -0
  11. data/examples/real_example.rb +63 -0
  12. data/examples/sample.png +0 -0
  13. data/examples/shared_strings_example.xlsx +0 -0
  14. data/lib/axlsx/content_type/content_type.rb +13 -12
  15. data/lib/axlsx/content_type/default.rb +10 -6
  16. data/lib/axlsx/content_type/override.rb +13 -8
  17. data/lib/axlsx/doc_props/app.rb +38 -41
  18. data/lib/axlsx/doc_props/core.rb +13 -17
  19. data/lib/axlsx/drawing/axis.rb +39 -19
  20. data/lib/axlsx/drawing/bar_3D_chart.rb +34 -32
  21. data/lib/axlsx/drawing/bar_series.rb +14 -14
  22. data/lib/axlsx/drawing/cat_axis.rb +16 -14
  23. data/lib/axlsx/drawing/cat_axis_data.rb +17 -18
  24. data/lib/axlsx/drawing/chart.rb +40 -41
  25. data/lib/axlsx/drawing/drawing.rb +27 -12
  26. data/lib/axlsx/drawing/graphic_frame.rb +22 -21
  27. data/lib/axlsx/drawing/hlink_click.rb~ +0 -0
  28. data/lib/axlsx/drawing/hyperlink.rb +96 -0
  29. data/lib/axlsx/drawing/hyperlink.rb~ +64 -0
  30. data/lib/axlsx/drawing/line_3D_chart.rb +31 -28
  31. data/lib/axlsx/drawing/line_series.rb +12 -11
  32. data/lib/axlsx/drawing/marker.rb +11 -8
  33. data/lib/axlsx/drawing/named_axis_data.rb +36 -0
  34. data/lib/axlsx/drawing/one_cell_anchor.rb +18 -16
  35. data/lib/axlsx/drawing/pic.rb +47 -33
  36. data/lib/axlsx/drawing/picture_locking.rb +22 -18
  37. data/lib/axlsx/drawing/pie_3D_chart.rb +11 -8
  38. data/lib/axlsx/drawing/pie_series.rb +16 -12
  39. data/lib/axlsx/drawing/scaling.rb +11 -10
  40. data/lib/axlsx/drawing/scatter_chart.rb +69 -0
  41. data/lib/axlsx/drawing/scatter_series.rb +39 -0
  42. data/lib/axlsx/drawing/ser_axis.rb +11 -10
  43. data/lib/axlsx/drawing/series.rb +16 -15
  44. data/lib/axlsx/drawing/series_title.rb +15 -14
  45. data/lib/axlsx/drawing/title.rb +27 -26
  46. data/lib/axlsx/drawing/two_cell_anchor.rb +19 -20
  47. data/lib/axlsx/drawing/val_axis.rb +9 -7
  48. data/lib/axlsx/drawing/val_axis_data.rb +18 -17
  49. data/lib/axlsx/drawing/view_3D.rb +23 -20
  50. data/lib/axlsx/package.rb +117 -52
  51. data/lib/axlsx/rels/relationship.rb +27 -8
  52. data/lib/axlsx/rels/relationships.rb +9 -2
  53. data/lib/axlsx/stylesheet/border.rb +28 -23
  54. data/lib/axlsx/stylesheet/border_pr.rb +17 -15
  55. data/lib/axlsx/stylesheet/cell_alignment.rb +24 -21
  56. data/lib/axlsx/stylesheet/cell_protection.rb +11 -7
  57. data/lib/axlsx/stylesheet/cell_style.rb +9 -5
  58. data/lib/axlsx/stylesheet/color.rb +20 -14
  59. data/lib/axlsx/stylesheet/fill.rb +8 -5
  60. data/lib/axlsx/stylesheet/font.rb +22 -14
  61. data/lib/axlsx/stylesheet/gradient_fill.rb +20 -17
  62. data/lib/axlsx/stylesheet/gradient_stop.rb +10 -6
  63. data/lib/axlsx/stylesheet/num_fmt.rb +13 -6
  64. data/lib/axlsx/stylesheet/pattern_fill.rb +26 -10
  65. data/lib/axlsx/stylesheet/styles.rb +55 -41
  66. data/lib/axlsx/stylesheet/table_style.rb +10 -4
  67. data/lib/axlsx/stylesheet/table_style_element.rb +11 -7
  68. data/lib/axlsx/stylesheet/table_styles.rb +12 -8
  69. data/lib/axlsx/stylesheet/xf.rb +30 -25
  70. data/lib/axlsx/util/cbf.rb +69 -33
  71. data/lib/axlsx/util/constants.rb +39 -20
  72. data/lib/axlsx/util/doc/_index.html +84 -0
  73. data/lib/axlsx/util/doc/class_list.html +47 -0
  74. data/lib/axlsx/util/doc/css/common.css +1 -0
  75. data/lib/axlsx/util/doc/css/full_list.css +55 -0
  76. data/lib/axlsx/util/doc/css/style.css +322 -0
  77. data/lib/axlsx/util/doc/file_list.html +46 -0
  78. data/lib/axlsx/util/doc/frames.html +13 -0
  79. data/lib/axlsx/util/doc/index.html +84 -0
  80. data/lib/axlsx/util/doc/js/app.js +205 -0
  81. data/lib/axlsx/util/doc/js/full_list.js +173 -0
  82. data/lib/axlsx/util/doc/js/jquery.js +16 -0
  83. data/lib/axlsx/util/doc/method_list.html +46 -0
  84. data/lib/axlsx/util/doc/top-level-namespace.html +95 -0
  85. data/lib/axlsx/util/font_tables.rb~ +0 -0
  86. data/lib/axlsx/util/ms_off_crypto.rb +10 -10
  87. data/lib/axlsx/util/parser.rb +1 -0
  88. data/lib/axlsx/util/simple_typed_list.rb +20 -10
  89. data/lib/axlsx/util/storage.rb +1 -1
  90. data/lib/axlsx/util/validators.rb +23 -7
  91. data/lib/axlsx/version.rb +5 -2
  92. data/lib/axlsx/workbook/shared_strings_table.rb +68 -0
  93. data/lib/axlsx/workbook/shared_strings_table.rb~ +69 -0
  94. data/lib/axlsx/workbook/workbook.rb +82 -26
  95. data/lib/axlsx/workbook/worksheet/cell.rb +166 -130
  96. data/lib/axlsx/workbook/worksheet/col.rb +114 -0
  97. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  98. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +29 -0
  99. data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -0
  100. data/lib/axlsx/workbook/worksheet/row.rb +53 -21
  101. data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
  102. data/lib/axlsx/workbook/worksheet/table.rb +96 -0
  103. data/lib/axlsx/workbook/worksheet/table.rb~ +97 -0
  104. data/lib/axlsx/workbook/worksheet/worksheet.rb +275 -124
  105. data/lib/axlsx.rb +41 -14
  106. data/lib/schema/dc.xsd +5 -5
  107. data/lib/schema/dcmitype.xsd +5 -3
  108. data/lib/schema/dcterms.xsd +15 -15
  109. data/lib/schema/opc-coreProperties.xsd +6 -2
  110. data/lib/schema/xml.xsd +7 -8
  111. data/test/benchmark.rb +73 -0
  112. data/test/content_type/tc_content_type.rb +31 -31
  113. data/test/content_type/tc_default.rb +9 -22
  114. data/test/content_type/tc_override.rb +8 -21
  115. data/test/doc_props/tc_app.rb +2 -8
  116. data/test/doc_props/tc_core.rb +6 -7
  117. data/test/drawing/tc_axis.rb +7 -3
  118. data/test/drawing/tc_bar_3D_chart.rb +6 -7
  119. data/test/drawing/tc_bar_series.rb +4 -5
  120. data/test/drawing/tc_cat_axis.rb +2 -3
  121. data/test/drawing/tc_cat_axis_data.rb +2 -3
  122. data/test/drawing/tc_chart.rb +13 -14
  123. data/test/drawing/tc_drawing.rb +7 -8
  124. data/test/drawing/tc_graphic_frame.rb +3 -4
  125. data/test/drawing/tc_hyperlink.rb +69 -0
  126. data/test/drawing/tc_line_3d_chart.rb +5 -6
  127. data/test/drawing/tc_line_series.rb +3 -4
  128. data/test/drawing/tc_marker.rb +3 -4
  129. data/test/drawing/tc_one_cell_anchor.rb +6 -7
  130. data/test/drawing/tc_pic.rb +13 -8
  131. data/test/drawing/tc_picture_locking.rb +2 -3
  132. data/test/drawing/tc_pie_3D_chart.rb +5 -6
  133. data/test/drawing/tc_pie_series.rb +4 -5
  134. data/test/drawing/tc_scaling.rb +3 -4
  135. data/test/drawing/tc_scatter_chart.rb +43 -0
  136. data/test/drawing/tc_scatter_series.rb +20 -0
  137. data/test/drawing/tc_ser_axis.rb +2 -3
  138. data/test/drawing/tc_series.rb +4 -5
  139. data/test/drawing/tc_series_title.rb +4 -5
  140. data/test/drawing/tc_title.rb +4 -5
  141. data/test/drawing/tc_two_cell_anchor.rb +4 -5
  142. data/test/drawing/tc_val_axis.rb +2 -3
  143. data/test/drawing/tc_val_axis_data.rb +2 -3
  144. data/test/drawing/tc_view_3D.rb +6 -7
  145. data/test/profile.rb +34 -0
  146. data/test/rels/tc_relationship.rb +10 -6
  147. data/test/rels/tc_relationships.rb +4 -5
  148. data/test/stylesheet/tc_border.rb +3 -4
  149. data/test/stylesheet/tc_border_pr.rb +3 -4
  150. data/test/stylesheet/tc_cell_alignment.rb +10 -6
  151. data/test/stylesheet/tc_cell_protection.rb +2 -3
  152. data/test/stylesheet/tc_cell_style.rb +2 -3
  153. data/test/stylesheet/tc_color.rb +2 -3
  154. data/test/stylesheet/tc_fill.rb +1 -2
  155. data/test/stylesheet/tc_font.rb +12 -5
  156. data/test/stylesheet/tc_gradient_fill.rb +1 -2
  157. data/test/stylesheet/tc_gradient_stop.rb +1 -2
  158. data/test/stylesheet/tc_num_fmt.rb +1 -2
  159. data/test/stylesheet/tc_pattern_fill.rb +3 -4
  160. data/test/stylesheet/tc_styles.rb +31 -6
  161. data/test/stylesheet/tc_table_style.rb +2 -3
  162. data/test/stylesheet/tc_table_style_element.rb +2 -3
  163. data/test/stylesheet/tc_table_styles.rb +3 -4
  164. data/test/stylesheet/tc_xf.rb +16 -17
  165. data/test/tc_axlsx.rb +39 -0
  166. data/test/tc_helper.rb +3 -0
  167. data/test/tc_package.rb +43 -9
  168. data/test/util/tc_simple_typed_list.rb +8 -9
  169. data/test/util/tc_validators.rb +7 -8
  170. data/test/workbook/tc_shared_strings_table.rb +38 -0
  171. data/test/workbook/tc_workbook.rb +32 -6
  172. data/test/workbook/worksheet/table/tc_table.rb +71 -0
  173. data/test/workbook/worksheet/table/tc_table.rb~ +72 -0
  174. data/test/workbook/worksheet/tc_cell.rb +80 -13
  175. data/test/workbook/worksheet/tc_col.rb +59 -0
  176. data/test/workbook/worksheet/tc_col.rb~ +10 -0
  177. data/test/workbook/worksheet/tc_date_time_converter.rb +132 -0
  178. data/test/workbook/worksheet/tc_page_margins.rb +97 -0
  179. data/test/workbook/worksheet/tc_row.rb +54 -4
  180. data/test/workbook/worksheet/tc_worksheet.rb +205 -31
  181. metadata +78 -84
  182. data/examples/example.rb~ +0 -112
  183. data/test/drawing/tc_line_series.tc~ +0 -34
  184. data/test/drawing/tc_picture_locking.rb~ +0 -77
@@ -1,11 +1,11 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: UTF-8
2
2
  module Axlsx
3
3
  # A GradientFill defines the color and positioning for gradiant cell fill.
4
- # @see Open Office XML Part 1 §18.8.24
4
+ # @see Open Office XML Part 1 §18.8.24
5
5
  class GradientFill
6
6
 
7
7
  # The type of gradient.
8
- # @note
8
+ # @note
9
9
  # valid options are
10
10
  # :linear
11
11
  # :path
@@ -26,7 +26,7 @@ module Axlsx
26
26
 
27
27
  # Percentage format top
28
28
  # @return [Float]
29
- attr_reader :top
29
+ attr_reader :top
30
30
 
31
31
  # Percentage format bottom
32
32
  # @return [Float]
@@ -35,7 +35,7 @@ module Axlsx
35
35
  # Collection of stop objects
36
36
  # @return [SimpleTypedList]
37
37
  attr_reader :stop
38
-
38
+
39
39
  # Creates a new GradientFill object
40
40
  # @option options [Symbol] type
41
41
  # @option options [Float] degree
@@ -52,25 +52,28 @@ module Axlsx
52
52
  end
53
53
 
54
54
  # @see type
55
- def type=(v) Axlsx::validate_gradient_type v; @type = v end
55
+ def type=(v) Axlsx::validate_gradient_type v; @type = v end
56
56
  # @see degree
57
- def degree=(v) Axlsx::validate_float v; @degree = v end
57
+ def degree=(v) Axlsx::validate_float v; @degree = v end
58
58
  # @see left
59
- def left=(v) DataTypeValidator.validate "GradientFill.left", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @left = v end
59
+ def left=(v) DataTypeValidator.validate "GradientFill.left", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @left = v end
60
60
  # @see right
61
- def right=(v) DataTypeValidator.validate "GradientFill.right", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @right = v end
61
+ def right=(v) DataTypeValidator.validate "GradientFill.right", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @right = v end
62
62
  # @see top
63
- def top=(v) DataTypeValidator.validate "GradientFill.top", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @top = v end
63
+ def top=(v) DataTypeValidator.validate "GradientFill.top", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @top = v end
64
64
  # @see bottom
65
- def bottom=(v) DataTypeValidator.validate "GradientFill.bottom", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @bottom= v end
65
+ def bottom=(v) DataTypeValidator.validate "GradientFill.bottom", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @bottom= v end
66
66
 
67
- # Serializes the gradientFill
68
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
67
+ # Serializes the object
68
+ # @param [String] str
69
69
  # @return [String]
70
- def to_xml(xml)
71
- xml.gradientFill(self.instance_values.reject { |k,v| k.to_sym == :stop }) {
72
- @stop.each { |s| s.to_xml(xml) }
73
- }
70
+ def to_xml_string(str = '')
71
+ str << '<gradientFill'
72
+ h = self.instance_values.reject { |k,v| k.to_sym == :stop }
73
+ str << h.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
74
+ str << '>'
75
+ @stop.each { |s| s.to_xml_string(str) }
76
+ str << '</gradientFill>'
74
77
  end
75
78
  end
76
79
  end
@@ -1,7 +1,7 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: UTF-8
2
2
  module Axlsx
3
3
  # The GradientStop object represents a color point in a gradient.
4
- # @see Open Office XML Part 1 §18.8.24
4
+ # @see Open Office XML Part 1 §18.8.24
5
5
  class GradientStop
6
6
  # The color for this gradient stop
7
7
  # @return [Color]
@@ -23,11 +23,15 @@ module Axlsx
23
23
  # @see color
24
24
  def color=(v) DataTypeValidator.validate "GradientStop.color", Color, v; @color=v end
25
25
  # @see position
26
- def position=(v) DataTypeValidator.validate "GradientStop.position", Float, v, lambda { |arg| arg >= 0 && arg <= 1}; @position = v end
26
+ def position=(v) DataTypeValidator.validate "GradientStop.position", Float, v, lambda { |arg| arg >= 0 && arg <= 1}; @position = v end
27
27
 
28
- # Serializes the gradientStop
29
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
28
+ # Serializes the object
29
+ # @param [String] str
30
30
  # @return [String]
31
- def to_xml(xml) xml.stop(:position => self.position) {self.color.to_xml(xml)} end
31
+ def to_xml_string(str = '')
32
+ str << '<stop position="' << position.to_s << '">'
33
+ self.color.to_xml_string(str)
34
+ str << '</stop>'
35
+ end
32
36
  end
33
37
  end
@@ -1,10 +1,11 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
- # A NumFmt object defines an identifier and formatting code for data in cells.
3
+ # A NumFmt object defines an identifier and formatting code for data in cells.
3
4
  # @note The recommended way to manage styles is Styles#add_style
4
5
  class NumFmt
5
6
  # @return [Integer] An unsinged integer referencing a standard or custom number format.
6
7
  # @note
7
- # These are the known formats I can dig up. The constant NUM_FMT_PERCENT is 9, and uses the default % formatting. Axlsx also defines a few formats for date and time that are commonly used in asia as NUM_FMT_YYYYMMDD and NUM_FRM_YYYYMMDDHHMMSS.
8
+ # These are the known formats I can dig up. The constant NUM_FMT_PERCENT is 9, and uses the default % formatting. Axlsx also defines a few formats for date and time that are commonly used in asia as NUM_FMT_YYYYMMDD and NUM_FRM_YYYYMMDDHHMMSS.
8
9
  # 1 0
9
10
  # 2 0.00
10
11
  # 3 #,##0
@@ -39,7 +40,7 @@ module Axlsx
39
40
  # @see Axlsx
40
41
  attr_reader :numFmtId
41
42
 
42
- # @return [String] The formatting to use for this number format.
43
+ # @return [String] The formatting to use for this number format.
43
44
  # @see http://support.microsoft.com/kb/264372
44
45
  attr_reader :formatCode
45
46
  def initialize(options={})
@@ -55,9 +56,15 @@ module Axlsx
55
56
  # @see formatCode
56
57
  def formatCode=(v) Axlsx::validate_string v; @formatCode = v end
57
58
 
58
- # Creates a numFmt element applying the instance values of this object as attributes.
59
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
60
- def to_xml(xml) xml.numFmt(self.instance_values) end
59
+
60
+ # Serializes the object
61
+ # @param [String] str
62
+ # @return [String]
63
+ def to_xml_string(str = '')
64
+ str << '<numFmt '
65
+ str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
66
+ str << '/>'
67
+ end
61
68
 
62
69
  end
63
70
  end
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # A PatternFill is the pattern and solid fill styling for a cell.
3
4
  # @note The recommended way to manage styles is with Styles#add_style
@@ -6,15 +7,15 @@ module Axlsx
6
7
 
7
8
  # The color to use for the the background in solid fills.
8
9
  # @return [Color]
9
- attr_reader :fgColor
10
+ attr_reader :fgColor
10
11
 
11
12
  # The color to use for the background of the fill when the type is not solid.
12
13
  # @return [Color]
13
14
  attr_reader :bgColor
14
15
 
15
16
  # The pattern type to use
16
- # @note
17
- # patternType must be one of
17
+ # @note
18
+ # patternType must be one of
18
19
  # :none
19
20
  # :solid
20
21
  # :mediumGray
@@ -52,15 +53,30 @@ module Axlsx
52
53
  # @see bgColor
53
54
  def bgColor=(v) DataTypeValidator.validate "PatternFill.bgColor", Color, v; @bgColor=v end
54
55
  # @see patternType
55
- def patternType=(v) Axlsx::validate_pattern_type v; @patternType = v end
56
+ def patternType=(v) Axlsx::validate_pattern_type v; @patternType = v end
56
57
 
57
- # Serializes the pattern fill
58
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
58
+ # Serializes the object
59
+ # @param [String] str
59
60
  # @return [String]
60
- def to_xml(xml)
61
- xml.patternFill(:patternType => self.patternType) {
62
- self.instance_values.reject { |k,v| k.to_sym == :patternType }.each { |k,v| xml.send(k, v.instance_values) }
63
- }
61
+ def to_xml_string(str = '')
62
+ str << '<patternFill patternType="' << patternType.to_s << '">'
63
+ if fgColor.is_a?(Color)
64
+ str << "<fgColor "
65
+ fgColor.instance_values.each do |key, value|
66
+ str << key.to_s << '="' << value.to_s << '" '
67
+ end
68
+ str << "/>"
69
+ end
70
+
71
+ if bgColor.is_a?(Color)
72
+ str << "<bgColor "
73
+ bgColor.instance_values.each do |key, value|
74
+ str << key.to_s << '="' << value.to_s << '" '
75
+ end
76
+ str << "/>"
77
+ end
78
+ str << '</patternFill>'
64
79
  end
80
+
65
81
  end
66
82
  end
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: UTF-8
2
2
  module Axlsx
3
3
  require 'axlsx/stylesheet/border.rb'
4
4
  require 'axlsx/stylesheet/border_pr.rb'
@@ -120,25 +120,26 @@ module Axlsx
120
120
  end
121
121
 
122
122
  # Drastically simplifies style creation and management.
123
- # @return [Integer]
123
+ # @return [Integer]
124
124
  # @option options [String] fg_color The text color
125
125
  # @option options [Integer] sz The text size
126
126
  # @option options [Boolean] b Indicates if the text should be bold
127
127
  # @option options [Boolean] i Indicates if the text should be italicised
128
+ # @option options [Boolean] u Indicates if the text should be underlined
128
129
  # @option options [Boolean] strike Indicates if the text should be rendered with a strikethrough
129
130
  # @option options [Boolean] strike Indicates if the text should be rendered with a shadow
130
131
  # @option options [Integer] charset The character set to use.
131
132
  # @option options [Integer] family The font family to use.
132
- # @option options [String] name The name of the font to use
133
+ # @option options [String] font_name The name of the font to use
133
134
  # @option options [Integer] num_fmt The number format to apply
134
135
  # @option options [String] format_code The formatting to apply. If this is specified, num_fmt is ignored.
135
- # @option options [Integer] border The border style to use.
136
+ # @option options [Integer|Hash] border The border style to use.
136
137
  # @option options [String] bg_color The background color to apply to the cell
137
138
  # @option options [Boolean] hidden Indicates if the cell should be hidden
138
139
  # @option options [Boolean] locked Indicates if the cell should be locked
139
140
  # @option options [Hash] alignment A hash defining any of the attributes used in CellAlignment
140
141
  # @see CellAlignment
141
- #
142
+ #
142
143
  # @example You Got Style
143
144
  # require "rubygems" # if that is your preferred way to manage gems!
144
145
  # require "axlsx"
@@ -147,7 +148,7 @@ module Axlsx
147
148
  # ws = p.workbook.add_worksheet
148
149
  #
149
150
  # # black text on a white background at 14pt with thin borders!
150
- # title = ws.style.add_style(:bg_color => "FFFF0000", :fg_color=>"#FF000000", :sz=>14, :border=>Axlsx::STYLE_THIN_BORDER
151
+ # title = ws.style.add_style(:bg_color => "FFFF0000", :fg_color=>"#FF000000", :sz=>14, :border=> {:style => :thin, :color => "FFFF0000"}
151
152
  #
152
153
  # ws.add_row :values => ["Least Popular Pets"]
153
154
  # ws.add_row :values => ["", "Dry Skinned Reptiles", "Bald Cats", "Violent Parrots"], :style=>title
@@ -164,15 +165,15 @@ module Axlsx
164
165
  # ws = p.workbook.add_worksheet
165
166
  #
166
167
  # # define your styles
167
- # title = ws.style.add_style(:bg_color => "FFFF0000",
168
+ # title = ws.style.add_style(:bg_color => "FFFF0000",
168
169
  # :fg_color=>"#FF000000",
169
- # :border=>Axlsx::STYLE_THIN_BORDER,
170
+ # :border=>Axlsx::STYLE_THIN_BORDER,
170
171
  # :alignment=>{:horizontal => :center})
171
172
  #
172
173
  # date_time = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_YYYYMMDDHHMMSS,
173
174
  # :border=>Axlsx::STYLE_THIN_BORDER)
174
175
  #
175
- # percent = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_PERCENT,
176
+ # percent = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_PERCENT,
176
177
  # :border=>Axlsx::STYLE_THIN_BORDER)
177
178
  #
178
179
  # currency = ws.style.add_style(:format_code=>"¥#,##0;[Red]¥-#,##0",
@@ -189,7 +190,7 @@ module Axlsx
189
190
  # f = File.open('example_you_got_style.xlsx', 'w')
190
191
  # p.serialize(f)
191
192
  def add_style(options={})
192
-
193
+
193
194
  numFmtId = if options[:format_code]
194
195
  n = @numFmts.map{ |f| f.numFmtId }.max + 1
195
196
  numFmts << NumFmt.new(:numFmtId => n, :formatCode=> options[:format_code])
@@ -197,82 +198,95 @@ module Axlsx
197
198
  else
198
199
  options[:num_fmt] || 0
199
200
  end
200
-
201
+
201
202
  borderId = options[:border] || 0
203
+
204
+ if borderId.is_a?(Hash)
205
+ raise ArgumentError, "border hash definitions must include both style and color" unless borderId.keys.include?(:style) && borderId.keys.include?(:color)
206
+
207
+ s = borderId[:style]
208
+ c = borderId[:color]
209
+ edges = borderId[:edges] || [:left, :right, :top, :bottom]
210
+
211
+ border = Border.new
212
+ edges.each {|pr| border.prs << BorderPr.new(:name => pr, :style=>s, :color => Color.new(:rgb => c))}
213
+ borderId = self.borders << border
214
+ end
215
+
202
216
  raise ArgumentError, "Invalid borderId" unless borderId < borders.size
203
-
217
+
204
218
  fill = if options[:bg_color]
205
219
  color = Color.new(:rgb=>options[:bg_color])
206
220
  pattern = PatternFill.new(:patternType =>:solid, :fgColor=>color)
207
- fills << Fill.new(pattern)
221
+ fills << Fill.new(pattern)
208
222
  else
209
223
  0
210
224
  end
211
-
212
- fontId = if (options.values_at(:fg_color, :sz, :b, :i, :strike, :outline, :shadow, :charset, :family, :font_name).length)
225
+
226
+ fontId = if (options.values_at(:fg_color, :sz, :b, :i, :u, :strike, :outline, :shadow, :charset, :family, :font_name).length)
213
227
  font = Font.new()
214
- [:b, :i, :strike, :outline, :shadow, :charset, :family, :sz].each { |k| font.send("#{k}=", options[k]) unless options[k].nil? }
228
+ [:b, :i, :u, :strike, :outline, :shadow, :charset, :family, :sz].each { |k| font.send("#{k}=", options[k]) unless options[k].nil? }
215
229
  font.color = Color.new(:rgb => options[:fg_color]) unless options[:fg_color].nil?
216
230
  font.name = options[:font_name] unless options[:font_name].nil?
217
231
  fonts << font
218
232
  else
219
- 0
233
+ 0
220
234
  end
221
-
235
+
222
236
  applyProtection = (options[:hidden] || options[:locked]) ? 1 : 0
223
-
237
+
224
238
  xf = Xf.new(:fillId => fill, :fontId=>fontId, :applyFill=>1, :applyFont=>1, :numFmtId=>numFmtId, :borderId=>borderId, :applyProtection=>applyProtection)
225
239
 
226
240
  xf.applyNumberFormat = true if xf.numFmtId > 0
227
-
241
+ xf.applyBorder = true if borderId > 0
242
+
228
243
  if options[:alignment]
229
244
  xf.alignment = CellAlignment.new(options[:alignment])
245
+ xf.applyAlignment = true
230
246
  end
231
-
247
+
232
248
  if applyProtection
233
249
  xf.protection = CellProtection.new(options)
234
250
  end
235
-
251
+
236
252
  cellXfs << xf
237
253
  end
238
-
239
- # Serializes the styles document
254
+
255
+ # Serializes the object
256
+ # @param [String] str
240
257
  # @return [String]
241
- def to_xml()
242
- builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
243
- xml.styleSheet(:xmlns => XML_NS) {
244
- [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key|
245
- self.instance_values[key.to_s].to_xml(xml) unless self.instance_values[key.to_s].nil?
246
- end
247
- }
258
+ def to_xml_string(str = '')
259
+ str << '<styleSheet xmlns="' << XML_NS << '">'
260
+ [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key|
261
+ self.instance_values[key.to_s].to_xml_string(str) unless self.instance_values[key.to_s].nil?
248
262
  end
249
- builder.to_xml
263
+ str << '</styleSheet>'
250
264
  end
251
265
 
252
266
  private
253
- # Creates the default set of styles the exel requires to be valid as well as setting up the
267
+ # Creates the default set of styles the exel requires to be valid as well as setting up the
254
268
  # Axlsx::STYLE_THIN_BORDER
255
269
  def load_default_styles
256
- @numFmts = SimpleTypedList.new NumFmt
270
+ @numFmts = SimpleTypedList.new NumFmt, 'numFmts'
257
271
  @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDD, :formatCode=> "yyyy/mm/dd")
258
272
  @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDDHHMMSS, :formatCode=> "yyyy/mm/dd hh:mm:ss")
259
273
 
260
274
  @numFmts.lock
261
275
 
262
- @fonts = SimpleTypedList.new Font
276
+ @fonts = SimpleTypedList.new Font, 'fonts'
263
277
  @fonts << Font.new(:name => "Arial", :sz => 11, :family=>1)
264
278
  @fonts.lock
265
279
 
266
- @fills = SimpleTypedList.new Fill
280
+ @fills = SimpleTypedList.new Fill, 'fills'
267
281
  @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:none))
268
282
  @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:gray125))
269
283
  @fills.lock
270
284
 
271
- @borders = SimpleTypedList.new Border
285
+ @borders = SimpleTypedList.new Border, 'borders'
272
286
  @borders << Border.new
273
287
  black_border = Border.new
274
- [:left, :right, :top, :bottom].each do |item|
275
- black_border.prs << BorderPr.new(:name=>item, :style=>:thin, :color=>Color.new(:rgb=>"FF000000"))
288
+ [:left, :right, :top, :bottom].each do |item|
289
+ black_border.prs << BorderPr.new(:name=>item, :style=>:thin, :color=>Color.new(:rgb=>"FF000000"))
276
290
  end
277
291
  @borders << black_border
278
292
  @borders.lock
@@ -281,7 +295,7 @@ module Axlsx
281
295
  @cellStyleXfs << Xf.new(:borderId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
282
296
  @cellStyleXfs.lock
283
297
 
284
- @cellStyles = SimpleTypedList.new CellStyle
298
+ @cellStyles = SimpleTypedList.new CellStyle, 'cellStyles'
285
299
  @cellStyles << CellStyle.new(:name =>"Normal", :builtinId =>0, :xfId=>0)
286
300
  @cellStyles.lock
287
301
 
@@ -297,4 +311,4 @@ module Axlsx
297
311
  end
298
312
  end
299
313
  end
300
-
314
+
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # A single table style definition and is a collection for tableStyleElements
3
4
  # @note Table are not supported in this version and only the defaults required for a valid workbook are created.
@@ -35,13 +36,18 @@ module Axlsx
35
36
  # @see table
36
37
  def table=(v) Axlsx::validate_boolean v; @table=v end
37
38
 
38
- # Serializes the table style
39
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
39
+ # Serializes the object
40
+ # @param [String] str
40
41
  # @return [String]
41
- def to_xml(xml)
42
+ def to_xml_string(str = '')
42
43
  attr = self.instance_values.select { |k, v| [:name, :pivot, :table].include? k }
43
44
  attr[:count] = self.size
44
- xml.tableStyle(attr) { self.each { |table_style_el| table_style_el.to_xml(xml) } }
45
+ str << '<tableStyle '
46
+ str << attr.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
47
+ str << '>'
48
+ each { |table_style_el| table_style_el.to_xml_string(str) }
49
+ str << '</tableStyle>'
45
50
  end
51
+
46
52
  end
47
53
  end
@@ -1,6 +1,7 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
- # an element of style that belongs to a table style.
3
- # @note tables and table styles are not supported in this version. This class exists in preparation for that support.
3
+ # an element of style that belongs to a table style.
4
+ # @note tables and table styles are not supported in this version. This class exists in preparation for that support.
4
5
  class TableStyleElement
5
6
  # The type of style element. The following type are allowed
6
7
  # :wholeTable
@@ -38,7 +39,7 @@ module Axlsx
38
39
  # @return [Integer]
39
40
  attr_reader :size
40
41
 
41
- # The dxfId this style element points to
42
+ # The dxfId this style element points to
42
43
  # @return [Integer]
43
44
  attr_reader :dxfId
44
45
 
@@ -61,11 +62,14 @@ module Axlsx
61
62
  # @see dxfId
62
63
  def dxfId=(v) Axlsx::validate_unsigned_int v; @dxfId = v end
63
64
 
64
- # Serializes the table style element
65
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
65
+ # Serializes the object
66
+ # @param [String] str
66
67
  # @return [String]
67
- def to_xml(xml)
68
- xml.tableStyleElement self.instance_values
68
+ def to_xml_string(str = '')
69
+ str << '<tableStyleElement '
70
+ str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
71
+ str << '/>'
69
72
  end
73
+
70
74
  end
71
75
  end
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # TableStyles represents a collection of style definitions for table styles and pivot table styles.
3
4
  # @note Support for custom table styles does not exist in this version. Many of the classes required are defined in preparation for future release. Please do not attempt to add custom table styles.
@@ -10,13 +11,13 @@ module Axlsx
10
11
  # The default pivot table style. The default value is 'PivotStyleLight6'
11
12
  # @return [String]
12
13
  attr_reader :defaultPivotStyle
13
-
14
+
14
15
  # Creates a new TableStyles object that is a container for TableStyle objects
15
16
  # @option options [String] defaultTableStyle
16
17
  # @option options [String] defaultPivotStyle
17
18
  def initialize(options={})
18
19
  @defaultTableStyle = options[:defaultTableStyle] || "TableStyleMedium9"
19
- @defaultPivotStyle = options[:defaultPivotStyle] || "PivotStyleLight16"
20
+ @defaultPivotStyle = options[:defaultPivotStyle] || "PivotStyleLight16"
20
21
  super TableStyle
21
22
  end
22
23
  # @see defaultTableStyle
@@ -24,16 +25,19 @@ module Axlsx
24
25
  # @see defaultPivotStyle
25
26
  def defaultPivotStyle=(v) Axlsx::validate_string(v); @defaultPivotStyle = v; end
26
27
 
27
- # Serializes the table styles element
28
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
28
+ # Serializes the object
29
+ # @param [String] str
29
30
  # @return [String]
30
- def to_xml(xml)
31
+ def to_xml_string(str = '')
31
32
  attr = self.instance_values.reject {|k, v| ![:defaultTableStyle, :defaultPivotStyle].include?(k.to_sym) }
32
33
  attr[:count] = self.size
33
- xml.tableStyles(attr) {
34
- self.each { |table_style| table_style.to_xml(xml) }
35
- }
34
+ str << '<tableStyles '
35
+ str << attr.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
36
+ str << '>'
37
+ each { |table_style| table_style.to_xml_string(str) }
38
+ str << '</tableStyles>'
36
39
  end
40
+
37
41
  end
38
42
 
39
43
  end
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # The Xf class defines a formatting record for use in Styles. The recommended way to manage styles for your workbook is with Styles#add_style
3
4
  # @see Styles#add_style
@@ -21,7 +22,7 @@ module Axlsx
21
22
  # index (0 based) of the font to be used in this style
22
23
  # @return [Integer]
23
24
  attr_reader :fontId
24
-
25
+
25
26
  # index (0 based) of the fill to be used in this style
26
27
  # @return [Integer]
27
28
  attr_reader :fillId
@@ -49,7 +50,7 @@ module Axlsx
49
50
  # indicates if the fontId should be applied
50
51
  # @return [Boolean]
51
52
  attr_reader :applyFont
52
-
53
+
53
54
  # indicates if the fillId should be applied
54
55
  # @return [Boolean]
55
56
  attr_reader :applyFill
@@ -74,7 +75,7 @@ module Axlsx
74
75
  # @option options [Integer] xfId
75
76
  # @option options [Boolean] quotePrefix
76
77
  # @option options [Boolean] pivotButton
77
- # @option options [Boolean] applyNumberFormat
78
+ # @option options [Boolean] applyNumberFormat
78
79
  # @option options [Boolean] applyFont
79
80
  # @option options [Boolean] applyFill
80
81
  # @option options [Boolean] applyBorder
@@ -86,8 +87,8 @@ module Axlsx
86
87
  options.each do |o|
87
88
  self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
88
89
  end
89
- end
90
-
90
+ end
91
+
91
92
  # @see Xf#alignment
92
93
  def alignment=(v) DataTypeValidator.validate "Xf.alignment", CellAlignment, v; @alignment = v end
93
94
 
@@ -95,44 +96,48 @@ module Axlsx
95
96
  def protection=(v) DataTypeValidator.validate "Xf.protection", CellProtection, v; @protection = v end
96
97
 
97
98
  # @see numFmtId
98
- def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end
99
+ def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end
99
100
 
100
101
  # @see fontId
101
- def fontId=(v) Axlsx::validate_unsigned_int v; @fontId = v end
102
+ def fontId=(v) Axlsx::validate_unsigned_int v; @fontId = v end
102
103
  # @see fillId
103
- def fillId=(v) Axlsx::validate_unsigned_int v; @fillId = v end
104
+ def fillId=(v) Axlsx::validate_unsigned_int v; @fillId = v end
104
105
  # @see borderId
105
- def borderId=(v) Axlsx::validate_unsigned_int v; @borderId = v end
106
+ def borderId=(v) Axlsx::validate_unsigned_int v; @borderId = v end
106
107
  # @see xfId
107
- def xfId=(v) Axlsx::validate_unsigned_int v; @xfId = v end
108
+ def xfId=(v) Axlsx::validate_unsigned_int v; @xfId = v end
108
109
  # @see quotePrefix
109
- def quotePrefix=(v) Axlsx::validate_boolean v; @quotePrefix = v end
110
+ def quotePrefix=(v) Axlsx::validate_boolean v; @quotePrefix = v end
110
111
  # @see pivotButton
111
- def pivotButton=(v) Axlsx::validate_boolean v; @pivotButton = v end
112
+ def pivotButton=(v) Axlsx::validate_boolean v; @pivotButton = v end
112
113
  # @see applyNumberFormat
113
- def applyNumberFormat=(v) Axlsx::validate_boolean v; @applyNumberFormat = v end
114
+ def applyNumberFormat=(v) Axlsx::validate_boolean v; @applyNumberFormat = v end
114
115
  # @see applyFont
115
- def applyFont=(v) Axlsx::validate_boolean v; @applyFont = v end
116
+ def applyFont=(v) Axlsx::validate_boolean v; @applyFont = v end
116
117
  # @see applyFill
117
- def applyFill=(v) Axlsx::validate_boolean v; @applyFill = v end
118
+ def applyFill=(v) Axlsx::validate_boolean v; @applyFill = v end
118
119
 
119
120
  # @see applyBorder
120
- def applyBorder=(v) Axlsx::validate_boolean v; @applyBorder = v end
121
+ def applyBorder=(v) Axlsx::validate_boolean v; @applyBorder = v end
121
122
 
122
123
  # @see applyAlignment
123
- def applyAlignment=(v) Axlsx::validate_boolean v; @applyAlignment = v end
124
+ def applyAlignment=(v) Axlsx::validate_boolean v; @applyAlignment = v end
124
125
 
125
126
  # @see applyProtection
126
- def applyProtection=(v) Axlsx::validate_boolean v; @applyProtection = v end
127
+ def applyProtection=(v) Axlsx::validate_boolean v; @applyProtection = v end
127
128
 
128
- # Serializes the xf elemen
129
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
129
+ # Serializes the object
130
+ # @param [String] str
130
131
  # @return [String]
131
- def to_xml(xml)
132
- xml.xf(self.instance_values.reject { |k, v| [:alignment, :protection, :extList, :name].include? k.to_sym}) {
133
- alignment.to_xml(xml) if self.alignment
134
- protection.to_xml(xml) if self.protection
135
- }
132
+ def to_xml_string(str = '')
133
+ str << '<xf '
134
+ h = instance_values.reject { |k, v| [:alignment, :protection, :extList, :name].include? k.to_sym}
135
+ str << h.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
136
+ str << '>'
137
+ alignment.to_xml_string(str) if self.alignment
138
+ protection.to_xml_string(str) if self.protection
139
+ str << '</xf>'
136
140
  end
141
+
137
142
  end
138
143
  end