axlsx 1.2.0 → 2.0.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 (208) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +3 -2
  3. data/.yardopts_guide +19 -0
  4. data/CHANGELOG.md +64 -1
  5. data/README.md +105 -87
  6. data/Rakefile +0 -5
  7. data/examples/2010_comments.rb +17 -0
  8. data/examples/anchor_swapping.rb +28 -0
  9. data/examples/auto_filter.rb +16 -0
  10. data/examples/basic_charts.rb +4 -0
  11. data/examples/colored_links.rb +59 -0
  12. data/examples/conditional_formatting/example_conditional_formatting.rb +4 -2
  13. data/examples/example.rb +569 -258
  14. data/examples/ios_preview.rb +14 -0
  15. data/examples/page_setup.rb +11 -0
  16. data/examples/pivot_table.rb +39 -0
  17. data/examples/skydrive/real_example.rb +8 -8
  18. data/examples/styles.rb +66 -0
  19. data/examples/underline.rb +13 -0
  20. data/examples/wrap_text.rb +21 -0
  21. data/lib/axlsx/content_type/abstract_content_type.rb +32 -0
  22. data/lib/axlsx/content_type/content_type.rb +1 -1
  23. data/lib/axlsx/content_type/default.rb +6 -37
  24. data/lib/axlsx/content_type/override.rb +6 -38
  25. data/lib/axlsx/doc_props/app.rb +10 -7
  26. data/lib/axlsx/doc_props/core.rb +6 -1
  27. data/lib/axlsx/drawing/axes.rb +61 -0
  28. data/lib/axlsx/drawing/axis.rb +20 -17
  29. data/lib/axlsx/drawing/bar_3D_chart.rb +31 -22
  30. data/lib/axlsx/drawing/cat_axis.rb +2 -10
  31. data/lib/axlsx/drawing/chart.rb +33 -6
  32. data/lib/axlsx/drawing/d_lbls.rb +21 -31
  33. data/lib/axlsx/drawing/drawing.rb +11 -17
  34. data/lib/axlsx/drawing/graphic_frame.rb +3 -3
  35. data/lib/axlsx/drawing/hyperlink.rb +42 -36
  36. data/lib/axlsx/drawing/line_3D_chart.rb +28 -70
  37. data/lib/axlsx/drawing/line_chart.rb +99 -0
  38. data/lib/axlsx/drawing/line_series.rb +20 -2
  39. data/lib/axlsx/drawing/marker.rb +38 -18
  40. data/lib/axlsx/drawing/num_data.rb +6 -6
  41. data/lib/axlsx/drawing/num_data_source.rb +17 -16
  42. data/lib/axlsx/drawing/one_cell_anchor.rb +28 -21
  43. data/lib/axlsx/drawing/pic.rb +43 -45
  44. data/lib/axlsx/drawing/picture_locking.rb +12 -44
  45. data/lib/axlsx/drawing/pie_3D_chart.rb +2 -1
  46. data/lib/axlsx/drawing/scaling.rb +13 -13
  47. data/lib/axlsx/drawing/scatter_chart.rb +32 -21
  48. data/lib/axlsx/drawing/ser_axis.rb +15 -16
  49. data/lib/axlsx/drawing/series.rb +3 -6
  50. data/lib/axlsx/drawing/str_data.rb +3 -3
  51. data/lib/axlsx/drawing/str_val.rb +7 -8
  52. data/lib/axlsx/drawing/two_cell_anchor.rb +7 -27
  53. data/lib/axlsx/drawing/val_axis.rb +14 -13
  54. data/lib/axlsx/drawing/view_3D.rb +52 -38
  55. data/lib/axlsx/drawing/vml_shape.rb +20 -79
  56. data/lib/axlsx/package.rb +58 -28
  57. data/lib/axlsx/rels/relationship.rb +75 -9
  58. data/lib/axlsx/rels/relationships.rb +8 -1
  59. data/lib/axlsx/stylesheet/border.rb +29 -20
  60. data/lib/axlsx/stylesheet/border_pr.rb +5 -4
  61. data/lib/axlsx/stylesheet/cell_alignment.rb +55 -29
  62. data/lib/axlsx/stylesheet/cell_protection.rb +7 -4
  63. data/lib/axlsx/stylesheet/cell_style.rb +19 -14
  64. data/lib/axlsx/stylesheet/color.rb +20 -17
  65. data/lib/axlsx/stylesheet/dxf.rb +4 -4
  66. data/lib/axlsx/stylesheet/font.rb +22 -22
  67. data/lib/axlsx/stylesheet/gradient_fill.rb +45 -21
  68. data/lib/axlsx/stylesheet/num_fmt.rb +23 -14
  69. data/lib/axlsx/stylesheet/pattern_fill.rb +12 -21
  70. data/lib/axlsx/stylesheet/styles.rb +37 -22
  71. data/lib/axlsx/stylesheet/table_style.rb +17 -16
  72. data/lib/axlsx/stylesheet/table_style_element.rb +15 -11
  73. data/lib/axlsx/stylesheet/table_styles.rb +14 -11
  74. data/lib/axlsx/stylesheet/xf.rb +28 -26
  75. data/lib/axlsx/util/accessors.rb +64 -0
  76. data/lib/axlsx/util/constants.rb +127 -6
  77. data/lib/axlsx/util/options_parser.rb +15 -0
  78. data/lib/axlsx/util/serialized_attributes.rb +79 -0
  79. data/lib/axlsx/util/simple_typed_list.rb +50 -15
  80. data/lib/axlsx/util/validators.rb +51 -16
  81. data/lib/axlsx/version.rb +2 -2
  82. data/lib/axlsx/workbook/defined_name.rb +14 -59
  83. data/lib/axlsx/workbook/shared_strings_table.rb +13 -4
  84. data/lib/axlsx/workbook/workbook.rb +85 -6
  85. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +77 -0
  86. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +94 -0
  87. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +246 -0
  88. data/lib/axlsx/workbook/worksheet/break.rb +37 -0
  89. data/lib/axlsx/workbook/worksheet/cell.rb +109 -107
  90. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +144 -0
  91. data/lib/axlsx/workbook/worksheet/cfvo.rb +15 -15
  92. data/lib/axlsx/workbook/worksheet/cfvos.rb +15 -0
  93. data/lib/axlsx/workbook/worksheet/col.rb +43 -29
  94. data/lib/axlsx/workbook/worksheet/col_breaks.rb +35 -0
  95. data/lib/axlsx/workbook/worksheet/color_scale.rb +62 -28
  96. data/lib/axlsx/workbook/worksheet/comment.rb +28 -37
  97. data/lib/axlsx/workbook/worksheet/comments.rb +3 -3
  98. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +11 -11
  99. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +31 -27
  100. data/lib/axlsx/workbook/worksheet/data_bar.rb +75 -41
  101. data/lib/axlsx/workbook/worksheet/data_validation.rb +61 -62
  102. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +6 -5
  103. data/lib/axlsx/workbook/worksheet/dimension.rb +0 -1
  104. data/lib/axlsx/workbook/worksheet/header_footer.rb +54 -0
  105. data/lib/axlsx/workbook/worksheet/icon_set.rb +20 -20
  106. data/lib/axlsx/workbook/worksheet/page_margins.rb +21 -19
  107. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +44 -0
  108. data/lib/axlsx/workbook/worksheet/page_setup.rb +152 -23
  109. data/lib/axlsx/workbook/worksheet/pane.rb +48 -51
  110. data/lib/axlsx/workbook/worksheet/pivot_table.rb +273 -0
  111. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +66 -0
  112. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +24 -0
  113. data/lib/axlsx/workbook/worksheet/print_options.rb +8 -30
  114. data/lib/axlsx/workbook/worksheet/protected_range.rb +16 -13
  115. data/lib/axlsx/workbook/worksheet/row.rb +59 -83
  116. data/lib/axlsx/workbook/worksheet/row_breaks.rb +33 -0
  117. data/lib/axlsx/workbook/worksheet/selection.rb +30 -38
  118. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +29 -0
  119. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +60 -0
  120. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +49 -4
  121. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +51 -155
  122. data/lib/axlsx/workbook/worksheet/sheet_view.rb +68 -234
  123. data/lib/axlsx/workbook/worksheet/table.rb +25 -19
  124. data/lib/axlsx/workbook/worksheet/table_style_info.rb +51 -0
  125. data/lib/axlsx/workbook/worksheet/tables.rb +1 -1
  126. data/lib/axlsx/workbook/worksheet/worksheet.rb +174 -35
  127. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +6 -5
  128. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +4 -10
  129. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +74 -0
  130. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +38 -0
  131. data/lib/axlsx.rb +55 -5
  132. data/lib/schema/sml.xsd +4 -0
  133. data/test/axlsx.qcachegrind +2226 -0
  134. data/test/benchmark.rb +2 -3
  135. data/test/content_type/tc_default.rb +0 -11
  136. data/test/content_type/tc_override.rb +0 -13
  137. data/test/doc_props/tc_core.rb +7 -0
  138. data/test/drawing/tc_axes.rb +8 -0
  139. data/test/drawing/tc_axis.rb +15 -17
  140. data/test/drawing/tc_bar_3D_chart.rb +6 -0
  141. data/test/drawing/tc_cat_axis.rb +9 -9
  142. data/test/drawing/tc_chart.rb +20 -0
  143. data/test/drawing/tc_d_lbls.rb +14 -4
  144. data/test/drawing/tc_drawing.rb +0 -5
  145. data/test/drawing/tc_graphic_frame.rb +5 -3
  146. data/test/drawing/tc_hyperlink.rb +0 -4
  147. data/test/drawing/tc_line_chart.rb +39 -0
  148. data/test/drawing/tc_line_series.rb +8 -2
  149. data/test/drawing/tc_pic.rb +14 -3
  150. data/test/drawing/tc_ser_axis.rb +13 -12
  151. data/test/drawing/tc_two_cell_anchor.rb +3 -3
  152. data/test/drawing/tc_val_axis.rb +6 -6
  153. data/test/drawing/tc_vml_shape.rb +9 -3
  154. data/test/profile.rb +11 -21
  155. data/test/rels/tc_relationship.rb +32 -9
  156. data/test/rels/tc_relationships.rb +12 -1
  157. data/test/stylesheet/tc_color.rb +6 -0
  158. data/test/stylesheet/tc_styles.rb +15 -2
  159. data/test/tc_axlsx.rb +35 -2
  160. data/test/tc_helper.rb +7 -1
  161. data/test/tc_package.rb +44 -5
  162. data/test/util/tc_serialized_attributes.rb +19 -0
  163. data/test/util/tc_validators.rb +26 -1
  164. data/test/workbook/tc_defined_name.rb +2 -2
  165. data/test/workbook/tc_shared_strings_table.rb +6 -0
  166. data/test/workbook/tc_workbook.rb +38 -1
  167. data/test/workbook/worksheet/auto_filter/tc_auto_filter.rb +38 -0
  168. data/test/workbook/worksheet/auto_filter/tc_filter_column.rb +76 -0
  169. data/test/workbook/worksheet/auto_filter/tc_filters.rb +50 -0
  170. data/test/workbook/worksheet/tc_break.rb +49 -0
  171. data/test/workbook/worksheet/tc_cell.rb +40 -6
  172. data/test/workbook/worksheet/tc_col.rb +11 -1
  173. data/test/workbook/worksheet/tc_color_scale.rb +31 -2
  174. data/test/workbook/worksheet/tc_comment.rb +21 -6
  175. data/test/workbook/worksheet/tc_comments.rb +2 -2
  176. data/test/workbook/worksheet/tc_conditional_formatting.rb +7 -0
  177. data/test/workbook/worksheet/tc_data_bar.rb +7 -0
  178. data/test/workbook/worksheet/tc_date_time_converter.rb +3 -11
  179. data/test/workbook/worksheet/tc_header_footer.rb +151 -0
  180. data/test/workbook/worksheet/tc_page_set_up_pr.rb +15 -0
  181. data/test/workbook/worksheet/tc_page_setup.rb +6 -1
  182. data/test/workbook/worksheet/tc_pane.rb +5 -45
  183. data/test/workbook/worksheet/tc_pivot_table.rb +120 -0
  184. data/test/workbook/worksheet/tc_pivot_table_cache_definition.rb +54 -0
  185. data/test/workbook/worksheet/tc_row.rb +4 -4
  186. data/test/workbook/worksheet/tc_selection.rb +9 -48
  187. data/test/workbook/worksheet/tc_sheet_calc_pr.rb +18 -0
  188. data/test/workbook/worksheet/tc_sheet_format_pr.rb +88 -0
  189. data/test/workbook/worksheet/tc_sheet_pr.rb +27 -0
  190. data/test/workbook/worksheet/tc_sheet_view.rb +39 -39
  191. data/test/workbook/worksheet/{table/tc_table.rb → tc_table.rb} +8 -3
  192. data/test/workbook/worksheet/tc_table_style_info.rb +53 -0
  193. data/test/workbook/worksheet/tc_worksheet.rb +131 -16
  194. data/test/workbook/worksheet/tc_worksheet_hyperlink.rb +55 -0
  195. metadata +97 -64
  196. data/examples/example.xlsx +0 -0
  197. data/examples/example_streamed.xlsx +0 -0
  198. data/examples/extractive.pdf +0 -0
  199. data/examples/image1.gif +0 -0
  200. data/examples/image1.jpg +0 -0
  201. data/examples/image1.png +0 -0
  202. data/examples/no-use_autowidth.xlsx +0 -0
  203. data/examples/sample.png +0 -0
  204. data/examples/scraping_html.rb +0 -91
  205. data/examples/shared_strings_example.xlsx +0 -0
  206. data/examples/sheet_view.rb +0 -34
  207. data/examples/two_cell_anchor_image.rb +0 -11
  208. data/lib/axlsx/workbook/worksheet/auto_filter.rb +0 -34
