xlsxrb 0.1.17 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +256 -1
  3. data/README.md +111 -28
  4. data/lib/ruby_lsp/xlsxrb/completion_listener.rb +439 -139
  5. data/lib/xlsxrb/chart_builder.rb +540 -193
  6. data/lib/xlsxrb/dsl_helpers.rb +178 -73
  7. data/lib/xlsxrb/elements/cell.rb +197 -130
  8. data/lib/xlsxrb/elements/column.rb +88 -7
  9. data/lib/xlsxrb/elements/coordinate_access.rb +86 -59
  10. data/lib/xlsxrb/elements/image.rb +18 -19
  11. data/lib/xlsxrb/elements/metadata.rb +2379 -0
  12. data/lib/xlsxrb/elements/row.rb +84 -118
  13. data/lib/xlsxrb/elements/styles.rb +140 -90
  14. data/lib/xlsxrb/elements/types.rb +164 -22
  15. data/lib/xlsxrb/elements/workbook.rb +122 -140
  16. data/lib/xlsxrb/elements/worksheet.rb +451 -226
  17. data/lib/xlsxrb/elements.rb +1 -0
  18. data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +16 -5
  19. data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +14 -9
  20. data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +296 -34
  21. data/lib/xlsxrb/ooxml/reader/listeners/style_listeners.rb +17 -3
  22. data/lib/xlsxrb/ooxml/reader.rb +52 -50
  23. data/lib/xlsxrb/ooxml/styles_parser.rb +36 -5
  24. data/lib/xlsxrb/ooxml/utils.rb +2 -2
  25. data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
  26. data/lib/xlsxrb/ooxml/workbook_writer.rb +242 -109
  27. data/lib/xlsxrb/ooxml/worksheet_parser.rb +63 -31
  28. data/lib/xlsxrb/ooxml/worksheet_writer.rb +119 -56
  29. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +33 -13
  30. data/lib/xlsxrb/ooxml/writer/features_xml.rb +75 -15
  31. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +18 -8
  32. data/lib/xlsxrb/ooxml/writer.rb +124 -44
  33. data/lib/xlsxrb/ooxml/xml_parser.rb +78 -5
  34. data/lib/xlsxrb/ooxml/zip_generator.rb +2 -0
  35. data/lib/xlsxrb/ooxml/zip_reader.rb +112 -25
  36. data/lib/xlsxrb/ooxml/zip_writer.rb +119 -30
  37. data/lib/xlsxrb/stream_row.rb +56 -81
  38. data/lib/xlsxrb/stream_sheet.rb +542 -273
  39. data/lib/xlsxrb/stream_writer.rb +1536 -870
  40. data/lib/xlsxrb/style_builder.rb +667 -221
  41. data/lib/xlsxrb/version.rb +1 -1
  42. data/lib/xlsxrb/workbook_builder.rb +406 -143
  43. data/lib/xlsxrb/worksheet_builder.rb +1009 -900
  44. data/lib/xlsxrb.rb +1179 -1056
  45. data/sig/generated/xlsxrb/chart_builder.rbs +385 -159
  46. data/sig/generated/xlsxrb/dsl_helpers.rbs +81 -15
  47. data/sig/generated/xlsxrb/elements/cell.rbs +78 -70
  48. data/sig/generated/xlsxrb/elements/column.rbs +55 -3
  49. data/sig/generated/xlsxrb/elements/coordinate_access.rbs +35 -29
  50. data/sig/generated/xlsxrb/elements/image.rbs +7 -11
  51. data/sig/generated/xlsxrb/elements/metadata.rbs +1574 -0
  52. data/sig/generated/xlsxrb/elements/row.rbs +39 -42
  53. data/sig/generated/xlsxrb/elements/styles.rbs +66 -66
  54. data/sig/generated/xlsxrb/elements/types.rbs +27 -0
  55. data/sig/generated/xlsxrb/elements/workbook.rbs +46 -99
  56. data/sig/generated/xlsxrb/elements/worksheet.rbs +121 -96
  57. data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +61 -4
  58. data/sig/generated/xlsxrb/ooxml/reader.rbs +2 -2
  59. data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +1 -1
  60. data/sig/generated/xlsxrb/ooxml/utils.rbs +3 -3
  61. data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +13 -1
  62. data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
  63. data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +6 -4
  64. data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +7 -0
  65. data/sig/generated/xlsxrb/ooxml/writer.rbs +22 -13
  66. data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +70 -0
  67. data/sig/generated/xlsxrb/ooxml/zip_generator.rbs +2 -0
  68. data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +24 -9
  69. data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +26 -1
  70. data/sig/generated/xlsxrb/stream_row.rbs +32 -33
  71. data/sig/generated/xlsxrb/stream_sheet.rbs +233 -181
  72. data/sig/generated/xlsxrb/stream_writer.rbs +961 -667
  73. data/sig/generated/xlsxrb/style_builder.rbs +402 -128
  74. data/sig/generated/xlsxrb/workbook_builder.rbs +223 -71
  75. data/sig/generated/xlsxrb/worksheet_builder.rbs +579 -453
  76. data/sig/generated/xlsxrb.rbs +205 -144
  77. data/sig/rexml.rbs +8 -0
  78. metadata +5 -5
  79. data/lib/xlsxrb/utils.rb +0 -291
  80. data/sig/generated/xlsxrb/utils.rbs +0 -184
@@ -33,7 +33,7 @@ module Xlsxrb
33
33
  def self.write(target, sheets:, shared_strings: [], shared_strings_index: nil, styles: nil,
34
34
  defined_names: nil, core_properties: nil, app_properties: nil,
35
35
  custom_properties: nil, workbook_protection: nil, workbook_properties: nil,
36
- compression_level: Zlib::DEFAULT_COMPRESSION)
36
+ compression_level: DEFAULT_COMPRESSION_LEVEL)
37
37
  writer = new(sheets: sheets, shared_strings: shared_strings, shared_strings_index: shared_strings_index, styles: styles,
38
38
  defined_names: defined_names, core_properties: core_properties,
39
39
  app_properties: app_properties, custom_properties: custom_properties,
@@ -45,18 +45,18 @@ module Xlsxrb
45
45
  def initialize(sheets:, shared_strings: [], shared_strings_index: nil, styles: nil,
46
46
  defined_names: nil, core_properties: nil, app_properties: nil,
47
47
  custom_properties: nil, workbook_protection: nil, workbook_properties: nil,
48
- compression_level: Zlib::DEFAULT_COMPRESSION)
48
+ compression_level: DEFAULT_COMPRESSION_LEVEL)
49
49
  @sheets = sheets
