caxlsx 2.0.2 → 3.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +125 -30
  3. data/README.md +65 -151
  4. data/Rakefile +9 -11
  5. data/examples/{image1.jpeg → assets/image1.jpeg} +0 -0
  6. data/examples/generate.rb +15 -0
  7. data/lib/axlsx.rb +35 -17
  8. data/lib/axlsx/content_type/abstract_content_type.rb +1 -1
  9. data/lib/axlsx/content_type/content_type.rb +1 -1
  10. data/lib/axlsx/doc_props/app.rb +1 -1
  11. data/lib/axlsx/doc_props/core.rb +5 -5
  12. data/lib/axlsx/drawing/area_chart.rb +99 -0
  13. data/lib/axlsx/drawing/area_series.rb +110 -0
  14. data/lib/axlsx/drawing/axes.rb +1 -1
  15. data/lib/axlsx/drawing/axis.rb +12 -9
  16. data/lib/axlsx/drawing/bar_3D_chart.rb +13 -13
  17. data/lib/axlsx/drawing/bar_chart.rb +143 -0
  18. data/lib/axlsx/drawing/bar_series.rb +12 -14
  19. data/lib/axlsx/drawing/bubble_chart.rb +59 -0
  20. data/lib/axlsx/drawing/bubble_series.rb +63 -0
  21. data/lib/axlsx/drawing/cat_axis.rb +5 -5
  22. data/lib/axlsx/drawing/chart.rb +52 -8
  23. data/lib/axlsx/drawing/d_lbls.rb +4 -4
  24. data/lib/axlsx/drawing/drawing.rb +6 -1
  25. data/lib/axlsx/drawing/graphic_frame.rb +3 -3
  26. data/lib/axlsx/drawing/hyperlink.rb +1 -3
  27. data/lib/axlsx/drawing/line_3D_chart.rb +2 -2
  28. data/lib/axlsx/drawing/line_chart.rb +10 -10
  29. data/lib/axlsx/drawing/line_series.rb +32 -3
  30. data/lib/axlsx/drawing/marker.rb +1 -1
  31. data/lib/axlsx/drawing/num_data.rb +4 -4
  32. data/lib/axlsx/drawing/num_data_source.rb +6 -6
  33. data/lib/axlsx/drawing/num_val.rb +3 -1
  34. data/lib/axlsx/drawing/one_cell_anchor.rb +3 -2
  35. data/lib/axlsx/drawing/pic.rb +25 -19
  36. data/lib/axlsx/drawing/picture_locking.rb +1 -3
  37. data/lib/axlsx/drawing/pie_3D_chart.rb +5 -6
  38. data/lib/axlsx/drawing/pie_series.rb +6 -6
  39. data/lib/axlsx/drawing/scaling.rb +6 -6
  40. data/lib/axlsx/drawing/scatter_chart.rb +10 -10
  41. data/lib/axlsx/drawing/scatter_series.rb +40 -7
  42. data/lib/axlsx/drawing/ser_axis.rb +2 -2
  43. data/lib/axlsx/drawing/series.rb +3 -3
  44. data/lib/axlsx/drawing/series_title.rb +4 -2
  45. data/lib/axlsx/drawing/str_data.rb +3 -3
  46. data/lib/axlsx/drawing/str_val.rb +3 -1
  47. data/lib/axlsx/drawing/title.rb +23 -4
  48. data/lib/axlsx/drawing/two_cell_anchor.rb +6 -1
  49. data/lib/axlsx/drawing/val_axis.rb +1 -1
  50. data/lib/axlsx/drawing/view_3D.rb +2 -2
  51. data/lib/axlsx/drawing/vml_drawing.rb +1 -1
  52. data/lib/axlsx/package.rb +58 -47
  53. data/lib/axlsx/rels/relationship.rb +27 -26
  54. data/lib/axlsx/rels/relationships.rb +7 -4
  55. data/lib/axlsx/stylesheet/border_pr.rb +2 -2
  56. data/lib/axlsx/stylesheet/cell_alignment.rb +1 -3
  57. data/lib/axlsx/stylesheet/cell_protection.rb +1 -3
  58. data/lib/axlsx/stylesheet/cell_style.rb +1 -3
  59. data/lib/axlsx/stylesheet/color.rb +1 -3
  60. data/lib/axlsx/stylesheet/font.rb +11 -3
  61. data/lib/axlsx/stylesheet/gradient_stop.rb +1 -1
  62. data/lib/axlsx/stylesheet/num_fmt.rb +10 -3
  63. data/lib/axlsx/stylesheet/pattern_fill.rb +1 -1
  64. data/lib/axlsx/stylesheet/styles.rb +7 -7
  65. data/lib/axlsx/stylesheet/table_style_element.rb +1 -3
  66. data/lib/axlsx/util/accessors.rb +6 -6
  67. data/lib/axlsx/util/constants.rb +108 -99
  68. data/lib/axlsx/util/mime_type_utils.rb +11 -0
  69. data/lib/axlsx/util/options_parser.rb +2 -1
  70. data/lib/axlsx/util/serialized_attributes.rb +16 -6
  71. data/lib/axlsx/util/simple_typed_list.rb +28 -52
  72. data/lib/axlsx/util/storage.rb +4 -4
  73. data/lib/axlsx/util/validators.rb +31 -19
  74. data/lib/axlsx/util/zip_command.rb +73 -0
  75. data/lib/axlsx/version.rb +1 -1
  76. data/lib/axlsx/workbook/defined_name.rb +11 -12
  77. data/lib/axlsx/workbook/defined_names.rb +2 -2
  78. data/lib/axlsx/workbook/shared_strings_table.rb +5 -5
  79. data/lib/axlsx/workbook/workbook.rb +36 -20
  80. data/lib/axlsx/workbook/workbook_view.rb +80 -0
  81. data/lib/axlsx/workbook/workbook_views.rb +22 -0
  82. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +2 -2
  83. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +1 -3
  84. data/lib/axlsx/workbook/worksheet/break.rb +1 -3
  85. data/lib/axlsx/workbook/worksheet/cell.rb +164 -75
  86. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +63 -43
  87. data/lib/axlsx/workbook/worksheet/cfvo.rb +1 -3
  88. data/lib/axlsx/workbook/worksheet/cfvos.rb +4 -1
  89. data/lib/axlsx/workbook/worksheet/col.rb +14 -13
  90. data/lib/axlsx/workbook/worksheet/col_breaks.rb +2 -2
  91. data/lib/axlsx/workbook/worksheet/cols.rb +5 -2
  92. data/lib/axlsx/workbook/worksheet/comment.rb +5 -6
  93. data/lib/axlsx/workbook/worksheet/comments.rb +9 -12
  94. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +1 -1
  95. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +1 -1
  96. data/lib/axlsx/workbook/worksheet/data_bar.rb +4 -6
  97. data/lib/axlsx/workbook/worksheet/data_validation.rb +8 -6
  98. data/lib/axlsx/workbook/worksheet/dimension.rb +2 -2
  99. data/lib/axlsx/workbook/worksheet/header_footer.rb +6 -8
  100. data/lib/axlsx/workbook/worksheet/icon_set.rb +3 -5
  101. data/lib/axlsx/workbook/worksheet/merged_cells.rb +4 -2
  102. data/lib/axlsx/workbook/worksheet/outline_pr.rb +33 -0
  103. data/lib/axlsx/workbook/worksheet/page_margins.rb +1 -3
  104. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +1 -1
  105. data/lib/axlsx/workbook/worksheet/page_setup.rb +21 -23
  106. data/lib/axlsx/workbook/worksheet/pane.rb +1 -3
  107. data/lib/axlsx/workbook/worksheet/pivot_table.rb +44 -28
  108. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +4 -4
  109. data/lib/axlsx/workbook/worksheet/print_options.rb +1 -3
  110. data/lib/axlsx/workbook/worksheet/protected_range.rb +1 -3
  111. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +5 -2
  112. data/lib/axlsx/workbook/worksheet/rich_text.rb +55 -0
  113. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +250 -0
  114. data/lib/axlsx/workbook/worksheet/row.rb +42 -52
  115. data/lib/axlsx/workbook/worksheet/row_breaks.rb +2 -2
  116. data/lib/axlsx/workbook/worksheet/selection.rb +1 -3
  117. data/lib/axlsx/workbook/worksheet/sheet_data.rb +3 -1
  118. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +21 -3
  119. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +1 -3
  120. data/lib/axlsx/workbook/worksheet/table.rb +6 -6
  121. data/lib/axlsx/workbook/worksheet/table_style_info.rb +1 -3
  122. data/lib/axlsx/workbook/worksheet/tables.rb +4 -1
  123. data/lib/axlsx/workbook/worksheet/worksheet.rb +76 -81
  124. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +10 -10
  125. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +3 -3
  126. data/lib/caxlsx.rb +2 -0
  127. data/test/drawing/tc_area_chart.rb +39 -0
  128. data/test/drawing/tc_area_series.rb +71 -0
  129. data/test/drawing/tc_axis.rb +27 -0
  130. data/test/drawing/tc_bar_chart.rb +71 -0
  131. data/test/drawing/tc_bubble_chart.rb +44 -0
  132. data/test/drawing/tc_bubble_series.rb +21 -0
  133. data/test/drawing/tc_chart.rb +23 -10
  134. data/test/drawing/tc_data_source.rb +6 -0
  135. data/test/drawing/tc_drawing.rb +4 -4
  136. data/test/drawing/tc_hyperlink.rb +1 -1
  137. data/test/drawing/tc_line_chart.rb +5 -5
  138. data/test/drawing/tc_line_series.rb +47 -6
  139. data/test/drawing/tc_one_cell_anchor.rb +1 -1
  140. data/test/drawing/tc_pic.rb +11 -15
  141. data/test/drawing/tc_pie_series.rb +2 -1
  142. data/test/drawing/tc_scatter_series.rb +36 -1
  143. data/test/drawing/tc_series_title.rb +21 -0
  144. data/test/drawing/tc_str_val.rb +9 -0
  145. data/test/drawing/tc_title.rb +21 -0
  146. data/test/fixtures/image1.gif +0 -0
  147. data/test/fixtures/image1.jpeg +0 -0
  148. data/test/fixtures/image1.jpg +0 -0
  149. data/test/fixtures/image1.png +0 -0
  150. data/test/fixtures/image1_fake.jpg +0 -0
  151. data/test/rels/tc_relationship.rb +8 -0
  152. data/test/stylesheet/tc_font.rb +14 -2
  153. data/test/stylesheet/tc_styles.rb +29 -3
  154. data/test/tc_axlsx.rb +37 -0
  155. data/test/tc_helper.rb +2 -0
  156. data/test/tc_package.rb +50 -13
  157. data/test/util/tc_mime_type_utils.rb +13 -0
  158. data/test/util/tc_simple_typed_list.rb +2 -3
  159. data/test/util/tc_validators.rb +35 -11
  160. data/test/workbook/tc_defined_name.rb +12 -4
  161. data/test/workbook/tc_shared_strings_table.rb +16 -1
  162. data/test/workbook/tc_workbook.rb +38 -3
  163. data/test/workbook/tc_workbook_view.rb +50 -0
  164. data/test/workbook/worksheet/auto_filter/tc_filters.rb +1 -1
  165. data/test/workbook/worksheet/tc_break.rb +1 -1
  166. data/test/workbook/worksheet/tc_cell.rb +143 -9
  167. data/test/workbook/worksheet/tc_col.rb +18 -3
  168. data/test/workbook/worksheet/tc_conditional_formatting.rb +2 -2
  169. data/test/workbook/worksheet/tc_data_bar.rb +1 -1
  170. data/test/workbook/worksheet/tc_data_validation.rb +11 -11
  171. data/test/workbook/worksheet/tc_header_footer.rb +2 -2
  172. data/test/workbook/worksheet/tc_icon_set.rb +1 -1
  173. data/test/workbook/worksheet/tc_outline_pr.rb +19 -0
  174. data/test/workbook/worksheet/tc_page_setup.rb +3 -3
  175. data/test/workbook/worksheet/tc_pivot_table.rb +21 -6
  176. data/test/workbook/worksheet/tc_print_options.rb +1 -1
  177. data/test/workbook/worksheet/tc_rich_text.rb +44 -0
  178. data/test/workbook/worksheet/tc_rich_text_run.rb +173 -0
  179. data/test/workbook/worksheet/tc_row.rb +24 -2
  180. data/test/workbook/worksheet/tc_sheet_calc_pr.rb +1 -1
  181. data/test/workbook/worksheet/tc_sheet_format_pr.rb +4 -4
  182. data/test/workbook/worksheet/tc_sheet_pr.rb +26 -4
  183. data/test/workbook/worksheet/tc_sheet_protection.rb +5 -5
  184. data/test/workbook/worksheet/tc_sheet_view.rb +4 -4
  185. data/test/workbook/worksheet/tc_table.rb +2 -3
  186. data/test/workbook/worksheet/tc_worksheet.rb +123 -60
  187. metadata +180 -128
  188. data/examples/2010_comments.rb +0 -17
  189. data/examples/anchor_swapping.rb +0 -28
  190. data/examples/auto_filter.rb +0 -16
  191. data/examples/basic_charts.rb +0 -58
  192. data/examples/chart_colors.rb +0 -88
  193. data/examples/colored_links.rb +0 -59
  194. data/examples/conditional_formatting/example_conditional_formatting.rb +0 -74
  195. data/examples/conditional_formatting/getting_barred.rb +0 -37
  196. data/examples/conditional_formatting/hitting_the_high_notes.rb +0 -37
  197. data/examples/conditional_formatting/scaled_colors.rb +0 -39
  198. data/examples/conditional_formatting/stop_and_go.rb +0 -37
  199. data/examples/data_validation.rb +0 -50
  200. data/examples/example.rb +0 -777
  201. data/examples/extractive.rb +0 -45
  202. data/examples/ios_preview.rb +0 -14
  203. data/examples/page_setup.rb +0 -11
  204. data/examples/pivot_table.rb +0 -39
  205. data/examples/sheet_protection.rb +0 -10
  206. data/examples/skydrive/real_example.rb +0 -63
  207. data/examples/styles.rb +0 -66
  208. data/examples/underline.rb +0 -13
  209. data/examples/wrap_text.rb +0 -21
  210. data/lib/axlsx/util/parser.rb +0 -44