@@ -2,6 +2,21 @@
2
2
  module Axlsx
3
3
  # The color class represents a color used for borders, fills an fonts
4
4
  class Color
5
+
6
+ include Axlsx::OptionsParser
7
+ include Axlsx::SerializedAttributes
8
+
9
+ # Creates a new Color object
10
+ # @option options [Boolean] auto
11
+ # @option options [String] rgb
12
+ # @option options [Float] tint
13
+ def initialize(options={})
14
+ @rgb = "FF000000"
15
+ parse_options options
16
+ end
17
+
18
+ serializable_attributes :auto, :rgb, :tint
19
+
5
20
  # Determines if the color is system color dependant
6
21
  # @return [Boolean]
7
22
  attr_reader :auto
@@ -31,22 +46,12 @@ module Axlsx
31
46
  # @return [Float]
32
47
  attr_reader :tint
33
48
 
34
- # Creates a new Color object
35
- # @option options [Boolean] auto
36
- # @option options [String] rgb
37
- # @option options [Float] tint
38
- def initialize(options={})
39
- @rgb = "FF000000"
40
- options.each do |o|
41
- self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
42
- end
43
- end
44
- # @see auto
49
+ # @see auto
45
50
  def auto=(v) Axlsx::validate_boolean v; @auto = v end
