xlsxrb 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +88 -0
  3. data/README.md +25 -3
  4. data/lib/xlsxrb/dsl_helpers.rb +65 -1
  5. data/lib/xlsxrb/elements/styles.rb +15 -6
  6. data/lib/xlsxrb/elements/worksheet.rb +2 -2
  7. data/lib/xlsxrb/number_formatter.rb +26 -8
  8. data/lib/xlsxrb/ooxml/cfb.rb +11 -3
  9. data/lib/xlsxrb/ooxml/crypto/agile.rb +48 -10
  10. data/lib/xlsxrb/ooxml/crypto/standard.rb +5 -1
  11. data/lib/xlsxrb/ooxml/crypto.rb +3 -0
  12. data/lib/xlsxrb/ooxml/shared_strings_parser.rb +26 -8
  13. data/lib/xlsxrb/ooxml/utils.rb +13 -0
  14. data/lib/xlsxrb/ooxml/workbook_writer.rb +43 -21
  15. data/lib/xlsxrb/ooxml/worksheet_parser.rb +116 -12
  16. data/lib/xlsxrb/ooxml/worksheet_writer.rb +338 -259
  17. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +206 -201
  18. data/lib/xlsxrb/ooxml/writer/features_xml.rb +109 -102
  19. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +46 -46
  20. data/lib/xlsxrb/ooxml/writer.rb +167 -160
  21. data/lib/xlsxrb/ooxml/xml_builder.rb +35 -7
  22. data/lib/xlsxrb/ooxml/xml_parser.rb +34 -1
  23. data/lib/xlsxrb/ooxml/zip_reader.rb +95 -68
  24. data/lib/xlsxrb/ooxml/zip_writer.rb +16 -0
  25. data/lib/xlsxrb/stream_row.rb +2 -2
  26. data/lib/xlsxrb/stream_sheet.rb +26 -11
  27. data/lib/xlsxrb/stream_writer.rb +212 -86
  28. data/lib/xlsxrb/style_builder.rb +339 -0
  29. data/lib/xlsxrb/version.rb +1 -1
  30. data/lib/xlsxrb/workbook_builder.rb +239 -88
  31. data/lib/xlsxrb/worksheet_builder.rb +847 -139
  32. data/lib/xlsxrb.rb +87 -21
  33. data/sig/generated/xlsxrb/dsl_helpers.rbs +37 -0
  34. data/sig/generated/xlsxrb/elements/styles.rbs +5 -0
  35. data/sig/generated/xlsxrb/number_formatter.rbs +7 -0
  36. data/sig/generated/xlsxrb/ooxml/cfb.rbs +2 -0
  37. data/sig/generated/xlsxrb/ooxml/crypto/agile.rbs +10 -0
  38. data/sig/generated/xlsxrb/ooxml/shared_strings_parser.rbs +7 -0
  39. data/sig/generated/xlsxrb/ooxml/utils.rbs +10 -0
  40. data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +13 -0
  41. data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +40 -0
  42. data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +53 -0
  43. data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +3 -0
  44. data/sig/generated/xlsxrb/ooxml/writer.rbs +4 -0
  45. data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +19 -2
  46. data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +10 -0
  47. data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +12 -1
  48. data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -0
  49. data/sig/generated/xlsxrb/stream_row.rbs +2 -2
  50. data/sig/generated/xlsxrb/stream_sheet.rbs +9 -0
  51. data/sig/generated/xlsxrb/stream_writer.rbs +46 -6
  52. data/sig/generated/xlsxrb/style_builder.rbs +261 -0
  53. data/sig/generated/xlsxrb/workbook_builder.rbs +42 -9
  54. data/sig/generated/xlsxrb/worksheet_builder.rbs +618 -4
  55. data/sig/generated/xlsxrb.rbs +53 -8
  56. metadata +9 -3
@@ -84,6 +84,7 @@ module Xlsxrb
84
84
  @sheet_entry_started = false
85
85
  @current_row_writer = nil
86
86
  @current_columns = []
87
+ @current_column_style_names = {} # column index => style name, for date style variants
87
88
  @current_charts = []
88
89
  @current_hyperlinks = []
89
90
  @current_auto_filter = nil
@@ -127,6 +128,7 @@ module Xlsxrb
127
128
  @styles = {} # { style_name => StyleBuilder }
128
129
  @style_writer = Ooxml::Writer.new(default_font: default_font)
129
130
  @style_name_to_id = {}
131
+ @time_like_style_ids = {} # cell style id => style id for Time-like values, see time_like_style_id
130
132
  style("__xlsxrb_date", number_format: DslHelpers::DATE_NUMBER_FORMAT)
131
133
  style("__xlsxrb_time", number_format: DslHelpers::TIME_NUMBER_FORMAT)
132
134
  # Workbook-level settings
@@ -212,6 +214,8 @@ module Xlsxrb
212
214
  # @yield [style_builder]
213
215
  # @yieldparam style_builder [Xlsxrb::StyleBuilder]
214
216
  # @return [StyleBuilder]
217
+ # Using the returned value is deprecated and warns; in version 0.3.0, this method
218
+ # will return nil. Configure the style with options or in the block.
215
219
  #: (String | Symbol name, **untyped opts) ?{ (StyleBuilder) -> void } -> StyleBuilder
216
220
  def style(name, **opts)
217
221
  style_name = name.to_s
@@ -222,8 +226,14 @@ module Xlsxrb
222
226
 
223
227
  # Register immediately with low-level style writer
224
228
  @style_name_to_id[style_name] = style_builder.register_with(@style_writer)
229
+ # Date and time variants of an earlier definition do not apply to this one.
230
+ @style_name_to_id.delete("#{style_name}__xlsxrb_date")
231
+ @style_name_to_id.delete("#{style_name}__xlsxrb_time")
232
+ # Column styles of the current sheet that use this name resolve to the new id.
233
+ @current_row_writer&.forget_column_style_ids
234
+ @time_like_style_ids.clear
225
235
 
226
- style_builder
236
+ ReturnedStyleBuilder.new(style_builder)
227
237
  end
228
238
 
229
239
  # Sets or configures the workbook-wide default font.
@@ -341,7 +351,7 @@ module Xlsxrb
341
351
  # @api public
342
352
  #: (String | Symbol name, **untyped opts) ?{ (Xlsxrb::StyleBuilder) -> void } -> Xlsxrb::StyleBuilder
343
353
  def style(...)
344
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
354
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
345
355
 
346
356
  @writer.style(...)
347
357
  end
@@ -356,7 +366,7 @@ module Xlsxrb
356
366
  # @api public
357
367
  #: (String range) -> void
358
368
  def merge(...)
359
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
369
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
360
370
 
361
371
  @writer.merge(...)
362
372
  end
@@ -377,7 +387,7 @@ module Xlsxrb
377
387
  # @api public
378
388
  #: (?preset: String, ?text: String?, ?from_column: Integer?, ?from_row: Integer, ?to_column: Integer?, ?to_row: Integer, **untyped opts) -> void
