module Xlsxrb::DslHelpers
Internal helper module for normalizing and validating DSL arguments shared between {WorksheetBuilder} (in-memory) and {StreamWriter} (streaming).
@api private
Constants
- CF_ICON_SETS
- CF_OPERATORS
- CF_TIME_PERIODS
- CF_TYPES
- DATE_NUMBER_FORMAT
-
Number formats given to Date and Time cell values that have no format of their own.
- DEFAULT_STYLE_NAMES
-
Style names that select the workbook’s default cell format.
- FORBIDDEN_SHEET_CHARS
- INLINE_CELL_STYLES_HINT
-
Hint for a cell_styles Hash that reads like an inline style.
- MAX_CELL_TEXT_LENGTH
- MAX_SHEET_NAME_LENGTH
- TIME_NUMBER_FORMAT
- TOTALS_ROW_FUNCTIONS
Public Class Methods
(String range) → String
Source
# File lib/xlsxrb/dsl_helpers.rb, line 167 def self.absolute_range(range) range.gsub(/(?:\$)?([A-Z]+)(?:\$)?(\d+)\b/, '$\1$\2') end
Converts a cell range (e.g. “A1:B2” or “A1”) to an absolute reference format (e.g. “$A$1:$B$2” or “$A$1”).
@param range [String] @return [String]
(Array[Integer] col_run, Array[Integer] row_run) → String
Source
# File lib/xlsxrb/dsl_helpers.rb, line 364 def self.area_ref(col_run, row_run) c1, c2 = col_run r1, r2 = row_run ref1 = "#{Elements::Cell._column_letter(c1)}#{r1 + 1}" c1 == c2 && r1 == r2 ? ref1 : "#{ref1}:#{Elements::Cell._column_letter(c2)}#{r2 + 1}" end
Builds an A1-style reference from a column interval [col_start, col_end] (0-based) and a row interval [row_start, row_end] (0-based row index).
@param col_run [Array(Integer, Integer)] @param row_run [Array(Integer, Integer)] @return [String]
(String text) → void
Source
# File lib/xlsxrb/dsl_helpers.rb, line 814 def self.check_cell_text_length!(text) length = Ooxml::Utils.utf16_length(text) return if length <= MAX_CELL_TEXT_LENGTH raise ArgumentError, "Cell text length #{length} exceeds Excel limit of 32,767 characters, counted in UTF-16 code units as Excel does" end
Raises ArgumentError if text is longer than a cell holds: 32,767 UTF-16 code units, as Excel counts it (ADR 0011). Text of N UTF-16 code units has at least N bytes, so callers on hot paths call this only for text longer than MAX_CELL_TEXT_LENGTH bytes, which bytesize tells in O(1).
@param text [String] @return [void] @raise [ArgumentError]
(untyped column, untyped style) → void
Source
# File lib/xlsxrb/dsl_helpers.rb, line 223 def self.check_column_style!(column, style) raise ArgumentError, "#{column.inspect} is a style option, but #{INLINE_CELL_STYLES_HINT}" if StyleBuilder::OPTION_KEYS.include?(column) case style when nil, String, Symbol, Integer, Hash nil else raise ArgumentError, "#{style.inspect} for column #{column.inspect} is not a style; #{INLINE_CELL_STYLES_HINT}" end end
Raises when an entry of a cell_styles Hash cannot be a column mapped to a style: a style option name as the key (a column name has at most three letters), or a value that is not a style name, a style index, an inline style, or nil.
@param column [Object] The entry’s key. @param style [Object] The entry’s value. @return [void] @raise [ArgumentError]
(untyped name, untyped defined_names) → void
Source
# File lib/xlsxrb/dsl_helpers.rb, line 257 def self.check_style_name!(name, defined_names) return unless name.is_a?(String) || name.is_a?(Symbol) return if DEFAULT_STYLE_NAMES.include?(name) || defined_names.include?(name.to_s) user_names = defined_names.to_a.map { |n| n.is_a?(Array) ? n.first : n }.map(&:to_s).reject { |n| n.start_with?("__") }.sort message = "Undefined style #{name.inspect}" suggestion = closest_name(name, user_names) message << "; did you mean #{suggestion.to_sym.inspect}?" if suggestion message << (user_names.empty? ? " (no styles are defined; define one with style(name, ...))" : " (defined styles: #{user_names.map { |n| n.to_sym.inspect }.join(", ")})") raise ArgumentError, message end
Raises ArgumentError when name is a style name that has not been defined. Integers (style indexes), Hashes (inline styles), nil, and DEFAULT_STYLE_NAMES pass.
@param name [Object] Style reference given by the caller. @param defined_names [Array<String>, Hash{String => Object}] Defined style names. @return [void] @raise [ArgumentError] if name is an undefined style name.
(Array[Array[Integer]] col_runs, Array[Array[Integer]] row_runs, ?Integer max_per_validation) → Array[String]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 380 def self.chunked_sqrefs(col_runs, row_runs, max_per_validation = MAX_REFS_PER_VALIDATION) validate_runs!(row_runs, MAX_ROW, "Row", "rows") validate_runs!(col_runs, MAX_COLUMN, "Column", "columns") refs = col_runs.flat_map { |col_run| row_runs.map { |row_run| area_ref(col_run, row_run) } } refs.each_slice(max_per_validation).map { |chunk| chunk.join(" ") } end
Combines column intervals and row intervals into chunked space-delimited sqref strings respecting the maximum reference limit per dataValidation tag (default 32,767).
@param col_runs [Array<Array(Integer, Integer)>] @param row_runs [Array<Array(Integer, Integer)>] @param max_per_validation [Integer] @return [Array<String>] @raise [ArgumentError] if a run falls outside the Excel grid.
(String | Symbol word, Array[String | Symbol] candidates) → String?
Source
# File lib/xlsxrb/dsl_helpers.rb, line 243 def self.closest_name(word, candidates) return nil unless defined?(DidYouMean::SpellChecker) DidYouMean::SpellChecker.new(dictionary: candidates.map(&:to_s)).correct(word.to_s).first end
Returns the candidate closest to word (by DidYouMean), or nil.
@param word [String, Symbol] @param candidates [Array<String, Symbol>] @return [String, nil]
(untyped excluded, Integer min, Integer max) → Array[[Integer, Integer]]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 344 def self.complement_runs(excluded, min, max) runs = [] start_pt = min compress_runs(excluded).each do |e_start, e_end| if e_end >= start_pt && e_start <= max runs << [start_pt, e_start - 1] if e_start > start_pt start_pt = e_end + 1 end end runs << [start_pt, max] if start_pt <= max runs end
Returns minimal disjoint runs within min..max that are NOT present in excluded.
@param excluded [Integer, Range, Enumerable, nil] @param min [Integer] @param max [Integer] @return [Array<Array(Integer, Integer)>]
(untyped values) → Array[[Integer, Integer]]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 311 def self.compress_runs(values) intervals = [] items = values.is_a?(Array) ? values : [values] items.each do |item| case item when Range first = item.begin last = item.exclude_end? ? item.end - 1 : item.end intervals << [first, last] if first <= last when Integer intervals << [item, item] end end merged = [] intervals.sort.each do |s, e| last = merged.last if !last || s > last.last + 1 merged << [s, e] else last[1] = [last.last, e].max end end merged end
Compresses a sorted or unsorted collection of integers and/or ranges into minimal disjoint [start, finish] runs. Operates on intervals directly without expanding ranges into individual integer arrays.
@param values [Integer, Range, Enumerable, nil] @return [Array<Array(Integer, Integer)>]
(untyped value, String style_name, StyleBuilder? style_builder) → [String, String]?
Source
# File lib/xlsxrb/dsl_helpers.rb, line 282 def self.date_style_variant(value, style_name, style_builder) return nil if style_builder.nil? || style_builder.number_format case value when Time, DateTime then ["#{style_name}__xlsxrb_time", TIME_NUMBER_FORMAT] when Date then ["#{style_name}__xlsxrb_date", DATE_NUMBER_FORMAT] end end
Decides whether a Date or Time value styled with style_builder needs a variant of that style carrying the default date or time number format. Without it, a style such as bold: true replaces the date format and Excel shows the bare serial number.
@param value [Object] Cell value. @param style_name [String] Name of the cell’s style. @param style_builder [StyleBuilder, nil] The style’s definition. @return [Array(String, String), nil] Variant style name and number format, or nil.
(Hash[Symbol, untyped] rule) → Hash[Symbol, untyped]?
Source
# File lib/xlsxrb/dsl_helpers.rb, line 653 def self.extract_dxf_from_rule!(rule) font = (rule.delete(:font) || {}).dup font[:color] = rule.delete(:font_color) if rule.key?(:font_color) font[:bold] = rule.delete(:bold) if rule.key?(:bold) font[:italic] = rule.delete(:italic) if rule.key?(:italic) font[:underline] = rule.delete(:underline) if rule.key?(:underline) font[:font_size] = rule.delete(:font_size) if rule.key?(:font_size) fill = (rule.delete(:fill) || {}).dup if rule.key?(:fill_color) fill[:pattern] = "solid" fill[:fg_color] = rule.delete(:fill_color) end dxf = {} dxf[:font] = font unless font.empty? dxf[:fill] = fill unless fill.empty? dxf.empty? ? nil : dxf end
Extracts a differential format (dxf) hash from a conditional formatting rule hash. Mutates the rule hash by deleting extracted font and fill keys.
@param rule [Hash] @return [Hash, nil]
(String ref) → String?
Source
# File lib/xlsxrb/dsl_helpers.rb, line 419 def self.format_absolute_reference(ref) u = ref.upcase case u when /\A\$?[A-Z]{1,3}\$?\d+(?::\$?[A-Z]{1,3}\$?\d+)?\z/ absolute_range(u) when /\A\$?([A-Z]{1,3}):\$?([A-Z]{1,3})\z/, /\A\$?(\d+):\$?(\d+)\z/ "$#{Regexp.last_match(1)}:$#{Regexp.last_match(2)}" end end
Converts a cell reference, rectangular cell range, column range, or row range to absolute $ form. Returns nil if the input is not a coordinate or range.
@param ref [String] @return [String, nil]
(untyped index) → Array[Integer]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 71 def self.normalize_column_indices(index) case index when Range, Array index.map { |i| Elements::Cell._column_index(i) } else [Elements::Cell._column_index(index)] end end
Normalizes column index or letter or Range to an array of 0-based column integers.
@param index [Integer, String, Symbol, Range, Array] @return [Array<Integer>]
(?String? sqref, **untyped opts) ?{ (ConditionalFormatBuilder) → void } → Array[Hash[Symbol, untyped]]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 680 def self.normalize_conditional_format(sqref = nil, **opts) if block_given? builder = ConditionalFormatBuilder.new(sqref, **opts) yield builder opts = builder.to_h else opts = opts.dup end sqref = opts.delete(:sqref) || sqref # Determine sqrefs sqrefs = [] has_coords = opts.key?(:columns) || opts.key?(:rows) if sqref.nil? && has_coords cols_val = opts.delete(:columns) raw_cols = cols_val || 0 rows_val = opts.delete(:rows) raw_rows = rows_val || (0...Xlsxrb::MAX_ROW) except_rows = opts.delete(:except_rows) col_indices = normalize_column_indices(raw_cols) col_runs = compress_runs(col_indices) row_runs = compress_runs(raw_rows) row_runs = row_runs.flat_map { |s, e| complement_runs(except_rows, s, e) } if except_rows sqrefs = chunked_sqrefs(col_runs, row_runs) elsif sqref opts.delete(:columns) opts.delete(:rows) opts.delete(:except_rows) str_sqref = sqref.to_s refs = str_sqref.split sqrefs = if refs.size > Xlsxrb::MAX_REFS_PER_VALIDATION refs.each_slice(Xlsxrb::MAX_REFS_PER_VALIDATION).map { |chunk| chunk.join(" ") } else [str_sqref] end end raise ArgumentError, "conditional format sqref or coordinates (columns/rows) must be provided" if sqrefs.empty? # Normalize time_period if provided if opts.key?(:time_period) && opts[:time_period] tp = opts[:time_period] opts[:time_period] = CF_TIME_PERIODS[tp.to_sym] || tp.to_s end # Infer rule type if not explicitly provided if opts[:type].nil? if opts[:time_period] opts[:type] = :time_period elsif opts[:rank] opts[:type] = :top10 elsif !opts[:above_average].nil? || !opts[:equal_average].nil? || opts[:std_dev] opts[:type] = :above_average elsif opts[:text] opts[:type] = :contains_text end end type_str = opts[:type].to_s # Normalize color_scale if opts.key?(:color_scale) || type_str == "colorScale" || type_str == "color_scale" cs = (opts[:color_scale] || {}).dup colors = cs[:colors] || opts.delete(:colors) if colors.is_a?(Array) colors = colors.map do |c| c.is_a?(String) && c =~ /\A[0-9A-Fa-f]{6}\z/ ? "FF#{c.upcase}" : c end cs[:colors] = colors if !cs.key?(:cfvo) || cs[:cfvo].nil? cs[:cfvo] = if colors.size == 2 [{ type: "min" }, { type: "max" }] else [{ type: "min" }, { type: "percent", val: "50" }, { type: "max" }] end end end opts[:color_scale] = cs unless cs.empty? end # Normalize icon_set if opts.key?(:icon_set) || type_str == "iconSet" || type_str == "icon_set" is = (opts[:icon_set] || {}).dup style_name = is[:icon_set] || opts.delete(:icon_style) || is[:icon_style] if style_name mapped_style = CF_ICON_SETS[style_name.to_sym] || style_name.to_s is[:icon_set] = mapped_style if !is.key?(:cfvo) || is[:cfvo].nil? is[:cfvo] = if mapped_style.start_with?("4") [ { type: "percent", val: "0" }, { type: "percent", val: "25" }, { type: "percent", val: "50" }, { type: "percent", val: "75" } ] elsif mapped_style.start_with?("5") [ { type: "percent", val: "0" }, { type: "percent", val: "20" }, { type: "percent", val: "40" }, { type: "percent", val: "60" }, { type: "percent", val: "80" } ] else [ { type: "percent", val: "0" }, { type: "percent", val: "33" }, { type: "percent", val: "67" } ] end end end opts[:icon_set] = is unless is.empty? end sqrefs.map { |ref| opts.merge(sqref: ref) } end
Normalizes conditional format arguments (sqref, coordinate targeting, types, operators, color scales, icon sets).
@param sqref [String, nil] @param opts [Hash] @yield [ConditionalFormatBuilder] @return [Array<Hash>]
(?String? sqref, **untyped opts) ?{ (DataValidationBuilder) → void } → Array[Hash[Symbol, untyped]]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 516 def self.normalize_data_validation(sqref = nil, **opts) if block_given? builder = DataValidationBuilder.new(sqref, **opts) yield builder opts = builder.to_h else opts = opts.dup end sqref = opts.delete(:sqref) || sqref list_val = opts.delete(:list) if list_val.is_a?(Array) opts[:type] ||= "list" opts[:formula1] ||= %("#{list_val.join(",")}") end # ST_Xstring conversion for newlines in prompt and error messages opts[:prompt] = opts[:prompt].to_s.gsub(/\r\n|\r|\n/, "_x000a_") if opts[:prompt] opts[:error] = opts[:error].to_s.gsub(/\r\n|\r|\n/, "_x000a_") if opts[:error] opts[:prompt_title] = opts[:prompt_title].to_s.gsub(/\r\n|\r|\n/, "_x000a_") if opts[:prompt_title] opts[:error_title] = opts[:error_title].to_s.gsub(/\r\n|\r|\n/, "_x000a_") if opts[:error_title] # Auto-infer show_input_message / show_error_message opts[:show_input_message] = true if !opts.key?(:show_input_message) && (!opts[:prompt].nil? || !opts[:prompt_title].nil?) opts[:show_error_message] = true if !opts.key?(:show_error_message) && (!opts[:error].nil? || !opts[:error_title].nil?) # Determine sqrefs sqrefs = [] has_coords = opts.key?(:columns) || opts.key?(:rows) if sqref.nil? && has_coords cols_val = opts.delete(:columns) raw_cols = cols_val || 0 rows_val = opts.delete(:rows) raw_rows = rows_val || (0...Xlsxrb::MAX_ROW) except_rows = opts.delete(:except_rows) col_indices = normalize_column_indices(raw_cols) col_runs = compress_runs(col_indices) row_runs = compress_runs(raw_rows) row_runs = row_runs.flat_map { |s, e| complement_runs(except_rows, s, e) } if except_rows sqrefs = chunked_sqrefs(col_runs, row_runs) elsif sqref opts.delete(:columns) opts.delete(:rows) opts.delete(:except_rows) str_sqref = sqref.to_s refs = str_sqref.split sqrefs = if refs.size > Xlsxrb::MAX_REFS_PER_VALIDATION refs.each_slice(Xlsxrb::MAX_REFS_PER_VALIDATION).map { |chunk| chunk.join(" ") } else [str_sqref] end end raise ArgumentError, "data validation sqref or coordinates (columns/rows) must be provided" if sqrefs.empty? sqrefs.map { |ref| opts.merge(sqref: ref) } end
Normalizes data validation arguments (sqref, coordinate targeting, list arrays, input/error message inference).
@param sqref [String, nil] @param opts [Hash] @yield [DataValidationBuilder] @return [Array<Hash>]
(?untyped value, ?sheet: untyped, ?columns: untyped, ?rows: untyped, ?range: untyped) → String
Source
# File lib/xlsxrb/dsl_helpers.rb, line 439 def self.normalize_defined_name_value(value = nil, sheet: nil, columns: nil, rows: nil, range: nil) if range value = range elsif columns || rows c_letter = Elements::Cell._column_letter(columns ? Elements::Cell._column_index(columns) : 0) value = if rows.is_a?(Range) r1 = rows.begin r2 = rows.exclude_end? ? rows.end - 1 : rows.end "$#{c_letter}$#{r1 + 1}:$#{c_letter}$#{r2 + 1}" elsif rows "$#{c_letter}$#{rows + 1}" else "$#{c_letter}:$#{c_letter}" end end str = value.to_s abs = format_absolute_reference(str) if abs sheet ? "#{quote_sheet_name(sheet)}!#{abs}" : abs else str end end
Normalizes defined name value, auto-formatting bare coordinates and ranges with sheet quotes and absolute dollar references. Leaves constants, formulas, and already-qualified references untouched.
@param value [Object, nil] @param sheet [String, nil] @param col [Integer, String, nil] @param rows [Integer, Range, nil] @param range [String, nil] @return [String]
(String range, ?strict_excel_mode: bool) → String
Source
# File lib/xlsxrb/dsl_helpers.rb, line 20 def self.normalize_merge_range(range, strict_excel_mode: true) str = range.to_s raise ArgumentError, "Invalid merge range format: '#{str}'. Expected format like 'A1:B2'." if strict_excel_mode && !str.match?(/\A[A-Za-z]{1,3}\d+(:[A-Za-z]{1,3}\d+)?\z/) str end
Normalizes merge cell range string into a canonical range reference (e.g. “A1:C3”).
@param range [String] @param strict_excel_mode [Boolean] @return [String]
(?left: Float?, ?right: Float?, ?top: Float?, ?bottom: Float?, ?header: Float?, ?footer: Float?) → Hash[Symbol, Float]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 90 def self.normalize_page_margins(left: nil, right: nil, top: nil, bottom: nil, header: nil, footer: nil) { left: left, right: right, top: top, bottom: bottom, header: header, footer: footer }.compact end
Normalizes page margins into a compact hash.
@param left [Float, nil] @param right [Float, nil] @param top [Float, nil] @param bottom [Float, nil] @param header [Float, nil] @param footer [Float, nil] @return [Hash{Symbol => Float}]
(untyped columns) → String?
Source
# File lib/xlsxrb/dsl_helpers.rb, line 491 def self.normalize_print_titles_columns(columns) return nil if columns.nil? case columns when Integer, Symbol letter = Elements::Cell._column_letter(columns) "#{letter}:#{letter}" when Range c1 = Elements::Cell._column_letter(columns.begin) c2_val = columns.exclude_end? ? Elements::Cell._column_index(columns.end) - 1 : Elements::Cell._column_index(columns.end) c2 = Elements::Cell._column_letter(c2_val) "#{c1}:#{c2}" else s = columns.to_s s.include?(":") ? s : "#{s}:#{s}" end end
Normalizes print titles repeating column specification to an Excel range string (e.g. “A:B”). Accepts 0-based Integer, Symbol, Range, or Excel column range string.
@param columns [Integer, Symbol, Range, String, nil] @return [String, nil]
(untyped rows) → String?
Source
# File lib/xlsxrb/dsl_helpers.rb, line 470 def self.normalize_print_titles_rows(rows) return nil if rows.nil? case rows when Integer "#{rows + 1}:#{rows + 1}" when Range r1 = rows.begin r2 = rows.exclude_end? ? rows.end - 1 : rows.end "#{r1 + 1}:#{r2 + 1}" else rows.to_s end end
Normalizes print titles repeating row specification to an Excel range string (e.g. “1:2”). Accepts 0-based Integer, 0-based Range, or 1-based Excel range string.
@param rows [Integer, Range, String, nil] @return [String, nil]
(Hash[Symbol, untyped] opts) → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 32 def self.normalize_protection_options(opts) normalized = opts.dup plain_password = normalized[:password] needs_hash = plain_password.is_a?(String) && !plain_password.empty? && normalized[:algorithm_name].nil? && normalized[:hash_value].nil? && normalized[:salt_value].nil? && normalized[:spin_count].nil? && !plain_password.match?(/\A[0-9A-Fa-f]{4}\z/) if needs_hash normalized.delete(:password) normalized.merge!(Ooxml::Utils.hash_password(plain_password)) end normalized end
Normalizes sheet protection options and hashes plain-text password if supplied.
@param opts [Hash] @return [Hash]
(untyped styles) → untyped
Source
# File lib/xlsxrb/dsl_helpers.rb, line 194 def self.normalize_row_styles(styles) return styles unless styles.is_a?(Hash) styles_array = [] styles.each do |k, v| check_column_style!(k, v) if k.is_a?(Range) || k.is_a?(Array) k.each { |idx| styles_array[Elements::Cell._column_index(idx)] = v } else styles_array[Elements::Cell._column_index(k)] = v end end styles_array end
Normalizes row styles: if given a Hash of column-keyed styles (including Ranges/Arrays), expands to a sparse Array of styles. If already an Array or nil, returns as-is without allocations.
@param styles [untyped] @return [untyped] @raise [ArgumentError] If the Hash looks like an inline style rather than columns mapped to styles.
(untyped values) → untyped
Source
# File lib/xlsxrb/dsl_helpers.rb, line 177 def self.normalize_row_values(values) return values unless values.is_a?(Hash) cells_array = [] values.each do |k, v| cells_array[Elements::Cell._column_index(k)] = v end cells_array end
Normalizes row values: if given a column-keyed Hash, converts to a sparse Array. If already an Array or other non-Hash enumerable, returns as-is without allocations.
@param values [Array<Object>, Hash{String, Integer, Symbol => Object}, nil] @return [Array<Object>, nil]
(String ref, columns: untyped, ?name: String?, ?display_name: String?, ?style: (String | Hash[Symbol, untyped])?, **untyped opts) → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 118 def self.normalize_table_options(ref, columns:, name: nil, display_name: nil, style: nil, **opts) tbl = { ref: ref } cols = columns.is_a?(Array) ? columns : [columns] tbl[:columns] = cols.map do |c| if c.is_a?(Hash) col = c.dup col[:name] = col[:name].to_s if col.key?(:name) calc_f = col[:calculated_column_formula] || col[:formula] col[:calculated_column_formula] = calc_f.to_s if calc_f tot_f = col[:totals_row_formula] col[:totals_row_formula] = tot_f.to_s if tot_f if col.key?(:totals_row_function) fn = col[:totals_row_function] col[:totals_row_function] = TOTALS_ROW_FUNCTIONS[fn.to_sym] || fn.to_s elsif tot_f col[:totals_row_function] = "custom" end col else c.to_s end end tbl[:name] = name.to_s if name tbl[:display_name] = display_name.to_s if display_name tbl[:style] = style if style # Normalize total row options if opts.key?(:totals_row) tr = opts.delete(:totals_row) tbl[:totals_row_count] = if tr == true 1 elsif tr == false 0 else tr.to_i end elsif opts.key?(:totals_row_count) tbl[:totals_row_count] = opts.delete(:totals_row_count).to_i end tbl.merge!(opts) tbl end
Normalizes table options hash.
@param ref [String] @param columns [Array<String>, Array<Hash>, Array<Symbol>] @param name [String, nil] @param display_name [String, nil] @param style [String, Hash, nil] @param opts [Hash] @return [Hash]
(Hash[Symbol, untyped] opts) → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/dsl_helpers.rb, line 53 def self.normalize_workbook_protection(opts) normalized = opts.dup password = normalized.delete(:password) return normalized if password.nil? || password.to_s.empty? if password.to_s.match?(/\A[0-9A-Fa-f]{4}\z/) normalized[:workbook_password] ||= password.to_s else normalized.merge!(Ooxml::Utils.hash_password(password.to_s)) end normalized end
Normalizes workbook protection options. A plain-text password is hashed into algorithm_name, hash_value, salt_value, and spin_count (as for sheet protection); a legacy 4-digit hexadecimal hash is kept as workbook_password.
@param opts [Hash] @return [Hash]
(untyped sheet_name) → String
Source
# File lib/xlsxrb/dsl_helpers.rb, line 409 def self.quote_sheet_name(sheet_name) "'#{sheet_name.to_s.gsub("'", "''")}'" end
Quotes a sheet name for use in formula and defined name references, escaping single quotes with double single quotes.
@param sheet_name [String, Object] @return [String]
(untyped name, ?replace_char: String) → String
Source
# File lib/xlsxrb/dsl_helpers.rb, line 845 def self.sanitize_sheet_name(name, replace_char: "_") raw = name.to_s.strip str = raw.gsub(FORBIDDEN_SHEET_CHARS, replace_char) str = str.sub(/\A'+/) { replace_char * Regexp.last_match(0).length } str = "Sheet" if str.empty? # Excel counts the limit in UTF-16 code units (ADR 0011); keep whole characters. units = 0 base = str.each_char.take_while { |char| (units += Ooxml::Utils.utf16_length(char)) <= MAX_SHEET_NAME_LENGTH }.join base = base.sub(/'+\z/) { replace_char * Regexp.last_match(0).length } return "#{base}#{replace_char}" if base.casecmp?("History") base end
Sanitizes a worksheet name to ensure it conforms to Excel constraints within 31 characters. Replaces forbidden characters (\ / ? * [ ] : and control characters), replaces leading/trailing apostrophes, and replaces reserved ‘History’ with ‘History_’.
@param name [Object] Desired worksheet name. @param replace_char [String] Replacement character for invalid characters. @return [String] Excel-compliant worksheet name.
(String text) → String
Source
# File lib/xlsxrb/dsl_helpers.rb, line 828 def self.truncate_cell_text(text) return text if text.bytesize <= MAX_CELL_TEXT_LENGTH units = text.encode(Encoding::UTF_16LE).byteslice(0, MAX_CELL_TEXT_LENGTH * 2) #: String # A cut between the two code units of a character leaves a lone high surrogate. units = units.byteslice(0, units.bytesize - 2) unless units.valid_encoding? #: String units.encode(Encoding::UTF_8) end
Cuts text to the whole characters that fit in a cell: 32,767 UTF-16 code units, as Excel counts them (ADR 0011). Text of 32,767 bytes or fewer always fits and is returned as is, so most text is not counted. Longer text is returned as a new UTF-8 String.
@param text [String] @return [String]
(Integer row_index, Float | Integer | nil height, ?strict_excel_mode: bool) → void
Source
# File lib/xlsxrb/dsl_helpers.rb, line 298 def self.validate_row_bounds!(row_index, height, strict_excel_mode: true) return unless strict_excel_mode raise ArgumentError, "Row index #{row_index} exceeds Excel limit of 1,048,576 rows" if row_index >= 1_048_576 raise ArgumentError, "Row height #{height} must be between 0 and 409 points (Excel limitation)" if height && (height.negative? || height > 409) end
Validates row index and height against Excel limits in strict mode.
@param row_index [Integer] @param height [Float, Integer, nil] @param strict_excel_mode [Boolean] @return [void]
(Array[Array[Integer]] runs, Integer limit, String label, String plural) → void
Source
# File lib/xlsxrb/dsl_helpers.rb, line 395 def self.validate_runs!(runs, limit, label, plural) runs.each do |first, last| bad = first.negative? ? first : last next if bad.between?(0, limit - 1) raise ArgumentError, "#{label} index #{bad} is out of range: #{plural} are 0-based and must be between 0 and #{limit - 1}" end end
Raises if any [start, finish] run lies outside 0...limit.
@param runs [Array<Array(Integer, Integer)>] @param limit [Integer] Number of valid indices (e.g. MAX_ROW). @param label [String] Capitalized axis name for the error message. @param plural [String] Plural axis name for the error message. @return [void]