46
51
  # @see color
47
52
  def rgb=(v)
48
53
  Axlsx::validate_string(v)
49
- v.upcase!
54
+ v = v.upcase
50
55
  v = v * 3 if v.size == 2
51
56
  v = v.rjust(8, 'FF')
52
57
  raise ArgumentError, "Invalid color rgb value: #{v}." unless v.match(/[0-9A-F]{8}/)
@@ -64,11 +69,9 @@ module Axlsx
64
69
  # Serializes the object
65
70
  # @param [String] str
66
71
  # @return [String]
67
- def to_xml_string(str = '')
68
- str << "<color "
69
- self.instance_values.each do |key, value|
70
- str << key.to_s << '="' << value.to_s << '" '
71
- end
72
+ def to_xml_string(str = '', tag_name = 'color')
73
+ str << "<" << tag_name << " "
74
+ serialized_attributes str
72
75
  str << "/>"
73
76
  end
74
77
  end
@@ -3,6 +3,9 @@ module Axlsx
3
3
  # The Dxf class defines an incremental formatting record for use in Styles. The recommended way to manage styles for your workbook is with Styles#add_style
4
4
  # @see Styles#add_style
5
5
  class Dxf
6
+
7
+ include Axlsx::OptionsParser
8
+
6
9
  # The order in which the child elements is put in the XML seems to