50
50
  @shared_strings = shared_strings || []
51
51
  @shared_strings_index = shared_strings_index
52
52
  @styles = styles || {}
53
53
  @defined_names = defined_names || []
54
- @core_properties = core_properties || {}
55
- @app_properties = app_properties || {}
54
+ @core_properties = core_properties.is_a?(Elements::MetadataRecord) ? core_properties.to_h : (core_properties || {})
55
+ @app_properties = app_properties.is_a?(Elements::MetadataRecord) ? app_properties.to_h : (app_properties || {})
56
56
  @custom_properties = custom_properties || []
57
- @workbook_protection = workbook_protection
58
- @workbook_properties = workbook_properties || { update_links: "never" }
59
- @compression_level = compression_level || Zlib::DEFAULT_COMPRESSION
57
+ @workbook_protection = workbook_protection.is_a?(Elements::MetadataRecord) ? workbook_protection.to_h : workbook_protection
58
+ @workbook_properties = workbook_properties.is_a?(Elements::MetadataRecord) ? workbook_properties.to_h : (workbook_properties || { update_links: "never" })
59
+ @compression_level = compression_level || DEFAULT_COMPRESSION_LEVEL
60
60
  @drawing_count = 0
61
61
  @chart_count = 0
62
62
  @comment_count = 0
@@ -68,6 +68,8 @@ module Xlsxrb
68
68
  end
69
69
 
70
70
  def write_to(target)
71
+ raise ArgumentError, "Workbook must contain at least one sheet (Excel limitation)" if @sheets.empty?
72
+
71
73
  preprocess_conditional_formats!
72
74
 
73
75
  active_tab_idx = resolve_active_tab_index
@@ -79,6 +81,7 @@ module Xlsxrb
79
81
  sheet_tables = sheet[:tables] || []
80
82
 
81
83
  sv = sheet[:sheet_view]
84
+ sv = sv.to_h if sv && !sv.is_a?(Hash)
82
85
  if idx == effective_active_idx
83
86
  sv = (sv || {}).merge(tab_selected: true)
84
87
  elsif sv&.key?(:tab_selected)
@@ -105,7 +108,7 @@ module Xlsxrb
105
108
  page_setup: sheet[:page_setup],
106
109
  header_footer: sheet[:header_footer],
107
110
  row_breaks: sheet[:row_breaks],
108
- col_breaks: sheet[:col_breaks],
111
+ column_breaks: sheet[:column_breaks],
109
112
  freeze_pane: sheet[:freeze_pane],
110
113
  split_pane: sheet[:split_pane],
111
114
  selection: sheet[:selection],
@@ -250,6 +253,7 @@ module Xlsxrb
250
253
 
251
254
  def write_auxiliary_parts(zip)
252
255
  drawing_idx = 0
256
+ media_idx = 0
253
257
  comment_idx = 0
254
258
  tc_idx = 0
255
259
  table_idx = 0
@@ -287,9 +291,11 @@ module Xlsxrb
287
291
  end
288
292
 
289
293
  sheet_images.each do |img|
290
- media_path = "xl/media/image#{drawing_idx}.#{img[:ext] || "png"}"
291
- zip.add_binary_entry(media_path, img[:file_data])
292
- drawing_rels_data << { type: :image, target: "../media/image#{drawing_idx}.#{img[:ext] || "png"}" }
294
+ # One media file per image: a sheet can hold several.
295
+ media_idx += 1
296
+ media_name = "image#{media_idx}.#{img[:ext] || "png"}"
297
+ zip.add_binary_entry("xl/media/#{media_name}", img[:file_data])
298
+ drawing_rels_data << { type: :image, target: "../media/#{media_name}" }
293
299
  drawing_parts << { kind: :pic, img: img, rid_index: drawing_rels_data.size }
294
300
  end
295
301
 
@@ -321,7 +327,9 @@ module Xlsxrb
321
327
  comment_idx += 1
322
328
  comment_writer = Xlsxrb::Ooxml::Writer.new
323
329
  sheet_comments.each do |c|
324
- comment_writer.add_comment(c[:cell] || c[:ref], c[:text], author: c[:author] || "Author")
330
+ c = c.to_h if c.is_a?(Elements::MetadataRecord)
331
+ comment_writer.add_comment(c[:cell] || c[:ref], c[:text], author: c[:author] || "Author", visible: c[:visible] == true,
332
+ anchor: c[:anchor])
325
333
  end
326
334
  zip.add_entry("xl/comments#{comment_idx}.xml", comment_writer.send(:generate_comments_xml, comment_writer.comments))
327
335
  zip.add_entry("xl/drawings/vmlDrawing#{comment_idx}.vml", comment_writer.send(:generate_vml_drawing_xml, comment_writer.comments))
@@ -332,14 +340,15 @@ module Xlsxrb
332
340
  tc_idx += 1
333
341
  tc_writer = Xlsxrb::Ooxml::Writer.new
334
342
  sheet_threaded_comments.each do |c|
343
+ c = c.to_h if c.is_a?(Elements::MetadataRecord)
335
344
  tc_writer.add_threaded_comment(
336
345
  c[:cell] || c[:ref],
337
346
  c[:text],
338
347
  author: c[:author] || "Author",
339
- timestamp: c[:timestamp] || c[:dt],
348
+ timestamp: c[:timestamp] || c[:dt] || c[:created_at],
340
349
  id: c[:id],
341
350
  parent_id: c[:parent_id],
342
- done: c[:done],
351
+ done: c.key?(:done) ? c[:done] : c[:resolved],
343
352
  user_id: c[:user_id],
344
353
  provider_id: c[:provider_id]
345
354
  )
