xlsxrb 0.2.0 → 0.2.1
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 +37 -0
- data/README.md +1 -1
- data/lib/xlsxrb/dsl_helpers.rb +49 -1
- data/lib/xlsxrb/elements/worksheet.rb +2 -2
- data/lib/xlsxrb/ooxml/utils.rb +13 -0
- data/lib/xlsxrb/ooxml/workbook_writer.rb +13 -2
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +302 -228
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +11 -4
- data/lib/xlsxrb/ooxml/writer.rb +1 -1
- data/lib/xlsxrb/ooxml/xml_builder.rb +18 -0
- data/lib/xlsxrb/stream_row.rb +2 -2
- data/lib/xlsxrb/stream_writer.rb +203 -80
- data/lib/xlsxrb/style_builder.rb +339 -0
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +239 -88
- data/lib/xlsxrb/worksheet_builder.rb +841 -134
- data/lib/xlsxrb.rb +19 -0
- data/sig/generated/xlsxrb/dsl_helpers.rbs +28 -0
- data/sig/generated/xlsxrb/ooxml/utils.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +5 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +53 -0
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +3 -0
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +12 -0
- data/sig/generated/xlsxrb/stream_row.rbs +2 -2
- data/sig/generated/xlsxrb/stream_writer.rbs +40 -4
- data/sig/generated/xlsxrb/style_builder.rbs +261 -0
- data/sig/generated/xlsxrb/workbook_builder.rbs +42 -9
- data/sig/generated/xlsxrb/worksheet_builder.rbs +615 -3
- data/sig/generated/xlsxrb.rbs +15 -0
- metadata +1 -1
|
@@ -27,8 +27,10 @@ module Xlsxrb
|
|
|
27
27
|
def initialize(strict_excel_mode: true, default_font: nil)
|
|
28
28
|
@strict_excel_mode = strict_excel_mode
|
|
29
29
|
@default_font = default_font
|
|
30
|
-
@sheets = []
|
|
30
|
+
@sheets = [] # Each sheet without its rows, for its name and state
|
|
31
|
+
@sheet_snapshots = [] # Each sheet as its block left it, rows still pending
|
|
31
32
|
@sheet_builders = [] # Keep track of sheet builders for style processing
|
|
33
|
+
@style_log = StyleLog.new # When each style is defined, for the rows that use it
|
|
32
34
|
@defined_names = []
|
|
33
35
|
@styles = {}
|
|
34
36
|
@core_properties = {}
|
|
@@ -75,6 +77,8 @@ module Xlsxrb
|
|
|
75
77
|
# @yield [style_builder]
|
|
76
78
|
# @yieldparam style_builder [Xlsxrb::StyleBuilder]
|
|
77
79
|
# @return [StyleBuilder]
|
|
80
|
+
# Using the returned value is deprecated and warns; in version 0.3.0, this method
|
|
81
|
+
# will return nil. Configure the style with options or in the block.
|
|
78
82
|
# @api public
|
|
79
83
|
#: (String | Symbol name, **untyped opts) ?{ (StyleBuilder) -> void } -> StyleBuilder
|
|
80
84
|
def style(name, **opts)
|
|
@@ -83,6 +87,8 @@ module Xlsxrb
|
|
|
83
87
|
style_builder.apply_options!(**opts) unless opts.empty?
|
|
84
88
|
yield style_builder if block_given?
|
|
85
89
|
@styles[style_name] = style_builder
|
|
90
|
+
@style_log.record(style_name, style_builder)
|
|
91
|
+
ReturnedStyleBuilder.new(style_builder)
|
|
86
92
|
end
|
|
87
93
|
|
|
88
94
|
# Creates an {Elements::Formula} object.
|
|
@@ -252,14 +258,15 @@ module Xlsxrb
|
|
|
252
258
|
# @param opts [Hash] Additional sheet properties.
|
|
253
259
|
# @yield [sheet_builder]
|
|
254
260
|
# @yieldparam sheet_builder [Xlsxrb::WorksheetBuilder]
|
|
255
|
-
# @return [
|
|
261
|
+
# @return [DeferredWorksheet] The sheet as {WorksheetBuilder#build} returns it, with
|
|
262
|
+
# style names in its cells. It is built when first used.
|
|
256
263
|
# @raise [ArgumentError] If sheet name violates Excel limits in strict mode.
|
|
257
264
|
# @api public
|
|
258
|
-
#: (?String? name, ?state: Symbol | String | nil, ?tab_color: String | Hash[Symbol, untyped] | Symbol | nil, ?code_name: String | Symbol | nil, **untyped opts) ?{ (WorksheetBuilder) -> void } ->
|
|
265
|
+
#: (?String? name, ?state: Symbol | String | nil, ?tab_color: String | Hash[Symbol, untyped] | Symbol | nil, ?code_name: String | Symbol | nil, **untyped opts) ?{ (WorksheetBuilder) -> void } -> DeferredWorksheet
|
|
259
266
|
def sheet(name = nil, state: nil, tab_color: nil, code_name: nil, **opts)
|
|
260
267
|
sheet_count = [@sheets.size, @sheet_builders.size].max
|
|
261
268
|
name ||= "Sheet#{sheet_count + 1}"
|
|
262
|
-
raise ArgumentError, "Sheet name '#{name}' must be <= 31 characters (Excel limitation)" if @strict_excel_mode && name
|
|
269
|
+
raise ArgumentError, "Sheet name '#{name}' must be <= 31 characters, counted in UTF-16 code units as Excel does (Excel limitation)" if @strict_excel_mode && Ooxml::Utils.utf16_length(name) > 31
|
|
263
270
|
raise ArgumentError, "Sheet name '#{name}' contains invalid characters (Excel limitation)" if name.match?(Elements::Worksheet.forbidden_name_chars)
|
|
264
271
|
raise ArgumentError, "Sheet name '#{name}' cannot start or end with an apostrophe (Excel limitation)" if @strict_excel_mode && (name.start_with?("'") || name.end_with?("'"))
|
|
265
272
|
raise ArgumentError, "Sheet name '#{name}' is reserved by Excel (Excel limitation)" if @strict_excel_mode && name.casecmp?("History")
|
|
@@ -279,7 +286,7 @@ module Xlsxrb
|
|
|
279
286
|
|
|
280
287
|
@active_sheet = name if active
|
|
281
288
|
|
|
282
|
-
sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state)
|
|
289
|
+
sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state, style_log: @style_log)
|
|
283
290
|
sheet_builder.code_name = code_name if code_name
|
|
284
291
|
sheet_builder.tab_color = tab_color if tab_color
|
|
285
292
|
sheet_builder.sheet_properties(**opts) unless opts.empty?
|
|
@@ -287,22 +294,39 @@ module Xlsxrb
|
|
|
287
294
|
yield sheet_builder if block_given?
|
|
288
295
|
@defined_names.concat(sheet_builder.defined_names)
|
|
289
296
|
sheet_builder.defined_names.clear
|
|
290
|
-
|
|
291
|
-
@
|
|
292
|
-
|
|
297
|
+
snapshot = sheet_builder.close
|
|
298
|
+
@sheet_snapshots << snapshot
|
|
299
|
+
@sheets << snapshot.frame
|
|
300
|
+
DeferredWorksheet.new(snapshot)
|
|
293
301
|
end
|
|
294
302
|
|
|
295
|
-
#
|
|
303
|
+
# Adds a worksheet, as {#sheet} does.
|
|
304
|
+
#
|
|
305
|
+
# @deprecated Use {#sheet} to add a sheet. In version 0.3.0, this method will return the
|
|
306
|
+
# {WorksheetBuilder} of an existing sheet, or of the current sheet without an argument,
|
|
307
|
+
# and raise for a sheet that does not exist. Calling it with an argument, or without
|
|
308
|
+
# one after a sheet was added, warns.
|
|
296
309
|
#
|
|
297
310
|
# @param name_or_index [String, Symbol, Integer, nil]
|
|
298
311
|
# @yield [sheet_builder]
|
|
299
|
-
# @return [
|
|
312
|
+
# @return [DeferredWorksheet]
|
|
300
313
|
# @api public
|
|
301
|
-
#: (?String | Symbol | Integer | nil name_or_index) ?{ (WorksheetBuilder) -> void } ->
|
|
314
|
+
#: (?String | Symbol | Integer | nil name_or_index) ?{ (WorksheetBuilder) -> void } -> DeferredWorksheet
|
|
302
315
|
def [](name_or_index = nil, &)
|
|
316
|
+
if !name_or_index.nil?
|
|
317
|
+
Deprecation.warn(INDEX_CREATES_SHEET_MESSAGE)
|
|
318
|
+
elsif !@sheet_builders.empty?
|
|
319
|
+
Deprecation.warn(INDEX_ADDS_SHEET_MESSAGE)
|
|
320
|
+
end
|
|
303
321
|
sheet(name_or_index&.to_s, &)
|
|
304
322
|
end
|
|
305
323
|
|
|
324
|
+
INDEX_CREATES_SHEET_MESSAGE = "Creating a sheet with WorkbookBuilder#[] is deprecated; use sheet. In version 0.3.0, " \
|
|
325
|
+
"#[] will return the WorksheetBuilder of an existing sheet and raise for a sheet that does not exist."
|
|
326
|
+
INDEX_ADDS_SHEET_MESSAGE = "WorkbookBuilder#[] without an argument adds a sheet; in version 0.3.0 it will return " \
|
|
327
|
+
"the current sheet instead. Use sheet to add a sheet."
|
|
328
|
+
private_constant :INDEX_CREATES_SHEET_MESSAGE, :INDEX_ADDS_SHEET_MESSAGE
|
|
329
|
+
|
|
306
330
|
# --- Workbook-Level Methods ---
|
|
307
331
|
|
|
308
332
|
# Adds a defined name (named range or formula) to the workbook.
|
|
@@ -604,7 +628,7 @@ module Xlsxrb
|
|
|
604
628
|
end
|
|
605
629
|
|
|
606
630
|
# Process styles from all sheets and collect style definitions
|
|
607
|
-
processed_sheets, styles_definition = process_styles(@
|
|
631
|
+
processed_sheets, styles_definition = process_styles(@sheet_snapshots)
|
|
608
632
|
|
|
609
633
|
# Auto-register print_area and print_titles defined names from sheet builders if not explicitly defined
|
|
610
634
|
@sheet_builders.each do |sb|
|
|
@@ -700,7 +724,9 @@ module Xlsxrb
|
|
|
700
724
|
end
|
|
701
725
|
end
|
|
702
726
|
|
|
703
|
-
|
|
727
|
+
# Registers the styles of every sheet and builds the sheets' rows with their style
|
|
728
|
+
# names resolved to style indexes. Each cell is created once, here.
|
|
729
|
+
#: (Array[WorksheetBuilder::Snapshot] sheets) -> [Array[Elements::Worksheet], Hash[Symbol, untyped]]
|
|
704
730
|
def process_styles(sheets)
|
|
705
731
|
# Collect all unique StyleBuilders from workbook and sheets
|
|
706
732
|
all_style_builders = @styles.dup
|
|
@@ -712,7 +738,7 @@ module Xlsxrb
|
|
|
712
738
|
|
|
713
739
|
# Also collect inline styles from columns and rows
|
|
714
740
|
sheets.each do |sheet|
|
|
715
|
-
sheet.columns.each do |col|
|
|
741
|
+
sheet.frame.columns.each do |col|
|
|
716
742
|
raw_col_style = col.style_index
|
|
717
743
|
next unless raw_col_style.is_a?(Hash)
|
|
718
744
|
|
|
@@ -725,7 +751,7 @@ module Xlsxrb
|
|
|
725
751
|
end
|
|
726
752
|
|
|
727
753
|
sheet.rows.each do |row|
|
|
728
|
-
raw_row_style = row.
|
|
754
|
+
raw_row_style = row.style
|
|
729
755
|
next unless raw_row_style.is_a?(Hash)
|
|
730
756
|
|
|
731
757
|
inline_name = "__inline_row_#{raw_row_style.hash}"
|
|
@@ -735,40 +761,87 @@ module Xlsxrb
|
|
|
735
761
|
sb.apply_options!(**raw_row_style)
|
|
736
762
|
all_style_builders[inline_name] = sb
|
|
737
763
|
end
|
|
764
|
+
end
|
|
738
765
|
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
766
|
+
# A name defined more than once with different formats stands for the definition
|
|
767
|
+
# made last before each row was added, or for the first one if the row came before
|
|
768
|
+
# all of them, as for rows written by StreamWriter. The definition the name holds
|
|
769
|
+
# in all_style_builders keeps the name; each other one gets a key of its own, and
|
|
770
|
+
# is registered after the other styles if a row, a column, or a cell uses it.
|
|
771
|
+
versions = {} #: Hash[String, Array[[Integer, String]]]
|
|
772
|
+
version_builders = {} #: Hash[String, StyleBuilder]
|
|
773
|
+
@style_log.definitions.each do |name, definitions|
|
|
774
|
+
current = all_style_builders[name]
|
|
775
|
+
next if current.nil? || definitions.all? { |(_, builder)| builder.equal?(current) || builder.same_format?(current) }
|
|
776
|
+
|
|
777
|
+
versions[name] = definitions.map do |defined_at, builder|
|
|
778
|
+
next [defined_at, name] if builder.equal?(current)
|
|
779
|
+
|
|
780
|
+
key = "#{name}\u0000#{defined_at}"
|
|
781
|
+
version_builders[key] = builder
|
|
782
|
+
[defined_at, key]
|
|
744
783
|
end
|
|
745
784
|
end
|
|
785
|
+
used_versions = {} #: Hash[String, true]
|
|
746
786
|
|
|
747
|
-
#
|
|
748
|
-
#
|
|
787
|
+
# One pass over the cells checks their style names (names that were never defined
|
|
788
|
+
# are a mistake, not an unstyled cell) and finds the Date and Time cells styled
|
|
789
|
+
# without a number format. Those get a variant of their style that keeps the
|
|
790
|
+
# default date or time format. The variants are registered after every other
|
|
791
|
+
# style, in the order they are found.
|
|
749
792
|
date_variants = {}
|
|
793
|
+
variant_builders = {}
|
|
794
|
+
checked_style = style_key = nil
|
|
750
795
|
sheets.each do |sheet|
|
|
796
|
+
sheet.frame.columns.each do |col|
|
|
797
|
+
DslHelpers.check_style_name!(col.style_index, all_style_builders)
|
|
798
|
+
use_style_version(col.style_index, sheet.column_style_clocks[col.index], versions, used_versions) unless versions.empty?
|
|
799
|
+
end
|
|
751
800
|
sheet.rows.each do |row|
|
|
752
|
-
row.
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
801
|
+
DslHelpers.check_style_name!(row.style, all_style_builders)
|
|
802
|
+
use_style_version(row.style, row.clock, versions, used_versions) unless versions.empty?
|
|
803
|
+
styles = row.styles
|
|
804
|
+
next if styles.nil?
|
|
805
|
+
|
|
806
|
+
values = row.values
|
|
807
|
+
if styles.is_a?(Array)
|
|
808
|
+
col_index = 0
|
|
809
|
+
while col_index < styles.size
|
|
810
|
+
style = styles[col_index]
|
|
811
|
+
unless style.nil?
|
|
812
|
+
# Rows usually repeat one style object; check it once.
|
|
813
|
+
unless style.equal?(checked_style)
|
|
814
|
+
# Symbol#name looks a Symbol up without allocating a String.
|
|
815
|
+
style_key = style.is_a?(Symbol) ? style.name : style
|
|
816
|
+
DslHelpers.check_style_name!(style, all_style_builders) unless all_style_builders.key?(style_key)
|
|
817
|
+
checked_style = style
|
|
818
|
+
end
|
|
819
|
+
use_style_version(style_key, row.clock, versions, used_versions) unless versions.empty?
|
|
820
|
+
add_date_variant(values[col_index], style_key, row.clock, versions, all_style_builders, version_builders, date_variants, variant_builders) if style_key.is_a?(String)
|
|
821
|
+
end
|
|
822
|
+
col_index += 1
|
|
823
|
+
end
|
|
824
|
+
elsif row.width.positive?
|
|
825
|
+
unless styles.equal?(checked_style)
|
|
826
|
+
style_key = styles.is_a?(Symbol) ? styles.name : styles
|
|
827
|
+
DslHelpers.check_style_name!(styles, all_style_builders) unless all_style_builders.key?(style_key)
|
|
828
|
+
checked_style = styles
|
|
829
|
+
end
|
|
830
|
+
use_style_version(style_key, row.clock, versions, used_versions) unless versions.empty?
|
|
831
|
+
values.each { |value| add_date_variant(value, style_key, row.clock, versions, all_style_builders, version_builders, date_variants, variant_builders) } if style_key.is_a?(String)
|
|
762
832
|
end
|
|
763
833
|
end
|
|
764
834
|
end
|
|
835
|
+
version_builders.each { |key, builder| all_style_builders[key] ||= builder if used_versions.key?(key) }
|
|
836
|
+
variant_builders.each { |name, builder| all_style_builders[name] ||= builder }
|
|
765
837
|
|
|
766
838
|
if all_style_builders.empty?
|
|
839
|
+
named_sheets = sheets.map(&:worksheet)
|
|
767
840
|
if @default_font
|
|
768
841
|
temp_writer = Ooxml::Writer.new(default_font: @default_font)
|
|
769
|
-
return [
|
|
842
|
+
return [named_sheets, extract_styles_from_writer(temp_writer)]
|
|
770
843
|
end
|
|
771
|
-
return [
|
|
844
|
+
return [named_sheets, {}]
|
|
772
845
|
end
|
|
773
846
|
|
|
774
847
|
# Create a temporary writer to register styles and get numeric IDs
|
|
@@ -782,12 +855,12 @@ module Xlsxrb
|
|
|
782
855
|
# Capture the style definitions from the temporary writer
|
|
783
856
|
styles_definition = extract_styles_from_writer(temp_writer)
|
|
784
857
|
|
|
785
|
-
#
|
|
858
|
+
# Build all sheets with resolved style IDs and style inheritance
|
|
786
859
|
updated_sheets = sheets.map do |sheet|
|
|
787
860
|
col_style_map = {}
|
|
788
861
|
col_fmt_map = {}
|
|
789
862
|
|
|
790
|
-
new_columns = sheet.columns.map do |col|
|
|
863
|
+
new_columns = sheet.frame.columns.map do |col|
|
|
791
864
|
raw_col_style = col.style_index
|
|
792
865
|
resolved_col_style = if raw_col_style.is_a?(Hash)
|
|
793
866
|
inline_name = "__inline_col_#{raw_col_style.hash}"
|
|
@@ -797,11 +870,12 @@ module Xlsxrb
|
|
|
797
870
|
end
|
|
798
871
|
style_name_to_id[inline_name]
|
|
799
872
|
elsif (raw_col_style.is_a?(String) || raw_col_style.is_a?(Symbol)) && style_name_to_id.key?(raw_col_style.to_s)
|
|
800
|
-
|
|
873
|
+
col_key = style_version(raw_col_style.to_s, sheet.column_style_clocks[col.index], versions)
|
|
874
|
+
sb = all_style_builders[col_key]
|
|
801
875
|
if sb&.number_format
|
|
802
876
|
col_fmt_map[col.index] = sb.number_format.is_a?(String) ? sb.number_format : Ooxml::Utils::BUILTIN_NUM_FMT_CODES[sb.number_format]
|
|
803
877
|
end
|
|
804
|
-
style_name_to_id[
|
|
878
|
+
style_name_to_id[col_key]
|
|
805
879
|
else
|
|
806
880
|
raw_col_style
|
|
807
881
|
end
|
|
@@ -828,77 +902,154 @@ module Xlsxrb
|
|
|
828
902
|
end
|
|
829
903
|
end
|
|
830
904
|
|
|
905
|
+
last_raw_style = last_resolved_style = nil
|
|
831
906
|
new_rows = sheet.rows.map do |row|
|
|
832
|
-
|
|
907
|
+
row_clock = row.clock
|
|
908
|
+
# A name may stand for another definition in this row, so resolve it again.
|
|
909
|
+
last_raw_style = nil unless versions.empty?
|
|
910
|
+
raw_row_style = row.style
|
|
833
911
|
resolved_row_style = if raw_row_style.is_a?(Hash)
|
|
834
912
|
inline_name = "__inline_row_#{raw_row_style.hash}"
|
|
835
913
|
style_name_to_id[inline_name]
|
|
836
914
|
elsif (raw_row_style.is_a?(String) || raw_row_style.is_a?(Symbol)) && style_name_to_id.key?(raw_row_style.to_s)
|
|
837
|
-
style_name_to_id[raw_row_style.to_s]
|
|
915
|
+
style_name_to_id[style_version(raw_row_style.to_s, row_clock, versions)]
|
|
838
916
|
elsif raw_row_style.is_a?(Integer)
|
|
839
917
|
raw_row_style
|
|
840
918
|
end
|
|
841
|
-
row_unmapped = row.unmapped_data
|
|
842
|
-
if row_unmapped&.key?(:style_index)
|
|
843
|
-
row_unmapped = row_unmapped.dup
|
|
844
|
-
row_unmapped.delete(:style_index)
|
|
845
|
-
end
|
|
846
919
|
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
920
|
+
row_index = row.index
|
|
921
|
+
values = row.values
|
|
922
|
+
styles = row.styles
|
|
923
|
+
codes = row.format_codes
|
|
924
|
+
style_lookup = styles.is_a?(Array)
|
|
925
|
+
code_lookup = codes.is_a?(Array)
|
|
926
|
+
all_valid = row.within_limits?
|
|
927
|
+
width = row.width
|
|
928
|
+
cells = [] #: Array[Elements::Cell]
|
|
929
|
+
col_index = 0
|
|
930
|
+
while col_index < width
|
|
931
|
+
value = col_index < values.size ? values[col_index] : nil
|
|
932
|
+
style = style_lookup ? styles[col_index] : styles
|
|
933
|
+
if value.nil? && style.nil?
|
|
934
|
+
col_index += 1
|
|
935
|
+
next
|
|
936
|
+
end
|
|
937
|
+
|
|
938
|
+
format_code = code_lookup ? codes[col_index] : codes
|
|
939
|
+
raw_cell_style = style
|
|
940
|
+
date_format = nil
|
|
941
|
+
unless date_variants.empty?
|
|
942
|
+
cell_value = value.is_a?(Elements::Formula) ? value.cached_value : value
|
|
943
|
+
if (cell_value.is_a?(Date) || cell_value.is_a?(Time)) && (style.is_a?(String) || style.is_a?(Symbol))
|
|
944
|
+
date_variant = date_variants[[style_version(style.to_s, row_clock, versions), cell_value.is_a?(Time) || cell_value.is_a?(DateTime)]]
|
|
945
|
+
raw_cell_style, date_format = date_variant if date_variant
|
|
946
|
+
end
|
|
947
|
+
end
|
|
948
|
+
|
|
949
|
+
resolved_cell_style = if raw_cell_style.nil?
|
|
950
|
+
resolved_row_style || col_style_map[col_index]
|
|
951
|
+
elsif raw_cell_style.equal?(last_raw_style)
|
|
952
|
+
# Rows usually repeat one style object; reuse its last resolution.
|
|
953
|
+
last_resolved_style
|
|
856
954
|
else
|
|
857
|
-
raw_cell_style
|
|
955
|
+
last_raw_style = raw_cell_style
|
|
956
|
+
# Symbol#name looks a Symbol up without allocating a String.
|
|
957
|
+
style_key = raw_cell_style.is_a?(Symbol) ? raw_cell_style.name : raw_cell_style
|
|
958
|
+
style_key = style_version(style_key, row_clock, versions) if style_key.is_a?(String) && !versions.empty?
|
|
959
|
+
last_resolved_style = (style_key.is_a?(String) && style_name_to_id[style_key]) || raw_cell_style
|
|
858
960
|
end
|
|
859
961
|
|
|
860
|
-
target_fmt =
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
comment: cell.comment,
|
|
874
|
-
raw_value: cell.raw_value,
|
|
875
|
-
format_code: target_fmt,
|
|
876
|
-
unmapped_data: cell.unmapped_data,
|
|
877
|
-
errors: cell.errors
|
|
878
|
-
)
|
|
879
|
-
end
|
|
962
|
+
target_fmt = format_code || date_format || col_fmt_map[col_index]
|
|
963
|
+
unchanged = resolved_cell_style == style && target_fmt == format_code
|
|
964
|
+
valid = all_valid || Elements::Cell.valid_coordinates?(row_index, col_index)
|
|
965
|
+
|
|
966
|
+
cells << if valid && !value.is_a?(Elements::Formula) && !(unchanged && style.is_a?(Integer))
|
|
967
|
+
# A valid cell has no errors, so it skips the checks of Cell.new.
|
|
968
|
+
Elements::Cell.fast_create(row_index, col_index, value, resolved_cell_style, nil, value&.to_s, target_fmt)
|
|
969
|
+
elsif unchanged
|
|
970
|
+
WorksheetBuilder::PendingRow.cell(row_index, col_index, value, style, format_code, valid)
|
|
971
|
+
else
|
|
972
|
+
resolved_cell(row_index, col_index, value, resolved_cell_style, target_fmt, valid)
|
|
973
|
+
end
|
|
974
|
+
col_index += 1
|
|
880
975
|
end
|
|
881
976
|
|
|
882
|
-
|
|
883
|
-
index: row.index,
|
|
884
|
-
cells: new_cells,
|
|
885
|
-
height: row.height,
|
|
886
|
-
hidden: row.hidden?,
|
|
887
|
-
custom_height: row.custom_height?,
|
|
888
|
-
collapsed: row.collapsed?,
|
|
889
|
-
outline_level: row.outline_level,
|
|
890
|
-
style_index: resolved_row_style,
|
|
891
|
-
unmapped_data: row_unmapped,
|
|
892
|
-
errors: row.errors
|
|
893
|
-
)
|
|
977
|
+
row.to_row(sheet.attach_links(cells), resolved_row_style)
|
|
894
978
|
end
|
|
895
979
|
|
|
896
|
-
sheet.
|
|
980
|
+
sheet.worksheet(new_rows, new_columns)
|
|
897
981
|
end
|
|
898
982
|
|
|
899
983
|
[updated_sheets, styles_definition]
|
|
900
984
|
end
|
|
901
985
|
|
|
986
|
+
# Returns the key of the definition that the style +name+ stands for at style log
|
|
987
|
+
# +clock+: the last one made by then, or the first one if none was. A name defined
|
|
988
|
+
# once, or always with the same format, is its own key.
|
|
989
|
+
#: (String name, Integer? clock, Hash[String, Array[[Integer, String]]] versions) -> String
|
|
990
|
+
def style_version(name, clock, versions)
|
|
991
|
+
definitions = versions[name]
|
|
992
|
+
return name if definitions.nil? || clock.nil?
|
|
993
|
+
|
|
994
|
+
key = definitions.first.last
|
|
995
|
+
definitions.each do |defined_at, version_key|
|
|
996
|
+
break if defined_at > clock
|
|
997
|
+
|
|
998
|
+
key = version_key
|
|
999
|
+
end
|
|
1000
|
+
key
|
|
1001
|
+
end
|
|
1002
|
+
|
|
1003
|
+
# Marks the definition that the style +name+ stands for at style log +clock+ as used.
|
|
1004
|
+
#: (untyped name, Integer? clock, Hash[String, Array[[Integer, String]]] versions, Hash[String, true] used_versions) -> void
|
|
1005
|
+
def use_style_version(name, clock, versions, used_versions)
|
|
1006
|
+
name = name.name if name.is_a?(Symbol)
|
|
1007
|
+
return unless name.is_a?(String) && versions.key?(name)
|
|
1008
|
+
|
|
1009
|
+
used_versions[style_version(name, clock, versions)] = true
|
|
1010
|
+
end
|
|
1011
|
+
|
|
1012
|
+
# Records the date or time variant of the style named +style_key+ that a Date or
|
|
1013
|
+
# Time +value+ in a row added at style log +clock+ needs, if any.
|
|
1014
|
+
#: (untyped value, String style_key, Integer clock, Hash[String, Array[[Integer, String]]] versions, Hash[String, untyped] all_style_builders, Hash[String, StyleBuilder] version_builders, Hash[untyped, untyped] date_variants, Hash[String, untyped] variant_builders) -> void
|
|
1015
|
+
def add_date_variant(value, style_key, clock, versions, all_style_builders, version_builders, date_variants, variant_builders)
|
|
1016
|
+
value = value.cached_value if value.is_a?(Elements::Formula)
|
|
1017
|
+
return unless value.is_a?(Date) || value.is_a?(Time)
|
|
1018
|
+
|
|
1019
|
+
style_key = style_version(style_key, clock, versions)
|
|
1020
|
+
style_builder = all_style_builders[style_key] || version_builders[style_key]
|
|
1021
|
+
variant = DslHelpers.date_style_variant(value, style_key, style_builder)
|
|
1022
|
+
return unless variant
|
|
1023
|
+
|
|
1024
|
+
variant_name, number_format = variant
|
|
1025
|
+
date_variants[[style_key, value.is_a?(Time) || value.is_a?(DateTime)]] = variant
|
|
1026
|
+
variant_builders[variant_name] ||= style_builder.with_number_format(variant_name, number_format)
|
|
1027
|
+
end
|
|
1028
|
+
|
|
1029
|
+
# Creates a cell whose style was resolved to +style+ (an index, or a name with no index).
|
|
1030
|
+
#: (Integer row_index, Integer column_index, untyped value, untyped style, String? format_code, bool valid_coordinates) -> Elements::Cell
|
|
1031
|
+
def resolved_cell(row_index, column_index, value, style, format_code, valid_coordinates)
|
|
1032
|
+
formula = nil
|
|
1033
|
+
if value.is_a?(Elements::Formula)
|
|
1034
|
+
formula = value
|
|
1035
|
+
value = formula.cached_value
|
|
1036
|
+
valid_coordinates &&= Elements::Cell.valid_value?(value)
|
|
1037
|
+
end
|
|
1038
|
+
raw_value = value&.to_s
|
|
1039
|
+
return Elements::Cell.fast_create(row_index, column_index, value, style, formula, raw_value, format_code) if valid_coordinates
|
|
1040
|
+
|
|
1041
|
+
Elements::Cell.new(
|
|
1042
|
+
row_index: row_index,
|
|
1043
|
+
column_index: column_index,
|
|
1044
|
+
value: value,
|
|
1045
|
+
formula: formula,
|
|
1046
|
+
style: (style unless style.is_a?(Integer)),
|
|
1047
|
+
style_index: (style if style.is_a?(Integer)),
|
|
1048
|
+
raw_value: raw_value,
|
|
1049
|
+
format_code: format_code
|
|
1050
|
+
)
|
|
1051
|
+
end
|
|
1052
|
+
|
|
902
1053
|
#: (untyped writer) -> Hash[Symbol, untyped]
|
|
903
1054
|
def extract_styles_from_writer(writer)
|
|
904
1055
|
# Extract style definitions from the writer that can be reused
|