@@ -3,43 +3,32 @@ module Axlsx
3
3
  # The Cell Serializer class contains the logic for serializing cells based on their type.
4
4
  class CellSerializer
5
5
  class << self
6
-
7
-
8
6
  # Calls the proper serialization method based on type.
9
7
  # @param [Integer] row_index The index of the cell's row
10
8
  # @param [Integer] column_index The index of the cell's column
11
9
  # @param [String] str The string to apend serialization to.
12
10
  # @return [String]
13
11
  def to_xml_string(row_index, column_index, cell, str='')
14
- str << '<c r="' << Axlsx::cell_r(column_index, row_index) << '" s="' << cell.style.to_s << '" '
12
+ str << ('<c r="' << Axlsx::cell_r(column_index, row_index) << '" s="' << cell.style.to_s << '" ')
15
13
  return str << '/>' if cell.value.nil?
16
- method = (cell.type.to_s << '_type_serialization').to_sym
14
+ method = cell.type
17
15
  self.send(method, cell, str)
18
16
  str << '</c>'
19
- end
20
-
17
+ end
21
18
 
22
19
  # builds an xml text run based on this cells attributes.
23
20
  # @param [String] str The string instance this run will be concated to.
24
21
  # @return [String]
25
22
  def run_xml_string(cell, str = '')