@@ -357,23 +366,15 @@ module Xlsxrb
357
366
 
358
367
  sheet_pivot_tables = sheet[:pivot_tables] || []
359
368
  unless sheet_pivot_tables.empty?
369
+ # The writer only collects the definitions here, so its default sheet holds them
370
+ # whatever the worksheet is called; source ranges name their own sheet.
360
371
  pivot_writer = Xlsxrb::Ooxml::Writer.new
361
- pivot_writer.add_sheet(sheet[:name])
362
372
  sheet_pivot_tables.each do |pt|
363
- pivot_writer.add_pivot_table(
364
- pt[:source_ref],
365
- row_fields: pt[:row_fields],
366
- data_fields: pt[:data_fields],
367
- col_fields: pt[:col_fields] || [],
368
- dest_ref: pt[:dest_ref] || "E1",
369
- name: pt[:name],
370
- field_names: pt[:field_names],
371
- items: pt[:items],
372
- sheet: sheet[:name]
373
- )
373
+ source_ref, args = pivot_table_arguments(pt)
374
+ pivot_writer.add_pivot_table(source_ref, **args)
374
375
  end
375
376
 
376
- pivot_data = pivot_writer.pivot_tables(sheet: sheet[:name])
377
+ pivot_data = pivot_writer.pivot_tables
377
378
  pivot_data.each do |pt_data|
378
379
  pivot_cache_idx += 1
379
380
  pivot_table_idx += 1
@@ -423,6 +424,51 @@ module Xlsxrb
423
424
 
424
425
  private
425
426
 
427
+ PIVOT_CACHE_OPTIONS = {
428
+ save_data: :cache_save_data, enable_refresh: :cache_enable_refresh, refreshed_by: :cache_refreshed_by,
429
+ refreshed_version: :cache_refreshed_version, created_version: :cache_created_version,
430
+ optimize_memory: :cache_optimize_memory
431
+ }.freeze
432
+ private_constant :PIVOT_CACHE_OPTIONS
433
+
434
+ # Returns the source and keyword arguments of Writer#add_pivot_table for a pivot table hash,
435
+ # as the builders create it or as Xlsxrb.load reads it (with the source, field names, and
436
+ # shared items in :cache, and pivot field attributes in :fields).
437
+ #: (Hash[Symbol, untyped] pivot_table) -> [String?, Hash[Symbol, untyped]]
438
+ def pivot_table_arguments(pivot_table)
439
+ cache = pivot_table[:cache] || {}
440
+ cache_fields = cache[:fields] || []
441
+ source_ref = pivot_table[:source_ref]
442
+ if source_ref.nil? && cache[:source_ref]
443
+ source_ref = cache[:source_sheet] ? "#{DslHelpers.quote_sheet_name(cache[:source_sheet])}!#{cache[:source_ref]}" : cache[:source_ref]
444
+ end
445
+ field_names = pivot_table[:field_names] || (cache_fields.map { |f| f[:name] } unless cache_fields.empty?)
446
+ items = pivot_table[:items]
447
+ if items.nil? && cache_fields.any? { |f| f[:shared_items] }
448
+ items = cache_fields.each_with_index.filter_map { |f, i| [i, f[:shared_items]] if f[:shared_items] }.to_h
449
+ end
450
+ field_attrs = pivot_table[:field_attrs]
451
+ if field_attrs.nil? && pivot_table[:fields]
452
+ field_attrs = pivot_table[:fields].each_with_index.map do |f, i|
453
+ cf = cache_fields[i] || {}
454
+ f.except(:items).merge(cache_num_fmt_id: cf[:num_fmt_id], cache_caption: cf[:caption], cache_formula: cf[:formula]).compact
455
+ end
456
+ end
457
+ options = pivot_table.except(:source_ref, :row_fields, :data_fields, :column_fields, :col_fields, :destination, :dest_ref, :ref,
458
+ :field_names, :items, :field_attrs, :fields, :cache, :cache_id, :sheet)
459
+ # Page fields of a loaded pivot table are not written, so neither are their counts.
460
+ options = options.except(:row_page_count, :col_page_count, :column_page_count) if pivot_table[:fields]
461
+ PIVOT_CACHE_OPTIONS.each { |key, option| options[option] = cache[key] if !cache[key].nil? && !options.key?(option) }
462
+ options[:source_name] ||= cache[:source_name] if cache[:source_name]
463
+ args = options.merge(
464
+ row_fields: pivot_table[:row_fields] || [], data_fields: pivot_table[:data_fields] || [],
465
+ column_fields: pivot_table[:column_fields] || pivot_table[:col_fields] || [],
466
+ destination: pivot_table[:destination] || pivot_table[:dest_ref] || pivot_table[:ref] || "E1",
467
+ field_names: field_names, items: items, field_attrs: field_attrs
468
+ )
469
+ [source_ref, args]
470
+ end
471
+
426
472
  def build_sheet_rels_from_list(rels)
427
473
  io = StringIO.new
428
474
  b = XmlBuilder.new(io)
@@ -602,9 +648,25 @@ module Xlsxrb
602
648
  # Workbook protection
603
649
  if @workbook_protection
604
650
  wp_attrs = {}