7
10
  # be important for Excel
8
11
  CHILD_ELEMENTS = [:font, :numFmt, :fill, :alignment, :border, :protection]
@@ -42,10 +45,7 @@ module Axlsx
42
45
  # @option options [CellAlignment] alignment
43
46
  # @option options [CellProtection] protection
44
47
  def initialize(options={})
45
- options.each do |o|
46
- next if o[1].nil?
47
- self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
48
- end
48
+ parse_options options
49
49
  end
50
50
 
51
51
  # @see Dxf#alignment
@@ -4,6 +4,27 @@ module Axlsx
4
4
  # @note The recommended way to manage fonts, and other styles is Styles#add_style
5
5
  # @see Styles#add_style
6
6
  class Font
7
+ include Axlsx::OptionsParser
8
+
9
+ # Creates a new Font
10
+ # @option options [String] name
11
+ # @option options [Integer] charset
12
+ # @option options [Integer] family
13
+ # @option options [Integer] family
14
+ # @option options [Boolean] b
15
+ # @option options [Boolean] i
16
+ # @option options [Boolean] u
17
+ # @option options [Boolean] strike
18
+ # @option options [Boolean] outline
19
+ # @option options [Boolean] shadow
20
+ # @option options [Boolean] condense
21
+ # @option options [Boolean] extend
22
+ # @option options [Color] color
23
+ # @option options [Integer] sz
24
+ def initialize(options={})
25
+ parse_options options
26
+ end
27
+
7
28
  # The name of the font
8
29
  # @return [String]
9
30
  attr_reader :name
@@ -86,28 +107,7 @@ module Axlsx
86
107
  # @return [Integer]
87
108
  attr_reader :sz
88
109
 
89
- # Creates a new Font
90
- # @option options [String] name
91
- # @option options [Integer] charset
92
- # @option options [Integer] family
93
- # @option options [Integer] family
94
- # @option options [Boolean] b
95
- # @option options [Boolean] i
96
- # @option options [Boolean] u
97
- # @option options [Boolean] strike
98
- # @option options [Boolean] outline
99
- # @option options [Boolean] shadow
100
- # @option options [Boolean] condense
101
- # @option options [Boolean] extend
102
- # @option options [Color] color
103
- # @option options [Integer] sz
104
- def initialize(options={})
105
- options.each do |o|
106
- next if o[1].nil?
107
- self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
108
- end
109
- end
110
- # @see name
110
+ # @see name
111
111
  def name=(v) Axlsx::validate_string v; @name = v end
112
112
  # @see charset
113
113
  def charset=(v) Axlsx::validate_unsigned_int v; @charset = v end
@@ -4,6 +4,24 @@ module Axlsx
4
4
  # @see Open Office XML Part 1 §18.8.24
5
5
  class GradientFill
6
6
 
7
+ include Axlsx::OptionsParser
8
+ include Axlsx::SerializedAttributes
9
+
10
+ # Creates a new GradientFill object
11
+ # @option options [Symbol] type
12
+ # @option options [Float] degree
13
+ # @option options [Float] left
14
+ # @option options [Float] right
15
+ # @option options [Float] top
16
+ # @option options [Float] bottom
17
+ def initialize(options={})
18
+ options[:type] ||= :linear
19
+ parse_options options
20
+ @stop = SimpleTypedList.new GradientStop
21
+ end
22
+
23
+ serializable_attributes :type, :degree, :left, :right, :top, :bottom
24
+
7
25
  # The type of gradient.
8
26
  # @note
9
27
  # valid options are
@@ -36,41 +54,47 @@ module Axlsx
36
54
  # @return [SimpleTypedList]
37
55
  attr_reader :stop
38
56
 
