axlsx 1.2.0 → 1.3.3

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 (161) hide show
  1. data/.yardopts +3 -2
  2. data/CHANGELOG.md +34 -1
  3. data/README.md +47 -33
  4. data/Rakefile +1 -1
  5. data/examples/auto_filter.rb +16 -0
  6. data/examples/basic_charts.rb +4 -0
  7. data/examples/colored_links.rb +59 -0
  8. data/examples/doc/_index.html +88 -0
  9. data/examples/doc/class_list.html +53 -0
  10. data/examples/doc/css/common.css +1 -0
  11. data/examples/doc/css/full_list.css +57 -0
  12. data/examples/doc/css/style.css +328 -0
  13. data/examples/doc/file_list.html +52 -0
  14. data/examples/doc/frames.html +28 -0
  15. data/examples/doc/index.html +88 -0
  16. data/examples/doc/js/app.js +214 -0
  17. data/examples/doc/js/full_list.js +173 -0
  18. data/examples/doc/js/jquery.js +4 -0
  19. data/examples/doc/method_list.html +52 -0
  20. data/examples/doc/top-level-namespace.html +102 -0
  21. data/examples/example.rb +447 -257
  22. data/examples/finance.rb +82 -0
  23. data/examples/hyperlinks.rb +23 -0
  24. data/examples/page_setup.rb +11 -0
  25. data/examples/skydrive/axlsx.csv +1 -0
  26. data/examples/skydrive/axlsx.xlsx +0 -0
  27. data/examples/skydrive/real_example.rb +8 -8
  28. data/examples/sprk2012/Screen Shot 2012-09-11 at 10.42.06 PM.png +0 -0
  29. data/examples/sprk2012/Screen Shot 2012-09-11 at 11.07.48 PM.png +0 -0
  30. data/examples/sprk2012/Screen Shot 2012-09-11 at 8.31.50 PM.png +0 -0
  31. data/examples/sprk2012/Screen Shot 2012-09-11 at 9.23.27 PM.png +0 -0
  32. data/examples/sprk2012/Screen Shot 2012-09-11 at 9.32.06 PM.png +0 -0
  33. data/examples/sprk2012/Screen Shot 2012-09-11 at 9.33.35 PM.png +0 -0
  34. data/examples/sprk2012/Screen Shot 2012-09-11 at 9.46.44 PM.png +0 -0
  35. data/examples/sprk2012/Screen Shot 2012-09-12 at 5.07.23 PM.png +0 -0
  36. data/examples/sprk2012/basics.rb +11 -0
  37. data/examples/sprk2012/basics.xlsx +0 -0
  38. data/examples/sprk2012/gravatar.jpeg +0 -0
  39. data/examples/sprk2012/hair_club.jpg +0 -0
  40. data/examples/sprk2012/images.rb +9 -0
  41. data/examples/{example.xlsx → sprk2012/images.xlsx} +0 -0
  42. data/examples/sprk2012/line_chart.rb +56 -0
  43. data/examples/sprk2012/line_chart.xlsx +0 -0
  44. data/examples/sprk2012/sprk2012.key +0 -0
  45. data/examples/sprk2012/styles.rb +20 -0
  46. data/examples/sprk2012/styles.xlsx +0 -0
  47. data/examples/styles.rb +62 -0
  48. data/lib/axlsx/content_type/abstract_content_type.rb +32 -0
  49. data/lib/axlsx/content_type/content_type.rb +1 -1
  50. data/lib/axlsx/content_type/default.rb +6 -37
  51. data/lib/axlsx/content_type/override.rb +6 -38
  52. data/lib/axlsx/doc_props/app.rb +10 -7
  53. data/lib/axlsx/drawing/axis.rb +4 -4
  54. data/lib/axlsx/drawing/chart.rb +2 -3
  55. data/lib/axlsx/drawing/d_lbls.rb +21 -31
  56. data/lib/axlsx/drawing/drawing.rb +6 -0
  57. data/lib/axlsx/drawing/hyperlink.rb +40 -32
  58. data/lib/axlsx/drawing/marker.rb +13 -13
  59. data/lib/axlsx/drawing/num_data.rb +6 -6
  60. data/lib/axlsx/drawing/num_data_source.rb +17 -16
  61. data/lib/axlsx/drawing/one_cell_anchor.rb +20 -22
  62. data/lib/axlsx/drawing/pic.rb +25 -27
  63. data/lib/axlsx/drawing/picture_locking.rb +12 -44
  64. data/lib/axlsx/drawing/scaling.rb +13 -13
  65. data/lib/axlsx/drawing/scatter_chart.rb +3 -3
  66. data/lib/axlsx/drawing/series.rb +3 -6
  67. data/lib/axlsx/drawing/str_data.rb +3 -3
  68. data/lib/axlsx/drawing/str_val.rb +7 -8
  69. data/lib/axlsx/drawing/view_3D.rb +52 -38
  70. data/lib/axlsx/drawing/vml_shape.rb +23 -23
  71. data/lib/axlsx/package.rb +14 -17
  72. data/lib/axlsx/stylesheet/border.rb +29 -20
  73. data/lib/axlsx/stylesheet/border_pr.rb +5 -4
  74. data/lib/axlsx/stylesheet/cell_alignment.rb +55 -29
  75. data/lib/axlsx/stylesheet/cell_protection.rb +7 -4
  76. data/lib/axlsx/stylesheet/cell_style.rb +19 -14
  77. data/lib/axlsx/stylesheet/color.rb +19 -16
  78. data/lib/axlsx/stylesheet/dxf.rb +4 -4
  79. data/lib/axlsx/stylesheet/font.rb +22 -22
  80. data/lib/axlsx/stylesheet/gradient_fill.rb +45 -21
  81. data/lib/axlsx/stylesheet/num_fmt.rb +22 -13
  82. data/lib/axlsx/stylesheet/pattern_fill.rb +12 -21
  83. data/lib/axlsx/stylesheet/styles.rb +11 -2
  84. data/lib/axlsx/stylesheet/table_style.rb +17 -16
  85. data/lib/axlsx/stylesheet/table_style_element.rb +15 -11
  86. data/lib/axlsx/stylesheet/table_styles.rb +14 -11
  87. data/lib/axlsx/stylesheet/xf.rb +28 -26
  88. data/lib/axlsx/util/accessors.rb +49 -0
  89. data/lib/axlsx/util/constants.rb +105 -4
  90. data/lib/axlsx/util/options_parser.rb +15 -0
  91. data/lib/axlsx/util/serialized_attributes.rb +46 -0
  92. data/lib/axlsx/util/simple_typed_list.rb +16 -2
  93. data/lib/axlsx/util/validators.rb +33 -8
  94. data/lib/axlsx/version.rb +2 -2
  95. data/lib/axlsx/workbook/defined_name.rb +13 -58
  96. data/lib/axlsx/workbook/workbook.rb +33 -2
  97. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +77 -0
  98. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +94 -0
  99. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +246 -0
  100. data/lib/axlsx/workbook/worksheet/cell.rb +82 -45
  101. data/lib/axlsx/workbook/worksheet/cfvo.rb +15 -15
  102. data/lib/axlsx/workbook/worksheet/cfvos.rb +18 -0
  103. data/lib/axlsx/workbook/worksheet/col.rb +34 -27
  104. data/lib/axlsx/workbook/worksheet/color_scale.rb +7 -13
  105. data/lib/axlsx/workbook/worksheet/comment.rb +14 -11
  106. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +11 -11
  107. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +27 -25
  108. data/lib/axlsx/workbook/worksheet/data_bar.rb +44 -34
  109. data/lib/axlsx/workbook/worksheet/data_validation.rb +61 -62
  110. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +1 -1
  111. data/lib/axlsx/workbook/worksheet/dimension.rb +0 -1
  112. data/lib/axlsx/workbook/worksheet/icon_set.rb +20 -20
  113. data/lib/axlsx/workbook/worksheet/page_margins.rb +21 -19
  114. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +44 -0
  115. data/lib/axlsx/workbook/worksheet/page_setup.rb +152 -23
  116. data/lib/axlsx/workbook/worksheet/pane.rb +48 -51
  117. data/lib/axlsx/workbook/worksheet/print_options.rb +8 -30
  118. data/lib/axlsx/workbook/worksheet/protected_range.rb +16 -13
  119. data/lib/axlsx/workbook/worksheet/row.rb +48 -81
  120. data/lib/axlsx/workbook/worksheet/selection.rb +30 -38
  121. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +29 -0
  122. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +49 -4
  123. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +51 -155
  124. data/lib/axlsx/workbook/worksheet/sheet_view.rb +68 -234
  125. data/lib/axlsx/workbook/worksheet/table.rb +22 -17
  126. data/lib/axlsx/workbook/worksheet/table_style_info.rb +51 -0
  127. data/lib/axlsx/workbook/worksheet/worksheet.rb +51 -14
  128. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +1 -1
  129. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +79 -0
  130. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +38 -0
  131. data/lib/axlsx.rb +19 -5
  132. data/test/content_type/tc_default.rb +0 -11
  133. data/test/content_type/tc_override.rb +0 -13
  134. data/test/drawing/tc_d_lbls.rb +14 -4
  135. data/test/stylesheet/tc_styles.rb +13 -0
  136. data/test/tc_axlsx.rb +20 -2
  137. data/test/tc_package.rb +1 -0
  138. data/test/util/tc_validators.rb +22 -1
  139. data/test/workbook/tc_defined_name.rb +2 -2
  140. data/test/workbook/tc_workbook.rb +15 -0
  141. data/test/workbook/worksheet/auto_filter/tc_auto_filter.rb +38 -0
  142. data/test/workbook/worksheet/auto_filter/tc_filter_column.rb +76 -0
  143. data/test/workbook/worksheet/auto_filter/tc_filters.rb +50 -0
  144. data/test/workbook/worksheet/tc_cell.rb +35 -5
  145. data/test/workbook/worksheet/tc_col.rb +11 -1
  146. data/test/workbook/worksheet/tc_page_set_up_pr.rb +15 -0
  147. data/test/workbook/worksheet/tc_page_setup.rb +6 -1
  148. data/test/workbook/worksheet/tc_pane.rb +5 -45
  149. data/test/workbook/worksheet/tc_row.rb +4 -4
  150. data/test/workbook/worksheet/tc_selection.rb +9 -48
  151. data/test/workbook/worksheet/tc_sheet_calc_pr.rb +18 -0
  152. data/test/workbook/worksheet/tc_sheet_pr.rb +27 -0
  153. data/test/workbook/worksheet/{table/tc_table.rb → tc_table.rb} +6 -1
  154. data/test/workbook/worksheet/tc_table_style_info.rb +53 -0
  155. data/test/workbook/worksheet/tc_worksheet.rb +17 -3
  156. data/test/workbook/worksheet/tc_worksheet_hyperlink.rb +64 -0
  157. metadata +74 -10
  158. data/examples/example_streamed.xlsx +0 -0
  159. data/examples/no-use_autowidth.xlsx +0 -0
  160. data/examples/shared_strings_example.xlsx +0 -0
  161. 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,17 +46,7 @@ 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)