379
389
  def shape(...)
380
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
390
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
381
391
 
382
392
  @writer.shape(...)
383
393
  end
@@ -402,7 +412,7 @@ module Xlsxrb
402
412
  # @api public
403
413
  #: (Array[untyped] | Hash[untyped, untyped] values, ?cell_styles: untyped, ?row_style: untyped, ?height: Float | Integer | nil, ?hidden: bool, ?custom_height: bool, ?collapsed: bool, ?outline_level: Integer | nil) -> void
404
414
  def row(values, cell_styles: nil, row_style: nil, height: nil, hidden: false, custom_height: false, collapsed: false, outline_level: nil)
405
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
415
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
406
416
 
407
417
  @writer.row(values, cell_styles: cell_styles, row_style: row_style, height: height, hidden: hidden, custom_height: custom_height, collapsed: collapsed, outline_level: outline_level)
408
418
  end
@@ -431,7 +441,7 @@ module Xlsxrb
431
441
  # @api public
432
442
  #: (?Integer count) -> void
433
443
  def skip_rows(count = 1)
434
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
444
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
435
445
 
436
446
  @writer.skip_rows(count)
437
447
  end
@@ -453,7 +463,7 @@ module Xlsxrb
453
463
  # @api public
454
464
  #: (Integer | String | Symbol | Range[Integer | String | Symbol] | Array[Integer | String | Symbol] index, ?width: Float | Integer | nil, ?hidden: bool, ?custom_width: bool, ?outline_level: Integer | nil, ?collapsed: bool, ?style: (Integer | Symbol | String | Hash[Symbol, untyped])?, ?best_fit: bool) -> void
455
465
  def column(...)
456
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
466
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
457
467
 
458
468
  @writer.column(...)
459
469
  end
@@ -470,7 +480,7 @@ module Xlsxrb
470
480
  # @api public
471
481
  #: (Integer | String | Range[Integer | String] | Array[Integer | String] index, ?level: Integer, ?collapsed: bool, ?hidden: bool, ?width: Float | Integer | nil, ?style: untyped) -> void
472
482
  def group_columns(...)
473
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
483
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
474
484
 
475
485
  @writer.group_columns(...)
476
486
  end
@@ -486,7 +496,7 @@ module Xlsxrb
486
496
  # @api public
487
497
  #: (?untyped rows, ?level: Integer, ?collapsed: bool, ?hidden: bool) ?{ (self) -> void } -> void
488
498
  def group_rows(rows = nil, level: 1, collapsed: false, hidden: false)
489
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
499
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
490
500
 
491
501
  if block_given?
492
502
  @writer.group_rows(rows, level: level, collapsed: collapsed, hidden: hidden) do
@@ -509,7 +519,7 @@ module Xlsxrb
509
519
  #: () -> Hash[Symbol, untyped]
510
520
  #: (?summary_below: bool?, ?summary_right: bool?, ?show_outline_symbols: bool?, ?apply_styles: bool?, **untyped opts) -> void
511
521
  def outline_properties(...)
512
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
522
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
513
523
 
514
524
  @writer.outline_properties(...)
515
525
  end
@@ -521,7 +531,7 @@ module Xlsxrb
521
531
  # @api public
522
532
  #: (Hash[Symbol, untyped] opts) -> void
523
533
  def outline_properties=(opts)
524
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
534
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
525
535
 
526
536
  @writer.outline_properties = opts
527
537
  end
@@ -542,7 +552,7 @@ module Xlsxrb
542
552
  # @api public
543
553
  #: (?Symbol | String? type, **untyped options) ?{ (ChartBuilder) -> void } -> void
544
554
  def chart(...)
545
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
555
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
546
556
 
547
557
  @writer.chart(...)
548
558
  end
@@ -564,7 +574,7 @@ module Xlsxrb
564
574
  # @api public
565
575
  #: (String | Integer cell, ?(String | Elements::Hyperlink)? url, ?display: String?, ?tooltip: String?, ?location: String?) -> void
566
576
  def hyperlink(...)
567
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
577
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
568
578
 
569
579
  @writer.hyperlink(...)
570
580
  end
@@ -575,7 +585,7 @@ module Xlsxrb
575
585
  # @api public
576
586
  #: () -> String?
577
587
  def auto_filter
578
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
588
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
579
589
 
580
590
  @writer.auto_filter
581
591
  end
@@ -587,7 +597,7 @@ module Xlsxrb
587
597
  # @api public
588
598
  #: (String? range) -> String?
589
599
  def auto_filter=(range)
590
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
600
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
591
601
 
592
602
  @writer.auto_filter = range
593
603
  end
@@ -603,7 +613,7 @@ module Xlsxrb
603
613
  # @api public
604
614
  #: (Integer | String | Symbol col_id, (Hash[untyped, untyped] | Array[untyped]) filter) -> void
605
615
  def filter_column(...)
606
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
616
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
607
617
 
608
618
  @writer.filter_column(...)
609
619
  end
@@ -621,7 +631,7 @@ module Xlsxrb
621
631
  #: () -> Elements::SortState?
622
632
  #: (?String? ref, ?Array[Hash[untyped, untyped]]? sort_conditions, **untyped opts) -> void
623
633
  def sort_state(...)
624
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
634
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
625
635
 
626
636
  @writer.sort_state(...)
627
637
  end
@@ -633,7 +643,7 @@ module Xlsxrb
633
643
  # @api public
634
644
  #: (Elements::SortState | Hash[Symbol, untyped] | nil opts) -> void
635
645
  def sort_state=(opts)
636
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
646
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
637
647
 
638
648
  @writer.sort_state = opts
639
649
  end
@@ -657,7 +667,7 @@ module Xlsxrb
657
667
  # @api public
658
668
  #: (?String? range, ?type: (String | Symbol)?, **untyped opts) ?{ (DataValidationBuilder) -> void } -> void
659
669
  def data_validation(...)
660
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
670
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
661
671
 
662
672
  @writer.data_validation(...)
663
673
  end
@@ -681,7 +691,7 @@ module Xlsxrb
681
691
  # @api public
682
692
  #: (?String? sqref, ?type: String | Symbol, **untyped opts) ?{ (ConditionalFormatBuilder) -> void } -> void
683
693
  def conditional_format(sqref = nil, **opts, &)
684
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
694
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
685
695
 
686
696
  @writer.conditional_format(sqref, **opts, &)
687
697
  end
@@ -708,7 +718,7 @@ module Xlsxrb
708
718
  # @api public
709
719
  #: (?String? ref, ?columns: untyped, ?name: String?, ?display_name: String?, ?style: String?, **untyped opts) ?{ (TableBuilder) -> void } -> void
710
720
  def table(...)
711
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
721
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
712
722
 
713
723
  @writer.table(...)
714
724
  end
@@ -726,7 +736,7 @@ module Xlsxrb
726
736
  # @api public
