axlsx 1.1.0 → 1.2.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 (214) hide show
  1. data/.yardopts +5 -2
  2. data/CHANGELOG.md +78 -0
  3. data/LICENSE +1 -1
  4. data/README.md +132 -331
  5. data/Rakefile +17 -5
  6. data/examples/basic_charts.rb +54 -0
  7. data/examples/chart_colors.rb +88 -0
  8. data/examples/conditional_formatting/example_conditional_formatting.rb +72 -0
  9. data/examples/conditional_formatting/getting_barred.rb +37 -0
  10. data/examples/conditional_formatting/hitting_the_high_notes.rb +37 -0
  11. data/examples/conditional_formatting/scaled_colors.rb +39 -0
  12. data/examples/conditional_formatting/stop_and_go.rb +37 -0
  13. data/examples/data_validation.rb +50 -0
  14. data/examples/example.rb +300 -152
  15. data/examples/example.xlsx +0 -0
  16. data/examples/example_streamed.xlsx +0 -0
  17. data/examples/extractive.pdf +0 -0
  18. data/examples/extractive.rb +45 -0
  19. data/examples/no-use_autowidth.xlsx +0 -0
  20. data/examples/sample.png +0 -0
  21. data/examples/scraping_html.rb +91 -0
  22. data/examples/shared_strings_example.xlsx +0 -0
  23. data/examples/sheet_protection.rb +10 -0
  24. data/examples/sheet_view.rb +34 -0
  25. data/examples/skydrive/real_example.rb +63 -0
  26. data/examples/two_cell_anchor_image.rb +11 -0
  27. data/lib/axlsx/content_type/content_type.rb +2 -0
  28. data/lib/axlsx/content_type/default.rb +30 -15
  29. data/lib/axlsx/content_type/override.rb +29 -14
  30. data/lib/axlsx/doc_props/app.rb +161 -104
  31. data/lib/axlsx/doc_props/core.rb +9 -5
  32. data/lib/axlsx/drawing/ax_data_source.rb +26 -0
  33. data/lib/axlsx/drawing/axis.rb +106 -35
  34. data/lib/axlsx/drawing/bar_3D_chart.rb +49 -45
  35. data/lib/axlsx/drawing/bar_series.rb +25 -7
  36. data/lib/axlsx/drawing/cat_axis.rb +50 -22
  37. data/lib/axlsx/drawing/chart.rb +69 -56
  38. data/lib/axlsx/drawing/d_lbls.rb +100 -0
  39. data/lib/axlsx/drawing/drawing.rb +28 -12
  40. data/lib/axlsx/drawing/graphic_frame.rb +2 -1
  41. data/lib/axlsx/drawing/line_3D_chart.rb +3 -10
  42. data/lib/axlsx/drawing/line_series.rb +20 -4
  43. data/lib/axlsx/drawing/num_data.rb +52 -0
  44. data/lib/axlsx/drawing/num_data_source.rb +61 -0
  45. data/lib/axlsx/drawing/num_val.rb +32 -0
  46. data/lib/axlsx/drawing/pic.rb +44 -4
  47. data/lib/axlsx/drawing/pie_3D_chart.rb +10 -7
  48. data/lib/axlsx/drawing/pie_series.rb +18 -4
  49. data/lib/axlsx/drawing/scatter_chart.rb +12 -18
  50. data/lib/axlsx/drawing/scatter_series.rb +28 -2
  51. data/lib/axlsx/drawing/ser_axis.rb +1 -1
  52. data/lib/axlsx/drawing/series.rb +1 -1
  53. data/lib/axlsx/drawing/str_data.rb +42 -0
  54. data/lib/axlsx/drawing/str_val.rb +33 -0
  55. data/lib/axlsx/drawing/title.rb +21 -9
  56. data/lib/axlsx/drawing/two_cell_anchor.rb +46 -9
  57. data/lib/axlsx/drawing/view_3D.rb +44 -31
  58. data/lib/axlsx/drawing/vml_drawing.rb +42 -0
  59. data/lib/axlsx/drawing/vml_shape.rb +125 -0
  60. data/lib/axlsx/package.rb +44 -16
  61. data/lib/axlsx/rels/relationship.rb +3 -3
  62. data/lib/axlsx/rels/relationships.rb +1 -11
  63. data/lib/axlsx/stylesheet/color.rb +0 -1
  64. data/lib/axlsx/stylesheet/dxf.rb +79 -0
  65. data/lib/axlsx/stylesheet/font.rb +2 -1
  66. data/lib/axlsx/stylesheet/gradient_fill.rb +1 -1
  67. data/lib/axlsx/stylesheet/styles.rb +143 -50
  68. data/lib/axlsx/stylesheet/table_style.rb +3 -3
  69. data/lib/axlsx/stylesheet/xf.rb +2 -0
  70. data/lib/axlsx/util/constants.rb +36 -0
  71. data/lib/axlsx/util/simple_typed_list.rb +15 -21
  72. data/lib/axlsx/util/validators.rb +122 -5
  73. data/lib/axlsx/version.rb +2 -7
  74. data/lib/axlsx/workbook/defined_name.rb +174 -0
  75. data/lib/axlsx/workbook/defined_names.rb +21 -0
  76. data/lib/axlsx/workbook/shared_strings_table.rb +12 -13
  77. data/lib/axlsx/workbook/workbook.rb +71 -13
  78. data/lib/axlsx/workbook/worksheet/auto_filter.rb +34 -0
  79. data/lib/axlsx/workbook/worksheet/cell.rb +45 -36
  80. data/lib/axlsx/workbook/worksheet/cfvo.rb +62 -0
  81. data/lib/axlsx/workbook/worksheet/col.rb +17 -1
  82. data/lib/axlsx/workbook/worksheet/color_scale.rb +76 -0
  83. data/lib/axlsx/workbook/worksheet/cols.rb +20 -0
  84. data/lib/axlsx/workbook/worksheet/comment.rb +101 -0
  85. data/lib/axlsx/workbook/worksheet/comments.rb +85 -0
  86. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +82 -0
  87. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +216 -0
  88. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +25 -0
  89. data/lib/axlsx/workbook/worksheet/data_bar.rb +97 -0
  90. data/lib/axlsx/workbook/worksheet/data_validation.rb +245 -0
  91. data/lib/axlsx/workbook/worksheet/data_validations.rb +28 -0
  92. data/lib/axlsx/workbook/worksheet/dimension.rb +65 -0
  93. data/lib/axlsx/workbook/worksheet/icon_set.rb +83 -0
  94. data/lib/axlsx/workbook/worksheet/merged_cells.rb +35 -0
  95. data/lib/axlsx/workbook/worksheet/page_setup.rb +113 -0
  96. data/lib/axlsx/workbook/worksheet/pane.rb +144 -0
  97. data/lib/axlsx/workbook/worksheet/print_options.rb +63 -0
  98. data/lib/axlsx/workbook/worksheet/protected_range.rb +46 -0
  99. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +34 -0
  100. data/lib/axlsx/workbook/worksheet/row.rb +69 -12
  101. data/lib/axlsx/workbook/worksheet/selection.rb +111 -0
  102. data/lib/axlsx/workbook/worksheet/sheet_data.rb +25 -0
  103. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +24 -0
  104. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +224 -0
  105. data/lib/axlsx/workbook/worksheet/sheet_view.rb +379 -0
  106. data/lib/axlsx/workbook/worksheet/table.rb +3 -3
  107. data/lib/axlsx/workbook/worksheet/tables.rb +31 -0
  108. data/lib/axlsx/workbook/worksheet/worksheet.rb +379 -233
  109. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +57 -0
  110. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +64 -0
  111. data/lib/axlsx.rb +31 -16
  112. data/test/benchmark.rb +30 -38
  113. data/test/content_type/tc_content_type.rb +1 -6
  114. data/test/doc_props/tc_app.rb +31 -1
  115. data/test/doc_props/tc_core.rb +3 -1
  116. data/test/drawing/tc_axis.rb +46 -3
  117. data/test/drawing/tc_bar_3D_chart.rb +12 -12
  118. data/test/drawing/tc_bar_series.rb +17 -13
  119. data/test/drawing/tc_cat_axis.rb +3 -3
  120. data/test/drawing/tc_cat_axis_data.rb +23 -13
  121. data/test/drawing/tc_chart.rb +29 -11
  122. data/test/drawing/tc_d_lbls.rb +47 -0
  123. data/test/drawing/tc_data_source.rb +17 -0
  124. data/test/drawing/tc_drawing.rb +11 -5
  125. data/test/drawing/tc_hyperlink.rb +4 -5
  126. data/test/drawing/tc_line_series.rb +9 -11
  127. data/test/drawing/tc_named_axis_data.rb +27 -0
  128. data/test/drawing/tc_num_data.rb +31 -0
  129. data/test/drawing/tc_num_val.rb +29 -0
  130. data/test/drawing/tc_pic.rb +22 -2
  131. data/test/drawing/tc_pie_3D_chart.rb +4 -8
  132. data/test/drawing/tc_pie_series.rb +11 -13
  133. data/test/drawing/tc_scatter_chart.rb +6 -1
  134. data/test/drawing/tc_scatter_series.rb +6 -5
  135. data/test/drawing/tc_series.rb +1 -1
  136. data/test/drawing/tc_str_data.rb +18 -0
  137. data/test/drawing/tc_str_val.rb +21 -0
  138. data/test/drawing/tc_title.rb +16 -0
  139. data/test/drawing/tc_two_cell_anchor.rb +2 -3
  140. data/test/drawing/tc_view_3D.rb +18 -18
  141. data/test/drawing/tc_vml_drawing.rb +25 -0
  142. data/test/drawing/tc_vml_shape.rb +100 -0
  143. data/test/profile.rb +2 -1
  144. data/test/rels/tc_relationship.rb +1 -0
  145. data/test/stylesheet/tc_dxf.rb +81 -0
  146. data/test/stylesheet/tc_gradient_fill.rb +8 -0
  147. data/test/stylesheet/tc_pattern_fill.rb +7 -1
  148. data/test/stylesheet/tc_styles.rb +163 -3
  149. data/test/stylesheet/tc_table_style.rb +8 -0
  150. data/test/stylesheet/tc_table_style_element.rb +10 -1
  151. data/test/tc_helper.rb +1 -0
  152. data/test/tc_package.rb +93 -17
  153. data/test/util/tc_simple_typed_list.rb +13 -0
  154. data/test/util/tc_validators.rb +95 -9
  155. data/test/workbook/tc_defined_name.rb +41 -0
  156. data/test/workbook/tc_workbook.rb +5 -3
  157. data/test/workbook/worksheet/table/tc_table.rb +3 -11
  158. data/test/workbook/worksheet/tc_cell.rb +52 -18
  159. data/test/workbook/worksheet/tc_cfvo.rb +31 -0
  160. data/test/workbook/worksheet/tc_col.rb +9 -0
  161. data/test/workbook/worksheet/tc_color_scale.rb +29 -0
  162. data/test/workbook/worksheet/tc_comment.rb +57 -0
  163. data/test/workbook/worksheet/tc_comments.rb +57 -0
  164. data/test/workbook/worksheet/tc_conditional_formatting.rb +217 -0
  165. data/test/workbook/worksheet/tc_data_bar.rb +39 -0
  166. data/test/workbook/worksheet/tc_data_validation.rb +265 -0
  167. data/test/workbook/worksheet/tc_date_time_converter.rb +11 -5
  168. data/test/workbook/worksheet/tc_icon_set.rb +45 -0
  169. data/test/workbook/worksheet/tc_page_setup.rb +138 -0
  170. data/test/workbook/worksheet/tc_pane.rb +94 -0
  171. data/test/workbook/worksheet/tc_print_options.rb +72 -0
  172. data/test/workbook/worksheet/tc_protected_range.rb +17 -0
  173. data/test/workbook/worksheet/tc_row.rb +44 -13
  174. data/test/workbook/worksheet/tc_selection.rb +94 -0
  175. data/test/workbook/worksheet/tc_sheet_protection.rb +117 -0
  176. data/test/workbook/worksheet/tc_sheet_view.rb +214 -0
  177. data/test/workbook/worksheet/tc_worksheet.rb +147 -50
  178. metadata +216 -148
  179. data/examples/example.csv +0 -1000
  180. data/examples/example.rb~ +0 -112
  181. data/lib/axlsx/drawing/cat_axis_data.rb +0 -34
  182. data/lib/axlsx/drawing/hlink_click.rb~ +0 -0
  183. data/lib/axlsx/drawing/hyperlink.rb~ +0 -64
  184. data/lib/axlsx/drawing/named_axis_data.rb +0 -36
  185. data/lib/axlsx/drawing/picture_locking.rb~ +0 -36
  186. data/lib/axlsx/drawing/val_axis_data.rb +0 -27
  187. data/lib/axlsx/stylesheet/#num_fmt.rb# +0 -69
  188. data/lib/axlsx/util/cbf.rb +0 -333
  189. data/lib/axlsx/util/cfb.rb~ +0 -201
  190. data/lib/axlsx/util/ms_off_crypto.rb +0 -189
  191. data/lib/axlsx/util/ms_off_crypto.rb~ +0 -3
  192. data/lib/axlsx/util/ms_offcrypto.rb~ +0 -0
  193. data/lib/axlsx/util/parser.rb~ +0 -6
  194. data/lib/axlsx/util/storage.rb~ +0 -0
  195. data/lib/axlsx/workbook/shared_strings_table.rb~ +0 -69
  196. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  197. data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
  198. data/lib/axlsx/workbook/worksheet/table.rb~ +0 -97
  199. data/lib/schema/dc.xsd~ +0 -118
  200. data/lib/schema/dcterms.xsd~ +0 -331
  201. data/lib/schema/opc-coreProperties.xsd~ +0 -50
  202. data/test/#benchmark.txt# +0 -7
  203. data/test/#tc_helper.rb# +0 -3
  204. data/test/benchmark.rb~ +0 -0
  205. data/test/benchmark.txt +0 -6
  206. data/test/benchmark.txt~ +0 -6
  207. data/test/drawing/tc_val_axis_data.rb +0 -17
  208. data/test/example.csv +0 -1000
  209. data/test/example.xlsx +0 -0
  210. data/test/example_streamed.xlsx +0 -0
  211. data/test/tc_axlsx.rb~ +0 -0
  212. data/test/tc_helper.rb~ +0 -3
  213. data/test/workbook/worksheet/table/tc_table.rb~ +0 -72
  214. data/test/workbook/worksheet/tc_col.rb~ +0 -10