605
- wp_attrs[:lockStructure] = "1" if @workbook_protection[:lock_structure]
606
- wp_attrs[:lockWindows] = "1" if @workbook_protection[:lock_windows]
607
- wp_attrs[:workbookPassword] = @workbook_protection[:password] if @workbook_protection[:password]
651
+ wp = @workbook_protection
652
+ legacy_password = wp[:workbook_password] || wp[:password]
653
+ wp_attrs[:workbookPassword] = legacy_password.to_s if legacy_password
654
+ wp_attrs[:lockStructure] = "1" if wp[:lock_structure]
655
+ wp_attrs[:lockWindows] = "1" if wp[:lock_windows]
656
+ wp_attrs[:lockRevision] = "1" if wp[:lock_revision]
657
+ wp_attrs[:revisionsPassword] = wp[:revisions_password].to_s if wp[:revisions_password]
658
+ if wp[:algorithm_name]
659
+ wp_attrs[:workbookAlgorithmName] = wp[:algorithm_name].to_s
660
+ wp_attrs[:workbookHashValue] = wp[:hash_value].to_s if wp[:hash_value]
661
+ wp_attrs[:workbookSaltValue] = wp[:salt_value].to_s if wp[:salt_value]
662
+ wp_attrs[:workbookSpinCount] = wp[:spin_count].to_s if wp[:spin_count]
663
+ end
664
+ if wp[:revisions_algorithm_name]
665
+ wp_attrs[:revisionsAlgorithmName] = wp[:revisions_algorithm_name].to_s
666
+ wp_attrs[:revisionsHashValue] = wp[:revisions_hash_value].to_s if wp[:revisions_hash_value]
667
+ wp_attrs[:revisionsSaltValue] = wp[:revisions_salt_value].to_s if wp[:revisions_salt_value]
668
+ wp_attrs[:revisionsSpinCount] = wp[:revisions_spin_count].to_s if wp[:revisions_spin_count]
669
+ end
608
670
  b.empty_tag("workbookProtection", wp_attrs) unless wp_attrs.empty?
609
671
  end
610
672
 
@@ -614,7 +676,7 @@ module Xlsxrb
614
676
  show_sheet_tabs = @workbook_properties&.[](:show_sheet_tabs)
615
677
  tab_ratio = @workbook_properties&.[](:tab_ratio)
616
678
 
617
- emit_active_tab = !@workbook_properties&.[](:active_tab).nil? || active_tab_idx&.positive?
679
+ emit_active_tab = !(@workbook_properties&.[](:active_sheet_index) || @workbook_properties&.[](:active_sheet) || @workbook_properties&.[](:active_tab)).nil? || active_tab_idx&.positive?
618
680
 
619
681
  has_book_views = emit_active_tab || first_sheet_idx || !show_sheet_tabs.nil? || tab_ratio
620
682
  if has_book_views
@@ -648,16 +710,17 @@ module Xlsxrb
648
710
  unless @defined_names.empty?
649
711
  b.open_tag("definedNames")
650
712
  @defined_names.each do |dn|
713
+ dn = dn.to_h if dn.is_a?(Elements::MetadataRecord)
651
714
  dn_attrs = { name: dn[:name] }
652
715
  local_id = dn[:local_sheet_id]
653
- if !local_id && dn[:sheet]
654
- target_name = dn[:sheet].to_s
716
+ target_name = (dn[:sheet] || dn[:local_sheet_name])&.to_s
717
+ if !local_id && target_name
655
718
  matched_idx = @sheets.find_index { |s| (s.respond_to?(:name) ? s.name : s[:name]) == target_name }
656
719
  local_id = matched_idx if matched_idx
657
720
  end
658
721
  dn_attrs[:localSheetId] = local_id.to_s if local_id
659
722
  dn_attrs[:hidden] = "1" if dn[:hidden]
660
- val = dn[:value] || (dn[:sheet] && dn[:range] ? "'#{dn[:sheet]}'!#{dn[:range]}" : dn[:range])
723
+ val = dn[:value] || (target_name && dn[:range] ? "'#{target_name}'!#{dn[:range]}" : dn[:range])
661
724
  b.tag("definedName", dn_attrs) { |_| b.text(val) }
662
725
  end
663
726
  b.close_tag("definedNames")
@@ -741,8 +804,8 @@ module Xlsxrb
741
804
  b.open_tag("styleSheet", { xmlns: SSML_NS })
742
805
 
743
806
  # Number formats
744
- raw_num_fmts = if @styles.respond_to?(:num_fmts)
745
- @styles.num_fmts
807
+ raw_num_fmts = if @styles.respond_to?(:number_formats)
808
+ @styles.number_formats
746
809
  else
747
810
  @styles&.dig(:num_fmts) || []
748
811
  end
@@ -778,6 +841,7 @@ module Xlsxrb
778
841
  fonts = [{}] if fonts.empty? # At least one default font
779
842
  b.tag("fonts", { count: fonts.size.to_s }) do |_|
780
843
  fonts.each do |font_props|
844
+ font_props = font_props.to_h if font_props.is_a?(Elements::MetadataRecord)
781
845
  b.tag("font") do |_|
782
846
  b.empty_tag("b") if font_props[:bold]
783
847
  b.empty_tag("i") if font_props[:italic]
@@ -798,7 +862,8 @@ module Xlsxrb
798
862
  mapped_va = StyleBuilder::VERT_ALIGN_MAP[va_val.to_sym] || va_val.to_s
799
863
  b.empty_tag("vertAlign", { val: mapped_va })
800
864
  end
801
- b.empty_tag("sz", { val: (font_props[:sz] || 11).to_s }) if font_props[:sz] || fonts == [{}]
865
+ sz_val = font_props[:size] || font_props[:sz]
866
+ b.empty_tag("sz", { val: (sz_val || 11).to_s }) if sz_val || fonts == [{}]
802
867
  color_attrs = color_tag_attrs(font_props[:color] || (font_props.key?(:rgb) || font_props.key?(:theme) || font_props.key?(:indexed) ? font_props : nil))
803
868
  b.empty_tag("color", color_attrs) if color_attrs
804
869
  b.empty_tag("name", { val: font_props[:name] || "Calibri" })
@@ -818,9 +883,12 @@ module Xlsxrb
818
883
  fills = [{ pattern: "none" }, { pattern: "gray125" }] if fills.empty?
819
884
  b.tag("fills", { count: fills.size.to_s }) do |_|
820
885
  fills.each do |fill_props|
886
+ fill_props = fill_props.to_h if fill_props.is_a?(Elements::MetadataRecord)
821
887
  b.tag("fill") do |_|
822
888
  if fill_props[:gradient]
823
- b.open_tag("gradientFill", { type: fill_props[:gradient][:type], degree: fill_props[:gradient][:degree]&.to_s }.compact)
889
+ gradient = fill_props[:gradient]
890
+ b.open_tag("gradientFill", { type: gradient[:type], degree: gradient[:degree]&.to_s, left: gradient[:left]&.to_s,
891
+ right: gradient[:right]&.to_s, top: gradient[:top]&.to_s, bottom: gradient[:bottom]&.to_s }.compact)
824
892
  fill_props[:gradient][:stops].each do |stop|
