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
@@ -3,6 +3,7 @@
3
3
  # rbs_inline: enabled
4
4
 
5
5
  require_relative "elements/types"
6
+ require_relative "elements/metadata"
6
7
  require_relative "elements/cell"
7
8
  require_relative "elements/row"
8
9
  require_relative "elements/column"
@@ -58,7 +58,11 @@ module Xlsxrb
58
58
  when "vertAlign"
59
59
  @current_font[:vert_align] = attributes["val"] if @inside_rpr && attributes["val"]
60
60
  when "sz"
61
- @current_font[:sz] = attributes["val"]&.to_f if @inside_rpr
61
+ if @inside_rpr
62
+ sz_val = attributes["val"]&.to_f
63
+ @current_font[:sz] = sz_val
64
+ @current_font[:size] = sz_val
65
+ end
62
66
  when "color"
63
67
  if @inside_rpr
64
68
  if attributes["rgb"]
@@ -228,7 +232,11 @@ module Xlsxrb
228
232
  when "vertAlign"
229
233
  @is_current_font[:vert_align] = attributes["val"] if @inside_is_rpr && attributes["val"]
230
234
  when "sz"
231
- @is_current_font[:sz] = attributes["val"]&.to_f if @inside_is_rpr
235
+ if @inside_is_rpr
236
+ sz_val = attributes["val"]&.to_f
237
+ @is_current_font[:sz] = sz_val
238
+ @is_current_font[:size] = sz_val
239
+ end
232
240
  when "color"
233
241
  if @inside_is_rpr
234
242
  if attributes["rgb"]
@@ -347,7 +355,7 @@ module Xlsxrb
347
355
  if !expr.empty?
348
356
  @shared_formula_masters[@formula_si] = { cell: @current_cell_ref, expression: expr }
349
357
  elsif (master = @shared_formula_masters[@formula_si])
350
- expr = Xlsxrb::Utils.expand_formula(master[:expression], from_ref: master[:cell], to_ref: @current_cell_ref)
358
+ expr = Xlsxrb.expand_formula(master[:expression], from_ref: master[:cell], to_ref: @current_cell_ref)
351
359
  end
352
360
  end
