xlsxrb 0.1.14 → 0.1.15

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.
@@ -44,8 +44,11 @@ module Xlsxrb
44
44
  @shapes = []
45
45
  @sheet_properties = {}
46
46
  @sheet_view = {}
47
+ @sheet_format = {}
47
48
  @row_breaks = []
48
49
  @col_breaks = []
50
+ @protected_ranges = []
51
+ @cell_watches = []
49
52
  @print_area = nil
50
53
  @print_titles = nil
51
54
  end
@@ -338,12 +341,21 @@ module Xlsxrb
338
341
 
339
342
  # Adds a data validation rule to a cell or range.
340
343
  #
341
- # @param sqref [String] Target cell or range reference (e.g. "B2:B100").
344
+ # @param sqref [String, nil] Target cell or range reference (e.g. "B2:B100").
342
345
  # @param opts [Hash] Validation configuration (e.g. type: :list, formula1: '"Option1,Option2"').
346
+ # @yield [data_validation_builder] Optional configuration block.
343
347
  # @return [void]
344
348
  # @api public
345
- #: (String sqref, **untyped opts) -> void
346
- def validate_data(sqref, **opts)
349
+ #: (?String? sqref, **untyped opts) ?{ (DataValidationBuilder) -> void } -> void
350
+ def validate_data(sqref = nil, **opts)
351
+ if block_given?
352
+ builder = DataValidationBuilder.new(sqref, **opts)
353
+ yield builder
354
+ opts = builder.to_h
355
+ sqref = opts.delete(:sqref) || sqref
356
+ end
357
+ raise ArgumentError, "data validation sqref must be provided" unless sqref
358
+
347
359
  @data_validations << opts.merge(sqref: sqref)
348
360
  end
349
361
 
@@ -360,17 +372,32 @@ module Xlsxrb
360
372
 
361
373
  # Adds an Excel Table (ListObject) to the sheet.
362
374
  #
363
- # @param ref [String] The table range (e.g. "A1:D20").
364
- # @param columns [Array<String>] Column header names.
375
+ # @param ref [String, nil] The table range (e.g. "A1:D20").
376
+ # @param columns [Array<String>, nil] Column header names.
365
377
  # @param name [String, nil] Table name identifier.
366
378
  # @param display_name [String, nil] Table display name.
367
379
  # @param style [String, nil] Built-in table style name (e.g. "TableStyleMedium2").
368
380
  # @param opts [Hash] Additional table properties.
381
+ # @yield [table_builder] Optional configuration block.
369
382
  # @return [void]
370
383
  # @api public
371
- #: (String ref, columns: Array[String | Hash[Symbol, untyped]], ?name: String?, ?display_name: String?, ?style: String?, **untyped opts) -> void
372
- def table(ref, columns:, name: nil, display_name: nil, style: nil, **)
373
- @tables << DslHelpers.normalize_table_options(ref, columns: columns, name: name, display_name: display_name, style: style, **)
384
+ #: (?String? ref, ?columns: Array[String | Hash[Symbol, untyped]]?, ?name: String?, ?display_name: String?, ?style: String?, **untyped opts) ?{ (TableBuilder) -> void } -> void
385
+ def table(ref = nil, columns: nil, name: nil, display_name: nil, style: nil, **opts)
386
+ opts = opts.dup
387
+ if block_given?
388
+ builder = TableBuilder.new(ref, columns: columns, name: name, display_name: display_name, style: style, **opts)
389
+ yield builder
390
+ opts = builder.to_h
391
+ ref = opts.delete(:ref) || ref
392
+ columns = opts.delete(:columns) || columns
393
+ name = opts.delete(:name) || name
394
+ display_name = opts.delete(:display_name) || display_name
395
+ style = opts.delete(:style) || style
396
+ end
397
+ raise ArgumentError, "table ref must be provided" unless ref
398
+ raise ArgumentError, "table columns must be provided" if columns.nil?
399
+
400
+ @tables << DslHelpers.normalize_table_options(ref, columns: columns, name: name, display_name: display_name, style: style, **opts)
374
401
  end
375
402
 
376
403
  # Adds a Pivot Table to the sheet.
@@ -728,6 +755,30 @@ module Xlsxrb
728
755
  @sheet_view[name] = value
729
756
  end
730
757
 
