axlsx 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (207) hide show
  1. data/README.md +21 -3
  2. data/doc/Axlsx.html +150 -6
  3. data/doc/Axlsx/App.html +1 -1
  4. data/doc/Axlsx/Axis.html +1 -1
  5. data/doc/Axlsx/Bar3DChart.html +1 -1
  6. data/doc/Axlsx/BarSeries.html +1 -1
  7. data/doc/Axlsx/Border.html +1 -1
  8. data/doc/Axlsx/BorderPr.html +1 -1
  9. data/doc/Axlsx/CatAxis.html +1 -1
  10. data/doc/Axlsx/CatAxisData.html +7 -5
  11. data/doc/Axlsx/Cell.html +1 -1
  12. data/doc/Axlsx/CellAlignment.html +1 -1
  13. data/doc/Axlsx/CellProtection.html +1 -1
  14. data/doc/Axlsx/CellStyle.html +1 -1
  15. data/doc/Axlsx/Chart.html +16 -16
  16. data/doc/Axlsx/Color.html +1 -1
  17. data/doc/Axlsx/ContentType.html +1 -1
  18. data/doc/Axlsx/Core.html +1 -1
  19. data/doc/Axlsx/DataTypeValidator.html +1 -1
  20. data/doc/Axlsx/Default.html +1 -1
  21. data/doc/Axlsx/Drawing.html +243 -65
  22. data/doc/Axlsx/Fill.html +1 -1
  23. data/doc/Axlsx/Font.html +1 -1
  24. data/doc/Axlsx/GradientFill.html +1 -1
  25. data/doc/Axlsx/GradientStop.html +1 -1
  26. data/doc/Axlsx/GraphicFrame.html +21 -21
  27. data/doc/Axlsx/Line3DChart.html +1 -1
  28. data/doc/Axlsx/LineSeries.html +6 -8
  29. data/doc/Axlsx/Marker.html +1 -1
  30. data/doc/Axlsx/NumFmt.html +1 -1
  31. data/doc/Axlsx/OneCellAnchor.html +988 -0
  32. data/doc/Axlsx/Override.html +1 -1
  33. data/doc/Axlsx/Package.html +127 -75
  34. data/doc/Axlsx/PatternFill.html +1 -1
  35. data/doc/Axlsx/Pic.html +1532 -0
  36. data/doc/Axlsx/Pie3DChart.html +5 -7
  37. data/doc/Axlsx/PieSeries.html +1 -1
  38. data/doc/Axlsx/RegexValidator.html +1 -1
  39. data/doc/Axlsx/Relationship.html +6 -6
  40. data/doc/Axlsx/Relationships.html +1 -1
  41. data/doc/Axlsx/RestrictionValidator.html +1 -1
  42. data/doc/Axlsx/Row.html +1 -1
  43. data/doc/Axlsx/Scaling.html +1 -1
  44. data/doc/Axlsx/SerAxis.html +1 -1
  45. data/doc/Axlsx/Series.html +1 -1
  46. data/doc/Axlsx/SeriesTitle.html +1 -1
  47. data/doc/Axlsx/SimpleTypedList.html +1 -1
  48. data/doc/Axlsx/Styles.html +1 -1
  49. data/doc/Axlsx/TableStyle.html +1 -1
  50. data/doc/Axlsx/TableStyleElement.html +1 -1
  51. data/doc/Axlsx/TableStyles.html +1 -1
  52. data/doc/Axlsx/Title.html +1 -1
  53. data/doc/Axlsx/TwoCellAnchor.html +165 -61
  54. data/doc/Axlsx/ValAxis.html +1 -1
  55. data/doc/Axlsx/ValAxisData.html +1 -1
  56. data/doc/Axlsx/View3D.html +1 -1
  57. data/doc/Axlsx/Workbook.html +151 -47
  58. data/doc/Axlsx/Worksheet.html +172 -35
  59. data/doc/Axlsx/Xf.html +1 -1
  60. data/doc/_index.html +15 -1
  61. data/doc/class_list.html +1 -1
  62. data/doc/file.README.html +29 -4
  63. data/doc/index.html +29 -4
  64. data/doc/method_list.html +571 -363
  65. data/doc/top-level-namespace.html +1 -1
  66. data/examples/example.rb +13 -0
  67. data/examples/image1.gif +0 -0
  68. data/examples/image1.jpeg +0 -0
  69. data/examples/image1.jpg +0 -0
  70. data/examples/image1.png +0 -0
  71. data/lib/axlsx/drawing/bar_series.rb +0 -1
  72. data/lib/axlsx/drawing/cat_axis_data.rb +1 -0
  73. data/lib/axlsx/drawing/chart.rb +0 -1
  74. data/lib/axlsx/drawing/drawing.rb +31 -7
  75. data/lib/axlsx/drawing/graphic_frame.rb +0 -2
  76. data/lib/axlsx/drawing/line_series.rb +0 -1
  77. data/lib/axlsx/drawing/one_cell_anchor.rb +89 -0
  78. data/lib/axlsx/drawing/pic.rb +158 -0
  79. data/lib/axlsx/drawing/pie_3D_chart.rb +0 -1
  80. data/lib/axlsx/drawing/two_cell_anchor.rb +17 -4
  81. data/lib/axlsx/package.rb +31 -5
  82. data/lib/axlsx/rels/relationship.rb +1 -2
  83. data/lib/axlsx/util/constants.rb +24 -0
  84. data/lib/axlsx/util/validators.rb +2 -2
  85. data/lib/axlsx/version.rb +1 -1
  86. data/lib/axlsx/workbook/workbook.rb +8 -0
  87. data/lib/axlsx/workbook/worksheet/worksheet.rb +10 -1
  88. data/test/drawing/tc_drawing.rb +10 -1
  89. data/test/drawing/tc_one_cell_anchor.rb +67 -0
  90. data/test/drawing/tc_pic.rb +71 -0
  91. data/test/tc_package.rb +0 -2
  92. metadata +13 -118
  93. data/examples/#example.rb# +0 -87
  94. data/examples/example.rb~ +0 -79
  95. data/examples/multi_chart.xlsx +0 -0
  96. data/lib/axlsx.rb~ +0 -67
  97. data/lib/axlsx/content_type/content_type.rb~ +0 -20
  98. data/lib/axlsx/content_type/default.rb~ +0 -32
  99. data/lib/axlsx/content_type/override.rb~ +0 -30
  100. data/lib/axlsx/doc_props/app.rb~ +0 -127
  101. data/lib/axlsx/doc_props/core.rb~ +0 -25
  102. data/lib/axlsx/drawing/#bar_series.rb# +0 -62
  103. data/lib/axlsx/drawing/axis.rb~ +0 -0
  104. data/lib/axlsx/drawing/bar_3D_chart.rb~ +0 -64
  105. data/lib/axlsx/drawing/bar_series.rb~ +0 -92
  106. data/lib/axlsx/drawing/cat_axis.rb~ +0 -32
  107. data/lib/axlsx/drawing/cat_axis_data.rb~ +0 -28
  108. data/lib/axlsx/drawing/chart.rb~ +0 -0
  109. data/lib/axlsx/drawing/drawing.rb~ +0 -102
  110. data/lib/axlsx/drawing/graphic_frame.rb~ +0 -40
  111. data/lib/axlsx/drawing/line_3D_chart.rb~ +0 -138
  112. data/lib/axlsx/drawing/line_series.rb~ +0 -91
  113. data/lib/axlsx/drawing/marker.rb~ +0 -50
  114. data/lib/axlsx/drawing/pie_3D_chart.rb~ +0 -132
  115. data/lib/axlsx/drawing/pie_series.rb~ +0 -0
  116. data/lib/axlsx/drawing/scaling.rb~ +0 -0
  117. data/lib/axlsx/drawing/ser_axis.rb~ +0 -48
  118. data/lib/axlsx/drawing/series.rb~ +0 -114
  119. data/lib/axlsx/drawing/series_title.rb~ +0 -18
  120. data/lib/axlsx/drawing/title.rb~ +0 -69
  121. data/lib/axlsx/drawing/two_cell_anchor.rb~ +0 -70
  122. data/lib/axlsx/drawing/val_axis.rb~ +0 -34
  123. data/lib/axlsx/drawing/val_axis_data.rb~ +0 -29
  124. data/lib/axlsx/drawing/view_3D.rb~ +0 -21
  125. data/lib/axlsx/package.rb~ +0 -181
  126. data/lib/axlsx/rels/relationship.rb~ +0 -18
  127. data/lib/axlsx/rels/relationships.rb~ +0 -23
  128. data/lib/axlsx/stylesheet/border.rb~ +0 -24
  129. data/lib/axlsx/stylesheet/border_pr.rb~ +0 -64
  130. data/lib/axlsx/stylesheet/cell_alignment.rb~ +0 -93
  131. data/lib/axlsx/stylesheet/cell_protection.rb~ +0 -16
  132. data/lib/axlsx/stylesheet/cell_style.rb~ +0 -61
  133. data/lib/axlsx/stylesheet/color.rb~ +0 -56
  134. data/lib/axlsx/stylesheet/fill.rb~ +0 -31
  135. data/lib/axlsx/stylesheet/font.rb~ +0 -33
  136. data/lib/axlsx/stylesheet/gradient_fill.rb~ +0 -70
  137. data/lib/axlsx/stylesheet/gradient_stop.rb~ +0 -15
  138. data/lib/axlsx/stylesheet/num_fmt.rb~ +0 -60
  139. data/lib/axlsx/stylesheet/pattern_fill.rb~ +0 -63
  140. data/lib/axlsx/stylesheet/styles.rb~ +0 -279
  141. data/lib/axlsx/stylesheet/table_style.rb~ +0 -43
  142. data/lib/axlsx/stylesheet/table_style_element.rb~ +0 -66
  143. data/lib/axlsx/stylesheet/table_styles.rb~ +0 -36
  144. data/lib/axlsx/stylesheet/xf.rb~ +0 -37
  145. data/lib/axlsx/util/constants.rb~ +0 -187
  146. data/lib/axlsx/util/monkey_patches_for_true_zip_stream.rb~ +0 -61
  147. data/lib/axlsx/util/simple_typed_list.rb~ +0 -79
  148. data/lib/axlsx/util/validators.rb~ +0 -132
  149. data/lib/axlsx/util/xml_escape.rb~ +0 -6
  150. data/lib/axlsx/workbook/workbook.rb~ +0 -130
  151. data/lib/axlsx/workbook/worksheet/cell.rb~ +0 -185
  152. data/lib/axlsx/workbook/worksheet/row.rb~ +0 -92
  153. data/lib/axlsx/workbook/worksheet/worksheet.rb~ +0 -194
  154. data/test/content_type/tc_content_type.rb~ +0 -81
  155. data/test/content_type/tc_default.rb~ +0 -40
  156. data/test/content_type/tc_override.rb~ +0 -40
  157. data/test/doc_props/tc_app.rb~ +0 -19
  158. data/test/drawing/tc_axis.rb~ +0 -0
  159. data/test/drawing/tc_bar_3D_chart.rb~ +0 -66
  160. data/test/drawing/tc_bar_series.rb~ +0 -31
  161. data/test/drawing/tc_cat_axis.rb~ +0 -39
  162. data/test/drawing/tc_cat_axis_data.rb~ +0 -32
  163. data/test/drawing/tc_chart.rb~ +0 -59
  164. data/test/drawing/tc_graphic_frame.rb~ +0 -21
  165. data/test/drawing/tc_line_3d_chart.rb~ +0 -48
  166. data/test/drawing/tc_line_series.rb~ +0 -34
  167. data/test/drawing/tc_marker.rb~ +0 -26
  168. data/test/drawing/tc_pie_3D_chart.rb~ +0 -33
  169. data/test/drawing/tc_pie_series.rb~ +0 -26
  170. data/test/drawing/tc_scaling.rb~ +0 -45
  171. data/test/drawing/tc_ser_axis.rb~ +0 -20
  172. data/test/drawing/tc_series.rb~ +0 -31
  173. data/test/drawing/tc_series_title.rb~ +0 -34
  174. data/test/drawing/tc_title.rb~ +0 -34
  175. data/test/drawing/tc_two_cell_anchor.rb~ +0 -37
  176. data/test/drawing/tc_val_axis.rb~ +0 -32
  177. data/test/drawing/tc_val_axis_data.rb~ +0 -18
  178. data/test/drawing/tc_view_3D.rb~ +0 -37
  179. data/test/rels/tc_relationship.rb~ +0 -39
  180. data/test/rels/tc_relationships.rb~ +0 -37
  181. data/test/stylesheet/tc_border.rb~ +0 -31
  182. data/test/stylesheet/tc_border_pr.rb~ +0 -31
  183. data/test/stylesheet/tc_cell_alignment.rb~ +0 -38
  184. data/test/stylesheet/tc_cell_protection.rb~ +0 -77
  185. data/test/stylesheet/tc_cell_style.rb~ +0 -30
  186. data/test/stylesheet/tc_color.rb~ +0 -38
  187. data/test/stylesheet/tc_fill.rb~ +0 -19
  188. data/test/stylesheet/tc_font.rb~ +0 -19
  189. data/test/stylesheet/tc_gradient_fill.rb~ +0 -114
  190. data/test/stylesheet/tc_gradient_stop.rb~ +0 -65
  191. data/test/stylesheet/tc_num_fmt.rb~ +0 -32
  192. data/test/stylesheet/tc_pattern_fill.rb~ +0 -31
  193. data/test/stylesheet/tc_table_style.rb~ +0 -38
  194. data/test/stylesheet/tc_table_style_element.rb~ +0 -37
  195. data/test/stylesheet/tc_table_styles.rb~ +0 -37
  196. data/test/stylesheet/tc_xf.rb~ +0 -30
  197. data/test/tc_app.rb~ +0 -19
  198. data/test/tc_border_pr.rb~ +0 -21
  199. data/test/tc_package.rb~ +0 -64
  200. data/test/tc_pie_3D_chart.rb~ +0 -66
  201. data/test/tc_relationships.rb~ +0 -37
  202. data/test/tc_series.rb~ +0 -31
  203. data/test/tc_styles.rb~ +0 -64
  204. data/test/tc_validators.rb~ +0 -77
  205. data/test/tc_worksheet.rb~ +0 -85
  206. data/test/workbook/worksheet/tc_row.rb~ +0 -30
  207. data/test/workbook/worksheet/tc_worksheet.rb~ +0 -85
