axlsx 1.1.7 → 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 (164) hide show
  1. data/.yardopts +5 -2
  2. data/CHANGELOG.md +39 -0
  3. data/README.md +84 -46
  4. data/Rakefile +3 -2
  5. data/examples/basic_charts.rb +8 -0
  6. data/examples/chart_colors.rb +18 -3
  7. data/examples/example.rb +106 -46
  8. data/examples/example.xlsx +0 -0
  9. data/examples/example_streamed.xlsx +0 -0
  10. data/examples/extractive.pdf +0 -0
  11. data/examples/no-use_autowidth.xlsx +0 -0
  12. data/examples/scraping_html.rb +91 -0
  13. data/examples/shared_strings_example.xlsx +0 -0
  14. data/lib/axlsx/content_type/content_type.rb +2 -0
  15. data/lib/axlsx/content_type/default.rb +21 -12
  16. data/lib/axlsx/content_type/override.rb +22 -11
  17. data/lib/axlsx/doc_props/app.rb +36 -32
  18. data/lib/axlsx/doc_props/core.rb +9 -5
  19. data/lib/axlsx/drawing/ax_data_source.rb +7 -6
  20. data/lib/axlsx/drawing/axis.rb +48 -27
  21. data/lib/axlsx/drawing/bar_3D_chart.rb +49 -45
  22. data/lib/axlsx/drawing/bar_series.rb +1 -0
  23. data/lib/axlsx/drawing/cat_axis.rb +42 -38
  24. data/lib/axlsx/drawing/chart.rb +63 -52
  25. data/lib/axlsx/drawing/d_lbls.rb +100 -0
  26. data/lib/axlsx/drawing/drawing.rb +7 -4
  27. data/lib/axlsx/drawing/line_3D_chart.rb +3 -10
  28. data/lib/axlsx/drawing/num_data_source.rb +1 -1
  29. data/lib/axlsx/drawing/pie_3D_chart.rb +10 -7
  30. data/lib/axlsx/drawing/scatter_chart.rb +2 -8
  31. data/lib/axlsx/drawing/two_cell_anchor.rb +41 -9
  32. data/lib/axlsx/drawing/view_3D.rb +41 -31
  33. data/lib/axlsx/drawing/vml_drawing.rb +1 -1
  34. data/lib/axlsx/package.rb +11 -11
  35. data/lib/axlsx/rels/relationship.rb +3 -3
  36. data/lib/axlsx/stylesheet/styles.rb +1 -1
  37. data/lib/axlsx/util/constants.rb +4 -0
  38. data/lib/axlsx/util/simple_typed_list.rb +15 -8
  39. data/lib/axlsx/util/validators.rb +2 -2
  40. data/lib/axlsx/version.rb +2 -7
  41. data/lib/axlsx/workbook/defined_name.rb +174 -0
  42. data/lib/axlsx/workbook/defined_names.rb +21 -0
  43. data/lib/axlsx/workbook/workbook.rb +39 -14
  44. data/lib/axlsx/workbook/worksheet/auto_filter.rb +34 -0
  45. data/lib/axlsx/workbook/worksheet/cell.rb +25 -2
  46. data/lib/axlsx/workbook/worksheet/col.rb +15 -0
  47. data/lib/axlsx/workbook/worksheet/cols.rb +20 -0
  48. data/lib/axlsx/workbook/worksheet/comments.rb +8 -0
  49. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +25 -0
  50. data/lib/axlsx/workbook/worksheet/data_bar.rb +1 -1
  51. data/lib/axlsx/workbook/worksheet/data_validations.rb +28 -0
  52. data/lib/axlsx/workbook/worksheet/dimension.rb +65 -0
  53. data/lib/axlsx/workbook/worksheet/merged_cells.rb +35 -0
  54. data/lib/axlsx/workbook/worksheet/page_setup.rb +9 -0
  55. data/lib/axlsx/workbook/worksheet/protected_range.rb +46 -0
  56. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +34 -0
  57. data/lib/axlsx/workbook/worksheet/row.rb +1 -1
  58. data/lib/axlsx/workbook/worksheet/sheet_data.rb +25 -0
  59. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +24 -0
  60. data/lib/axlsx/workbook/worksheet/tables.rb +31 -0
  61. data/lib/axlsx/workbook/worksheet/worksheet.rb +300 -293
  62. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +57 -0
  63. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +64 -0
  64. data/lib/axlsx.rb +21 -14
  65. data/test/content_type/tc_content_type.rb +1 -6
  66. data/test/doc_props/tc_core.rb +1 -1
  67. data/test/drawing/tc_axis.rb +8 -0
  68. data/test/drawing/tc_bar_3D_chart.rb +12 -12
  69. data/test/drawing/tc_bar_series.rb +1 -2
  70. data/test/drawing/tc_chart.rb +8 -6
  71. data/test/drawing/tc_d_lbls.rb +47 -0
  72. data/test/drawing/tc_drawing.rb +5 -4
  73. data/test/drawing/tc_line_series.rb +1 -1
  74. data/test/drawing/tc_pie_3D_chart.rb +4 -8
  75. data/test/drawing/tc_pie_series.rb +1 -1
  76. data/test/drawing/tc_scatter_series.rb +1 -1
  77. data/test/drawing/tc_series.rb +1 -1
  78. data/test/drawing/tc_view_3D.rb +18 -18
  79. data/test/tc_package.rb +18 -1
  80. data/test/workbook/tc_defined_name.rb +41 -0
  81. data/test/workbook/tc_workbook.rb +5 -3
  82. data/test/workbook/worksheet/table/tc_table.rb +0 -8
  83. data/test/workbook/worksheet/tc_cell.rb +2 -4
  84. data/test/workbook/worksheet/tc_page_setup.rb +20 -3
  85. data/test/workbook/worksheet/tc_protected_range.rb +17 -0
  86. data/test/workbook/worksheet/tc_row.rb +2 -2
  87. data/test/workbook/worksheet/tc_selection.rb +1 -1
  88. data/test/workbook/worksheet/tc_worksheet.rb +53 -34
  89. metadata +93 -101
  90. data/examples/axis-titles.xlsx +0 -0
  91. data/examples/basic_charts.xlsx +0 -0
  92. data/examples/chart_colors.xlsx +0 -0
  93. data/examples/charts.xlsx +0 -0
  94. data/examples/conditional_formatting/getting_barred.xlsx +0 -0
  95. data/examples/doc/_index.html +0 -84
  96. data/examples/doc/class_list.html +0 -47
  97. data/examples/doc/css/common.css +0 -1
  98. data/examples/doc/css/full_list.css +0 -55
  99. data/examples/doc/css/style.css +0 -322
  100. data/examples/doc/file_list.html +0 -46
  101. data/examples/doc/frames.html +0 -13
  102. data/examples/doc/index.html +0 -84
  103. data/examples/doc/js/app.js +0 -205
  104. data/examples/doc/js/full_list.js +0 -173
  105. data/examples/doc/js/jquery.js +0 -16
  106. data/examples/doc/method_list.html +0 -46
  107. data/examples/doc/top-level-namespace.html +0 -95
  108. data/examples/examples_saved.xlsx +0 -0
  109. data/examples/extractive.xlsx +0 -0
  110. data/examples/fish.xlsx +0 -0
  111. data/examples/pareto.rb +0 -28
  112. data/examples/pareto.xlsx +0 -0
  113. data/examples/pie_chart_excel.xlsx +0 -0
  114. data/examples/pie_chart_saved.xlsx +0 -0
  115. data/examples/sheet_protection.xlsx +0 -0
  116. data/examples/sheet_view.xlsx +0 -0
  117. data/examples/two_cell_anchor_image.xlsx +0 -0
  118. data/examples/~$example.xlsx +0 -0
  119. data/lib/axlsx/drawing/ax_data_source.rb~ +0 -55
  120. data/lib/axlsx/drawing/data_source.rb~ +0 -51
  121. data/lib/axlsx/drawing/hlink_click.rb~ +0 -0
  122. data/lib/axlsx/drawing/hyperlink.rb~ +0 -64
  123. data/lib/axlsx/drawing/num_data.rb~ +0 -51
  124. data/lib/axlsx/drawing/num_data_source.rb~ +0 -54
  125. data/lib/axlsx/drawing/num_val.rb~ +0 -40
  126. data/lib/axlsx/drawing/picture_locking.rb~ +0 -36
  127. data/lib/axlsx/drawing/ref.rb~ +0 -41
  128. data/lib/axlsx/drawing/str_data.rb~ +0 -58
  129. data/lib/axlsx/drawing/str_val.rb~ +0 -35
  130. data/lib/axlsx/drawing/vml_drawing.rb~ +0 -6
  131. data/lib/axlsx/drawing/vml_shape.rb~ +0 -61
  132. data/lib/axlsx/util/cbf.rb +0 -333
  133. data/lib/axlsx/util/cfb.rb~ +0 -201
  134. data/lib/axlsx/util/font_tables.rb~ +0 -0
  135. data/lib/axlsx/util/ms_off_crypto.rb +0 -189
  136. data/lib/axlsx/util/ms_off_crypto.rb~ +0 -3
  137. data/lib/axlsx/util/ms_offcrypto.rb~ +0 -0
  138. data/lib/axlsx/util/parser.rb~ +0 -6
  139. data/lib/axlsx/util/storage.rb~ +0 -0
  140. data/lib/axlsx/workbook/shared_strings_table.rb~ +0 -69
  141. data/lib/axlsx/workbook/worksheet/cfvo.rb~ +0 -0
  142. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  143. data/lib/axlsx/workbook/worksheet/color_scale.rb~ +0 -46
  144. data/lib/axlsx/workbook/worksheet/comment.rb~ +0 -91
  145. data/lib/axlsx/workbook/worksheet/comments.rb~ +0 -86
  146. data/lib/axlsx/workbook/worksheet/data_bar.rb~ +0 -0
  147. data/lib/axlsx/workbook/worksheet/icon_set.rb~ +0 -95
  148. data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
  149. data/lib/axlsx/workbook/worksheet/table.rb~ +0 -97
  150. data/lib/schema/dc.xsd~ +0 -118
  151. data/lib/schema/dcterms.xsd~ +0 -331
  152. data/lib/schema/opc-coreProperties.xsd~ +0 -50
  153. data/test/drawing/tc_data_source.rb~ +0 -30
  154. data/test/drawing/tc_num_data.rb~ +0 -35
  155. data/test/drawing/tc_num_val.rb~ +0 -29
  156. data/test/drawing/tc_str_data.rb~ +0 -30
  157. data/test/drawing/tc_str_val.rb~ +0 -26
  158. data/test/drawing/tc_vml_drawing.rb~ +0 -0
  159. data/test/workbook/worksheet/table/tc_table.rb~ +0 -72
  160. data/test/workbook/worksheet/tc_cfvo.rb~ +0 -20
  161. data/test/workbook/worksheet/tc_col.rb~ +0 -10
  162. data/test/workbook/worksheet/tc_color_scale.rb~ +0 -0
  163. data/test/workbook/worksheet/tc_data_bar.rb~ +0 -0
  164. data/test/workbook/worksheet/tc_icon_set.rb~ +0 -0