758
+ # Configures sheet format properties (e.g. default_row_height, default_col_width).
759
+ #
760
+ # @example Using keyword arguments
761
+ # sheet.sheet_format(default_row_height: 24, default_col_width: 15)
762
+ #
763
+ # @example Using property name and value
764
+ # sheet.sheet_format(:default_row_height, 24)
765
+ #
766
+ # @param name [Symbol, String, Hash, nil] Property name or hash of properties.
767
+ # @param value [Object, nil] Property value when name is provided.
768
+ # @param opts [Hash] Keyword arguments for sheet format properties.
769
+ # @return [Hash]
770
+ # @api public
771
+ #: (?Symbol | String | Hash[Symbol, untyped] | nil name, ?untyped value, **untyped opts) -> Hash[Symbol, untyped]
772
+ def sheet_format(name = nil, value = nil, **opts)
773
+ if name.is_a?(Hash)
774
+ @sheet_format.merge!(name)
775
+ elsif name && value
776
+ @sheet_format[name.to_sym] = value
777
+ end
778
+ @sheet_format.merge!(opts) unless opts.empty?
779
+ @sheet_format
780
+ end
781
+
731
782
  # Inserts a horizontal page break before the specified row.
732
783
  #
733
784
  # @param row_num [Integer] 1-based row number.
@@ -749,6 +800,34 @@ module Xlsxrb
749
800
  @col_breaks << col_index
750
801
  end
751
802
 
803
+ # Adds a protected range to the worksheet.
804
+ #
805
+ # @param name [String] Name of the protected range.
806
+ # @param sqref [String] Cell range (e.g. "A1:B10").
807
+ # @param password [String, nil] Optional password or hash.
808
+ # @param opts [Hash] Additional options.
809
+ # @return [void]
810
+ # @api public
811
+ #: (String name, String sqref, ?password: String?, **untyped opts) -> void
812
+ def protected_range(name, sqref, password: nil, **opts)
813
+ entry = { name: name.to_s, sqref: sqref.to_s }
814
+ entry[:password] = password if password
815
+ entry.merge!(opts) unless opts.empty?
816
+ @protected_ranges << DslHelpers.normalize_protection_options(entry)
817
+ end
818
+ alias add_protected_range protected_range
819
+
820
+ # Adds a cell watch to the worksheet.
821
+ #
822
+ # @param ref [String] Cell reference (e.g. "A1").
823
+ # @return [void]
824
+ # @api public
825
+ #: (String ref) -> void
826
+ def cell_watch(ref)
827
+ @cell_watches << ref.to_s
828
+ end
829
+ alias add_cell_watch cell_watch
830
+
752
831
  # Builds and returns the in-memory {Elements::Worksheet}.
753
832
  #
754
833
  # @return [Elements::Worksheet]
@@ -778,9 +857,12 @@ module Xlsxrb
778
857
  facade_meta[:images] = @images unless @images.empty?
779
858
  facade_meta[:shapes] = @shapes unless @shapes.empty?
780
859
  facade_meta[:sheet_properties] = @sheet_properties unless @sheet_properties.empty?
860
+ facade_meta[:sheet_format] = @sheet_format unless @sheet_format.empty?
781
861
  facade_meta[:sheet_view] = @sheet_view unless @sheet_view.empty?
782
862
  facade_meta[:row_breaks] = @row_breaks unless @row_breaks.empty?
783
863
  facade_meta[:col_breaks] = @col_breaks unless @col_breaks.empty?
864
+ facade_meta[:protected_ranges] = @protected_ranges unless @protected_ranges.empty?
865
+ facade_meta[:cell_watches] = @cell_watches unless @cell_watches.empty?
784
866
  facade_meta[:print_area] = @print_area if @print_area
785
867
  facade_meta[:print_titles] = @print_titles if @print_titles
786
868
 
@@ -828,7 +910,24 @@ module Xlsxrb
828
910
  comments: @comments,
829
911
  print_area: @print_area,
830
912
  print_titles: @print_titles,
831
- sparkline_groups: @sparkline_groups
913
+ sparkline_groups: @sparkline_groups,
914
+ merged_cells: @merge_cells_ranges,
915
+ auto_filter: @auto_filter,
916
+ tables: @tables,
917
+ shapes: @shapes,
918
+ freeze_pane: @freeze_pane,
919
+ page_setup: @page_setup,
920
+ page_margins: @page_margins,
921
+ header_footer: @header_footer,
922
+ print_options: @print_options,
923
+ sheet_protection: @sheet_protection,
924
+ sheet_view: @sheet_view,
925
+ sheet_properties: @sheet_properties,
926
+ sheet_format: @sheet_format,
927
+ row_breaks: @row_breaks,
928
+ col_breaks: @col_breaks,
929
+ protected_ranges: @protected_ranges,
930
+ cell_watches: @cell_watches
832
931
  )
