axlsx 1.1.6 → 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 (173) hide show
  1. data/.yardopts +5 -2
  2. data/CHANGELOG.md +39 -0
  3. data/README.md +97 -46
  4. data/Rakefile +3 -2
  5. data/examples/basic_charts.rb +54 -0
  6. data/examples/chart_colors.rb +18 -3
  7. data/examples/example.rb +118 -42
  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/examples/sheet_view.rb +34 -0
  15. data/lib/axlsx/content_type/content_type.rb +2 -0
  16. data/lib/axlsx/content_type/default.rb +21 -12
  17. data/lib/axlsx/content_type/override.rb +22 -11
  18. data/lib/axlsx/doc_props/app.rb +36 -32
  19. data/lib/axlsx/doc_props/core.rb +9 -5
  20. data/lib/axlsx/drawing/ax_data_source.rb +7 -6
  21. data/lib/axlsx/drawing/axis.rb +64 -25
  22. data/lib/axlsx/drawing/bar_3D_chart.rb +49 -45
  23. data/lib/axlsx/drawing/bar_series.rb +1 -0
  24. data/lib/axlsx/drawing/cat_axis.rb +43 -39
  25. data/lib/axlsx/drawing/chart.rb +65 -54
  26. data/lib/axlsx/drawing/d_lbls.rb +100 -0
  27. data/lib/axlsx/drawing/drawing.rb +7 -5
  28. data/lib/axlsx/drawing/graphic_frame.rb +2 -2
  29. data/lib/axlsx/drawing/line_3D_chart.rb +3 -10
  30. data/lib/axlsx/drawing/num_data.rb +2 -2
  31. data/lib/axlsx/drawing/num_data_source.rb +1 -1
  32. data/lib/axlsx/drawing/pie_3D_chart.rb +10 -7
  33. data/lib/axlsx/drawing/scatter_chart.rb +2 -8
  34. data/lib/axlsx/drawing/title.rb +21 -9
  35. data/lib/axlsx/drawing/two_cell_anchor.rb +41 -9
  36. data/lib/axlsx/drawing/view_3D.rb +41 -31
  37. data/lib/axlsx/drawing/vml_drawing.rb +1 -1
  38. data/lib/axlsx/package.rb +20 -16
  39. data/lib/axlsx/rels/relationship.rb +3 -3
  40. data/lib/axlsx/stylesheet/styles.rb +1 -1
  41. data/lib/axlsx/util/constants.rb +4 -0
  42. data/lib/axlsx/util/simple_typed_list.rb +15 -8
  43. data/lib/axlsx/util/validators.rb +28 -4
  44. data/lib/axlsx/version.rb +2 -7
  45. data/lib/axlsx/workbook/defined_name.rb +174 -0
  46. data/lib/axlsx/workbook/defined_names.rb +21 -0
  47. data/lib/axlsx/workbook/workbook.rb +46 -17
  48. data/lib/axlsx/workbook/worksheet/auto_filter.rb +34 -0
  49. data/lib/axlsx/workbook/worksheet/cell.rb +28 -1
  50. data/lib/axlsx/workbook/worksheet/col.rb +15 -0
  51. data/lib/axlsx/workbook/worksheet/cols.rb +20 -0
  52. data/lib/axlsx/workbook/worksheet/comments.rb +8 -0
  53. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +25 -0
  54. data/lib/axlsx/workbook/worksheet/data_bar.rb +1 -1
  55. data/lib/axlsx/workbook/worksheet/data_validations.rb +28 -0
  56. data/lib/axlsx/workbook/worksheet/dimension.rb +65 -0
  57. data/lib/axlsx/workbook/worksheet/merged_cells.rb +35 -0
  58. data/lib/axlsx/workbook/worksheet/page_setup.rb +10 -1
  59. data/lib/axlsx/workbook/worksheet/pane.rb +144 -0
  60. data/lib/axlsx/workbook/worksheet/protected_range.rb +46 -0
  61. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +34 -0
  62. data/lib/axlsx/workbook/worksheet/row.rb +1 -1
  63. data/lib/axlsx/workbook/worksheet/selection.rb +111 -0
  64. data/lib/axlsx/workbook/worksheet/sheet_data.rb +25 -0
  65. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +24 -0
  66. data/lib/axlsx/workbook/worksheet/sheet_view.rb +379 -0
  67. data/lib/axlsx/workbook/worksheet/tables.rb +31 -0
  68. data/lib/axlsx/workbook/worksheet/worksheet.rb +315 -291
  69. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +57 -0
  70. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +64 -0
  71. data/lib/axlsx.rb +24 -16
  72. data/test/content_type/tc_content_type.rb +1 -6
  73. data/test/doc_props/tc_core.rb +1 -1
  74. data/test/drawing/tc_axis.rb +27 -2
  75. data/test/drawing/tc_bar_3D_chart.rb +12 -12
  76. data/test/drawing/tc_bar_series.rb +1 -2
  77. data/test/drawing/tc_cat_axis.rb +3 -3
  78. data/test/drawing/tc_chart.rb +8 -6
  79. data/test/drawing/tc_d_lbls.rb +47 -0
  80. data/test/drawing/tc_drawing.rb +5 -4
  81. data/test/drawing/tc_line_series.rb +1 -1
  82. data/test/drawing/tc_num_data.rb +5 -1
  83. data/test/drawing/tc_pie_3D_chart.rb +4 -8
  84. data/test/drawing/tc_pie_series.rb +1 -1
  85. data/test/drawing/tc_scatter_series.rb +1 -1
  86. data/test/drawing/tc_series.rb +1 -1
  87. data/test/drawing/tc_title.rb +16 -0
  88. data/test/drawing/tc_view_3D.rb +18 -18
  89. data/test/tc_package.rb +41 -4
  90. data/test/util/tc_validators.rb +68 -11
  91. data/test/workbook/tc_defined_name.rb +41 -0
  92. data/test/workbook/tc_workbook.rb +5 -3
  93. data/test/workbook/worksheet/table/tc_table.rb +0 -8
  94. data/test/workbook/worksheet/tc_cell.rb +2 -4
  95. data/test/workbook/worksheet/tc_page_setup.rb +20 -3
  96. data/test/workbook/worksheet/tc_pane.rb +94 -0
  97. data/test/workbook/worksheet/tc_protected_range.rb +17 -0
  98. data/test/workbook/worksheet/tc_row.rb +2 -2
  99. data/test/workbook/worksheet/tc_selection.rb +94 -0
  100. data/test/workbook/worksheet/tc_sheet_view.rb +214 -0
  101. data/test/workbook/worksheet/tc_worksheet.rb +53 -41
  102. metadata +104 -97
  103. data/examples/chart_colors.xlsx +0 -0
  104. data/examples/conditional_formatting/getting_barred.xlsx +0 -0
  105. data/examples/doc/_index.html +0 -84
  106. data/examples/doc/class_list.html +0 -47
  107. data/examples/doc/css/common.css +0 -1
  108. data/examples/doc/css/full_list.css +0 -55
  109. data/examples/doc/css/style.css +0 -322
  110. data/examples/doc/file_list.html +0 -46
  111. data/examples/doc/frames.html +0 -13
  112. data/examples/doc/index.html +0 -84
  113. data/examples/doc/js/app.js +0 -205
  114. data/examples/doc/js/full_list.js +0 -173
  115. data/examples/doc/js/jquery.js +0 -16
  116. data/examples/doc/method_list.html +0 -46
  117. data/examples/doc/top-level-namespace.html +0 -95
  118. data/examples/examples_saved.xlsx +0 -0
  119. data/examples/fish.xlsx +0 -0
  120. data/examples/pareto.rb +0 -28
  121. data/examples/pareto.xlsx +0 -0
  122. data/examples/pie_chart.rb +0 -16
  123. data/examples/pie_chart.xlsx +0 -0
  124. data/examples/pie_chart_saved.xlsx +0 -0
  125. data/examples/sheet_protection.xlsx +0 -0
  126. data/examples/two_cell_anchor_image.xlsx +0 -0
  127. data/examples/~$pie_chart_saved.xlsx +0 -0
  128. data/lib/axlsx/drawing/ax_data_source.rb~ +0 -55
  129. data/lib/axlsx/drawing/data_source.rb~ +0 -51
  130. data/lib/axlsx/drawing/hlink_click.rb~ +0 -0
  131. data/lib/axlsx/drawing/hyperlink.rb~ +0 -64
  132. data/lib/axlsx/drawing/num_data.rb~ +0 -51
  133. data/lib/axlsx/drawing/num_data_source.rb~ +0 -54
  134. data/lib/axlsx/drawing/num_val.rb~ +0 -40
  135. data/lib/axlsx/drawing/picture_locking.rb~ +0 -36
  136. data/lib/axlsx/drawing/ref.rb~ +0 -41
  137. data/lib/axlsx/drawing/str_data.rb~ +0 -58
  138. data/lib/axlsx/drawing/str_val.rb~ +0 -35
  139. data/lib/axlsx/drawing/vml_drawing.rb~ +0 -6
  140. data/lib/axlsx/drawing/vml_shape.rb~ +0 -61
  141. data/lib/axlsx/util/cbf.rb +0 -333
  142. data/lib/axlsx/util/cfb.rb~ +0 -201
  143. data/lib/axlsx/util/font_tables.rb~ +0 -0
  144. data/lib/axlsx/util/ms_off_crypto.rb +0 -189
  145. data/lib/axlsx/util/ms_off_crypto.rb~ +0 -3
  146. data/lib/axlsx/util/ms_offcrypto.rb~ +0 -0
  147. data/lib/axlsx/util/parser.rb~ +0 -6
  148. data/lib/axlsx/util/storage.rb~ +0 -0
  149. data/lib/axlsx/workbook/shared_strings_table.rb~ +0 -69
  150. data/lib/axlsx/workbook/worksheet/cfvo.rb~ +0 -0
  151. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  152. data/lib/axlsx/workbook/worksheet/color_scale.rb~ +0 -46
  153. data/lib/axlsx/workbook/worksheet/comment.rb~ +0 -91
  154. data/lib/axlsx/workbook/worksheet/comments.rb~ +0 -86
  155. data/lib/axlsx/workbook/worksheet/data_bar.rb~ +0 -0
  156. data/lib/axlsx/workbook/worksheet/icon_set.rb~ +0 -95
  157. data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
  158. data/lib/axlsx/workbook/worksheet/table.rb~ +0 -97
  159. data/lib/schema/dc.xsd~ +0 -118
  160. data/lib/schema/dcterms.xsd~ +0 -331
  161. data/lib/schema/opc-coreProperties.xsd~ +0 -50
  162. data/test/drawing/tc_data_source.rb~ +0 -30
  163. data/test/drawing/tc_num_data.rb~ +0 -35
  164. data/test/drawing/tc_num_val.rb~ +0 -29
  165. data/test/drawing/tc_str_data.rb~ +0 -30
  166. data/test/drawing/tc_str_val.rb~ +0 -26
  167. data/test/drawing/tc_vml_drawing.rb~ +0 -0
  168. data/test/workbook/worksheet/table/tc_table.rb~ +0 -72
  169. data/test/workbook/worksheet/tc_cfvo.rb~ +0 -20
  170. data/test/workbook/worksheet/tc_col.rb~ +0 -10
  171. data/test/workbook/worksheet/tc_color_scale.rb~ +0 -0
  172. data/test/workbook/worksheet/tc_data_bar.rb~ +0 -0
  173. data/test/workbook/worksheet/tc_icon_set.rb~ +0 -0