@@ -0,0 +1,216 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Axlsx
3
+ # Conditional formatting rules specify formulas whose evaluations
4
+ # format cells
5
+ #
6
+ # @note The recommended way to manage these rules is via Worksheet#add_conditional_formatting
7
+ # @see Worksheet#add_conditional_formatting
8
+ # @see ConditionalFormattingRule#initialize
9
+ class ConditionalFormattingRule
10
+
11
+ # instance values that must be serialized as their own elements - e.g. not attributes.
12
+ CHILD_ELEMENTS = [:formula, :color_scale, :data_bar, :icon_set]
13
+
14
+ # Formula
15
+ # @return [String]
16
+ attr_reader :formula
17
+
18
+ # Type (ST_CfType)
19
+ # options are expression, cellIs, colorScale, dataBar, iconSet,
20
+ # top10, uniqueValues, duplicateValues, containsText,
21
+ # notContainsText, beginsWith, endsWith, containsBlanks,
22
+ # notContainsBlanks, containsErrors, notContainsErrors,
23
+ # timePeriod, aboveAverage
24
+ # @return [Symbol]
25
+ attr_reader :type
26
+
27
+ # Above average rule
28
+ # Indicates whether the rule is an "above average" rule. True
29
+ # indicates 'above average'. This attribute is ignored if type is
30
+ # not equal to aboveAverage.
31
+ # @return [Boolean]
32
+ attr_reader :aboveAverage
33
+
34
+ # Bottom N rule
35
+ # @return [Boolean]
36
+ attr_reader :bottom
37
+
38
+ # Differential Formatting Id
39
+ # @return [Integer]
40
+ attr_reader :dxfId
41
+
42
+ # Equal Average
43
+ # Flag indicating whether the 'aboveAverage' and 'belowAverage'
44
+ # criteria is inclusive of the average itself, or exclusive of
45
+ # that value.
46
+ # @return [Boolean]
47
+ attr_reader :equalAverage
48
+
49
+ # Operator
50
+ # The operator in a "cell value is" conditional formatting
51
+ # rule. This attribute is ignored if type is not equal to cellIs
52
+ #
53
+ # Operator must be one of lessThan, lessThanOrEqual, equal,
54
+ # notEqual, greaterThanOrEqual, greaterThan, between, notBetween,
55
+ # containsText, notContains, beginsWith, endsWith
56
+ # @return [Symbol]
57
+ attr_reader :operator
58
+
59
+ # Priority
60
+ # The priority of this conditional formatting rule. This value is
61
+ # used to determine which format should be evaluated and
62
+ # rendered. Lower numeric values are higher priority than higher
63
+ # numeric values, where '1' is the highest priority.
64
+ # @return [Integer]
65
+ attr_reader :priority
66
+
67
+ # Text
68
+ # used in a "text contains" conditional formatting
69
+ # rule.
70
+ # @return [String]
71
+ attr_reader :text
72
+
73
+ # percent (Top 10 Percent)
74
+ # indicates whether a "top/bottom n" rule is a "top/bottom n
75
+ # percent" rule. This attribute is ignored if type is not equal to
76
+ # top10.
77
+ # @return [Boolean]
78
+ attr_reader :percent
79
+
80
+ # rank (Rank)
81
+ # The value of "n" in a "top/bottom n" conditional formatting
82
+ # rule. This attribute is ignored if type is not equal to top10.
83
+ # @return [Integer]
84
+ attr_reader :rank
85
+
86
+ # stdDev (StdDev)
87
+ # The number of standard deviations to include above or below the
88
+ # average in the conditional formatting rule. This attribute is
89
+ # ignored if type is not equal to aboveAverage. If a value is
90
+ # present for stdDev and the rule type = aboveAverage, then this
91
+ # rule is automatically an "above or below N standard deviations"
92
+ # rule.
93
+ # @return [Integer]
94
+ attr_reader :stdDev
95
+
96
+ # stopIfTrue (Stop If True)
97
+ # If this flag is '1', no rules with lower priority shall be
98
+ # applied over this rule, when this rule evaluates to true.
99
+ # @return [Boolean]
100
+ attr_reader :stopIfTrue
101
+
102
+ # timePeriod (Time Period)
103
+ # The applicable time period in a "date occurring…" conditional
104
+ # formatting rule. This attribute is ignored if type is not equal
105
+ # to timePeriod.
106
+ # Valid types are today, yesterday, tomorrow, last7Days,
107
+ # thisMonth, lastMonth, nextMonth, thisWeek, lastWeek, nextWeek
108
+ attr_reader :timePeriod
109
+
110
+
111
+ # colorScale (Color Scale)
112
+ # The color scale to apply to this conditional formatting
113
+ # @return [ColorScale]
114
+ def color_scale
115
+ @color_scale ||= ColorScale.new
116
+ end
117
+
118
+ # dataBar (Data Bar)
119
+ # The data bar to apply to this conditional formatting
120
+ # @return [DataBar]
121
+ def data_bar
122
+ @data_bar ||= DataBar.new
123
+ end
124
+
125
+ # iconSet (Icon Set)
126
+ # The icon set to apply to this conditional formatting
127
+ # @return [IconSet]
128
+ def icon_set
129
+ @icon_set ||= IconSet.new
130
+ end
131
+
132
+ # Creates a new Conditional Formatting Rule object
133
+ # @option options [Symbol] type The type of this formatting rule
134
+ # @option options [Boolean] aboveAverage This is an aboveAverage rule
135
+ # @option options [Boolean] bottom This is a bottom N rule.
136
+ # @option options [Integer] dxfId The formatting id to apply to matches
137
+ # @option options [Boolean] equalAverage Is the aboveAverage or belowAverage rule inclusive
138
+ # @option options [Integer] priority The priority of the rule, 1 is highest
139
+ # @option options [Symbol] operator Which operator to apply
140
+ # @option options [String] text The value to apply a text operator against
141
+ # @option options [Boolean] percent If a top/bottom N rule, evaluate as N% rather than N
142
+ # @option options [Integer] rank If a top/bottom N rule, the value of N
143
+ # @option options [Integer] stdDev The number of standard deviations above or below the average to match
144
+ # @option options [Boolean] stopIfTrue Stop evaluating rules after this rule matches
145
+ # @option options [Symbol] timePeriod The time period in a date occuring... rule
146
+ # @option options [String] formula The formula to match against in i.e. an equal rule
147
+ def initialize(options={})
148
+ @color_scale = @data_bar = @icon_set = @formula = nil
149
+ options.each do |o|
150
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
151
+ end
152
+ end
153
+
154
+ # @see type
155
+ def type=(v); Axlsx::validate_conditional_formatting_type(v); @type = v end
156
+ # @see aboveAverage
157
+ def aboveAverage=(v); Axlsx::validate_boolean(v); @aboveAverage = v end
158
+ # @see bottom
159
+ def bottom=(v); Axlsx::validate_boolean(v); @bottom = v end
160
+ # @see dxfId
161
+ def dxfId=(v); Axlsx::validate_unsigned_numeric(v); @dxfId = v end
162
+ # @see equalAverage
163
+ def equalAverage=(v); Axlsx::validate_boolean(v); @equalAverage = v end
164
+ # @see priority
165
+ def priority=(v); Axlsx::validate_unsigned_numeric(v); @priority = v end
166
+ # @see operator
167
+ def operator=(v); Axlsx::validate_conditional_formatting_operator(v); @operator = v end
168
+ # @see text
169
+ def text=(v); Axlsx::validate_string(v); @text = v end
170
+ # @see percent
171
+ def percent=(v); Axlsx::validate_boolean(v); @percent = v end
172
+ # @see rank
173
+ def rank=(v); Axlsx::validate_unsigned_numeric(v); @rank = v end
174
+ # @see stdDev
175
+ def stdDev=(v); Axlsx::validate_unsigned_numeric(v); @stdDev = v end
176
+ # @see stopIfTrue
177
+ def stopIfTrue=(v); Axlsx::validate_boolean(v); @stopIfTrue = v end
178
+ # @see timePeriod
179
+ def timePeriod=(v); Axlsx::validate_time_period_type(v); @timePeriod = v end
180
+ # @see formula
181
+ def formula=(v); Axlsx::validate_string(v); @formula = v end
182
+
183
+ # @see color_scale
184
+ def color_scale=(v)
185
+ Axlsx::DataTypeValidator.validate 'conditional_formatting_rule.color_scale', ColorScale, v
186
+ @color_scale = v
187
+ end
188
+
189
+ # @see data_bar
190
+ def data_bar=(v)
191
+ Axlsx::DataTypeValidator.validate 'conditional_formatting_rule.data_bar', DataBar, v
192
+ @data_bar = v
193
+ end
194
+
195
+ # @see icon_set
196
+ def icon_set=(v)
197
+ Axlsx::DataTypeValidator.validate 'conditional_formatting_rule.icon_set', IconSet, v
198
+ @icon_set = v
199
+ end
200
+
201
+
202
+ # Serializes the conditional formatting rule
203
+ # @param [String] str
204
+ # @return [String]
205
+ def to_xml_string(str = '')
206
+ str << '<cfRule '
207
+ str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' unless CHILD_ELEMENTS.include?(key.to_sym) }.join(' ')
208
+ str << '>'
209
+ str << '<formula>' << self.formula << '</formula>' if @formula
210
+ @color_scale.to_xml_string(str) if @color_scale && @type == :colorScale
211
+ @data_bar.to_xml_string(str) if @data_bar && @type == :dataBar
212
+ @icon_set.to_xml_string(str) if @icon_set && @type == :iconSet
213
+ str << '</cfRule>'
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,25 @@
1
+ module Axlsx
2
+
3
+ # A simple, self serializing class for storing conditional formattings
4
+ class ConditionalFormattings < SimpleTypedList
5
+
6
+ # creates a new Tables object
7
+ def initialize(worksheet)
8
+ raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
9
+ super ConditionalFormatting
10
+ @worksheet = worksheet
11
+ end
12
+
13
+ # The worksheet that owns this collection of tables
14
+ # @return [Worksheet]
15
+ attr_reader :worksheet
16
+
17
+ # serialize the conditional formattings
18
+ def to_xml_string(str = "")
19
+ return if empty?
20
+ each { |item| item.to_xml_string(str) }
21
+ end
22
+ end
23
+
24
+ end
25
+
@@ -0,0 +1,97 @@
1
+ module Axlsx
2
+ # Conditional Format Rule data bar object
3
+ # Describes a data bar conditional formatting rule.
4
+
5
+ # @note The recommended way to manage these rules is via Worksheet#add_conditional_formatting
6
+ # @see Worksheet#add_conditional_formatting
7
+ # @see ConditionalFormattingRule#initialize
8
+ class DataBar
9
+
10
+ # instance values that must be serialized as their own elements - e.g. not attributes.
11
+ CHILD_ELEMENTS = [:value_objects, :color]
12
+
13
+ # minLength attribute
14
+ # The minimum length of the data bar, as a percentage of the cell width.
15
+ # The default value is 10
16
+ # @return [Integer]
17
+ attr_reader :minLength
18
+
19
+ # maxLength attribute
20
+ # The maximum length of the data bar, as a percentage of the cell width.
21
+ # The default value is 90
22
+ # @return [Integer]
23
+ attr_reader :maxLength
24
+
25
+ # maxLength attribute
26
+ # Indicates whether to show the values of the cells on which this data bar is applied.
27
+ # The default value is true
28
+ # @return [Boolean]
29
+ attr_reader :showValue
30
+
31
+ # A simple typed list of cfvos
32
+ # @return [SimpleTypedList]
33
+ # @see Cfvo
34
+ attr_reader :value_objects
35
+
36
+ # color
37
+ # the color object used in the data bar formatting
38
+ # @return [Color]
39
+ def color
40
+ @color ||= Color.new :rgb => "FF0000FF"
41
+ end
42
+
43
+ # Creates a new data bar conditional formatting object
44
+ # @option options [Integer] minLength
45
+ # @option options [Integer] maxLength
46
+ # @option options [Boolean] showValue
47
+ # @option options [String] color - the rbg value used to color the bars
48
+ def initialize(options = {})
49
+ @minLength = 10
50
+ @maxLength = 90
51
+ @showValue = true
52
+ initialize_value_objects
53
+ options.each do |o|
54
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
55
+ end
56
+ yield self if block_given?
57
+ end
58
+
59
+ # @see minLength
60
+ def minLength=(v); Axlsx.validate_unsigned_int(v); @minLength = v end
61
+ # @see maxLength
62
+ def maxLength=(v); Axlsx.validate_unsigned_int(v); @maxLength = v end
63
+
64
+ # @see showValue
65
+ def showValue=(v); Axlsx.validate_boolean(v); @showValue = v end
66
+
67
+ # Sets the color for the data bars.
68
+ # @param [Color|String] v The color object, or rgb string value to apply
69
+ def color=(v)
70
+ @color = v if v.is_a? Color
71
+ self.color.rgb = v if v.is_a? String
72
+ @color
73
+ end
74
+
75
+ # Serialize this object to an xml string
76
+ # @param [String] str
77
+ # @return [String]
78
+ def to_xml_string(str="")
79
+ str << '<dataBar '
80
+ str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' unless CHILD_ELEMENTS.include?(key.to_sym) }.join(' ')
81
+ str << '>'
82
+ @value_objects.each { |cfvo| cfvo.to_xml_string(str) }
83
+ self.color.to_xml_string(str)
84
+ str << '</dataBar>'
85
+ end
86
+
87
+ private
88
+
89
+ # Initalize the simple typed list of value objects
90
+ # I am keeping this private for now as I am not sure what impact changes to the required two cfvo objects will do.
91
+ def initialize_value_objects
92
+ @value_objects = SimpleTypedList.new Cfvo
93
+ @value_objects.concat [Cfvo.new(:type => :min, :val => 0), Cfvo.new(:type => :max, :val => 0)]
94
+ @value_objects.lock
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,245 @@
1
+ # encoding: UTF-8
2
+ module Axlsx
3
+ # Data validation allows the validation of cell data
4
+ #
5
+ # @note The recommended way to manage data validations is via Worksheet#add_data_validation
6
+ # @see Worksheet#add_data_validation
7
+ class DataValidation
8
+
9
+ # instance values that must be serialized as their own elements - e.g. not attributes.
10
+ CHILD_ELEMENTS = [:formula1, :formula2]
11
+
12
+ # Formula1
13
+ # Available for type whole, decimal, date, time, textLength, list, custom
14
+ # @see type
15
+ # @return [String]
16
+ # default nil
17
+ attr_reader :formula1
18
+
19
+ # Formula2
20
+ # Available for type whole, decimal, date, time, textLength
21
+ # @see type
22
+ # @return [String]
23
+ # default nil
24
+ attr_reader :formula2
25
+
26
+ # Allow Blank
27
+ # A boolean value indicating whether the data validation allows the use of empty or blank
28
+ # entries. 1 means empty entries are OK and do not violate the validation constraints.
29
+ # Available for type whole, decimal, date, time, textLength, list, custom
30
+ # @see type
31
+ # @return [Boolean]
32
+ # default true
33
+ attr_reader :allowBlank
34
+
35
+ # Error Message
36
+ # Message text of error alert.
37
+ # Available for type whole, decimal, date, time, textLength, list, custom
38
+ # @see type
39
+ # @return [String]
40
+ # default nil
41
+ attr_reader :error
42
+
43
+ # Error Style (ST_DataValidationErrorStyle)
44
+ # The style of error alert used for this data validation.
45
+ # Options are:
46
+ # * information: This data validation error style uses an information icon in the error alert.
47
+ # * stop: This data validation error style uses a stop icon in the error alert.
48
+ # * warning: This data validation error style uses a warning icon in the error alert.
49
+ # Available for type whole, decimal, date, time, textLength, list, custom
50
+ # @see type
51
+ # @return [Symbol]
52
+ # default :stop
53
+ attr_reader :errorStyle
54
+
55
+ # Error Title
56
+ # Title bar text of error alert.
57
+ # Available for type whole, decimal, date, time, textLength, list, custom
58
+ # @see type
59
+ # @return [String]
60
+ # default nil
61
+ attr_reader :errorTitle
62
+
63
+ # Operator (ST_DataValidationOperator)
64
+ # The relational operator used with this data validation.
65
+ # Options are:
66
+ # * between: Data validation which checks if a value is between two other values.
67
+ # * equal: Data validation which checks if a value is equal to a specified value.
68
+ # * greater_than: Data validation which checks if a value is greater than a specified value.
69
+ # * greater_than_or_equal: Data validation which checks if a value is greater than or equal to a specified value.
70
+ # * less_than: Data validation which checks if a value is less than a specified value.
71
+ # * less_than_or_equal: Data validation which checks if a value is less than or equal to a specified value.
72
+ # * not_between: Data validation which checks if a value is not between two other values.
73
+ # * not_equal: Data validation which checks if a value is not equal to a specified value.
74
+ # Available for type whole, decimal, date, time, textLength
75
+ # @see type
76
+ # @return [Symbol]
77
+ # default nil
78
+ attr_reader :operator
79
+
80
+ # Input prompt
81
+ # Message text of input prompt.
82
+ # Available for type whole, decimal, date, time, textLength, list, custom
83
+ # @see type
84
+ # @return [String]
85
+ # default nil
86
+ attr_reader :prompt
87
+
88
+ # Prompt title
89
+ # Title bar text of input prompt.
90
+ # Available for type whole, decimal, date, time, textLength, list, custom
91
+ # @see type
92
+ # @return [String]
93
+ # default nil
94
+ attr_reader :promptTitle
95
+
96
+ # Show drop down
97
+ # A boolean value indicating whether to display a dropdown combo box for a list type data
98
+ # validation. Be careful: false shows the dropdown list!
99
+ # Available for type list
100
+ # @see type
101
+ # @return [Boolean]
102
+ # default false
103
+ attr_reader :showDropDown
104
+
105
+ # Show error message
106
+ # A boolean value indicating whether to display the error alert message when an invalid
107
+ # value has been entered, according to the criteria specified.
108
+ # Available for type whole, decimal, date, time, textLength, list, custom
109
+ # @see type
110
+ # @return [Boolean]
111
+ # default false
112
+ attr_reader :showErrorMessage
113
+
114
+ # Show input message
115
+ # A boolean value indicating whether to display the input prompt message.
116
+ # Available for type whole, decimal, date, time, textLength, list, custom
117
+ # @see type
118
+ # @return [Boolean]
119
+ # default false
120
+ attr_reader :showInputMessage
121
+
122
+ # Range over which data validation is applied, in "A1:B2" format
123
+ # Available for type whole, decimal, date, time, textLength, list, custom
124
+ # @see type
125
+ # @return [String]
126
+ # default nil
127
+ attr_reader :sqref
128
+
129
+ # The type (ST_DataValidationType) of data validation.
130
+ # Options are:
131
+ # * custom: Data validation which uses a custom formula to check the cell value.
132
+ # * date: Data validation which checks for date values satisfying the given condition.
133
+ # * decimal: Data validation which checks for decimal values satisfying the given condition.
134
+ # * list: Data validation which checks for a value matching one of list of values.
135
+ # * none: No data validation.
136
+ # * textLength: Data validation which checks for text values, whose length satisfies the given condition.
137
+ # * time: Data validation which checks for time values satisfying the given condition.
138
+ # * whole: Data validation which checks for whole number values satisfying the given condition.
139
+ # @return [Symbol]
140
+ # default none
141
+ attr_reader :type
142
+
143
+ # Creates a new {DataValidation} object
144
+ # @option options [String] formula1
145
+ # @option options [String] formula2
146
+ # @option options [Boolean] allowBlank - A boolean value indicating whether the data validation allows the use of empty or blank entries.
147
+ # @option options [String] error - Message text of error alert.
148
+ # @option options [Symbol] errorStyle - The style of error alert used for this data validation.
149
+ # @option options [String] errorTitle - itle bar text of error alert.
150
+ # @option options [Symbol] operator - The relational operator used with this data validation.
151
+ # @option options [String] prompt - Message text of input prompt.
152
+ # @option options [String] promptTitle - Title bar text of input prompt.
153
+ # @option options [Boolean] showDropDown - A boolean value indicating whether to display a dropdown combo box for a list type data validation
154
+ # @option options [Boolean] showErrorMessage - A boolean value indicating whether to display the error alert message when an invalid value has been entered, according to the criteria specified.
155
+ # @option options [Boolean] showInputMessage - A boolean value indicating whether to display the input prompt message.
156
+ # @option options [String] sqref - Range over which data validation is applied, in "A1:B2" format.
157
+ # @option options [Symbol] type - The type of data validation.
158
+ def initialize(options={})
159
+ # defaults
160
+ @formula1 = @formula2 = @error = @errorTitle = @operator = @prompt = @promptTitle = @sqref = nil
161
+ @allowBlank = @showErrorMessage = true
162
+ @showDropDown = @showInputMessage = false
163
+ @type = :none
164
+ @errorStyle = :stop
165
+
166
+ options.each do |o|
167
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
168
+ end
169
+ end
170
+
171
+ # @see formula1
172
+ def formula1=(v); Axlsx::validate_string(v); @formula1 = v end
173
+
174
+ # @see formula2
175
+ def formula2=(v); Axlsx::validate_string(v); @formula2 = v end
176
+
177
+ # @see allowBlank
178
+ def allowBlank=(v); Axlsx::validate_boolean(v); @allowBlank = v end
179
+
180
+ # @see error
181
+ def error=(v); Axlsx::validate_string(v); @error = v end
182
+
183
+ # @see errorStyle
184
+ def errorStyle=(v); Axlsx::validate_data_validation_error_style(v); @errorStyle = v end
185
+
186
+ # @see errorTitle
187
+ def errorTitle=(v); Axlsx::validate_string(v); @errorTitle = v end
188
+
189
+ # @see operator
190
+ def operator=(v); Axlsx::validate_data_validation_operator(v); @operator = v end
191
+
192
+ # @see prompt
193
+ def prompt=(v); Axlsx::validate_string(v); @prompt = v end
194
+
195
+ # @see promptTitle
196
+ def promptTitle=(v); Axlsx::validate_string(v); @promptTitle = v end
197
+
198
+ # @see showDropDown
199
+ def showDropDown=(v); Axlsx::validate_boolean(v); @showDropDown = v end
200
+
201
+ # @see showErrorMessage
202
+ def showErrorMessage=(v); Axlsx::validate_boolean(v); @showErrorMessage = v end
203
+
204
+ # @see showInputMessage
205
+ def showInputMessage=(v); Axlsx::validate_boolean(v); @showInputMessage = v end
206
+
207
+ # @see sqref
208
+ def sqref=(v); Axlsx::validate_string(v); @sqref = v end
209
+
210
+ # @see type
211
+ def type=(v); Axlsx::validate_data_validation_type(v); @type = v end
212
+
213
+ # Serializes the data validation
214
+ # @param [String] str
215
+ # @return [String]
216
+ def to_xml_string(str = '')
217
+ valid_attributes = get_valid_attributes
218
+
219
+ str << '<dataValidation '
220
+ str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' if (valid_attributes.include?(key.to_sym) and not CHILD_ELEMENTS.include?(key.to_sym)) }.join(' ')
221
+ str << '>'
222
+ str << '<formula1>' << self.formula1 << '</formula1>' if @formula1 and valid_attributes.include?(:formula1)
223
+ str << '<formula2>' << self.formula2 << '</formula2>' if @formula2 and valid_attributes.include?(:formula2)
224
+ str << '</dataValidation>'
225
+ end
226
+
227
+ private
228
+ def get_valid_attributes
229
+ attributes = [:allowBlank, :error, :errorStyle, :errorTitle, :prompt, :promptTitle, :showErrorMessage, :showInputMessage, :sqref, :type ]
230
+
231
+ if [:whole, :decimal, :data, :time, :textLength].include?(@type)
232
+ attributes << [:operator, :formula1]
233
+ attributes << [:formula2] if [:between, :notBetween].include?(@operator)
234
+ elsif @type == :list
235
+ attributes << [:showDropDown, :formula1]
236
+ elsif @type == :custom
237
+ attributes << [:formula1]
238
+ else
239
+ attributes = []
240
+ end
241
+
242
+ attributes.flatten!
243
+ end
244
+ end
245
+ end
@@ -0,0 +1,28 @@
1
+ module Axlsx
2
+
3
+ # A simple, self serializing class for storing conditional formattings
4
+ class DataValidations < SimpleTypedList
5
+
6
+ # creates a new Tables object
7
+ def initialize(worksheet)
8
+ raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
9
+ super DataValidation
10
+ @worksheet = worksheet
11
+ end
12
+
13
+ # The worksheet that owns this collection of tables
14
+ # @return [Worksheet]
15
+ attr_reader :worksheet
16
+
17
+ # serialize the conditional formattings
18
+ def to_xml_string(str = "")
19
+ return if empty?
20
+ str << "<dataValidations count='#{size}'>"
21
+ each { |item| item.to_xml_string(str) }
22
+ str << '</dataValidations>'
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+
@@ -0,0 +1,65 @@
1
+ module Axlsx
2
+
3
+ # This class manages the dimensions for a worksheet.
4
+ # While this node is optional in the specification some readers like
5
+ # LibraOffice require this node to render the sheet
6
+ class Dimension
7
+
8
+ # the default value for the first cell in the dimension
9
+ # @return [String]
10
+ def self.default_first
11
+ @@default_first ||= 'A1'
12
+ end
13
+
14
+ # the default value for the last cell in the dimension
15
+ # @return [String]
16
+ def self.default_last
17
+ @@default_last ||= 'AA200'
18
+ end
19
+
20
+
21
+ # Creates a new dimension object
22
+ # @param[Worksheet] worksheet - the worksheet this dimension applies
23
+ # to.
24
+ def initialize(worksheet)
25
+ raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
26
+ @worksheet = worksheet
27
+ end
28
+
29
+ attr_reader :worksheet
30
+
31
+ # the full refernece for this dimension
32
+ # @return [String]
33
+ def sqref
34
+ "#{first_cell_reference}:#{last_cell_reference}"
35
+ end
36
+
37
+ # serialize the object
38
+ # @return [String]
39
+ def to_xml_string(str = '')
40
+ return if worksheet.rows.empty?
41
+ str << "<dimension ref=\"%s\"></dimension>" % sqref
42
+ end
43
+
44
+ # The first cell in the dimension
45
+ # @return [String]
46
+ def first_cell_reference
47
+ dimension_reference(worksheet.rows.first.cells.first, Dimension.default_first)
48
+ end
49
+
50
+ # the last cell in the dimension
51
+ # @return [String]
52
+ def last_cell_reference
53
+ dimension_reference(worksheet.rows.last.cells.last, Dimension.default_last)
54
+ end
55
+
56
+ private
57
+
58
+ # Returns the reference of a cell or the default specified
59
+ # @return [String]
60
+ def dimension_reference(cell, default)
61
+ return default unless cell.respond_to?(:r)
62
+ cell.r
63
+ end
64
+ end
65
+ end