class Xlsxrb::WorkbookBuilder
DSL context for building an in-memory {Elements::Workbook} in {Xlsxrb.build}.
@example Build an in-memory workbook with multiple sheets
workbook = Xlsxrb.build do |builder| builder.sheet("Sales") do |sheet| sheet.row(["Product", "Revenue"]) sheet.row(["Widget", 1000]) end builder.core_properties(creator: "Reporting System") end
Attributes
(String | Symbol | Integer | nil)
Gets or sets the active worksheet tab in the workbook window.
@return [String, Symbol, Integer, nil] @api public
(String | Symbol | Integer | nil)
Gets or sets the first sheet tab visible in the sheet tab scroll area.
@return [String, Symbol, Integer, nil] @api public
(bool? visible) → bool?
Sets whether sheet tabs are visible in the workbook window.
@param visible [Boolean, nil] @return [Boolean, nil] @api public
Hash[String, StyleBuilder]
@return [Hash{String => StyleBuilder}]
Integer?
Gets or sets the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
@return [Integer, nil] @api public
Public Class Methods
(?strict_excel_mode: bool, ?default_font: untyped?) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 27 def initialize(strict_excel_mode: true, default_font: nil) @strict_excel_mode = strict_excel_mode @default_font = default_font @sheets = [] # Each sheet without its rows, for its name and state @sheet_snapshots = [] # Each sheet as its block left it, rows still pending @sheet_builders = [] # Keep track of sheet builders for style processing @style_log = StyleLog.new # When each style is defined, for the rows that use it @defined_names = [] @styles = {} @core_properties = {} @app_properties = {} @custom_properties = [] @workbook_protection = nil @workbook_properties = { update_links: "never" } @active_sheet = nil @first_sheet = nil @show_sheet_tabs = nil @tab_ratio = nil end
@param strict_excel_mode [Boolean] Whether to enforce Microsoft Excel specifications. @param default_font [Hash, nil] Optional default font options (e.g. ‘{ name: “Arial”, size: 12 }`).
Public Instance Methods
(?String | Symbol | Integer | nil name_or_index) ?{ (WorksheetBuilder) → void } → DeferredWorksheet
Source
# File lib/xlsxrb/workbook_builder.rb, line 315 def [](name_or_index = nil, &) if !name_or_index.nil? Deprecation.warn(INDEX_CREATES_SHEET_MESSAGE) elsif !@sheet_builders.empty? Deprecation.warn(INDEX_ADDS_SHEET_MESSAGE) end sheet(name_or_index&.to_s, &) end
Adds a worksheet, as {#sheet} does.
@deprecated Use {#sheet} to add a sheet. In version 0.3.0, this method will return the
{WorksheetBuilder} of an existing sheet, or of the current sheet without an argument,
and raise for a sheet that does not exist. Calling it with an argument, or without
one after a sheet was added, warns.
@param name_or_index [String, Symbol, Integer, nil] @yield [sheet_builder] @return [DeferredWorksheet] @api public
() → Elements::AppProperties
(**untyped opts) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 558 def app_properties(**opts) return Elements::AppProperties.new(**@app_properties) if opts.empty? opts.each do |k, v| norm_name = k.to_sym == :application ? :app_name : k.to_sym @app_properties[norm_name] = v end nil end
Configures or queries extended application metadata properties.
When called with no arguments, returns the {Elements::AppProperties} value object. When called with keyword options, configures properties and returns nil.
@param opts [Hash] Application properties map (:company, :manager, :app_version, etc.). @return [Elements::AppProperties, void] @api public
(Hash[Symbol, untyped] | Elements::AppProperties props) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 574 def app_properties=(props) h = props.respond_to?(:to_h) ? props.to_h : (props || {}) @app_properties = {} app_properties(**h) nil end
Replaces application document properties.
@param props [Hash, Elements::AppProperties] @return [void] @api public
(String expression, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), **untyped attrs) → Elements::Formula
Source
# File lib/xlsxrb/workbook_builder.rb, line 116 def array_formula(...) Xlsxrb.array_formula(...) end
Creates an array or dynamic array {Elements::Formula} object.
@param expression [String] The formula expression without leading ‘=’. @param ref [String, nil] Target cell or range reference (keyword). @param cached_value [Object, nil] Optional precomputed value. @return [Elements::Formula] @api public
() → Elements::Workbook
Source
# File lib/xlsxrb/workbook_builder.rb, line 630 def build if @strict_excel_mode raise ArgumentError, "Workbook must contain at least one sheet (Excel limitation)" if @sheets.empty? raise ArgumentError, "Workbook must contain at least one visible sheet (Excel limitation)" if @sheets.none? { |s| s.state == :visible } end # Process styles from all sheets and collect style definitions processed_sheets, styles_definition = process_styles(@sheet_snapshots) # Auto-register print_area and print_titles defined names from sheet builders if not explicitly defined @sheet_builders.each do |sb| set_print_area(sb.print_area, sheet: sb.name) if sb.print_area && @defined_names.none? { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sb.name } print_titles(rows: sb.print_titles.rows, columns: sb.print_titles.columns, sheet: sb.name) if sb.print_titles && @defined_names.none? { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sb.name } sb.defined_names&.each do |dn| @defined_names << dn end end active_tab_idx = if @active_sheet.is_a?(Integer) @active_sheet elsif @active_sheet idx = processed_sheets.find_index { |s| s.name == @active_sheet.to_s } raise ArgumentError, "Active sheet '#{@active_sheet}' not found" if idx.nil? && @strict_excel_mode idx || @active_sheet end if active_tab_idx.nil? first_vis_idx = processed_sheets.find_index { |s| s.state == :visible } active_tab_idx = first_vis_idx if first_vis_idx&.positive? end if active_tab_idx.is_a?(Integer) && @strict_excel_mode target = processed_sheets[active_tab_idx] raise ArgumentError, "Active sheet index #{active_tab_idx} out of bounds" if target.nil? raise ArgumentError, "Cannot set hidden sheet '#{target.name}' as active sheet" if target.state != :visible end first_sheet_idx = if @first_sheet.is_a?(Integer) @first_sheet elsif @first_sheet idx = processed_sheets.find_index { |s| s.name == @first_sheet.to_s } raise ArgumentError, "First sheet '#{@first_sheet}' not found" if idx.nil? && @strict_excel_mode idx || @first_sheet end @workbook_properties[:active_sheet] = active_tab_idx unless active_tab_idx.nil? @workbook_properties[:active_sheet_index] = active_tab_idx unless active_tab_idx.nil? @workbook_properties[:first_sheet] = first_sheet_idx unless first_sheet_idx.nil? @workbook_properties[:show_sheet_tabs] = @show_sheet_tabs unless @show_sheet_tabs.nil? @workbook_properties[:tab_ratio] = @tab_ratio unless @tab_ratio.nil? # Store workbook-level metadata in unmapped_data wb_meta = {} dns = resolve_defined_names(@defined_names, processed_sheets) wb_meta[:defined_names] = dns unless dns.empty? wb_meta[:core_properties] = @core_properties unless @core_properties.empty? wb_meta[:app_properties] = @app_properties unless @app_properties.empty? wb_meta[:custom_properties] = @custom_properties unless @custom_properties.empty? wb_meta[:workbook_protection] = @workbook_protection if @workbook_protection wb_meta[:workbook_properties] = @workbook_properties unless @workbook_properties.empty? Elements::Workbook.new( sheets: processed_sheets, styles: styles_definition, unmapped_data: wb_meta.empty? ? {} : { facade: wb_meta }, defined_names: dns ) end
Builds and returns the compiled {Elements::Workbook}.
@return [Elements::Workbook] @raise [ArgumentError] If workbook contains zero sheets in strict mode. @api public
(String | Symbol | Elements::CellError code) → Elements::CellError
Source
# File lib/xlsxrb/workbook_builder.rb, line 149 def cell_error(...) Xlsxrb.cell_error(...) end
Creates or returns an {Elements::CellError} object.
@param code [String, Symbol, Elements::CellError] Error code or symbol. @return [Elements::CellError] @api public
() → String?
Source
# File lib/xlsxrb/workbook_builder.rb, line 196 def code_name @workbook_properties[:code_name] end
Gets the internal code name of the workbook.
@return [String, nil] @api public
(String | Symbol | nil name) → String?
Source
# File lib/xlsxrb/workbook_builder.rb, line 206 def code_name=(name) @workbook_properties[:code_name] = name&.to_s end
Sets the internal code name of the workbook.
@param name [String, Symbol, nil] The code name. @return [String, nil] @api public
() → Elements::CoreProperties
(**untyped opts) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 529 def core_properties(**opts) return Elements::CoreProperties.new(**@core_properties) if opts.empty? @core_properties.merge!(opts) nil end
Configures or queries Dublin Core document metadata properties.
When called with no arguments, returns the {Elements::CoreProperties} value object. When called with keyword options, configures properties and returns nil.
@param opts [Hash] Core properties map (:creator, :title, :subject, :description, etc.). @return [Elements::CoreProperties, void] @api public
(Hash[Symbol, untyped] | Elements::CoreProperties props) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 542 def core_properties=(props) h = props.respond_to?(:to_h) ? props.to_h : (props || {}) @core_properties = h.transform_keys(&:to_sym) nil end
Replaces core document properties.
@param props [Hash, Elements::CoreProperties] @return [void] @api public
() → Hash[String, Elements::CustomProperty]
Source
# File lib/xlsxrb/workbook_builder.rb, line 612 def custom_properties @custom_properties.to_h do |cp| prop = if cp.is_a?(Elements::CustomProperty) cp else val = cp.key?(:value) ? cp[:value] : cp["value"] Elements::CustomProperty.new(name: (cp[:name] || cp["name"]).to_s, value: val, type: cp[:type] || cp["type"] || :string) end [prop.name.to_s, prop] end.freeze end
Returns custom document properties.
@return [Hash<String, Elements::CustomProperty>] @api public
(String name, String | Integer | Float | bool | Time value, ?type: ::Symbol) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 603 def custom_property(name, value, type: :string) @custom_properties << { name: name, value: value, type: type } end
Adds a custom document property.
@param name [String] Property name. @param value [String, Integer, Float, Boolean, Time] Property value. @param type [Symbol] Value type (:string, :number, :bool, :date). @return [void] @api public
(?String | Hash[Symbol, untyped]? name, ?Float | Integer | nil size, **untyped opts) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 55 def default_font(name = nil, size = nil, **opts) existing = @default_font.is_a?(Hash) ? @default_font.dup : {} if name.is_a?(Hash) opts = name.merge(opts) name = opts[:name] end existing[:name] = name if name existing[:size] = size if size @default_font = existing.merge(opts) end
Sets or configures the workbook-wide default font.
@param name [String, Hash, nil] Font name or font options Hash. @param size [Float, Integer, nil] Font size in points. @param opts [Hash] Additional font options (e.g. bold:, italic:, family:, charset:, color:). @return [void] @api public
(String name, ?untyped value, ?sheet: String?, ?scope: (String | Symbol)?, ?hidden: bool, ?columns: untyped, ?rows: untyped, ?range: untyped) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 346 def defined_name(name, value = nil, sheet: nil, scope: nil, hidden: false, columns: nil, rows: nil, range: nil) normalized_val = DslHelpers.normalize_defined_name_value(value, sheet: sheet, columns: columns, rows: rows, range: range) entry = { name: name, value: normalized_val, hidden: hidden } local_sheet = if scope scope == :workbook ? nil : scope.to_s end entry[:local_sheet_name] = local_sheet if local_sheet @defined_names << entry end
Adds a defined name (named range or formula) to the workbook. Bare cell references and coordinate keywords are automatically formatted with sheet quotes and absolute $ references.
@param name [String] Defined name identifier. @param value [Object, nil] Value or formula expression. @param sheet [String, nil] Reference sheet name for coordinates/formula. @param scope [String, Symbol, nil] Optional sheet name or :workbook for scope (defaults to workbook). @param hidden [Boolean] Whether the defined name is hidden from Excel’s UI. @param columns [Integer, String, nil] Column index or letter for coordinate-based reference. @param rows [Integer, Range, nil] Row or row range (0-based) for coordinate-based reference. @param range [String, nil] Cell range reference. @return [void] @api public
() → Array[Elements::DefinedName]
Source
# File lib/xlsxrb/workbook_builder.rb, line 361 def defined_names @defined_names.map { |dn| dn.is_a?(Elements::DefinedName) ? dn : Elements::DefinedName.new(**dn) } end
Returns workbook-level defined names.
@return [Array<Elements::DefinedName>] @api public
(String expression, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), ?type: (Symbol | String)?, ?ref: String?, ?shared_index: Integer?) → Elements::Formula
Source
# File lib/xlsxrb/workbook_builder.rb, line 104 def formula(...) Xlsxrb.formula(...) end
Creates an {Elements::Formula} object.
@param expression [String] The formula expression without leading ‘=’. @param cached_value [Object, nil] Optional precomputed value. @param type [Symbol, String, nil] Formula type (:shared, :array, etc.). @param ref [String, nil] Target cell or range reference. @param shared_index [Integer, nil] Shared formula group index (0-based). @return [Elements::Formula] @api public
(?sheet: String?) → String?
Source
# File lib/xlsxrb/workbook_builder.rb, line 371 def print_area(sheet: nil) sheet_name = sheet || @sheets.last&.name || "Sheet1" dn = @defined_names.find { |d| d[:name] == "_xlnm.Print_Area" && d[:local_sheet_name] == sheet_name } dn ? dn[:value].split("!", 2).last.tr("$", "") : nil end
Returns the print area range for the sheet, or nil if none.
@param sheet [String, nil] Target sheet name (defaults to latest or “Sheet1”). @return [String, nil] @api public
(String? range) → String?
Source
# File lib/xlsxrb/workbook_builder.rb, line 383 def print_area=(range) sheet_name = @sheets.last&.name || "Sheet1" set_print_area(range, sheet: sheet_name) end
Sets the print area for the current worksheet.
@param range [String, nil] Cell range (e.g. “A1:G50”). @return [String, nil] @api public
() → Elements::PrintTitles?
(?rows: (String | Integer | Range[Integer])?, ?columns: (String | Integer | Symbol | Range[untyped])?, ?sheet: String?) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 400 def print_titles(rows: nil, columns: nil, sheet: nil) sheet_name = sheet || @sheets.last&.name || "Sheet1" if rows.nil? && columns.nil? existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name } return nil unless existing c = Regexp.last_match(1).delete("$") if existing[:value] =~ /'(?:''|[^'])*'!\$([A-Za-z]+:\$[A-Za-z]+)/ r = Regexp.last_match(1).delete("$") if existing[:value] =~ /'(?:''|[^'])*'!\$([0-9]+:\$[0-9]+)/ return Elements::PrintTitles.new(rows: r, columns: c) end c = DslHelpers.normalize_print_titles_columns(columns) r = DslHelpers.normalize_print_titles_rows(rows) existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name } if existing && (r.nil? || c.nil?) c = Regexp.last_match(1).delete("$") if c.nil? && existing[:value] =~ /'(?:''|[^'])*'!\$([A-Za-z]+:\$[A-Za-z]+)/ r = Regexp.last_match(1).delete("$") if r.nil? && existing[:value] =~ /'(?:''|[^'])*'!\$([0-9]+:\$[0-9]+)/ end quoted_sheet = DslHelpers.quote_sheet_name(sheet_name) parts = [] parts << "#{quoted_sheet}!$#{c.sub(":", ":$")}" if c parts << "#{quoted_sheet}!$#{r.sub(":", ":$")}" if r value = parts.join(",") @defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name } defined_name("_xlnm.Print_Titles", value, sheet: sheet_name, scope: sheet_name) nil end
Sets or gets repeating print titles (rows and/or columns) for pagination.
When called with no row/column arguments, returns the {Elements::PrintTitles} value object or nil. When called with row/column arguments, sets repeating print titles and returns nil.
@param rows [String, nil] Repeating row range (e.g. “1:2”). @param columns [String, nil] Repeating column range (e.g. “A:B”). @param sheet [String, nil] Target sheet name. @return [Elements::PrintTitles, nil, void] @api public
(Elements::PrintTitles | Hash[Symbol, untyped] | nil opts) → (Elements::PrintTitles | Hash[Symbol, untyped] | nil)
Source
# File lib/xlsxrb/workbook_builder.rb, line 434 def print_titles=(opts) if opts.nil? sheet_name = @sheets.last&.name || "Sheet1" @defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name } else h = opts.respond_to?(:to_h) ? opts.to_h : opts print_titles(**h) end opts end
Sets repeating print titles.
@param opts [Elements::PrintTitles, Hash, nil] Options with :rows and/or :columns. @return [Elements::PrintTitles, Hash, nil] @api public
(?core: Hash[Symbol, String | Integer | Time]?, ?app: Hash[Symbol, String | Integer | Time]?, ?custom: Hash[String | Symbol, untyped]?) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 589 def properties(core: nil, app: nil, custom: nil) core_properties(**core) if core app_properties(**app) if app custom&.each { |k, v| custom_property(k.to_s, v) } end
Sets multiple core, app, and/or custom properties simultaneously.
@param core [Hash{Symbol => Object}, nil] Core properties map. @param app [Hash{Symbol => Object}, nil] App properties map. @param custom [Hash{String, Symbol => Object}, nil] Custom properties map. @return [void] @api public
(Integer | String first, ?(Integer | String)? last, ?sheet: String?) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 477 def repeat_columns(first, last = nil, sheet: nil) cols_str = if first.is_a?(String) first.include?(":") ? first : "#{first}:#{last || first}" else first_name = Elements::Cell._column_letter(first) last_name = if last.is_a?(Integer) Elements::Cell._column_letter(last) elsif last last.to_s else first_name end "#{first_name}:#{last_name}" end print_titles(columns: cols_str, sheet: sheet) end
Sets repeating title columns for pagination on the target or active sheet.
@example Repeat columns A through C
wb.repeat_columns("A", "C")
@param first [Integer, String] First column index (0-based) or column name/range. @param last [Integer, String, nil] Optional last column index/name. @param sheet [String, nil] Target sheet name. @return [void] @api public
(Integer | String first, ?(Integer | String)? last, ?sheet: String?) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 456 def repeat_rows(first, last = nil, sheet: nil) rows_str = if first.is_a?(String) first.include?(":") ? first : "#{first}:#{last || first}" else last ||= first "#{first + 1}:#{last + 1}" end print_titles(rows: rows_str, sheet: sheet) end
Sets repeating title rows for pagination on the target or active sheet.
@example Repeat first row
wb.repeat_rows(0)
@param first [Integer, String] First row index (0-based) or range string (e.g. “1:2”). @param last [Integer, String, nil] Optional last row index (0-based). @param sheet [String, nil] Target sheet name. @return [void] @api public
(*(Hash[Symbol, untyped] | Elements::RichTextRun | String | Array[untyped] | untyped) runs, ?text: String?, ?base_font: untyped, **untyped font_props) → Elements::RichText
Source
# File lib/xlsxrb/workbook_builder.rb, line 139 def rich_text(...) Xlsxrb.rich_text(...) end
Creates an {Elements::RichText} object.
@param runs [Array] Text runs or styling options. @return [Elements::RichText] @api public
(?String? name, ?state: Symbol | String | nil, ?tab_color: String | Hash[Symbol, untyped] | Symbol | nil, ?code_name: String | Symbol | nil, **untyped opts) ?{ (WorksheetBuilder) → void } → DeferredWorksheet
Source
# File lib/xlsxrb/workbook_builder.rb, line 266 def sheet(name = nil, state: nil, tab_color: nil, code_name: nil, **opts) sheet_count = [@sheets.size, @sheet_builders.size].max name ||= "Sheet#{sheet_count + 1}" raise ArgumentError, "Sheet name '#{name}' must be <= 31 characters, counted in UTF-16 code units as Excel does (Excel limitation)" if @strict_excel_mode && Ooxml::Utils.utf16_length(name) > 31 raise ArgumentError, "Sheet name '#{name}' contains invalid characters (Excel limitation)" if name.match?(Elements::Worksheet.forbidden_name_chars) raise ArgumentError, "Sheet name '#{name}' cannot start or end with an apostrophe (Excel limitation)" if @strict_excel_mode && (name.start_with?("'") || name.end_with?("'")) raise ArgumentError, "Sheet name '#{name}' is reserved by Excel (Excel limitation)" if @strict_excel_mode && name.casecmp?("History") existing_names = @sheets.map { |s| s.respond_to?(:name) ? s.name.downcase : s.to_s.downcase } + @sheet_builders.map { |sb| sb.name.downcase } raise ArgumentError, "Sheet name '#{name}' is already used. Excel requires unique sheet names." if @strict_excel_mode && existing_names.include?(name.downcase) active = opts.delete(:active) state = opts.delete(:state) if opts.key?(:state) hidden = opts.delete(:hidden) code_name = opts.delete(:code_name) if opts.key?(:code_name) tab_color = opts.delete(:tab_color) if opts.key?(:tab_color) state ||= :hidden if hidden state ||= :visible state = state.to_sym raise ArgumentError, "Invalid sheet state ':#{state}'. Allowed states are :visible, :hidden, :very_hidden" unless VALID_SHEET_STATES.include?(state) @active_sheet = name if active sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state, style_log: @style_log) sheet_builder.code_name = code_name if code_name sheet_builder.tab_color = tab_color if tab_color sheet_builder.sheet_properties(**opts) unless opts.empty? @sheet_builders << sheet_builder yield sheet_builder if block_given? @defined_names.concat(sheet_builder.defined_names) sheet_builder.defined_names.clear snapshot = sheet_builder.close @sheet_snapshots << snapshot @sheets << snapshot.frame DeferredWorksheet.new(snapshot) end
Adds a new worksheet to the workbook.
@param name [String, nil] Sheet name (max 31 chars, no forbidden chars ‘[ ] * ? / \ :`). @param state [Symbol, String, nil] Sheet visibility state (:visible, :hidden, :very_hidden). @param tab_color [String, Hash, Symbol, nil] Tab color hex string (e.g. “FF0000”) or hash options. @param code_name [String, Symbol, nil] Internal VBA code name of the sheet. @param opts [Hash] Additional sheet properties. @yield [sheet_builder] @yieldparam sheet_builder [Xlsxrb::WorksheetBuilder] @return [DeferredWorksheet] The sheet as {WorksheetBuilder#build} returns it, with
style names in its cells. It is built when first used.
@raise [ArgumentError] If sheet name violates Excel limits in strict mode. @api public
() → Array[String]
Source
# File lib/xlsxrb/workbook_builder.rb, line 248 def sheet_names (@sheets.map { |s| s.respond_to?(:name) ? s.name : s.to_s } + @sheet_builders.map(&:name)).uniq end
Returns the list of worksheet names added to this workbook so far.
@return [Array<String>] Worksheet names. @api public
() → bool
Source
# File lib/xlsxrb/workbook_builder.rb, line 180 def show_sheet_tabs? @show_sheet_tabs != false end
Returns whether sheet tabs are visible in the workbook window.
@return [Boolean] @api public
(String | Symbol name, **untyped opts) ?{ (StyleBuilder) → void } → StyleBuilder
Source
# File lib/xlsxrb/workbook_builder.rb, line 84 def style(name, **opts) style_name = name.to_s style_builder = StyleBuilder.new(style_name) style_builder.apply_options!(**opts) unless opts.empty? yield style_builder if block_given? @styles[style_name] = style_builder @style_log.record(style_name, style_builder) ReturnedStyleBuilder.new(style_builder) end
Defines or configures a named cell style at the workbook level.
@example
workbook.style(:header, bold: true, fill_color: "4F81BD", font_color: "FFFFFF")
@param name [String, Symbol] The name of the style. @param opts [Hash] Style options (e.g. bold: true, fill_color: “FF0000”). @yield [style_builder] @yieldparam style_builder [Xlsxrb::StyleBuilder] @return [StyleBuilder]
Using the returned value is deprecated and warns; in version 0.3.0, this method will return nil. Configure the style with options or in the block.
() → Elements::WorkbookProperties
(**untyped opts) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 224 def workbook_properties(**opts) return Elements::WorkbookProperties.new(**@workbook_properties) if opts.empty? @workbook_properties.merge!(opts) nil end
Configures or queries workbook-level properties.
When called with no arguments, returns the {Elements::WorkbookProperties} value object. When called with keyword options, configures properties and returns nil.
@note **SECURITY WARNING:** If you set :update_links to anything other than ‘“never”`,
you may expose end-users to malicious external reference vulnerabilities (e.g., CSV/DDE Injection) when they open the generated Excel file. Ensure you fully trust the exported data.
@param opts [Hash] Workbook properties map. @return [Elements::WorkbookProperties, void] @api public
(untyped props) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 237 def workbook_properties=(props) h = props.respond_to?(:to_h) ? props.to_h : (props || {}) @workbook_properties = h.transform_keys(&:to_sym) nil end
Sets workbook-level properties.
@param props [Hash, Elements::MetadataRecord] @return [void] @api public
(**String | Integer | bool | nil opts) → (Elements::WorkbookProtection | nil)
Source
# File lib/xlsxrb/workbook_builder.rb, line 500 def workbook_protection(**opts) if opts.empty? @workbook_protection ? Elements::WorkbookProtection.new(**@workbook_protection) : nil else @workbook_protection = DslHelpers.normalize_workbook_protection(opts) nil end end
Sets or returns workbook structure and window protection.
@param opts [Hash] Protection options (e.g. lock_structure: true, password: “secret”). @return [Elements::WorkbookProtection, nil, void] @api public
(untyped opts) → void
Source
# File lib/xlsxrb/workbook_builder.rb, line 515 def workbook_protection=(opts) @workbook_protection = opts.is_a?(Elements::MetadataRecord) ? opts.to_h : (opts && DslHelpers.normalize_workbook_protection(opts)) end
Sets workbook protection settings.
@param opts [Hash, Elements::MetadataRecord, nil] @return [void] @api public