39
- # Creates a new GradientFill object
40
- # @option options [Symbol] type
41
- # @option options [Float] degree
42
- # @option options [Float] left
43
- # @option options [Float] right
44
- # @option options [Float] top
45
- # @option options [Float] bottom
46
- def initialize(options={})
47
- options[:type] ||= :linear
48
- options.each do |o|
49
- self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
50
- end
51
- @stop = SimpleTypedList.new GradientStop
52
- end
53
-
54
57
  # @see type
55
58
  def type=(v) Axlsx::validate_gradient_type v; @type = v end
59
+
56
60
  # @see degree
57
61
  def degree=(v) Axlsx::validate_float v; @degree = v end
62
+
58
63
  # @see left
59
- def left=(v) DataTypeValidator.validate "GradientFill.left", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @left = v end
64
+ def left=(v)
65
+ validate_format_percentage "GradientFill.left", v
66
+ @left = v
67
+ end
68
+
60
69
  # @see right
61
- def right=(v) DataTypeValidator.validate "GradientFill.right", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @right = v end
70
+ def right=(v)
71
+ validate_format_percentage "GradientFill.right", v
72
+ @right = v
73
+ end
74
+
62
75
  # @see top
63
- def top=(v) DataTypeValidator.validate "GradientFill.top", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @top = v end
76
+ def top=(v)
77
+ validate_format_percentage "GradientFill.top", v
78
+ @top = v
79
+ end
80
+
64
81
  # @see bottom
65
- def bottom=(v) DataTypeValidator.validate "GradientFill.bottom", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @bottom= v end
82
+ def bottom=(v)
83
+ validate_format_percentage "GradientFill.bottom", v
84
+ @bottom = v
85
+ end
86
+
87
+ # validates that the value provided is between 0.0 and 1.0
88
+ def validate_format_percentage(name, value)
89
+ DataTypeValidator.validate name, Float, value, lambda { |arg| arg >= 0.0 && arg <= 1.0}
90
+ end
66
91
 
67
92
  # Serializes the object
68
93
  # @param [String] str
69
94
  # @return [String]
70
95
  def to_xml_string(str = '')
71
96
  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(' ')
97
+ serialized_attributes str
74
98
  str << '>'
75
99
  @stop.each { |s| s.to_xml_string(str) }
76
100
  str << '</gradientFill>'
@@ -3,7 +3,27 @@ module Axlsx
3
3
  # A NumFmt object defines an identifier and formatting code for data in cells.
4
4
  # @note The recommended way to manage styles is Styles#add_style
5
5
  class NumFmt
6
- # @return [Integer] An unsinged integer referencing a standard or custom number format.
6
+
7
+ include Axlsx::OptionsParser
8
+ include Axlsx::SerializedAttributes
9
+
10
+ # Creates a new NumFmt object
11
+ # @param [Hash] options Options for the number format object
12
+ # @option [Integer] numFmtId The predefined format id or new format id for this format
13
+ # @option [String] formatCode The format code for this number format
14
+ def initialize(options={})
15
+ @numFmtId = 0
16
+ @formatCode = ""
17
+ parse_options options
18
+ end
19
+
20
+ serializable_attributes :formatCode, :numFmtId
21
+
22
+ # @return [String] The formatting to use for this number format.
23
+ # @see http://support.microsoft.com/kb/264372
24
+ attr_reader :formatCode
25
+
26
+ # @return [Integer] An unsigned integer referencing a standard or custom number format.
7
27
  # @note
8
28
  # 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.
9
29
  # 1 0
@@ -40,29 +60,18 @@ module Axlsx
40
60
  # @see Axlsx
41
61
  attr_reader :numFmtId
42
62
 
43
- # @return [String] The formatting to use for this number format.
44
- # @see http://support.microsoft.com/kb/264372
45
- attr_reader :formatCode
46
- def initialize(options={})
47
- @numFmtId = 0
48
- @formatCode = ""
49
- options.each do |o|
50
- self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
51
- end
52
- end
53
-
54
63
  # @see numFmtId
55
64
  def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end
65
+
56
66
  # @see formatCode
57
67
  def formatCode=(v) Axlsx::validate_string v; @formatCode = v end
58
68
 
59
-
60
69
  # Serializes the object
61
70
  # @param [String] str
62
71
  # @return [String]
63
72
  def to_xml_string(str = '')
64
73
  str << '<numFmt '
65
- str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
74
+ serialized_attributes str
66
75
  str << '/>'
67
76
  end
68
77
 
@@ -5,6 +5,16 @@ module Axlsx
5
5
  # @see Style#add_style
6
6
  class PatternFill
7
7
 
8
+ include Axlsx::OptionsParser
9
+ # Creates a new PatternFill Object
10
+ # @option options [Symbol] patternType
11
+ # @option options [Color] fgColor
12
+ # @option options [Color] bgColor
13
+ def initialize(options={})
14
+ @patternType = :none
15
+ parse_options options
16
+ end
17
+
8
18
  # The color to use for the the background in solid fills.
9
19
  # @return [Color]
10
20
  attr_reader :fgColor
@@ -38,16 +48,6 @@ module Axlsx
38
48
  # @see Office Open XML Part 1 18.18.55
39
49
  attr_reader :patternType
40
50
 