@@ -12,63 +12,73 @@ module Axlsx
12
12
  # x rotation for the chart
13
13
  # must be between -90 and 90
14
14
  # @return [Integer]
15
- attr_reader :rotX
16
-
15
+ attr_reader :rot_x
16
+ alias :rotX :rot_x
17
+
17
18
  # height of chart as % of chart
18
19
  # must be between 5% and 500%
19
20
  # @return [String]
20
- attr_reader :hPercent
21
+ attr_reader :h_percent
22
+ alias :hPercent :h_percent
21
23
 
22
24
  # y rotation for the chart
23
25
  # must be between 0 and 360
24
26
  # @return [Integer]
25
- attr_reader :rotY
26
-
27
+ attr_reader :rot_y
28
+ alias :rotY :rot_y
29
+
27
30
  # depth or chart as % of chart width
28
31
  # must be between 20% and 2000%
29
32
  # @return [String]
30
- attr_reader :depthPercent
33
+ attr_reader :depth_percent
34
+ alias :depthPercent :depth_percent
31
35
 
32
36
  # Chart axis are at right angles
33
37
  # @return [Boolean]
34
- attr_reader :rAngAx
38
+ attr_reader :r_ang_ax
39
+ alias :rAngAx :r_ang_ax
35
40
 
36
41
  # field of view angle
