xlsxrb 0.2.1 → 0.2.2
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 +51 -0
- data/README.md +24 -2
- data/lib/xlsxrb/dsl_helpers.rb +16 -0
- data/lib/xlsxrb/elements/styles.rb +15 -6
- data/lib/xlsxrb/number_formatter.rb +26 -8
- data/lib/xlsxrb/ooxml/cfb.rb +11 -3
- data/lib/xlsxrb/ooxml/crypto/agile.rb +48 -10
- data/lib/xlsxrb/ooxml/crypto/standard.rb +5 -1
- data/lib/xlsxrb/ooxml/crypto.rb +3 -0
- data/lib/xlsxrb/ooxml/shared_strings_parser.rb +26 -8
- data/lib/xlsxrb/ooxml/workbook_writer.rb +30 -19
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +116 -12
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +43 -38
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +206 -201
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +99 -99
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +46 -46
- data/lib/xlsxrb/ooxml/writer.rb +166 -159
- data/lib/xlsxrb/ooxml/xml_builder.rb +17 -7
- data/lib/xlsxrb/ooxml/xml_parser.rb +34 -1
- data/lib/xlsxrb/ooxml/zip_reader.rb +95 -68
- data/lib/xlsxrb/ooxml/zip_writer.rb +16 -0
- data/lib/xlsxrb/stream_sheet.rb +26 -11
- data/lib/xlsxrb/stream_writer.rb +9 -6
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/worksheet_builder.rb +6 -5
- data/lib/xlsxrb.rb +68 -21
- data/sig/generated/xlsxrb/dsl_helpers.rbs +9 -0
- data/sig/generated/xlsxrb/elements/styles.rbs +5 -0
- data/sig/generated/xlsxrb/number_formatter.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/cfb.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/crypto/agile.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/shared_strings_parser.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +8 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +40 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +4 -0
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +7 -2
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +12 -1
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -0
- data/sig/generated/xlsxrb/stream_sheet.rbs +9 -0
- data/sig/generated/xlsxrb/stream_writer.rbs +6 -2
- data/sig/generated/xlsxrb/worksheet_builder.rbs +3 -1
- data/sig/generated/xlsxrb.rbs +38 -8
- metadata +9 -3
data/lib/xlsxrb.rb
CHANGED
|
@@ -704,6 +704,28 @@ module Xlsxrb
|
|
|
704
704
|
DslHelpers.sanitize_sheet_name(name, replace_char: replace_char)
|
|
705
705
|
end
|
|
706
706
|
|
|
707
|
+
# Cuts text to what a cell holds: 32,767 characters counted in UTF-16 code units, as
|
|
708
|
+
# Excel counts them, so a character outside the Basic Multilingual Plane, such as an emoji,
|
|
709
|
+
# counts as two. A character is never split. Writing longer text raises ArgumentError
|
|
710
|
+
# with +strict_excel_mode+ (the default); call this first to keep the start of the text
|
|
711
|
+
# instead.
|
|
712
|
+
#
|
|
713
|
+
# Text of 32,767 bytes or fewer always fits, so it is returned as is after its byte size
|
|
714
|
+
# is checked, without counting its characters. Longer text is returned as a new UTF-8
|
|
715
|
+
# String, even if it fits.
|
|
716
|
+
#
|
|
717
|
+
# @example
|
|
718
|
+
# Xlsxrb.truncate_cell_text("x" * 40_000).length #=> 32767
|
|
719
|
+
# Xlsxrb.truncate_cell_text("😀" * 20_000).length #=> 16383
|
|
720
|
+
#
|
|
721
|
+
# @param text [String]
|
|
722
|
+
# @return [String]
|
|
723
|
+
# @api public
|
|
724
|
+
#: (String text) -> String
|
|
725
|
+
def self.truncate_cell_text(text)
|
|
726
|
+
DslHelpers.truncate_cell_text(text)
|
|
727
|
+
end
|
|
728
|
+
|
|
707
729
|
# Reads an XLSX file (streaming and lazy-loaded by default) from a file path, IO stream, or binary String.
|
|
708
730
|
#
|
|
709
731
|
# Sheets and rows are streamed lazily with O(1) constant memory. If a block is given,
|
|
@@ -735,12 +757,13 @@ module Xlsxrb
|
|
|
735
757
|
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) { (StreamSheet) -> void } -> void
|
|
736
758
|
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) -> Enumerator[StreamSheet, void]
|
|
737
759
|
def self.read(source, password: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, max_uncompressed_size: nil, &)
|
|
738
|
-
|
|
760
|
+
# The prepared source is what the Enumerator reads, so that it is prepared once.
|
|
761
|
+
source = prepare_source_io(source, password)
|
|
739
762
|
return to_enum(:read, source, password: password, trim_empty_rows: trim_empty_rows, pad_empty_rows: pad_empty_rows, pad_empty_cells: pad_empty_cells, max_uncompressed_size: max_uncompressed_size) unless block_given?
|
|
740
763
|
|
|
741
764
|
zip_options = {}
|
|
742
765
|
zip_options[:max_uncompressed_size] = max_uncompressed_size if max_uncompressed_size
|
|
743
|
-
zip_reader = Ooxml::ZipReader.open(
|
|
766
|
+
zip_reader = Ooxml::ZipReader.open(source, **zip_options)
|
|
744
767
|
|
|
745
768
|
begin
|
|
746
769
|
workbook_path = resolve_workbook_entry(zip_reader)
|
|
@@ -947,30 +970,36 @@ module Xlsxrb
|
|
|
947
970
|
|
|
948
971
|
# Writes an XLSX file or IO stream (streaming or in-memory), or returns a binary string.
|
|
949
972
|
#
|
|
950
|
-
# @overload write(?target, password: nil, encryption_mode:
|
|
973
|
+
# @overload write(?target, password: nil, encryption_mode: nil, strict_excel_mode: true, &block)
|
|
951
974
|
# Streaming write: yields a {StreamWriter} context for high-speed, zero-allocation XLSX generation.
|
|
952
975
|
# If target is omitted, returns the generated XLSX as an in-memory binary String.
|
|
953
976
|
# @param target [String, IO, StringIO, nil] Optional destination file path or writable IO object.
|
|
954
977
|
# @param password [String, nil] Optional password to encrypt the generated XLSX file.
|
|
955
|
-
# @param encryption_mode [Symbol] Encryption
|
|
978
|
+
# @param encryption_mode [Symbol, nil] Encryption scheme used with a password: `:agile` (AES-256, with a check
|
|
979
|
+
# that the file is unchanged) or `:standard` (AES-128; deprecated, and an error from 0.3.0). When omitted,
|
|
980
|
+
# Standard is written in 0.2 and Agile from 0.3.0. See docs/adr/0013-write-agile-encryption-read-both.md.
|
|
956
981
|
# @param strict_excel_mode [Boolean] Whether to enforce Microsoft Excel specification limits.
|
|
957
982
|
# @yield [stream_writer]
|
|
958
983
|
# @yieldparam stream_writer [Xlsxrb::StreamWriter]
|
|
959
984
|
# @return [String, void] Binary data if target is omitted, or void if written to target.
|
|
960
985
|
#
|
|
961
|
-
# @overload write(workbook, password: nil, encryption_mode:
|
|
986
|
+
# @overload write(workbook, password: nil, encryption_mode: nil)
|
|
962
987
|
# In-memory write: exports the workbook to an in-memory binary String.
|
|
963
988
|
# @param workbook [Elements::Workbook] The workbook to write.
|
|
964
989
|
# @param password [String, nil] Optional password to encrypt the binary string.
|
|
965
|
-
# @param encryption_mode [Symbol] Encryption
|
|
990
|
+
# @param encryption_mode [Symbol, nil] Encryption scheme used with a password: `:agile` (AES-256, with a check
|
|
991
|
+
# that the file is unchanged) or `:standard` (AES-128; deprecated, and an error from 0.3.0). When omitted,
|
|
992
|
+
# Standard is written in 0.2 and Agile from 0.3.0. See docs/adr/0013-write-agile-encryption-read-both.md.
|
|
966
993
|
# @return [String] Binary data representing the XLSX file.
|
|
967
994
|
#
|
|
968
|
-
# @overload write(target, workbook, password: nil, encryption_mode:
|
|
995
|
+
# @overload write(target, workbook, password: nil, encryption_mode: nil)
|
|
969
996
|
# In-memory write: writes the workbook to a file path or IO stream.
|
|
970
997
|
# @param target [String, IO, StringIO] Destination file path or writable IO object.
|
|
971
998
|
# @param workbook [Elements::Workbook] The workbook to write.
|
|
972
999
|
# @param password [String, nil] Optional password to encrypt the output file.
|
|
973
|
-
# @param encryption_mode [Symbol] Encryption
|
|
1000
|
+
# @param encryption_mode [Symbol, nil] Encryption scheme used with a password: `:agile` (AES-256, with a check
|
|
1001
|
+
# that the file is unchanged) or `:standard` (AES-128; deprecated, and an error from 0.3.0). When omitted,
|
|
1002
|
+
# Standard is written in 0.2 and Agile from 0.3.0. See docs/adr/0013-write-agile-encryption-read-both.md.
|
|
974
1003
|
# @return [void]
|
|
975
1004
|
#
|
|
976
1005
|
# @example Streaming write to file
|
|
@@ -999,8 +1028,8 @@ module Xlsxrb
|
|
|
999
1028
|
# @param compression_level [Integer, nil] ZIP deflate compression level (0..9, or Zlib::DEFAULT_COMPRESSION
|
|
1000
1029
|
# for zlib's level 6). Defaults to {DEFAULT_COMPRESSION_LEVEL}. Higher levels write smaller files more slowly;
|
|
1001
1030
|
# level 0 stores entries without compression. See docs/adr/0009-default-zip-compression-level.md.
|
|
1002
|
-
#: (?untyped target_or_workbook, ?Elements::Workbook | untyped workbook_or_nil, ?password: String?, ?encryption_mode: Symbol
|
|
1003
|
-
def self.write(target_or_workbook = OMITTED_TARGET, workbook_or_nil = nil, password: nil, encryption_mode:
|
|
1031
|
+
#: (?untyped target_or_workbook, ?Elements::Workbook | untyped workbook_or_nil, ?password: String?, ?encryption_mode: Symbol?, ?strict_excel_mode: bool, ?default_font: untyped?, ?shared_strings: bool, ?compression_level: Integer | nil) ?{ (StreamWriter) -> void } -> untyped
|
|
1032
|
+
def self.write(target_or_workbook = OMITTED_TARGET, workbook_or_nil = nil, password: nil, encryption_mode: nil, strict_excel_mode: true, default_font: nil, shared_strings: true, compression_level: DEFAULT_COMPRESSION_LEVEL, &)
|
|
1004
1033
|
if block_given?
|
|
1005
1034
|
if target_or_workbook.equal?(OMITTED_TARGET)
|
|
1006
1035
|
io = StringIO.new
|
|
@@ -1020,7 +1049,7 @@ module Xlsxrb
|
|
|
1020
1049
|
yield stream_writer
|
|
1021
1050
|
stream_writer.close
|
|
1022
1051
|
plain_bytes = buf.string.b
|
|
1023
|
-
encrypted_bytes = Ooxml::Crypto.encrypt(plain_bytes, password, mode: encryption_mode)
|
|
1052
|
+
encrypted_bytes = Ooxml::Crypto.encrypt(plain_bytes, password, mode: write_encryption_mode(encryption_mode))
|
|
1024
1053
|
if target.is_a?(String)
|
|
1025
1054
|
File.binwrite(target, encrypted_bytes)
|
|
1026
1055
|
elsif target.respond_to?(:write)
|
|
@@ -1239,7 +1268,7 @@ module Xlsxrb
|
|
|
1239
1268
|
workbook_properties: wb_properties,
|
|
1240
1269
|
compression_level: compression_level
|
|
1241
1270
|
)
|
|
1242
|
-
encrypted_bytes = Ooxml::Crypto.encrypt(buf.string.b, password, mode: encryption_mode)
|
|
1271
|
+
encrypted_bytes = Ooxml::Crypto.encrypt(buf.string.b, password, mode: write_encryption_mode(encryption_mode))
|
|
1243
1272
|
if target.is_a?(String)
|
|
1244
1273
|
File.binwrite(target, encrypted_bytes)
|
|
1245
1274
|
elsif target.respond_to?(:write)
|
|
@@ -2599,11 +2628,27 @@ module Xlsxrb
|
|
|
2599
2628
|
)
|
|
2600
2629
|
end
|
|
2601
2630
|
|
|
2631
|
+
STANDARD_ENCRYPTION_MESSAGE = "encryption_mode: :standard is deprecated; pass encryption_mode: :agile, which Excel 2010 and later open. " \
|
|
2632
|
+
"In version 0.3.0 a password always writes Agile encryption and :standard raises ArgumentError."
|
|
2633
|
+
private_constant :STANDARD_ENCRYPTION_MESSAGE
|
|
2634
|
+
|
|
2635
|
+
# The scheme to encrypt with. Standard stays the default in 0.2; asking for it by name warns.
|
|
2636
|
+
#: (Symbol? encryption_mode) -> Symbol
|
|
2637
|
+
def write_encryption_mode(encryption_mode)
|
|
2638
|
+
Deprecation.warn(STANDARD_ENCRYPTION_MESSAGE, uplevel: nil) if encryption_mode == :standard
|
|
2639
|
+
encryption_mode || :standard
|
|
2640
|
+
end
|
|
2641
|
+
|
|
2642
|
+
STRING_SOURCE_AS_DATA_MESSAGE = "Passing xlsx data as a String to Xlsxrb.load and Xlsxrb.read is deprecated; " \
|
|
2643
|
+
"pass a stream, as in StringIO.new(data). In version 0.3.0 a String is always a file path."
|
|
2644
|
+
private_constant :STRING_SOURCE_AS_DATA_MESSAGE
|
|
2645
|
+
|
|
2602
2646
|
#: (untyped source, String? password) -> untyped
|
|
2603
2647
|
def prepare_source_io(source, password)
|
|
2604
2648
|
if source.is_a?(String)
|
|
2605
2649
|
bin_source = source.encoding == Encoding::BINARY ? source : source.b
|
|
2606
2650
|
if bin_source.start_with?("PK\x03\x04".b) || bin_source.include?("\x00".b) || Ooxml::Cfb::Reader.cfb?(bin_source)
|
|
2651
|
+
Deprecation.warn(STRING_SOURCE_AS_DATA_MESSAGE, uplevel: nil)
|
|
2607
2652
|
if Ooxml::Cfb::Reader.cfb?(bin_source)
|
|
2608
2653
|
decrypted_zip = Ooxml::Crypto.decrypt(bin_source, password)
|
|
2609
2654
|
StringIO.new(decrypted_zip)
|
|
@@ -2632,18 +2677,20 @@ module Xlsxrb
|
|
|
2632
2677
|
raise Errno::ENOENT, shown
|
|
2633
2678
|
end
|
|
2634
2679
|
elsif source.respond_to?(:read) && source.respond_to?(:pos) && source.respond_to?(:seek)
|
|
2635
|
-
|
|
2680
|
+
# Only looking at the first bytes may fail, for a stream that cannot seek. An error
|
|
2681
|
+
# from decrypting, such as a wrong password, is the caller's to see.
|
|
2682
|
+
first_bytes = begin
|
|
2636
2683
|
cur_pos = source.pos
|
|
2637
|
-
|
|
2684
|
+
peeked = source.read(8)
|
|
2638
2685
|
source.seek(cur_pos)
|
|
2639
|
-
|
|
2640
|
-
full_data = source.read
|
|
2641
|
-
decrypted_zip = Ooxml::Crypto.decrypt(full_data, password)
|
|
2642
|
-
StringIO.new(decrypted_zip)
|
|
2643
|
-
else
|
|
2644
|
-
source
|
|
2645
|
-
end
|
|
2686
|
+
peeked
|
|
2646
2687
|
rescue StandardError
|
|
2688
|
+
nil
|
|
2689
|
+
end
|
|
2690
|
+
if Ooxml::Cfb::Reader.cfb?(first_bytes)
|
|
2691
|
+
decrypted_zip = Ooxml::Crypto.decrypt(source.read, password)
|
|
2692
|
+
StringIO.new(decrypted_zip)
|
|
2693
|
+
else
|
|
2647
2694
|
source
|
|
2648
2695
|
end
|
|
2649
2696
|
else
|
|
@@ -288,6 +288,15 @@ module Xlsxrb
|
|
|
288
288
|
# : (String text) -> void
|
|
289
289
|
def self.check_cell_text_length!: (String text) -> void
|
|
290
290
|
|
|
291
|
+
# Cuts +text+ to the whole characters that fit in a cell: 32,767 UTF-16 code units, as
|
|
292
|
+
# Excel counts them (ADR 0011). Text of 32,767 bytes or fewer always fits and is returned
|
|
293
|
+
# as is, so most text is not counted. Longer text is returned as a new UTF-8 String.
|
|
294
|
+
#
|
|
295
|
+
# @param text [String]
|
|
296
|
+
# @return [String]
|
|
297
|
+
# : (String text) -> String
|
|
298
|
+
def self.truncate_cell_text: (String text) -> String
|
|
299
|
+
|
|
291
300
|
# Sanitizes a worksheet name to ensure it conforms to Excel constraints within 31 characters.
|
|
292
301
|
# Replaces forbidden characters (\ / ? * [ ] : and control characters),
|
|
293
302
|
# replaces leading/trailing apostrophes, and replaces reserved 'History' with 'History_'.
|
|
@@ -337,6 +337,11 @@ module Xlsxrb
|
|
|
337
337
|
|
|
338
338
|
private
|
|
339
339
|
|
|
340
|
+
# Whether +value+ can be an index into cellXfs. A larger Integer is not one, and
|
|
341
|
+
# Array#[] raises RangeError for an Integer that does not fit in a machine word.
|
|
342
|
+
# : (untyped value) -> bool
|
|
343
|
+
def style_index?: (untyped value) -> bool
|
|
344
|
+
|
|
340
345
|
def next_num_fmt_id: () -> untyped
|
|
341
346
|
|
|
342
347
|
def invalidate_cache!: () -> untyped
|
|
@@ -10,6 +10,13 @@ module Xlsxrb
|
|
|
10
10
|
# Built-in date and time format code strings.
|
|
11
11
|
DATE_PATTERNS: untyped
|
|
12
12
|
|
|
13
|
+
# Excel accepts format codes of up to 255 characters. Longer codes are not applied: the
|
|
14
|
+
# cost of formatting grows with the code, for every cell that uses it.
|
|
15
|
+
MAX_FORMAT_CODE_LENGTH: ::Integer
|
|
16
|
+
|
|
17
|
+
# Stands for a literal "%" while a date format code is converted to a strftime pattern.
|
|
18
|
+
LITERAL_PERCENT: ::String
|
|
19
|
+
|
|
13
20
|
# Formats a cell value according to an Excel format code string.
|
|
14
21
|
#
|
|
15
22
|
# @param value [Object, nil] The cell's raw or typed value.
|
|
@@ -83,6 +83,8 @@ module Xlsxrb
|
|
|
83
83
|
|
|
84
84
|
def parse_header: () -> untyped
|
|
85
85
|
|
|
86
|
+
# The header takes the first sector: 512 bytes in version 3, and 4,096 in version 4,
|
|
87
|
+
# where it is padded with zeros. Sector 0 follows it.
|
|
86
88
|
def sector_offset: (untyped sector_id) -> untyped
|
|
87
89
|
|
|
88
90
|
def read_sector: (untyped sector_id) -> untyped
|
|
@@ -25,6 +25,16 @@ module Xlsxrb
|
|
|
25
25
|
|
|
26
26
|
private def self.parse_encryption_info: (untyped bytes) -> untyped
|
|
27
27
|
|
|
28
|
+
# Data integrity ([MS-OFFCRYPTO] 2.3.4.14): the HMAC of the whole EncryptedPackage
|
|
29
|
+
# stream. The HMAC key and value are encrypted with the package key, each with an
|
|
30
|
+
# IV derived from the keyData salt.
|
|
31
|
+
private def self.integrity_verified?: (untyped info, untyped cipher_name, untyped digest, untyped package_key, untyped encrypted_package_bytes) -> untyped
|
|
32
|
+
|
|
33
|
+
# xlsxrb 0.2.1 and earlier wrote an HMAC of the package before encryption, with the HMAC
|
|
34
|
+
# key and value encrypted under keys derived from the password. Files it wrote are
|
|
35
|
+
# still read; this HMAC also fails for a package that was changed.
|
|
36
|
+
private def self.legacy_integrity_verified?: (untyped info, untyped cipher_name, untyped digest, untyped h_final, untyped plain) -> untyped
|
|
37
|
+
|
|
28
38
|
private def self.digest_for: (untyped name) -> untyped
|
|
29
39
|
|
|
30
40
|
private def self.cipher_name_for: (untyped algo, untyped key_bits) -> untyped
|
|
@@ -15,6 +15,13 @@ module Xlsxrb
|
|
|
15
15
|
|
|
16
16
|
def self.fast_scan: (untyped xml_src) -> untyped
|
|
17
17
|
|
|
18
|
+
# Returns the index of the first +pattern+ at or after +from+, or the size of +xml+ when
|
|
19
|
+
# there is none. +cached+ is the result of the previous call for the same pattern with a
|
|
20
|
+
# smaller +from+; while it is not behind +from+ it is still the answer, so a caller that
|
|
21
|
+
# moves forward searches each stretch of +xml+ once.
|
|
22
|
+
# : (String xml, String pattern, Integer from, Integer? cached) -> Integer
|
|
23
|
+
def self.next_index: (String xml, String pattern, Integer from, Integer? cached) -> Integer
|
|
24
|
+
|
|
18
25
|
def self.extract_multi_t: (untyped xml, untyped from, untyped to, untyped t_start_pattern, untyped t_start_len, untyped t_end_tag, untyped t_end_len, untyped rph_start_pattern, untyped rph_end_tag, untyped rph_end_len, ?has_rph: untyped) -> untyped
|
|
19
26
|
|
|
20
27
|
def self.normalize_newlines: (untyped str) -> untyped
|
|
@@ -88,6 +88,14 @@ module Xlsxrb
|
|
|
88
88
|
|
|
89
89
|
ESCAPE_MAP: untyped
|
|
90
90
|
|
|
91
|
+
MEDIA_EXTENSION_RE: ::Regexp
|
|
92
|
+
|
|
93
|
+
# Returns the extension for a media part name. It becomes part of a ZIP entry name, a
|
|
94
|
+
# relationship target, and a content type, so anything but letters and digits is
|
|
95
|
+
# rejected.
|
|
96
|
+
# : (untyped ext) -> String
|
|
97
|
+
def media_extension: (untyped ext) -> String
|
|
98
|
+
|
|
91
99
|
def escape_xml: (untyped value) -> untyped
|
|
92
100
|
|
|
93
101
|
# Escapes cell text: encodes it as ST_Xstring (XmlBuilder.encode_xstring), so that
|
|
@@ -78,6 +78,20 @@ module Xlsxrb
|
|
|
78
78
|
# : (String raw) -> (Integer | Float)?
|
|
79
79
|
def self.numeric_cell_value: (String raw) -> (Integer | Float)?
|
|
80
80
|
|
|
81
|
+
# Excel's last row. A larger row number is not a worksheet row, and an unbounded one
|
|
82
|
+
# would be built digit by digit into an arbitrarily large Integer.
|
|
83
|
+
MAX_ROW_NUMBER: ::Integer
|
|
84
|
+
|
|
85
|
+
# Longer text is not read as a number. Excel writes numbers in at most 25 characters;
|
|
86
|
+
# converting millions of digits to an Integer takes time that grows faster than their
|
|
87
|
+
# number, and matching them takes memory in proportion.
|
|
88
|
+
MAX_NUMERIC_TEXT_BYTES: ::Integer
|
|
89
|
+
|
|
90
|
+
# Looks up a shared string by the index in a cell's <v>. An index that is negative or
|
|
91
|
+
# too long to be one reads as an empty string, as an index past the end does.
|
|
92
|
+
# : (Array[untyped] shared_strings, String raw_index) -> untyped
|
|
93
|
+
def self.shared_string_at: (Array[untyped] shared_strings, String raw_index) -> untyped
|
|
94
|
+
|
|
81
95
|
# SECURITY NOTE: Decodes only standard predefined XML entities in a single pass.
|
|
82
96
|
# This completely prevents "Billion Laughs" attacks (exponential entity expansion)
|
|
83
97
|
# because it avoids recursive expansion and ignores custom entities entirely.
|
|
@@ -105,6 +119,9 @@ module Xlsxrb
|
|
|
105
119
|
# rubocop:disable Style/OptionalBooleanParameter
|
|
106
120
|
def self.fast_scan_rows_direct: (untyped xml_src, untyped shared_strings, untyped _part_name, ?untyped styles, ?untyped date1904, ?untyped pad_empty_cells) ?{ (?) -> untyped } -> untyped
|
|
107
121
|
|
|
122
|
+
# Bytes of one row, or of markup between rows, held while streaming a worksheet.
|
|
123
|
+
MAX_STREAM_BUFFER_BYTES: untyped
|
|
124
|
+
|
|
108
125
|
# rubocop:disable Style/OptionalBooleanParameter
|
|
109
126
|
def self.scan_rows_stream: (untyped source, untyped shared_strings, untyped _part_name, ?untyped styles, ?untyped date1904, ?untyped pad_empty_cells) ?{ (?) -> untyped } -> untyped
|
|
110
127
|
|
|
@@ -125,6 +142,29 @@ module Xlsxrb
|
|
|
125
142
|
|
|
126
143
|
ATTR_S: ::Regexp
|
|
127
144
|
|
|
145
|
+
CELL_CLOSE_RE: ::Regexp
|
|
146
|
+
|
|
147
|
+
UNCLOSED_CELL_RE: ::Regexp
|
|
148
|
+
|
|
149
|
+
INLINE_STRING_OPEN_RE: ::Regexp
|
|
150
|
+
|
|
151
|
+
INLINE_STRING_CLOSE_RE: ::Regexp
|
|
152
|
+
|
|
153
|
+
# Raises ParseError for a row whose last <c> or <is> start tag has no end tag.
|
|
154
|
+
#
|
|
155
|
+
# SECURITY NOTE: CELL_FAST_RE looks ahead for </c> and </is>. For a start tag with no end
|
|
156
|
+
# tag after it, that search runs to the end of the row and fails, once per such tag, so
|
|
157
|
+
# a row of 40,000 unclosed <c> tags (a 3 KB file) took minutes. Every search made here
|
|
158
|
+
# is a single pass over the row.
|
|
159
|
+
# : (String chunk) -> void
|
|
160
|
+
def self.verify_cells_closed!: (String chunk) -> void
|
|
161
|
+
|
|
162
|
+
# Returns the bytes of a row with every sequence that is not UTF-8 replaced by U+FFFD,
|
|
163
|
+
# so that the text, formulas, and attributes cut from it are valid strings. A row that
|
|
164
|
+
# is valid, as nearly all are, is returned as is.
|
|
165
|
+
# : (String chunk) -> String
|
|
166
|
+
def self.scrub_row: (String chunk) -> String
|
|
167
|
+
|
|
128
168
|
def self.fast_parse_cells_direct: (untyped xml, untyped from, untyped to, untyped shared_strings, untyped row_source, ?untyped prefix) -> untyped
|
|
129
169
|
|
|
130
170
|
# rubocop:disable Style/OptionalBooleanParameter
|
|
@@ -686,6 +686,10 @@ module Xlsxrb
|
|
|
686
686
|
|
|
687
687
|
CHART_TYPE_MAP: untyped
|
|
688
688
|
|
|
689
|
+
# The elements of EG_ColorTransform in DrawingML. A transform's type is written as an
|
|
690
|
+
# element name, so only these are accepted.
|
|
691
|
+
COLOR_TRANSFORMS: untyped
|
|
692
|
+
|
|
689
693
|
NO_AXIS_CHARTS: untyped
|
|
690
694
|
|
|
691
695
|
THREE_AXIS_CHARTS: untyped
|
|
@@ -39,7 +39,11 @@ module Xlsxrb
|
|
|
39
39
|
|
|
40
40
|
UNESCAPE_RE: ::Regexp
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
INVALID_XML_CHARS_RE: ::Regexp
|
|
43
|
+
|
|
44
|
+
# Escapes XML special characters in a string, and removes characters that XML 1.0
|
|
45
|
+
# cannot hold, such as U+0001 and U+FFFE: a part that contains one is not well-formed.
|
|
46
|
+
# Binary strings are only escaped.
|
|
43
47
|
#
|
|
44
48
|
# @param value [Object]
|
|
45
49
|
# @return [String]
|
|
@@ -58,12 +62,13 @@ module Xlsxrb
|
|
|
58
62
|
# : (String value) -> String
|
|
59
63
|
def self.encode_xstring: (String value) -> String
|
|
60
64
|
|
|
61
|
-
|
|
65
|
+
ENTITY_RE: ::Regexp
|
|
62
66
|
|
|
63
67
|
XSTRING_RE: ::Regexp
|
|
64
68
|
|
|
65
69
|
# Unescapes predefined and numeric/hex XML entities, as well as
|
|
66
70
|
# ISO/IEC 29500-1 ST_Xstring (_xHHHH_) control character sequences.
|
|
71
|
+
# Each entity is decoded once: "&lt;" is the text "<", not "<".
|
|
67
72
|
#
|
|
68
73
|
# @param value [Object]
|
|
69
74
|
# @return [String]
|
|
@@ -14,8 +14,18 @@ module Xlsxrb
|
|
|
14
14
|
# : (String xml_string) -> REXML::Parsers::SAX2Parser
|
|
15
15
|
def self.sax2_parser: (String xml_string) -> REXML::Parsers::SAX2Parser
|
|
16
16
|
|
|
17
|
+
# Elements may nest this deep. SpreadsheetML, DrawingML, and VML parts stay far below
|
|
18
|
+
# it, and REXML's SAX2 parser takes time proportional to the depth for every tag, so
|
|
19
|
+
# that 20,000 nested elements (a 3 KB file) took 15 seconds.
|
|
20
|
+
MAX_ELEMENT_DEPTH: ::Integer
|
|
21
|
+
|
|
17
22
|
# SAX2 parser that wraps each listener in an AttributeDecoder.
|
|
18
23
|
class DecodingSAX2Parser < REXML::Parsers::SAX2Parser
|
|
24
|
+
# Parses the document, reporting malformed XML and exceeded entity expansion limits
|
|
25
|
+
# as Xlsxrb::ParseError.
|
|
26
|
+
# : () -> untyped
|
|
27
|
+
def parse: () -> untyped
|
|
28
|
+
|
|
19
29
|
# : (*untyped args) ?{ (*untyped) -> untyped } -> untyped
|
|
20
30
|
def listen: (*untyped args) ?{ (*untyped) -> untyped } -> untyped
|
|
21
31
|
end
|
|
@@ -82,6 +82,8 @@ module Xlsxrb
|
|
|
82
82
|
# : () -> Hash[String, String?]
|
|
83
83
|
def read_all: () -> Hash[String, String?]
|
|
84
84
|
|
|
85
|
+
XML_PART_SUFFIXES: untyped
|
|
86
|
+
|
|
85
87
|
# Returns raw bytes for a single entry, or nil if not found.
|
|
86
88
|
# : (String name) -> String?
|
|
87
89
|
def read_entry: (String name) -> String?
|
|
@@ -134,10 +136,19 @@ module Xlsxrb
|
|
|
134
136
|
|
|
135
137
|
def parse_catalog_from_local_headers: (untyped io) -> untyped
|
|
136
138
|
|
|
137
|
-
|
|
139
|
+
# Finds where a deflate stream of unknown length ends by inflating it, without keeping
|
|
140
|
+
# the output, and consumes the data descriptor that follows.
|
|
141
|
+
# Returns the compressed size, the uncompressed size, and the descriptor's CRC-32.
|
|
142
|
+
# : (untyped io, Integer method, ?zip64: bool) -> [Integer, Integer, Integer?]
|
|
143
|
+
def scan_data_descriptor_entry: (untyped io, Integer method, ?zip64: bool) -> [ Integer, Integer, Integer? ]
|
|
138
144
|
|
|
139
145
|
def decompress: (untyped raw, untyped method) -> untyped
|
|
140
146
|
|
|
147
|
+
# Raises ZipError when the entry's data would lie outside the archive, so that a
|
|
148
|
+
# forged size is never passed to IO#read as a length to allocate.
|
|
149
|
+
# : (Entry entry) -> void
|
|
150
|
+
def verify_entry_bounds!: (Entry entry) -> void
|
|
151
|
+
|
|
141
152
|
def safe_inflate: (untyped raw, untyped wbits) -> untyped
|
|
142
153
|
|
|
143
154
|
def normalize_entry_name: (untyped name) -> untyped
|
|
@@ -52,6 +52,14 @@ module Xlsxrb
|
|
|
52
52
|
|
|
53
53
|
private
|
|
54
54
|
|
|
55
|
+
UNSAFE_ENTRY_NAME_RE: ::Regexp
|
|
56
|
+
|
|
57
|
+
# Rejects names that a program extracting the archive could write outside its target
|
|
58
|
+
# directory (absolute paths, ".." segments, backslashes) and names with control
|
|
59
|
+
# characters.
|
|
60
|
+
# : (String path) -> void
|
|
61
|
+
def validate_entry_name!: (String path) -> void
|
|
62
|
+
|
|
55
63
|
def determine_seekable: () -> untyped
|
|
56
64
|
|
|
57
65
|
# : (Integer value) -> bool
|
|
@@ -645,6 +645,15 @@ module Xlsxrb
|
|
|
645
645
|
# : () -> String
|
|
646
646
|
def raw_sheet_xml: () -> String
|
|
647
647
|
|
|
648
|
+
DIMENSION_SCAN_LIMIT: untyped
|
|
649
|
+
|
|
650
|
+
DIMENSION_RE: ::Regexp
|
|
651
|
+
|
|
652
|
+
# Returns the ref of the <dimension> element in +xml+, or nil. Text that is not valid
|
|
653
|
+
# UTF-8 is searched as bytes.
|
|
654
|
+
# : (String xml) -> String?
|
|
655
|
+
def dimension_ref_in: (String xml) -> String?
|
|
656
|
+
|
|
648
657
|
# Extracts dimension reference from XML source lazily without parsing the entire file.
|
|
649
658
|
# : () -> String?
|
|
650
659
|
def extract_dimension: () -> String?
|
|
@@ -877,7 +877,9 @@ module Xlsxrb
|
|
|
877
877
|
|
|
878
878
|
# Sets the worksheet background image (<picture>).
|
|
879
879
|
#
|
|
880
|
-
# @param val [String, Hash, Elements::Image, nil] Image binary data,
|
|
880
|
+
# @param val [String, Hash, Elements::Image, nil] Image binary data, or a Hash with +:data+ and
|
|
881
|
+
# +:ext+. A String is always the image itself: to use a file, pass its bytes, as in
|
|
882
|
+
# +{ data: File.binread(path), ext: "jpg" }+.
|
|
881
883
|
# @return [untyped]
|
|
882
884
|
# @api public
|
|
883
885
|
# : (untyped val) -> untyped
|
|
@@ -1593,7 +1595,9 @@ module Xlsxrb
|
|
|
1593
1595
|
|
|
1594
1596
|
# Sets the worksheet background image (<picture>).
|
|
1595
1597
|
#
|
|
1596
|
-
# @param val [String, Hash, Elements::Image, nil] Image binary data,
|
|
1598
|
+
# @param val [String, Hash, Elements::Image, nil] Image binary data, or a Hash with +:data+ and
|
|
1599
|
+
# +:ext+. A String is always the image itself: to use a file, pass its bytes, as in
|
|
1600
|
+
# +{ data: File.binread(path), ext: "jpg" }+.
|
|
1597
1601
|
# @return [untyped]
|
|
1598
1602
|
# @api public
|
|
1599
1603
|
# : (untyped val) -> untyped
|
|
@@ -681,7 +681,9 @@ module Xlsxrb
|
|
|
681
681
|
|
|
682
682
|
# Sets the worksheet background image (<picture>).
|
|
683
683
|
#
|
|
684
|
-
# @param val [String, Hash, Elements::Image, nil] Image binary data,
|
|
684
|
+
# @param val [String, Hash, Elements::Image, nil] Image binary data, or a Hash with +:data+ and
|
|
685
|
+
# +:ext+. A String is always the image itself: to use a file, pass its bytes, as in
|
|
686
|
+
# +{ data: File.binread(path), ext: "jpg" }+.
|
|
685
687
|
# @return [untyped]
|
|
686
688
|
# @api public
|
|
687
689
|
# : (untyped val) -> untyped
|
data/sig/generated/xlsxrb.rbs
CHANGED
|
@@ -419,6 +419,26 @@ module Xlsxrb
|
|
|
419
419
|
# : (untyped name, ?replace_char: String) -> String
|
|
420
420
|
def self.sanitize_sheet_name: (untyped name, ?replace_char: String) -> String
|
|
421
421
|
|
|
422
|
+
# Cuts text to what a cell holds: 32,767 characters counted in UTF-16 code units, as
|
|
423
|
+
# Excel counts them, so a character outside the Basic Multilingual Plane, such as an emoji,
|
|
424
|
+
# counts as two. A character is never split. Writing longer text raises ArgumentError
|
|
425
|
+
# with +strict_excel_mode+ (the default); call this first to keep the start of the text
|
|
426
|
+
# instead.
|
|
427
|
+
#
|
|
428
|
+
# Text of 32,767 bytes or fewer always fits, so it is returned as is after its byte size
|
|
429
|
+
# is checked, without counting its characters. Longer text is returned as a new UTF-8
|
|
430
|
+
# String, even if it fits.
|
|
431
|
+
#
|
|
432
|
+
# @example
|
|
433
|
+
# Xlsxrb.truncate_cell_text("x" * 40_000).length #=> 32767
|
|
434
|
+
# Xlsxrb.truncate_cell_text("😀" * 20_000).length #=> 16383
|
|
435
|
+
#
|
|
436
|
+
# @param text [String]
|
|
437
|
+
# @return [String]
|
|
438
|
+
# @api public
|
|
439
|
+
# : (String text) -> String
|
|
440
|
+
def self.truncate_cell_text: (String text) -> String
|
|
441
|
+
|
|
422
442
|
# Reads an XLSX file (streaming and lazy-loaded by default) from a file path, IO stream, or binary String.
|
|
423
443
|
#
|
|
424
444
|
# Sheets and rows are streamed lazily with O(1) constant memory. If a block is given,
|
|
@@ -473,30 +493,36 @@ module Xlsxrb
|
|
|
473
493
|
|
|
474
494
|
# Writes an XLSX file or IO stream (streaming or in-memory), or returns a binary string.
|
|
475
495
|
#
|
|
476
|
-
# @overload write(?target, password: nil, encryption_mode:
|
|
496
|
+
# @overload write(?target, password: nil, encryption_mode: nil, strict_excel_mode: true, &block)
|
|
477
497
|
# Streaming write: yields a {StreamWriter} context for high-speed, zero-allocation XLSX generation.
|
|
478
498
|
# If target is omitted, returns the generated XLSX as an in-memory binary String.
|
|
479
499
|
# @param target [String, IO, StringIO, nil] Optional destination file path or writable IO object.
|
|
480
500
|
# @param password [String, nil] Optional password to encrypt the generated XLSX file.
|
|
481
|
-
# @param encryption_mode [Symbol] Encryption
|
|
501
|
+
# @param encryption_mode [Symbol, nil] Encryption scheme used with a password: `:agile` (AES-256, with a check
|
|
502
|
+
# that the file is unchanged) or `:standard` (AES-128; deprecated, and an error from 0.3.0). When omitted,
|
|
503
|
+
# Standard is written in 0.2 and Agile from 0.3.0. See docs/adr/0013-write-agile-encryption-read-both.md.
|
|
482
504
|
# @param strict_excel_mode [Boolean] Whether to enforce Microsoft Excel specification limits.
|
|
483
505
|
# @yield [stream_writer]
|
|
484
506
|
# @yieldparam stream_writer [Xlsxrb::StreamWriter]
|
|
485
507
|
# @return [String, void] Binary data if target is omitted, or void if written to target.
|
|
486
508
|
#
|
|
487
|
-
# @overload write(workbook, password: nil, encryption_mode:
|
|
509
|
+
# @overload write(workbook, password: nil, encryption_mode: nil)
|
|
488
510
|
# In-memory write: exports the workbook to an in-memory binary String.
|
|
489
511
|
# @param workbook [Elements::Workbook] The workbook to write.
|
|
490
512
|
# @param password [String, nil] Optional password to encrypt the binary string.
|
|
491
|
-
# @param encryption_mode [Symbol] Encryption
|
|
513
|
+
# @param encryption_mode [Symbol, nil] Encryption scheme used with a password: `:agile` (AES-256, with a check
|
|
514
|
+
# that the file is unchanged) or `:standard` (AES-128; deprecated, and an error from 0.3.0). When omitted,
|
|
515
|
+
# Standard is written in 0.2 and Agile from 0.3.0. See docs/adr/0013-write-agile-encryption-read-both.md.
|
|
492
516
|
# @return [String] Binary data representing the XLSX file.
|
|
493
517
|
#
|
|
494
|
-
# @overload write(target, workbook, password: nil, encryption_mode:
|
|
518
|
+
# @overload write(target, workbook, password: nil, encryption_mode: nil)
|
|
495
519
|
# In-memory write: writes the workbook to a file path or IO stream.
|
|
496
520
|
# @param target [String, IO, StringIO] Destination file path or writable IO object.
|
|
497
521
|
# @param workbook [Elements::Workbook] The workbook to write.
|
|
498
522
|
# @param password [String, nil] Optional password to encrypt the output file.
|
|
499
|
-
# @param encryption_mode [Symbol] Encryption
|
|
523
|
+
# @param encryption_mode [Symbol, nil] Encryption scheme used with a password: `:agile` (AES-256, with a check
|
|
524
|
+
# that the file is unchanged) or `:standard` (AES-128; deprecated, and an error from 0.3.0). When omitted,
|
|
525
|
+
# Standard is written in 0.2 and Agile from 0.3.0. See docs/adr/0013-write-agile-encryption-read-both.md.
|
|
500
526
|
# @return [void]
|
|
501
527
|
#
|
|
502
528
|
# @example Streaming write to file
|
|
@@ -525,8 +551,8 @@ module Xlsxrb
|
|
|
525
551
|
# @param compression_level [Integer, nil] ZIP deflate compression level (0..9, or Zlib::DEFAULT_COMPRESSION
|
|
526
552
|
# for zlib's level 6). Defaults to {DEFAULT_COMPRESSION_LEVEL}. Higher levels write smaller files more slowly;
|
|
527
553
|
# level 0 stores entries without compression. See docs/adr/0009-default-zip-compression-level.md.
|
|
528
|
-
# : (?untyped target_or_workbook, ?Elements::Workbook | untyped workbook_or_nil, ?password: String?, ?encryption_mode: Symbol
|
|
529
|
-
def self.write: (?untyped target_or_workbook, ?Elements::Workbook | untyped workbook_or_nil, ?password: String?, ?encryption_mode: Symbol
|
|
554
|
+
# : (?untyped target_or_workbook, ?Elements::Workbook | untyped workbook_or_nil, ?password: String?, ?encryption_mode: Symbol?, ?strict_excel_mode: bool, ?default_font: untyped?, ?shared_strings: bool, ?compression_level: Integer | nil) ?{ (StreamWriter) -> void } -> untyped
|
|
555
|
+
def self.write: (?untyped target_or_workbook, ?Elements::Workbook | untyped workbook_or_nil, ?password: String?, ?encryption_mode: Symbol?, ?strict_excel_mode: bool, ?default_font: untyped?, ?shared_strings: bool, ?compression_level: Integer | nil) ?{ (StreamWriter) -> void } -> untyped
|
|
530
556
|
|
|
531
557
|
# Builds an in-memory {Elements::Workbook} using a declarative DSL.
|
|
532
558
|
#
|
|
@@ -622,6 +648,10 @@ module Xlsxrb
|
|
|
622
648
|
# : (untyped raw_row, ?Hash[String, untyped] hyperlinks, ?(Array[untyped] | Hash[String, untyped]) comments, ?Hash[untyped, untyped]? styles, ?date1904: bool, ?threaded_comments: (Array[untyped] | Hash[String, untyped])) -> (Elements::Row | untyped)
|
|
623
649
|
private def self.build_row_from_raw: (untyped raw_row, ?Hash[String, untyped] hyperlinks, ?Array[untyped] | Hash[String, untyped] comments, ?Hash[untyped, untyped]? styles, ?date1904: bool, ?threaded_comments: Array[untyped] | Hash[String, untyped]) -> (Elements::Row | untyped)
|
|
624
650
|
|
|
651
|
+
# The scheme to encrypt with. Standard stays the default in 0.2; asking for it by name warns.
|
|
652
|
+
# : (Symbol? encryption_mode) -> Symbol
|
|
653
|
+
private def self.write_encryption_mode: (Symbol? encryption_mode) -> Symbol
|
|
654
|
+
|
|
625
655
|
# : (untyped source, String? password) -> untyped
|
|
626
656
|
private def self.prepare_source_io: (untyped source, String? password) -> untyped
|
|
627
657
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: xlsxrb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- niku
|
|
@@ -29,14 +29,20 @@ dependencies:
|
|
|
29
29
|
requirements:
|
|
30
30
|
- - "~>"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '3.
|
|
32
|
+
version: '3.4'
|
|
33
|
+
- - ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: 3.4.2
|
|
33
36
|
type: :runtime
|
|
34
37
|
prerelease: false
|
|
35
38
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
39
|
requirements:
|
|
37
40
|
- - "~>"
|
|
38
41
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: '3.
|
|
42
|
+
version: '3.4'
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 3.4.2
|
|
40
46
|
description: A streaming-capable, low-memory XLSX reading and writing library for
|
|
41
47
|
Ruby, relying solely on standard libraries and bundled gems.
|
|
42
48
|
email:
|