26
23
  if cell.is_text_run?
27
- data = cell.instance_values.reject{|key, value| value == nil || key == 'value' || key == 'type' }
28
- keys = data.keys & Cell::INLINE_STYLES
29
- str << "<r><rPr>"
30
- keys.each do |key|
31
- case key
32
- when 'font_name'
33
- str << "<rFont val='"<< cell.font_name << "'/>"
34
- when 'color'
35
- str << data[key].to_xml_string
36
- else
37
- str << "<" << key.to_s << " val='" << data[key].to_s << "'/>"
38
- end
39
- end
40
- str << "</rPr>" << "<t>" << cell.value.to_s << "</t></r>"
24
+ valid = RichTextRun::INLINE_STYLES - [:value, :type]
25
+ data = Hash[cell.instance_values.map{ |k, v| [k.to_sym, v] }]
26
+ data = data.select { |key, value| valid.include?(key) && !value.nil? }
27
+ RichText.new(cell.value.to_s, data).to_xml_string(str)
28
+ elsif cell.contains_rich_text?
29
+ cell.value.to_xml_string(str)
41
30
  else
42
- str << "<t>" << cell.value.to_s << "</t>"
31
+ str << ('<t>' << cell.clean_value << '</t>')
43
32
  end
44
33
  str
45
34
  end