37
42
  # @return [Integer]
38
43
  attr_reader :perspective
39
44
 
40
45
  # Creates a new View3D for charts
41
- # @option options [Integer] rotX
42
- # @option options [String] hPercent
43
- # @option options [Integer] rotY
44
- # @option options [String] depthPercent
45
- # @option options [Boolean] rAngAx
46
+ # @option options [Integer] rot_x
47
+ # @option options [String] h_percent
48
+ # @option options [Integer] rot_y
49
+ # @option options [String] depth_percent
50
+ # @option options [Boolean] r_ang_ax
46
51
  # @option options [Integer] perspective
47
52
  def initialize(options={})
48
- @rotX, @hPercent, @rotY, @depthPercent, @rAngAx, @perspective = nil, nil, nil, nil, nil, nil
53
+ @rot_x, @h_percent, @rot_y, @depth_percent, @r_ang_ax, @perspective = nil, nil, nil, nil, nil, nil
49
54
  options.each do |o|
50
55
  self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
51
56
  end
52
57
  end
53
58
 
54
- # @see rotX
55
- def rotX=(v) DataTypeValidator.validate "#{self.class}.rotX", [Integer, Fixnum], v, lambda {|arg| arg >= -90 && arg <= 90 }; @rotX = v; end
59
+ # @see rot_x
60
+ def rot_x=(v) DataTypeValidator.validate "#{self.class}.rot_x", [Integer, Fixnum], v, lambda {|arg| arg >= -90 && arg <= 90 }; @rot_x = v; end
61
+ alias :rotX= :rot_x=
56
62
 
57
- # @see hPercent
58
- def hPercent=(v)
59
- RegexValidator.validate "#{self.class}.hPercent", H_PERCENT_REGEX, v
60
- @hPercent = v
63
+ # @see h_percent
64
+ def h_percent=(v)
65
+ RegexValidator.validate "#{self.class}.h_percent", H_PERCENT_REGEX, v
66
+ @h_percent = v
61
67
  end
68
+ alias :hPercent= :h_percent=
62
69
 
63
- # @see rotY
64
- def rotY=(v) DataTypeValidator.validate "#{self.class}.rotY", [Integer, Fixnum], v, lambda {|arg| arg >= 0 && arg <= 360 }; @rotY = v; end
65
-
66
- # @see depthPercent
67
- def depthPercent=(v) RegexValidator.validate "#{self.class}.depthPercent", DEPTH_PERCENT_REGEX, v; @depthPercent = v; end
70
+ # @see rot_y
71
+ def rot_y=(v) DataTypeValidator.validate "#{self.class}.rot_y", [Integer, Fixnum], v, lambda {|arg| arg >= 0 && arg <= 360 }; @rot_y = v; end
72
+ alias :rotY= :rot_y=
68
73
 
69
- # @see rAngAx
70
- def rAngAx=(v) Axlsx::validate_boolean(v); @rAngAx = v; end
74
+ # @see depth_percent
75
+ def depth_percent=(v) RegexValidator.validate "#{self.class}.depth_percent", DEPTH_PERCENT_REGEX, v; @depth_percent = v; end
76
+ alias :depthPercent= :depth_percent=
71
77
 
78
+ # @see r_ang_ax
79
+ def r_ang_ax=(v) Axlsx::validate_boolean(v); @r_ang_ax = v; end
80
+ alias :rAngAx= :r_ang_ax=
81
+
72
82
  # @see perspective
73
83
  def perspective=(v) DataTypeValidator.validate "#{self.class}.perspective", [Integer, Fixnum], v, lambda {|arg| arg >= 0 && arg <= 240 }; @perspective = v; end
74
84
 
@@ -78,11 +88,11 @@ module Axlsx
78
88
  # @return [String]
79
89
  def to_xml_string(str = '')
80
90
  str << '<c:view3D>'