727
737
  #: (String | Integer cell, String text, ?author: String, ?visible: bool) -> void
728
738
  def comment(...)
729
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
739
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
730
740
 
731
741
  @writer.comment(...)
732
742
  end
@@ -741,7 +751,7 @@ module Xlsxrb
741
751
  # @api public
742
752
  #: (String | Integer cell, String text, ?author: untyped, ?timestamp: untyped, ?id: String?, ?parent_id: String?, ?done: bool?, ?user_id: String?, ?provider_id: String?) -> void
743
753
  def threaded_comment(...)
744
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
754
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
745
755
 
746
756
  @writer.threaded_comment(...)
747
757
  end
@@ -761,7 +771,7 @@ module Xlsxrb
761
771
  # @api public
762
772
  #: (String source_ref, row_fields: untyped, data_fields: untyped, ?column_fields: untyped, ?ref: String?, ?name: String?, ?field_names: untyped, ?items: untyped, **untyped opts) -> void
763
773
  def pivot_table(...)
764
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
774
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
765
775
 
766
776
  @writer.pivot_table(...)
767
777
  end
@@ -778,7 +788,7 @@ module Xlsxrb
778
788
  # @api public
779
789
  #: (sparklines: Array[Hash[Symbol, untyped]], ?type: (String | Symbol)?, **untyped opts) -> void
780
790
  def sparkline_group(...)
781
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
791
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
782
792
 
783
793
  @writer.sparkline_group(...)
784
794
  end
@@ -801,7 +811,7 @@ module Xlsxrb
801
811
  # @api public
802
812
  #: (Symbol | String new_state) -> Symbol
803
813
  def state=(new_state)
804
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
814
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
805
815
 
806
816
  @writer.state = new_state
807
817
  @writer.state
@@ -823,7 +833,7 @@ module Xlsxrb
823
833
  # @api public
824
834
  #: (String | Symbol | Hash[Symbol, untyped] | nil spec) -> (String | Hash[Symbol, untyped] | nil)
825
835
  def tab_color=(spec)
826
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
836
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
827
837
 
828
838
  @writer.tab_color = spec
829
839
  end
@@ -836,7 +846,7 @@ module Xlsxrb
836
846
  #: () -> Elements::SheetProperties
837
847
  #: (**untyped opts) -> void
838
848
  def sheet_properties(**opts)
839
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
849
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
840
850
 
841
851
  @writer.sheet_properties(**opts)
842
852
  end
@@ -847,7 +857,7 @@ module Xlsxrb
847
857
  # @api public
848
858
  #: (Hash[Symbol, untyped] | Elements::SheetProperties opts) -> void
849
859
  def sheet_properties=(opts)
850
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
860
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
851
861
 
852
862
  @writer.sheet_properties = opts
853
863
  end
@@ -867,7 +877,7 @@ module Xlsxrb
867
877
  # @api public
868
878
  #: (String name, ?untyped value, ?hidden: bool, ?columns: untyped, ?rows: untyped, ?range: untyped, ?sheet: String?, ?scope: (String | Symbol)?) -> void
869
879
  def defined_name(name, value = nil, hidden: false, columns: nil, rows: nil, range: nil, sheet: nil, scope: nil)
870
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
880
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
871
881
 
872
882
  ref_sheet = sheet || @sheet_name
873
883
  local_sheet = if scope
@@ -894,7 +904,7 @@ module Xlsxrb
894
904
  #: () -> Elements::FreezePane?
895
905
  #: (?row: Integer | nil, ?column: (Integer | String)?, ?top_left_cell: String?, ?active_pane: (String | Symbol)?) -> void
896
906
  def freeze_pane(...)
897
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
907
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
898
908
 
899
909
  @writer.freeze_pane(...)
900
910
  end
@@ -906,7 +916,7 @@ module Xlsxrb
906
916
  # @api public
907
917
  #: (Elements::FreezePane | Hash[Symbol, untyped] | nil opts) -> void
908
918
  def freeze_pane=(opts)
909
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
919
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
910
920
 
911
921
  @writer.freeze_pane = opts
912
922
  end
@@ -927,7 +937,7 @@ module Xlsxrb
927
937
  # @api public
928
938
  #: (String? range) -> String?
929
939
  def print_area=(range)
930
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
940
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
931
941
 
932
942
  @writer.send(:set_print_area, range, sheet: @sheet_name)
933
943
  end
@@ -944,7 +954,7 @@ module Xlsxrb
944
954
  #: () -> Elements::PrintTitles?
945
955
  #: (?rows: (String | Integer | Range[Integer])?, ?columns: (String | Integer | Symbol | Range[untyped])?) -> void
946
956
  def print_titles(rows: nil, columns: nil)
947
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
957
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
948
958
 
949
959
  if rows.nil? && columns.nil?
950
960
  @writer.print_titles(sheet: @sheet_name)
@@ -960,7 +970,7 @@ module Xlsxrb
960
970
  # @api public
961
971
  #: (Elements::PrintTitles | Hash[Symbol, untyped] | nil opts) -> (Elements::PrintTitles | Hash[Symbol, untyped] | nil)
962
972
  def print_titles=(opts)
963
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
973
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
964
974
 
965
975
  if opts.nil?
966
976
  @writer.print_titles = nil
@@ -979,7 +989,7 @@ module Xlsxrb
979
989
  # @api public
980
990
  #: (Integer | String first, ?(Integer | String)? last) -> void
981
991
  def repeat_rows(first, last = nil)
982
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
992
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
983
993
 
984
994
  @writer.repeat_rows(first, last, sheet: @sheet_name)
985
995
  end
@@ -992,7 +1002,7 @@ module Xlsxrb
992
1002
  # @api public
993
1003
  #: (Integer | String first, ?(Integer | String)? last) -> void
994
1004
  def repeat_columns(first, last = nil)
995
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1005
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
996
1006
 
997
1007
  @writer.repeat_columns(first, last, sheet: @sheet_name)
998
1008
  end
@@ -1013,7 +1023,7 @@ module Xlsxrb
1013
1023
  #: () -> Elements::SplitPane?
1014
1024
  #: (?x_split: Integer | nil, ?y_split: Integer | nil, ?top_left_cell: String?, ?active_pane: (String | Symbol)?) -> void
1015
1025
  def split_pane(...)
1016
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1026
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1017
1027
 
1018
1028
  @writer.split_pane(...)
1019
1029
  end
@@ -1025,7 +1035,7 @@ module Xlsxrb
1025
1035
  # @api public
1026
1036
  #: (Elements::SplitPane | Hash[Symbol, untyped] | nil opts) -> void
1027
1037
  def split_pane=(opts)
1028
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1038
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1029
1039
 
1030
1040
  @writer.split_pane = opts
1031
1041
  end
@@ -1043,7 +1053,7 @@ module Xlsxrb
1043
1053
  #: () -> (Elements::Selection | Array[Elements::Selection])?