@@ -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,16 +14,30 @@ 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'
24
-
37
+ require 'axlsx/workbook/worksheet/data_validations.rb'
38
+ require 'axlsx/workbook/worksheet/sheet_view.rb'
39
+ require 'axlsx/workbook/worksheet/pane.rb'
40
+ require 'axlsx/workbook/worksheet/selection.rb'
25
41
  # The Workbook class is an xlsx workbook that manages worksheets, charts, drawings and styles.
26
42
  # The following parts of the Office Open XML spreadsheet specification are not implimented in this version.
27
43
  #
@@ -99,13 +115,21 @@ require 'axlsx/workbook/worksheet/data_validation.rb'
99
115
  attr_reader :tables
100
116
 
101
117
 
102
- # A colllection of comments associated with this workbook
103
- # @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
104
128
  # @see Worksheet#add_comment
105
129
  # @see Comment
106
130
  # @return [Comments]
107
131
  def comments
108
- self.worksheets.map { |ws| ws.comments }.compact
132
+ worksheets.map { |sheet| sheet.comments }.compact
109
133
  end
110
134
 
111
135
  # The styles associated with this workbook
@@ -141,7 +165,6 @@ require 'axlsx/workbook/worksheet/data_validation.rb'
141
165
  @drawings = SimpleTypedList.new Drawing
