axlsx 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. data/Rakefile +13 -0
  2. data/lib/axlsx.rb +38 -0
  3. data/lib/axlsx/content_type/content_type.rb +23 -0
  4. data/lib/axlsx/content_type/default.rb +32 -0
  5. data/lib/axlsx/content_type/override.rb +30 -0
  6. data/lib/axlsx/doc_props/app.rb +148 -0
  7. data/lib/axlsx/doc_props/core.rb +34 -0
  8. data/lib/axlsx/drawing/axis.rb +78 -0
  9. data/lib/axlsx/drawing/bar_3D_chart.rb +138 -0
  10. data/lib/axlsx/drawing/bar_series.rb +91 -0
  11. data/lib/axlsx/drawing/cat_axis.rb +58 -0
  12. data/lib/axlsx/drawing/chart.rb +120 -0
  13. data/lib/axlsx/drawing/drawing.rb +121 -0
  14. data/lib/axlsx/drawing/graphic_frame.rb +55 -0
  15. data/lib/axlsx/drawing/marker.rb +57 -0
  16. data/lib/axlsx/drawing/pie_3D_chart.rb +59 -0
  17. data/lib/axlsx/drawing/pie_series.rb +88 -0
  18. data/lib/axlsx/drawing/scaling.rb +53 -0
  19. data/lib/axlsx/drawing/series.rb +70 -0
  20. data/lib/axlsx/drawing/title.rb +69 -0
  21. data/lib/axlsx/drawing/two_cell_anchor.rb +88 -0
  22. data/lib/axlsx/drawing/val_axis.rb +34 -0
  23. data/lib/axlsx/drawing/view_3D.rb +72 -0
  24. data/lib/axlsx/package.rb +181 -0
  25. data/lib/axlsx/rels/relationship.rb +43 -0
  26. data/lib/axlsx/rels/relationships.rb +25 -0
  27. data/lib/axlsx/stylesheet/border.rb +52 -0
  28. data/lib/axlsx/stylesheet/border_pr.rb +65 -0
  29. data/lib/axlsx/stylesheet/cell_alignment.rb +96 -0
  30. data/lib/axlsx/stylesheet/cell_protection.rb +33 -0
  31. data/lib/axlsx/stylesheet/cell_style.rb +60 -0
  32. data/lib/axlsx/stylesheet/color.rb +57 -0
  33. data/lib/axlsx/stylesheet/fill.rb +31 -0
  34. data/lib/axlsx/stylesheet/font.rb +128 -0
  35. data/lib/axlsx/stylesheet/gradient_fill.rb +70 -0
  36. data/lib/axlsx/stylesheet/gradient_stop.rb +31 -0
  37. data/lib/axlsx/stylesheet/num_fmt.rb +61 -0
  38. data/lib/axlsx/stylesheet/pattern_fill.rb +64 -0
  39. data/lib/axlsx/stylesheet/styles.rb +296 -0
  40. data/lib/axlsx/stylesheet/table_style.rb +44 -0
  41. data/lib/axlsx/stylesheet/table_style_element.rb +66 -0
  42. data/lib/axlsx/stylesheet/table_styles.rb +39 -0
  43. data/lib/axlsx/stylesheet/xf.rb +117 -0
  44. data/lib/axlsx/util/constants.rb +189 -0
  45. data/lib/axlsx/util/simple_typed_list.rb +150 -0
  46. data/lib/axlsx/util/validators.rb +132 -0
  47. data/lib/axlsx/workbook/workbook.rb +130 -0
  48. data/lib/axlsx/workbook/worksheet/cell.rb +184 -0
  49. data/lib/axlsx/workbook/worksheet/row.rb +92 -0
  50. data/lib/axlsx/workbook/worksheet/worksheet.rb +194 -0
  51. data/lib/schema/dc.xsd +118 -0
  52. data/lib/schema/dcmitype.xsd +50 -0
  53. data/lib/schema/dcterms.xsd +331 -0
  54. data/lib/schema/dml-chart.xsd +1499 -0
  55. data/lib/schema/dml-chartDrawing.xsd +146 -0
  56. data/lib/schema/dml-compatibility.xsd +14 -0
  57. data/lib/schema/dml-diagram.xsd +1091 -0
  58. data/lib/schema/dml-lockedCanvas.xsd +11 -0
  59. data/lib/schema/dml-main.xsd +3048 -0
  60. data/lib/schema/dml-picture.xsd +23 -0
  61. data/lib/schema/dml-spreadsheetDrawing.xsd +185 -0
  62. data/lib/schema/dml-wordprocessingDrawing.xsd +185 -0
  63. data/lib/schema/opc-contentTypes.xsd +42 -0
  64. data/lib/schema/opc-coreProperties.xsd +50 -0
  65. data/lib/schema/opc-digSig.xsd +49 -0
  66. data/lib/schema/opc-relationships.xsd +33 -0
  67. data/lib/schema/pml.xsd +1676 -0
  68. data/lib/schema/shared-additionalCharacteristics.xsd +28 -0
  69. data/lib/schema/shared-bibliography.xsd +144 -0
  70. data/lib/schema/shared-commonSimpleTypes.xsd +166 -0
  71. data/lib/schema/shared-customXmlDataProperties.xsd +25 -0
  72. data/lib/schema/shared-customXmlSchemaProperties.xsd +18 -0
  73. data/lib/schema/shared-documentPropertiesCustom.xsd +59 -0
  74. data/lib/schema/shared-documentPropertiesExtended.xsd +56 -0
  75. data/lib/schema/shared-documentPropertiesVariantTypes.xsd +195 -0
  76. data/lib/schema/shared-math.xsd +582 -0
  77. data/lib/schema/shared-relationshipReference.xsd +25 -0
  78. data/lib/schema/sml.xsd +4430 -0
  79. data/lib/schema/vml-main.xsd +569 -0
  80. data/lib/schema/vml-officeDrawing.xsd +509 -0
  81. data/lib/schema/vml-presentationDrawing.xsd +12 -0
  82. data/lib/schema/vml-spreadsheetDrawing.xsd +108 -0
  83. data/lib/schema/vml-wordprocessingDrawing.xsd +96 -0
  84. data/lib/schema/wml.xsd +3644 -0
  85. data/lib/schema/xml.xsd +117 -0
  86. data/test/content_type/tc_content_type.rb +81 -0
  87. data/test/content_type/tc_content_type.rb~ +81 -0
  88. data/test/content_type/tc_default.rb +40 -0
  89. data/test/content_type/tc_default.rb~ +40 -0
  90. data/test/content_type/tc_override.rb +40 -0
  91. data/test/content_type/tc_override.rb~ +40 -0
  92. data/test/doc_props/tc_app.rb +19 -0
  93. data/test/doc_props/tc_app.rb~ +19 -0
  94. data/test/doc_props/tc_core.rb +34 -0
  95. data/test/drawing/tc_axis.rb +39 -0
  96. data/test/drawing/tc_axis.rb~ +0 -0
  97. data/test/drawing/tc_bar_3D_chart.rb +66 -0
  98. data/test/drawing/tc_bar_3D_chart.rb~ +4 -0
  99. data/test/drawing/tc_bar_series.rb +34 -0
  100. data/test/drawing/tc_bar_series.rb~ +31 -0
  101. data/test/drawing/tc_cat_axis.rb +32 -0
  102. data/test/drawing/tc_cat_axis.rb~ +39 -0
  103. data/test/drawing/tc_chart.rb +59 -0
  104. data/test/drawing/tc_chart.rb~ +58 -0
  105. data/test/drawing/tc_drawing.rb +71 -0
  106. data/test/drawing/tc_graphic_frame.rb +26 -0
  107. data/test/drawing/tc_graphic_frame.rb~ +21 -0
  108. data/test/drawing/tc_marker.rb +45 -0
  109. data/test/drawing/tc_marker.rb~ +26 -0
  110. data/test/drawing/tc_pie_3D_chart.rb +33 -0
  111. data/test/drawing/tc_pie_3D_chart.rb~ +58 -0
  112. data/test/drawing/tc_pie_series.rb +35 -0
  113. data/test/drawing/tc_pie_series.rb~ +26 -0
  114. data/test/drawing/tc_scaling.rb +37 -0
  115. data/test/drawing/tc_scaling.rb~ +45 -0
  116. data/test/drawing/tc_series.rb +24 -0
  117. data/test/drawing/tc_series.rb~ +31 -0
  118. data/test/drawing/tc_title.rb +34 -0
  119. data/test/drawing/tc_title.rb~ +37 -0
  120. data/test/drawing/tc_two_cell_anchor.rb +37 -0
  121. data/test/drawing/tc_two_cell_anchor.rb~ +35 -0
  122. data/test/drawing/tc_val_axis.rb +20 -0
  123. data/test/drawing/tc_val_axis.rb~ +32 -0
  124. data/test/drawing/tc_view_3D.rb +46 -0
  125. data/test/drawing/tc_view_3D.rb~ +37 -0
  126. data/test/rels/tc_relationship.rb +16 -0
  127. data/test/rels/tc_relationship.rb~ +39 -0
  128. data/test/rels/tc_relationships.rb +32 -0
  129. data/test/rels/tc_relationships.rb~ +37 -0
  130. data/test/stylesheet/tc_border.rb +38 -0
  131. data/test/stylesheet/tc_border.rb~ +31 -0
  132. data/test/stylesheet/tc_border_pr.rb +33 -0
  133. data/test/stylesheet/tc_border_pr.rb~ +31 -0
  134. data/test/stylesheet/tc_cell_alignment.rb +77 -0
  135. data/test/stylesheet/tc_cell_alignment.rb~ +38 -0
  136. data/test/stylesheet/tc_cell_protection.rb +30 -0
  137. data/test/stylesheet/tc_cell_protection.rb~ +77 -0
  138. data/test/stylesheet/tc_cell_style.rb +58 -0
  139. data/test/stylesheet/tc_cell_style.rb~ +30 -0
  140. data/test/stylesheet/tc_color.rb +38 -0
  141. data/test/stylesheet/tc_color.rb~ +38 -0
  142. data/test/stylesheet/tc_fill.rb +19 -0
  143. data/test/stylesheet/tc_fill.rb~ +19 -0
  144. data/test/stylesheet/tc_font.rb +114 -0
  145. data/test/stylesheet/tc_font.rb~ +19 -0
  146. data/test/stylesheet/tc_gradient_fill.rb +65 -0
  147. data/test/stylesheet/tc_gradient_fill.rb~ +114 -0
  148. data/test/stylesheet/tc_gradient_stop.rb +32 -0
  149. data/test/stylesheet/tc_gradient_stop.rb~ +65 -0
  150. data/test/stylesheet/tc_num_fmt.rb +31 -0
  151. data/test/stylesheet/tc_num_fmt.rb~ +32 -0
  152. data/test/stylesheet/tc_pattern_fill.rb +38 -0
  153. data/test/stylesheet/tc_pattern_fill.rb~ +31 -0
  154. data/test/stylesheet/tc_styles.rb +64 -0
  155. data/test/stylesheet/tc_table_style.rb +37 -0
  156. data/test/stylesheet/tc_table_style.rb~ +38 -0
  157. data/test/stylesheet/tc_table_style_element.rb +37 -0
  158. data/test/stylesheet/tc_table_style_element.rb~ +37 -0
  159. data/test/stylesheet/tc_table_styles.rb +30 -0
  160. data/test/stylesheet/tc_table_styles.rb~ +37 -0
  161. data/test/stylesheet/tc_xf.rb +121 -0
  162. data/test/stylesheet/tc_xf.rb~ +30 -0
  163. data/test/tc_app.rb~ +19 -0
  164. data/test/tc_border_pr.rb~ +21 -0
  165. data/test/tc_package.rb +70 -0
  166. data/test/tc_package.rb~ +64 -0
  167. data/test/tc_pie_3D_chart.rb~ +66 -0
  168. data/test/tc_relationships.rb~ +37 -0
  169. data/test/tc_series.rb~ +31 -0
  170. data/test/tc_styles.rb~ +64 -0
  171. data/test/tc_validators.rb~ +77 -0
  172. data/test/tc_worksheet.rb~ +85 -0
  173. data/test/util/tc_simple_typed_list.rb +66 -0
  174. data/test/util/tc_validators.rb +76 -0
  175. data/test/workbook/tc_workbook.rb +53 -0
  176. data/test/workbook/worksheet/tc_cell.rb +78 -0
  177. data/test/workbook/worksheet/tc_row.rb +30 -0
  178. data/test/workbook/worksheet/tc_worksheet.rb +85 -0
  179. metadata +378 -0
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Axlsx
3
+ # The GradientStop object represents a color point in a gradient.
4
+ # @see Open Office XML Part 1 §18.8.24
5
+ class GradientStop
6
+ # The color for this gradient stop
7
+ # @return [Color]
8
+ # @see Color
9
+ attr_accessor :color
10
+
11
+ # The position of the color
12
+ # @return [Float]
13
+ attr_accessor :position
14
+
15
+ # Creates a new GradientStop object
16
+ # @param [Color] color
17
+ # @param [Float] position
18
+ def initialize(color, position)
19
+ self.color = color
20
+ self.position = position
21
+ end
22
+
23
+ def color=(v) DataTypeValidator.validate "GradientStop.color", Color, v; @color=v end
24
+ def position=(v) DataTypeValidator.validate "GradientStop.position", Float, v, lambda { |v| v >= 0 && v <= 1}; @position = v end
25
+
26
+ # Serializes the gradientStop
27
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
28
+ # @return [String]
29
+ def to_xml(xml) xml.stop(:position => self.position) {self.color.to_xml(xml)} end
30
+ end
31
+ end
@@ -0,0 +1,61 @@
1
+ module Axlsx
2
+ # A NumFmt object defines an identifier and formatting code for data in cells.
3
+ # @note The recommended way to manage styles is Styles#add_style
4
+ class NumFmt
5
+ # @return [Integer] An unsinged integer referencing a standard or custom number format.
6
+ # @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
+ # 1 0
9
+ # 2 0.00
10
+ # 3 #,##0
11
+ # 4 #,##0.00
12
+ # 5 $#,##0_);($#,##0)
13
+ # 6 $#,##0_);[Red]($#,##0)
14
+ # 7 $#,##0.00_);($#,##0.00)
15
+ # 8 $#,##0.00_);[Red]($#,##0.00)
16
+ # 9 0%
17
+ # 10 0.00%
18
+ # 11 0.00E+00
19
+ # 12 # ?/?
20
+ # 13 # ??/??
21
+ # 14 m/d/yyyy
22
+ # 15 d-mmm-yy
23
+ # 16 d-mmm
24
+ # 17 mmm-yy
25
+ # 18 h:mm AM/PM
26
+ # 19 h:mm:ss AM/PM
27
+ # 20 h:mm
28
+ # 21 h:mm:ss
29
+ # 22 m/d/yyyy h:mm
30
+ # 37 #,##0_);(#,##0)
31
+ # 38 #,##0_);[Red](#,##0)
32
+ # 39 #,##0.00_);(#,##0.00)
33
+ # 40 #,##0.00_);[Red](#,##0.00)
34
+ # 45 mm:ss
35
+ # 46 [h]:mm:ss
36
+ # 47 mm:ss.0
37
+ # 48 ##0.0E+0
38
+ # 49 @
39
+ # @see Axlsx
40
+ attr_accessor :numFmtId
41
+
42
+ # @return [String] The formatting to use for this number format.
43
+ # @see http://support.microsoft.com/kb/264372
44
+ attr_accessor :formatCode
45
+ def initialize(options={})
46
+ @numFmtId = 0
47
+ @formatCode = ""
48
+ options.each do |o|
49
+ self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
50
+ end
51
+ end
52
+
53
+ def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end
54
+ def formatCode=(v) Axlsx::validate_string v; @formatCode = v end
55
+
56
+ # Creates a numFmt element applying the instance values of this object as attributes.
57
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
58
+ def to_xml(xml) xml.numFmt(self.instance_values) end
59
+
60
+ end
61
+ end
@@ -0,0 +1,64 @@
1
+ module Axlsx
2
+ # A PatternFill is the pattern and solid fill styling for a cell.
3
+ # @note The recommended way to manage styles is with Styles#add_style
4
+ # @see Style#add_style
5
+ class PatternFill
6
+
7
+ # The color to use for the the background in solid fills.
8
+ # @return [Color]
9
+ attr_accessor :fgColor
10
+
11
+ # The color to use for the background of the fill when the type is not solid.
12
+ # @return [Color]
13
+ attr_accessor :bgColor
14
+
15
+ # The pattern type to use
16
+ # @note
17
+ # patternType must be one of
18
+ # :none
19
+ # :solid
20
+ # :mediumGray
21
+ # :darkGray
22
+ # :lightGray
23
+ # :darkHorizontal
24
+ # :darkVertical
25
+ # :darkDown
26
+ # :darkUp
27
+ # :darkGrid
28
+ # :darkTrellis
29
+ # :lightHorizontal
30
+ # :lightVertical
31
+ # :lightDown
32
+ # :lightUp
33
+ # :lightGrid
34
+ # :lightTrellis
35
+ # :gray125
36
+ # :gray0625
37
+ # @see Office Open XML Part 1 18.18.55
38
+ attr_accessor :patternType
39
+
40
+ # Creates a new PatternFill Object
41
+ # @option options [Symbol] patternType
42
+ # @option options [Color] fgColor
43
+ # @option options [Color] bgColor
44
+ def initialize(options={})
45
+ @patternType = :none
46
+ options.each do |o|
47
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
48
+ end
49
+ end
50
+
51
+ def fgColor=(v) DataTypeValidator.validate "PatternFill.fgColor", Color, v; @fgColor=v end
52
+ def bgColor=(v) DataTypeValidator.validate "PatternFill.bgColor", Color, v; @bgColor=v end
53
+ def patternType=(v) Axlsx::validate_pattern_type v; @patternType = v end
54
+
55
+ # Serializes the pattern fill
56
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
57
+ # @return [String]
58
+ def to_xml(xml)
59
+ xml.patternFill(:patternType => self.patternType) {
60
+ self.instance_values.reject { |k,v| k.to_sym == :patternType }.each { |k,v| xml.send(k, v.instance_values) }
61
+ }
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,296 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Axlsx
3
+ require 'axlsx/stylesheet/border.rb'
4
+ require 'axlsx/stylesheet/border_pr.rb'
5
+ require 'axlsx/stylesheet/cell_alignment.rb'
6
+ require 'axlsx/stylesheet/cell_style.rb'
7
+ require 'axlsx/stylesheet/color.rb'
8
+ require 'axlsx/stylesheet/fill.rb'
9
+ require 'axlsx/stylesheet/font.rb'
10
+ require 'axlsx/stylesheet/gradient_fill.rb'
11
+ require 'axlsx/stylesheet/gradient_stop.rb'
12
+ require 'axlsx/stylesheet/num_fmt.rb'
13
+ require 'axlsx/stylesheet/pattern_fill.rb'
14
+ require 'axlsx/stylesheet/table_style.rb'
15
+ require 'axlsx/stylesheet/table_styles.rb'
16
+ require 'axlsx/stylesheet/table_style_element.rb'
17
+ require 'axlsx/stylesheet/xf.rb'
18
+ require 'axlsx/stylesheet/cell_protection.rb'
19
+
20
+ #The Styles class manages worksheet styles
21
+ # In addition to creating the require style objects for a valid xlsx package, this class provides the key mechanism for adding styles to your workbook, and safely applying them to the cells of your worksheet.
22
+ # All portions of the stylesheet are implemented here exception colors, which specify legacy and modified pallete colors, and exLst, whic is used as a future feature data storage area.
23
+ # @see Office Open XML Part 1 18.8.11 for gory details on how this stuff gets put together
24
+ # @see Styles#add_style
25
+ # @note The recommended way to manage styles is with add_style
26
+ class Styles
27
+ # numFmts for your styles.
28
+ # The default styles, which change based on the system local, are as follows.
29
+ # id formatCode
30
+ # 0 General
31
+ # 1 0
32
+ # 2 0.00
33
+ # 3 #,##0
34
+ # 4 #,##0.00
35
+ # 9 0%
36
+ # 10 0.00%
37
+ # 11 0.00E+00
38
+ # 12 # ?/?
39
+ # 13 # ??/??
40
+ # 14 mm-dd-yy
41
+ # 15 d-mmm-yy
42
+ # 16 d-mmm
43
+ # 17 mmm-yy
44
+ # 18 h:mm AM/PM
45
+ # 19 h:mm:ss AM/PM
46
+ # 20 h:mm
47
+ # 21 h:mm:ss
48
+ # 22 m/d/yy h:mm
49
+ # 37 #,##0 ;(#,##0)
50
+ # 38 #,##0 ;[Red](#,##0)
51
+ # 39 #,##0.00;(#,##0.00)
52
+ # 40 #,##0.00;[Red](#,##0.00)
53
+ # 45 mm:ss
54
+ # 46 [h]:mm:ss
55
+ # 47 mmss.0
56
+ # 48 ##0.0E+0
57
+ # 49 @
58
+ # Axlsx also defines the following constants which you can use in add_style.
59
+ # NUM_FMT_PERCENT formats to "0%"
60
+ # NUM_FMT_YYYYMMDD formats to "yyyy/mm/dd"
61
+ # NUM_FMT_YYYYMMDDHHMMSS formats to "yyyy/mm/dd hh:mm:ss"
62
+ # @see Office Open XML Part 1 - 18.8.31 for more information on creating number formats
63
+ # @return [SimpleTypedList]
64
+ # @note The recommended way to manage styles is with add_style
65
+ # @see Styles#add_style
66
+ attr_reader :numFmts
67
+
68
+ # The collection of fonts used in this workbook
69
+ # @return [SimpleTypedList]
70
+ # @note The recommended way to manage styles is with add_style
71
+ # @see Styles#add_style
72
+ attr_reader :fonts
73
+
74
+ # The collection of fills used in this workbook
75
+ # @return [SimpleTypedList]
76
+ # @note The recommended way to manage styles is with add_style
77
+ # @see Styles#add_style
78
+ attr_reader :fills
79
+
80
+ # The collection of borders used in this workbook
81
+ # Axlsx predefines THIN_BORDER which can be used to put a border around all of your cells.
82
+ # @return [SimpleTypedList]
83
+ # @note The recommended way to manage styles is with add_style
84
+ # @see Styles#add_style
85
+ attr_reader :borders
86
+
87
+ # The collection of master formatting records for named cell styles, which means records defined in cellStyles, in the workbook
88
+ # @return [SimpleTypedList]
89
+ # @note The recommended way to manage styles is with add_style
90
+ # @see Styles#add_style
91
+ attr_reader :cellStyleXfs
92
+
93
+ # The collection of named styles, referencing cellStyleXfs items in the workbook.
94
+ # @return [SimpleTypedList]
95
+ # @note The recommended way to manage styles is with add_style
96
+ # @see Styles#add_style
97
+ attr_reader :cellStyles
98
+
99
+ # The collection of master formatting records. This is the list that you will actually use in styling a workbook.
100
+ # @return [SimpleTypedList]
101
+ # @note The recommended way to manage styles is with add_style
102
+ # @see Styles#add_style
103
+ attr_reader :cellXfs
104
+
105
+ # The collection of non-cell formatting records used in the worksheet.
106
+ # @return [SimpleTypedList]
107
+ # @note The recommended way to manage styles is with add_style
108
+ # @see Styles#add_style
109
+ attr_reader :dxfs
110
+
111
+ # The collection of table styles that will be available to the user in the excel UI
112
+ # @return [SimpleTypedList]
113
+ # @note The recommended way to manage styles is with add_style
114
+ # @see Styles#add_style
115
+ attr_reader :tableStyles
116
+
117
+ # Creates a new Styles object and prepopulates it with the requires objects to generate a valid package style part.
118
+ def initialize()
119
+ load_default_styles
120
+ end
121
+
122
+ # Drastically simplifies style creation and management.
123
+ # @return [Integer]
124
+ # @option options [String] fg_color The text color
125
+ # @option options [Integer] sz The text size
126
+ # @option options [Boolean] b Indicates if the text should be bold
127
+ # @option options [Boolean] i Indicates if the text should be italicised
128
+ # @option options [Boolean] strike Indicates if the text should be rendered with a strikethrough
129
+ # @option options [Boolean] strike Indicates if the text should be rendered with a shadow
130
+ # @option options [Integer] charset The character set to use.
131
+ # @option options [Integer] family The font family to use.
132
+ # @option options [String] name The name of the font to use
133
+ # @option options [Integer] num_fmt The number format to apply
134
+ # @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 [String] bg_color The background color to apply to the cell
137
+ # @option options [Boolean] hidden Indicates if the cell should be hidden
138
+ # @option options [Boolean] locked Indicates if the cell should be locked
139
+ # @option options [Hash] alignment A hash defining any of the attributes used in CellAlignment
140
+ # @see CellAlignment
141
+ #
142
+ # @example You Got Style
143
+ # require "rubygems" # if that is your preferred way to manage gems!
144
+ # require "axlsx"
145
+ #
146
+ # p = Axlsx::Package.new
147
+ # ws = p.workbook.add_worksheet
148
+ #
149
+ # # 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
+ #
152
+ # ws.add_row :values => ["Least Popular Pets"]
153
+ # ws.add_row :values => ["", "Dry Skinned Reptiles", "Bald Cats", "Violent Parrots"], :style=>title
154
+ # ws.add_row :values => ["Votes", 6, 4, 1], :style=>Axlsx::STYLE_THIN_BORDER
155
+ # f = File.open('example_you_got_style.xlsx', 'w')
156
+ # p.serialize(f)
157
+ #
158
+ # @example Styling specifically
159
+ # # an example of applying specific styles to specific cells
160
+ # require "rubygems" # if that is your preferred way to manage gems!
161
+ # require "axlsx"
162
+ #
163
+ # p = Axlsx::Package.new
164
+ # ws = p.workbook.add_worksheet
165
+ #
166
+ # # define your styles
167
+ # title = ws.style.add_style(:bg_color => "FFFF0000",
168
+ # :fg_color=>"#FF000000",
169
+ # :border=>Axlsx::STYLE_THIN_BORDER,
170
+ # :alignment=>{:horizontal => :center})
171
+ #
172
+ # date_time = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_YYYYMMDDHHMMSS,
173
+ # :border=>Axlsx::STYLE_THIN_BORDER)
174
+ #
175
+ # percent = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_PERCENT,
176
+ # :border=>Axlsx::STYLE_THIN_BORDER)
177
+ #
178
+ # currency = ws.style.add_style(:format_code=>"¥#,##0;[Red]¥-#,##0",
179
+ # :border=>Axlsx::STYLE_THIN_BORDER)
180
+ #
181
+ # # build your rows
182
+ # ws.add_row :values => ["Genreated At:", Time.now], :styles=>[nil, date_time]
183
+ # ws.add_row :values => ["Previous Year Quarterly Profits (JPY)"], :style=>title
184
+ # ws.add_row :values => ["Quarter", "Profit", "% of Total"], :style=>title
185
+ # ws.add_row :values => ["Q1", 4000, 40], :style=>[title, currency, percent]
186
+ # ws.add_row :values => ["Q2", 3000, 30], :style=>[title, currency, percent]
187
+ # ws.add_row :values => ["Q3", 1000, 10], :style=>[title, currency, percent]
188
+ # ws.add_row :values => ["Q4", 2000, 20], :style=>[title, currency, percent]
189
+ # f = File.open('example_you_got_style.xlsx', 'w')
190
+ # p.serialize(f)
191
+ def add_style(options={})
192
+
193
+ numFmtId = if options[:format_code]
194
+ n = @numFmts.map{ |f| f.numFmtId }.max + 1
195
+ numFmts << NumFmt.new(:numFmtId => n, :formatCode=> options[:format_code])
196
+ n
197
+ else
198
+ options[:num_fmt] || 0
199
+ end
200
+
201
+ borderId = options[:border] || 0
202
+ raise ArgumentError, "Invalid borderId" unless borderId < borders.size
203
+
204
+ fill = if options[:bg_color]
205
+ color = Color.new(:rgb=>options[:bg_color])
206
+ pattern = PatternFill.new(:patternType =>:solid, :fgColor=>color)
207
+ fills << Fill.new(pattern)
208
+ else
209
+ 0
210
+ end
211
+
212
+ fontId = if (options.values_at(:fg_color, :sz, :b, :i, :strike, :outline, :shadow, :charset, :family, :font_name).length)
213
+ font = Font.new()
214
+ [:b, :i, :strike, :outline, :shadow, :charset, :family, :sz].each { |k| font.send("#{k}=", options[k]) unless options[k].nil? }
215
+ font.color = Color.new(:rgb => options[:fg_color]) unless options[:fg_color].nil?
216
+ font.name = options[:font_name] unless options[:font_name].nil?
217
+ fonts << font
218
+ else
219
+ 0
220
+ end
221
+
222
+ applyProtection = (options[:hidden] || options[:locked]) ? 1 : 0
223
+
224
+ xf = Xf.new(:fillId => fill, :fontId=>fontId, :applyFill=>1, :applyFont=>1, :numFmtId=>numFmtId, :borderId=>borderId, :applyProtection=>applyProtection)
225
+
226
+ if options[:alignment]
227
+ xf.alignment = CellAlignment.new(options[:alignment])
228
+ end
229
+
230
+ if applyProtection
231
+ xf.protection = CellProtection.new(options)
232
+ end
233
+
234
+ cellXfs << xf
235
+ end
236
+
237
+ # Serializes the styles document
238
+ # @return [String]
239
+ def to_xml()
240
+ builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
241
+ xml.styleSheet(:xmlns => XML_NS) {
242
+ [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :dxfs, :tableStyles].each do |key|
243
+ self.instance_values[key.to_s].to_xml(xml)
244
+ end
245
+ }
246
+ end
247
+ builder.to_xml
248
+ end
249
+
250
+ private
251
+ # Creates the default set of styles the exel requires to be valid as well as setting up the
252
+ # Axlsx::STYLE_THIN_BORDER
253
+ def load_default_styles
254
+ @numFmts = SimpleTypedList.new NumFmt
255
+ @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDD, :formatCode=> "yyyy/mm/dd")
256
+ @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDDHHMMSS, :formatCode=> "yyyy/mm/dd hh:mm:ss")
257
+
258
+ @numFmts.lock
259
+
260
+ @fonts = SimpleTypedList.new Font
261
+ @fonts << Font.new(:name => "Arial", :sz => 11, :family=>1)
262
+ @fonts.lock
263
+
264
+ @fills = SimpleTypedList.new Fill
265
+ @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:none))
266
+ @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:gray125))
267
+ @fills.lock
268
+
269
+ @borders = SimpleTypedList.new Border
270
+ @borders << Border.new
271
+ black_border = Border.new
272
+ [:left, :right, :top, :bottom].each do |item|
273
+ black_border.prs << BorderPr.new(:name=>item, :style=>:thin, :color=>Color.new(:rgb=>"FF000000"))
274
+ end
275
+ @borders << black_border
276
+ @borders.lock
277
+
278
+ @cellStyleXfs = SimpleTypedList.new Xf, "cellStyleXfs"
279
+ @cellStyleXfs << Xf.new(:borderId=>0, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
280
+ @cellStyleXfs.lock
281
+
282
+ @cellStyles = SimpleTypedList.new CellStyle
283
+ @cellStyles << CellStyle.new(:name =>"標準", :builtinId =>0, :xfId=>0)
284
+ @cellStyles.lock
285
+
286
+ @cellXfs = SimpleTypedList.new Xf, "cellXfs"
287
+ @cellXfs << Xf.new(:borderId=>0, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
288
+ @cellXfs << Xf.new(:borderId=>1, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
289
+ @cellXfs.lock
290
+
291
+ @dxfs = SimpleTypedList.new(Xf, "dxfs"); @dxfs.lock
292
+ @tableStyles = TableStyles.new(:defaultTableStyle => "TableStyleMedium9", :defaultPivotStyle => "PivotStyleLight16"); @tableStyles.lock
293
+ end
294
+ end
295
+ end
296
+