class Xlsxrb::Elements::Workbook
Represents an entire in-memory XLSX workbook.
@example Access sheets
workbook = Xlsxrb.read("report.xlsx") sheet = workbook.sheet(0) # or workbook["Sheet1"] workbook.each { |s| puts s.name }
@api public Class syntax is required for the mutant subject matcher.
Public Class Methods
(?sheets: Array[Elements::Worksheet], ?shared_strings: Array[String], ?styles: Elements::Styles | Hash[untyped, untyped], ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?, ?defined_names: Array[untyped]?, ?workbook_properties: (Elements::WorkbookProperties | Hash[Symbol, untyped])?, ?core_properties: (Elements::CoreProperties | Hash[Symbol, untyped])?, ?app_properties: (Elements::AppProperties | Hash[Symbol, untyped])?, ?workbook_protection: (Elements::WorkbookProtection | Hash[Symbol, untyped])?, ?custom_properties: (Hash[String, Elements::CustomProperty] | Hash[untyped, untyped] | Array[untyped])?, ?date1904: bool) → void
Source
# File lib/xlsxrb/elements/workbook.rb, line 37 def initialize(sheets: [], shared_strings: [], styles: {}, unmapped_data: {}, errors: nil, defined_names: nil, workbook_properties: nil, core_properties: nil, app_properties: nil, workbook_protection: nil, custom_properties: nil, date1904: false) dns = (defined_names || unmapped_data[:defined_names] || unmapped_data.dig(:facade, :defined_names) || []).map do |dn| dn.is_a?(Elements::DefinedName) ? dn : Elements::DefinedName.new(**dn) end computed_errors = errors || self.class.validate(sheets) unmapped = unmapped_data raw_wb_props = workbook_properties || unmapped[:workbook_properties] || unmapped.dig(:facade, :workbook_properties) || {} norm_wb_props = raw_wb_props.is_a?(Elements::WorkbookProperties) ? raw_wb_props : Elements::WorkbookProperties.new(**raw_wb_props) if date1904 && !norm_wb_props.date1904? norm_wb_props = norm_wb_props.with(date1904: true) unmapped = unmapped.dup unmapped[:workbook_properties] = (unmapped[:workbook_properties] || {}).merge(date1904: true) end raw_core = core_properties || unmapped[:core_properties] || unmapped.dig(:facade, :core_properties) || {} norm_core = raw_core.is_a?(Elements::CoreProperties) ? raw_core : Elements::CoreProperties.new(**raw_core) raw_app = app_properties || unmapped[:app_properties] || unmapped.dig(:facade, :app_properties) || {} norm_app = raw_app.is_a?(Elements::AppProperties) ? raw_app : Elements::AppProperties.new(**raw_app) raw_prot = workbook_protection || unmapped[:workbook_protection] || unmapped.dig(:facade, :workbook_protection) norm_prot = if raw_prot.nil? nil elsif raw_prot.is_a?(Elements::WorkbookProtection) raw_prot else Elements::WorkbookProtection.new(**raw_prot) end raw_custom = custom_properties || unmapped[:custom_properties] || unmapped.dig(:facade, :custom_properties) || {} norm_custom = if raw_custom.is_a?(Hash) raw_custom.map do |k, v| if v.is_a?(Elements::CustomProperty) v elsif v.is_a?(Hash) Elements::CustomProperty.new(name: (v[:name] || v["name"] || k).to_s, value: v.key?(:value) ? v[:value] : v["value"], type: v[:type] || v["type"]) else Elements::CustomProperty.new(name: k.to_s, value: v) end end elsif raw_custom.is_a?(Array) raw_custom.map do |v| if v.is_a?(Hash) Elements::CustomProperty.new(name: (v[:name] || v["name"]).to_s, value: v.key?(:value) ? v[:value] : v["value"], type: v[:type] || v["type"]) else v end end else [] end.to_h { |cp| [cp.name.to_s, cp] }.freeze norm_styles = styles.is_a?(Elements::Styles) ? styles : Elements::Styles.new(styles || {}) super(**{ sheets: sheets.freeze, shared_strings: shared_strings.freeze, styles: norm_styles, unmapped_data: unmapped, errors: computed_errors.freeze, defined_names: dns.freeze, workbook_properties: norm_wb_props, core_properties: norm_core, app_properties: norm_app, workbook_protection: norm_prot, custom_properties: norm_custom }) end
@param sheets [Array<Elements::Worksheet>] Worksheets in the workbook. @param shared_strings [Array<String>] Shared strings table. @param styles [Elements::Styles, Hash] Styles definition. @param unmapped_data [Hash] Additional metadata for round-tripping. @param errors [Array<String>, nil] Validation errors. @param defined_names [Array<Hash>, nil] Defined names list. @param workbook_properties [Elements::WorkbookProperties, Hash, nil] Workbook properties. @param core_properties [Elements::CoreProperties, Hash, nil] Core properties. @param app_properties [Elements::AppProperties, Hash, nil] App properties. @param workbook_protection [Elements::WorkbookProtection, Hash, nil] Workbook protection. @param custom_properties [Hash, Array, nil] Custom properties. @param date1904 [Boolean] Whether the workbook uses the 1904 date system.
(untyped sheets) → Array[String]
Source
# File lib/xlsxrb/elements/workbook.rb, line 362 def self.validate(sheets) errs = [] errs << "sheets must be an Array (got #{sheets.class})" unless sheets.is_a?(Array) if sheets.is_a?(Array) errs << "workbook must have at least one sheet" if sheets.empty? names = sheets.map(&:name) if names.uniq.size != names.size dups = names.select { |n| names.count(n) > 1 }.uniq errs << "duplicate sheet name: #{dups.map(&:inspect).join(", ")} — sheet names must be unique" end end errs end
Validates workbook structure according to OOXML specifications.
@param sheets [Array<Elements::Worksheet>] @return [Array<String>] List of error messages. @api private
Public Instance Methods
() → Elements::Worksheet?
Source
# File lib/xlsxrb/elements/workbook.rb, line 158 def active_sheet idx = active_sheet_index idx ? sheets[idx] : sheets.first end
Returns the active worksheet in the workbook, or the first worksheet if not set.
@return [Elements::Worksheet, nil] @api public
() → Integer?
Source
# File lib/xlsxrb/elements/workbook.rb, line 149 def active_sheet_index workbook_properties.active_sheet end
Returns the 0-based active worksheet index from workbookView, or nil if not set.
@return [Integer, nil] @api public
() → String?
Source
# File lib/xlsxrb/elements/workbook.rb, line 140 def code_name workbook_properties.code_name end
Returns the internal VBA or application code name of the workbook, or nil if not specified.
@return [String, nil] @api public
() → bool
Source
# File lib/xlsxrb/elements/workbook.rb, line 131 def date1904? workbook_properties.date1904? end
Returns whether the workbook uses the 1904 date system.
@return [Boolean] @api public
(String name, ?sheet: (String | Symbol | Integer)?) → Elements::DefinedName?
Source
# File lib/xlsxrb/elements/workbook.rb, line 210 def defined_name(name, sheet: nil) sheet_idx = case sheet when Integer then sheet when String, Symbol target = sheet.to_s sheets.find_index { |s| s.name == target } end dn_name = ->(dn) { dn.respond_to?(:name) ? dn.name : dn[:name] } dn_local = ->(dn) { dn.respond_to?(:local_sheet_id) ? dn.local_sheet_id : dn[:local_sheet_id] } if sheet_idx defined_names.find { |dn| dn_name.call(dn) == name && dn_local.call(dn) == sheet_idx } else defined_names.find { |dn| dn_name.call(dn) == name && dn_local.call(dn).nil? } || defined_names.find { |dn| dn_name.call(dn) == name } end end
Returns the defined name matching name (and optionally sheet), or nil.
@example
wb.defined_name("SalesTotal") wb.defined_name("Print_Area", sheet: "Sheet1")
@param name [String] The defined name. @param sheet [String, Symbol, Integer, nil] Optional sheet name or 0-based index. @return [Elements::DefinedName, nil] @api public
() { (Elements::Worksheet) → void } → void
() → Enumerator[Elements::Worksheet, void]
Source
# File lib/xlsxrb/elements/workbook.rb, line 114 def each(&) sheets.each(&) end
Iterate over worksheets.
@example
workbook.each do |sheet| puts sheet.name end
@yield [sheet] @yieldparam sheet [Elements::Worksheet] @return [Enumerator, void]
() → Integer?
Source
# File lib/xlsxrb/elements/workbook.rb, line 168 def first_sheet workbook_properties.first_sheet end
Returns the index of the first sheet tab visible in the sheet tab scroll area.
@return [Integer, nil] @api public
(Integer | String | untyped sheet_identifier, String | Symbol | Integer ref_or_row, ?Integer? col) → String?
Source
# File lib/xlsxrb/elements/workbook.rb, line 259 def formatted_value(sheet_identifier, ref_or_row, col = nil) target_sheet = sheet(sheet_identifier) target_sheet&.formatted_value(ref_or_row, col) end
Returns the formatted string representation of a cell’s value in the specified sheet.
@param sheet_identifier [Integer, String, untyped] 0-based index or sheet name. @param ref_or_row [String, Symbol, Integer] Cell reference (e.g. “A1”) or 0-based row index. @param col [Integer, nil] Optional 0-based column index. @return [String, nil] @api public
() → bool
Source
# File lib/xlsxrb/elements/workbook.rb, line 195 def full_calc_on_load? workbook_properties.full_calc_on_load? end
Returns whether Excel is instructed to perform a full recalculation when loading the workbook.
@return [Boolean] @api public
() → Array[Elements::Image]
Source
# File lib/xlsxrb/elements/workbook.rb, line 269 def images sheets.flat_map(&:images) end
Returns all embedded images across all worksheets.
@return [Array<Elements::Image>] @api public
(Integer | String | untyped sheet_identifier, String | Symbol | Integer ref_or_row, ?Integer? col) → Array[Elements::Image]
Source
# File lib/xlsxrb/elements/workbook.rb, line 281 def images_at(sheet_identifier, ref_or_row, col = nil) ident = sheet_identifier.is_a?(Symbol) ? sheet_identifier.to_s : sheet_identifier target_sheet = sheet(ident) return [] unless target_sheet target_sheet.images_at(ref_or_row, col) end
Returns all images anchored at a specific cell in the given worksheet.
@param sheet_identifier [Integer, String, untyped] Sheet index or sheet name. @param ref_or_row [String, Symbol, Integer] Cell reference (e.g. “A1”) or 0-based row index. @param col [Integer, nil] Optional 0-based column index. @return [Array<Elements::Image>] @api public
(String | IO | StringIO filepath, ?shared_strings: bool, ?compression_level: Integer?, **untyped options) → void
Source
# File lib/xlsxrb/elements/workbook.rb, line 313 def save(filepath, shared_strings: true, compression_level: nil, **options) Xlsxrb.write(filepath, self, shared_strings: shared_strings, compression_level: compression_level, **options) end
Save the workbook to an XLSX file.
@example
wb.save("output.xlsx")
@param filepath [String, IO, StringIO] Destination file path or IO stream. @param shared_strings [Boolean] Whether to intern strings into a shared strings table. @param compression_level [Integer, nil] ZIP deflate compression level (0..9). @param options [Hash] Optional write options forwarded to {Xlsxrb.write} (e.g. password:, encryption_mode:). @return [void] @api public
(?Integer | String | Symbol | untyped identifier) → Elements::Worksheet?
Source
# File lib/xlsxrb/elements/workbook.rb, line 240 def sheet(identifier = 0) case identifier when Integer sheets[identifier] when String, Symbol target = identifier.to_s sheets.find { |s| s.name == target } end end
Returns the worksheet at the given 0-based index or by name.
@example
wb.sheet(0) wb.sheet("Sales") wb.sheet(:Sales)
@param identifier [Integer, String, Symbol, untyped] 0-based index or sheet name. @return [Elements::Worksheet, nil] @api public
() → Array[String]
Source
# File lib/xlsxrb/elements/workbook.rb, line 297 def sheet_names sheets.map(&:name) end
Loads all sheets into memory, returning an Elements::Workbook where every worksheet is a fully-parsed Elements::Worksheet supporting coordinate random access.
Returns an Array of all worksheet names.
@return [Array<String>] @api public
() → bool
Source
# File lib/xlsxrb/elements/workbook.rb, line 177 def show_sheet_tabs? workbook_properties.show_sheet_tabs? end
Returns whether sheet tabs are displayed in the workbook window.
@return [Boolean] @api public
() → Integer?
Source
# File lib/xlsxrb/elements/workbook.rb, line 186 def tab_ratio workbook_properties.tab_ratio end
Returns the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
@return [Integer, nil] @api public
(?shared_strings: bool, ?compression_level: Integer?, **untyped options) → String
Source
# File lib/xlsxrb/elements/workbook.rb, line 328 def to_data(shared_strings: true, compression_level: nil, **options) Xlsxrb.write(self, shared_strings: shared_strings, compression_level: compression_level, **options) end
Serializes the workbook to a binary XLSX data string in memory.
@example Export workbook bytes
data = wb.to_data
@param shared_strings [Boolean] Whether to intern strings into a shared strings table. @param compression_level [Integer, nil] ZIP deflate compression level (0..9). @param options [Hash] Additional write options forwarded to {Xlsxrb.write} (e.g. password:, encryption_mode:). @return [String] Binary XLSX data. @api public
() → bool
Source
# File lib/xlsxrb/elements/workbook.rb, line 122 def valid? errors.empty? end
Returns whether the workbook is valid according to ECMA-376 rules.
@return [Boolean]
(**untyped) → Elements::Workbook
Source
# File lib/xlsxrb/elements/workbook.rb, line 339 def with(**changes) return self if changes.empty? updated = changes.dup if updated.key?(:sheets) && !updated.key?(:defined_names) && updated[:sheets].is_a?(Array) synced = sync_sheet_print_defined_names(updated[:sheets]) updated[:defined_names] = synced if synced end unless updated.key?(:unmapped_data) cleaned = strip_stale_unmapped_data(updated) updated[:unmapped_data] = cleaned if cleaned end super(**updated) end
Returns a new Workbook with attributes replaced, clearing stale unmapped/facade metadata and keeping sheet-scoped print defined names synchronized when sheets change.
@param changes [Hash] @return [Elements::Workbook] @api public