41
- # Creates a new PatternFill Object
42
- # @option options [Symbol] patternType
43
- # @option options [Color] fgColor
44
- # @option options [Color] bgColor
45
- def initialize(options={})
46
- @patternType = :none
47
- options.each do |o|
48
- self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
49
- end
50
- end
51
51
  # @see fgColor
52
52
  def fgColor=(v) DataTypeValidator.validate "PatternFill.fgColor", Color, v; @fgColor=v end
53
53
  # @see bgColor
@@ -61,22 +61,13 @@ module Axlsx
61
61
  def to_xml_string(str = '')
62
62
  str << '<patternFill patternType="' << patternType.to_s << '">'
63
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 << "/>"
64
+ fgColor.to_xml_string str, "fgColor"
69
65
  end
70
66
 
71
67
  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 << "/>"
68
+ bgColor.to_xml_string str, "bgColor"
77
69
  end
78
70
  str << '</patternFill>'
79
71
  end
80
-
81
72
  end
82
73
  end
@@ -135,6 +135,7 @@ module Axlsx
135
135
  # @option options [Integer] num_fmt The number format to apply
136
136
  # @option options [String] format_code The formatting to apply.
137
137
  # @option options [Integer|Hash] border The border style to use.
138
+ # borders support style, color and edges options @see parse_border_options
138
139
  # @option options [String] bg_color The background color to apply to the cell
139
140
  # @option options [Boolean] hidden Indicates if the cell should be hidden
140
141
  # @option options [Boolean] locked Indicates if the cell should be locked
@@ -152,9 +153,9 @@ module Axlsx
152
153
  # # black text on a white background at 14pt with thin borders!
153
154
  # title = ws.style.add_style(:bg_color => "FFFF0000", :fg_color=>"#FF000000", :sz=>14, :border=> {:style => :thin, :color => "FFFF0000"}
154
155
  #
155
- # ws.add_row :values => ["Least Popular Pets"]
156
- # ws.add_row :values => ["", "Dry Skinned Reptiles", "Bald Cats", "Violent Parrots"], :style=>title
157
- # ws.add_row :values => ["Votes", 6, 4, 1], :style=>Axlsx::STYLE_THIN_BORDER
156
+ # ws.add_row ["Least Popular Pets"]
157
+ # ws.add_row ["", "Dry Skinned Reptiles", "Bald Cats", "Violent Parrots"], :style=>title
158
+ # ws.add_row ["Votes", 6, 4, 1], :style=>Axlsx::STYLE_THIN_BORDER
158
159
  # f = File.open('example_you_got_style.xlsx', 'w')
159
160
  # p.serialize(f)
160
161
  #
@@ -182,13 +183,13 @@ module Axlsx
182
183
  # :border=>Axlsx::STYLE_THIN_BORDER)
183
184
  #
184
185
  # # build your rows
185
- # ws.add_row :values => ["Genreated At:", Time.now], :styles=>[nil, date_time]
186
- # ws.add_row :values => ["Previous Year Quarterly Profits (JPY)"], :style=>title
187
- # ws.add_row :values => ["Quarter", "Profit", "% of Total"], :style=>title
188
- # ws.add_row :values => ["Q1", 4000, 40], :style=>[title, currency, percent]
189
- # ws.add_row :values => ["Q2", 3000, 30], :style=>[title, currency, percent]
190
- # ws.add_row :values => ["Q3", 1000, 10], :style=>[title, currency, percent]
191
- # ws.add_row :values => ["Q4", 2000, 20], :style=>[title, currency, percent]
186
+ # ws.add_row ["Generated At:", Time.now], :styles=>[nil, date_time]
187
+ # ws.add_row ["Previous Year Quarterly Profits (JPY)"], :style=>title
188
+ # ws.add_row ["Quarter", "Profit", "% of Total"], :style=>title
189
+ # ws.add_row ["Q1", 4000, 40], :style=>[title, currency, percent]
190
+ # ws.add_row ["Q2", 3000, 30], :style=>[title, currency, percent]
191
+ # ws.add_row ["Q3", 1000, 10], :style=>[title, currency, percent]
192
+ # ws.add_row ["Q4", 2000, 20], :style=>[title, currency, percent]
192
193
  # f = File.open('example_you_got_style.xlsx', 'w')
193
194
  # p.serialize(f)
194
195
  #
@@ -206,13 +207,13 @@ module Axlsx
206
207
  # :fg_color=>"#FF000000",
207
208
  # :type => :dxf)
208
209
  #
209
- # ws.add_row :values => ["Genreated At:", Time.now], :styles=>[nil, date_time]
210
- # ws.add_row :values => ["Previous Year Quarterly Profits (JPY)"], :style=>title
211
- # ws.add_row :values => ["Quarter", "Profit", "% of Total"], :style=>title
212
- # ws.add_row :values => ["Q1", 4000, 40], :style=>[title, currency, percent]
213
- # ws.add_row :values => ["Q2", 3000, 30], :style=>[title, currency, percent]
214
- # ws.add_row :values => ["Q3", 1000, 10], :style=>[title, currency, percent]
215
- # ws.add_row :values => ["Q4", 2000, 20], :style=>[title, currency, percent]
210
+ # ws.add_row ["Genreated At:", Time.now], :styles=>[nil, date_time]
211
+ # ws.add_row ["Previous Year Quarterly Profits (JPY)"], :style=>title
212
+ # ws.add_row ["Quarter", "Profit", "% of Total"], :style=>title
213
+ # ws.add_row ["Q1", 4000, 40], :style=>[title, currency, percent]
214
+ # ws.add_row ["Q2", 3000, 30], :style=>[title, currency, percent]
215
+ # ws.add_row ["Q3", 1000, 10], :style=>[title, currency, percent]
216
+ # ws.add_row ["Q4", 2000, 20], :style=>[title, currency, percent]
216
217
  #