@@ -48,16 +37,15 @@ module Axlsx
48
37
  # @param [Cell] cell The cell that is being serialized
49
38
  # @param [String] str The string the serialized content will be appended to.
50
39
  # @return [String]
51
- def iso_8601_type_serialization(cell, str='')
40
+ def iso_8601(cell, str='')
52
41
  value_serialization 'd', cell.value, str
53
42
  end
54
43
 
55
-
56
44
  # serializes cells that are type date
57
45
  # @param [Cell] cell The cell that is being serialized
58
46
  # @param [String] str The string the serialized content will be appended to.
59
47
  # @return [String]
60
- def date_type_serialization(cell, str='')
48
+ def date(cell, str='')
61
49
  value_serialization false, DateTimeConverter::date_to_serial(cell.value).to_s, str
62
50
  end
63
51
 
@@ -65,7 +53,7 @@ module Axlsx
65
53
  # @param [Cell] cell The cell that is being serialized
66
54
  # @param [String] str The string the serialized content will be appended to.
67
55
  # @return [String]
68
- def time_type_serialization(cell, str='')
56
+ def time(cell, str='')
69
57
  value_serialization false, DateTimeConverter::time_to_serial(cell.value).to_s, str
70
58
  end
71
59
 
@@ -73,7 +61,7 @@ module Axlsx
73
61
  # @param [Cell] cell The cell that is being serialized