81
- str << '<c:rotX val="' << @rotX.to_s << '"/>' unless @rotX.nil?
82
- str << '<c:hPercent val="' << @hPercent.to_s << '"/>' unless @hPercent.nil?
83
- str << '<c:rotY val="' << @rotY.to_s << '"/>' unless @rotY.nil?
84
- str << '<c:depthPercent val="' << @depthPercent.to_s << '"/>' unless @depthPercent.nil?
85
- str << '<c:rAngAx val="' << @rAngAx.to_s << '"/>' unless @rAngAx.nil?
91
+ str << '<c:rotX val="' << @rot_x.to_s << '"/>' unless @rot_x.nil?
92
+ str << '<c:hPercent val="' << @h_percent.to_s << '"/>' unless @h_percent.nil?
93
+ str << '<c:rotY val="' << @rot_y.to_s << '"/>' unless @rot_y.nil?
94
+ str << '<c:depthPercent val="' << @depth_percent.to_s << '"/>' unless @depth_percent.nil?
95
+ str << '<c:rAngAx val="' << @r_ang_ax.to_s << '"/>' unless @r_ang_ax.nil?
86
96
  str << '<c:perspective val="' << @perspective.to_s << '"/>' unless @perspective.nil?
87
97
  str << '</c:view3D>'
88
98
  end
@@ -4,7 +4,7 @@ module Axlsx
4
4
  class VmlDrawing
5
5
 
6
6
  # creates a new Vml Drawing object.
7
- # @param [Comments] the comments object this drawing is associated with
7
+ # @param [Comments] comments the comments object this drawing is associated with
8
8
  def initialize(comments)
9
9
  raise ArgumentError, "you must provide a comments object" unless comments.is_a?(Comments)
10
10
  @comments = comments
data/lib/axlsx/package.rb CHANGED
@@ -156,18 +156,18 @@ module Axlsx
156
156
  # @return [Zip::ZipOutputStream]
157
157
  def write_parts(zip)
158
158
  p = parts
159
- p.each do |part|
160
- unless part[:doc].nil?
161
- zip.put_next_entry(part[:entry]);
162
- entry = ['1.9.2', '1.9.3'].include?(RUBY_VERSION) ? part[:doc].force_encoding('BINARY') : part[:doc]
163
- zip.puts(entry)
164
- end
165
- unless part[:path].nil?
166
- zip.put_next_entry(part[:entry]);
167
- # binread for 1.9.3
168
- zip.write IO.respond_to?(:binread) ? IO.binread(part[:path]) : IO.read(part[:path])
169
- end
159
+ p.each do |part|
160
+ unless part[:doc].nil?
161
+ zip.put_next_entry(part[:entry])
162
+ entry = ['1.9.2', '1.9.3'].include?(RUBY_VERSION) ? part[:doc].force_encoding('BINARY') : part[:doc]
163
+ zip.puts(entry)
170
164
  end
165
+ unless part[:path].nil?
166
+ zip.put_next_entry(part[:entry]);
167
+ # binread for 1.9.3
168
+ zip.write IO.respond_to?(:binread) ? IO.binread(part[:path]) : IO.read(part[:path])
169
+ end
170
+ end
171
171
  zip
172
172
  end
173
173
 
@@ -30,9 +30,9 @@ module Axlsx
30
30
  attr_reader :TargetMode
31
31
 
32
32
  # creates a new relationship
33
- # @param [String] Type The type of the relationship
34
- # @param [String] Target The target for the relationship
35
- # @option [Symbol] target_mode only accepts :external.
33
+ # @param [String] type The type of the relationship
34
+ # @param [String] target The target for the relationship
35
+ # @option [Symbol] :target_mode only accepts :external.
36
36
  def initialize(type, target, options={})
37
37
  self.Target=target
38
38
  self.Type=type
@@ -133,7 +133,7 @@ module Axlsx
133
133
  # @option options [Integer] family The font family to use.
134
134
  # @option options [String] font_name The name of the font to use
135
135
  # @option options [Integer] num_fmt The number format to apply
136
- # @option options [String] format_code The formatting to apply. If this is specified, num_fmt is ignored.
136
+ # @option options [String] format_code The formatting to apply.
137
137
  # @option options [Integer|Hash] border The border style to use.
138
138
  # @option options [String] bg_color The background color to apply to the cell
139
139
  # @option options [Boolean] hidden Indicates if the cell should be hidden
@@ -249,6 +249,10 @@ module Axlsx
249
249
 
250
250
  # error message for sheets that use a name which is longer than 31 bytes
251
251
  ERR_SHEET_NAME_TOO_LONG = "Your worksheet name '%s' is too long. Worksheet names must be 31 characters (bytes) or less"
252
+
253
+ # error message for sheets that use a name which includes a colon
254
+
255
+ ERR_SHEET_NAME_COLON_FORBIDDEN = "Your worksheet name '%s' contains a colon, which is not allowed by MS Excel and will cause repair warnings. Please change the name of your sheet."
252
256
 
253
257
  # error message for duplicate sheet names
254
258
  ERR_DUPLICATE_SHEET_NAME = "There is already a worksheet in this workbook named '%s'. Please use a unique name"
@@ -18,7 +18,7 @@ module Axlsx
18
18
 
19
19
  # Creats a new typed list
20
20
  # @param [Array, Class] type An array of Class objects or a single Class object
21
- # @param [String] serialize The tag name to use in serialization
21
+ # @param [String] serialize_as The tag name to use in serialization
22
22
  # @raise [ArgumentError] if all members of type are not Class objects
23
23
  def initialize type, serialize_as=nil
24
24
  if type.is_a? Array
@@ -50,6 +50,18 @@ module Axlsx
50
50
  @locked_at = nil
51
51
  self
52
52
  end
53
+
54
+ # join operator
55
+ # @param [Array] v the array to join
56
+ # @raise [ArgumentError] if any of the values being joined are not
57
+ # one of the allowed types
58
+ # @return [SimpleTypedList]
59
+ def +(v)
60
+ v.each do |item|
61
+ DataTypeValidator.validate "SimpleTypedList.+", @allowed_types, item
62
+ @list << item
63
+ end
64
+ end
53
65
 