217
218
  # ws.add_conditional_formatting("A1:A7", { :type => :cellIs, :operator => :greaterThan, :formula => "2000", :dxfId => profitable, :priority => 1 })
218
219
  # f = File.open('example_differential_styling', 'w')
@@ -278,6 +279,10 @@ module Axlsx
278
279
  # @return [Font|Integer]
279
280
  def parse_font_options(options={})
280
281
  return if (options.keys & [:fg_color, :sz, :b, :i, :u, :strike, :outline, :shadow, :charset, :family, :font_name]).empty?
282
+ fonts.first.instance_values.each do |key, value|
283
+ # Thanks for that 1.8.7 - cant do a simple merge...
284
+ options[key.to_sym] = value unless options.keys.include?(key.to_sym)
285
+ end
281
286
  font = Font.new(options)
282
287
  font.color = Color.new(:rgb => options[:fg_color]) if options[:fg_color]
283
288
  font.name = options[:font_name] if options[:font_name]
@@ -291,14 +296,22 @@ module Axlsx
291
296
  def parse_fill_options(options={})
292
297
  return unless options[:bg_color]
293
298
  color = Color.new(:rgb=>options[:bg_color])
294
- pattern = PatternFill.new(:patternType =>:solid, :fgColor=>color)
299
+ dxf = options[:type] == :dxf
300
+ color_key = dxf ? :bgColor : :fgColor
301
+ pattern = PatternFill.new(:patternType =>:solid, color_key=>color)
295
302
  fill = Fill.new(pattern)
296
- options[:type] == :dxf ? fill : fills << fill
303
+ dxf ? fill : fills << fill
297
304
  end
298
305
 
299
306
  # parses Style#add_style options for borders.
300
307
  # @note noop if :border is not specified in options
301
- # @option options [Hash|Integer] A border style definition hash or the index of an existing border. Border style definition hashes must include :style and color: key-value entries and may include an :edges entry that references an array of symbols identifying which border edges you wish to apply the style or any other valid Border initializer options. If the :edges entity is not provided the style is applied to all edges of cells that reference this style.
308
+ # @option options [Hash|Integer] A border style definition hash or the index of an existing border.
309
+ # Border style definition hashes must include :style and :color key-value entries and
310
+ # may include an :edges entry that references an array of symbols identifying which border edges
311
+ # you wish to apply the style or any other valid Border initializer options.
312
+ # If the :edges entity is not provided the style is applied to all edges of cells that reference this style.
313
+ # Also available :border_top, :border_right, :border_bottom and :border_left options with :style and/or :color
314
+ # key-value entries, which override :border values.
302
315
  # @example
303
316
  # #apply a thick red border to the top and bottom
304
317
  # { :border => { :style => :thick, :color => "FFFF0000", :edges => [:top, :bottom] }
@@ -307,10 +320,12 @@ module Axlsx
307
320
  return unless options[:border]
308
321
  b_opts = options[:border]
309
322
  if b_opts.is_a?(Hash)
310
- raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % b_opts) unless b_opts.values_at(:style, :color).size == 2
323
+ raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % b_opts) unless b_opts.keys.include?(:style) && b_opts.keys.include?(:color)
311
324
  border = Border.new b_opts
312
325
  (b_opts[:edges] || [:left, :right, :top, :bottom]).each do |edge|
313
- b_options = { :name => edge, :style => b_opts[:style], :color => Color.new(:rgb => b_opts[:color]) }
326
+ edge_options = options["border_#{edge}".to_sym] || {}
327
+ border_edge = b_opts.merge(edge_options)
328
+ b_options = { :name => edge, :style => border_edge[:style], :color => Color.new(:rgb => border_edge[:color]) }
314
329
  border.prs << BorderPr.new(b_options)
315
330
  end
316
331
  options[:type] == :dxf ? border : borders << border
@@ -4,6 +4,22 @@ module Axlsx
4
4
  # @note Table are not supported in this version and only the defaults required for a valid workbook are created.
5
5
  class TableStyle < SimpleTypedList
6
6
 
7
+ include Axlsx::OptionsParser
8
+ include Axlsx::SerializedAttributes
9
+
10
+ # creates a new TableStyle object
11
+ # @raise [ArgumentError] if name option is not provided.
12
+ # @param [String] name
13
+ # @option options [Boolean] pivot
14
+ # @option options [Boolean] table
15
+ def initialize(name, options={})
16
+ self.name = name
17
+ parse_options options
18
+ super TableStyleElement
19
+ end
20
+
21
+ serializable_attributes :name, :pivot, :table
22
+
7
23
  # The name of this table style