@@ -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,6 +3,26 @@ 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
+
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] fomratCode 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
+
6
26
  # @return [Integer] An unsinged 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.
@@ -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
@@ -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]
@@ -298,7 +303,11 @@ module Axlsx
298
303
 
299
304
  # parses Style#add_style options for borders.
300
305
  # @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.
306
+ # @option options [Hash|Integer] A border style definition hash or the index of an existing border.
307
+ # Border style definition hashes must include :style and :color key-value entries and
308
+ # may include an :edges entry that references an array of symbols identifying which border edges
309
+ # you wish to apply the style or any other valid Border initializer options.
310
+ # If the :edges entity is not provided the style is applied to all edges of cells that reference this style.
302
311
  # @example
303
312
  # #apply a thick red border to the top and bottom
304
313
  # { :border => { :style => :thick, :color => "FFFF0000", :edges => [:top, :bottom] }
@@ -307,7 +316,7 @@ module Axlsx
307
316
  return unless options[:border]
308
317
  b_opts = options[:border]
309
318
  if b_opts.is_a?(Hash)
310
- raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % b_opts) unless b_opts.values_at(:style, :color).size == 2
319
+ raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % b_opts) unless b_opts.keys.include?(:style) && b_opts.keys.include?(:color)
311
320
  border = Border.new b_opts
