class Xlsxrb::WorkbookBuilder
DSL context for Xlsxrb.build.
Public Class Methods
Source
# File lib/xlsxrb.rb, line 253 def initialize @sheets = [] @sheet_builders = [] # Keep track of sheet builders for style processing @defined_names = [] @core_properties = {} @app_properties = {} @custom_properties = [] @workbook_protection = nil end
Public Instance Methods
# File lib/xlsxrb.rb, line 316 def add_custom_property(name, value, type: :string) @custom_properties << { name: name, value: value, type: type } end
Add a custom document property.
# File lib/xlsxrb.rb, line 275 def add_defined_name(name, value, sheet: nil, hidden: false) entry = { name: name, value: value, hidden: hidden } entry[:local_sheet_name] = sheet if sheet @defined_names << entry end
Add a defined name.
# File lib/xlsxrb.rb, line 264 def add_sheet(name = nil, &block) name ||= "Sheet#{@sheets.size + 1}" sheet_builder = WorksheetBuilder.new(name) block.call(sheet_builder) if block_given? @sheet_builders << sheet_builder @sheets << sheet_builder.build end
Add a new sheet.
Source
# File lib/xlsxrb.rb, line 320 def build # Process styles from all sheets and collect style definitions processed_sheets, styles_definition = process_styles(@sheets) # Store workbook-level metadata in unmapped_data wb_meta = {} wb_meta[:defined_names] = resolve_defined_names(@defined_names, processed_sheets) unless @defined_names.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 Elements::Workbook.new( sheets: processed_sheets, styles: styles_definition, unmapped_data: wb_meta.empty? ? {} : { facade: wb_meta } ) end
# File lib/xlsxrb.rb, line 311 def set_app_property(name, value) @app_properties[name] = value end
Set an app document property.
# File lib/xlsxrb.rb, line 306 def set_core_property(name, value) @core_properties[name] = value end
Set a core document property.
# File lib/xlsxrb.rb, line 282 def set_print_area(range, sheet: nil) sheet_name = sheet || @sheets.last&.name || "Sheet1" value = "'#{sheet_name}'!#{absolute_range(range)}" @defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sheet_name } add_defined_name("_xlnm.Print_Area", value, sheet: sheet_name) end
Set the print area for a sheet.
# File lib/xlsxrb.rb, line 290 def set_print_titles(rows: nil, cols: nil, sheet: nil) sheet_name = sheet || @sheets.last&.name || "Sheet1" parts = [] parts << "'#{sheet_name}'!$#{cols.sub(":", ":$")}" if cols parts << "'#{sheet_name}'!$#{rows.sub(":", ":$")}" if rows value = parts.join(",") @defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name } add_defined_name("_xlnm.Print_Titles", value, sheet: sheet_name) end
Set print titles for a sheet.
# File lib/xlsxrb.rb, line 301 def set_workbook_protection(**opts) @workbook_protection = opts end
Set workbook protection.