833
932
  end
834
933
 
@@ -837,4 +936,408 @@ module Xlsxrb
837
936
  #: Hash[String, StyleBuilder]
838
937
  attr_reader :styles
839
938
  end
939
+
940
+ # Builder context for configuring an Excel Table (ListObject) in block form.
941
+ #
942
+ # @api public
943
+ class TableBuilder
944
+ # @param ref [String, nil] Table range (e.g. "A1:D10").
945
+ # @param columns [Array, nil] Column header names or definition hashes.
946
+ # @param name [String, nil] Table name identifier.
947
+ # @param display_name [String, nil] Table display name.
948
+ # @param style [String, nil] Built-in table style name (e.g. "TableStyleMedium2").
949
+ # @param opts [Hash] Additional table properties.
950
+ #: (?String? ref, ?columns: untyped, ?name: String?, ?display_name: String?, ?style: String?, **untyped opts) -> void
951
+ def initialize(ref = nil, columns: nil, name: nil, display_name: nil, style: nil, **opts)
952
+ @options = opts.dup
953
+ @options[:ref] = ref if ref
954
+ @options[:columns] = columns.is_a?(Array) ? columns.dup : [columns] if columns
955
+ @options[:name] = name if name
956
+ @options[:display_name] = display_name if display_name
957
+ @options[:style] = style if style
958
+ end
959
+
960
+ #: (untyped key) -> untyped
961
+ def [](key)
962
+ @options[key.to_sym]
963
+ end
964
+
965
+ #: (untyped key, untyped value) -> untyped
966
+ def []=(key, value)
967
+ @options[key.to_sym] = value
968
+ end
969
+
970
+ #: (?String? val) -> String?
971
+ def ref(val = nil)
972
+ if val
973
+ @options[:ref] = val.to_s
974
+ else
975
+ @options[:ref]
976
+ end
977
+ end
978
+
979
+ #: (String val) -> String
980
+ def ref=(val)
981
+ @options[:ref] = val.to_s
982
+ end
983
+
984
+ #: (*untyped cols) -> untyped
985
+ def columns(*cols)
986
+ if cols.empty?
987
+ @options[:columns]
988
+ elsif cols.size == 1 && cols.first.is_a?(Array)
989
+ @options[:columns] = cols.first
990
+ else
991
+ @options[:columns] = cols
992
+ end
993
+ end
994
+
995
+ #: (untyped cols) -> untyped
996
+ def columns=(cols)
997
+ @options[:columns] = cols.is_a?(Array) ? cols : [cols]
998
+ end
999
+
1000
+ # Appends a single column name or column definition.
1001
+ #
1002
+ # @param col [String, Hash] Column header name or definition.
1003
+ # @return [void]
1004
+ #: (String | Hash[Symbol, untyped] col) -> void
1005
+ def column(col)
1006
+ (@options[:columns] ||= []) << col
1007
+ end
1008
+ alias add_column column
1009
+
1010
+ #: (?String? val) -> String?
1011
+ def name(val = nil)
1012
+ if val
1013
+ @options[:name] = val.to_s
1014
+ else
1015
+ @options[:name]
1016
+ end
1017
+ end
1018
+
1019
+ #: (String val) -> String
1020
+ def name=(val)
1021
+ @options[:name] = val.to_s
1022
+ end
1023
+
1024
+ #: (?String? val) -> String?
1025
+ def display_name(val = nil)
1026
+ if val
1027
+ @options[:display_name] = val.to_s
1028
+ else
1029
+ @options[:display_name]
1030
+ end
1031
+ end
1032
+
1033
+ #: (String val) -> String
1034
+ def display_name=(val)
1035
+ @options[:display_name] = val.to_s
1036
+ end
1037
+
1038
+ #: (?String? val) -> String?
1039
+ def style(val = nil)
1040
+ if val
1041
+ @options[:style] = val.to_s
1042
+ else
1043
+ @options[:style]
1044
+ end
1045
+ end
1046
+
1047
+ #: (String val) -> String
1048
+ def style=(val)
1049
+ @options[:style] = val.to_s
1050
+ end
1051
+
1052
+ #: (?bool? val) -> bool?
1053
+ def show_row_stripes(val = nil)
1054
+ if val.nil?
1055
+ @options[:show_row_stripes]
1056
+ else
1057
+ @options[:show_row_stripes] = val ? true : false
1058
+ end
1059
+ end
1060
+
1061
+ #: (bool val) -> bool
1062
+ def show_row_stripes=(val)
1063
+ @options[:show_row_stripes] = val ? true : false
1064
+ end
1065
+
1066
+ #: (?bool? val) -> bool?
1067
+ def show_column_stripes(val = nil)
1068
+ if val.nil?
1069
+ @options[:show_column_stripes]
1070
+ else
1071
+ @options[:show_column_stripes] = val ? true : false
1072
+ end
1073
+ end
1074
+
1075
+ #: (bool val) -> bool
1076
+ def show_column_stripes=(val)
1077
+ @options[:show_column_stripes] = val ? true : false
1078
+ end
1079
+
1080
+ #: (?bool? val) -> bool?
1081
+ def show_first_column(val = nil)
1082
+ if val.nil?
1083
+ @options[:show_first_column]
1084
+ else
1085
+ @options[:show_first_column] = val ? true : false
1086
+ end
1087
+ end
1088
+
1089
+ #: (bool val) -> bool
1090
+ def show_first_column=(val)
1091
+ @options[:show_first_column] = val ? true : false
1092
+ end
1093
+
1094
+ #: (?bool? val) -> bool?
1095
+ def show_last_column(val = nil)
1096
+ if val.nil?
1097
+ @options[:show_last_column]
1098
+ else
1099
+ @options[:show_last_column] = val ? true : false
1100
+ end
1101
+ end
1102
+
1103
+ #: (bool val) -> bool
1104
+ def show_last_column=(val)
1105
+ @options[:show_last_column] = val ? true : false
1106
+ end
1107
+
1108
+ #: (?Integer? val) -> Integer?
1109
+ def total_row_count(val = nil)
1110
+ if val.nil?
1111
+ @options[:total_row_count]
1112
+ else
1113
+ @options[:total_row_count] = val.to_i
1114
+ end
1115
+ end
1116
+
1117
+ #: (Integer val) -> Integer
1118
+ def total_row_count=(val)
1119
+ @options[:total_row_count] = val.to_i
1120
+ end
1121
+
1122
+ #: () -> Hash[Symbol, untyped]
1123
+ def to_h
1124
+ @options.dup
1125
+ end
1126
+ end
1127
+
1128
+ # Builder context for configuring data validation rules in block form.
1129
+ #
1130
+ # @api public
1131
+ class DataValidationBuilder
1132
+ # @param sqref [String, nil] Target cell or range reference.
1133
+ # @param opts [Hash] Additional validation properties.
1134
+ #: (?String? sqref, **untyped opts) -> void
1135
+ def initialize(sqref = nil, **opts)
1136
+ @options = opts.dup
1137
+ @options[:sqref] = sqref if sqref
1138
+ end
1139
+
1140
+ #: (untyped key) -> untyped
1141
+ def [](key)
1142
+ @options[key.to_sym]
1143
+ end
1144
+
1145
+ #: (untyped key, untyped value) -> untyped
1146
+ def []=(key, value)
1147
+ @options[key.to_sym] = value
1148
+ end
1149
+
1150
+ #: (?String? val) -> String?
1151
+ def sqref(val = nil)
1152
+ if val
1153
+ @options[:sqref] = val.to_s
1154
+ else
1155
+ @options[:sqref]
1156
+ end
1157
+ end
1158
+
1159
+ #: (String val) -> String
1160
+ def sqref=(val)
1161
+ @options[:sqref] = val.to_s
1162
+ end
1163
+ alias range sqref
1164
+ alias range= sqref=
1165
+
1166
+ #: (?untyped val) -> untyped
1167
+ def type(val = nil)
1168
+ if val
1169
+ @options[:type] = val
1170
+ else
1171
+ @options[:type]
1172
+ end
1173
+ end
1174
+
1175
+ #: (untyped val) -> untyped
1176
+ def type=(val)
1177
+ @options[:type] = val
1178
+ end
1179
+
1180
+ #: (?untyped val) -> untyped
1181
+ def operator(val = nil)
1182
+ if val
1183
+ @options[:operator] = val
1184
+ else
1185
+ @options[:operator]
1186
+ end
1187
+ end
1188
+
1189
+ #: (untyped val) -> untyped
1190
+ def operator=(val)
1191
+ @options[:operator] = val
1192
+ end
1193
+
1194
+ #: (?untyped val) -> untyped
1195
+ def formula1(val = nil)
1196
+ if val
1197
+ @options[:formula1] = val
1198
+ else
1199
+ @options[:formula1]
1200
+ end
1201
+ end
1202
+
1203
+ #: (untyped val) -> untyped
1204
+ def formula1=(val)
1205
+ @options[:formula1] = val
1206
+ end
1207
+
1208
+ #: (?untyped val) -> untyped
1209
+ def formula2(val = nil)
1210
+ if val
1211
+ @options[:formula2] = val
1212
+ else
1213
+ @options[:formula2]
1214
+ end
1215
+ end
1216
+
1217
+ #: (untyped val) -> untyped
1218
+ def formula2=(val)
1219
+ @options[:formula2] = val
1220
+ end
1221
+
1222
+ #: (?bool? val) -> bool?
1223
+ def allow_blank(val = nil)
1224
+ if val.nil?
1225
+ @options[:allow_blank]
1226
+ else
1227
+ @options[:allow_blank] = val ? true : false
1228
+ end
1229
+ end
1230
+
1231
+ #: (bool val) -> bool
1232
+ def allow_blank=(val)
1233
+ @options[:allow_blank] = val ? true : false
1234
+ end
1235
+
1236
+ #: (?bool? val) -> bool?
1237
+ def show_input_message(val = nil)
1238
+ if val.nil?
1239
+ @options[:show_input_message]
1240
+ else
1241
+ @options[:show_input_message] = val ? true : false
1242
+ end
1243
+ end
1244
+
1245
+ #: (bool val) -> bool
1246
+ def show_input_message=(val)
1247
+ @options[:show_input_message] = val ? true : false
1248
+ end
1249
+
1250
+ #: (?bool? val) -> bool?
1251
+ def show_error_message(val = nil)
1252
+ if val.nil?
1253
+ @options[:show_error_message]
1254
+ else
1255
+ @options[:show_error_message] = val ? true : false
1256
+ end
1257
+ end
1258
+
1259
+ #: (bool val) -> bool
1260
+ def show_error_message=(val)
1261
+ @options[:show_error_message] = val ? true : false
1262
+ end
1263
+
1264
+ #: (?String? val) -> String?
1265
+ def error_title(val = nil)
1266
+ if val
1267
+ @options[:error_title] = val.to_s
1268
+ else
1269
+ @options[:error_title]
1270
+ end
1271
+ end
1272
+
1273
+ #: (String val) -> String
1274
+ def error_title=(val)
1275
+ @options[:error_title] = val.to_s
1276
+ end
1277
+
1278
+ #: (?String? val) -> String?
1279
+ def error_message(val = nil)
1280
+ if val
1281
+ @options[:error] = val.to_s
1282
+ else
1283
+ @options[:error]
1284
+ end
1285
+ end
1286
+
1287
+ #: (String val) -> String
1288
+ def error_message=(val)
1289
+ @options[:error] = val.to_s
1290
+ end
1291
+ alias error error_message
1292
+ alias error= error_message=
1293
+
1294
+ #: (?String? val) -> String?
1295
+ def prompt_title(val = nil)
1296
+ if val
1297
+ @options[:prompt_title] = val.to_s
1298
+ else
1299
+ @options[:prompt_title]
1300
+ end
1301
+ end
1302
+
1303
+ #: (String val) -> String
1304
+ def prompt_title=(val)
1305
+ @options[:prompt_title] = val.to_s
1306
+ end
1307
+
1308
+ #: (?String? val) -> String?
1309
+ def prompt_message(val = nil)
1310
+ if val
1311
+ @options[:prompt] = val.to_s
1312
+ else
1313
+ @options[:prompt]
1314
+ end
1315
+ end
1316
+
1317
+ #: (String val) -> String
1318
+ def prompt_message=(val)
1319
+ @options[:prompt] = val.to_s
1320
+ end
1321
+ alias prompt prompt_message
1322
+ alias prompt= prompt_message=
1323
+
1324
+ #: (?untyped val) -> untyped
1325
+ def error_style(val = nil)
1326
+ if val
1327
+ @options[:error_style] = val
1328
+ else
1329
+ @options[:error_style]
1330
+ end
1331
+ end
1332
+
1333
+ #: (untyped val) -> untyped
1334
+ def error_style=(val)
1335
+ @options[:error_style] = val
1336
+ end
1337
+
1338
+ #: () -> Hash[Symbol, untyped]
1339
+ def to_h
1340
+ @options.dup
1341
+ end
1342
+ end
840
1343
  end