825
893
  b.tag("stop", { position: stop[:position].to_s }) do |_|
826
894
  c_attrs = color_tag_attrs(stop[:color] || stop)
@@ -829,7 +897,8 @@ module Xlsxrb
829
897
  end
830
898
  b.close_tag("gradientFill")
831
899
  else
832
- b.tag("patternFill", { patternType: fill_props[:pattern] || "none" }) do |_|
900
+ # Elements::Fill names the pattern :pattern_type; StyleBuilder fills use :pattern.
901
+ b.tag("patternFill", { patternType: fill_props[:pattern_type] || fill_props[:pattern] || "none" }) do |_|
833
902
  fg_attrs = color_tag_attrs(fill_props[:fg_color] || (fill_props[:fg_color_theme] ? { theme: fill_props[:fg_color_theme], tint: fill_props[:fg_color_tint] } : nil) || (fill_props[:fg_color_indexed] ? { indexed: fill_props[:fg_color_indexed] } : nil))
834
903
  b.empty_tag("fgColor", fg_attrs) if fg_attrs
835
904
 
@@ -850,6 +919,7 @@ module Xlsxrb
850
919
  borders = [{}] if borders.empty?
851
920
  b.tag("borders", { count: borders.size.to_s }) do |_|
852
921
  borders.each do |border_props|
922
+ border_props = border_props.to_h if border_props.is_a?(Elements::MetadataRecord)
853
923
  border_attrs = {}
854
924
  border_attrs[:diagonalUp] = "1" if border_props[:diagonal_up]
855
925
  border_attrs[:diagonalDown] = "1" if border_props[:diagonal_down]
@@ -869,9 +939,16 @@ module Xlsxrb
869
939
  end
870
940
  end
871
941
 
872
- # cellStyleXfs
873
- b.tag("cellStyleXfs", { count: "1" }) do |_|
874
- b.empty_tag("xf", { numFmtId: "0", fontId: "0", fillId: "0", borderId: "0" })
942
+ # cellStyleXfs: the base formats that cellXfs and named cell styles refer to by xfId.
943
+ style_xfs = if @styles.respond_to?(:cell_style_xfs)
944
+ @styles.cell_style_xfs
945
+ else
946
+ @styles&.dig(:cell_style_xfs) || []
947
+ end
948
+ style_xf_count = [style_xfs.size, 1].max
949
+ b.tag("cellStyleXfs", { count: style_xf_count.to_s }) do |_|
950
+ style_xfs.each { |xf| write_xf(b, xf, style_xf_count: nil) }
951
+ b.empty_tag("xf", { numFmtId: "0", fontId: "0", fillId: "0", borderId: "0" }) if style_xfs.empty?
875
952
  end
876
953
 
877
954
  # Cell XFs (formatting)
@@ -882,51 +959,27 @@ module Xlsxrb
882
959
  end
883
960
  xf_count = [xf_entries.size, 1].max
884
961
  b.tag("cellXfs", { count: xf_count.to_s }) do |_|
885
- xf_entries.each do |xf|
886
- attrs = {
887
- numFmtId: (xf[:num_fmt_id] || 0).to_s,
888
- fontId: (xf[:font_id] || 0).to_s,
889
- fillId: (xf[:fill_id] || 0).to_s,
890
- borderId: (xf[:border_id] || 0).to_s
891
- }
892
- attrs[:xfId] = xf[:xf_id].to_s if xf[:xf_id]
893
- attrs[:applyNumberFormat] = "1" if xf[:num_fmt_id]&.to_i&.positive?
894
- attrs[:applyFont] = "1" if xf[:font_id]&.to_i&.positive?
895
- attrs[:applyFill] = "1" if xf[:fill_id]&.to_i&.positive?
896
- attrs[:applyBorder] = "1" if xf[:border_id]&.to_i&.positive?
897
- attrs[:applyAlignment] = "1" if xf[:alignment]
898
- attrs[:applyProtection] = "1" if xf[:protection]
899
-
900
- if xf[:alignment] || xf[:protection]
901
- b.tag("xf", attrs) do |_|
902
- if xf[:alignment]
903
- align_attrs = {}
904
- align_attrs[:horizontal] = xf[:alignment][:horizontal] if xf[:alignment][:horizontal]
905
- align_attrs[:vertical] = xf[:alignment][:vertical] if xf[:alignment][:vertical]
906
- align_attrs[:wrapText] = "1" if xf[:alignment][:wrap_text]
907
- align_attrs[:textRotation] = xf[:alignment][:text_rotation].to_s if xf[:alignment][:text_rotation]
908
- align_attrs[:indent] = xf[:alignment][:indent].to_s if xf[:alignment][:indent]
909
- align_attrs[:relativeIndent] = xf[:alignment][:relative_indent].to_s if xf[:alignment][:relative_indent]
910
- align_attrs[:shrinkToFit] = "1" if xf[:alignment][:shrink_to_fit]
911
- align_attrs[:readingOrder] = xf[:alignment][:reading_order].to_s if xf[:alignment][:reading_order]
912
- align_attrs[:justifyLastLine] = "1" if xf[:alignment][:justify_last_line]
913
- b.empty_tag("alignment", align_attrs)
914
- end
915
- if xf[:protection]
916
- prot_attrs = {}
917
- prot_attrs[:locked] = xf[:protection][:locked] ? "1" : "0" unless xf[:protection][:locked].nil?
918
- prot_attrs[:hidden] = xf[:protection][:hidden] ? "1" : "0" unless xf[:protection][:hidden].nil?
919
- b.empty_tag("protection", prot_attrs)
920
- end
921
- end
922
- else
923
- b.empty_tag("xf", attrs)
924
- end
925
- end
962
+ xf_entries.each { |xf| write_xf(b, xf, style_xf_count: style_xf_count) }
926
963
  # Add default if empty