74
62
  # @param [String] str The string the serialized content will be appended to.
75
63
  # @return [String]
76
- def boolean_type_serialization(cell, str='')
64
+ def boolean(cell, str='')
77
65
  value_serialization 'b', cell.value.to_s, str
78
66
  end
79
67
 
@@ -81,26 +69,34 @@ module Axlsx
81
69
  # @param [Cell] cell The cell that is being serialized
82
70
  # @param [String] str The string the serialized content will be appended to.
83
71
  # @return [String]
84
- def float_type_serialization(cell, str='')
85
- numeric_type_serialization cell, str
72
+ def float(cell, str='')
73
+ numeric cell, str
86
74
  end
87
75
 
88
76
  # Serializes cells that are type integer
89
77
  # @param [Cell] cell The cell that is being serialized
90
78
  # @param [String] str The string the serialized content will be appended to.
91
79
  # @return [String]
92
- def integer_type_serialization(cell, str = '')
93
- numeric_type_serialization cell, str
80
+ def integer(cell, str = '')
81
+ numeric cell, str
94
82
  end
95
83
 
96
-
97
84
  # Serializes cells that are type formula
98
85
  # @param [Cell] cell The cell that is being serialized
99
86
  # @param [String] str The string the serialized content will be appended to.
100
87
  # @return [String]
101
88
  def formula_serialization(cell, str='')
102
- str << 't="str">' << '<f>' << cell.value.to_s.sub('=', '') << '</f>'
103
- str << '<v>' << cell.formula_value.to_s << '</v>' unless cell.formula_value.nil?
89
+ str << ('t="str"><f>' << cell.clean_value.to_s.sub('=', '') << '</f>')
90
+ str << ('<v>' << cell.formula_value.to_s << '</v>') unless cell.formula_value.nil?
91
+ end
92
+
93
+ # Serializes cells that are type array formula
94
+ # @param [Cell] cell The cell that is being serialized
95
+ # @param [String] str The string the serialized content will be appended to.
96
+ # @return [String]
97
+ def array_formula_serialization(cell, str='')
98
+ str << ('t="str">' << '<f t="array" ref="' << cell.r << '">' << cell.clean_value.to_s.sub('{=', '').sub(/}$/, '') << '</f>')
99
+ str << ('<v>' << cell.formula_value.to_s << '</v>') unless cell.formula_value.nil?
104
100
  end
105
101
 
106
102
  # Serializes cells that are type inline_string
@@ -108,7 +104,7 @@ module Axlsx
108
104
  # @param [String] str The string the serialized content will be appended to.
109
105
  # @return [String]
110
106
  def inline_string_serialization(cell, str = '')
111
- str << 't="inlineStr">' << '<is>'
107
+ str << 't="inlineStr"><is>'
112
108
  run_xml_string cell, str
113
109
  str << '</is>'
114
110
  end
@@ -117,28 +113,52 @@ module Axlsx
117
113
  # @param [Cell] cell The cell that is being serialized
