caxlsx 3.3.0 → 4.5.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +87 -11
- data/README.md +43 -16
- data/Rakefile +5 -11
- data/examples/generate.rb +3 -1
- data/lib/axlsx/content_type/abstract_content_type.rb +15 -10
- data/lib/axlsx/content_type/content_type.rb +8 -10
- data/lib/axlsx/content_type/default.rb +7 -6
- data/lib/axlsx/content_type/override.rb +6 -5
- data/lib/axlsx/doc_props/app.rb +96 -32
- data/lib/axlsx/doc_props/core.rb +9 -12
- data/lib/axlsx/drawing/area_chart.rb +16 -17
- data/lib/axlsx/drawing/area_series.rb +23 -18
- data/lib/axlsx/drawing/ax_data_source.rb +4 -7
- data/lib/axlsx/drawing/axes.rb +11 -10
- data/lib/axlsx/drawing/axis.rb +83 -36
- data/lib/axlsx/drawing/bar_3D_chart.rb +30 -21
- data/lib/axlsx/drawing/bar_chart.rb +29 -20
- data/lib/axlsx/drawing/bar_series.rb +23 -18
- data/lib/axlsx/drawing/bubble_chart.rb +9 -10
- data/lib/axlsx/drawing/bubble_series.rb +11 -12
- data/lib/axlsx/drawing/cat_axis.rb +38 -28
- data/lib/axlsx/drawing/chart.rb +52 -30
- data/lib/axlsx/drawing/d_lbls.rb +36 -32
- data/lib/axlsx/drawing/drawing.rb +61 -62
- data/lib/axlsx/drawing/graphic_frame.rb +6 -7
- data/lib/axlsx/drawing/hyperlink.rb +22 -14
- data/lib/axlsx/drawing/line_3D_chart.rb +15 -17
- data/lib/axlsx/drawing/line_chart.rb +16 -17
- data/lib/axlsx/drawing/line_series.rb +23 -18
- data/lib/axlsx/drawing/marker.rb +29 -16
- data/lib/axlsx/drawing/num_data.rb +13 -16
- data/lib/axlsx/drawing/num_data_source.rb +11 -13
- data/lib/axlsx/drawing/num_val.rb +10 -11
- data/lib/axlsx/drawing/one_cell_anchor.rb +16 -10
- data/lib/axlsx/drawing/pic.rb +68 -24
- data/lib/axlsx/drawing/picture_locking.rb +6 -7
- data/lib/axlsx/drawing/pie_3D_chart.rb +9 -12
- data/lib/axlsx/drawing/pie_chart.rb +36 -0
- data/lib/axlsx/drawing/pie_series.rb +26 -17
- data/lib/axlsx/drawing/scaling.rb +26 -13
- data/lib/axlsx/drawing/scatter_chart.rb +10 -11
- data/lib/axlsx/drawing/scatter_series.rb +17 -18
- data/lib/axlsx/drawing/ser_axis.rb +22 -19
- data/lib/axlsx/drawing/series.rb +14 -8
- data/lib/axlsx/drawing/series_title.rb +6 -6
- data/lib/axlsx/drawing/str_data.rb +10 -13
- data/lib/axlsx/drawing/str_val.rb +8 -9
- data/lib/axlsx/drawing/title.rb +23 -27
- data/lib/axlsx/drawing/two_cell_anchor.rb +9 -9
- data/lib/axlsx/drawing/val_axis.rb +7 -8
- data/lib/axlsx/drawing/view_3D.rb +38 -30
- data/lib/axlsx/drawing/vml_drawing.rb +19 -20
- data/lib/axlsx/drawing/vml_shape.rb +25 -26
- data/lib/axlsx/package.rb +134 -99
- data/lib/axlsx/rels/relationship.rb +42 -30
- data/lib/axlsx/rels/relationships.rb +9 -10
- data/lib/axlsx/stylesheet/border.rb +16 -8
- data/lib/axlsx/stylesheet/border_pr.rb +24 -12
- data/lib/axlsx/stylesheet/cell_alignment.rb +44 -21
- data/lib/axlsx/stylesheet/cell_protection.rb +13 -7
- data/lib/axlsx/stylesheet/cell_style.rb +36 -15
- data/lib/axlsx/stylesheet/color.rb +23 -14
- data/lib/axlsx/stylesheet/dxf.rb +36 -15
- data/lib/axlsx/stylesheet/fill.rb +7 -6
- data/lib/axlsx/stylesheet/font.rb +71 -22
- data/lib/axlsx/stylesheet/gradient_fill.rb +16 -10
- data/lib/axlsx/stylesheet/gradient_stop.rb +14 -6
- data/lib/axlsx/stylesheet/num_fmt.rb +15 -14
- data/lib/axlsx/stylesheet/pattern_fill.rb +20 -8
- data/lib/axlsx/stylesheet/styles.rb +167 -118
- data/lib/axlsx/stylesheet/table_style.rb +20 -10
- data/lib/axlsx/stylesheet/table_style_element.rb +16 -8
- data/lib/axlsx/stylesheet/table_styles.rb +16 -11
- data/lib/axlsx/stylesheet/theme.rb +163 -0
- data/lib/axlsx/stylesheet/xf.rb +78 -25
- data/lib/axlsx/util/accessors.rb +13 -11
- data/lib/axlsx/util/buffered_zip_output_stream.rb +64 -0
- data/lib/axlsx/util/constants.rb +133 -110
- data/lib/axlsx/util/mime_type_utils.rb +75 -5
- data/lib/axlsx/util/options_parser.rb +4 -3
- data/lib/axlsx/util/serialized_attributes.rb +47 -23
- data/lib/axlsx/util/simple_typed_list.rb +77 -64
- data/lib/axlsx/util/storage.rb +38 -41
- data/lib/axlsx/util/uri_utils.rb +70 -0
- data/lib/axlsx/util/validators.rb +111 -48
- data/lib/axlsx/util/zip_command.rb +10 -12
- data/lib/axlsx/version.rb +3 -2
- data/lib/axlsx/workbook/defined_name.rb +13 -9
- data/lib/axlsx/workbook/defined_names.rb +5 -4
- data/lib/axlsx/workbook/shared_strings_table.rb +13 -14
- data/lib/axlsx/workbook/workbook.rb +152 -120
- data/lib/axlsx/workbook/workbook_view.rb +9 -12
- data/lib/axlsx/workbook/workbook_views.rb +5 -5
- data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +87 -15
- data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +16 -10
- data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +28 -25
- data/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb +51 -0
- data/lib/axlsx/workbook/worksheet/auto_filter/sort_state.rb +54 -0
- data/lib/axlsx/workbook/worksheet/border_creator.rb +34 -29
- data/lib/axlsx/workbook/worksheet/break.rb +4 -5
- data/lib/axlsx/workbook/worksheet/cell.rb +125 -78
- data/lib/axlsx/workbook/worksheet/cell_serializer.rb +33 -29
- data/lib/axlsx/workbook/worksheet/cfvo.rb +13 -5
- data/lib/axlsx/workbook/worksheet/cfvos.rb +5 -5
- data/lib/axlsx/workbook/worksheet/col.rb +32 -19
- data/lib/axlsx/workbook/worksheet/col_breaks.rb +9 -8
- data/lib/axlsx/workbook/worksheet/color_scale.rb +16 -16
- data/lib/axlsx/workbook/worksheet/cols.rb +10 -8
- data/lib/axlsx/workbook/worksheet/comment.rb +14 -13
- data/lib/axlsx/workbook/worksheet/comments.rb +11 -13
- data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +19 -9
- data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +75 -22
- data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +8 -8
- data/lib/axlsx/workbook/worksheet/data_bar.rb +29 -30
- data/lib/axlsx/workbook/worksheet/data_validation.rb +73 -33
- data/lib/axlsx/workbook/worksheet/data_validations.rb +8 -9
- data/lib/axlsx/workbook/worksheet/date_time_converter.rb +8 -8
- data/lib/axlsx/workbook/worksheet/dimension.rb +10 -7
- data/lib/axlsx/workbook/worksheet/header_footer.rb +5 -4
- data/lib/axlsx/workbook/worksheet/icon_set.rb +40 -12
- data/lib/axlsx/workbook/worksheet/merged_cells.rb +11 -11
- data/lib/axlsx/workbook/worksheet/outline_pr.rb +7 -4
- data/lib/axlsx/workbook/worksheet/page_margins.rb +41 -13
- data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +7 -6
- data/lib/axlsx/workbook/worksheet/page_setup.rb +153 -131
- data/lib/axlsx/workbook/worksheet/pane.rb +34 -26
- data/lib/axlsx/workbook/worksheet/pivot_table.rb +78 -29
- data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +12 -13
- data/lib/axlsx/workbook/worksheet/pivot_tables.rb +6 -5
- data/lib/axlsx/workbook/worksheet/print_options.rb +4 -2
- data/lib/axlsx/workbook/worksheet/protected_range.rb +6 -5
- data/lib/axlsx/workbook/worksheet/protected_ranges.rb +13 -11
- data/lib/axlsx/workbook/worksheet/rich_text.rb +6 -6
- data/lib/axlsx/workbook/worksheet/rich_text_run.rb +66 -28
- data/lib/axlsx/workbook/worksheet/row.rb +34 -23
- data/lib/axlsx/workbook/worksheet/row_breaks.rb +9 -8
- data/lib/axlsx/workbook/worksheet/selection.rb +22 -16
- data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +11 -7
- data/lib/axlsx/workbook/worksheet/sheet_data.rb +7 -7
- data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +23 -19
- data/lib/axlsx/workbook/worksheet/sheet_pr.rb +12 -8
- data/lib/axlsx/workbook/worksheet/sheet_protection.rb +22 -21
- data/lib/axlsx/workbook/worksheet/sheet_view.rb +71 -55
- data/lib/axlsx/workbook/worksheet/table.rb +13 -13
- data/lib/axlsx/workbook/worksheet/table_style_info.rb +8 -6
- data/lib/axlsx/workbook/worksheet/tables.rb +8 -6
- data/lib/axlsx/workbook/worksheet/worksheet.rb +120 -79
- data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +13 -11
- data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +11 -4
- data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +12 -10
- data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +9 -7
- data/lib/axlsx.rb +120 -60
- data/lib/caxlsx.rb +3 -2
- metadata +42 -75
data/lib/axlsx/package.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
2
3
|
module Axlsx
|
|
3
|
-
# Package is responsible for managing all the bits and
|
|
4
|
+
# Package is responsible for managing all the bits and pieces that Open Office XML requires to make a valid
|
|
4
5
|
# xlsx document including validation and serialization.
|
|
5
6
|
class Package
|
|
6
7
|
include Axlsx::OptionsParser
|
|
@@ -20,7 +21,7 @@ module Axlsx
|
|
|
20
21
|
# @option options [Time] :created_at Timestamp in the document properties (defaults to current time).
|
|
21
22
|
# @option options [Boolean] :use_shared_strings This is passed to the workbook to specify that shared strings should be used when serializing the package.
|
|
22
23
|
# @example Package.new :author => 'you!', :workbook => Workbook.new
|
|
23
|
-
def initialize(options={})
|
|
24
|
+
def initialize(options = {})
|
|
24
25
|
@workbook = nil
|
|
25
26
|
@core, @app = Core.new, App.new
|
|
26
27
|
@core.creator = options[:author] || @core.creator
|
|
@@ -32,12 +33,10 @@ module Axlsx
|
|
|
32
33
|
# Shortcut to specify that the workbook should use autowidth
|
|
33
34
|
# @see Workbook#use_autowidth
|
|
34
35
|
def use_autowidth=(v)
|
|
35
|
-
Axlsx
|
|
36
|
+
Axlsx.validate_boolean(v)
|
|
36
37
|
workbook.use_autowidth = v
|
|
37
38
|
end
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
40
|
# Shortcut to determine if the workbook is configured to use shared strings
|
|
42
41
|
# @see Workbook#use_shared_strings
|
|
43
42
|
def use_shared_strings
|
|
@@ -47,19 +46,20 @@ module Axlsx
|
|
|
47
46
|
# Shortcut to specify that the workbook should use shared strings
|
|
48
47
|
# @see Workbook#use_shared_strings
|
|
49
48
|
def use_shared_strings=(v)
|
|
50
|
-
Axlsx
|
|
49
|
+
Axlsx.validate_boolean(v)
|
|
51
50
|
workbook.use_shared_strings = v
|
|
52
51
|
end
|
|
52
|
+
|
|
53
53
|
# The workbook this package will serialize or validate.
|
|
54
54
|
# @return [Workbook] If no workbook instance has been assigned with this package a new Workbook instance is returned.
|
|
55
55
|
# @raise ArgumentError if workbook parameter is not a Workbook instance.
|
|
56
56
|
# @note As there are multiple ways to instantiate a workbook for the package,
|
|
57
57
|
# here are a few examples:
|
|
58
|
-
# # assign directly during package
|
|
58
|
+
# # assign directly during package instantiation
|
|
59
59
|
# wb = Package.new(:workbook => Workbook.new).workbook
|
|
60
60
|
#
|
|
61
61
|
# # get a fresh workbook automatically from the package
|
|
62
|
-
# wb =
|
|
62
|
+
# wb = Package.new().workbook
|
|
63
63
|
# # # set the workbook after creating the package
|
|
64
64
|
# wb = Package.new().workbook = Workbook.new
|
|
65
65
|
def workbook
|
|
@@ -69,7 +69,10 @@ module Axlsx
|
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
# @see workbook
|
|
72
|
-
def workbook=(workbook)
|
|
72
|
+
def workbook=(workbook)
|
|
73
|
+
DataTypeValidator.validate :Package_workbook, Workbook, workbook
|
|
74
|
+
@workbook = workbook
|
|
75
|
+
end
|
|
73
76
|
|
|
74
77
|
# Serialize your workbook to disk as an xlsx document.
|
|
75
78
|
#
|
|
@@ -79,6 +82,8 @@ module Axlsx
|
|
|
79
82
|
# @option options [String] :zip_command When `nil`, `#serialize` with RubyZip to
|
|
80
83
|
# zip the XLSX file contents. When a String, the provided zip command (e.g.,
|
|
81
84
|
# "zip") is used to zip the file contents (may be faster for large files)
|
|
85
|
+
# @option options [String] :password When specified, the serialized packaged will be
|
|
86
|
+
# encrypted with the password. Requires ooxml_crypt gem.
|
|
82
87
|
# @return [Boolean] False if confirm_valid and validation errors exist. True if the package was serialized
|
|
83
88
|
# @note A tremendous amount of effort has gone into ensuring that you cannot create invalid xlsx documents.
|
|
84
89
|
# options[:confirm_valid] should be used in the rare case that you cannot open the serialized file.
|
|
@@ -101,40 +106,64 @@ module Axlsx
|
|
|
101
106
|
# s = p.to_stream()
|
|
102
107
|
# File.open('example_streamed.xlsx', 'wb') { |f| f.write(s.read) }
|
|
103
108
|
def serialize(output, options = {}, secondary_options = nil)
|
|
104
|
-
|
|
105
|
-
workbook.apply_styles
|
|
109
|
+
unless workbook.styles_applied
|
|
110
|
+
workbook.apply_styles
|
|
106
111
|
end
|
|
107
112
|
|
|
108
|
-
confirm_valid, zip_command = parse_serialize_options(options, secondary_options)
|
|
109
|
-
return false unless !confirm_valid ||
|
|
113
|
+
confirm_valid, zip_command, password = parse_serialize_options(options, secondary_options)
|
|
114
|
+
return false unless !confirm_valid || validate.empty?
|
|
115
|
+
|
|
110
116
|
zip_provider = if zip_command
|
|
111
117
|
ZipCommand.new(zip_command)
|
|
112
118
|
else
|
|
113
|
-
|
|
119
|
+
BufferedZipOutputStream
|
|
114
120
|
end
|
|
115
121
|
Relationship.initialize_ids_cache
|
|
116
122
|
zip_provider.open(output) do |zip|
|
|
117
123
|
write_parts(zip)
|
|
118
124
|
end
|
|
125
|
+
|
|
126
|
+
if password && !password.empty?
|
|
127
|
+
require_ooxml_crypt!
|
|
128
|
+
OoxmlCrypt.encrypt_file(output, password, output)
|
|
129
|
+
end
|
|
130
|
+
|
|
119
131
|
true
|
|
120
132
|
ensure
|
|
121
133
|
Relationship.clear_ids_cache
|
|
122
134
|
end
|
|
123
135
|
|
|
124
|
-
|
|
125
136
|
# Serialize your workbook to a StringIO instance
|
|
126
|
-
# @param [Boolean]
|
|
127
|
-
#
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
137
|
+
# @param [Boolean] old_confirm_valid (Deprecated) Validate the package prior to serialization.
|
|
138
|
+
# Use :confirm_valid keyword arg instead.
|
|
139
|
+
# @option kwargs [Boolean] :confirm_valid Validate the package prior to serialization.
|
|
140
|
+
# @option kwargs [String] :password When specified, the serialized packaged will be
|
|
141
|
+
# encrypted with the password. Requires ooxml_crypt gem.
|
|
142
|
+
# @return [StringIO|Boolean] False if confirm_valid and validation errors exist. Rewound string IO if not.
|
|
143
|
+
def to_stream(old_confirm_valid = nil, confirm_valid: false, password: nil)
|
|
144
|
+
unless old_confirm_valid.nil?
|
|
145
|
+
warn "[DEPRECATION] Axlsx::Package#to_stream with confirm_valid as a non-keyword arg is deprecated. " \
|
|
146
|
+
"Use keyword arg instead e.g., package.to_stream(confirm_valid: false)"
|
|
147
|
+
confirm_valid ||= old_confirm_valid
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
unless workbook.styles_applied
|
|
151
|
+
workbook.apply_styles
|
|
131
152
|
end
|
|
132
153
|
|
|
133
|
-
return false unless !confirm_valid ||
|
|
154
|
+
return false unless !confirm_valid || validate.empty?
|
|
155
|
+
|
|
134
156
|
Relationship.initialize_ids_cache
|
|
135
|
-
|
|
136
|
-
|
|
157
|
+
stream = BufferedZipOutputStream.write_buffer do |zip|
|
|
158
|
+
write_parts(zip)
|
|
159
|
+
end
|
|
137
160
|
stream.rewind
|
|
161
|
+
|
|
162
|
+
if password && !password.empty?
|
|
163
|
+
require_ooxml_crypt!
|
|
164
|
+
stream = StringIO.new(OoxmlCrypt.encrypt(stream.read, password))
|
|
165
|
+
end
|
|
166
|
+
|
|
138
167
|
stream
|
|
139
168
|
ensure
|
|
140
169
|
Relationship.clear_ids_cache
|
|
@@ -142,8 +171,8 @@ module Axlsx
|
|
|
142
171
|
|
|
143
172
|
# Encrypt the package into a CFB using the password provided
|
|
144
173
|
# This is not ready yet
|
|
145
|
-
def encrypt(file_name, password)
|
|
146
|
-
|
|
174
|
+
def encrypt(file_name, password) # rubocop:disable Naming/PredicateMethod
|
|
175
|
+
false
|
|
147
176
|
# moc = MsOffCrypto.new(file_name, password)
|
|
148
177
|
# moc.save
|
|
149
178
|
end
|
|
@@ -157,7 +186,7 @@ module Axlsx
|
|
|
157
186
|
#
|
|
158
187
|
# If by chance you are able to create a package that does not validate it indicates that the internal
|
|
159
188
|
# validation is not robust enough and needs to be improved. Please report your errors to the gem author.
|
|
160
|
-
# @see
|
|
189
|
+
# @see https://www.ecma-international.org/publications-and-standards/standards/ecma-376/
|
|
161
190
|
# @example
|
|
162
191
|
# # The following will output any error messages found in serialization.
|
|
163
192
|
# p = Axlsx::Package.new
|
|
@@ -187,7 +216,7 @@ module Axlsx
|
|
|
187
216
|
end
|
|
188
217
|
unless part[:path].nil?
|
|
189
218
|
zip.put_next_entry(zip_entry_for_part(part))
|
|
190
|
-
zip.write
|
|
219
|
+
zip.write File.read(part[:path], mode: "rb")
|
|
191
220
|
end
|
|
192
221
|
end
|
|
193
222
|
zip
|
|
@@ -202,11 +231,12 @@ module Axlsx
|
|
|
202
231
|
# Note: {Core#created} also defaults to the current time – so to generate identical axlsx packages you have
|
|
203
232
|
# to set this explicitly, too (eg. with `Package.new(created_at: Time.local(2013, 1, 1))`).
|
|
204
233
|
#
|
|
205
|
-
# @param part A hash describing a part of this
|
|
234
|
+
# @param part A hash describing a part of this package (see {#parts})
|
|
206
235
|
# @return [Zip::Entry]
|
|
207
236
|
def zip_entry_for_part(part)
|
|
208
237
|
timestamp = Zip::DOSTime.at(@core.created.to_i)
|
|
209
|
-
|
|
238
|
+
|
|
239
|
+
Zip::Entry.new("", part[:entry], time: timestamp)
|
|
210
240
|
end
|
|
211
241
|
|
|
212
242
|
# The parts of a package
|
|
@@ -214,62 +244,62 @@ module Axlsx
|
|
|
214
244
|
# @private
|
|
215
245
|
def parts
|
|
216
246
|
parts = [
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
247
|
+
{ entry: "xl/#{STYLES_PN}", doc: workbook.styles, schema: SML_XSD },
|
|
248
|
+
{ entry: "xl/#{THEME_PN}", doc: workbook.theme, schema: THEME_XSD },
|
|
249
|
+
{ entry: CORE_PN, doc: @core, schema: CORE_XSD },
|
|
250
|
+
{ entry: APP_PN, doc: @app, schema: APP_XSD },
|
|
251
|
+
{ entry: WORKBOOK_RELS_PN, doc: workbook.relationships, schema: RELS_XSD },
|
|
252
|
+
{ entry: WORKBOOK_PN, doc: workbook, schema: SML_XSD }
|
|
222
253
|
]
|
|
223
254
|
|
|
224
255
|
workbook.drawings.each do |drawing|
|
|
225
|
-
parts << {:
|
|
226
|
-
parts << {:
|
|
256
|
+
parts << { entry: "xl/#{drawing.rels_pn}", doc: drawing.relationships, schema: RELS_XSD }
|
|
257
|
+
parts << { entry: "xl/#{drawing.pn}", doc: drawing, schema: DRAWING_XSD }
|
|
227
258
|
end
|
|
228
259
|
|
|
229
|
-
|
|
230
260
|
workbook.tables.each do |table|
|
|
231
|
-
parts << {:
|
|
261
|
+
parts << { entry: "xl/#{table.pn}", doc: table, schema: SML_XSD }
|
|
232
262
|
end
|
|
233
263
|
workbook.pivot_tables.each do |pivot_table|
|
|
234
264
|
cache_definition = pivot_table.cache_definition
|
|
235
|
-
parts << {:
|
|
236
|
-
parts << {:
|
|
237
|
-
parts << {:
|
|
265
|
+
parts << { entry: "xl/#{pivot_table.rels_pn}", doc: pivot_table.relationships, schema: RELS_XSD }
|
|
266
|
+
parts << { entry: "xl/#{pivot_table.pn}", doc: pivot_table } # , :schema => SML_XSD}
|
|
267
|
+
parts << { entry: "xl/#{cache_definition.pn}", doc: cache_definition } # , :schema => SML_XSD}
|
|
238
268
|
end
|
|
239
269
|
|
|
240
|
-
workbook.comments.each do|comment|
|
|
241
|
-
|
|
242
|
-
parts << { :
|
|
243
|
-
parts << { :
|
|
270
|
+
workbook.comments.each do |comment|
|
|
271
|
+
unless comment.empty?
|
|
272
|
+
parts << { entry: "xl/#{comment.pn}", doc: comment, schema: SML_XSD }
|
|
273
|
+
parts << { entry: "xl/#{comment.vml_drawing.pn}", doc: comment.vml_drawing, schema: nil }
|
|
244
274
|
end
|
|
245
275
|
end
|
|
246
276
|
|
|
247
277
|
workbook.charts.each do |chart|
|
|
248
|
-
parts << {:
|
|
278
|
+
parts << { entry: "xl/#{chart.pn}", doc: chart, schema: DRAWING_XSD }
|
|
249
279
|
end
|
|
250
280
|
|
|
251
281
|
workbook.images.each do |image|
|
|
252
|
-
parts << {:
|
|
282
|
+
parts << { entry: "xl/#{image.pn}", path: image.image_src } unless image.remote?
|
|
253
283
|
end
|
|
254
284
|
|
|
255
285
|
if use_shared_strings
|
|
256
|
-
parts << {:
|
|
286
|
+
parts << { entry: "xl/#{SHARED_STRINGS_PN}", doc: workbook.shared_strings, schema: SML_XSD }
|
|
257
287
|
end
|
|
258
288
|
|
|
259
289
|
workbook.worksheets.each do |sheet|
|
|
260
|
-
parts << {:
|
|
261
|
-
parts << {:
|
|
290
|
+
parts << { entry: "xl/#{sheet.rels_pn}", doc: sheet.relationships, schema: RELS_XSD }
|
|
291
|
+
parts << { entry: "xl/#{sheet.pn}", doc: sheet, schema: SML_XSD }
|
|
262
292
|
end
|
|
263
293
|
|
|
264
294
|
# Sort parts for correct MIME detection
|
|
265
295
|
[
|
|
266
|
-
{:
|
|
267
|
-
{:
|
|
268
|
-
*
|
|
296
|
+
{ entry: CONTENT_TYPES_PN, doc: content_types, schema: CONTENT_TYPES_XSD },
|
|
297
|
+
{ entry: RELS_PN, doc: relationships, schema: RELS_XSD },
|
|
298
|
+
*parts.sort_by { |part| part[:entry] }.reverse
|
|
269
299
|
]
|
|
270
300
|
end
|
|
271
301
|
|
|
272
|
-
# Performs xsd validation for a
|
|
302
|
+
# Performs xsd validation for a single document
|
|
273
303
|
#
|
|
274
304
|
# @param [String] schema path to the xsd schema to be used in validation.
|
|
275
305
|
# @param [String] doc The xml text to be validated
|
|
@@ -278,11 +308,8 @@ module Axlsx
|
|
|
278
308
|
def validate_single_doc(schema, doc)
|
|
279
309
|
schema = Nokogiri::XML::Schema(File.open(schema))
|
|
280
310
|
doc = Nokogiri::XML(doc)
|
|
281
|
-
|
|
282
|
-
schema.validate(doc)
|
|
283
|
-
errors << error
|
|
284
|
-
end
|
|
285
|
-
errors
|
|
311
|
+
|
|
312
|
+
schema.validate(doc)
|
|
286
313
|
end
|
|
287
314
|
|
|
288
315
|
# Appends override objects for drawings, charts, and sheets as they exist in your workbook to the default content types.
|
|
@@ -291,56 +318,56 @@ module Axlsx
|
|
|
291
318
|
def content_types
|
|
292
319
|
c_types = base_content_types
|
|
293
320
|
workbook.drawings.each do |drawing|
|
|
294
|
-
c_types << Axlsx::Override.new(:
|
|
295
|
-
:
|
|
321
|
+
c_types << Axlsx::Override.new(PartName: "/xl/#{drawing.pn}",
|
|
322
|
+
ContentType: DRAWING_CT)
|
|
296
323
|
end
|
|
297
324
|
|
|
298
325
|
workbook.charts.each do |chart|
|
|
299
|
-
c_types << Axlsx::Override.new(:
|
|
300
|
-
:
|
|
326
|
+
c_types << Axlsx::Override.new(PartName: "/xl/#{chart.pn}",
|
|
327
|
+
ContentType: CHART_CT)
|
|
301
328
|
end
|
|
302
329
|
|
|
303
330
|
workbook.tables.each do |table|
|
|
304
|
-
c_types << Axlsx::Override.new(:
|
|
305
|
-
:
|
|
331
|
+
c_types << Axlsx::Override.new(PartName: "/xl/#{table.pn}",
|
|
332
|
+
ContentType: TABLE_CT)
|
|
306
333
|
end
|
|
307
334
|
|
|
308
335
|
workbook.pivot_tables.each do |pivot_table|
|
|
309
|
-
c_types << Axlsx::Override.new(:
|
|
310
|
-
:
|
|
311
|
-
c_types << Axlsx::Override.new(:
|
|
312
|
-
:
|
|
336
|
+
c_types << Axlsx::Override.new(PartName: "/xl/#{pivot_table.pn}",
|
|
337
|
+
ContentType: PIVOT_TABLE_CT)
|
|
338
|
+
c_types << Axlsx::Override.new(PartName: "/xl/#{pivot_table.cache_definition.pn}",
|
|
339
|
+
ContentType: PIVOT_TABLE_CACHE_DEFINITION_CT)
|
|
313
340
|
end
|
|
314
341
|
|
|
315
342
|
workbook.comments.each do |comment|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
343
|
+
unless comment.empty?
|
|
344
|
+
c_types << Axlsx::Override.new(PartName: "/xl/#{comment.pn}",
|
|
345
|
+
ContentType: COMMENT_CT)
|
|
319
346
|
end
|
|
320
347
|
end
|
|
321
348
|
|
|
322
|
-
|
|
323
|
-
c_types << Axlsx::Default.new(:
|
|
349
|
+
unless workbook.comments.empty?
|
|
350
|
+
c_types << Axlsx::Default.new(Extension: "vml", ContentType: VML_DRAWING_CT)
|
|
324
351
|
end
|
|
325
352
|
|
|
326
353
|
workbook.worksheets.each do |sheet|
|
|
327
|
-
c_types << Axlsx::Override.new(:
|
|
328
|
-
|
|
354
|
+
c_types << Axlsx::Override.new(PartName: "/xl/#{sheet.pn}",
|
|
355
|
+
ContentType: WORKSHEET_CT)
|
|
329
356
|
end
|
|
330
357
|
exts = workbook.images.map { |image| image.extname.downcase }
|
|
331
358
|
exts.uniq.each do |ext|
|
|
332
|
-
ct = if
|
|
359
|
+
ct = if JPEG_EXS.include?(ext)
|
|
333
360
|
JPEG_CT
|
|
334
|
-
elsif ext ==
|
|
361
|
+
elsif ext == GIF_EX
|
|
335
362
|
GIF_CT
|
|
336
|
-
elsif ext ==
|
|
363
|
+
elsif ext == PNG_EX
|
|
337
364
|
PNG_CT
|
|
338
365
|
end
|
|
339
|
-
c_types << Axlsx::Default.new(:
|
|
366
|
+
c_types << Axlsx::Default.new(ContentType: ct, Extension: ext)
|
|
340
367
|
end
|
|
341
368
|
if use_shared_strings
|
|
342
|
-
c_types << Axlsx::Override.new(:
|
|
343
|
-
:
|
|
369
|
+
c_types << Axlsx::Override.new(PartName: "/xl/#{SHARED_STRINGS_PN}",
|
|
370
|
+
ContentType: SHARED_STRINGS_CT)
|
|
344
371
|
end
|
|
345
372
|
c_types
|
|
346
373
|
end
|
|
@@ -349,13 +376,14 @@ module Axlsx
|
|
|
349
376
|
# @return [ContentType]
|
|
350
377
|
# @private
|
|
351
378
|
def base_content_types
|
|
352
|
-
c_types = ContentType.new
|
|
353
|
-
c_types << Default.new(:
|
|
354
|
-
c_types << Default.new(:
|
|
355
|
-
c_types << Override.new(:
|
|
356
|
-
c_types << Override.new(:
|
|
357
|
-
c_types << Override.new(:
|
|
358
|
-
c_types <<
|
|
379
|
+
c_types = ContentType.new
|
|
380
|
+
c_types << Default.new(ContentType: RELS_CT, Extension: RELS_EX)
|
|
381
|
+
c_types << Default.new(Extension: XML_EX, ContentType: XML_CT)
|
|
382
|
+
c_types << Override.new(PartName: "/#{APP_PN}", ContentType: APP_CT)
|
|
383
|
+
c_types << Override.new(PartName: "/#{CORE_PN}", ContentType: CORE_CT)
|
|
384
|
+
c_types << Override.new(PartName: "/xl/#{STYLES_PN}", ContentType: STYLES_CT)
|
|
385
|
+
c_types << Override.new(PartName: "/xl/#{THEME_PN}", ContentType: THEME_CT)
|
|
386
|
+
c_types << Axlsx::Override.new(PartName: "/#{WORKBOOK_PN}", ContentType: WORKBOOK_CT)
|
|
359
387
|
c_types.lock
|
|
360
388
|
c_types
|
|
361
389
|
end
|
|
@@ -373,26 +401,33 @@ module Axlsx
|
|
|
373
401
|
end
|
|
374
402
|
|
|
375
403
|
# Parse the arguments of `#serialize`
|
|
376
|
-
# @return [Boolean, (String or nil)] Returns
|
|
377
|
-
# `confirm_valid` and
|
|
404
|
+
# @return [Boolean, (String or nil), (String or nil)] Returns a 3-tuple where values are
|
|
405
|
+
# `confirm_valid`, `zip_command`, and `password`.
|
|
378
406
|
# @private
|
|
379
407
|
def parse_serialize_options(options, secondary_options)
|
|
380
408
|
if secondary_options
|
|
381
|
-
warn "[DEPRECATION] Axlsx::Package#serialize with 3 arguments is deprecated. "
|
|
382
|
-
|
|
409
|
+
warn "[DEPRECATION] Axlsx::Package#serialize with 3 arguments is deprecated. " \
|
|
410
|
+
"Use keyword args instead e.g., package.serialize(output, confirm_valid: false, zip_command: 'zip')"
|
|
383
411
|
end
|
|
384
412
|
if options.is_a?(Hash)
|
|
385
413
|
options.merge!(secondary_options || {})
|
|
386
|
-
invalid_keys = options.keys - [:confirm_valid, :zip_command]
|
|
414
|
+
invalid_keys = options.keys - [:confirm_valid, :zip_command, :password]
|
|
387
415
|
if invalid_keys.any?
|
|
388
|
-
raise ArgumentError
|
|
416
|
+
raise ArgumentError, "Invalid keyword arguments: #{invalid_keys}"
|
|
389
417
|
end
|
|
390
|
-
|
|
418
|
+
|
|
419
|
+
[options.fetch(:confirm_valid, false), options.fetch(:zip_command, nil), options.fetch(:password, nil)]
|
|
391
420
|
else
|
|
392
|
-
warn "[DEPRECATION] Axlsx::Package#serialize with confirm_valid as a boolean is deprecated. "
|
|
393
|
-
|
|
421
|
+
warn "[DEPRECATION] Axlsx::Package#serialize with confirm_valid as a boolean is deprecated. " \
|
|
422
|
+
"Use keyword args instead e.g., package.serialize(output, confirm_valid: false)"
|
|
394
423
|
parse_serialize_options((secondary_options || {}).merge(confirm_valid: options), nil)
|
|
395
424
|
end
|
|
396
425
|
end
|
|
426
|
+
|
|
427
|
+
def require_ooxml_crypt!
|
|
428
|
+
return if defined?(OoxmlCrypt)
|
|
429
|
+
|
|
430
|
+
raise 'Axlsx encryption requires ooxml_crypt gem'
|
|
431
|
+
end
|
|
397
432
|
end
|
|
398
433
|
end
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
2
3
|
module Axlsx
|
|
3
4
|
# A relationship defines a reference between package parts.
|
|
4
5
|
# @note Packages automatically manage relationships.
|
|
5
6
|
class Relationship
|
|
6
|
-
|
|
7
7
|
class << self
|
|
8
8
|
# Keeps track of relationship ids in use.
|
|
9
9
|
# @return [Array]
|
|
@@ -12,27 +12,27 @@ module Axlsx
|
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
# Initialize cached ids.
|
|
15
|
-
#
|
|
15
|
+
#
|
|
16
16
|
# This should be called before serializing a package (see {Package#serialize} and
|
|
17
|
-
# {Package#to_stream}) to make sure that serialization is idempotent (i.e.
|
|
18
|
-
# Relationship instances are generated with the same IDs
|
|
17
|
+
# {Package#to_stream}) to make sure that serialization is idempotent (i.e.
|
|
18
|
+
# Relationship instances are generated with the same IDs every time the package
|
|
19
19
|
# is serialized).
|
|
20
20
|
def initialize_ids_cache
|
|
21
21
|
Thread.current[:axlsx_relationship_ids_cache] = {}
|
|
22
22
|
end
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
# Clear cached ids.
|
|
25
|
-
#
|
|
25
|
+
#
|
|
26
26
|
# This should be called after serializing a package (see {Package#serialize} and
|
|
27
27
|
# {Package#to_stream}) to free the memory allocated for cache.
|
|
28
|
-
#
|
|
29
|
-
# Also, calling this avoids memory leaks (cached ids lingering around
|
|
30
|
-
# forever).
|
|
28
|
+
#
|
|
29
|
+
# Also, calling this avoids memory leaks (cached ids lingering around
|
|
30
|
+
# forever).
|
|
31
31
|
def clear_ids_cache
|
|
32
32
|
Thread.current[:axlsx_relationship_ids_cache] = nil
|
|
33
33
|
end
|
|
34
|
-
|
|
35
|
-
# Generate and return a unique id (eg. `rId123`) Used for setting {#Id}.
|
|
34
|
+
|
|
35
|
+
# Generate and return a unique id (eg. `rId123`) Used for setting {#Id}.
|
|
36
36
|
#
|
|
37
37
|
# The generated id depends on the number of previously cached ids, so using
|
|
38
38
|
# {clear_ids_cache} will automatically reset the generated ids, too.
|
|
@@ -42,12 +42,12 @@ module Axlsx
|
|
|
42
42
|
end
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
# The id of the relationship (eg. "rId123"). Most instances get their own unique id.
|
|
46
|
-
# However, some instances need to share the same id – see {#
|
|
45
|
+
# The id of the relationship (eg. "rId123"). Most instances get their own unique id.
|
|
46
|
+
# However, some instances need to share the same id – see {#ids_cache_key}
|
|
47
47
|
# for details.
|
|
48
48
|
# @return [String]
|
|
49
49
|
attr_reader :Id
|
|
50
|
-
|
|
50
|
+
|
|
51
51
|
# The location of the relationship target
|
|
52
52
|
# @return [String]
|
|
53
53
|
attr_reader :Target
|
|
@@ -77,43 +77,56 @@ module Axlsx
|
|
|
77
77
|
# The source object the relations belongs to (e.g. a hyperlink, drawing, ...). Needed when
|
|
78
78
|
# looking up the relationship for a specific object (see {Relationships#for}).
|
|
79
79
|
attr_reader :source_obj
|
|
80
|
-
|
|
81
|
-
# Initializes a new relationship.
|
|
80
|
+
|
|
81
|
+
# Initializes a new relationship.
|
|
82
82
|
# @param [Object] source_obj see {#source_obj}
|
|
83
83
|
# @param [String] type The type of the relationship
|
|
84
84
|
# @param [String] target The target for the relationship
|
|
85
85
|
# @option [Symbol] :target_mode only accepts :external.
|
|
86
|
-
def initialize(source_obj, type, target, options={})
|
|
86
|
+
def initialize(source_obj, type, target, options = {})
|
|
87
87
|
@source_obj = source_obj
|
|
88
|
-
self.Target=target
|
|
89
|
-
self.Type=type
|
|
88
|
+
self.Target = target
|
|
89
|
+
self.Type = type
|
|
90
90
|
self.TargetMode = options[:target_mode] if options[:target_mode]
|
|
91
91
|
@Id = (self.class.ids_cache[ids_cache_key] ||= self.class.next_free_id)
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
# @see Target
|
|
95
|
-
def Target=(v)
|
|
95
|
+
def Target=(v)
|
|
96
|
+
Axlsx.validate_string v
|
|
97
|
+
@Target = v
|
|
98
|
+
end
|
|
99
|
+
|
|
96
100
|
# @see Type
|
|
97
|
-
def Type=(v)
|
|
101
|
+
def Type=(v)
|
|
102
|
+
Axlsx.validate_relationship_type v
|
|
103
|
+
@Type = v
|
|
104
|
+
end
|
|
98
105
|
|
|
99
106
|
# @see TargetMode
|
|
100
|
-
def TargetMode=(v)
|
|
107
|
+
def TargetMode=(v)
|
|
108
|
+
RestrictionValidator.validate 'Relationship.TargetMode', [:External, :Internal], v
|
|
109
|
+
@TargetMode = v
|
|
110
|
+
end
|
|
101
111
|
|
|
102
112
|
# serialize relationship
|
|
103
113
|
# @param [String] str
|
|
104
114
|
# @return [String]
|
|
105
|
-
def to_xml_string(str = '')
|
|
106
|
-
h = Axlsx.instance_values_for(self).reject{|k, _| k == "source_obj"}
|
|
115
|
+
def to_xml_string(str = +'')
|
|
116
|
+
h = Axlsx.instance_values_for(self).reject { |k, _| k == "source_obj" }
|
|
107
117
|
str << '<Relationship '
|
|
108
|
-
|
|
118
|
+
h.each_with_index do |key_value, index|
|
|
119
|
+
str << ' ' unless index == 0
|
|
120
|
+
str << key_value.first.to_s << '="' << Axlsx.coder.encode(key_value.last.to_s) << '"'
|
|
121
|
+
end
|
|
109
122
|
str << '/>'
|
|
110
123
|
end
|
|
111
|
-
|
|
124
|
+
|
|
112
125
|
# A key that determines whether this relationship should use already generated id.
|
|
113
126
|
#
|
|
114
127
|
# Instances designating the same relationship need to use the same id. We can not simply
|
|
115
|
-
# compare the {#Target} attribute, though: `foo/bar.xml`, `../foo/bar.xml`,
|
|
116
|
-
# `../../foo/bar.xml` etc. are all different but probably mean the same file (this
|
|
128
|
+
# compare the {#Target} attribute, though: `foo/bar.xml`, `../foo/bar.xml`,
|
|
129
|
+
# `../../foo/bar.xml` etc. are all different but probably mean the same file (this
|
|
117
130
|
# is especially an issue for relationships in the context of pivot tables). So lets
|
|
118
131
|
# just ignore this attribute for now (except when {#TargetMode} is set to `:External` –
|
|
119
132
|
# then {#Target} will be an absolute URL and thus can safely be compared).
|
|
@@ -125,6 +138,5 @@ module Axlsx
|
|
|
125
138
|
key << self.Target if self.TargetMode == :External
|
|
126
139
|
key
|
|
127
140
|
end
|
|
128
|
-
|
|
129
141
|
end
|
|
130
142
|
end
|
|
@@ -1,32 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
2
3
|
module Axlsx
|
|
3
|
-
|
|
4
|
+
require_relative 'relationship'
|
|
4
5
|
|
|
5
6
|
# Relationships are a collection of Relations that define how package parts are related.
|
|
6
|
-
# @note The package automatically manages
|
|
7
|
+
# @note The package automatically manages relationships.
|
|
7
8
|
class Relationships < SimpleTypedList
|
|
8
|
-
|
|
9
9
|
# Creates a new Relationships collection based on SimpleTypedList
|
|
10
10
|
def initialize
|
|
11
|
-
super
|
|
11
|
+
super(Relationship)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
# The relationship instance for the given source object, or nil if none exists.
|
|
15
15
|
# @see Relationship#source_obj
|
|
16
16
|
# @return [Relationship]
|
|
17
17
|
def for(source_obj)
|
|
18
|
-
find{ |rel| rel.source_obj == source_obj }
|
|
18
|
+
find { |rel| rel.source_obj == source_obj }
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
# serialize relationships
|
|
22
22
|
# @param [String] str
|
|
23
23
|
# @return [String]
|
|
24
|
-
def to_xml_string(str = '')
|
|
24
|
+
def to_xml_string(str = +'')
|
|
25
25
|
str << '<?xml version="1.0" encoding="UTF-8"?>'
|
|
26
|
-
str <<
|
|
27
|
-
each{ |rel| rel.to_xml_string(str) }
|
|
26
|
+
str << '<Relationships xmlns="' << RELS_R << '">'
|
|
27
|
+
each { |rel| rel.to_xml_string(str) }
|
|
28
28
|
str << '</Relationships>'
|
|
29
29
|
end
|
|
30
|
-
|
|
31
30
|
end
|
|
32
31
|
end
|