1044
1054
  #: (?String? active_cell, ?sqref: String?, ?pane: (String | Symbol)?) -> void
1045
1055
  def selection(...)
1046
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1056
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1047
1057
 
1048
1058
  @writer.selection(...)
1049
1059
  end
@@ -1055,7 +1065,7 @@ module Xlsxrb
1055
1065
  # @api public
1056
1066
  #: (Elements::Selection | Array[Elements::Selection | Hash[Symbol, untyped]] | Hash[Symbol, untyped] | nil opts) -> void
1057
1067
  def selection=(opts)
1058
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1068
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1059
1069
 
1060
1070
  @writer.selection = opts
1061
1071
  end
@@ -1076,7 +1086,7 @@ module Xlsxrb
1076
1086
  #: () -> Elements::PageMargins?
1077
1087
  #: (?left: Float | nil, ?right: Float | nil, ?top: Float | nil, ?bottom: Float | nil, ?header: Float | nil, ?footer: Float | nil) -> void
1078
1088
  def page_margins(...)
1079
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1089
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1080
1090
 
1081
1091
  @writer.page_margins(...)
1082
1092
  end
@@ -1088,7 +1098,7 @@ module Xlsxrb
1088
1098
  # @api public
1089
1099
  #: (Hash[Symbol, untyped] | Elements::PageMargins | nil opts) -> void
1090
1100
  def page_margins=(opts)
1091
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1101
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1092
1102
 
1093
1103
  @writer.page_margins = opts
1094
1104
  end
@@ -1106,7 +1116,7 @@ module Xlsxrb
1106
1116
  #: () -> Elements::PageSetup
1107
1117
  #: (**untyped opts) -> void
1108
1118
  def page_setup(...)
1109
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1119
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1110
1120
 
1111
1121
  @writer.page_setup(...)
1112
1122
  end
@@ -1118,7 +1128,7 @@ module Xlsxrb
1118
1128
  # @api public
1119
1129
  #: (Hash[Symbol, untyped] | Elements::PageSetup opts) -> void
1120
1130
  def page_setup=(opts)
1121
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1131
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1122
1132
 
1123
1133
  @writer.page_setup = opts
1124
1134
  end
@@ -1134,7 +1144,7 @@ module Xlsxrb
1134
1144
  #: () -> Elements::HeaderFooter
1135
1145
  #: (**untyped opts) -> void
1136
1146
  def header_footer(...)
1137
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1147
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1138
1148
 
1139
1149
  @writer.header_footer(...)
1140
1150
  end
@@ -1146,7 +1156,7 @@ module Xlsxrb
1146
1156
  # @api public
1147
1157
  #: (Hash[Symbol, untyped] | Elements::HeaderFooter opts) -> void
1148
1158
  def header_footer=(opts)
1149
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1159
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1150
1160
 
1151
1161
  @writer.header_footer = opts
1152
1162
  end
@@ -1160,7 +1170,7 @@ module Xlsxrb
1160
1170
  #: () -> Elements::PrintOptions
1161
1171
  #: (**untyped opts) -> void
1162
1172
  def print_options(**opts)
1163
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1173
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1164
1174
 
1165
1175
  @writer.print_options(**opts)
1166
1176
  end
@@ -1171,7 +1181,7 @@ module Xlsxrb
1171
1181
  # @api public
1172
1182
  #: (Hash[Symbol, untyped] | Elements::PrintOptions opts) -> void
1173
1183
  def print_options=(opts)
1174
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1184
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1175
1185
 
1176
1186
  @writer.print_options = opts
1177
1187
  end
@@ -1187,7 +1197,7 @@ module Xlsxrb
1187
1197
  #: () -> Elements::SheetProtection?
1188
1198
  #: (**untyped opts) -> void
1189
1199
  def sheet_protection(...)
1190
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1200
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1191
1201
 
1192
1202
  @writer.sheet_protection(...)
1193
1203
  end
@@ -1199,7 +1209,7 @@ module Xlsxrb
1199
1209
  # @api public
1200
1210
  #: (Elements::SheetProtection | Hash[Symbol, untyped] | bool | nil opts) -> void
1201
1211
  def sheet_protection=(opts)
1202
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1212
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1203
1213
 
1204
1214
  @writer.sheet_protection = opts
1205
1215
  end
@@ -1210,7 +1220,7 @@ module Xlsxrb
1210
1220
  # @api public
1211
1221
  #: () -> Array[Elements::Column]
1212
1222
  def columns
1213
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1223
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1214
1224
 
1215
1225
  @writer.columns
1216
1226
  end
@@ -1228,7 +1238,7 @@ module Xlsxrb
1228
1238
  # @api public
1229
1239
  #: (String file_data, ?ext: String, ?from_column: Integer?, ?from_row: Integer, ?to_column: Integer?, ?to_row: Integer, **untyped opts) -> void
1230
1240
  def image(...)
1231
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1241
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1232
1242
 
1233
1243
  @writer.image(...)
1234
1244
  end
@@ -1244,12 +1254,14 @@ module Xlsxrb
1244
1254
 
1245
1255
  # Sets the worksheet background image (<picture>).
1246
1256
  #
1247
- # @param val [String, Hash, Elements::Image, nil] Image binary data, file path, or image options hash.
1257
+ # @param val [String, Hash, Elements::Image, nil] Image binary data, or a Hash with +:data+ and
1258
+ # +:ext+. A String is always the image itself: to use a file, pass its bytes, as in
1259
+ # +{ data: File.binread(path), ext: "jpg" }+.
1248
1260
  # @return [untyped]
1249
1261
  # @api public
1250
1262
  #: (untyped val) -> untyped
1251
1263
  def background_image=(val)
1252
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1264
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1253
1265
 
1254
1266
  @writer.background_image = val
1255
1267
  end
@@ -1262,7 +1274,7 @@ module Xlsxrb
1262
1274
  #: () -> Elements::SheetView
1263
1275
  #: (**untyped opts) -> void
1264
1276
  def sheet_view(**opts)
1265
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1277
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1266
1278
 
1267
1279
  @writer.sheet_view(**opts)
1268
1280
  end
@@ -1273,7 +1285,7 @@ module Xlsxrb
1273
1285
  # @api public
1274
1286
  #: (Hash[Symbol, untyped] | Elements::SheetView opts) -> void
1275
1287
  def sheet_view=(opts)
1276
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1288
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1277
1289
 
1278
1290
  @writer.sheet_view = opts
1279
1291
  end
@@ -1286,7 +1298,7 @@ module Xlsxrb
1286
1298
  #: () -> Elements::SheetFormat
1287
1299
  #: (**untyped opts) -> void
1288
1300
  def sheet_format(**opts)
1289
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1301
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1290
1302
 
1291
1303
  @writer.sheet_format(**opts)
1292
1304
  end
@@ -1297,7 +1309,7 @@ module Xlsxrb
1297
1309
  # @api public