353
361
  @cells[@current_cell_ref] = Xlsxrb::Elements::Formula.new(
@@ -393,7 +401,7 @@ module Xlsxrb
393
401
  next unless val.is_a?(Xlsxrb::Elements::Formula) && val.type == :shared && (val.expression.nil? || val.expression.empty?)
394
402
  next unless val.shared_index && (master = @shared_formula_masters[val.shared_index])
395
403
 
396
- expanded = Xlsxrb::Utils.expand_formula(master[:expression], from_ref: master[:cell], to_ref: ref)
404
+ expanded = Xlsxrb.expand_formula(master[:expression], from_ref: master[:cell], to_ref: ref)
397
405
  @cells[ref] = val.with(expression: expanded)
398
406
  end
399
407
  end
@@ -576,7 +584,10 @@ module Xlsxrb
576
584
  lr = attributes["lockRevision"]
577
585
  prot[:lock_revision] = %w[1 true].include?(lr) unless lr.nil?
578
586
  wp = attributes["workbookPassword"]
579
- prot[:password] = wp if wp
587
+ if wp
588
+ prot[:password] = wp
589
+ prot[:workbook_password] = wp
590
+ end
580
591
  an = attributes["workbookAlgorithmName"]
581
592
  if an
582
593
  prot[:algorithm_name] = an
@@ -1037,7 +1037,7 @@ module Xlsxrb
1037
1037
  # Set paragraph text from concatenation of all run texts
1038
1038
  para[:text] = para[:runs].filter_map { |r| r[:text] }.join unless para[:text]
1039
1039
  # Set paragraph font from last run's font for backward compat
1040
- last_font = para[:runs].rfind { |r| r[:font] }&.dig(:font)
1040
+ last_font = para[:runs].reverse_each.find { |r| r[:font] }&.dig(:font)
1041
1041
  para[:font] = last_font if last_font && !para[:font]
1042
1042
  # Remove :runs key when only one run (simplify output)
1043
1043
  para.delete(:runs) if para[:runs].size <= 1
@@ -1859,7 +1859,7 @@ module Xlsxrb
1859
1859
  @inside_disp_units_lbl_ln = true
1860
1860
  if attributes["w"]
1861
1861
  du = @val_axis_disp_units
1862
- du = {} unless du.is_a?(Hash)
1862
+ du = du.is_a?(String) ? { built_in_unit: du } : {} unless du.is_a?(Hash)
1863
1863
  @val_axis_disp_units = du
1864
1864
  (du[:label] ||= {})[:line_width] = attributes["w"].to_i / 12_700.0
1865
1865
  end
@@ -1965,7 +1965,7 @@ module Xlsxrb
1965
1965
  end
1966
1966
  elsif @inside_disp_units_lbl_ln && attributes["val"]
1967
1967
  du = @val_axis_disp_units
1968
- du = {} unless du.is_a?(Hash)
1968
+ du = du.is_a?(String) ? { built_in_unit: du } : {} unless du.is_a?(Hash)
1969
1969
  @val_axis_disp_units = du
1970
1970
  (du[:label] ||= {})[:line_dash] = attributes["val"]
1971
1971
  elsif @inside_ax_sp_pr && @inside_ax_ln && attributes["val"]
@@ -2079,7 +2079,7 @@ module Xlsxrb
2079
2079
  @current_err_bars[:no_fill] = true
2080
2080
  elsif @inside_disp_units_lbl_sp_pr
2081
2081
  du = @val_axis_disp_units
2082
- du = {} unless du.is_a?(Hash)
2082
+ du = du.is_a?(String) ? { built_in_unit: du } : {} unless du.is_a?(Hash)
2083
2083
  @val_axis_disp_units = du
2084
2084
  (du[:label] ||= {})[:no_fill] = true
2085
2085
  elsif @inside_legend_sp_pr
@@ -3179,7 +3179,7 @@ module Xlsxrb
3179
3179
  when "dispUnitsLbl"
3180
3180
  if @disp_units_lbl_font
3181
3181
  du = @val_axis_disp_units
3182
- du = {} unless du.is_a?(Hash)
3182
+ du = du.is_a?(String) ? { built_in_unit: du } : {} unless du.is_a?(Hash)
3183
3183
  @val_axis_disp_units = du
3184
3184
  (du[:label] ||= {})[:font] = @disp_units_lbl_font
3185
3185
  end
@@ -3456,12 +3456,12 @@ module Xlsxrb
3456
3456
  end
3457
3457
  elsif @inside_disp_units_lbl_sp_pr && @inside_disp_units_lbl_ln && @inside_disp_units_lbl_solid_fill
3458
3458
  du = @val_axis_disp_units
3459
- du = {} unless du.is_a?(Hash)
3459
+ du = du.is_a?(String) ? { built_in_unit: du } : {} unless du.is_a?(Hash)
3460
3460
  @val_axis_disp_units = du
3461
3461
  (du[:label] ||= {})[:line_color] = color_value
3462
3462
  elsif @inside_disp_units_lbl_sp_pr && @inside_disp_units_lbl_solid_fill
3463
3463
  du = @val_axis_disp_units
3464
- du = {} unless du.is_a?(Hash)
3464
+ du = du.is_a?(String) ? { built_in_unit: du } : {} unless du.is_a?(Hash)
3465
3465
  @val_axis_disp_units = du
3466
3466
  (du[:label] ||= {})[:fill_color] = color_value
3467
3467
  elsif @inside_wall_sp_pr && @inside_wall_ln && @inside_wall_solid_fill && @current_wall
@@ -3529,7 +3529,8 @@ module Xlsxrb
3529
3529
  @text_buffer = +""
3530
3530
  when "comment"
3531
3531
  @inside_comment = true
3532
- @current_comment = { ref: attributes["ref"], author_id: attributes["authorId"]&.to_i }
3532
+ r = attributes["ref"]
3533
+ @current_comment = { ref: r, author_id: attributes["authorId"]&.to_i }
3533
3534
  @current_comment[:guid] = attributes["guid"] if attributes["guid"]
3534
3535
  sid = attributes["shapeId"]
3535
3536
  @current_comment[:shape_id] = sid.to_i if sid
@@ -3562,7 +3563,11 @@ module Xlsxrb
3562
3563
  when "vertAlign"
3563
3564
  @current_font[:vert_align] = attributes["val"] if @inside_rpr && attributes["val"]
3564
3565
  when "sz"
3565
- @current_font[:sz] = attributes["val"]&.to_f if @inside_rpr
3566
+ if @inside_rpr
3567
+ sz_val = attributes["val"]&.to_f
3568
+ @current_font[:sz] = sz_val
3569
+ @current_font[:size] = sz_val
3570
+ end
3566
3571
  when "color"
3567
3572
  if @inside_rpr
3568
3573
  if attributes["rgb"]
@@ -105,6 +105,7 @@ module Xlsxrb
105
105
  @inside_custom_filters = false
106
106
  when "filterColumn"
107
107
  if @current_col_id && @current_filter
108
+ @current_filter[:hidden] = true if @fc_hidden_button
108
109
  @current_filter[:hidden_button] = true if @fc_hidden_button
109
110
  @current_filter[:show_button] = false if @fc_show_button == false
110
111
  @filter_columns[@current_col_id] = @current_filter
@@ -227,7 +228,9 @@ module Xlsxrb
227
228
  name = element_name(local_name, qname)
228
229
  case name
229
230
  when "Application"
230
- @properties[:application] = @text_buffer.dup
231
+ val = @text_buffer.dup
232
+ @properties[:application] = val
233
+ @properties[:app_name] = val
231
234
  @current_field = nil
232
235
  when "AppVersion"
233
236
  @properties[:app_version] = @text_buffer.dup
@@ -462,6 +465,10 @@ module Xlsxrb
462
465
  @properties[:fit_to_page] = %w[1 true].include?(ftp) unless ftp.nil?
463
466
  apb = attributes["autoPageBreaks"]
464
467
  @properties[:auto_page_breaks] = %w[1 true].include?(apb) unless apb.nil?
468
+ @properties[:page_setup_properties] = {
469
+ fit_to_page: @properties[:fit_to_page],
470
+ auto_page_breaks: @properties[:auto_page_breaks]
471
+ }.compact
465
472
  end
466
473
  when "sheetCalcPr"
467
474
  fcol = attributes["fullCalcOnLoad"]
@@ -528,7 +535,9 @@ module Xlsxrb
528
535
  %w[1 true].include?(val)
529
536
  end
530
537
  end
531
- @protection = prot unless prot.empty?
538
+ # The sheet attribute defaults to false: without it the element only records settings.
539
+ prot[:sheet] = false unless attributes.key?("sheet")
540
+ @protection = prot
532
541
  end
533
542
 
534
543
  private
@@ -782,13 +791,22 @@ module Xlsxrb
782
791
  drh = attributes["defaultRowHeight"]
783
792
  @properties[:default_row_height] = drh.to_f if drh
784
793
  dcw = attributes["defaultColWidth"]
785
- @properties[:default_col_width] = dcw.to_f if dcw
794
+ if dcw
795
+ @properties[:default_column_width] = dcw.to_f
796
+ @properties[:default_col_width] = dcw.to_f
797
+ end
786
798
  bcw = attributes["baseColWidth"]
787
- @properties[:base_col_width] = bcw.to_i if bcw
799
+ if bcw
800
+ @properties[:base_column_width] = bcw.to_i
801
+ @properties[:base_col_width] = bcw.to_i
802
+ end
788
803
  olr = attributes["outlineLevelRow"]
789
804
  @properties[:outline_level_row] = olr.to_i if olr
790
805
  olc = attributes["outlineLevelCol"]
791
- @properties[:outline_level_col] = olc.to_i if olc
806
+ if olc
807
+ @properties[:outline_level_column] = olc.to_i
808
+ @properties[:outline_level_col] = olc.to_i
809
+ end
792
810
  ch = attributes["customHeight"]
793
811
  @properties[:custom_height] = true if %w[1 true].include?(ch)
794
812
  zh = attributes["zeroHeight"]
@@ -875,18 +893,23 @@ module Xlsxrb
875
893
  frozen = attributes["state"] == "frozen"
876
894
  tlc = attributes["topLeftCell"]
877
895
  ap = attributes["activePane"]
896
+ col_val = xs ? xs.to_i : 0
897
+ row_val = ys ? ys.to_i : 0
878
898
  p = if frozen
879
899
  {
880
- row: ys ? ys.to_i : 0,
881
- col: xs ? xs.to_i : 0,
900
+ row: row_val,
901
+ column: col_val,
902
+ col: col_val,
903
+ top_left_cell: tlc,
882
904
  state: :frozen
883
905
  }
884
906
  else
885
907
  {
886
- row: ys ? ys.to_i : 0,
887
- col: xs ? xs.to_i : 0,
888
- x_split: xs ? xs.to_i : 0,
889
- y_split: ys ? ys.to_i : 0,
908
+ row: row_val,
909
+ column: col_val,
910
+ col: col_val,
911
+ x_split: col_val,
912
+ y_split: row_val,
890
913
  top_left_cell: tlc,
891
914
  state: :split
892
915
  }
@@ -956,10 +979,10 @@ module Xlsxrb
956
979
  @current_dv[:allow_blank] = true if attributes["allowBlank"] == "1"
957
980
  @current_dv[:show_input_message] = true if attributes["showInputMessage"] == "1"
958
981
  @current_dv[:show_error_message] = true if attributes["showErrorMessage"] == "1"
959
- @current_dv[:error_title] = xml_unescape(attributes["errorTitle"]) if attributes["errorTitle"]
960
- @current_dv[:error] = xml_unescape(attributes["error"]) if attributes["error"]
961
- @current_dv[:prompt_title] = xml_unescape(attributes["promptTitle"]) if attributes["promptTitle"]
962
- @current_dv[:prompt] = xml_unescape(attributes["prompt"]) if attributes["prompt"]
982
+ @current_dv[:error_title] = attributes["errorTitle"] if attributes["errorTitle"]
983
+ @current_dv[:error] = attributes["error"] if attributes["error"]
984
+ @current_dv[:prompt_title] = attributes["promptTitle"] if attributes["promptTitle"]
985
+ @current_dv[:prompt] = attributes["prompt"] if attributes["prompt"]
963
986
  @current_dv[:show_drop_down] = true if attributes["showDropDown"] == "1"
964
987
  @current_dv[:ime_mode] = attributes["imeMode"] if attributes["imeMode"]
965
988
  when "formula1"
@@ -1005,10 +1028,6 @@ module Xlsxrb
1005
1028
  local_name
1006
1029
  end
1007
1030
  end
1008
-
1009
- def xml_unescape(str)
1010
- str.gsub("&amp;", "&").gsub("&lt;", "<").gsub("&gt;", ">").gsub("&quot;", '"').gsub("&apos;", "'")
1011
- end
1012
1031
  end
1013
1032
 
1014
1033
  # SAX2 listener for parsing conditionalFormatting elements.
@@ -1039,7 +1058,11 @@ module Xlsxrb
1039
1058
  @current_rule[:pivot] = true if @current_pivot
1040
1059
  @current_rule[:priority] = attributes["priority"].to_i if attributes["priority"]
1041
1060
  @current_rule[:operator] = attributes["operator"] if attributes["operator"]
1042
- @current_rule[:format_id] = attributes["dxfId"].to_i if attributes["dxfId"]
1061
+ if attributes["dxfId"]
1062
+ did = attributes["dxfId"].to_i
1063
+ @current_rule[:dxf_id] = did
1064
+ @current_rule[:format_id] = did
1065
+ end
1043
1066
  @current_rule[:stop_if_true] = true if attributes["stopIfTrue"] == "1"
1044
1067
  @current_rule[:above_average] = false if attributes["aboveAverage"] == "0"
1045
1068
  @current_rule[:equal_average] = true if attributes["equalAverage"] == "1"
@@ -1108,7 +1131,11 @@ module Xlsxrb
1108
1131
  name = element_name(local_name, qname)
1109
1132
  case name
1110
1133
  when "formula"
1111
- (@current_rule[:formulas] ||= []) << @text_buffer.dup if @current_rule
1134
+ if @current_rule
1135
+ val = @text_buffer.dup
1136
+ (@current_rule[:formulas] ||= []) << val
1137
+ @current_rule[:formula] = @current_rule[:formulas]
1138
+ end
1112
1139
  @inside_formula = false
1113
1140
  when "cfRule"
1114
1141
  @rules << @current_rule if @current_rule
@@ -1154,7 +1181,7 @@ module Xlsxrb
1154
1181
  class PrintPageListener
1155
1182
  include REXML::SAX2Listener
1156
1183
 
1157
- attr_reader :print_options, :page_margins, :page_setup, :header_footer, :row_breaks, :col_breaks
1184
+ attr_reader :print_options, :page_margins, :page_setup, :header_footer, :row_breaks, :col_breaks, :column_breaks
1158
1185
 
1159
1186
  def initialize
1160
1187
  @print_options = {}
@@ -1163,6 +1190,7 @@ module Xlsxrb
1163
1190
  @header_footer = {}
1164
1191
  @row_breaks = []
1165
1192
  @col_breaks = []
1193
+ @column_breaks = @col_breaks
1166
1194
  @inside_header_footer = false
1167
1195
  @inside_row_breaks = false
1168
1196
  @inside_col_breaks = false
@@ -1478,12 +1506,18 @@ module Xlsxrb
1478
1506
  @inside_pivot_field = false
1479
1507
  @inside_items = false
1480
1508
  @current_items = []
1509
+ @definition_depth = 0
1481
1510
  end
1482
1511
 
1483
1512
  def start_element(_uri, local_name, qname, attributes)
1484
1513
  name = element_name(local_name, qname)
1485
1514
  case name
1486
1515
  when "pivotTableDefinition"
1516
+ # Excel 2010 and later also write an x14:pivotTableDefinition inside extLst;
1517
+ # only the outermost element describes the pivot table.
1518
+ @definition_depth += 1
1519
+ return if @definition_depth > 1
1520
+
1487
1521
  @pivot_table = {
1488
1522
  name: attributes["name"],
1489
1523
  cache_id: attributes["cacheId"]&.to_i
@@ -1491,7 +1525,11 @@ module Xlsxrb
1491
1525
  @pivot_table[:data_caption] = attributes["dataCaption"] if attributes["dataCaption"]
1492
1526
  @pivot_table[:data_on_rows] = attributes["dataOnRows"] == "1" if attributes["dataOnRows"]
1493
1527
  @pivot_table[:row_grand_totals] = attributes["rowGrandTotals"] != "0" if attributes["rowGrandTotals"]
1494
- @pivot_table[:col_grand_totals] = attributes["colGrandTotals"] != "0" if attributes["colGrandTotals"]
1528
+ if attributes["colGrandTotals"]
1529
+ cgt = attributes["colGrandTotals"] != "0"
1530
+ @pivot_table[:column_grand_totals] = cgt
1531
+ @pivot_table[:col_grand_totals] = cgt
1532
+ end
1495
1533
  @pivot_table[:compact] = attributes["compact"] != "0" if attributes["compact"]
1496
1534
  @pivot_table[:outline] = attributes["outline"] != "0" if attributes["outline"]
1497
1535
  @pivot_table[:outline_data] = attributes["outlineData"] == "1" if attributes["outlineData"]
@@ -1543,8 +1581,15 @@ module Xlsxrb
1543
1581
  @pivot_table[:page_wrap] = pw.to_i if pw
1544
1582
  when "location"
1545
1583
  @pivot_table[:ref] = attributes["ref"] if @pivot_table
1584
+ { "firstHeaderRow" => :first_header_row, "firstDataRow" => :first_data_row, "firstDataCol" => :first_data_col }.each do |attr, key|
1585
+ @pivot_table[key] = attributes[attr].to_i if @pivot_table && attributes[attr]
1586
+ end
1546
1587
  @pivot_table[:row_page_count] = attributes["rowPageCount"]&.to_i if attributes["rowPageCount"]
1547
- @pivot_table[:col_page_count] = attributes["colPageCount"]&.to_i if attributes["colPageCount"]
1588
+ if attributes["colPageCount"]
1589
+ cpc = attributes["colPageCount"].to_i
1590
+ @pivot_table[:column_page_count] = cpc
1591
+ @pivot_table[:col_page_count] = cpc
1592
+ end
1548
1593
  when "pivotField"
1549
1594
  @inside_pivot_field = true
1550
1595
  @current_field = {}
@@ -1601,11 +1646,19 @@ module Xlsxrb
1601
1646
  srh = attributes["showRowHeaders"]
1602
1647
  psi[:show_row_headers] = srh == "1" unless srh.nil?
1603
1648
  sch = attributes["showColHeaders"]
1604
- psi[:show_col_headers] = sch == "1" unless sch.nil?
1649
+ unless sch.nil?
1650
+ val = sch == "1"
1651
+ psi[:show_column_headers] = val
1652
+ psi[:show_col_headers] = val
1653
+ end
1605
1654
  srs = attributes["showRowStripes"]
1606
1655
  psi[:show_row_stripes] = srs == "1" unless srs.nil?
1607
1656
  scs = attributes["showColStripes"]
1608
- psi[:show_col_stripes] = scs == "1" unless scs.nil?
1657
+ unless scs.nil?
1658
+ val = scs == "1"
1659
+ psi[:show_column_stripes] = val
1660
+ psi[:show_col_stripes] = val
1661
+ end
1609
1662
  slc = attributes["showLastColumn"]
1610
1663
  psi[:show_last_column] = slc == "1" unless slc.nil?
1611
1664
  @pivot_table[:pivot_table_style] = psi
@@ -1623,9 +1676,11 @@ module Xlsxrb
1623
1676
  when "items"
1624
1677
  @inside_items = false
1625
1678
  when "pivotTableDefinition"
1626
- if @pivot_table
1679
+ @definition_depth -= 1
1680
+ if @pivot_table && @definition_depth.zero?
1627
1681
  @pivot_table[:fields] = @fields
1628
1682
  @pivot_table[:row_fields] = @row_fields
1683
+ @pivot_table[:column_fields] = @col_fields
1629
1684
  @pivot_table[:col_fields] = @col_fields
1630
1685
  @pivot_table[:data_fields] = @data_fields
1631
1686
  end
@@ -1687,7 +1742,7 @@ module Xlsxrb
1687
1742
  @current_field[:name] = attributes["name"] if attributes["name"]
1688
1743
  @current_field[:num_fmt_id] = attributes["numFmtId"]&.to_i if attributes["numFmtId"]
1689
1744
  @current_field[:caption] = attributes["caption"] if attributes["caption"]
1690
- @current_field[:formula] = xml_unescape(attributes["formula"]) if attributes["formula"]
1745
+ @current_field[:formula] = attributes["formula"] if attributes["formula"]
1691
1746
  when "sharedItems"
1692
1747
  @current_shared_items = [] if @current_field
1693
1748
  when "s", "d", "e"
@@ -1730,10 +1785,6 @@ module Xlsxrb
1730
1785
  local_name
1731
1786
  end
1732
1787
  end
1733
-
1734
- def xml_unescape(str)
1735
- str.gsub("&amp;", "&").gsub("&lt;", "<").gsub("&gt;", ">").gsub("&quot;", '"').gsub("&apos;", "'")
1736
- end
1737
1788
  end
1738
1789
 
1739
1790
  # SAX2 listener for parsing pivotCacheRecords XML.
@@ -1866,14 +1917,20 @@ module Xlsxrb
1866
1917
  name = element_name(local_name, qname)
1867
1918
  case name
1868
1919
  when "threadedComment"
1920
+ timestamp = attributes["dT"] || attributes["dt"]
1869
1921
  @current_comment = {
1870
1922
  ref: attributes["ref"],
1871
- dt: attributes["dT"] || attributes["dt"],
1923
+ dt: timestamp,
1924
+ created_at: timestamp,
1872
1925
  person_id: attributes["personId"],
1926
+ author_id: attributes["personId"],
1873
1927
  id: attributes["id"]
1874
1928
  }
1875
1929
  @current_comment[:parent_id] = attributes["parentId"] if attributes["parentId"]
1876
- @current_comment[:done] = %w[1 true].include?(attributes["done"]) if attributes.key?("done")
1930
+ if attributes.key?("done")
1931
+ @current_comment[:done] = %w[1 true].include?(attributes["done"])
1932
+ @current_comment[:resolved] = @current_comment[:done]
1933
+ end
1877
1934
  when "text"
1878
1935
  if @current_comment
1879
1936
  @inside_text = true
@@ -1910,6 +1967,211 @@ module Xlsxrb
1910
1967
  end
1911
1968
  end
1912
1969
  end
1970
+
1971
+ # SAX2 listener for parsing <x14:sparklineGroups> and child <x14:sparklineGroup> elements.
1972
+ class SparklineGroupsListener
1973
+ include REXML::SAX2Listener
1974
+
1975
+ COLOR_TAG_MAP = {
1976
+ "colorSeries" => :color_series,
1977
+ "colorNegative" => :color_negative,
1978
+ "colorAxis" => :color_axis,
1979
+ "colorMarkers" => :color_markers,
1980
+ "colorFirst" => :color_first,
1981
+ "colorLast" => :color_last,
1982
+ "colorHigh" => :color_high,
1983
+ "colorLow" => :color_low
1984
+ }.freeze
1985
+
1986
+ # @return [Array<Hash[Symbol, untyped]>]
1987
+ #: Array[Hash[Symbol, untyped]]
1988
+ attr_reader :groups
1989
+ alias sparkline_groups groups
1990
+
1991
+ #: () -> void
1992
+ def initialize
1993
+ @groups = []
1994
+ @current_group = nil
1995
+ @current_sparkline = nil
1996
+ @inside_f = false
1997
+ @inside_sqref = false
1998
+ @text_buffer = +""
1999
+ end
2000
+
2001
+ #: (untyped _uri, String? local_name, String? qname, Hash[String, String] attributes) -> void
2002
+ def start_element(_uri, local_name, qname, attributes)
2003
+ name = element_name(local_name, qname)
2004
+ case name
2005
+ when "sparklineGroup"
2006
+ disp = attributes["displayEmptyCellsAs"]
2007
+ max_attr = attributes["manualMax"]
2008
+ min_attr = attributes["manualMin"]
2009
+ lw_attr = attributes["lineWeight"]
2010
+ max = parse_numeric(max_attr)
2011
+ min = parse_numeric(min_attr)
2012
+ line_weight = parse_numeric(lw_attr)
2013
+
2014
+ @current_group = {
2015
+ type: attributes["type"] || "line",
2016
+ display_empty: disp,
2017
+ display_empty_cells_as: disp,
2018
+ markers: %w[1 true].include?(attributes["markers"]),
2019
+ high: %w[1 true].include?(attributes["high"]),
2020
+ low: %w[1 true].include?(attributes["low"]),
2021
+ first: %w[1 true].include?(attributes["first"]),
2022
+ last: %w[1 true].include?(attributes["last"]),
2023
+ negative: %w[1 true].include?(attributes["negative"]),
2024
+ max: max,
2025
+ manual_max: max,
2026
+ min: min,
2027
+ manual_min: min,
2028
+ line_weight: line_weight,
2029
+ sparklines: []
2030
+ }
2031
+ when "colorSeries", "colorNegative", "colorAxis", "colorMarkers", "colorFirst", "colorLast", "colorHigh", "colorLow"
2032
+ if @current_group
2033
+ color = parse_color(attributes)
2034
+ key = COLOR_TAG_MAP[name]
2035
+ @current_group[key] = color if color && key
2036
+ end
2037
+ when "sparkline"
2038
+ @current_sparkline = {}
2039
+ when "f"
2040
+ if @current_sparkline
2041
+ @inside_f = true
2042
+ @text_buffer = +""
2043
+ end
2044
+ when "sqref"
2045
+ if @current_sparkline
2046
+ @inside_sqref = true
2047
+ @text_buffer = +""
2048
+ end
2049
+ end
2050
+ end
2051
+
2052
+ #: (String text) -> void
2053
+ def characters(text)
2054
+ @text_buffer << text if @inside_f || @inside_sqref
2055
+ end
2056
+
2057
+ #: (untyped _uri, String? local_name, String? qname) -> void
2058
+ def end_element(_uri, local_name, qname)
2059
+ name = element_name(local_name, qname)
2060
+ case name
2061
+ when "f"
2062
+ if @current_sparkline && @inside_f
2063
+ val = @text_buffer.strip
2064
+ @current_sparkline[:data_ref] = val
2065
+ @current_sparkline[:formula] = val
2066
+ @inside_f = false
2067
+ end
2068
+ when "sqref"
2069
+ if @current_sparkline && @inside_sqref
2070
+ val = @text_buffer.strip
2071
+ @current_sparkline[:location_ref] = val
2072
+ @current_sparkline[:sqref] = val
2073
+ @inside_sqref = false
2074
+ end
2075
+ when "sparkline"
2076
+ if @current_group && @current_sparkline
2077
+ @current_group[:sparklines] << @current_sparkline
2078
+ @current_sparkline = nil
2079
+ end
2080
+ when "sparklineGroup"
2081
+ if @current_group
2082
+ @groups << @current_group
2083
+ @current_group = nil
2084
+ end
2085
+ end
2086
+ end
2087
+
2088
+ private
2089
+
2090
+ #: (String? str) -> (Integer | Float | nil)
2091
+ def parse_numeric(str)
2092
+ return nil if str.nil? || str.empty?
2093
+
2094
+ str.include?(".") ? str.to_f : str.to_i
2095
+ end
2096
+
2097
+ #: (Hash[String, String] attributes) -> (String | Hash[Symbol, untyped] | nil)
2098
+ def parse_color(attributes)
2099
+ if attributes["rgb"]
2100
+ attributes["rgb"]
2101
+ elsif attributes["theme"] || attributes["tint"]
2102
+ c = {}
2103
+ c[:theme] = attributes["theme"].to_i if attributes["theme"]
2104
+ c[:tint] = attributes["tint"].to_f if attributes["tint"]
2105
+ c
2106
+ end
2107
+ end
2108
+
2109
+ #: (String? local_name, String? qname) -> String
2110
+ def element_name(local_name, qname)
2111
+ if local_name.nil? || local_name.empty?
2112
+ qname.to_s.split(":").last
2113
+ else
2114
+ local_name
2115
+ end
2116
+ end
2117
+ end
2118
+
2119
+ # SAX2 listener for parsing VML drawings of header and footer images (<v:shape> and <v:imagedata>).
2120
+ class VmlHeaderFooterListener
2121
+ include REXML::SAX2Listener
2122
+
2123
+ # @return [Array<Hash[Symbol, untyped]>]
2124
+ #: Array[Hash[Symbol, untyped]]
2125
+ attr_reader :shapes
2126
+
2127
+ #: () -> void
2128
+ def initialize
2129
+ @shapes = []
2130
+ @current_shape = nil
2131
+ end
2132
+
2133
+ #: (untyped _uri, String? local_name, String? qname, Hash[String, String] attributes) -> void
2134
+ def start_element(_uri, local_name, qname, attributes)
2135
+ name = element_name(local_name, qname)
2136
+ case name
2137
+ when "shape"
2138
+ @current_shape = {
2139
+ id: attributes["id"]
2140
+ }
2141
+ when "imagedata"
2142
+ if @current_shape
2143
+ rel_id = attributes["o:relid"] || attributes["relid"] || attributes["r:id"] ||
2144
+ attributes.find { |k, _| k.to_s.end_with?(":relid") }&.last ||
2145
+ attributes.find { |k, _| k.to_s.end_with?(":id") }&.last
2146
+ title = attributes["o:title"] || attributes["title"] ||
2147
+ attributes.find { |k, _| k.to_s.end_with?(":title") }&.last
2148
+ @current_shape[:rel_id] = rel_id
2149
+ @current_shape[:title] = title if title
2150
+ end
2151
+ end
2152
+ end
2153
+
2154
+ #: (untyped _uri, String? local_name, String? qname) -> void
2155
+ def end_element(_uri, local_name, qname)
2156
+ name = element_name(local_name, qname)
2157
+ case name
2158
+ when "shape"
2159
+ @shapes << @current_shape if @current_shape && @current_shape[:rel_id]
2160
+ @current_shape = nil
2161
+ end
2162
+ end
2163
+
2164
+ private
2165
+
2166
+ #: (String? local_name, String? qname) -> String
2167
+ def element_name(local_name, qname)
2168
+ if local_name.nil? || local_name.empty?
2169
+ qname.to_s.split(":").last
2170
+ else
2171
+ local_name
2172
+ end
2173
+ end
2174
+ end
1913
2175
  end
1914
2176
  end
1915
2177
  end