312
321
  (b_opts[:edges] || [:left, :right, :top, :bottom]).each do |edge|
313
322
  b_options = { :name => edge, :style => b_opts[:style], :color => Color.new(:rgb => b_opts[:color]) }
@@ -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>'
@@ -5,6 +5,32 @@ module Axlsx
5
5
  class Xf
6
6
  #does not support extList (ExtensionList)
7
7
 
8
+ include Axlsx::SerializedAttributes
9
+ include Axlsx::OptionsParser
10
+ # Creates a new Xf object
11
+ # @option options [Integer] numFmtId
12
+ # @option options [Integer] fontId
13
+ # @option options [Integer] fillId
14
+ # @option options [Integer] borderId
15
+ # @option options [Integer] xfId
16
+ # @option options [Boolean] quotePrefix
17
+ # @option options [Boolean] pivotButton
18
+ # @option options [Boolean] applyNumberFormat
19
+ # @option options [Boolean] applyFont
20
+ # @option options [Boolean] applyFill
21
+ # @option options [Boolean] applyBorder
22
+ # @option options [Boolean] applyAlignment
23
+ # @option options [Boolean] applyProtection
24
+ # @option options [CellAlignment] alignment
25
+ # @option options [CellProtection] protection
26
+ def initialize(options={})
27
+ parse_options options
28
+ end
29
+
30
+ serializable_attributes :numFmtId, :fontId, :fillId, :borderId, :xfId, :quotePrefix,
31
+ :pivotButton, :applyNumberFormat, :applyFont, :applyFill, :applyBorder, :applyAlignment,
32
+ :applyProtection
33
+
8
34
  # The cell alignment for this style