927
964
  b.empty_tag("xf", { numFmtId: "0", fontId: "0", fillId: "0", borderId: "0" }) if xf_entries.empty?
928
965
  end
929
966
 
967
+ # Named cell styles, such as Normal and Heading 1, kept from a loaded workbook.
968
+ cell_styles = if @styles.respond_to?(:cell_styles)
969
+ @styles.cell_styles
970
+ else
971
+ @styles&.dig(:cell_styles) || []
972
+ end
973
+ cell_styles = cell_styles.map { |cs| cs.is_a?(Elements::MetadataRecord) ? cs.to_h : cs }
974
+ .select { |cs| cs[:name] && cs[:xf_id].to_i < style_xf_count }
975
+ unless cell_styles.empty?
976
+ b.tag("cellStyles", { count: cell_styles.size.to_s }) do |_|
977
+ cell_styles.each do |cs|
978
+ b.empty_tag("cellStyle", { name: cs[:name], xfId: cs[:xf_id].to_i.to_s, builtinId: cs[:builtin_id]&.to_s }.compact)
979
+ end
980
+ end
981
+ end
982
+
930
983
  # Differential formats (for conditional formatting)
931
984
  dxfs = if @styles.respond_to?(:dxfs)
932
985
  @styles.dxfs
@@ -936,16 +989,18 @@ module Xlsxrb
936
989
  unless dxfs.empty?
937
990
  b.tag("dxfs", { count: dxfs.size.to_s }) do |_|
938
991
  dxfs.each do |dxf|
992
+ d = dxf.is_a?(Elements::MetadataRecord) ? dxf.to_h : dxf
939
993
  b.tag("dxf") do |_|
940
- write_dxf_font(b, dxf[:font]) if dxf[:font]
941
- if dxf[:num_fmt]
942
- nf = dxf[:num_fmt]
943
- b.empty_tag("numFmt", { numFmtId: nf[:num_fmt_id].to_s, formatCode: nf[:format_code].to_s })
994
+ write_dxf_font(b, d[:font]) if d[:font]
995
+ nf = d[:number_format] || d[:num_fmt]
996
+ if nf
997
+ nf_h = nf.is_a?(Elements::MetadataRecord) ? nf.to_h : nf
998
+ b.empty_tag("numFmt", { numFmtId: nf_h[:num_fmt_id].to_s, formatCode: nf_h[:format_code].to_s })
944
999
  end
945
- write_dxf_fill(b, dxf[:fill]) if dxf[:fill]
946
- write_dxf_alignment(b, dxf[:alignment]) if dxf[:alignment]
947
- write_dxf_border(b, dxf[:border]) if dxf[:border]
948
- write_dxf_protection(b, dxf[:protection]) if dxf[:protection]
1000
+ write_dxf_fill(b, d[:fill]) if d[:fill]
1001
+ write_dxf_alignment(b, d[:alignment]) if d[:alignment]
1002
+ write_dxf_border(b, d[:border]) if d[:border]
1003
+ write_dxf_protection(b, d[:protection]) if d[:protection]
949
1004
  end
950
1005
  end
951
1006
  end
@@ -961,9 +1016,9 @@ module Xlsxrb
961
1016
  next if rules.nil? || rules.empty?
962
1017
 
963
1018
  sheet[:conditional_formats] = rules.map do |rule|
964
- next rule if rule[:format_id]
1019
+ next rule if rule.respond_to?(:dxf_id) ? rule.dxf_id : (rule.is_a?(Hash) && (rule[:dxf_id] || rule[:format_id]))
965
1020
 
966
- normalized = rule.dup
1021
+ normalized = rule.is_a?(Hash) ? rule.dup : rule.to_h
967
1022
  dxf = normalized.delete(:dxf) || extract_dxf_from_rule!(normalized)
968
1023
  next normalized unless dxf
969
1024
 
@@ -976,7 +1031,7 @@ module Xlsxrb
976
1031
  @styles[:dxfs].size - 1
977
1032
  end
978
1033
  end
979
- normalized[:format_id] = dxf_id
1034
+ normalized[:dxf_id] = dxf_id
980
1035
  normalized
981
1036
  end
982
1037
  end
@@ -986,7 +1041,50 @@ module Xlsxrb
986
1041
  DslHelpers.extract_dxf_from_rule!(rule)
987
1042
  end
988
1043
 
1044
+ # Writes one <xf> of cellXfs, or of cellStyleXfs when style_xf_count is nil. An xfId that
1045
+ # points past the written cellStyleXfs is dropped, so it falls back to the first entry.
1046
+ def write_xf(builder, format, style_xf_count:)
1047
+ format = format.to_h if format.is_a?(Elements::MetadataRecord)
1048
+ attrs = {
1049
+ numFmtId: (format[:num_fmt_id] || 0).to_s,
1050
+ fontId: (format[:font_id] || 0).to_s,
1051
+ fillId: (format[:fill_id] || 0).to_s,
1052
+ borderId: (format[:border_id] || 0).to_s
1053
+ }
1054
+ attrs[:xfId] = format[:xf_id].to_s if style_xf_count && format[:xf_id] && format[:xf_id].to_i < style_xf_count
1055
+ attrs[:applyNumberFormat] = "1" if format[:num_fmt_id]&.to_i&.positive?
1056
+ attrs[:applyFont] = "1" if format[:font_id]&.to_i&.positive?
1057
+ attrs[:applyFill] = "1" if format[:fill_id]&.to_i&.positive?
1058
+ attrs[:applyBorder] = "1" if format[:border_id]&.to_i&.positive?
1059
+ attrs[:applyAlignment] = "1" if format[:alignment]
1060
+ attrs[:applyProtection] = "1" if format[:protection]
1061
+ return builder.empty_tag("xf", attrs) unless format[:alignment] || format[:protection]
1062
+
1063
+ builder.tag("xf", attrs) do |_|
1064
+ if format[:alignment]
1065
+ align_attrs = {}
1066
+ align_attrs[:horizontal] = format[:alignment][:horizontal] if format[:alignment][:horizontal]
1067
+ align_attrs[:vertical] = format[:alignment][:vertical] if format[:alignment][:vertical]
1068
+ align_attrs[:wrapText] = "1" if format[:alignment][:wrap_text]
1069
+ align_attrs[:textRotation] = format[:alignment][:text_rotation].to_s if format[:alignment][:text_rotation]
1070
+ align_attrs[:indent] = format[:alignment][:indent].to_s if format[:alignment][:indent]
1071
+ align_attrs[:relativeIndent] = format[:alignment][:relative_indent].to_s if format[:alignment][:relative_indent]
1072
+ align_attrs[:shrinkToFit] = "1" if format[:alignment][:shrink_to_fit]
1073
+ align_attrs[:readingOrder] = format[:alignment][:reading_order].to_s if format[:alignment][:reading_order]
1074
+ align_attrs[:justifyLastLine] = "1" if format[:alignment][:justify_last_line]
1075
+ builder.empty_tag("alignment", align_attrs)
1076
+ end
1077
+ if format[:protection]
1078
+ prot_attrs = {}
1079
+ prot_attrs[:locked] = format[:protection][:locked] ? "1" : "0" unless format[:protection][:locked].nil?
1080
+ prot_attrs[:hidden] = format[:protection][:hidden] ? "1" : "0" unless format[:protection][:hidden].nil?
1081
+ builder.empty_tag("protection", prot_attrs)
1082
+ end
1083
+ end
1084
+ end
1085
+
989
1086
  def write_dxf_font(builder, font)