1298
1310
  #: (Hash[Symbol, untyped] | Elements::SheetFormat opts) -> void
1299
1311
  def sheet_format=(opts)
1300
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1312
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1301
1313
 
1302
1314
  @writer.sheet_format = opts
1303
1315
  end
@@ -1311,7 +1323,7 @@ module Xlsxrb
1311
1323
  # @api public
1312
1324
  #: (Integer row_index) -> void
1313
1325
  def row_break(...)
1314
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1326
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1315
1327
 
1316
1328
  @writer.row_break(...)
1317
1329
  end
@@ -1326,7 +1338,7 @@ module Xlsxrb
1326
1338
  # @api public
1327
1339
  #: (Integer | String | Symbol col) -> void
1328
1340
  def column_break(...)
1329
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1341
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1330
1342
 
1331
1343
  @writer.column_break(...)
1332
1344
  end
@@ -1342,7 +1354,7 @@ module Xlsxrb
1342
1354
  # @api public
1343
1355
  #: (String name, String sqref, ?password: String?, **untyped opts) -> void
1344
1356
  def protected_range(...)
1345
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1357
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1346
1358
 
1347
1359
  @writer.protected_range(...)
1348
1360
  end
@@ -1354,7 +1366,7 @@ module Xlsxrb
1354
1366
  # @api public
1355
1367
  #: (String ref) -> void
1356
1368
  def cell_watch(...)
1357
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1369
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1358
1370
 
1359
1371
  @writer.cell_watch(...)
1360
1372
  end
@@ -1367,7 +1379,7 @@ module Xlsxrb
1367
1379
  # @api public
1368
1380
  #: (String sqref, ?eval_error: bool, ?two_digit_text_year: bool, ?number_stored_as_text: bool, ?formula: bool, ?formula_range: bool, ?unlocked_formula: bool, ?empty_cell_reference: bool, ?list_data_validation: bool, ?calculated_column: bool) -> void
1369
1381
  def ignored_error(...)
1370
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1382
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1371
1383
 
1372
1384
  @writer.ignored_error(...)
1373
1385
  end
@@ -1397,7 +1409,7 @@ module Xlsxrb
1397
1409
  # @api public
1398
1410
  #: (Hash[Symbol, untyped] | Integer | nil spec) -> (Hash[Symbol, untyped] | Integer | nil)
1399
1411
  def phonetic_properties=(spec)
1400
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1412
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1401
1413
 
1402
1414
  @writer.phonetic_properties = spec
1403
1415
  end
@@ -1418,7 +1430,7 @@ module Xlsxrb
1418
1430
  # @api public
1419
1431
  #: (String | Symbol | nil val) -> String?
1420
1432
  def code_name=(val)
1421
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1433
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1422
1434
 
1423
1435
  @writer.code_name = val
1424
1436
  end
@@ -1439,7 +1451,7 @@ module Xlsxrb
1439
1451
  # @api public
1440
1452
  #: (bool? enabled) -> bool??
1441
1453
  def fit_to_page=(enabled)
1442
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1454
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1443
1455
 
1444
1456
  @writer.fit_to_page = enabled
1445
1457
  end
@@ -1460,10 +1472,21 @@ module Xlsxrb
1460
1472
  # @api public
1461
1473
  #: (Hash[Symbol, untyped]? spec) -> Hash[Symbol, untyped]?
1462
1474
  def sheet_calc_properties=(spec)
1463
- raise Error, "Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet." if @writer.current_sheet != @sheet_name
1475
+ raise inactive_sheet_error if @writer.current_sheet != @sheet_name
1464
1476
 
1465
1477
  @writer.sheet_calc_properties = spec
1466
1478
  end
1479
+
1480
+ private
1481
+
1482
+ # The error for writing through a proxy whose sheet is not the current sheet: an
1483
+ # earlier sheet, or a sheet that was never created.
1484
+ #: () -> Error
1485
+ def inactive_sheet_error
1486
+ return Error.new("Sheet '#{@sheet_name}' does not exist. Create it with sheet before writing to it.") unless @writer.sheet_names.include?(@sheet_name)
1487
+
1488
+ Error.new("Sheet '#{@sheet_name}' is no longer active. In streaming mode, you cannot write to a previous sheet.")
1489
+ end
1467
1490
  end
1468
1491
 
1469
1492
  # Returns the list of worksheet names created in this workbook so far.
@@ -1492,7 +1515,7 @@ module Xlsxrb
1492
1515
  def sheet(name = nil, state: nil, tab_color: nil, code_name: nil, **opts)
1493
1516
  sheet_count = @sheets.size + (@current_sheet ? 1 : 0)
1494
1517
  name ||= "Sheet#{sheet_count + 1}"
1495
- raise ArgumentError, "Sheet name '#{name}' must be <= 31 characters (Excel limitation)" if @strict_excel_mode && name.length > 31
1518
+ 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
1496
1519
  raise ArgumentError, "Sheet name '#{name}' contains invalid characters (Excel limitation)" if name.match?(Elements::Worksheet.forbidden_name_chars)
1497
1520
  raise ArgumentError, "Sheet name '#{name}' cannot start or end with an apostrophe (Excel limitation)" if @strict_excel_mode && (name.start_with?("'") || name.end_with?("'"))
1498
1521
  raise ArgumentError, "Sheet name '#{name}' is reserved by Excel (Excel limitation)" if @strict_excel_mode && name.casecmp?("History")
@@ -1524,8 +1547,15 @@ module Xlsxrb
1524
1547
  @current_sheet
1525
1548
  end
1526
1549
 
1550
+ UNKNOWN_SHEET_MESSAGE = "Looking up a sheet that does not exist with StreamWriter#[] is deprecated; " \
1551
+ "create it with sheet. In version 0.3.0, this will raise ArgumentError."
1552
+ private_constant :UNKNOWN_SHEET_MESSAGE
1553
+
1527
1554
  # Returns a WorksheetProxy for the current or named sheet.
1528
1555
  #
1556
+ # Looking up a sheet that does not exist is deprecated and warns; in version 0.3.0 it
1557
+ # will raise ArgumentError.
1558
+ #
1529
1559
  # @param name_or_index [String, Symbol, Integer, nil] Sheet name or 0-based sheet index.
1530
1560
  # @yield [proxy]
1531
1561
  # @yieldparam proxy [WorksheetProxy]
@@ -1541,6 +1571,7 @@ module Xlsxrb
1541
1571
  else
1542
1572
  name_or_index.to_s
1543
1573
  end
1574
+ Deprecation.warn(UNKNOWN_SHEET_MESSAGE) unless sheet_names.include?(target_name)
1544
1575
  proxy = WorksheetProxy.new(self, target_name)
1545
1576
  yield proxy if block_given?
1546
1577
  proxy
@@ -1561,9 +1592,12 @@ module Xlsxrb
1561
1592
  @current_sheet_io = StringIO.new(@current_row_buffer)
1562
1593
  date1904 = @workbook_properties&.[](:date1904) ? true : false