142
166
  @charts = SimpleTypedList.new Chart
143
167
  @images = SimpleTypedList.new Pic
144
-
145
168
  # Are these even used????? Check package serialization parts
146
169
  @tables = SimpleTypedList.new Table
147
170
  @comments = SimpleTypedList.new Comments
@@ -169,12 +192,14 @@ require 'axlsx/workbook/worksheet/data_validation.rb'
169
192
  def self.date1904() @@date1904; end
170
193
 
171
194
  # Indicates if the workbook should use autowidths or not.
172
- # this must be set before instantiating a worksheet to avoid Rmagix inclusion
195
+ # @note This gem no longer depends on RMagick for autowidth
196
+ # calculation. Thus the performance benefits of turning this off are
197
+ # marginal unless you are creating a very large sheet.
173
198
  # @return [Boolean]
174
199
  def use_autowidth() @use_autowidth; end
175
200
 
176
201
  # see @use_autowidth
177
- def use_autowidth=(v) Axlsx::validate_boolean v; @use_autowidth = v; end
202
+ def use_autowidth=(v=true) Axlsx::validate_boolean v; @use_autowidth = v; end
178
203
 
179
204
  # Adds a worksheet to this workbook
180
205
  # @return [Worksheet]
@@ -187,6 +212,14 @@ require 'axlsx/workbook/worksheet/data_validation.rb'
187
212
  worksheet