9
35
  # @return [CellAlignment]
10
36
  # @see CellAlignment
@@ -67,30 +93,7 @@ module Axlsx
67
93
  # @return [Boolean]
68
94
  attr_reader :applyProtection
69
95
 
70
- # Creates a new Xf object
71
- # @option options [Integer] numFmtId
72
- # @option options [Integer] fontId
73
- # @option options [Integer] fillId
74
- # @option options [Integer] borderId
75
- # @option options [Integer] xfId
76
- # @option options [Boolean] quotePrefix
77
- # @option options [Boolean] pivotButton
78
- # @option options [Boolean] applyNumberFormat
79
- # @option options [Boolean] applyFont
80
- # @option options [Boolean] applyFill
81
- # @option options [Boolean] applyBorder
82
- # @option options [Boolean] applyAlignment
83
- # @option options [Boolean] applyProtection
84
- # @option options [CellAlignment] alignment
85
- # @option options [CellProtection] protection
86
- def initialize(options={})
87
- options.each do |o|
88
- next if o[1].nil?
89
- self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
90
- end
91
- end
92
-
93
- # @see Xf#alignment
96
+ # @see Xf#alignment
94
97
  def alignment=(v) DataTypeValidator.validate "Xf.alignment", CellAlignment, v; @alignment = v end
95
98
 
96
99
  # @see protection
@@ -132,8 +135,7 @@ module Axlsx
132
135
  # @return [String]
133
136
  def to_xml_string(str = '')
134
137
  str << '<xf '
135
- h = instance_values.reject { |k, v| [:alignment, :protection, :extList, :name].include? k.to_sym}
136
- str << h.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
138
+ serialized_attributes str
137
139
  str << '>'
138
140
  alignment.to_xml_string(str) if self.alignment
139
141
  protection.to_xml_string(str) if self.protection
@@ -0,0 +1,49 @@
1
+ module Axlsx
2
+ # This module defines some of the more common validating attribute
3
+ # accessors that we use in Axlsx
4
+ #
5
+ # When this module is included in your class you can simply call
6
+ #
7
+ # string_attr_access :foo
8
+ #
9
+ # To generate a new, validating set of accessors for foo.
10
+ module Accessors
11
+ def self.included(base)
12
+ base.send :extend, ClassMethods
13
+ end
14
+
15
+ # Defines the class level xxx_attr_accessor methods
16
+ module ClassMethods
17
+
18
+ # Creates one or more string validated attr_accessors
19
+ # @param [Array] symbols An array of symbols representing the
20
+ # names of the attributes you will add to your class.
21
+ def string_attr_accessor(*symbols)
22
+ validated_attr_accessor(symbols, 'validate_string')
23
+ end
24
+
25
+ # Creates on or more boolean validated attr_accessors
26
+ # @param [Array] symbols An array of symbols representing the
27
+ # names of the attributes you will add to your class.
28
+ def boolean_attr_accessor(*symbols)
29
+ validated_attr_accessor(symbols, 'validate_boolean')
30
+ end
31
+
32
+ # Template for defining validated write accessors
33
+ SETTER = "def %s=(value) Axlsx::%s(value); @%s = value; end"
34
+
35
+ # Creates the reader and writer access methods
36
+ # @param [Array] symbols The names of the attributes to create
37
+ # @param [String] validator The axlsx validation method to use when
38
+ # validating assignation.
39
+ # @see lib/axlsx/util/validators.rb
40
+ def validated_attr_accessor(symbols, validator)
41
+ symbols.each do |symbol|
42
+ attr_reader symbol
43
+ module_eval(SETTER % [symbol.to_s, validator, symbol.to_s], __FILE__, __LINE__)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+