54
66
  # Concat operator
55
67
  # @param [Any] v the data to be added
@@ -60,12 +72,7 @@ module Axlsx
60
72
  @list << v
61
73
  @list.size - 1
62
74
  end
63
-
64
- # alternate of << method
65
- # @see <<
66
- def push(v)
67
- self.<< v
68
- end
75
+ alias :push :<<
69
76
 
70
77
  # delete the item from the list
71
78
  # @param [Any] v The item to be deleted.
@@ -149,7 +156,7 @@ module Axlsx
149
156
 
150
157
  def to_xml_string(str = '')
151
158
  classname = @allowed_types[0].name.split('::').last
152
- el_name = serialize_as || (classname[0,1].downcase + classname[1..-1])
159
+ el_name = serialize_as.to_s || (classname[0,1].downcase + classname[1..-1])
153
160
  str << '<' << el_name << ' count="' << @list.size.to_s << '">'
154
161
  @list.each { |item| item.to_xml_string(str) }
155
162
  str << '</' << el_name << '>'
@@ -181,7 +181,7 @@ module Axlsx
181
181
  # Requires that the value is a valid scatterStyle
182
182
  # must be one of :none | :line | :lineMarker | :marker | :smooth | :smoothMarker
183
183
  # must be one of "none" | "line" | "lineMarker" | "marker" | "smooth" | "smoothMarker"
184
- # @param [Symbol|String] the value to validate
184
+ # @param [Symbol|String] v the value to validate
185
185
  def self.validate_scatter_style(v)
186
186
  Axlsx::RestrictionValidator.validate "ScatterChart.scatterStyle", [:none, :line, :lineMarker, :marker, :smooth, :smoothMarker], v.to_sym
187
187
  end
@@ -262,4 +262,4 @@ module Axlsx
262
262
  def self.validate_split_state_type(v)
263
263
  RestrictionValidator.validate :split_state_type, [:frozen, :frozen_split, :split], v
264
264
  end
265
- end
265
+ end
data/lib/axlsx/version.rb CHANGED
@@ -1,10 +1,5 @@
1
1
  # encoding: UTF-8
2
2
  module Axlsx
3
-
4
- # The version of the gem
5
- # When using bunle exec rake and referencing the gem on github or locally
6
- # it will use the gemspec, which preloads this constant for the gem's version.
7
- # We check to make sure that it has not already been loaded
8
- VERSION="1.1.7" unless defined? Axlsx::VERSION
9
-
3
+ # The current version
4
+ VERSION="1.2.0"
10
5
  end