1087
+ font = font.to_h if font.is_a?(Elements::MetadataRecord)
990
1088
  builder.tag("font") do |_|
991
1089
  if font.key?(:bold)
992
1090
  font[:bold] ? builder.empty_tag("b") : builder.empty_tag("b", { val: "0" })
@@ -1012,15 +1110,17 @@ module Xlsxrb
1012
1110
  mapped_va = StyleBuilder::VERT_ALIGN_MAP[va_val.to_sym] || va_val.to_s
1013
1111
  builder.empty_tag("vertAlign", { val: mapped_va })
1014
1112
  end
1113
+ # sz, color, and name follow vertAlign in this order, as in CT_Font and Excel's output.
1114
+ sz_val = font[:sz] || font[:size]
1115
+ builder.empty_tag("sz", { val: sz_val.to_s }) if sz_val
1015
1116
  color_attrs = color_tag_attrs(font[:color] || font)
1016
1117
  builder.empty_tag("color", color_attrs) if color_attrs
1017
1118
  builder.empty_tag("name", { val: font[:name] }) if font[:name]
1018
- sz_val = font[:sz] || font[:size]
1019
- builder.empty_tag("sz", { val: sz_val.to_s }) if sz_val
1020
1119
  end
1021
1120
  end
1022
1121
 
1023
1122
  def write_dxf_fill(builder, fill)
1123
+ fill = fill.to_h if fill.is_a?(Elements::MetadataRecord)
1024
1124
  builder.tag("fill") do |_|
1025
1125
  pattern_attrs = {}
1026
1126
  pattern = fill[:pattern] || fill[:pattern_type]
@@ -1037,6 +1137,7 @@ module Xlsxrb
1037
1137
  end
1038
1138
 
1039
1139
  def write_dxf_alignment(builder, alignment)
1140
+ alignment = alignment.to_h if alignment.is_a?(Elements::MetadataRecord)
1040
1141
  return if alignment.nil? || alignment.empty?
1041
1142
 
1042
1143
  align_attrs = {}
@@ -1053,6 +1154,7 @@ module Xlsxrb
1053
1154
  end
1054
1155
 
1055
1156
  def write_dxf_border(builder, border)
1157
+ border = border.to_h if border.is_a?(Elements::MetadataRecord)
1056
1158
  return if border.nil? || border.empty?
1057
1159
 
1058
1160
  border_attrs = {}
@@ -1081,6 +1183,7 @@ module Xlsxrb
1081
1183
  end
1082
1184
 
1083
1185
  def write_dxf_protection(builder, protection)
1186
+ protection = protection.to_h if protection.is_a?(Elements::MetadataRecord)
1084
1187
  return if protection.nil? || protection.empty?
1085
1188
 
1086
1189
  prot_attrs = {}
@@ -1167,7 +1270,8 @@ module Xlsxrb
1167
1270
  if str.is_a?(Xlsxrb::Elements::RichText)
1168
1271
  buf << "<si>"
1169
1272
  str.runs.each do |run|
1170
- font = run[:font]
1273
+ font = run.respond_to?(:font) ? run.font : run[:font]
1274
+ text = run.respond_to?(:text) ? run.text : run[:text]
1171
1275
  if font && !font.empty?
1172
1276
  buf << "<r><rPr>"
1173
1277
  buf << "<b/>" if font[:bold]
@@ -1202,7 +1306,7 @@ module Xlsxrb
1202
1306
  else
1203
1307
  buf << "<r><t>"
1204
1308
  end
1205
- buf << escape_xml(run[:text].to_s) << "</t></r>"
1309
+ buf << escape_xml(text.to_s) << "</t></r>"
1206
1310
  end
1207
1311
  buf << "</si>"
1208
1312
  else
@@ -1226,7 +1330,7 @@ module Xlsxrb
1226
1330
  sort_state: nil, merge_cells: nil, conditional_formats: nil,
1227
1331
  data_validations: nil, hyperlinks: nil,
1228
1332
  print_options: nil, page_margins: nil, page_setup: nil,
1229
- header_footer: nil, row_breaks: nil, col_breaks: nil,
1333
+ header_footer: nil, row_breaks: nil, col_breaks: nil, column_breaks: nil,
1230
1334
  cell_watches: nil, ignored_errors: nil, freeze_pane: nil, split_pane: nil,
1231
1335
  selection: nil, sheet_view: nil, sheet_properties: nil,
1232
1336
  sheet_format: nil, tables: nil, table_start_rid: nil,
@@ -1256,8 +1360,8 @@ module Xlsxrb
1256
1360
  else
1257
1361
  attrs = {}
1258
1362
  attrs[:height] = row.height if row.height