188
213
  end
189
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
+
190
223
  # The workbook relationships. This is managed automatically by the workbook
191
224
  # @return [Relationships]
192
225
  def relationships
@@ -228,17 +261,13 @@ require 'axlsx/workbook/worksheet/data_validation.rb'
228
261
  str << '<workbookPr date1904="' << @@date1904.to_s << '"/>'
229
262
  str << '<sheets>'
230
263
  @worksheets.each_with_index do |sheet, index|
231
- str << '<sheet name="' << sheet.name << '" sheetId="' << (index+1).to_s << '" r:id="' << sheet.rId << '"/>'
232
- end
233
- str << '</sheets>'
234
- str << '<definedNames>'
235
- @worksheets.each_with_index do |sheet, index|
236
- if sheet.auto_filter
237
- str << '<definedName name="_xlnm._FilterDatabase" localSheetId="' << index.to_s << '" hidden="1">'
238
- 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
239
267
  end
240
268
  end
241
- str << '</definedNames>'
269
+ str << '</sheets>'
270
+ defined_names.to_xml_string(str)
242
271
  str << '</workbook>'
243
272
  end
244
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
@@ -317,8 +317,35 @@ module Axlsx
317
317
  str << '</c>'
318
318
  end
319
319
 
320
+ def is_formula?
321
+ @type == :string && @value.to_s.start_with?('=')
322
+ end
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
+
320
343
  private
321
344
 
345
+ def font_size
346
+ sz || @styles.fonts[@styles.cellXfs[style].fontId].sz
347
+ end
348
+
322
349
  # Utility method for setting inline style attributes
323
350
  def set_run_style( validator, attr, value)
324
351
  return unless INLINE_STYLES.include?(attr.to_s)
@@ -102,6 +102,21 @@ module Axlsx
102
102
  self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
103
103
  end
104
104
  end