1563
1594
  @current_row_writer = Ooxml::WorksheetWriter.new(@current_sheet_io, date1904: date1904)
1595
+ @current_row_writer.time_like_style_resolver = method(:time_like_style_id)
1596
+ @current_row_writer.strict_excel_mode = @strict_excel_mode
1564
1597
  @current_row_writer.instance_variable_set(:@started, true)
1565
1598
 
1566
1599
  @current_columns = []
1600
+ @current_column_style_names = {} # column index => style name, for date style variants
1567
1601
  @current_charts = []
1568
1602
  @current_hyperlinks = []
1569
1603
  @current_auto_filter = nil
@@ -1641,6 +1675,8 @@ module Xlsxrb
1641
1675
 
1642
1676
  date1904 = @workbook_properties&.[](:date1904) ? true : false
1643
1677
  @current_row_writer = Ooxml::WorksheetWriter.new(zip_io, date1904: date1904)
1678
+ @current_row_writer.time_like_style_resolver = method(:time_like_style_id)
1679
+ @current_row_writer.strict_excel_mode = @strict_excel_mode
1644
1680
  @current_row_writer.start(
1645
1681
  columns: @current_columns,
1646
1682
  sheet_properties: @current_sheet_properties.empty? ? nil : @current_sheet_properties,
@@ -1676,11 +1712,8 @@ module Xlsxrb
1676
1712
 
1677
1713
  values = DslHelpers.normalize_row_values(values)
1678
1714
  cell_styles = DslHelpers.normalize_row_styles(cell_styles)
1679
- if cell_styles
1680
- check_style_names!(cell_styles)
1681
- cell_styles = with_date_style_variants(values, cell_styles)
1682
- end
1683
- DslHelpers.check_style_name!(row_style, @style_name_to_id)
1715
+ check_style_names!(cell_styles) if cell_styles
1716
+ check_style_name!(row_style)
1684
1717
 
1685
1718
  if @current_charts && !@current_charts.empty?
1686
1719
  @current_cells ||= {}
@@ -1702,6 +1735,9 @@ module Xlsxrb
1702
1735
  else
1703
1736
  row_style
1704
1737
  end
1738
+ if cell_styles || row_style || !@current_column_style_names.empty?
1739
+ cell_styles = with_date_style_variants(values, cell_styles, row_style.is_a?(Hash) ? inline_name : row_style)
1740
+ end
1705
1741
 
1706
1742
  effective_outline = outline_level || @current_row_outline_level
1707
1743
  effective_collapsed = collapsed || @current_row_collapsed
@@ -1750,6 +1786,7 @@ module Xlsxrb
1750
1786
 
1751
1787
  sheet if @current_sheet.nil?
1752
1788
  @current_row_index += count
1789
+ nil
1753
1790
  end
1754
1791
 
1755
1792
  # Sets column formatting and properties for one or multiple columns.
@@ -1769,6 +1806,7 @@ module Xlsxrb
1769
1806
  raise ArgumentError, "Column width #{width} must be between 0 and 255 characters (Excel limitation)" if @strict_excel_mode && width && (width.negative? || width > 255)
1770
1807
 
1771
1808
  sheet if @current_sheet.nil?
1809
+ warn_late_sheet_setting("column")
1772
1810
 
1773
1811
  DslHelpers.check_style_name!(style, @style_name_to_id)
1774
1812
  style_id = if style.is_a?(Hash)
@@ -1780,7 +1818,18 @@ module Xlsxrb
1780
1818
  else
1781
1819
  style
1782
1820
  end
1821
+ # The style's name, so that dates in the column can get a variant of it with a date format
1822
+ style_name = if style.is_a?(Hash)
1823
+ inline_name
1824
+ elsif style.is_a?(String) || style.is_a?(Symbol)
1825
+ style.to_s
1826
+ end
1783
1827
  DslHelpers.normalize_column_indices(index).each do |idx|
1828
+ if style_name
1829
+ @current_column_style_names[idx] = style_name
1830
+ elsif style
1831
+ @current_column_style_names.delete(idx)
1832
+ end
1784
1833
  existing = @current_columns.find { |c| c[:index] == idx }
1785
1834
  if existing
1786
1835
  existing[:width] = width unless width.nil?
@@ -1891,6 +1940,7 @@ module Xlsxrb
1891
1940
  @current_row_collapsed = prev_collapsed
1892
1941
  @current_row_hidden = prev_hidden
1893
1942
  end
1943
+ nil
1894
1944
  end
1895
1945
 
1896
1946
  # Configures or queries worksheet-level outline and grouping view properties.
@@ -1912,6 +1962,7 @@ module Xlsxrb
1912
1962
  #: (?summary_below: bool?, ?summary_right: bool?, ?show_outline_symbols: bool?, ?apply_styles: bool?, **untyped opts) -> void
1913
1963
  def outline_properties(summary_below: nil, summary_right: nil, show_outline_symbols: nil, apply_styles: nil, **opts)
1914
1964
  sheet if @current_sheet.nil?
1965
+ warn_late_sheet_setting("outline_properties") unless summary_below.nil? && summary_right.nil? && show_outline_symbols.nil? && apply_styles.nil? && opts.empty?
1915
1966
 
1916
1967
  return @current_sheet_properties.slice(:summary_below, :summary_right, :show_outline_symbols, :apply_styles) if summary_below.nil? && summary_right.nil? && show_outline_symbols.nil? && apply_styles.nil? && opts.empty?
1917
1968
 
@@ -2258,6 +2309,7 @@ module Xlsxrb
2258
2309
  #: (?row: Integer | nil, ?column: (Integer | String)?, ?top_left_cell: String?, ?active_pane: (String | Symbol)?) -> void
2259
2310
  def freeze_pane(row: nil, column: nil, top_left_cell: nil, active_pane: nil)
2260
2311
  sheet if @current_sheet.nil?
2312
+ warn_late_sheet_setting("freeze_pane") unless row.nil? && column.nil? && top_left_cell.nil? && active_pane.nil?
2261
2313
  if row.nil? && column.nil? && top_left_cell.nil? && active_pane.nil?
2262
2314
  return @current_freeze_pane ? Elements::FreezePane.new(**@current_freeze_pane) : nil
2263
2315
  end
@@ -2278,6 +2330,7 @@ module Xlsxrb
2278
2330
  #: (Elements::FreezePane | Hash[Symbol, untyped] | nil opts) -> void
2279
2331
  def freeze_pane=(opts)
2280
2332
  sheet if @current_sheet.nil?
2333
+ warn_late_sheet_setting("freeze_pane=")
2281
2334
  if opts.nil?
2282
2335
  @current_freeze_pane = nil
2283
2336
  else
@@ -2308,6 +2361,7 @@ module Xlsxrb
2308
2361
  #: (?x_split: Integer | nil, ?y_split: Integer | nil, ?top_left_cell: String?, ?active_pane: (String | Symbol)?) -> void
2309
2362
  def split_pane(x_split: nil, y_split: nil, top_left_cell: nil, active_pane: nil)
2310
2363
  sheet if @current_sheet.nil?
2364
+ warn_late_sheet_setting("split_pane") unless x_split.nil? && y_split.nil? && top_left_cell.nil? && active_pane.nil?
2311
2365
  if x_split.nil? && y_split.nil? && top_left_cell.nil? && active_pane.nil?
2312
2366
  return @current_split_pane ? Elements::SplitPane.new(**@current_split_pane) : nil
2313
2367
  end
@@ -2326,6 +2380,7 @@ module Xlsxrb
2326
2380
  #: (Elements::SplitPane | Hash[Symbol, untyped] | nil opts) -> void
2327
2381
  def split_pane=(opts)
2328
2382
  sheet if @current_sheet.nil?
2383
+ warn_late_sheet_setting("split_pane=")
2329
2384
  if opts.nil?
2330
2385
  @current_split_pane = nil
2331
2386
  else
@@ -2351,6 +2406,7 @@ module Xlsxrb
2351
2406
  #: (?String? active_cell, ?sqref: String?, ?pane: (String | Symbol)?) -> void
2352
2407
  def selection(active_cell = nil, sqref: nil, pane: nil)
2353
2408
  sheet if @current_sheet.nil?
2409
+ warn_late_sheet_setting("selection") unless active_cell.nil? && sqref.nil? && pane.nil?
2354
2410
  if active_cell.nil? && sqref.nil? && pane.nil?
2355
2411
  return nil if @current_selection.nil?
2356
2412
  return @current_selection.map { |s| Elements::Selection.new(**s) } if @current_selection.is_a?(Array)
@@ -2378,6 +2434,7 @@ module Xlsxrb
2378
2434
  #: (Elements::Selection | Array[Elements::Selection | Hash[Symbol, untyped]] | Hash[Symbol, untyped] | nil opts) -> void
2379
2435
  def selection=(opts)
2380
2436
  sheet if @current_sheet.nil?
2437
+ warn_late_sheet_setting("selection=")
2381
2438
  @current_selection = if opts.nil?
2382
2439
  nil
2383
2440
  elsif opts.is_a?(Array)
@@ -2444,6 +2501,7 @@ module Xlsxrb
2444
2501
  #: (**untyped opts) -> void
2445
2502
  def page_setup(**opts)
2446
2503
  sheet if @current_sheet.nil?
2504
+ warn_late_sheet_setting("page_setup with fit_to_width or fit_to_height") if opts.key?(:fit_to_width) || opts.key?(:fit_to_height)
2447
2505
  return Elements::PageSetup.new(**@current_page_setup) if opts.empty?
2448
2506
 
2449
2507
  @current_page_setup.merge!(opts)
@@ -2600,7 +2658,9 @@ module Xlsxrb
2600
2658
 
2601
2659
  # Sets the worksheet background image (<picture>).
2602
2660
  #
2603
- # @param val [String, Hash, Elements::Image, nil] Image binary data, file path, or image options hash.
2661
+ # @param val [String, Hash, Elements::Image, nil] Image binary data, or a Hash with +:data+ and
2662
+ # +:ext+. A String is always the image itself: to use a file, pass its bytes, as in
2663
+ # +{ data: File.binread(path), ext: "jpg" }+.
2604
2664
  # @return [untyped]
2605
2665
  # @api public
2606
2666
  #: (untyped val) -> untyped
@@ -2613,10 +2673,9 @@ module Xlsxrb
2613
2673
 
2614
2674
  data = val
2615
2675
  inferred_ext = nil
2616
- if data.is_a?(String) && !data.include?("\x00") && File.file?(data)
2617
- inferred_ext = File.extname(data).delete_prefix(".").downcase
2618
- data = File.binread(data)
2619
- elsif data.is_a?(Hash)
2676
+ # SECURITY NOTE: A String is the image itself and is never read as a path. Reading it
2677
+ # as one let text that names a file put that file's bytes into the workbook (ADR 0012).
2678
+ if data.is_a?(Hash)
2620
2679
  inferred_ext = data[:ext] || (data[:filename] && File.extname(data[:filename]).delete_prefix("."))
2621
2680
  data = data[:file_data] || data[:data]
2622
2681
  elsif data.is_a?(Elements::Image)
@@ -2710,6 +2769,7 @@ module Xlsxrb
2710
2769
  #: (String | Symbol | Hash[Symbol, untyped] | nil spec) -> (String | Hash[Symbol, untyped] | nil)
2711
2770
  def tab_color=(spec)
2712
2771
  sheet if @current_sheet.nil?
2772
+ warn_late_sheet_setting("tab_color=")
2713
2773
  @current_sheet_properties[:tab_color] = spec
2714
2774
  end
2715
2775
 
@@ -2725,6 +2785,7 @@ module Xlsxrb
2725
2785
  #: (**untyped opts) -> void
2726
2786
  def sheet_properties(**opts)
2727
2787
  sheet if @current_sheet.nil?
2788
+ warn_late_sheet_setting("sheet_properties") unless opts.empty?
2728
2789
  return Elements::SheetProperties.new(**@current_sheet_properties) if opts.empty?
2729
2790
 
2730
2791
  opts.each do |k, v|
@@ -2751,6 +2812,7 @@ module Xlsxrb
2751
2812
  #: (Hash[Symbol, untyped] | Elements::SheetProperties opts) -> void
2752
2813
  def sheet_properties=(opts)
2753
2814
  sheet if @current_sheet.nil?
2815
+ warn_late_sheet_setting("sheet_properties=")
2754
2816
  h = opts.respond_to?(:to_h) ? opts.to_h : (opts || {})
2755
2817
  @current_sheet_properties = {}
2756
2818
  sheet_properties(**h)
@@ -2769,6 +2831,7 @@ module Xlsxrb
2769
2831
  #: (**untyped opts) -> void
2770
2832
  def sheet_view(**opts)
2771
2833
  sheet if @current_sheet.nil?
2834
+ warn_late_sheet_setting("sheet_view") unless opts.empty?
2772
2835
  return Elements::SheetView.new(**@current_sheet_view) if opts.empty?
2773
2836
 
2774
2837
  opts.each { |k, v| @current_sheet_view[k.to_sym] = v }
@@ -2783,6 +2846,7 @@ module Xlsxrb
2783
2846
  #: (Hash[Symbol, untyped] | Elements::SheetView opts) -> void
2784
2847
  def sheet_view=(opts)
2785
2848
  sheet if @current_sheet.nil?
2849
+ warn_late_sheet_setting("sheet_view=")
2786
2850
  h = opts.respond_to?(:to_h) ? opts.to_h : (opts || {})
2787
2851
  @current_sheet_view = h.transform_keys(&:to_sym)
2788
2852
  nil
@@ -2800,6 +2864,7 @@ module Xlsxrb
2800
2864
  #: (**untyped opts) -> void
2801
2865
  def sheet_format(**opts)
2802
2866
  sheet if @current_sheet.nil?
2867
+ warn_late_sheet_setting("sheet_format") unless opts.empty?
2803
2868
  return Elements::SheetFormat.new(**@current_sheet_format) if opts.empty?
2804
2869
 
2805
2870
  opts.each { |k, v| @current_sheet_format[k.to_sym] = v }
@@ -2814,6 +2879,7 @@ module Xlsxrb
2814
2879
  #: (Hash[Symbol, untyped] | Elements::SheetFormat opts) -> void
2815
2880
  def sheet_format=(opts)
2816
2881
  sheet if @current_sheet.nil?
2882
+ warn_late_sheet_setting("sheet_format=")
2817
2883
  h = opts.respond_to?(:to_h) ? opts.to_h : (opts || {})
2818
2884
  @current_sheet_format = h.transform_keys(&:to_sym)
2819
2885
  nil
@@ -2949,6 +3015,7 @@ module Xlsxrb
2949
3015
  if @current_sheet.nil?
2950
3016
  @workbook_properties[:code_name] = val&.to_s
2951
3017
  else
3018
+ warn_late_sheet_setting("code_name=")
2952
3019
  @current_sheet_properties[:code_name] = val&.to_s
2953
3020
  end
2954
3021
  end
@@ -2971,6 +3038,7 @@ module Xlsxrb
2971
3038
  #: (bool? enabled) -> bool?
2972
3039
  def fit_to_page=(enabled)
2973
3040
  sheet if @current_sheet.nil?
3041
+ warn_late_sheet_setting("fit_to_page=")
2974
3042
  @current_sheet_properties[:fit_to_page] = enabled ? true : false
2975
3043
  end
2976
3044
 
@@ -3386,21 +3454,79 @@ module Xlsxrb
3386
3454
  #: (untyped cell_styles) -> void
3387
3455
  def check_style_names!(cell_styles)
3388
3456
  if cell_styles.is_a?(Array)
3389
- cell_styles.each { |name| DslHelpers.check_style_name!(name, @style_name_to_id) }
3457
+ cell_styles.each { |name| check_style_name!(name) }
3390
3458
  else
3391
- DslHelpers.check_style_name!(cell_styles, @style_name_to_id)
3459
+ check_style_name!(cell_styles)
3392
3460
  end
3393
3461
  end
3394
3462
 
3395
- # Gives Date and Time values whose named cell style has no number format a variant of
3396
- # that style with the default date or time format, registered on first use.
3397
- #: (Array[untyped] values, untyped cell_styles) -> untyped
3398
- def with_date_style_variants(values, cell_styles)
3463
+ # Raises ArgumentError if +name+ is a style name that has not been defined. Defined
3464
+ # names return before DslHelpers.check_style_name!, whose Symbol#to_s allocates a
3465
+ # String for every styled cell.
3466
+ #: (untyped name) -> void
3467
+ def check_style_name!(name)
3468
+ return if name.is_a?(Symbol) ? @style_name_to_id.key?(name.name) : name.is_a?(String) && @style_name_to_id.key?(name)
3469
+
3470
+ DslHelpers.check_style_name!(name, @style_name_to_id)
3471
+ end
3472
+
3473
+ LATE_SHEET_SETTING_MESSAGE = "Calling %s after rows of the sheet are written is deprecated; call it before the first row. " \
3474
+ "StreamWriter writes this setting at the start of the sheet, so it is ignored once that start " \
3475
+ "is written, after about 64 KB of rows. In version 0.3.0, it will raise Xlsxrb::Error."
3476
+ private_constant :LATE_SHEET_SETTING_MESSAGE
3477
+
3478
+ # Warns when a setting that StreamWriter writes at the start of the sheet comes after
3479
+ # rows (or skipped rows) of the sheet. ADR 0010 makes it an error in 0.3.0.
3480
+ #: (String name) -> void
3481
+ def warn_late_sheet_setting(name)
3482
+ return unless @current_row_index.positive?
3483
+
3484
+ Deprecation.warn(format(LATE_SHEET_SETTING_MESSAGE, name), uplevel: nil)
3485
+ end
3486
+
3487
+ # The style id for a Time-like value (see Ooxml::WorksheetWriter::TimeLike) in a cell
3488
+ # whose style id is style_id: the date-time variant of that style when the style has no
3489
+ # number format, as a Time in the same cell gets from with_date_style_variants, and
3490
+ # style_id otherwise. Called by the worksheet writer only for such values.
3491
+ #: (Integer style_id) -> Integer
3492
+ def time_like_style_id(style_id)
3493
+ @time_like_style_ids[style_id] ||= begin
3494
+ style_name = @style_name_to_id.key(style_id)
3495
+ style_builder = style_name && @styles[style_name]
3496
+ if style_builder.nil? || style_builder.number_format
3497
+ style_id
3498
+ else
3499
+ variant_name = "#{style_name}__xlsxrb_time"
3500
+ unless @style_name_to_id.key?(variant_name)
3501
+ variant_builder = style_builder.with_number_format(variant_name, DslHelpers::TIME_NUMBER_FORMAT)
3502
+ @styles[variant_name] = variant_builder
3503
+ @style_name_to_id[variant_name] = variant_builder.register_with(@style_writer)
3504
+ end
3505
+ @style_name_to_id.fetch(variant_name)
3506
+ end
3507
+ end
3508
+ end
3509
+
3510
+ # Gives Date and Time values whose named style has no number format a variant of that
3511
+ # style with the default date or time format, registered on first use. A value without
3512
+ # a cell style gets a variant of the style it inherits: the row style, or the column
3513
+ # style in a row without one.
3514
+ #: (Array[untyped] values, untyped cell_styles, untyped row_style) -> untyped
3515
+ def with_date_style_variants(values, cell_styles, row_style)
3516
+ # This runs for every styled row. any? with a class checks the values in C, which
3517
+ # costs less than the loop below for the rows without dates.
3518
+ return cell_styles unless values.any?(Date) || values.any?(Time)
3519
+
3399
3520
  styles_for_cells = cell_styles.is_a?(Array) ? cell_styles : nil
3400
- values.each_with_index do |value, idx|
3521
+ idx = -1
3522
+ last = values.size - 1
3523
+ while idx < last
3524
+ idx += 1
3525
+ value = values[idx]
3401
3526
  next unless value.is_a?(Date) || value.is_a?(Time)
3402
3527
 
3403
3528
  style_name = styles_for_cells ? styles_for_cells[idx] : cell_styles
3529
+ style_name = row_style.nil? ? @current_column_style_names[idx] : row_style if style_name.nil?
3404
3530
  next unless style_name.is_a?(String) || style_name.is_a?(Symbol)
3405
3531
 
3406
3532
  variant = DslHelpers.date_style_variant(value, style_name.to_s, @styles[style_name.to_s])