118
114
  # @param [String] str The string the serialized content will be appended to.
119
115
  # @return [String]
120
- def string_type_serialization(cell, str='')
121
- if cell.is_formula?
116
+ def string(cell, str='')
117
+ if cell.is_array_formula?
118
+ array_formula_serialization cell, str
119
+ elsif cell.is_formula?
122
120
  formula_serialization cell, str
123
121
  elsif !cell.ssti.nil?
124
- value_serialization 's', cell.ssti.to_s, str
122
+ value_serialization 's', cell.ssti, str
125
123
  else
126
124
  inline_string_serialization cell, str
127
125
  end
128
126
  end
129
127
 
128
+ # Serializes cells that are of the type richtext
129
+ # @param [Cell] cell The cell that is being serialized
130
+ # @param [String] str The string the serialized content will be appended to.
131
+ # @return [String]
132
+ def richtext(cell, str)
133
+ if cell.ssti.nil?
134
+ inline_string_serialization cell, str
135
+ else
136
+ value_serialization 's', cell.ssti, str
137
+ end
138
+ end
139
+
140
+ # Serializes cells that are of the type text
141
+ # @param [Cell] cell The cell that is being serialized
142
+ # @param [String] str The string the serialized content will be appended to.
143
+ # @return [String]
144
+ def text(cell, str)
145
+ if cell.ssti.nil?
146
+ inline_string_serialization cell, str
147
+ else
148
+ value_serialization 's', cell.ssti, str
149
+ end
150
+ end
151
+
130
152
  private
131
153
 
132
- def numeric_type_serialization(cell, str = '')
133
- value_serialization 'n', cell.value.to_s, str
154
+ def numeric(cell, str = '')
155
+ value_serialization 'n', cell.value, str
134
156
  end
135
157
 
136
158
  def value_serialization(serialization_type, serialization_value, str = '')
137
- str << 't="' << serialization_type << '"' if serialization_type
138
- str << '><v>' << serialization_value << '</v>'
159
+ str << ('t="' << serialization_type.to_s << '"') if serialization_type
160
+ str << ('><v>' << serialization_value.to_s << '</v>')
139
161
  end
140
-
141
-
142
162
  end
143
163
  end
144
164
  end
@@ -54,9 +54,7 @@ module Axlsx
54
54
  # @param [String] str
55
55
  # @return [String]
56
56
  def to_xml_string(str = '')
57
- str << '<cfvo '
58
- serialized_attributes str
59
- str << ' />'
57
+ serialized_tag('cfvo', str)
60
58
  end
61
59
  end
62
60
  end
@@ -8,8 +8,11 @@ module Axlsx
8
8
  super(Cfvo)
9
9
  end
10
10
 
11
+ # Serialize the Cfvo object
12
+ # @param [String] str
13
+ # @return [String]
11
14
  def to_xml_string(str='')
12
- @list.each { |cfvo| cfvo.to_xml_string(str) }
15
+ each { |cfvo| cfvo.to_xml_string(str) }
13
16
  end
14
17
  end
15
18
  end
@@ -4,6 +4,10 @@ module Axlsx
4
4
  # The Col class defines column attributes for columns in sheets.
5
5
  class Col
6
6
 
7
+ # Maximum column width limit in MS Excel is 255 characters
8
+ # https://support.microsoft.com/en-us/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3
9
+ MAX_WIDTH = 255
10
+
7
11
  include Axlsx::OptionsParser
8
12
  include Axlsx::SerializedAttributes
9
13
  # Create a new Col objects
@@ -111,10 +115,10 @@ module Axlsx
111
115
  # TODO!!!
112
116
  #Axlsx.validate_unsigned_numeric(v) unless v == nil
113
117
  @custom_width = @best_fit = v != nil
114
- @width = v
118
+ @width = v.nil? ? v : [v, MAX_WIDTH].min
115
119
  end
116
120
 
117
- # updates the width for this col based on the cells autowidth and
121
+ # updates the width for this col based on the cells autowidth and
118
122
  # an optionally specified fixed width
119
123
  # @param [Cell] cell The cell to use in updating this col's width
120
124
  # @param [Integer] fixed_width If this is specified the width is set
@@ -122,22 +126,19 @@ module Axlsx
122
126
  # @param [Boolean] use_autowidth If this is false, the cell's
123
127
  # autowidth value will be ignored.