105
+
106
+ # updates the width for this col based on the cells autowidth and
107
+ # an optionally specified fixed width
108
+ # @param [Cell] cell The cell to use in updating this col's width
109
+ # @param [Integer] fixed_width If this is specified the width is set
110
+ # to this value and the cell's attributes are ignored.
111
+ # @param [Boolean] use_autowidth If this is false, the cell's
112
+ # autowidth value will be ignored.
113
+ def update_width(cell, fixed_width=nil, use_autowidth=true)
114
+ if fixed_width.is_a? Numeric
115
+ self.width = fixed_width
116
+ elsif use_autowidth
117
+ self.width = [width || 0, cell.autowidth || 0].max
118
+ end
119
+ end
105
120
 
106
121
  # Serialize this columns data to an xml string
107
122
  # @param [String] str
@@ -0,0 +1,20 @@
1
+ module Axlsx
2
+
3
+ # The cols class manages the col object used to manage column widths.
4
+ # This is where the magic happens with autowidth
5
+ class Cols < SimpleTypedList
6
+
7
+ def initialize(worksheet)
8
+ raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
9
+ super Col
10
+ @worksheet = worksheet
11
+ end
12
+
13
+ def to_xml_string(str = '')
14
+ return if empty?
15
+ str << '<cols>'
16
+ each { |item| item.to_xml_string(str) }
17
+ str << '</cols>'
18
+ end
19
+ end
20
+ end
@@ -53,6 +53,14 @@ module Axlsx
53
53
  @list.map { |comment| comment.author.to_s }.uniq.sort
54
54
  end
55
55
 
56
+ # The relationships required by this object
57
+ # @return [Array]
58
+ def relationships
59
+ [Relationship.new(VML_DRAWING_R, "../#{vml_drawing.pn}"),
60
+ Relationship.new(COMMENT_R, "../#{pn}"),
61
+ Relationship.new(COMMENT_R_NULL, "NULL")]
62
+ end
63
+
56
64
  # serialize the object
57
65
  # @param [String] str
58
66
  # @return [String]
@@ -0,0 +1,25 @@
1
+ module Axlsx
2
+
3
+ # A simple, self serializing class for storing conditional formattings
4
+ class ConditionalFormattings < SimpleTypedList
5
+
6
+ # creates a new Tables object
7
+ def initialize(worksheet)
8
+ raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
9
+ super ConditionalFormatting
10
+ @worksheet = worksheet
11
+ end
12
+
13
+ # The worksheet that owns this collection of tables
14
+ # @return [Worksheet]
15
+ attr_reader :worksheet
16
+
17
+ # serialize the conditional formattings
18
+ def to_xml_string(str = "")
19
+ return if empty?
20
+ each { |item| item.to_xml_string(str) }
21
+ end
22
+ end
23
+
24
+ end
25
+
@@ -65,7 +65,7 @@ module Axlsx
65
65
  def showValue=(v); Axlsx.validate_boolean(v); @showValue = v end
66
66
 
67
67
  # Sets the color for the data bars.
68
- # @param [Color|String] The color object, or rgb string value to apply
68
+ # @param [Color|String] v The color object, or rgb string value to apply
69
69
  def color=(v)
70
70
  @color = v if v.is_a? Color
71
71
  self.color.rgb = v if v.is_a? String