@@ -1,132 +0,0 @@
1
- module Axlsx
2
- # Validate a value against a specific list of allowed values.
3
- class RestrictionValidator
4
- # Perform validation
5
- # @param [String] name The name of what is being validatied. This is included in the error message
6
- # @param [Array] choices The list of choices to validate against
7
- # @param [Any] v The value to be validated
8
- # @raise [ArgumentError] Raised if the value provided is not in the list of choices.
9
- # @return [Boolean] true if validation succeeds.
10
- def self.validate(name, choices, v)
11
- raise ArgumentError, (ERR_RESTRICTION % [v.to_s, name, choices.inspect]) unless choices.include?(v)
12
- true
13
- end
14
- end
15
-
16
- # Validates the value against the regular expression provided.
17
- # @param [String] name The name of what is being validated. This is included in the output when the value is invalid
18
- # @param [Regexp] regex The regular expression to evaluate
19
- # @param [Any] v The value to validate.
20
- class RegexValidator
21
- def self.validate(name, regex, v)
22
- raise ArgumentError, (ERR_REGEX % [v.inspect, regex.to_s]) unless (v.respond_to?(:=~) && v =~ regex)
23
- end
24
- end
25
- # Validate that the class of the value provided is either an instance or the class of the allowed types and that any specified additional validation returns true.
26
- class DataTypeValidator
27
- # Perform validation
28
- # @param [String] name The name of what is being validated. This is included in the error message
29
- # @param [Array, Class] types A single class or array of classes that the value is validated against.
30
- # @param [Block] other Any block that must evaluate to true for the value to be valid
31
- # @raise [ArugumentError] Raised if the class of the value provided is not in the specified array of types or the block passed returns false
32
- # @return [Boolean] true if validation succeeds.
33
- # @see validate_boolean
34
- def self.validate(name, types, v, other= lambda{|v| true })
35
- types = [types] unless types.is_a? Array
36
- valid_type = false
37
- if v.class == Class
38
- types.each { |t| valid_type = true if v.ancestors.include?(t) }
39
- else
40
- types.each { |t| valid_type = true if v.is_a?(t) }
41
- end
42
- raise ArgumentError, (ERR_TYPE % [v.inspect, name, types.inspect]) unless (other.call(v) && valid_type)
43
- end
44
- true
45
- end
46
-
47
- # Requires that the value is a Fixnum or Integer and is greater or equal to 0
48
- # @param [Any] v The value validated
49
- # @raise [ArgumentError] raised if the value is not a Fixnum or Integer value greater or equal to 0
50
- # @return [Boolean] true if the data is valid
51
- def self.validate_unsigned_int(v)
52
- DataTypeValidator.validate(:unsigned_int, [Fixnum, Integer], v, lambda { |v| v.respond_to?(:>=) && v >= 0 })
53
- end
54
-
55
- # Requires that the value is a Fixnum or Integer
56
- # @param [Any] v The value validated
57
- def self.validate_int(v)
58
- DataTypeValidator.validate :unsigned_int, [Fixnum, Integer], v
59
- end
60
-
61
- # Requires that the value is a form that can be evaluated as a boolean in an xml document.
62
- # The value must be an instance of Fixnum, String, Integer, Symbol, TrueClass or FalseClass and
63
- # it must be one of 0, 1, "true", "false", :true, :false, true, false, "0", or "1"
64
- # @param [Any] v The value validated
65
- def self.validate_boolean(v)
66
- DataTypeValidator.validate(:boolean, [Fixnum, String, Integer, Symbol, TrueClass, FalseClass], v, lambda { |v| [0, 1, "true", "false", :true, :false, true, false, "0", "1"].include?(v) })
67
- end
68
-
69
- # Requires that the value is a String
70
- # @param [Any] v The value validated
71
- def self.validate_string(v)
72
- DataTypeValidator.validate :string, String, v
73
- end
74
-
75
- # Requires that the value is a Float
76
- # @param [Any] v The value validated
77
- def self.validate_float(v)
78
- DataTypeValidator.validate :float, Float, v
79
- end
80
-
81
- # Requires that the value is valid pattern type.
82
- # valid pattern types must be one of :none, :solid, :mediumGray, :darkGray, :lightGray, :darkHorizontal, :darkVertical, :darkDown,
83
- # :darkUp, :darkGrid, :darkTrellis, :lightHorizontal, :lightVertical, :lightDown, :lightUp, :lightGrid, :lightTrellis, :gray125, or :gray0625.
84
- # @param [Any] v The value validated
85
- def self.validate_pattern_type(v)
86
- RestrictionValidator.validate :pattern_type, [:none, :solid, :mediumGray, :darkGray, :lightGray, :darkHorizontal, :darkVertical, :darkDown, :darkUp, :darkGrid,
87
- :darkTrellis, :lightHorizontal, :lightVertical, :lightDown, :lightUp, :lightGrid, :lightTrellis, :gray125, :gray0625], v
88
- end
89
-
90
- # Requires that the value is a gradient_type.
91
- # valid types are :linear and :path
92
- # @param [Any] v The value validated
93
- def self.validate_gradient_type(v)
94
- RestrictionValidator.validate :gradient_type, [:linear, :path], v
95
- end
96
-
97
- # Requires that the value is a valid horizontal_alignment
98
- # :general, :left, :center, :right, :fill, :justify, :centerContinuous, :distributed are allowed
99
- # @param [Any] v The value validated
100
- def self.validate_horizontal_alignment(v)
101
- RestrictionValidator.validate :horizontal_alignment, [:general, :left, :center, :right, :fill, :justify, :centerContinuous, :distributed], v
102
- end
103
-
104
- # Requires that the value is a valid vertical_alignment
105
- # :top, :center, :bottom, :justify, :distributed are allowed
106
- # @param [Any] v The value validated
107
- def self.validate_vertical_alignment(v)
108
- RestrictionValidator.validate :vertical_alignment, [:top, :center, :bottom, :justify, :distributed], v
109
- end
110
-
111
- # Requires that the value is a valid content_type
112
- # TABLE_CT, WORKBOOK_CT, APP_CT, RELS_CT, STYLES_CT, XML_CT, WORKSHEET_CT, SHARED_STRINGS_CT, CORE_CT, CHART_CT, DRAWING_CT are allowed
113
- # @param [Any] v The value validated
114
- def self.validate_content_type(v)
115
- RestrictionValidator.validate :content_type, [TABLE_CT, WORKBOOK_CT, APP_CT, RELS_CT, STYLES_CT, XML_CT, WORKSHEET_CT, SHARED_STRINGS_CT, CORE_CT, CHART_CT, DRAWING_CT], v
116
- end
117
-
118
- # Requires that the value is a valid relationship_type
119
- # XML_NS_R, TABLE_R, WORKBOOK_R, WORKSHEET_R, APP_R, RELS_R, CORE_R, STYLES_R, CHART_R, DRAWING_R are allowed
120
- # @param [Any] v The value validated
121
- def self.validate_relationship_type(v)
122
- RestrictionValidator.validate :relationship_type, [XML_NS_R, TABLE_R, WORKBOOK_R, WORKSHEET_R, APP_R, RELS_R, CORE_R, STYLES_R, CHART_R, DRAWING_R], v
123
- end
124
-
125
- # Requires that the value is a valid table element type
126
- # :wholeTable, :headerRow, :totalRow, :firstColumn, :lastColumn, :firstRowStripe, :secondRowStripe, :firstColumnStripe, :secondColumnStripe, :firstHeaderCell, :lastHeaderCell, :firstTotalCell, :lastTotalCell, :firstSubtotalColumn, :secondSubtotalColumn, :thirdSubtotalColumn, :firstSubtotalRow, :secondSubtotalRow, :thirdSubtotalRow, :blankRow, :firstColumnSubheading, :secondColumnSubheading, :thirdColumnSubheading, :firstRowSubheading, :secondRowSubheading, :thirdRowSubheading, :pageFieldLabels, :pageFieldValues are allowed
127
- # @param [Any] v The value validated
128
- def self.validate_table_element_type(v)
129
- RestrictionValidator.validate :table_element_type, [:wholeTable, :headerRow, :totalRow, :firstColumn, :lastColumn, :firstRowStripe, :secondRowStripe, :firstColumnStripe, :secondColumnStripe, :firstHeaderCell, :lastHeaderCell, :firstTotalCell, :lastTotalCell, :firstSubtotalColumn, :secondSubtotalColumn, :thirdSubtotalColumn, :firstSubtotalRow, :secondSubtotalRow, :thirdSubtotalRow, :blankRow, :firstColumnSubheading, :secondColumnSubheading, :thirdColumnSubheading, :firstRowSubheading, :secondRowSubheading, :thirdRowSubheading, :pageFieldLabels, :pageFieldValues], v
130
- end
131
-
132
- end
@@ -1,6 +0,0 @@
1
- unless String.method_defined? :to_xs
2
- require 'fast_xs' #dep
3
- class String
4
- alias_method :to_xs, :fast_xs
5
- end
6
- end
@@ -1,130 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module Axlsx
3
-
4
- require 'axlsx/workbook/worksheet/cell.rb'
5
- require 'axlsx/workbook/worksheet/row.rb'
6
- require 'axlsx/workbook/worksheet/worksheet.rb'
7
-
8
- # The Workbook class is an xlsx workbook that manages worksheets, charts, drawings and styles.
9
- # The following parts of the Office Open XML spreadsheet specification are not implimented in this version.
10
- #
11
- # bookViews
12
- # calcPr
13
- # customWorkbookViews
14
- # definedNames
15
- # externalReferences
16
- # extLst
17
- # fileRecoveryPr
18
- # fileSharing
19
- # fileVersion
20
- # functionGroups
21
- # oleSize
22
- # pivotCaches
23
- # smartTagPr
24
- # smartTagTypes
25
- # webPublishing
26
- # webPublishObjects
27
- # workbookProtection
28
- # workbookPr*
29
- #
30
- # *workbookPr is only supported to the extend of date1904
31
- class Workbook
32
-
33
- # A collection of worksheets associated with this workbook.
34
- # @note The recommended way to manage worksheets is add_worksheet
35
- # @see Workbook#add_worksheet
36
- # @see Worksheet
37
- # @return [SimpleTypedList]
38
- attr_reader :worksheets
39
-
40
- # A colllection of charts associated with this workbook
41
- # @note The recommended way to manage charts is Worksheet#add_chart
42
- # @see Worksheet#add_chart
43
- # @see Chart
44
- # @return [SimpleTypedList]
45
- attr_reader :charts
46
-
47
- # A colllection of drawings associated with this workbook
48
- # @note The recommended way to manage drawings is Worksheet#add_chart
49
- # @see Worksheet#add_chart
50
- # @see Drawing
51
- # @return [SimpleTypedList]
52
- attr_reader :drawings
53
-
54
- # The styles associated with this workbook
55
- # @note The recommended way to manage styles is Styles#add_style
56
- # @see Style#add_style
57
- # @see Style
58
- # @return [Styles]
59
- attr_reader :styles
60
-
61
- # The workbook relationships. This is managed automatically by the workbook
62
- # @return [Relationships]
63
- attr_reader :relationships
64
-
65
- # Instance level access to the class variable 1904
66
- # @return [Boolean]
67
- attr_accessor :date1904
68
-
69
-
70
- # Indicates if the epoc date for serialization should be 1904. If false, 1900 is used.
71
- @@date1904 = false
72
-
73
- # Creates a new Workbook
74
- # @option options [Boolean] date1904
75
- def initialize(options={})
76
- @styles = Styles.new
77
- @worksheets = SimpleTypedList.new Worksheet
78
- @drawings = SimpleTypedList.new Drawing
79
- @charts = SimpleTypedList.new Chart
80
- self.date1904= options[:date1904] unless options[:date1904].nil?
81
- end
82
-
83
- def date1904=(v) Axlsx::validate_boolean v; @@date1904 = v; end
84
- def date1904() @@date1904; end
85
-
86
- # Sets the date1904 attribute to the provided boolean
87
- # @return [Boolean]
88
- def self.date1904=(v) Axlsx::validate_boolean v; @@date1904 = v; end
89
-
90
- # retrieves the date1904 attribute
91
- # @return [Boolean]
92
- def self.date1904() @@date1904; end
93
-
94
- # Adds a worksheet to this workbook
95
- # @return [Worksheet]
96
- # @option options [String] name The name of the worksheet.
97
- # @see Worksheet#initialize
98
- def add_worksheet(options={})
99
- worksheet = Worksheet.new(self, options)
100
- yield worksheet if block_given?
101
- worksheet
102
- end
103
-
104
- def relationships
105
- r = Relationships.new
106
- @worksheets.each do |sheet|
107
- r << Relationship.new(WORKSHEET_R, WORKSHEET_PN % (r.size+1))
108
- end
109
- r << Relationship.new(STYLES_R, STYLES_PN)
110
- r
111
- end
112
-
113
- # Serializes the workbook document
114
- # @return [String]
115
- def to_xml()
116
- add_worksheet unless worksheets.size > 0
117
- builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
118
- xml.workbook(:xmlns => XML_NS, :'xmlns:r' => XML_NS_R) {
119
- xml.workbookPr(:date1904=>@@date1904)
120
- xml.sheets {
121
- @worksheets.each_with_index do |sheet, index|
122
- xml.sheet(:name=>sheet.name, :sheetId=>index+1, :"r:id"=>sheet.rId)
123
- end
124
- }
125
- }
126
- end
127
- builder.to_xml(:indent=>0)
128
- end
129
- end
130
- end
@@ -1,185 +0,0 @@
1
- module Axlsx
2
- # A cell in a worksheet.
3
- # Cell stores inforamation requried to serialize a single worksheet cell to xml. You must provde the Row that the cell belongs to and the cells value. The data type will automatically be determed if you do not specify the :type option. The default style will be applied if you do not supply the :style option. Changing the cell's type will recast the value to the type specified. Altering the cell's value via the property accessor will also automatically cast the provided value to the cell's type.
4
- # @example Manually creating and manipulating Cell objects
5
- # ws = Workbook.new.add_worksheet
6
- # # This is the simple, and recommended way to create cells. Data types will automatically be determined for you.
7
- # ws.add_row :values => [1,"fish",Time.now]
8
- #
9
- # # but you can also do this
10
- # r = ws.add_row
11
- # r.add_cell 1
12
- #
13
- # # or even this
14
- # r = ws.add_row
15
- # c = Cell.new row, 1, :value=>integer
16
- #
17
- # # cells can also be accessed via Row#cells. The example here changes the cells type, which will automatically updated the value from 1 to 1.0
18
- # r.cells.last.type = :float
19
- #
20
- # @note The recommended way to generate cells is via Worksheet#add_row
21
- #
22
- # @see Worksheet#add_row
23
- class Cell
24
-
25
- # The index of the cellXfs item to be applied to this cell.
26
- # @return [Integer]
27
- # @see Axlsx::Styles
28
- attr_accessor :style
29
-
30
- # The row this cell belongs to.
31
- # @return [Row]
32
- attr_reader :row
33
-
34
- # The cell's data type. Currently only four types are supported, :time, :float, :integer and :string.
35
- # Changing the type for a cell will recast the value into that type. If no type option is specified in the constructor, the type is
36
- # automatically determed.
37
- # @see Cell#cell_type_from_value
38
- # @return [Symbol] The type of data this cell's value is cast to.
39
- # @raise [ArgumentExeption] Cell.type must be one of [:time, :float, :integer, :string]
40
- # @note
41
- # If the value provided cannot be cast into the type specified, type is changed to :string and the following logic is applied.
42
- # :string to :integer or :float, type coversions always return 0 or 0.0
43
- # :string, :integer, or :float to :time conversions always return the original value as a string and set the cells type to :string.
44
- # No support is currently implemented for parsing time strings.
45
- #
46
- attr_accessor :type
47
-
48
- # The value of this cell.
49
- # @return casted value based on cell's type attribute.
50
- attr_accessor :value
51
-
52
- # @param [Row] row The row this cell belongs to.
53
- # @param [Any] value The value associated with this cell.
54
- # @option options [Symbol] type The intended data type for this cell. If not specified the data type will be determined internally based on the vlue provided.
55
- # @option options [Integer] style The index of the cellXfs item to be applied to this cell. If not specified, the default style (0) will be applied.
56
- def initialize(row, value, options={})
57
- self.row=row
58
- #reference for validation
59
- @styles = row.worksheet.workbook.styles
60
- self.type= options[:type] || cell_type_from_value(value)
61
- self.style = options[:style] || 0
62
- self.value = value
63
- @row.cells << self
64
- end
65
-
66
- # @return [Integer] The index of the cell in the containing row.
67
- def index
68
- @row.cells.index(self)
69
- end
70
-
71
- # @return [String] The alpha(column)numeric(row) reference for this sell.
72
- # @example Relative Cell Reference
73
- # ws.rows.first.cells.first.r #=> "A1"
74
- def r
75
- "#{col_ref}#{@row.index+1}"
76
- end
77
-
78
- # @return [String] The absolute alpha(column)numeric(row) reference for this sell.
79
- # @example Absolute Cell Reference
80
- # ws.rows.first.cells.first.r #=> "$A$1"
81
- def r_abs
82
- "$#{r.split('').join('$')}"
83
- end
84
-
85
- # @return [Integer] The cellXfs item index applied to this cell.
86
- # @raise [ArgumentError] Invalid cellXfs id if the value provided is not within cellXfs items range.
87
- def style=(v)
88
- Axlsx::validate_unsigned_int(v)
89
- count = @styles.cellXfs.size
90
- raise ArgumentError, "Invalid cellXfs id" unless v < count
91
- @style = v
92
- end
93
-
94
- def type=(v)
95
- RestrictionValidator.validate "Cell.type", [:time, :float, :integer, :string], v
96
- @type=v
97
- self.value = @value
98
- @type
99
- end
100
-
101
- def value=(v)
102
- #TODO: consider doing value based type determination first?
103
- @value = cast_value(v)
104
- end
105
-
106
- # Serializes the cell
107
- # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
108
- # @return [String] xml text for the cell
109
- # @note
110
- # Shared Strings are not used in this library. All values are set directly in the each sheet.
111
- def to_xml(xml)
112
- if @type == :string
113
- #NOTE not sure why, but xml.t @v renders the text as html entities of unicode data
114
- xml.c(:r => r, :t=>:inlineStr, :s=>style) { xml.is { xml << "<t>#{value}</t>" } }
115
- else
116
- xml.c(:r => r, :s => style) { xml.v value }
117
- end
118
- end
119
-
120
- private
121
-
122
- # assigns the owning row for this cell.
123
- def row=(v) DataTypeValidator.validate "Cell.row", Row, v; @row=v end
124
-
125
- # converts the column index into alphabetical values.
126
- # @note This follows the standard spreadsheet convention of naming columns A to Z, followed by AA to AZ etc.
127
- # @return [String]
128
- def col_ref
129
- chars = []
130
- index = self.index
131
- while index >= 26 do
132
- chars << ((index % 26) + 65).chr
133
- index /= 26
134
- end
135
- chars << ((chars.empty? ? index : index-1) + 65).chr
136
- chars.reverse.join
137
- end
138
-
139
- # Determines the cell type based on the cell value.
140
- # @note This is only used when a cell is created but no :type option is specified, the following rules apply:
141
- # 1. If the value is an instance of Time, the type is set to :time
142
- # 2. :float and :integer types are determined by regular expression matching.
143
- # 3. Anything that does not meet either of the above is determined to be :string.
144
- # @return [Symbol] The determined type
145
- def cell_type_from_value(value)
146
- if value.is_a? Time
147
- :time
148
- elsif value.to_s.match(/\A[+-]?\d+\.\d+?\Z/) #float
149
- :float
150
- elsif value.to_s.match(/\A[+-]?\d+?\Z/) #numeric
151
- :integer
152
- else
153
- :string
154
- end
155
- end
156
-
157
- # Cast the value into this cells data type.
158
- # @note
159
- # About Time - Time in OOXML is *different* from what you might expect. The history as to why is interesting, but you can safely assume that if you are generating docs on a mac, you will want to specify Workbook.1904 as true when using time typed values.
160
- # @see Axlsx#date1904
161
- def cast_value(v)
162
- if type == :time && v.is_a?(Time)
163
- #todo consider a time parsing method to convert strings to time
164
- epoc = Workbook.date1904 ? Time.local(1904,1,1,0,0,0,0,v.zone) : Time.local(1900,1,1,0,0,0,0,v.zone)
165
- ((v - epoc) /60.0/60.0/24.0).to_f
166
- elsif type == :float
167
- v.to_f
168
- elsif type == :integer
169
- v.to_i
170
- else
171
- @type = :string
172
- v.to_s
173
- # curious as to why this would be the cells responsibility
174
- # convert your values before passing them in wankers! CGI.unescapeHTML(v.to_s).to_xs
175
- # to revert, load this once when the gem is loaded.
176
- # unless String.method_defined? :to_xs
177
- # require 'fast_xs' #dep
178
- # class String
179
- # alias_method :to_xs, :fast_xs
180
- # end
181
- # end
182
- end
183
- end
184
- end
185
- end