124
128
  def update_width(cell, fixed_width=nil, use_autowidth=true)
125
- if fixed_width.is_a? Numeric
126
- self.width = fixed_width
127
- elsif use_autowidth
128
- cell_width = cell.autowidth
129
- self.width = cell_width unless (width || 0) > (cell_width || 0)
130
- #self.width = [width || 0, cell.autowidth || 0].max
131
- end
132
- end
129
+ if fixed_width.is_a? Numeric
130
+ self.width = fixed_width
131
+ elsif use_autowidth
132
+ cell_width = cell.autowidth
133
+ self.width = cell_width unless (width || 0) > (cell_width || 0)
134
+ end
135
+ end
133
136
 
134
137
  # Serialize this columns data to an xml string
135
138
  # @param [String] str
136
139
  # @return [String]
137
140
  def to_xml_string(str = '')
138
- str << '<col '
139
- serialized_attributes str
140
- str << '/>'
141
+ serialized_tag('col', str)
141
142
  end
142
143
 
143
144
  end
@@ -16,7 +16,7 @@ module Axlsx
16
16
  # Break will be passed to the created break object.
17
17
  # @see Break
18
18
  def add_break(options)
19
- @list << Break.new(options.merge(:max => 1048575, :man => true))
19
+ self << Break.new(options.merge(:max => 1048575, :man => true))
20
20
  last
21
21
  end
22
22
 
@@ -27,7 +27,7 @@ module Axlsx
27
27
  # </colBreaks>
28
28
  def to_xml_string(str='')
29
29
  return if empty?
30
- str << '<colBreaks count="' << @list.size.to_s << '" manualBreakCount="' << @list.size.to_s << '">'
30
+ str << ('<colBreaks count="' << size.to_s << '" manualBreakCount="' << size.to_s << '">')
31
31
  each { |brk| brk.to_xml_string(str) }
32
32
  str << '</colBreaks>'
33
33
  end
@@ -1,6 +1,6 @@
1
1
  module Axlsx
2
2
 
3
- # The cols class manages the col object used to manage column widths.
3
+ # The cols class manages the col object used to manage column widths.
4
4
  # This is where the magic happens with autowidth
5
5
  class Cols < SimpleTypedList
6
6
 
@@ -10,11 +10,14 @@ module Axlsx
10
10
  @worksheet = worksheet
11
11
  end
12
12
 
13
+ # Serialize the Cols object
14
+ # @param [String] str
15
+ # @return [String]
13
16
  def to_xml_string(str = '')
14
17
  return if empty?
15
18
  str << '<cols>'
16
19
  each { |item| item.to_xml_string(str) }
17
- str << '</cols>'
20
+ str << '</cols>'
18
21
  end
19
22
  end
20
23
  end
@@ -24,11 +24,10 @@ module Axlsx
24
24
  string_attr_accessor :text, :author
25
25
  boolean_attr_accessor :visible
26
26
 
27
- # The owning Comments object
27
+ # The owning Comments object
28
28
  # @return [Comments]
29
29
  attr_reader :comments
30
30
 
31
-
32
31
  # The string based cell position reference (e.g. 'A1') that determines the positioning of this comment
33
32
  # @return [String|Cell]
34
33
  attr_reader :ref
@@ -53,7 +52,7 @@ module Axlsx
53
52
 
54
53
  # @see ref
55
54
  def ref=(v)
56
- Axlsx::DataTypeValidator.validate "Comment.ref", [String, Cell], v
55
+ Axlsx::DataTypeValidator.validate :comment_ref, [String, Cell], v
57
56
  @ref = v if v.is_a?(String)
58
57
  @ref = v.r if v.is_a?(Cell)
59
58
  end
@@ -63,15 +62,15 @@ module Axlsx
63
62
  # @return [String]
64
63
  def to_xml_string(str = "")
65
64
  author = @comments.authors[author_index]
66
- str << '<comment ref="' << ref << '" authorId="' << author_index.to_s << '">'
65
+ str << ('<comment ref="' << ref << '" authorId="' << author_index.to_s << '">')
67
66
  str << '<text>'
68
67
  unless author.to_s == ""
69
68
  str << '<r><rPr><b/><color indexed="81"/></rPr>'