@@ -0,0 +1,28 @@
1
+ module Axlsx
2
+
3
+ # A simple, self serializing class for storing conditional formattings
4
+ class DataValidations < SimpleTypedList
5
+
6
+ # creates a new Tables object
7
+ def initialize(worksheet)
8
+ raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
9
+ super DataValidation
10
+ @worksheet = worksheet
11
+ end
12
+
13
+ # The worksheet that owns this collection of tables
14
+ # @return [Worksheet]
15
+ attr_reader :worksheet
16
+
17
+ # serialize the conditional formattings
18
+ def to_xml_string(str = "")
19
+ return if empty?
20
+ str << "<dataValidations count='#{size}'>"
21
+ each { |item| item.to_xml_string(str) }
22
+ str << '</dataValidations>'
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+
@@ -0,0 +1,65 @@
1
+ module Axlsx
2
+
3
+ # This class manages the dimensions for a worksheet.
4
+ # While this node is optional in the specification some readers like
5
+ # LibraOffice require this node to render the sheet
6
+ class Dimension
7
+
8
+ # the default value for the first cell in the dimension
9
+ # @return [String]
10
+ def self.default_first
11
+ @@default_first ||= 'A1'
12
+ end
13
+
14
+ # the default value for the last cell in the dimension
15
+ # @return [String]
16
+ def self.default_last
17
+ @@default_last ||= 'AA200'
18
+ end
19
+
20
+
21
+ # Creates a new dimension object
22
+ # @param[Worksheet] worksheet - the worksheet this dimension applies
23
+ # to.
24
+ def initialize(worksheet)
25
+ raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
26
+ @worksheet = worksheet
27
+ end
28
+
29
+ attr_reader :worksheet
30
+
31
+ # the full refernece for this dimension
32
+ # @return [String]
33
+ def sqref
34
+ "#{first_cell_reference}:#{last_cell_reference}"
35
+ end
36
+
37
+ # serialize the object
38
+ # @return [String]
39
+ def to_xml_string(str = '')
40
+ return if worksheet.rows.empty?
41
+ str << "<dimension ref=\"%s\"></dimension>" % sqref
42
+ end
43
+
44
+ # The first cell in the dimension
45
+ # @return [String]
46
+ def first_cell_reference
47
+ dimension_reference(worksheet.rows.first.cells.first, Dimension.default_first)
48
+ end
49
+
50
+ # the last cell in the dimension
51
+ # @return [String]
52
+ def last_cell_reference
53
+ dimension_reference(worksheet.rows.last.cells.last, Dimension.default_last)
54
+ end
55
+
56
+ private
57
+
58
+ # Returns the reference of a cell or the default specified
59
+ # @return [String]
60
+ def dimension_reference(cell, default)
61
+ return default unless cell.respond_to?(:r)
62
+ cell.r
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,35 @@
1
+ module Axlsx
2
+
3
+ # A simple list of merged cells
4
+ class MergedCells < SimpleTypedList
5
+
6
+ # creates a new MergedCells object
7
+ # @param [Worksheet] worksheet
8
+ def initialize(worksheet)
9
+ raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet)
10
+ super String
11
+ end
12
+
13
+ # adds cells to the merged cells collection
14
+ # @param [Array||String] cells The cells to add to the merged cells
15
+ # collection. This can be an array of actual cells or a string style
16
+ # range like 'A1:C1'
17
+ def add(cells)
18
+ @list << if cells.is_a?(String)
19
+ cells
20
+ elsif cells.is_a?(Array)
21
+ Axlsx::cell_range(cells, false)
22
+ end
23
+ end
24
+
25
+ # serialize the object
26
+ # @param [String] str
27
+ # @return [String]
28
+ def to_xml_string(str = '')
29
+ return if @list.empty?
30
+ str << "<mergeCells count='#{size}'>"
31
+ each { |merged_cell| str << "<mergeCell ref='#{merged_cell}'></mergeCell>" }
32
+ str << '</mergeCells>'
33
+ end
34
+ end
35
+ end
@@ -79,7 +79,7 @@ module Axlsx
79
79
  # @see paper_width
80
80
  def paper_width=(v); Axlsx::validate_number_with_unit(v); @paper_width = v; end
81
81
  # @see scale
82
- def scale=(v); Axlsx::validate_page_scale(v); @scale = v; end
82
+ def scale=(v); Axlsx::validate_scale_10_400(v); @scale = v; end
83
83
 
84
84
  # convenience method to achieve sanity when setting fit_to_width and fit_to_height
85
85
  # as they both default to 1 if only their counterpart is specified.
@@ -92,6 +92,15 @@ module Axlsx
92
92
  [@fit_to_width, @fit_to_height]
93
93
  end
94
94
 
95
+
96
+ # helper method for worksheet to determine if the page setup is configured for fit to page printing
97
+ # We treat any page set up that has a value set for fit_to_width or fit_to_height value as fit_to_page.
98
+ # @return [Boolean]
99
+ def fit_to_page?
100
+ # is there some better what to express this?
101
+ (fit_to_width != nil || fit_to_height != nil)
102
+ end
103
+
95
104
  # Serializes the page settings element.
96
105
  # @param [String] str
97
106
  # @return [String]