@@ -0,0 +1,174 @@
1
+ # <definedNames>
2
+ # <definedName name="_xlnm.Print_Titles" localSheetId="0">Sheet1!$1:$1</definedName>
3
+ # </definedNames>
4
+
5
+ #<xsd:complexType name="CT_DefinedName">
6
+ # <xsd:simpleContent>
7
+ # <xsd:extension base="ST_Formula">
8
+ # <xsd:attribute name="name" type="s:ST_Xstring" use="required"/>
9
+ # <xsd:attribute name="comment" type="s:ST_Xstring" use="optional"/>
10
+ # <xsd:attribute name="customMenu" type="s:ST_Xstring" use="optional"/>
11
+ # <xsd:attribute name="description" type="s:ST_Xstring" use="optional"/>
12
+ # <xsd:attribute name="help" type="s:ST_Xstring" use="optional"/>
13
+ # <xsd:attribute name="statusBar" type="s:ST_Xstring" use="optional"/>
14
+ # <xsd:attribute name="localSheetId" type="xsd:unsignedInt" use="optional"/>
15
+ # <xsd:attribute name="hidden" type="xsd:boolean" use="optional" default="false"/>
16
+ # <xsd:attribute name="function" type="xsd:boolean" use="optional" default="false"/>
17
+ # <xsd:attribute name="vbProcedure" type="xsd:boolean" use="optional" default="false"/>
18
+ # <xsd:attribute name="xlm" type="xsd:boolean" use="optional" default="false"/>
19
+ # <xsd:attribute name="functionGroupId" type="xsd:unsignedInt" use="optional"/>
20
+ # <xsd:attribute name="shortcutKey" type="s:ST_Xstring" use="optional"/>
21
+ # <xsd:attribute name="publishToServer" type="xsd:boolean" use="optional" default="false"/>
22
+ # <xsd:attribute name="workbookParameter" type="xsd:boolean" use="optional" default="false"/>
23
+ # </xsd:extenstion>
24
+ # </xsd:simpleContent>
25
+
26
+ module Axlsx
27
+ # This element defines the defined names that are defined within this workbook.
28
+ # Defined names are descriptive text that is used to represents a cell, range of cells, formula, or constant value.
29
+ # Use easy-to-understand names, such as Products, to refer to hard to understand ranges, such as Sales!C20:C30.
30
+ # A defined name in a formula can make it easier to understand the purpose of the formula.
31
+ # @example
32
+ # The formula =SUM(FirstQuarterSales) might be easier to identify than =SUM(C20:C30
33
+ #
34
+ # Names are available to any sheet.
35
+ # @example
36
+ # If the name ProjectedSales refers to the range A20:A30 on the first worksheet in a workbook,
37
+ # you can use the name ProjectedSales on any other sheet in the same workbook to refer to range A20:A30 on the first worksheet.
38
+ # Names can also be used to represent formulas or values that do not change (constants).
39
+ #
40
+ # @example
41
+ # The name SalesTax can be used to represent the sales tax amount (such as 6.2 percent) applied to sales transactions.
42
+ # You can also link to a defined name in another workbook, or define a name that refers to cells in another workbook.
43
+ #
44
+ # @example
45
+ # The formula =SUM(Sales.xls!ProjectedSales) refers to the named range ProjectedSales in the workbook named Sales.
46
+ # A compliant producer or consumer considers a defined name in the range A1-XFD1048576 to be an error.
47
+ # All other names outside this range can be defined as names and overrides a cell reference if an ambiguity exists.
48
+ #
49
+ # @example
50
+ # For clarification: LOG10 is always a cell reference, LOG10() is always formula, LOGO1000 can be a defined name that overrides a cell reference.
51
+ class DefinedName
52
+ # creates a new DefinedName.
53
+ # @param [String] formula - the formula the defined name references
54
+ # @param [Hash] options - A hash of key/value pairs that will be mapped to this instances attributes.
55
+ #
56
+ # @option [String] name - Specifies the name that appears in the user interface for the defined name.
57
+ # This attribute is required.
58
+ # The following built-in names are defined in this SpreadsheetML specification:
59
+ # Print
60
+ # _xlnm.Print_Area: this defined name specifies the workbook's print area.
61
+ # _xlnm.Print_Titles: this defined name specifies the row(s) or column(s) to repeat
62
+ # the top of each printed page.
63
+ # Filter & Advanced Filter
64
+ # _xlnm.Criteria: this defined name refers to a range containing the criteria values
65
+ # to be used in applying an advanced filter to a range of data.
66
+ # _xlnm._FilterDatabase: can be one of the following
67
+ # a. this defined name refers to a range to which an advanced filter has been
68
+ # applied. This represents the source data range, unfiltered.
69
+ # b. This defined name refers to a range to which an AutoFilter has been
70
+ # applied.
71
+ # _xlnm.Extract: this defined name refers to the range containing the filtered output
72
+ # values resulting from applying an advanced filter criteria to a source range.
73
+ # Miscellaneous
74
+ # _xlnm.Consolidate_Area: the defined name refers to a consolidation area.
75
+ # _xlnm.Database: the range specified in the defined name is from a database data source.
76
+ # _xlnm.Sheet_Title: the defined name refers to a sheet title.
77
+ # @option [String] comment - A comment to optionally associate with the name
78
+ # @option [String] custom_menu - The menu text for the defined name
79
+ # @option [String] description - An optional description for the defined name
80
+ # @option [String] help - The help topic to display for this defined name
81
+ # @option [String] status_bar - The text to display on the application status bar when this defined name has focus
82
+ # @option [String] local_sheet_id - Specifies the sheet index in this workbook where data from an external reference is displayed
83
+ # @option [Boolean] hidden - Specifies a boolean value that indicates whether the defined name is hidden in the user interface.
84
+ # @option [Boolean] function - Specifies a boolean value that indicates that the defined name refers to a user-defined function.
85
+ # This attribute is used when there is an add-in or other code project associated with the file.
86
+ # @option [Boolean] vb_proceedure - Specifies a boolean value that indicates whether the defined name is related to an external function, command, or other executable code.
87
+ # @option [Boolean] xlm - Specifies a boolean value that indicates whether the defined name is related to an external function, command, or other executable code.
88
+ # @option [Integer] function_group_id - Specifies the function group index if the defined name refers to a function.
89
+ # The function group defines the general category for the function.
90
+ # This attribute is used when there is an add-in or other code project associated with the file.
91
+ # See Open Office XML Part 1 for more info.
92
+ # @option [String] short_cut_key - Specifies the keyboard shortcut for the defined name.
93
+ # @option [Boolean] publish_to_server - Specifies a boolean value that indicates whether the defined name is included in the
94
+ # version of the workbook that is published to or rendered on a Web or application server.
95
+ # @option [Boolean] workbook_parameter - Specifies a boolean value that indicates that the name is used as a workbook parameter on a
96
+ # version of the workbook that is published to or rendered on a Web or application server.
97
+ def initialize(formula, options={})
98
+ @formula = formula
99
+ options.each do |o|
100
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
101
+ end
102
+ end
103
+ attr_reader :local_sheet_id
104
+
105
+ # The local sheet index (0-based)
106
+ # @param [Integer] value the unsinged integer index of the sheet this defined_name applies to.
107
+ def local_sheet_id=(value)
108
+ Axlsx::validate_unsigned_int(value)
109
+ @local_sheet_id = value
110
+ end
111
+
112
+ # string attributes that will be added when this class is evaluated
113
+ STRING_ATTRIBUTES = [:short_cut_key, :status_bar, :help, :description, :custom_menu, :comment]
114
+
115
+ # boolean attributes that will be added when this class is evaluated
116
+ BOOLEAN_ATTRIBUTES = [:workbook_parameter, :publish_to_server, :xlm, :vb_proceedure, :function, :hidden]
117
+
118
+ # Dynamically create string attribute accessors
119
+ STRING_ATTRIBUTES.each do |attr|
120
+ class_eval %{
121
+ # The #{attr} attribute reader
122
+ # @return [String]
123
+ attr_reader :#{attr}
124
+
125
+ # The #{attr} writer
126
+ # @param [String] value The value to assign to #{attr}
127
+ # @return [String]
128
+ def #{attr}=(value)
129
+ Axlsx::validate_string(value)
130
+ @#{attr}= value
131
+ end
132
+ }
133
+ end
134
+
135
+ # Dynamically create boolean attribute accessors
136
+ BOOLEAN_ATTRIBUTES.each do |attr|
137
+ class_eval %{
138
+ # The #{attr} attribute reader
139
+ # @return [Boolean]
140
+ attr_reader :#{attr}
141
+
142
+ # The #{attr} writer
143
+ # @param [Boolean] value The value to assign to #{attr}
144
+ # @return [Boolean]
145
+ def #{attr}=(value)
146
+ Axlsx::validate_boolean(value)
147
+ @#{attr} = value
148
+ end
149
+ }
150
+ end
151
+
152
+ attr_reader :name
153
+ # The name of this defined name. Please refer to the class documentation for more information
154
+ def name=(value)
155
+ Axlsx::RestrictionValidator.validate 'DefinedName.name', %w(_xlnm.Print_Area _xlnm.Print_Titles _xlnm.Criteria _xlnm._FilterDatabase _xlnm.Extract _xlnm.Consolidate_Area _xlnm.Database _xlnm.Sheet_Title), value
156
+ @name = value
157
+ end
158
+
159
+ # The formula this defined name references
160
+ attr_reader :formula
161
+
162
+ def to_xml_string(str='')
163
+ raise ArgumentError, 'you must specify the name for this defined name. Please read the documentation for Axlsx::DefinedName for more details' unless name
164
+ str << '<definedName'
165
+ instance_values.each do |name, value|
166
+ unless name == 'formula'
167
+ str << ' ' << Axlsx::camel(name, false) << "='#{value}'"
168
+ end
169
+ end
170
+ str << '>' << @formula
171
+ str << '</definedName>'
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,21 @@
1
+ module Axlsx
2
+ # a simple types list of DefinedName objects
3
+ class DefinedNames < SimpleTypedList
4
+
5
+ # creates the DefinedNames object
6
+ def initialize
7
+ super DefinedName
8
+ end
9
+
10
+ # Serialize to xml
11
+ # @param [String] str
12
+ # @return [String]
13
+ def to_xml_string(str = '')
14
+ return if @list.empty?
15
+ str << "<definedNames>"
16
+ each { |defined_name| defined_name.to_xml_string(str) }
17
+ str << '</definedNames>'
18
+ end
19
+ end
20
+ end
21
+
@@ -1,7 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module Axlsx
3
-
3
+ require 'axlsx/workbook/worksheet/auto_filter.rb'
4
4
  require 'axlsx/workbook/worksheet/date_time_converter.rb'
