class Xlsxrb::Elements::Worksheet
Represents a single fully parsed, in-memory worksheet in a workbook. Provides coordinate random access (sheet), row lookups (row_at), and immutable cell updates (update_cell).
@example Access cells and rows
sheet = workbook.sheet(0).load cell = sheet["A1"] row = sheet.row_at(0)
Attributes
String?
Elements::BackgroundImage?
Array[String]
Array[Elements::Chart]
Array[Elements::PageBreak]
Array[Elements::Column]
Array[Elements::Comment]
Array[Elements::ConditionalFormat]
@return [Array<Hash[Symbol, untyped]>]
Corresponds to ECMA-376 Part 4 §18.3.1.18 (<conditionalFormatting> and <cfRule> elements). In the OpenXML specification, conditional formatting definitions are emitted as ‘<conditionalFormatting>` elements containing individual rule definitions (`<cfRule>`). While ECMA-376 uses the uncountable noun conditionalFormatting as the XML element name, xlsxrb exposes these rules collectively as {#conditional_formats} (Array of Hashes) to maintain symmetric plural collection naming alongside other collections (e.g. {#tables}, {#data_validations}, {#merged_cells}) and paired with the singular builder method {WorksheetBuilder#conditional_format}.
Array[Elements::DataValidation]
Array[String]
Hash[Integer, Elements::FilterColumn]
Elements::FreezePane?
Hash[String, Elements::Hyperlink]
Array[Elements::IgnoredError]
Array[Elements::Image]
Array[String]
String?
Elements::PageMargins?
Elements::PageSetup
Elements::PhoneticProperties?
Array[Elements::PivotTable]
String?
Elements::PrintOptions
Elements::PrintTitles?
Array[Elements::ProtectedRange]
Array[Elements::PageBreak]
Array[Elements::Row]
(Elements::Selection | Array[Elements::Selection])?
Array[Elements::Shape]
Elements::SheetFormat
Elements::SheetProperties
Elements::SheetProtection?
Elements::SheetView
Elements::SortState?
Array[Elements::SparklineGroup]
Elements::SplitPane?
Symbol
(Elements::Styles | Hash[untyped, untyped])?
Array[Elements::Table]
Array[Elements::ThreadedComment]
Hash[untyped, untyped]
Public Class Methods
() → Regexp
Source
# File lib/xlsxrb/elements/worksheet.rb, line 1043 def self.forbidden_name_chars FORBIDDEN_NAME_CHARS end
@api private
(name: String?, ?rows: Array[Elements::Row], ?columns: Array[Elements::Column], ?charts: Array[untyped], ?data_validations: Array[untyped], ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?, ?conditional_formats: Array[untyped]?, ?state: Symbol, ?hyperlinks: Hash[String, untyped], ?comments: Array[untyped], ?images: Array[Elements::Image], ?styles: (Elements::Styles | Hash[untyped, untyped])?, ?date1904: bool, ?dimension: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?print_area: String?, ?print_titles: untyped, ?sparkline_groups: Array[untyped], ?merged_cells: Array[String], ?auto_filter: String?, ?tables: Array[untyped], ?shapes: Array[untyped], ?freeze_pane: untyped, ?page_setup: untyped, ?page_margins: untyped, ?header_footer: untyped, ?print_options: untyped, ?sheet_protection: untyped, ?sheet_view: untyped, ?sheet_properties: untyped, ?sheet_format: untyped, ?row_breaks: Array[untyped], ?column_breaks: Array[untyped]?, ?protected_ranges: Array[untyped], ?cell_watches: Array[String], ?ignored_errors: Array[untyped], ?phonetic_properties: untyped, ?selection: untyped, ?sheet_calc_properties: untyped, ?background_image: untyped, ?threaded_comments: Array[untyped], ?pivot_tables: Array[untyped]?, ?split_pane: untyped, ?filter_columns: Hash[Integer, untyped]?, ?sort_state: untyped, ?header_footer_images: Array[untyped]?) → void
Source
# File lib/xlsxrb/elements/worksheet.rb, line 182 def initialize(name:, rows: [], columns: [], charts: [], data_validations: [], unmapped_data: {}, errors: nil, conditional_formats: nil, state: :visible, hyperlinks: {}, comments: [], images: [], styles: nil, date1904: false, dimension: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, print_area: nil, print_titles: nil, sparkline_groups: [], merged_cells: [], auto_filter: nil, tables: [], shapes: [], freeze_pane: nil, page_setup: {}, page_margins: nil, header_footer: {}, print_options: {}, sheet_protection: nil, sheet_view: {}, sheet_properties: {}, sheet_format: {}, row_breaks: [], column_breaks: nil, protected_ranges: [], cell_watches: [], ignored_errors: [], phonetic_properties: nil, selection: nil, sheet_calc_properties: nil, background_image: nil, threaded_comments: [], pivot_tables: nil, split_pane: nil, filter_columns: nil, sort_state: nil, header_footer_images: nil) @name = name @rows = (rows || []).freeze @columns = (columns || []).freeze @charts = (charts || []).map { |c| c.is_a?(Elements::Chart) ? c : Elements::Chart.new(**c) }.freeze cf = (conditional_formats || []).map do |c| if c.is_a?(Hash) Elements::ConditionalFormat.new(**c) else c end end @conditional_formats = cf.freeze @data_validations = (data_validations || []).map { |dv| dv.is_a?(Elements::DataValidation) ? dv : Elements::DataValidation.new(**dv) }.freeze @unmapped_data = (unmapped_data || {}).freeze computed_errors = errors || self.class.validate(@name, @rows) @errors = computed_errors.freeze @state = state ? state.to_sym : :visible @hyperlinks = (hyperlinks || {}).to_h do |ref, hl| hl_obj = case hl when Hash Elements::Hyperlink.new(**hl) when String Elements::Hyperlink.new(url: hl) else hl end [ref.to_s, hl_obj] end.freeze @comments = (comments || []).map { |c| c.is_a?(Elements::Comment) ? c : Elements::Comment.new(**c) }.freeze @images = (images || []).freeze @styles = styles @date1904 = date1904 ? true : false @dimension = dimension @print_area = print_area @print_titles = if print_titles.nil? nil elsif print_titles.is_a?(Elements::PrintTitles) print_titles else pt_cols = print_titles[:columns] || print_titles[:cols] Elements::PrintTitles.new(rows: print_titles[:rows], columns: pt_cols) end @sparkline_groups = (sparkline_groups || []).map do |sg| if sg.is_a?(Hash) sls = (sg[:sparklines] || []).map do |s| if s.is_a?(Hash) sqref = s[:sqref] || s[:location_ref] || s[:location] formula = s[:formula] || s[:data_ref] || s[:f] || s[:data] Elements::Sparkline.new(sqref: sqref&.to_s, formula: formula&.to_s) elsif s.is_a?(Elements::Sparkline) s else Elements::Sparkline.new(sqref: s.to_s) end end Elements::SparklineGroup.new(**sg, sparklines: sls) else sg end end.freeze @trim_empty_rows = trim_empty_rows ? true : false @pad_empty_rows = pad_empty_rows ? true : false @pad_empty_cells = pad_empty_cells ? true : false @threaded_comments = (threaded_comments || []).map { |tc| tc.is_a?(Elements::ThreadedComment) ? tc : Elements::ThreadedComment.new(**tc) }.freeze @merged_cells = (merged_cells || []).freeze @auto_filter = auto_filter @tables = (tables || []).map do |tb| if tb.is_a?(Hash) cols = (tb[:columns] || []).each_with_index.map do |col, idx| if col.is_a?(Hash) Elements::TableColumn.new(**col) elsif col.is_a?(Elements::TableColumn) col else Elements::TableColumn.new(name: col.to_s, id: idx + 1) end end si = tb[:style_info] || Elements::Table.style_info_from(tb) si = Elements::TableStyleInfo.new(**si) if si.is_a?(Hash) Elements::Table.new(**tb, columns: cols, style_info: si) else tb end end.freeze @shapes = (shapes || []).map { |s| s.is_a?(Elements::Shape) ? s : Elements::Shape.new(**s) }.freeze @freeze_pane = if freeze_pane.nil? nil elsif freeze_pane.is_a?(Elements::FreezePane) freeze_pane else Elements::FreezePane.new(**freeze_pane) end ps = page_setup || {} @page_setup = ps.is_a?(Elements::PageSetup) ? ps : Elements::PageSetup.new(**ps) @page_margins = if page_margins.nil? nil elsif page_margins.is_a?(Elements::PageMargins) page_margins else Elements::PageMargins.new(**page_margins) end hf = header_footer || {} @header_footer = hf.is_a?(Elements::HeaderFooter) ? hf : Elements::HeaderFooter.new(**hf) po = print_options || {} @print_options = po.is_a?(Elements::PrintOptions) ? po : Elements::PrintOptions.new(**po) @sheet_protection = if sheet_protection.nil? nil elsif sheet_protection.is_a?(Elements::SheetProtection) sheet_protection else Elements::SheetProtection.new(**sheet_protection) end sv = sheet_view || {} @sheet_view = sv.is_a?(Elements::SheetView) ? sv : Elements::SheetView.new(**sv) sp = sheet_properties || {} @sheet_properties = sp.is_a?(Elements::SheetProperties) ? sp : Elements::SheetProperties.new(**sp) sf = sheet_format || unmapped_data[:sheet_format] || unmapped_data.dig(:facade, :sheet_format) || {} @sheet_format = sf.is_a?(Elements::SheetFormat) ? sf : Elements::SheetFormat.new(**sf) @row_breaks = (row_breaks || []).map do |brk| if brk.is_a?(Elements::PageBreak) brk elsif brk.is_a?(Hash) Elements::PageBreak.new(**brk) else Elements::PageBreak.new(id: brk.to_i, max: 16_383, man: 1) end end.freeze raw_col_breaks = column_breaks || [] breaks = raw_col_breaks.map do |brk| if brk.is_a?(Elements::PageBreak) brk elsif brk.is_a?(Hash) Elements::PageBreak.new(**brk) else col_id = Elements::Cell._column_index(brk) Elements::PageBreak.new(id: col_id, max: 1_048_575, man: 1) end end.freeze @column_breaks = breaks pr = protected_ranges || unmapped_data[:protected_ranges] || unmapped_data.dig(:facade, :protected_ranges) || [] @protected_ranges = pr.map { |r| r.is_a?(Elements::ProtectedRange) ? r : Elements::ProtectedRange.new(**r) }.freeze @cell_watches = (cell_watches || unmapped_data[:cell_watches] || unmapped_data.dig(:facade, :cell_watches) || []).freeze ie = ignored_errors || unmapped_data[:ignored_errors] || unmapped_data.dig(:facade, :ignored_errors) || [] @ignored_errors = ie.map { |r| r.is_a?(Elements::IgnoredError) ? r : Elements::IgnoredError.new(**r) }.freeze pp = phonetic_properties || unmapped_data[:phonetic_properties] || unmapped_data.dig(:facade, :phonetic_properties) @phonetic_properties = if pp.nil? nil elsif pp.is_a?(Elements::PhoneticProperties) pp else Elements::PhoneticProperties.new(**pp) end @selection = if selection.nil? nil elsif selection.is_a?(Elements::Selection) selection elsif selection.is_a?(Array) selection.map { |s| s.is_a?(Elements::Selection) ? s : Elements::Selection.new(**s) } else Elements::Selection.new(**selection) end full_calc = if @sheet_properties.respond_to?(:full_calc_on_load?) @sheet_properties.full_calc_on_load? elsif @sheet_properties.is_a?(Hash) @sheet_properties[:full_calc_on_load] end calc = sheet_calc_properties || unmapped_data[:sheet_calc_properties] || unmapped_data[:sheet_calc_pr] || unmapped_data.dig(:facade, :sheet_calc_properties) || unmapped_data.dig(:facade, :sheet_calc_pr) || (full_calc ? { full_calc_on_load: true } : nil) @sheet_calc_pr = if calc.nil? nil elsif calc.is_a?(Elements::SheetCalcProperties) calc else Elements::SheetCalcProperties.new(**calc) end bg = background_image || unmapped_data[:background_image] || unmapped_data.dig(:facade, :background_image) @background_image = if bg.nil? nil elsif bg.is_a?(Elements::BackgroundImage) bg else Elements::BackgroundImage.new(**bg) end pt = pivot_tables || unmapped_data[:pivot_tables] || unmapped_data.dig(:facade, :pivot_tables) || [] @pivot_tables = pt.map { |r| r.is_a?(Elements::PivotTable) ? r : Elements::PivotTable.new(**r) }.freeze sp = split_pane || unmapped_data[:split_pane] || unmapped_data.dig(:facade, :split_pane) @split_pane = if sp.nil? nil elsif sp.is_a?(Elements::SplitPane) sp else Elements::SplitPane.new(**sp) end fc = filter_columns || unmapped_data[:filter_columns] || unmapped_data.dig(:facade, :filter_columns) || {} @filter_columns = fc.to_h do |k, v| column_id = k.to_i val = case v when Elements::FilterColumn v when Hash Elements::FilterColumn.from_spec(column_id, v) when Array Elements::FilterColumn.new(column_id: column_id, filters: v) else Elements::FilterColumn.new(column_id: column_id, filters: [v.to_s]) end [column_id, val] end.freeze st = sort_state || unmapped_data[:sort_state] || unmapped_data.dig(:facade, :sort_state) @sort_state = if st.nil? nil elsif st.is_a?(Elements::SortState) st else Elements::SortState.new(**st) end hfi = header_footer_images || unmapped_data[:header_footer_images] || unmapped_data.dig(:facade, :header_footer_images) || [] @header_footer_images = hfi.map { |r| r.is_a?(Elements::HeaderFooterImage) ? r : Elements::HeaderFooterImage.new(**r) }.freeze end
@param name [String] The worksheet name (max 31 characters). @param rows [Array<Elements::Row>] Rows in the sheet. @param columns [Array<Elements::Column>] Column definitions. @param charts [Array<Hash>] Charts in the sheet. @param conditional_formats [Array<Hash, Elements::ConditionalFormat>] Conditional formatting rules. @param data_validations [Array<Hash>] Data validation rules. @param unmapped_data [Hash] Additional metadata for round-tripping. @param errors [Array<String>, nil] Validation errors. @param state [Symbol] Sheet visibility state (:visible, :hidden, or :very_hidden). @param hyperlinks [Hash{String => Hash}] Hyperlink mappings by cell reference. @param comments [Array<Hash>] Comments and notes in the sheet. @param images [Array<Elements::Image>] Embedded images in the sheet. @param styles [Hash, nil] Optional parsed styles hash. @param dimension [String, nil] Sheet dimension reference string (e.g. “A1:Z50000”). @param trim_empty_rows [Boolean] Whether to omit trailing empty rows during iteration. @param pad_empty_rows [Boolean] Whether to yield empty rows for skipped row numbers. @param pad_empty_cells [Boolean] Whether to pad missing cells within rows. @param print_area [String, nil] Sheet print area range (e.g. “A1:H50”). @param print_titles [Hash, nil] Repeating print title rows/cols (e.g. { rows: “1:2”, cols: “A:B” }). @param sparkline_groups [Array<Hash>] Sparkline groups in the sheet. @param merged_cells [Array<String>] Merged cell ranges (e.g. [“A1:B2”]). @param auto_filter [String, nil] AutoFilter range (e.g. “A1:E10”). @param tables [Array<Hash>] Table definitions in the sheet. @param shapes [Array<Hash>] Shape definitions in the sheet. @param freeze_pane [Hash, nil] Freeze pane settings (e.g. { row: 1, col: 0 }). @param page_setup [Hash] Page setup properties (e.g. orientation, paper size). @param page_margins [Hash, nil] Page margins settings. @param header_footer [Hash] Header and footer configurations. @param print_options [Hash] Print options (e.g. gridlines, headings). @param sheet_protection [Hash, nil] Sheet protection settings. @param sheet_view [Hash] Sheet view configurations (e.g. zoom, gridlines). @param sheet_properties [Hash] Sheet properties (e.g. tabColor, outlinePr). @param sheet_format [Hash] Sheet format properties (e.g. defaultRowHeight, defaultColWidth). @param row_breaks [Array<Integer | Hash>] Explicit page row breaks. @param column_breaks [Array<Integer | Hash>] Explicit page column breaks. @param protected_ranges [Array<Hash>] Protected range definitions. @param cell_watches [Array<String>] Watched cell references. @param ignored_errors [Array<Hash>] Ignored error rules. @param phonetic_properties [Hash, nil] Phonetic text properties. @param selection [Hash, Array<Hash>, nil] Active cell and selection settings. @param sheet_calc_properties [Hash, nil] Worksheet calculation properties. @param background_image [Hash, nil] Worksheet background image specification. @param threaded_comments [Array<Hash>] Threaded comments in the sheet.
(untyped name) → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 1052 def self.valid_name?(name) case name when String !name.empty? && Ooxml::Utils.utf16_length(name) <= 31 && !name.match?(FORBIDDEN_NAME_CHARS) && !name.start_with?("'") && !name.end_with?("'") && !name.casecmp?("History") else false end end
Returns whether the worksheet name is valid according to OOXML specifications (1..31 chars, no forbidden chars).
@param name [Object] @return [Boolean]
(untyped name, untyped rows) → Array[String]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 1068 def self.validate(name, rows) errs = [] if name.nil? || !name.is_a?(String) || name.empty? errs << "worksheet name must be a non-empty String (got #{name.inspect})" else errs << "worksheet name cannot exceed 31 characters, counted in UTF-16 code units (got #{Ooxml::Utils.utf16_length(name)})" if Ooxml::Utils.utf16_length(name) > 31 errs << "worksheet name cannot contain \\, /, ?, *, [, ], or :" if name.match?(FORBIDDEN_NAME_CHARS) errs << "worksheet name cannot start or end with an apostrophe" if name.start_with?("'") || name.end_with?("'") errs << "worksheet name 'History' is reserved by Excel" if name.casecmp?("History") end errs << "rows must be an Array (got #{rows.class})" unless rows.is_a?(Array) if rows.is_a?(Array) indices = rows.map(&:index) if indices.uniq.size != indices.size dups = indices.select { |i| indices.count(i) > 1 }.uniq errs << "duplicate row index: #{dups.join(", ")} — row indices within a sheet must be unique" end end errs end
Validates worksheet name and rows against OOXML limits.
@param name [String] @param rows [Array<Elements::Row>] @return [Array<String>] List of errors. @api private
Public Instance Methods
(untyped other) → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 1006 def ==(other) return false unless other.is_a?(Worksheet) name == other.name && rows == other.rows && columns == other.columns && charts == other.charts && conditional_formats == other.conditional_formats && data_validations == other.data_validations && state == other.state && hyperlinks == other.hyperlinks && comments == other.comments && threaded_comments == other.threaded_comments && merged_cells == other.merged_cells && auto_filter == other.auto_filter && tables == other.tables && shapes == other.shapes && freeze_pane == other.freeze_pane && page_setup == other.page_setup && page_margins == other.page_margins && header_footer == other.header_footer && print_options == other.print_options && sheet_protection == other.sheet_protection && sheet_view == other.sheet_view && sheet_properties == other.sheet_properties && sheet_format == other.sheet_format && row_breaks == other.row_breaks && column_breaks == other.column_breaks && protected_ranges == other.protected_ranges && cell_watches == other.cell_watches && ignored_errors == other.ignored_errors && phonetic_properties == other.phonetic_properties && selection == other.selection && sheet_calc_properties == other.sheet_calc_properties && background_image == other.background_image end
Compare worksheets for equality.
() → String?
Source
# File lib/xlsxrb/elements/worksheet.rb, line 469 def code_name sheet_properties.code_name end
Returns the internal VBA or application code name of the worksheet, or nil if not specified.
@return [String, nil] @api public
(String | Symbol | Integer | nil ref_or_row, ?Integer | String | Symbol | nil col) → Elements::Comment?
Source
# File lib/xlsxrb/elements/worksheet.rb, line 736 def comment(ref_or_row, col = nil) ref = if col c_idx = Cell._column_index(col) "#{Cell._column_letter(c_idx)}#{ref_or_row.to_i + 1}" else ref_or_row.to_s.upcase end comments_by_ref[ref] end
Returns comment metadata for the given cell reference, or nil.
@param ref_or_row [String, Symbol, Integer] Cell reference (e.g. “A1”) or 0-based row index. @param col [Integer, String, Symbol, nil] Optional 0-based column index or column letter/Symbol. @return [Elements::Comment, nil] @api public
() → Hash[String, Elements::Comment]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 751 def comments_by_ref @comments_by_ref ||= @comments.each_with_object({}) do |c, acc| r = c.respond_to?(:ref) ? c.ref : (c[:ref] || c[:cell]) acc[r.to_s.upcase] = c if r end end
Returns comments indexed by cell reference.
@return [Hash<String, Elements::Comment>] @api public
() → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 434 def date1904? @date1904 end
Returns whether the worksheet uses the 1904 date system.
@return [Boolean] @api public
() → Array[untyped]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 125 def deconstruct [name, rows, columns] end
Support array pattern matching.
@return [Array<Object>] @api public
(Array[Symbol]?) → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 949 def deconstruct_keys(_keys) { name: name, rows: rows, columns: columns, charts: charts, conditional_formats: conditional_formats, data_validations: data_validations, unmapped_data: unmapped_data, errors: errors, state: state, hyperlinks: hyperlinks, comments: comments, threaded_comments: threaded_comments, styles: styles, date1904: date1904?, dimension: dimension, print_area: print_area, print_titles: print_titles, sparkline_groups: sparkline_groups, trim_empty_rows: @trim_empty_rows, pad_empty_rows: @pad_empty_rows, pad_empty_cells: @pad_empty_cells, merged_cells: merged_cells, auto_filter: auto_filter, tables: tables, shapes: shapes, freeze_pane: freeze_pane, split_pane: split_pane, pivot_tables: pivot_tables, filter_columns: filter_columns, sort_state: sort_state, images: images, header_footer_images: header_footer_images, page_setup: page_setup, page_margins: page_margins, header_footer: header_footer, print_options: print_options, sheet_protection: sheet_protection, sheet_view: sheet_view, sheet_properties: sheet_properties, sheet_format: sheet_format, row_breaks: row_breaks, column_breaks: column_breaks, protected_ranges: protected_ranges, cell_watches: cell_watches, ignored_errors: ignored_errors, phonetic_properties: phonetic_properties, selection: selection, sheet_calc_properties: sheet_calc_properties, code_name: code_name, background_image: background_image } end
Support pattern matching.
() → String?
Source
# File lib/xlsxrb/elements/worksheet.rb, line 444 def dimension @dimension ||= compute_dimension end
Returns the worksheet dimension reference string (e.g. “A1:Z50000”), or computes it dynamically from rows if not explicitly set.
@return [String, nil] @api public
(?trim_empty_rows: bool?, ?pad_empty_rows: bool?, ?pad_empty_cells: bool?) { (Elements::Row) → void } → void
(?trim_empty_rows: bool?, ?pad_empty_rows: bool?, ?pad_empty_cells: bool?) → Enumerator[Elements::Row, void]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 533 def each(trim_empty_rows: nil, pad_empty_rows: nil, pad_empty_cells: nil, &) return to_enum(:each, trim_empty_rows: trim_empty_rows, pad_empty_rows: pad_empty_rows, pad_empty_cells: pad_empty_cells) unless block_given? each_row(trim_empty_rows: trim_empty_rows, pad_empty_rows: pad_empty_rows, pad_empty_cells: pad_empty_cells, &) end
Iterate over rows in the worksheet.
@example
sheet.each do |row| puts row.to_a.inspect end
@overload each(trim_empty_rows: nil, pad_empty_rows: nil, pad_empty_cells: nil, &block)
@param trim_empty_rows [Boolean, nil] Whether to omit trailing empty rows. @param pad_empty_rows [Boolean, nil] Whether to yield empty rows for skipped row numbers. @param pad_empty_cells [Boolean, nil] Whether to pad missing cells within rows. @yield [row] @yieldparam row [Elements::Row] @return [void]
@overload each(trim_empty_rows: nil, pad_empty_rows: nil, pad_empty_cells: nil)
@param trim_empty_rows [Boolean, nil] Whether to omit trailing empty rows. @param pad_empty_rows [Boolean, nil] Whether to yield empty rows for skipped row numbers. @param pad_empty_cells [Boolean, nil] Whether to pad missing cells within rows. @return [Enumerator<Elements::Row, void>]
() { (Elements::Cell) → void } → void
() → Enumerator[Elements::Cell, void]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 654 def each_cell(&) return to_enum(:each_cell) unless block_given? cells.each(&) end
Iterate over all cells across rows.
@yield [cell] @yieldparam cell [Elements::Cell] @return [Enumerator, void] @api public
(?trim_empty_rows: bool?, ?pad_empty_rows: bool?, ?pad_empty_cells: bool?) { (Elements::Row) → void } → void
(?trim_empty_rows: bool?, ?pad_empty_rows: bool?, ?pad_empty_cells: bool?) → Enumerator[Elements::Row, void]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 563 def each_row(trim_empty_rows: nil, pad_empty_rows: nil, pad_empty_cells: nil, &block) return to_enum(:each_row, trim_empty_rows: trim_empty_rows, pad_empty_rows: pad_empty_rows, pad_empty_cells: pad_empty_cells) unless block trim = if trim_empty_rows.nil? @trim_empty_rows else (trim_empty_rows ? true : false) end pad_rows = if pad_empty_rows.nil? @pad_empty_rows else (pad_empty_rows ? true : false) end pad_cells = if pad_empty_cells.nil? @pad_empty_cells else (pad_empty_cells ? true : false) end row_consumer = if trim pending_empty = [] lambda do |row| if row.empty? pending_empty << row else pending_empty.each(&block) pending_empty.clear block.call(row) end end else block end if pad_rows current_expected_idx = 0 inner_consumer = row_consumer row_consumer = lambda do |row| while current_expected_idx < row.index empty_row = Row.new(index: current_expected_idx) inner_consumer.call(pad_cells ? empty_row.padded : empty_row) current_expected_idx += 1 end inner_consumer.call(row) current_expected_idx = row.index + 1 end end rows.each do |row| row_to_yield = pad_cells ? row.padded : row row_consumer.call(row_to_yield) end end
Iterate over rows in the worksheet.
@example
sheet.each_row do |row| puts "Row #{row.index}: #{row.to_a.inspect}" end
@overload each_row(trim_empty_rows: nil, pad_empty_rows: nil, pad_empty_cells: nil, &block)
@param trim_empty_rows [Boolean, nil] Whether to omit trailing empty rows. @param pad_empty_rows [Boolean, nil] Whether to yield empty rows for skipped row numbers. @param pad_empty_cells [Boolean, nil] Whether to pad missing cells within rows. @yield [row] @yieldparam row [Elements::Row] @return [void]
@overload each_row(trim_empty_rows: nil, pad_empty_rows: nil, pad_empty_cells: nil)
@param trim_empty_rows [Boolean, nil] Whether to omit trailing empty rows. @param pad_empty_rows [Boolean, nil] Whether to yield empty rows for skipped row numbers. @param pad_empty_cells [Boolean, nil] Whether to pad missing cells within rows. @return [Enumerator<Elements::Row, void>]
(?type_cast: bool, ?trim_empty_rows: bool?, ?pad_empty_rows: bool?, ?pad_empty_cells: bool?) { (Array[untyped]) → void } → void
(?type_cast: bool, ?trim_empty_rows: bool?, ?pad_empty_rows: bool?, ?pad_empty_cells: bool?) → Enumerator[Array[untyped], void]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 638 def each_row_values(type_cast: false, trim_empty_rows: nil, pad_empty_rows: nil, pad_empty_cells: nil, &block) return to_enum(:each_row_values, type_cast: type_cast, trim_empty_rows: trim_empty_rows, pad_empty_rows: pad_empty_rows, pad_empty_cells: pad_empty_cells) unless block each_row(trim_empty_rows: trim_empty_rows, pad_empty_rows: pad_empty_rows, pad_empty_cells: pad_empty_cells) do |row| block.call(row.values(type_cast: type_cast)) end end
Iterates over row values as Arrays.
@overload each_row_values(type_cast: false, trim_empty_rows: nil, pad_empty_rows: nil, pad_empty_cells: nil, &block)
@param type_cast [Boolean] Whether to coerce date/time serial numbers into Date/Time instances. @param trim_empty_rows [Boolean, nil] Whether to omit trailing empty rows. @param pad_empty_rows [Boolean, nil] Whether to yield empty rows for skipped row numbers. @param pad_empty_cells [Boolean, nil] Whether to pad missing cells within rows. @yield [values] @yieldparam values [Array<Object>] Row values array. @return [void]
@overload each_row_values(type_cast: false, trim_empty_rows: nil, pad_empty_rows: nil, pad_empty_cells: nil)
@param type_cast [Boolean] Whether to coerce date/time serial numbers into Date/Time instances. @param trim_empty_rows [Boolean, nil] Whether to omit trailing empty rows. @param pad_empty_rows [Boolean, nil] Whether to yield empty rows for skipped row numbers. @param pad_empty_cells [Boolean, nil] Whether to pad missing cells within rows. @return [Enumerator<Array<Object>, void>]
() → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 478 def fit_to_page? sheet_properties.fit_to_page? end
Returns whether fit-to-page is enabled for page setup on this worksheet.
@return [Boolean] @api public
(String | Symbol | Integer ref_or_row, ?Integer | String | Symbol | nil col) → String?
Source
# File lib/xlsxrb/elements/worksheet.rb, line 817 def formatted_value(ref_or_row, col = nil) self[ref_or_row, col]&.formatted_value end
Access a cell by reference or 0-based coordinates.
@param ref_or_row [String, Symbol, Integer] Cell reference (e.g. “A1”) or 0-based row index. @param col [Integer, String, Symbol, nil] Optional 0-based column index or column letter/Symbol. @return [Elements::Cell, nil] @api public Returns the formatted string representation of a cell’s value.
@param ref_or_row [String, Symbol, Integer] Cell reference (e.g. “A1”) or 0-based row index. @param col [Integer, String, Symbol, nil] Optional 0-based column index or column letter/Symbol. @return [String, nil] @api public
() → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 487 def full_calc_on_load? @sheet_calc_pr&.to_h&.[](:full_calc_on_load) == true || sheet_properties.to_h[:full_calc_on_load] == true end
Returns whether full calculation on load is enabled on this worksheet.
@return [Boolean] @api public
() → Integer
Source
# File lib/xlsxrb/elements/worksheet.rb, line 1028 def hash [ self.class, name, rows, columns, charts, conditional_formats, data_validations, state, hyperlinks, comments, threaded_comments, merged_cells, auto_filter, tables, shapes, freeze_pane, page_setup, page_margins, header_footer, print_options, sheet_protection, sheet_view, sheet_properties, sheet_format, row_breaks, column_breaks, protected_ranges, cell_watches, ignored_errors, phonetic_properties, selection, sheet_calc_properties, background_image ].hash end
(String | Symbol | Integer | nil ref_or_row, ?Integer | String | Symbol | nil col) → Elements::Hyperlink?
Source
# File lib/xlsxrb/elements/worksheet.rb, line 719 def hyperlink(ref_or_row, col = nil) ref = if col c_idx = Cell._column_index(col) "#{Cell._column_letter(c_idx)}#{ref_or_row.to_i + 1}" else ref_or_row.to_s.upcase end @hyperlinks[ref] end
Returns hyperlink metadata for the given cell reference, or nil.
@param ref_or_row [String, Symbol, Integer] Cell reference (e.g. “A1”) or 0-based row index. @param col [Integer, String, Symbol, nil] Optional 0-based column index or column letter/Symbol. @return [Elements::Hyperlink, nil] @api public
(String | Symbol | Integer ref_or_row, ?Integer | String | Symbol | nil col) → Array[Elements::Image]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 667 def images_at(ref_or_row, col = nil) ref = if col c_idx = col.is_a?(Integer) ? col : Cell._column_index(col) "#{Cell._column_letter(c_idx)}#{ref_or_row.to_i + 1}" else ref_or_row.to_s.upcase end images.select { |img| img.ref == ref } end
Returns all images anchored at a specific cell reference or 0-based coordinate.
@param ref_or_row [String, Symbol, Integer] Cell reference (e.g. “A1”) or 0-based row index. @param col [Integer, String, Symbol, nil] Optional 0-based column index or column letter/Symbol. @return [Array<Elements::Image>] @api public
() → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 496 def outline_properties props = sheet_properties props = props.to_h if props.is_a?(Elements::MetadataRecord) return props[:outline_properties] if props[:outline_properties] res = {} res[:summary_below] = props[:summary_below] unless props[:summary_below].nil? res[:summary_right] = props[:summary_right] unless props[:summary_right].nil? res[:show_outline_symbols] = props[:show_outline_symbols] unless props[:show_outline_symbols].nil? res[:apply_styles] = props[:apply_styles] unless props[:apply_styles].nil? res end
Returns the outline and grouping properties of the worksheet, or an empty hash if none configured.
@return [Hash] @api public
() → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 425 def pad_empty_cells? @pad_empty_cells end
Returns whether missing cells within rows are padded during iteration.
@return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 416 def pad_empty_rows? @pad_empty_rows end
Returns whether empty rows are yielded for skipped row numbers during iteration.
@return [Boolean] @api public
() → Elements::SheetCalcProperties?
Source
# File lib/xlsxrb/elements/worksheet.rb, line 134 def sheet_calc_properties @sheet_calc_pr end
Returns worksheet calculation properties (<sheetCalcPr>), or nil if none configured.
@return [Elements::SheetCalcProperties, nil] @api public
() → (String | Hash[Symbol, untyped])?
Source
# File lib/xlsxrb/elements/worksheet.rb, line 453 def tab_color props = sheet_properties props = props.to_h if props.is_a?(Elements::MetadataRecord) return props[:tab_color] if props[:tab_color] return { theme: props[:tab_color_theme], tint: props[:tab_color_tint] } if props[:tab_color_theme] return { indexed: props[:tab_color_indexed] } if props[:tab_color_indexed] return { auto: true } if props[:tab_color_auto] nil end
Returns the worksheet tab color specification, or nil if not set.
@return [String, Hash, nil] RGB hex string (e.g. “FFFF0000”) or color hash (e.g. { theme: 3, tint: -0.5 }). @api public
(String | Symbol | Integer ref_or_row, ?Integer | String | Symbol | nil col) → Elements::ThreadedComment?
Source
# File lib/xlsxrb/elements/worksheet.rb, line 765 def threaded_comment(ref_or_row, col = nil) ref = if col c_idx = col.is_a?(Integer) ? col : Cell._column_index(col) "#{Cell._column_letter(c_idx)}#{ref_or_row.to_i + 1}" else ref_or_row.to_s.upcase end threaded_comments_by_ref[ref]&.first end
Returns the root threaded comment for the given cell reference, or nil.
@param ref_or_row [String, Symbol, Integer] Cell reference (e.g. “A1”) or 0-based row index. @param col [Integer, String, Symbol, nil] Optional 0-based column index or column letter/Symbol. @return [Elements::ThreadedComment, nil] @api public
(String | Symbol | Integer ref_or_row, ?Integer | String | Symbol | nil col) → Array[Elements::ThreadedComment]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 782 def threaded_comments_at(ref_or_row, col = nil) ref = if col c_idx = col.is_a?(Integer) ? col : Cell._column_index(col) "#{Cell._column_letter(c_idx)}#{ref_or_row.to_i + 1}" else ref_or_row.to_s.upcase end threaded_comments_by_ref[ref] || [] end
Returns threaded comments for the given cell reference, or empty array.
@param ref_or_row [String, Symbol, Integer] Cell reference (e.g. “A1”) or 0-based row index. @param col [Integer, String, Symbol, nil] Optional 0-based column index or column letter/Symbol. @return [Array<Elements::ThreadedComment>] @api public
() → Hash[String, Array[Elements::ThreadedComment]]
Source
# File lib/xlsxrb/elements/worksheet.rb, line 797 def threaded_comments_by_ref @threaded_comments_by_ref ||= @threaded_comments.each_with_object({}) do |c, acc| r = c.respond_to?(:ref) ? c.ref : (c[:ref] || c[:cell]) (acc[r.to_s.upcase] ||= []) << c if r end end
Returns threaded comments indexed by cell reference.
@return [Hash<String, Array<Elements::ThreadedComment>>] @api public
() → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 407 def trim_empty_rows? @trim_empty_rows end
Returns whether trailing empty rows are omitted during iteration.
@return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 681 def valid? errors.empty? end
Returns whether the worksheet is valid according to OOXML specifications.
@return [Boolean]
() → bool
Source
# File lib/xlsxrb/elements/worksheet.rb, line 699 def visible? @state == :visible end
Returns whether the worksheet is visible.
@return [Boolean] @api public
(**untyped) → Elements::Worksheet
Source
# File lib/xlsxrb/elements/worksheet.rb, line 827 def with(**changes) new_name = changes.key?(:name) ? changes[:name] : name new_rows = changes.key?(:rows) ? changes[:rows] : rows new_cols = changes.key?(:columns) ? changes[:columns] : columns new_charts = changes.key?(:charts) ? changes[:charts] : charts new_cf = changes.key?(:conditional_formats) ? changes[:conditional_formats] : conditional_formats new_dv = changes.key?(:data_validations) ? changes[:data_validations] : data_validations new_unmapped = changes.key?(:unmapped_data) ? changes[:unmapped_data] : unmapped_data if new_unmapped.is_a?(Hash) && new_unmapped[:facade].is_a?(Hash) stale_keys = changes.keys.select { |k| new_unmapped[:facade].key?(k) } stale_keys << :col_breaks if changes.key?(:column_breaks) && new_unmapped[:facade].key?(:col_breaks) stale_keys << :sheet_calc_pr if changes.key?(:sheet_calc_properties) && new_unmapped[:facade].key?(:sheet_calc_pr) stale_keys << :merge_cells if changes.key?(:merged_cells) && new_unmapped[:facade].key?(:merge_cells) unless stale_keys.empty? new_unmapped = new_unmapped.dup new_facade = new_unmapped[:facade].dup stale_keys.each { |k| new_facade.delete(k) } new_unmapped[:facade] = new_facade end end new_errors = changes.key?(:errors) ? changes[:errors] : errors new_state = changes.key?(:state) ? changes[:state] : state new_hyperlinks = changes.key?(:hyperlinks) ? changes[:hyperlinks] : hyperlinks new_comments = changes.key?(:comments) ? changes[:comments] : comments new_styles = changes.key?(:styles) ? changes[:styles] : styles new_date1904 = changes.key?(:date1904) ? changes[:date1904] : date1904? new_dim = if changes.key?(:dimension) changes[:dimension] else (changes.key?(:rows) ? nil : @dimension) end new_print_area = changes.key?(:print_area) ? changes[:print_area] : print_area new_print_titles = changes.key?(:print_titles) ? changes[:print_titles] : print_titles new_sparklines = changes.key?(:sparkline_groups) ? changes[:sparkline_groups] : sparkline_groups new_trim = changes.key?(:trim_empty_rows) ? changes[:trim_empty_rows] : @trim_empty_rows new_pad_rows = changes.key?(:pad_empty_rows) ? changes[:pad_empty_rows] : @pad_empty_rows new_pad_cells = changes.key?(:pad_empty_cells) ? changes[:pad_empty_cells] : @pad_empty_cells new_images = changes.key?(:images) ? changes[:images] : images new_merged = changes.key?(:merged_cells) ? changes[:merged_cells] : merged_cells new_auto_filter = changes.key?(:auto_filter) ? changes[:auto_filter] : auto_filter new_tables = changes.key?(:tables) ? changes[:tables] : tables new_shapes = changes.key?(:shapes) ? changes[:shapes] : shapes new_freeze_pane = changes.key?(:freeze_pane) ? changes[:freeze_pane] : freeze_pane new_split_pane = changes.key?(:split_pane) ? changes[:split_pane] : split_pane new_pivot_tables = changes.key?(:pivot_tables) ? changes[:pivot_tables] : pivot_tables new_filter_columns = changes.key?(:filter_columns) ? changes[:filter_columns] : filter_columns new_sort_state = changes.key?(:sort_state) ? changes[:sort_state] : sort_state new_header_footer_images = changes.key?(:header_footer_images) ? changes[:header_footer_images] : header_footer_images new_page_setup = changes.key?(:page_setup) ? changes[:page_setup] : page_setup new_page_margins = changes.key?(:page_margins) ? changes[:page_margins] : page_margins new_header_footer = changes.key?(:header_footer) ? changes[:header_footer] : header_footer new_print_options = changes.key?(:print_options) ? changes[:print_options] : print_options new_sheet_protection = changes.key?(:sheet_protection) ? changes[:sheet_protection] : sheet_protection new_sheet_view = changes.key?(:sheet_view) ? changes[:sheet_view] : sheet_view new_sheet_properties = changes.key?(:sheet_properties) ? changes[:sheet_properties] : sheet_properties new_sheet_format = changes.key?(:sheet_format) ? changes[:sheet_format] : sheet_format new_row_breaks = changes.key?(:row_breaks) ? changes[:row_breaks] : row_breaks new_col_breaks = changes.key?(:column_breaks) ? changes[:column_breaks] : @column_breaks new_protected_ranges = changes.key?(:protected_ranges) ? changes[:protected_ranges] : protected_ranges new_cell_watches = changes.key?(:cell_watches) ? changes[:cell_watches] : cell_watches new_ignored_errors = changes.key?(:ignored_errors) ? changes[:ignored_errors] : ignored_errors new_phonetic_properties = changes.key?(:phonetic_properties) ? changes[:phonetic_properties] : phonetic_properties new_selection = changes.key?(:selection) ? changes[:selection] : selection new_sheet_calc_properties = changes.key?(:sheet_calc_properties) ? changes[:sheet_calc_properties] : @sheet_calc_pr new_background_image = changes.key?(:background_image) ? changes[:background_image] : background_image new_threaded_comments = changes.key?(:threaded_comments) ? changes[:threaded_comments] : threaded_comments self.class.new( name: new_name, rows: new_rows, columns: new_cols, charts: new_charts, conditional_formats: new_cf, data_validations: new_dv, unmapped_data: new_unmapped, errors: new_errors, state: new_state, hyperlinks: new_hyperlinks, comments: new_comments, images: new_images, styles: new_styles, date1904: new_date1904, dimension: new_dim, print_area: new_print_area, print_titles: new_print_titles, sparkline_groups: new_sparklines, trim_empty_rows: new_trim, pad_empty_rows: new_pad_rows, pad_empty_cells: new_pad_cells, merged_cells: new_merged, auto_filter: new_auto_filter, tables: new_tables, shapes: new_shapes, freeze_pane: new_freeze_pane, split_pane: new_split_pane, pivot_tables: new_pivot_tables, filter_columns: new_filter_columns, sort_state: new_sort_state, header_footer_images: new_header_footer_images, page_setup: new_page_setup, page_margins: new_page_margins, header_footer: new_header_footer, print_options: new_print_options, sheet_protection: new_sheet_protection, sheet_view: new_sheet_view, sheet_properties: new_sheet_properties, sheet_format: new_sheet_format, row_breaks: new_row_breaks, column_breaks: new_col_breaks, protected_ranges: new_protected_ranges, cell_watches: new_cell_watches, ignored_errors: new_ignored_errors, phonetic_properties: new_phonetic_properties, selection: new_selection, sheet_calc_properties: new_sheet_calc_properties, background_image: new_background_image, threaded_comments: new_threaded_comments ) end
Returns a new Worksheet with attributes replaced (Data-like behavior).
@param changes [Hash] @return [Worksheet] @api public