70
- str << "<t>" << ::CGI.escapeHTML(author.to_s) << ":\n</t></r>"
69
+ str << ("<t>" << ::CGI.escapeHTML(author.to_s) << ":\n</t></r>")
71
70
  end
72
71
  str << '<r>'
73
72
  str << '<rPr><color indexed="81"/></rPr>'
74
- str << '<t>' << ::CGI.escapeHTML(text) << '</t></r></text>'
73
+ str << ('<t>' << ::CGI.escapeHTML(text) << '</t></r></text>')
75
74
  str << '</comment>'
76
75
  end
77
76
 
@@ -42,23 +42,22 @@ module Axlsx
42
42
  raise ArgumentError, "Comment require an author" unless options[:author]
43
43
  raise ArgumentError, "Comment requires text" unless options[:text]
44
44
  raise ArgumentError, "Comment requires ref" unless options[:ref]
45
- @list << Comment.new(self, options)
46
- yield @list.last if block_given?
47
- @list.last
45
+ self << Comment.new(self, options)
46
+ yield last if block_given?
47
+ last
48
48
  end
49
49
 
50
50
  # A sorted list of the unique authors in the contained comments
51
51
  # @return [Array]
52
52
  def authors
53
- @list.map { |comment| comment.author.to_s }.uniq.sort
53
+ map { |comment| comment.author.to_s }.uniq.sort
54
54
  end
55
55
 
56
56
  # The relationships required by this object
57
57
  # @return [Array]
58
58
  def relationships
59
59
  [Relationship.new(self, VML_DRAWING_R, "../#{vml_drawing.pn}"),
60
- Relationship.new(self, COMMENT_R, "../#{pn}"),
61
- Relationship.new(self, COMMENT_R_NULL, "NULL")]
60
+ Relationship.new(self, COMMENT_R, "../#{pn}")]
62
61
  end
63
62
 
64
63
  # serialize the object
@@ -66,14 +65,12 @@ module Axlsx
66
65
  # @return [String]
67
66
  def to_xml_string(str="")
68
67
  str << '<?xml version="1.0" encoding="UTF-8"?>'
69
- str << '<comments xmlns="' << XML_NS << '">'
70
- str << '<authors>'
68
+ str << ('<comments xmlns="' << XML_NS << '"><authors>')
71
69
  authors.each do |author|
72
- str << '<author>' << author.to_s << '</author>'
70
+ str << ('<author>' << author.to_s << '</author>')
73
71
  end
74
- str << '</authors>'
75
- str << '<commentList>'
76
- @list.each do |comment|
72
+ str << '</authors><commentList>'
73
+ each do |comment|
77
74
  comment.to_xml_string str
78
75
  end
79
76
  str << '</commentList></comments>'
@@ -74,7 +74,7 @@ module Axlsx
74
74
  # @param [String] str
75
75
  # @return [String]
76
76
  def to_xml_string(str = '')
77
- str << '<conditionalFormatting sqref="' << sqref << '">'
77
+ str << ('<conditionalFormatting sqref="' << sqref << '">')
78
78
  str << rules.collect{ |rule| rule.to_xml_string }.join(' ')
79
79
  str << '</conditionalFormatting>'
80
80
  end
@@ -210,7 +210,7 @@ module Axlsx
210
210
  str << '<cfRule '
211
211
  serialized_attributes str
212
212
  str << '>'
213
- str << '<formula>' << [*self.formula].join('</formula><formula>') << '</formula>' if @formula
213
+ str << ('<formula>' << [*self.formula].join('</formula><formula>') << '</formula>') if @formula
214
214
  @color_scale.to_xml_string(str) if @color_scale && @type == :colorScale
215
215
  @data_bar.to_xml_string(str) if @data_bar && @type == :dataBar
216
216
  @icon_set.to_xml_string(str) if @icon_set && @type == :iconSet
@@ -107,12 +107,10 @@ module Axlsx
107
107
  # @param [String] str
108
108
  # @return [String]
109
109
  def to_xml_string(str="")
110
- str << '<dataBar '
111
- serialized_attributes str
112
- str << '>'
113
- value_objects.to_xml_string(str)
114
- self.color.to_xml_string(str)
115
- str << '</dataBar>'
110
+ serialized_tag('dataBar', str) do
111
+ value_objects.to_xml_string(str)
112
+ self.color.to_xml_string(str)
113
+ end
116
114
  end
117
115
 
118
116
  private