5
+ require 'axlsx/workbook/worksheet/protected_range.rb'
6
+ require 'axlsx/workbook/worksheet/protected_ranges.rb'
5
7
  require 'axlsx/workbook/worksheet/cell.rb'
6
8
  require 'axlsx/workbook/worksheet/page_margins.rb'
7
9
  require 'axlsx/workbook/worksheet/page_setup.rb'
@@ -12,15 +14,27 @@ require 'axlsx/workbook/worksheet/data_bar.rb'
12
14
  require 'axlsx/workbook/worksheet/icon_set.rb'
13
15
  require 'axlsx/workbook/worksheet/conditional_formatting.rb'
14
16
  require 'axlsx/workbook/worksheet/conditional_formatting_rule.rb'
17
+ require 'axlsx/workbook/worksheet/conditional_formattings.rb'
15
18
  require 'axlsx/workbook/worksheet/row.rb'
16
19
  require 'axlsx/workbook/worksheet/col.rb'
20
+ require 'axlsx/workbook/worksheet/cols.rb'
17
21
  require 'axlsx/workbook/worksheet/comments.rb'
18
22
  require 'axlsx/workbook/worksheet/comment.rb'
23
+ require 'axlsx/workbook/worksheet/merged_cells.rb'
19
24
  require 'axlsx/workbook/worksheet/sheet_protection.rb'
25
+ require 'axlsx/workbook/worksheet/sheet_pr.rb'
26
+ require 'axlsx/workbook/worksheet/dimension.rb'
27
+ require 'axlsx/workbook/worksheet/sheet_data.rb'
28
+ require 'axlsx/workbook/worksheet/worksheet_drawing.rb'
29
+ require 'axlsx/workbook/worksheet/worksheet_comments.rb'
20
30
  require 'axlsx/workbook/worksheet/worksheet.rb'
21
31
  require 'axlsx/workbook/shared_strings_table.rb'
32
+ require 'axlsx/workbook/defined_name.rb'
33
+ require 'axlsx/workbook/defined_names.rb'
22
34
  require 'axlsx/workbook/worksheet/table.rb'
35
+ require 'axlsx/workbook/worksheet/tables.rb'
23
36
  require 'axlsx/workbook/worksheet/data_validation.rb'
37
+ require 'axlsx/workbook/worksheet/data_validations.rb'
24
38
  require 'axlsx/workbook/worksheet/sheet_view.rb'
25
39
  require 'axlsx/workbook/worksheet/pane.rb'
26
40
  require 'axlsx/workbook/worksheet/selection.rb'
@@ -101,13 +115,21 @@ require 'axlsx/workbook/worksheet/selection.rb'
101
115
  attr_reader :tables
102
116
 
103
117
 
104
- # A colllection of comments associated with this workbook
105
- # @note The recommended way to manage comments is Worksheet#add_comment
118
+ # A collection of defined names for this workbook
119
+ # @note The recommended way to manage defined names is Workbook#add_defined_name
120
+ # @see DefinedName
121
+ # @return [DefinedNames]
122
+ def defined_names
123
+ @defined_names ||= DefinedNames.new
124
+ end
125
+
126
+ # A collection of comments associated with this workbook
127
+ # @note The recommended way to manage comments is WOrksheet#add_comment
106
128
  # @see Worksheet#add_comment
107
129
  # @see Comment
108
130
  # @return [Comments]