1259
- attrs[:hidden] = true if row.hidden
1260
- attrs[:custom_height] = true if row.custom_height
1363
+ attrs[:hidden] = true if row.hidden?
1364
+ attrs[:custom_height] = true if row.custom_height?
1261
1365
  attrs[:outline_level] = row.outline_level if row.outline_level
1262
1366
  style_idx = row.unmapped_data&.[](:style_index) || (row.respond_to?(:style_index) && row.style_index)
1263
1367
  attrs[:style_index] = style_idx if style_idx
@@ -1282,7 +1386,7 @@ module Xlsxrb
1282
1386
  page_setup: page_setup,
1283
1387
  header_footer: header_footer,
1284
1388
  row_breaks: row_breaks,
1285
- col_breaks: col_breaks,
1389
+ col_breaks: col_breaks || column_breaks,
1286
1390
  cell_watches: cell_watches,
1287
1391
  ignored_errors: ignored_errors,
1288
1392
  tables: tables,
@@ -1295,6 +1399,7 @@ module Xlsxrb
1295
1399
  end
1296
1400
 
1297
1401
  def build_table_xml(tbl, table_id)
1402
+ tbl = tbl.to_h if tbl.is_a?(Elements::MetadataRecord)
1298
1403
  io = StringIO.new
1299
1404
  b = XmlBuilder.new(io)
1300
1405
  b.declaration
@@ -1316,13 +1421,20 @@ module Xlsxrb
1316
1421
  columns = tbl[:columns] || []
1317
1422
  b.open_tag("tableColumns", { count: columns.size.to_s })
1318
1423
  columns.each_with_index do |col, ci|
1319
- col_name = col.is_a?(Hash) ? col[:name] : col
1424
+ col = col.to_h if col.is_a?(Elements::MetadataRecord)
1425
+ col_name = if col.is_a?(Hash)
1426
+ col[:name]
1427
+ elsif col.is_a?(Elements::TableColumn)
1428
+ col.name
1429
+ else
1430
+ col
1431
+ end
1320
1432
  col_attrs = { id: (ci + 1).to_s, name: col_name.to_s }
1321
1433
  calc_formula = nil
1322
1434
  tot_formula = nil
1323
- if col.is_a?(Hash)
1435
+ if col.is_a?(Hash) || col.is_a?(Elements::TableColumn)
1324
1436
  calc_formula = col[:calculated_column_formula] || col[:data_formula] || col[:formula]
1325
- tot_formula = col[:totals_row_formula] || col[:total_row_formula] || col[:total_formula]
1437
+ tot_formula = col[:totals_row_formula]
1326
1438
  trf = col[:totals_row_function] || ("custom" if tot_formula)
1327
1439
  col_attrs[:totalsRowFunction] = trf.to_s if trf
1328
1440
  col_attrs[:totalsRowLabel] = col[:totals_row_label].to_s if col[:totals_row_label]
@@ -1346,7 +1458,8 @@ module Xlsxrb
1346
1458
  # - style: "TableStyleMedium9"
1347
1459
  # - style: { name:, show_* }
1348
1460
  # and also show_* values at the table root for Facade compatibility.
1349
- raw_style = tbl[:style]
1461
+ raw_style = tbl[:style] || tbl[:style_info]
1462
+ raw_style = raw_style.to_h if raw_style.respond_to?(:to_h)
1350
1463
  style = if raw_style.is_a?(Hash)
1351
1464
  raw_style
1352
1465
  elsif raw_style
@@ -1419,7 +1532,8 @@ module Xlsxrb
1419
1532
  xmlns: "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties",
1420
1533
  "xmlns:vt": "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
1421
1534
  })
1422
- b.tag("Application") { |_| b.text(@app_properties[:application]) } if @app_properties[:application]
1535
+ app_val = @app_properties[:application] || @app_properties[:app_name]
1536
+ b.tag("Application") { |_| b.text(app_val) } if app_val
1423
1537
  b.tag("Company") { |_| b.text(@app_properties[:company]) } if @app_properties[:company]
1424
1538
  b.tag("Manager") { |_| b.text(@app_properties[:manager]) } if @app_properties[:manager]
1425
1539
  b.close_tag("Properties")
@@ -1434,7 +1548,26 @@ module Xlsxrb
1434
1548
  xmlns: "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
1435
1549
  "xmlns:vt": "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
1436
1550
  })
1437
- @custom_properties.each_with_index do |prop, idx|
1551
+ list = if @custom_properties.is_a?(Hash)
1552
+ @custom_properties.map do |name, val|
1553
+ if val.is_a?(Elements::CustomProperty) || (val.respond_to?(:name) && val.respond_to?(:value))
1554
+ { name: val.name || name.to_s, value: val.value, type: val.type }
1555
+ elsif val.is_a?(Hash)
1556
+ { name: val[:name] || name.to_s, value: val[:value], type: val[:type] }
1557
+ else
1558
+ { name: name.to_s, value: val, type: nil }
1559
+ end
1560
+ end
1561
+ else
1562
+ @custom_properties.map do |prop|
1563
+ if prop.is_a?(Elements::CustomProperty) || (prop.respond_to?(:name) && prop.respond_to?(:value))
1564
+ { name: prop.name, value: prop.value, type: prop.type }
1565
+ else
1566
+ prop
1567
+ end
1568
+ end
1569
+ end
1570
+ list.each_with_index do |prop, idx|
1438
1571
  b.open_tag("property", { fmtid: "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", pid: (idx + 2).to_s, name: prop[:name] })
1439
1572
  case prop[:type]
1440
1573
  when :number, :int, :i4
@@ -1455,7 +1588,7 @@ module Xlsxrb
1455
1588
  end
1456
1589
 
1457
1590
  def resolve_active_tab_index
1458
- active_tab = @workbook_properties&.[](:active_tab)
1591
+ active_tab = @workbook_properties&.[](:active_sheet_index) || @workbook_properties&.[](:active_sheet) || @workbook_properties&.[](:active_tab)
1459
1592
  if active_tab.nil?
1460
1593
  first_vis_idx = @sheets.find_index do |s|
1461
1594
  state = s.respond_to?(:state) ? s.state : s[:state]