xlsxrb 0.2.2 → 0.2.3
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 +43 -0
- data/lib/xlsxrb/elements/cell.rb +1 -1
- data/lib/xlsxrb/elements/workbook.rb +115 -4
- data/lib/xlsxrb/number_formatter.rb +5 -5
- data/lib/xlsxrb/ooxml/cfb.rb +61 -25
- data/lib/xlsxrb/ooxml/crypto/agile.rb +41 -12
- data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +4 -0
- data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +2 -0
- data/lib/xlsxrb/ooxml/shared_strings_parser.rb +4 -2
- data/lib/xlsxrb/ooxml/utils.rb +4 -3
- data/lib/xlsxrb/ooxml/workbook_parser.rb +88 -40
- data/lib/xlsxrb/ooxml/workbook_writer.rb +48 -24
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +186 -78
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +81 -18
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +1511 -1458
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +558 -525
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +272 -280
- data/lib/xlsxrb/ooxml/writer.rb +1017 -941
- data/lib/xlsxrb/ooxml/xml_builder.rb +48 -16
- data/lib/xlsxrb/ooxml/xml_parser.rb +37 -2
- data/lib/xlsxrb/ooxml/zip_generator.rb +10 -10
- data/lib/xlsxrb/ooxml/zip_reader.rb +19 -6
- data/lib/xlsxrb/ooxml/zip_writer.rb +15 -7
- data/lib/xlsxrb/stream_row.rb +8 -5
- data/lib/xlsxrb/stream_sheet.rb +3 -0
- data/lib/xlsxrb/stream_writer.rb +48 -18
- data/lib/xlsxrb/style_builder.rb +9 -4
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +14 -8
- data/lib/xlsxrb/worksheet_builder.rb +10 -4
- data/lib/xlsxrb.rb +415 -418
- data/sig/generated/xlsxrb/elements/workbook.rbs +23 -1
- data/sig/generated/xlsxrb/ooxml/reader/listeners/core_listeners.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/workbook_parser.rbs +21 -1
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +20 -2
- data/sig/generated/xlsxrb/ooxml/writer/drawing_xml.rbs +162 -47
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +20 -24
- data/sig/generated/xlsxrb/ooxml/writer/styles_xml.rbs +20 -20
- data/sig/generated/xlsxrb/ooxml/writer.rbs +61 -10
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +19 -1
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +21 -0
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +5 -0
- data/sig/generated/xlsxrb/stream_row.rbs +4 -3
- data/sig/generated/xlsxrb/style_builder.rbs +5 -5
- data/sig/generated/xlsxrb.rbs +41 -20
- metadata +1 -1
|
@@ -7,6 +7,20 @@ module Xlsxrb
|
|
|
7
7
|
# Streams well-formed XML to a writable IO without building a DOM.
|
|
8
8
|
class XmlBuilder
|
|
9
9
|
XML_HEADER = %(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n)
|
|
10
|
+
PLAIN_XML_HEADER = %(<?xml version="1.0" encoding="UTF-8"?>)
|
|
11
|
+
|
|
12
|
+
# Builds an XML string with a new {XmlBuilder}.
|
|
13
|
+
#
|
|
14
|
+
# @param declaration [Symbol, false, nil] `:standalone`, `:plain`, or falsy for none.
|
|
15
|
+
# @return [String]
|
|
16
|
+
#: (?declaration: (:standalone | :plain | false)?) { (XmlBuilder) -> void } -> String
|
|
17
|
+
def self.build(declaration: nil)
|
|
18
|
+
io = +""
|
|
19
|
+
builder = new(io)
|
|
20
|
+
builder.declaration(standalone: declaration == :standalone) if declaration
|
|
21
|
+
yield builder
|
|
22
|
+
io
|
|
23
|
+
end
|
|
10
24
|
|
|
11
25
|
ESCAPE_MAP = {
|
|
12
26
|
"&" => "&",
|
|
@@ -22,8 +36,9 @@ module Xlsxrb
|
|
|
22
36
|
@io = io
|
|
23
37
|
end
|
|
24
38
|
|
|
25
|
-
|
|
26
|
-
|
|
39
|
+
#: (?standalone: bool) -> self
|
|
40
|
+
def declaration(standalone: true)
|
|
41
|
+
@io << (standalone ? XML_HEADER : PLAIN_XML_HEADER)
|
|
27
42
|
self
|
|
28
43
|
end
|
|
29
44
|
|
|
@@ -42,25 +57,25 @@ module Xlsxrb
|
|
|
42
57
|
end
|
|
43
58
|
|
|
44
59
|
def open_tag(name, attrs = EMPTY_HASH)
|
|
45
|
-
@io
|
|
46
|
-
@io
|
|
60
|
+
@io << "<"
|
|
61
|
+
@io << name
|
|
47
62
|
write_attrs(attrs) unless attrs.empty?
|
|
48
|
-
@io
|
|
63
|
+
@io << ">"
|
|
49
64
|
self
|
|
50
65
|
end
|
|
51
66
|
|
|
52
67
|
def close_tag(name)
|
|
53
|
-
@io
|
|
54
|
-
@io
|
|
55
|
-
@io
|
|
68
|
+
@io << "</"
|
|
69
|
+
@io << name
|
|
70
|
+
@io << ">"
|
|
56
71
|
self
|
|
57
72
|
end
|
|
58
73
|
|
|
59
74
|
def empty_tag(name, attrs = EMPTY_HASH)
|
|
60
|
-
@io
|
|
61
|
-
@io
|
|
75
|
+
@io << "<"
|
|
76
|
+
@io << name
|
|
62
77
|
write_attrs(attrs) unless attrs.empty?
|
|
63
|
-
@io
|
|
78
|
+
@io << "/>"
|
|
64
79
|
self
|
|
65
80
|
end
|
|
66
81
|
|
|
@@ -69,6 +84,23 @@ module Xlsxrb
|
|
|
69
84
|
self
|
|
70
85
|
end
|
|
71
86
|
|
|
87
|
+
# Writes an element containing escaped text content.
|
|
88
|
+
#: (String name, untyped content, ?Hash[untyped, untyped] attrs) -> self
|
|
89
|
+
def text_tag(name, content, attrs = EMPTY_HASH)
|
|
90
|
+
open_tag(name, attrs)
|
|
91
|
+
text(content)
|
|
92
|
+
close_tag(name)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Writes ST_Xstring-encoded and XML-escaped text content.
|
|
96
|
+
#: (untyped content) -> self
|
|
97
|
+
def xstring_text(content)
|
|
98
|
+
str = content.to_s
|
|
99
|
+
str = str.dup.force_encoding(Encoding::UTF_8) if str.encoding.equal?(Encoding::BINARY)
|
|
100
|
+
@io << escape(self.class.encode_xstring(str))
|
|
101
|
+
self
|
|
102
|
+
end
|
|
103
|
+
|
|
72
104
|
# Write raw XML string (for unmapped_data restoration).
|
|
73
105
|
def raw(xml_string)
|
|
74
106
|
@io << xml_string
|
|
@@ -182,11 +214,11 @@ module Xlsxrb
|
|
|
182
214
|
attrs.each do |k, v|
|
|
183
215
|
next if v.nil?
|
|
184
216
|
|
|
185
|
-
@io
|
|
186
|
-
@io
|
|
187
|
-
@io
|
|
188
|
-
@io
|
|
189
|
-
@io
|
|
217
|
+
@io << " "
|
|
218
|
+
@io << k.to_s
|
|
219
|
+
@io << '="'
|
|
220
|
+
@io << escape(v)
|
|
221
|
+
@io << '"'
|
|
190
222
|
end
|
|
191
223
|
end
|
|
192
224
|
|
|
@@ -67,6 +67,39 @@ module Xlsxrb
|
|
|
67
67
|
parser.parse
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
+
# Parses XML string once using SAX2, forwarding events to all given listeners.
|
|
71
|
+
#: (String xml_string, Array[untyped] listeners) -> void
|
|
72
|
+
def self.parse_listeners(xml_string, listeners)
|
|
73
|
+
return if listeners.empty?
|
|
74
|
+
|
|
75
|
+
parse(xml_string, MultiListener.new(listeners))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Fans out SAX2 callbacks from a single parser pass across multiple listeners.
|
|
79
|
+
class MultiListener
|
|
80
|
+
include REXML::SAX2Listener
|
|
81
|
+
|
|
82
|
+
#: (Array[untyped] listeners) -> void
|
|
83
|
+
def initialize(listeners)
|
|
84
|
+
@listeners = listeners
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
#: (String? uri, String local_name, String qname, Hash[String, String] attributes) -> void
|
|
88
|
+
def start_element(uri, local_name, qname, attributes)
|
|
89
|
+
@listeners.each { |listener| listener.start_element(uri, local_name, qname, attributes) }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
#: (String text) -> void
|
|
93
|
+
def characters(text)
|
|
94
|
+
@listeners.each { |listener| listener.characters(text) }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
#: (String? uri, String local_name, String qname) -> void
|
|
98
|
+
def end_element(uri, local_name, qname)
|
|
99
|
+
@listeners.each { |listener| listener.end_element(uri, local_name, qname) }
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
70
103
|
# Returns a SAX2 parser whose listeners receive attribute values with XML entities
|
|
71
104
|
# decoded. REXML's SAX2 parser decodes text but passes attribute values as written, so
|
|
72
105
|
# formatCode=""$"0" would otherwise arrive with the " references.
|
|
@@ -89,9 +122,11 @@ module Xlsxrb
|
|
|
89
122
|
super
|
|
90
123
|
rescue REXML::ParseException => e
|
|
91
124
|
raise Xlsxrb::ParseError, "Malformed XML: #{e.message.lines.first.to_s.strip}"
|
|
125
|
+
rescue EncodingError => e
|
|
126
|
+
raise Xlsxrb::ParseError, "Malformed XML: #{e.message}"
|
|
92
127
|
rescue ArgumentError => e
|
|
93
|
-
# Bytes that are not valid in the document's encoding.
|
|
94
|
-
raise unless e.message.include?("invalid byte sequence")
|
|
128
|
+
# Bytes that are not valid in the document's encoding, or an unknown XML declaration encoding.
|
|
129
|
+
raise unless e.message.include?("invalid byte sequence") || e.message.start_with?("Bad encoding name ")
|
|
95
130
|
|
|
96
131
|
raise Xlsxrb::ParseError, "Malformed XML: #{e.message}"
|
|
97
132
|
rescue RuntimeError => e
|
|
@@ -72,15 +72,15 @@ module Xlsxrb
|
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def write_local_header(entry)
|
|
75
|
-
filename = entry[:path]
|
|
76
|
-
filename_bytes = filename.bytes
|
|
75
|
+
filename = entry[:path].encode("UTF-8")
|
|
76
|
+
filename_bytes = filename.b.bytes
|
|
77
77
|
|
|
78
78
|
# Local file header
|
|
79
79
|
local_header = []
|
|
80
80
|
local_header += [0x50, 0x4b, 0x03, 0x04] # Signature
|
|
81
|
-
local_header += [0x14, 0x00]
|
|
82
|
-
local_header += [0x00, 0x00]
|
|
83
|
-
local_header += [0x08, 0x00]
|
|
81
|
+
local_header += [0x14, 0x00] # Version needed to extract
|
|
82
|
+
local_header += filename.ascii_only? ? [0x00, 0x00] : [0x00, 0x08] # General purpose bit flag (EFS UTF-8 if non-ASCII)
|
|
83
|
+
local_header += [0x08, 0x00] # Compression method (8 = deflate)
|
|
84
84
|
|
|
85
85
|
# Last mod file time/date
|
|
86
86
|
dos_time = dos_datetime(entry[:mtime])
|
|
@@ -104,15 +104,15 @@ module Xlsxrb
|
|
|
104
104
|
offset = @file.pos
|
|
105
105
|
cd_offset = 0
|
|
106
106
|
@entries.each do |entry|
|
|
107
|
-
filename = entry[:path]
|
|
108
|
-
filename_bytes = filename.bytes
|
|
107
|
+
filename = entry[:path].encode("UTF-8")
|
|
108
|
+
filename_bytes = filename.b.bytes
|
|
109
109
|
|
|
110
110
|
cd_header = []
|
|
111
111
|
cd_header += [0x50, 0x4b, 0x01, 0x02] # Central directory file header signature
|
|
112
112
|
cd_header += [0x14, 0x03] # Version made by
|
|
113
113
|
cd_header += [0x14, 0x00] # Version needed to extract
|
|
114
|
-
cd_header += [0x00, 0x00]
|
|
115
|
-
cd_header += [0x08, 0x00]
|
|
114
|
+
cd_header += filename.ascii_only? ? [0x00, 0x00] : [0x00, 0x08] # General purpose bit flag (EFS UTF-8 if non-ASCII)
|
|
115
|
+
cd_header += [0x08, 0x00] # Compression method
|
|
116
116
|
|
|
117
117
|
dos_time = dos_datetime(entry[:mtime])
|
|
118
118
|
cd_header += dos_time
|
|
@@ -175,7 +175,7 @@ module Xlsxrb
|
|
|
175
175
|
end
|
|
176
176
|
|
|
177
177
|
def calculate_central_dir_size
|
|
178
|
-
@entries.sum { |entry| 46 + entry[:path].bytesize }
|
|
178
|
+
@entries.sum { |entry| 46 + entry[:path].encode("UTF-8").bytesize }
|
|
179
179
|
end
|
|
180
180
|
end
|
|
181
181
|
end
|
|
@@ -74,7 +74,7 @@ module Xlsxrb
|
|
|
74
74
|
return result
|
|
75
75
|
end
|
|
76
76
|
|
|
77
|
-
return
|
|
77
|
+
return String.new(encoding: @encoding || Encoding::BINARY) if length.zero?
|
|
78
78
|
|
|
79
79
|
while @buffer.bytesize - @buffer_pos < length && !@eof
|
|
80
80
|
chunk = fetch_chunk([length - (@buffer.bytesize - @buffer_pos), DEFAULT_CHUNK_SIZE].max)
|
|
@@ -303,6 +303,9 @@ module Xlsxrb
|
|
|
303
303
|
return nil unless entry
|
|
304
304
|
|
|
305
305
|
verify_entry_bounds!(entry)
|
|
306
|
+
limit = @max_uncompressed_size || MAX_UNCOMPRESSED_SIZE
|
|
307
|
+
raise ZipBombError, "ZIP bomb detected: Uncompressed size exceeds #{limit} bytes" if entry.method.zero? && entry.compressed_size > limit
|
|
308
|
+
|
|
306
309
|
raw = if @should_close_io && @raw_io.respond_to?(:closed?) && @raw_io.closed? && @filepath
|
|
307
310
|
File.open(@filepath, "rb") do |f|
|
|
308
311
|
f.seek(entry.data_offset, IO::SEEK_SET)
|
|
@@ -528,7 +531,11 @@ module Xlsxrb
|
|
|
528
531
|
break unless cd_header && cd_header.bytesize == 42
|
|
529
532
|
|
|
530
533
|
_v_made, _v_need, _gp, method, _time, _date, crc, csize, usize, nlen, elen, clen, _dnum, _iattr, _eattr, offset = cd_header.unpack("vvvvvvVVVvvvvvVV")
|
|
531
|
-
|
|
534
|
+
raw_name = io.read(nlen)
|
|
535
|
+
break unless raw_name && raw_name.bytesize == nlen
|
|
536
|
+
|
|
537
|
+
entry_name = raw_name.force_encoding(Encoding::UTF_8)
|
|
538
|
+
entry_name = entry_name.scrub unless entry_name.valid_encoding?
|
|
532
539
|
extra_data = elen.positive? ? io.read(elen) : nil
|
|
533
540
|
io.read(clen) if clen.positive?
|
|
534
541
|
|
|
@@ -594,7 +601,8 @@ module Xlsxrb
|
|
|
594
601
|
entry_name = io.read(name_len)
|
|
595
602
|
break unless entry_name && entry_name.bytesize == name_len
|
|
596
603
|
|
|
597
|
-
entry_name.force_encoding(
|
|
604
|
+
entry_name.force_encoding(Encoding::UTF_8)
|
|
605
|
+
entry_name = entry_name.scrub unless entry_name.valid_encoding?
|
|
598
606
|
extra_data = extra_len.positive? ? io.read(extra_len) : nil
|
|
599
607
|
uncompressed_size, compressed_size, = parse_zip64_extra(extra_data, uncompressed_size, compressed_size, 0) if extra_data && (uncompressed_size == 0xFFFF_FFFF || compressed_size == 0xFFFF_FFFF)
|
|
600
608
|
data_offset = io.pos
|
|
@@ -691,7 +699,11 @@ module Xlsxrb
|
|
|
691
699
|
|
|
692
700
|
def decompress(raw, method)
|
|
693
701
|
data = case method
|
|
694
|
-
when 0
|
|
702
|
+
when 0
|
|
703
|
+
limit = @max_uncompressed_size || MAX_UNCOMPRESSED_SIZE
|
|
704
|
+
raise ZipBombError, "ZIP bomb detected: Uncompressed size exceeds #{limit} bytes" if raw && raw.bytesize > limit
|
|
705
|
+
|
|
706
|
+
raw&.dup || ""
|
|
695
707
|
when 8 then safe_inflate(raw || "", -Zlib::MAX_WBITS)
|
|
696
708
|
else raise ZipError, "Unsupported ZIP compression method: #{method}"
|
|
697
709
|
end
|
|
@@ -738,7 +750,8 @@ module Xlsxrb
|
|
|
738
750
|
end
|
|
739
751
|
|
|
740
752
|
def normalize_entry_name(name)
|
|
741
|
-
name.
|
|
753
|
+
safe = name.valid_encoding? ? name : name.scrub
|
|
754
|
+
safe.include?("\\") ? safe.tr("\\", "/") : safe
|
|
742
755
|
end
|
|
743
756
|
|
|
744
757
|
def lookup_entry(name)
|
|
@@ -747,7 +760,7 @@ module Xlsxrb
|
|
|
747
760
|
end
|
|
748
761
|
|
|
749
762
|
def ci_catalog
|
|
750
|
-
@ci_catalog ||= catalog.transform_keys(
|
|
763
|
+
@ci_catalog ||= catalog.transform_keys { |k| (k.valid_encoding? ? k : k.scrub).downcase }
|
|
751
764
|
end
|
|
752
765
|
|
|
753
766
|
def normalize_text_encoding(data)
|
|
@@ -18,6 +18,7 @@ module Xlsxrb
|
|
|
18
18
|
VERSION_DEFAULT = 20
|
|
19
19
|
VERSION_ZIP64 = 45
|
|
20
20
|
DATA_DESCRIPTOR_FLAG = 0x0008
|
|
21
|
+
UTF8_FLAG = 0x0800
|
|
21
22
|
|
|
22
23
|
def self.open(target, compression_level: DEFAULT_COMPRESSION_LEVEL, &block)
|
|
23
24
|
io = target.is_a?(String) ? File.open(target, "wb") : target
|
|
@@ -80,7 +81,7 @@ module Xlsxrb
|
|
|
80
81
|
validate_entry_name!(path)
|
|
81
82
|
@entry_path = path
|
|
82
83
|
@entry_offset = @bytes_written
|
|
83
|
-
@entry_gp_flag = @seekable ? 0 : DATA_DESCRIPTOR_FLAG
|
|
84
|
+
@entry_gp_flag = entry_gp_flag(path, @seekable ? 0 : DATA_DESCRIPTOR_FLAG)
|
|
84
85
|
@entry_deflater = Zlib::Deflate.new(@compression_level, -Zlib::MAX_WBITS)
|
|
85
86
|
@entry_crc = Zlib.crc32
|
|
86
87
|
@entry_uncompressed_size = 0
|
|
@@ -122,14 +123,14 @@ module Xlsxrb
|
|
|
122
123
|
|
|
123
124
|
if @entry_seekable && !zip64
|
|
124
125
|
# Patch the local header if seekable
|
|
125
|
-
patch_local_header { write_local_header(@entry_path, final_crc, @entry_compressed_size, @entry_uncompressed_size, gp_flag:
|
|
126
|
+
patch_local_header { write_local_header(@entry_path, final_crc, @entry_compressed_size, @entry_uncompressed_size, gp_flag: @entry_gp_flag, count_bytes: false) }
|
|
126
127
|
else
|
|
127
128
|
# Unseekable output always ends a streamed entry with an 8-byte data descriptor.
|
|
128
129
|
# On seekable output, an entry that outgrew 32-bit sizes falls back to one too,
|
|
129
130
|
# because the local header has no room for a ZIP64 extra field.
|
|
130
131
|
write_data_descriptor(final_crc, @entry_compressed_size, @entry_uncompressed_size, zip64: zip64)
|
|
131
132
|
if @entry_seekable
|
|
132
|
-
@entry_gp_flag
|
|
133
|
+
@entry_gp_flag |= DATA_DESCRIPTOR_FLAG
|
|
133
134
|
patch_local_header { write_local_header(@entry_path, 0, 0, 0, gp_flag: @entry_gp_flag, count_bytes: false, version: VERSION_ZIP64) }
|
|
134
135
|
end
|
|
135
136
|
end
|
|
@@ -152,8 +153,9 @@ module Xlsxrb
|
|
|
152
153
|
raise "ZipWriter is closed" if @closed
|
|
153
154
|
|
|
154
155
|
validate_entry_name!(path)
|
|
156
|
+
gp_flag = entry_gp_flag(path)
|
|
155
157
|
offset = @bytes_written
|
|
156
|
-
write_local_header(path, crc32, compressed_size, uncompressed_size, gp_flag:
|
|
158
|
+
write_local_header(path, crc32, compressed_size, uncompressed_size, gp_flag: gp_flag, method: method)
|
|
157
159
|
|
|
158
160
|
if compressed_size.positive?
|
|
159
161
|
source_io.seek(data_offset, IO::SEEK_SET)
|
|
@@ -167,7 +169,7 @@ module Xlsxrb
|
|
|
167
169
|
compressed_size: compressed_size,
|
|
168
170
|
uncompressed_size: uncompressed_size,
|
|
169
171
|
offset: offset,
|
|
170
|
-
gp_flag:
|
|
172
|
+
gp_flag: gp_flag,
|
|
171
173
|
method: method
|
|
172
174
|
}
|
|
173
175
|
end
|
|
@@ -236,13 +238,19 @@ module Xlsxrb
|
|
|
236
238
|
@bytes_written += data.bytesize
|
|
237
239
|
end
|
|
238
240
|
|
|
241
|
+
#: (String path, ?Integer base_flag) -> Integer
|
|
242
|
+
def entry_gp_flag(path, base_flag = 0)
|
|
243
|
+
path.encode("UTF-8").b.ascii_only? ? base_flag : (base_flag | UTF8_FLAG)
|
|
244
|
+
end
|
|
245
|
+
|
|
239
246
|
def write_raw_entry(path, content_bytes)
|
|
240
247
|
validate_entry_name!(path)
|
|
248
|
+
gp_flag = entry_gp_flag(path)
|
|
241
249
|
crc = Zlib.crc32(content_bytes) & 0xFFFFFFFF
|
|
242
250
|
compressed = deflate(content_bytes)
|
|
243
251
|
offset = @bytes_written
|
|
244
252
|
|
|
245
|
-
write_local_header(path, crc, compressed.bytesize, content_bytes.bytesize, gp_flag:
|
|
253
|
+
write_local_header(path, crc, compressed.bytesize, content_bytes.bytesize, gp_flag: gp_flag)
|
|
246
254
|
write_bytes(compressed)
|
|
247
255
|
|
|
248
256
|
@entries << {
|
|
@@ -251,7 +259,7 @@ module Xlsxrb
|
|
|
251
259
|
compressed_size: compressed.bytesize,
|
|
252
260
|
uncompressed_size: content_bytes.bytesize,
|
|
253
261
|
offset: offset,
|
|
254
|
-
gp_flag:
|
|
262
|
+
gp_flag: gp_flag,
|
|
255
263
|
method: 8
|
|
256
264
|
}
|
|
257
265
|
end
|
data/lib/xlsxrb/stream_row.rb
CHANGED
|
@@ -43,9 +43,10 @@ module Xlsxrb
|
|
|
43
43
|
# @param styles [Elements::Styles, Hash, nil] Optional parsed styles.
|
|
44
44
|
# @param date1904 [Boolean] Whether the 1904 date system is active.
|
|
45
45
|
# @param pad_empty_cells [Boolean] Whether missing cells in this row are padded.
|
|
46
|
-
|
|
46
|
+
# @param shared_formulas [Hash{Integer => Hash}, nil] Sheet shared formula masters table.
|
|
47
|
+
#: (index: Integer, xml_bytes: String, from: Integer, to: Integer, shared_strings: Array[String], ?prefix: String, ?height: Float | Integer | nil, ?hidden: bool, ?custom_height: bool, ?collapsed: bool, ?outline_level: Integer | nil, ?style_index: Integer | nil, ?styles: (Elements::Styles | Hash[untyped, untyped])?, ?date1904: bool, ?pad_empty_cells: bool, ?shared_formulas: Hash[Integer, Hash[Symbol, untyped]]?) -> void
|
|
47
48
|
def initialize(index:, xml_bytes:, from:, to:, shared_strings:, prefix: "", height: nil, hidden: false,
|
|
48
|
-
custom_height: false, collapsed: false, outline_level: nil, style_index: nil, styles: nil, date1904: false, pad_empty_cells: false)
|
|
49
|
+
custom_height: false, collapsed: false, outline_level: nil, style_index: nil, styles: nil, date1904: false, pad_empty_cells: false, shared_formulas: nil)
|
|
49
50
|
@index = index
|
|
50
51
|
@xml = xml_bytes.encoding == Encoding::BINARY ? xml_bytes : xml_bytes.b
|
|
51
52
|
@from = from
|
|
@@ -61,12 +62,13 @@ module Xlsxrb
|
|
|
61
62
|
@styles = styles
|
|
62
63
|
@date1904 = date1904 ? true : false
|
|
63
64
|
@pad_empty_cells = pad_empty_cells ? true : false
|
|
65
|
+
@shared_formulas = shared_formulas
|
|
64
66
|
@cells = nil
|
|
65
67
|
end
|
|
66
68
|
|
|
67
69
|
# @api private
|
|
68
70
|
# rubocop:disable Style/OptionalBooleanParameter
|
|
69
|
-
def self.fast_create(index, xml_bytes, from, to, shared_strings, prefix = "", height = nil, hidden = false, custom_height = false, outline_level = nil, style_index = nil, styles = nil, date1904 = false, collapsed = false, pad_empty_cells = false)
|
|
71
|
+
def self.fast_create(index, xml_bytes, from, to, shared_strings, prefix = "", height = nil, hidden = false, custom_height = false, outline_level = nil, style_index = nil, styles = nil, date1904 = false, collapsed = false, pad_empty_cells = false, shared_formulas = nil)
|
|
70
72
|
inst = allocate
|
|
71
73
|
inst.instance_variable_set(:@index, index)
|
|
72
74
|
inst.instance_variable_set(:@xml, xml_bytes.encoding == Encoding::BINARY ? xml_bytes : xml_bytes.b)
|
|
@@ -83,6 +85,7 @@ module Xlsxrb
|
|
|
83
85
|
inst.instance_variable_set(:@styles, styles)
|
|
84
86
|
inst.instance_variable_set(:@date1904, date1904 ? true : false)
|
|
85
87
|
inst.instance_variable_set(:@pad_empty_cells, pad_empty_cells ? true : false)
|
|
88
|
+
inst.instance_variable_set(:@shared_formulas, shared_formulas)
|
|
86
89
|
inst.instance_variable_set(:@cells, nil)
|
|
87
90
|
inst
|
|
88
91
|
end
|
|
@@ -193,7 +196,7 @@ module Xlsxrb
|
|
|
193
196
|
if @cells || @pad_empty_cells
|
|
194
197
|
cells.each(&block)
|
|
195
198
|
else
|
|
196
|
-
Ooxml::WorksheetParser.fast_scan_cells_direct(@xml, @from, @to, @shared_strings, @index, @prefix, @styles, @date1904, &block)
|
|
199
|
+
Ooxml::WorksheetParser.fast_scan_cells_direct(@xml, @from, @to, @shared_strings, @index, @prefix, @styles, @date1904, @shared_formulas, &block)
|
|
197
200
|
end
|
|
198
201
|
end
|
|
199
202
|
|
|
@@ -217,7 +220,7 @@ module Xlsxrb
|
|
|
217
220
|
def cells
|
|
218
221
|
@cells ||= begin
|
|
219
222
|
arr = []
|
|
220
|
-
Ooxml::WorksheetParser.fast_scan_cells_direct(@xml, @from, @to, @shared_strings, @index, @prefix, @styles, @date1904) do |c|
|
|
223
|
+
Ooxml::WorksheetParser.fast_scan_cells_direct(@xml, @from, @to, @shared_strings, @index, @prefix, @styles, @date1904, @shared_formulas) do |c|
|
|
221
224
|
arr << c
|
|
222
225
|
end
|
|
223
226
|
if @pad_empty_cells && !arr.empty?
|
data/lib/xlsxrb/stream_sheet.rb
CHANGED
|
@@ -1075,6 +1075,8 @@ module Xlsxrb
|
|
|
1075
1075
|
width: rc[:width],
|
|
1076
1076
|
hidden: rc[:hidden] || false,
|
|
1077
1077
|
custom_width: rc[:custom_width] || false,
|
|
1078
|
+
best_fit: rc[:best_fit] || false,
|
|
1079
|
+
collapsed: rc[:collapsed] || false,
|
|
1078
1080
|
outline_level: rc[:outline_level],
|
|
1079
1081
|
style_index: rc[:style_index],
|
|
1080
1082
|
unmapped_data: col_unmapped
|
|
@@ -1249,6 +1251,7 @@ module Xlsxrb
|
|
|
1249
1251
|
# UTF-8 is searched as bytes.
|
|
1250
1252
|
#: (String xml) -> String?
|
|
1251
1253
|
def dimension_ref_in(xml)
|
|
1254
|
+
return nil unless xml.include?("dimension")
|
|
1252
1255
|
return xml.slice(DIMENSION_RE, 1) if xml.valid_encoding?
|
|
1253
1256
|
|
|
1254
1257
|
xml.b.slice(DIMENSION_RE, 1)&.force_encoding(Encoding::UTF_8)&.scrub
|
data/lib/xlsxrb/stream_writer.rb
CHANGED
|
@@ -1735,6 +1735,17 @@ module Xlsxrb
|
|
|
1735
1735
|
else
|
|
1736
1736
|
row_style
|
|
1737
1737
|
end
|
|
1738
|
+
if cell_styles.is_a?(Array) && cell_styles.any?(Hash)
|
|
1739
|
+
cell_styles = cell_styles.map do |cell_style|
|
|
1740
|
+
if cell_style.is_a?(Hash)
|
|
1741
|
+
inline_cell_name = "__inline_#{cell_style.hash}"
|
|
1742
|
+
style(inline_cell_name, **cell_style) unless @style_name_to_id.key?(inline_cell_name)
|
|
1743
|
+
inline_cell_name
|
|
1744
|
+
else
|
|
1745
|
+
cell_style
|
|
1746
|
+
end
|
|
1747
|
+
end
|
|
1748
|
+
end
|
|
1738
1749
|
if cell_styles || row_style || !@current_column_style_names.empty?
|
|
1739
1750
|
cell_styles = with_date_style_variants(values, cell_styles, row_style.is_a?(Hash) ? inline_name : row_style)
|
|
1740
1751
|
end
|
|
@@ -3160,22 +3171,23 @@ module Xlsxrb
|
|
|
3160
3171
|
existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
3161
3172
|
return nil unless existing
|
|
3162
3173
|
|
|
3163
|
-
c = Regexp.last_match(1).delete("$") if existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
|
|
3164
|
-
r = Regexp.last_match(1).delete("$") if existing[:value] =~ /'[^']*'!\$([0-9]+:\$[0-9]+)/
|
|
3174
|
+
c = Regexp.last_match(1).delete("$") if existing[:value] =~ /'(?:''|[^'])*'!\$([A-Za-z]+:\$[A-Za-z]+)/
|
|
3175
|
+
r = Regexp.last_match(1).delete("$") if existing[:value] =~ /'(?:''|[^'])*'!\$([0-9]+:\$[0-9]+)/
|
|
3165
3176
|
return Elements::PrintTitles.new(rows: r, columns: c)
|
|
3166
3177
|
end
|
|
3167
3178
|
|
|
3168
|
-
@current_print_titles = { rows: rows, columns: columns }.compact if sheet_name == @current_sheet
|
|
3169
3179
|
c = DslHelpers.normalize_print_titles_columns(columns)
|
|
3170
3180
|
r = DslHelpers.normalize_print_titles_rows(rows)
|
|
3171
3181
|
existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
3172
3182
|
if existing && (r.nil? || c.nil?)
|
|
3173
|
-
c = Regexp.last_match(1).delete("$") if c.nil? && existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
|
|
3174
|
-
r = Regexp.last_match(1).delete("$") if r.nil? && existing[:value] =~ /'[^']*'!\$([0-9]+:\$[0-9]+)/
|
|
3183
|
+
c = Regexp.last_match(1).delete("$") if c.nil? && existing[:value] =~ /'(?:''|[^'])*'!\$([A-Za-z]+:\$[A-Za-z]+)/
|
|
3184
|
+
r = Regexp.last_match(1).delete("$") if r.nil? && existing[:value] =~ /'(?:''|[^'])*'!\$([0-9]+:\$[0-9]+)/
|
|
3175
3185
|
end
|
|
3186
|
+
@current_print_titles = { rows: r, columns: c }.compact if sheet_name == @current_sheet
|
|
3187
|
+
quoted_sheet = DslHelpers.quote_sheet_name(sheet_name)
|
|
3176
3188
|
parts = []
|
|
3177
|
-
parts << "
|
|
3178
|
-
parts << "
|
|
3189
|
+
parts << "#{quoted_sheet}!$#{c.sub(":", ":$")}" if c
|
|
3190
|
+
parts << "#{quoted_sheet}!$#{r.sub(":", ":$")}" if r
|
|
3179
3191
|
value = parts.join(",")
|
|
3180
3192
|
@defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
3181
3193
|
defined_name("_xlnm.Print_Titles", value, sheet: sheet_name, scope: sheet_name) unless value.empty?
|
|
@@ -3372,7 +3384,12 @@ module Xlsxrb
|
|
|
3372
3384
|
#: () -> Hash[String, Elements::CustomProperty]
|
|
3373
3385
|
def custom_properties
|
|
3374
3386
|
@custom_properties.to_h do |cp|
|
|
3375
|
-
prop = cp.is_a?(Elements::CustomProperty)
|
|
3387
|
+
prop = if cp.is_a?(Elements::CustomProperty)
|
|
3388
|
+
cp
|
|
3389
|
+
else
|
|
3390
|
+
val = cp.key?(:value) ? cp[:value] : cp["value"]
|
|
3391
|
+
Elements::CustomProperty.new(name: (cp[:name] || cp["name"]).to_s, value: val, type: cp[:type] || cp["type"] || :string)
|
|
3392
|
+
end
|
|
3376
3393
|
[prop.name.to_s, prop]
|
|
3377
3394
|
end.freeze
|
|
3378
3395
|
end
|
|
@@ -3526,21 +3543,34 @@ module Xlsxrb
|
|
|
3526
3543
|
next unless value.is_a?(Date) || value.is_a?(Time)
|
|
3527
3544
|
|
|
3528
3545
|
style_name = styles_for_cells ? styles_for_cells[idx] : cell_styles
|
|
3529
|
-
|
|
3546
|
+
from_column = false
|
|
3547
|
+
if style_name.nil?
|
|
3548
|
+
if row_style.nil?
|
|
3549
|
+
style_name = @current_column_style_names[idx]
|
|
3550
|
+
from_column = !style_name.nil?
|
|
3551
|
+
else
|
|
3552
|
+
style_name = row_style
|
|
3553
|
+
end
|
|
3554
|
+
end
|
|
3530
3555
|
next unless style_name.is_a?(String) || style_name.is_a?(Symbol)
|
|
3531
3556
|
|
|
3532
3557
|
variant = DslHelpers.date_style_variant(value, style_name.to_s, @styles[style_name.to_s])
|
|
3533
|
-
|
|
3558
|
+
target_style = if variant
|
|
3559
|
+
variant_name, number_format = variant
|
|
3560
|
+
unless @style_name_to_id.key?(variant_name)
|
|
3561
|
+
variant_builder = @styles[style_name.to_s].with_number_format(variant_name, number_format)
|
|
3562
|
+
@styles[variant_name] = variant_builder
|
|
3563
|
+
@style_name_to_id[variant_name] = variant_builder.register_with(@style_writer)
|
|
3564
|
+
end
|
|
3565
|
+
variant_name
|
|
3566
|
+
elsif from_column
|
|
3567
|
+
style_name
|
|
3568
|
+
end
|
|
3569
|
+
next unless target_style
|
|
3534
3570
|
|
|
3535
|
-
variant_name, number_format = variant
|
|
3536
|
-
unless @style_name_to_id.key?(variant_name)
|
|
3537
|
-
variant_builder = @styles[style_name.to_s].with_number_format(variant_name, number_format)
|
|
3538
|
-
@styles[variant_name] = variant_builder
|
|
3539
|
-
@style_name_to_id[variant_name] = variant_builder.register_with(@style_writer)
|
|
3540
|
-
end
|
|
3541
3571
|
styles_for_cells ||= Array.new(values.size, cell_styles)
|
|
3542
3572
|
styles_for_cells = styles_for_cells.dup if styles_for_cells.equal?(cell_styles)
|
|
3543
|
-
styles_for_cells[idx] =
|
|
3573
|
+
styles_for_cells[idx] = target_style
|
|
3544
3574
|
end
|
|
3545
3575
|
styles_for_cells || cell_styles
|
|
3546
3576
|
end
|
|
@@ -3555,7 +3585,7 @@ module Xlsxrb
|
|
|
3555
3585
|
@current_print_area = range&.to_s if sheet_name == @current_sheet
|
|
3556
3586
|
@defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sheet_name }
|
|
3557
3587
|
if range
|
|
3558
|
-
value = "
|
|
3588
|
+
value = "#{DslHelpers.quote_sheet_name(sheet_name)}!#{absolute_range(range)}"
|
|
3559
3589
|
defined_name("_xlnm.Print_Area", value, sheet: sheet_name, scope: sheet_name)
|
|
3560
3590
|
end
|
|
3561
3591
|
range&.to_s
|
data/lib/xlsxrb/style_builder.rb
CHANGED
|
@@ -775,6 +775,7 @@ module Xlsxrb
|
|
|
775
775
|
def fill_pattern(pattern = nil, fg_color: nil, bg_color: nil)
|
|
776
776
|
return @fill_props[:pattern] if pattern.nil? && fg_color.nil? && bg_color.nil?
|
|
777
777
|
|
|
778
|
+
@fill_props.delete(:gradient)
|
|
778
779
|
@fill_props[:pattern] = pattern if pattern
|
|
779
780
|
@fill_props[:fg_color] = fg_color if fg_color
|
|
780
781
|
@fill_props[:bg_color] = bg_color if bg_color
|
|
@@ -790,6 +791,7 @@ module Xlsxrb
|
|
|
790
791
|
if pattern.is_a?(Hash)
|
|
791
792
|
fill_pattern(pattern[:pattern] || "solid", fg_color: pattern[:fg_color], bg_color: pattern[:bg_color])
|
|
792
793
|
else
|
|
794
|
+
@fill_props.delete(:gradient)
|
|
793
795
|
@fill_props[:pattern] = pattern
|
|
794
796
|
end
|
|
795
797
|
pattern
|
|
@@ -809,6 +811,8 @@ module Xlsxrb
|
|
|
809
811
|
# @api public
|
|
810
812
|
#: (String | Symbol color) -> (String | Symbol)
|
|
811
813
|
def fill_color=(color)
|
|
814
|
+
@fill_props.delete(:gradient)
|
|
815
|
+
@fill_props.delete(:bg_color)
|
|
812
816
|
@fill_props[:pattern] = "solid"
|
|
813
817
|
@fill_props[:fg_color] = resolve_color(color)
|
|
814
818
|
color
|
|
@@ -847,14 +851,15 @@ module Xlsxrb
|
|
|
847
851
|
# Sets or gets a gradient fill.
|
|
848
852
|
# @param type [String, nil] The gradient type.
|
|
849
853
|
# @param degree [Numeric, nil] The degree.
|
|
850
|
-
# @param stops [Array] The gradient stops.
|
|
854
|
+
# @param stops [Array, nil] The gradient stops.
|
|
851
855
|
# @return [Hash, void, nil]
|
|
852
856
|
# @api public
|
|
853
857
|
#: () -> Hash[Symbol, untyped]?
|
|
854
|
-
#: (?type: untyped, ?degree: Numeric | nil, ?stops: Array[untyped]) -> void
|
|
858
|
+
#: (?type: untyped, ?degree: Numeric | nil, ?stops: Array[untyped]?) -> void
|
|
855
859
|
def fill_gradient(type: nil, degree: nil, stops: [])
|
|
856
|
-
return @fill_props[:gradient] if type.nil? && degree.nil? && stops.empty?
|
|
860
|
+
return @fill_props[:gradient] if type.nil? && degree.nil? && (stops.nil? || stops.empty?)
|
|
857
861
|
|
|
862
|
+
@fill_props.clear
|
|
858
863
|
@fill_props[:gradient] = {
|
|
859
864
|
type: type,
|
|
860
865
|
degree: degree,
|
|
@@ -1452,7 +1457,7 @@ module Xlsxrb
|
|
|
1452
1457
|
definition_for_use.fill = opts
|
|
1453
1458
|
end
|
|
1454
1459
|
|
|
1455
|
-
#: (?type: untyped, ?degree: Numeric | nil, ?stops: Array[untyped]) -> void
|
|
1460
|
+
#: (?type: untyped, ?degree: Numeric | nil, ?stops: Array[untyped]?) -> void
|
|
1456
1461
|
def fill_gradient(...) = definition_for_use.fill_gradient(...)
|
|
1457
1462
|
|
|
1458
1463
|
#: (?all: Hash[Symbol, untyped]?, ?left: Hash[Symbol, untyped]?, ?right: Hash[Symbol, untyped]?, ?top: Hash[Symbol, untyped]?, ?bottom: Hash[Symbol, untyped]?, ?diagonal: Hash[Symbol, untyped]?) -> void
|
data/lib/xlsxrb/version.rb
CHANGED