8
24
  # @return [string]
9
25
  attr_reader :name
@@ -16,19 +32,6 @@ module Axlsx
16
32
  # @return [Boolean]
17
33
  attr_reader :table
18
34
 
19
- # creates a new TableStyle object
20
- # @raise [ArgumentError] if name option is not provided.
21
- # @param [String] name
22
- # @option options [Boolean] pivot
23
- # @option options [Boolean] table
24
- def initialize(name, options={})
25
- self.name = name
26
- options.each do |o|
27
- self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
28
- end
29
- super TableStyleElement
30
- end
31
-
32
35
  # @see name
33
36
  def name=(v) Axlsx::validate_string v; @name=v end
34
37
  # @see pivot
@@ -40,10 +43,8 @@ module Axlsx
40
43
  # @param [String] str
41
44
  # @return [String]
42
45
  def to_xml_string(str = '')
43
- attrs = instance_values.reject { |k, v| ![:name, :pivot, :table].include?(k) }
44
- attrs[:count] = self.size
45
46
  str << '<tableStyle '
46
- str << attrs.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
47
+ serialized_attributes str, {:count => self.size}
47
48
  str << '>'
48
49
  each { |table_style_el| table_style_el.to_xml_string(str) }
49
50
  str << '</tableStyle>'
@@ -3,6 +3,20 @@ module Axlsx
3
3
  # an element of style that belongs to a table style.
4
4
  # @note tables and table styles are not supported in this version. This class exists in preparation for that support.
5
5
  class TableStyleElement
6
+
7
+ include Axlsx::OptionsParser
8
+ include Axlsx::SerializedAttributes
9
+
10
+ # creates a new TableStyleElement object
11
+ # @option options [Symbol] type
12
+ # @option options [Integer] size
13
+ # @option options [Integer] dxfId
14
+ def initialize(options={})
15
+ parse_options options
16
+ end
17
+
18
+ serializable_attributes :type, :size, :dxfId
19
+
6
20
  # The type of style element. The following type are allowed
7
21
  # :wholeTable
8
22
  # :headerRow
@@ -43,16 +57,6 @@ module Axlsx
43
57
  # @return [Integer]
44
58
  attr_reader :dxfId
45
59
 
46
- # creates a new TableStyleElement object
47
- # @option options [Symbol] type
48
- # @option options [Integer] size
49
- # @option options [Integer] dxfId
50
- def initialize(options={})
51
- options.each do |o|
52
- self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
53
- end
54
- end
55
-
56
60
  # @see type
57
61
  def type=(v) Axlsx::validate_table_element_type v; @type = v end
58
62
 
@@ -67,7 +71,7 @@ module Axlsx
67
71
  # @return [String]
68
72
  def to_xml_string(str = '')
69
73
  str << '<tableStyleElement '
70
- str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
74
+ serialized_attributes str
71
75
  str << '/>'
72
76
  end
73
77
 
@@ -4,13 +4,7 @@ module Axlsx
4
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.
5
5
  class TableStyles < SimpleTypedList
6
6
 
7
- # The default table style. The default value is 'TableStyleMedium9'
8
- # @return [String]
9
- attr_reader :defaultTableStyle
10
-
11
- # The default pivot table style. The default value is 'PivotStyleLight6'
12
- # @return [String]
13
- attr_reader :defaultPivotStyle
7
+ include Axlsx::SerializedAttributes
14
8
 
15
9
  # Creates a new TableStyles object that is a container for TableStyle objects
16
10
  # @option options [String] defaultTableStyle
@@ -20,7 +14,18 @@ module Axlsx
20
14
  @defaultPivotStyle = options[:defaultPivotStyle] || "PivotStyleLight16"
21
15
  super TableStyle
22
16
  end
23
- # @see defaultTableStyle
17
+
18
+ serializable_attributes :defaultTableStyle, :defaultPivotStyle
19
+
20
+ # The default table style. The default value is 'TableStyleMedium9'
21
+ # @return [String]
22
+ attr_reader :defaultTableStyle
23
+
24
+ # The default pivot table style. The default value is 'PivotStyleLight6'
25
+ # @return [String]
26
+ attr_reader :defaultPivotStyle
27
+
28
+ # @see defaultTableStyle
24
29
  def defaultTableStyle=(v) Axlsx::validate_string(v); @defaultTableStyle = v; end
25
30
  # @see defaultPivotStyle
26
31
  def defaultPivotStyle=(v) Axlsx::validate_string(v); @defaultPivotStyle = v; end
@@ -29,10 +34,8 @@ module Axlsx
29
34
  # @param [String] str
30
35
  # @return [String]
31
36
  def to_xml_string(str = '')
32
- attr = self.instance_values.reject {|k, v| ![:defaultTableStyle, :defaultPivotStyle].include?(k.to_sym) }
33
- attr[:count] = self.size
34
37
  str << '<tableStyles '
35
- str << attr.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
38
+ serialized_attributes str, {:count => self.size }
36
39
  str << '>'
37
40
  each { |table_style| table_style.to_xml_string(str) }
38
41
  str << '</tableStyles>'