109
131
  def comments
110
- self.worksheets.map { |ws| ws.comments }.compact
132
+ worksheets.map { |sheet| sheet.comments }.compact
111
133
  end
112
134
 
113
135
  # The styles associated with this workbook
@@ -143,7 +165,6 @@ require 'axlsx/workbook/worksheet/selection.rb'
143
165
  @drawings = SimpleTypedList.new Drawing
144
166
  @charts = SimpleTypedList.new Chart
145
167
  @images = SimpleTypedList.new Pic
146
-
147
168
  # Are these even used????? Check package serialization parts
148
169
  @tables = SimpleTypedList.new Table
149
170
  @comments = SimpleTypedList.new Comments
@@ -191,6 +212,14 @@ require 'axlsx/workbook/worksheet/selection.rb'
191
212
  worksheet
192
213
  end
193
214
 
215
+ # Adds a defined name to this workbook
216
+ # @return [DefinedName]
217
+ # @param [String] formula @see DefinedName
218
+ # @param [Hash] options @see DefinedName
219
+ def add_defined_name(formula, options)
220
+ defined_names << DefinedName.new(formula, options)
221
+ end
222
+
194
223
  # The workbook relationships. This is managed automatically by the workbook
195
224
  # @return [Relationships]
196
225
  def relationships
@@ -232,17 +261,13 @@ require 'axlsx/workbook/worksheet/selection.rb'
232
261
  str << '<workbookPr date1904="' << @@date1904.to_s << '"/>'
233
262
  str << '<sheets>'
234
263
  @worksheets.each_with_index do |sheet, index|
235
- str << '<sheet name="' << sheet.name << '" sheetId="' << (index+1).to_s << '" r:id="' << sheet.rId << '"/>'
236
- end
237
- str << '</sheets>'
238
- str << '<definedNames>'
239
- @worksheets.each_with_index do |sheet, index|
240
- if sheet.auto_filter
241
- str << '<definedName name="_xlnm._FilterDatabase" localSheetId="' << index.to_s << '" hidden="1">'
242
- str << sheet.abs_auto_filter << '</definedName>'
264
+ str << '<sheet name="' << sheet.name << '" sheetId="' << (index+1).to_s << '" r:id="' << sheet.rId << '"/>'
265
+ if defined_name = sheet.auto_filter.defined_name
266
+ add_defined_name defined_name, :name => '_xlnm._FilterDatabase', :local_sheet_id => index, :hidden => 1
243
267
  end
244
268
  end
245
- str << '</definedNames>'
269
+ str << '</sheets>'
270
+ defined_names.to_xml_string(str)
246
271
  str << '</workbook>'
247
272
  end
248
273
 
@@ -0,0 +1,34 @@
1
+ module Axlsx
2
+
3
+ #This class represents an auto filter range in a worksheet
4
+ class AutoFilter
5
+
6
+ # creates a new Autofilter object
7
+ # @param [Worksheet] worksheet
8
+ def initialize(worksheet)
9
+ raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet)
10
+ @worksheet = worksheet
11
+ end
12
+
13
+ attr_reader :worksheet
14
+
15
+ # The range the autofilter should be applied to.
16
+ # This should be a string like 'A1:B8'
17
+ # @return [String]
18
+ attr_accessor :range
19
+
20
+ # the formula for the defined name required for this auto filter
21
+ # @return [String]
22
+ def defined_name
23
+ return unless range
24
+ Axlsx.cell_range(range.split(':').collect { |name| worksheet.name_to_cell(name)})
25
+ end
26
+
27
+ # serialize the object
28
+ # @return [String]
29
+ def to_xml_string(str='')
30
+ str << "<autoFilter ref='#{range}'></autoFilter>"
31
+ end
32
+
33
+ end
34
+ end
@@ -142,7 +142,7 @@ module Axlsx
142
142
  # The inline color property for the cell
143
143
  # @return [Color]
144
144
  attr_reader :color
145
- # @param [String] The 8 character representation for an rgb color #FFFFFFFF"
145
+ # @param [String] v The 8 character representation for an rgb color #FFFFFFFF"
146
146
  def color=(v)
147
147
  @color = v.is_a?(Color) ? v : Color.new(:rgb=>v)
148
148
  @is_text_run = true
@@ -318,11 +318,34 @@ module Axlsx
318
318
  end
319
319
 
320
320
  def is_formula?
321
- @type == :string && @value.start_with?('=')
321
+ @type == :string && @value.to_s.start_with?('=')
322
322
  end
323
323
 
324
+ # This is still not perfect...
325
+ # - scaling is not linear as font sizes increst
326
+ # - different fonts have different mdw and char widths
327
+ def autowidth
328
+ return if is_formula? || value == nil
329
+ mdw = 1.78 #This is the widest width of 0..9 in arial@10px)
330
+ font_scale = (font_size/10.0).to_f
331
+ ((value.to_s.count(Worksheet.thin_chars) * mdw + 5) / mdw * 256) / 256.0 * font_scale
332
+ end
333
+
334
+ # returns the absolute or relative string style reference for
335
+ # this cell.
336
+ # @param [Boolean] absolute -when false a relative reference will be
337
+ # returned.
338
+ # @return [String]
339
+ def reference(absolute=true)
340
+ absolute ? r_abs : r
341
+ end
342
+
324
343
  private
325
344
 
345
+ def font_size
346
+ sz || @styles.fonts[@styles.cellXfs[style].fontId].sz
347
+ end
348
+
326
349
  # Utility method for setting inline style attributes
327
350
  def set_run_style( validator, attr, value)
328
351
  return unless INLINE_STYLES.include?(attr.to_s)