class Xlsxrb::Ooxml::WorkbookWriter
Orchestrates writing a complete XLSX workbook. Generates all required OpenXML parts and assembles them into a ZIP.
Constants
- CT_NS
- DOC_REL
- REL_NS
- SSML_NS
Public Class Methods
(sheets: untyped, ?shared_strings: untyped, ?shared_strings_index: untyped, ?styles: untyped, ?defined_names: untyped, ?core_properties: untyped, ?app_properties: untyped, ?custom_properties: untyped, ?workbook_protection: untyped, ?workbook_properties: untyped, ?compression_level: untyped) → untyped
Source
# File lib/xlsxrb/ooxml/workbook_writer.rb, line 45 def initialize(sheets:, shared_strings: [], shared_strings_index: nil, styles: nil, defined_names: nil, core_properties: nil, app_properties: nil, custom_properties: nil, workbook_protection: nil, workbook_properties: nil, compression_level: DEFAULT_COMPRESSION_LEVEL) @sheets = sheets @shared_strings = shared_strings || [] @shared_strings_index = shared_strings_index @styles = styles || {} @defined_names = defined_names || [] @core_properties = core_properties.is_a?(Elements::MetadataRecord) ? core_properties.to_h : (core_properties || {}) @app_properties = app_properties.is_a?(Elements::MetadataRecord) ? app_properties.to_h : (app_properties || {}) @custom_properties = custom_properties || [] @workbook_protection = workbook_protection.is_a?(Elements::MetadataRecord) ? workbook_protection.to_h : workbook_protection @workbook_properties = workbook_properties.is_a?(Elements::MetadataRecord) ? workbook_properties.to_h : (workbook_properties || { update_links: "never" }) @compression_level = compression_level || DEFAULT_COMPRESSION_LEVEL @drawing_count = 0 @chart_count = 0 @comment_count = 0 @table_count = 0 @pivot_cache_count = 0 @pivot_table_count = 0 @threaded_comment_count = 0 @persons = [] end
(untyped target, sheets: untyped, ?shared_strings: untyped, ?shared_strings_index: untyped, ?styles: untyped, ?defined_names: untyped, ?core_properties: untyped, ?app_properties: untyped, ?custom_properties: untyped, ?workbook_protection: untyped, ?workbook_properties: untyped, ?compression_level: untyped) → untyped
Source
# File lib/xlsxrb/ooxml/workbook_writer.rb, line 33 def self.write(target, sheets:, shared_strings: [], shared_strings_index: nil, styles: nil, defined_names: nil, core_properties: nil, app_properties: nil, custom_properties: nil, workbook_protection: nil, workbook_properties: nil, compression_level: DEFAULT_COMPRESSION_LEVEL) writer = new(sheets: sheets, shared_strings: shared_strings, shared_strings_index: shared_strings_index, styles: styles, defined_names: defined_names, core_properties: core_properties, app_properties: app_properties, custom_properties: custom_properties, workbook_protection: workbook_protection, workbook_properties: workbook_properties, compression_level: compression_level) writer.write_to(target) end
Public Instance Methods
(untyped sheet, untyped idx) → untyped
Source
# File lib/xlsxrb/ooxml/workbook_writer.rb, line 135 def prepare_sheet_auxiliary(sheet, idx) sheet_images = sheet[:images] || [] sheet_charts = sheet[:charts] || [] sheet_shapes = sheet[:shapes] || [] sheet_comments = sheet[:comments] || [] sheet_threaded_comments = sheet[:threaded_comments] || [] sheet_tables = sheet[:tables] || [] sheet_hyperlinks = sheet[:hyperlinks] || [] has_drawing = sheet_images.any? || sheet_charts.any? || sheet_shapes.any? has_comments = sheet_comments.any? has_threaded_comments = sheet_threaded_comments.any? sheet_rels = [] drawing_rid = nil vml_rid = nil table_start_rid = nil picture_rid = nil hyperlink_rels = [] if has_drawing @drawing_count += 1 drawing_rid_num = sheet_rels.size + 1 sheet_rels << { id: "rId#{drawing_rid_num}", type: "#{DOC_REL}/drawing", target: "../drawings/drawing#{@drawing_count}.xml" } drawing_rid = "rId#{drawing_rid_num}" end if has_comments @comment_count += 1 comment_rid_num = sheet_rels.size + 1 sheet_rels << { id: "rId#{comment_rid_num}", type: "#{DOC_REL}/comments", target: "../comments#{@comment_count}.xml" } vml_rid_num = sheet_rels.size + 1 sheet_rels << { id: "rId#{vml_rid_num}", type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing", target: "../drawings/vmlDrawing#{@comment_count}.vml" } vml_rid = "rId#{vml_rid_num}" end if has_threaded_comments @threaded_comment_count += 1 tc_rid_num = sheet_rels.size + 1 sheet_rels << { id: "rId#{tc_rid_num}", type: "http://schemas.microsoft.com/office/2017/10/relationships/threadedComment", target: "../threadedComments/threadedComment#{@threaded_comment_count}.xml" } end sheet_hyperlinks.each do |link| next unless link[:url] h_rid_num = sheet_rels.size + 1 sheet_rels << { id: "rId#{h_rid_num}", type: "#{DOC_REL}/hyperlink", target: link[:url], target_mode: "External" } hyperlink_rels << h_rid_num end h_rid_idx = 0 enriched_hyperlinks = sheet_hyperlinks.map do |link| if link[:url] rid = hyperlink_rels[h_rid_idx] h_rid_idx += 1 link.merge(_rid: rid) else link end end unless sheet_tables.empty? table_start_rid = sheet_rels.size + 1 sheet_tables.each do @table_count += 1 t_rid_num = sheet_rels.size + 1 sheet_rels << { id: "rId#{t_rid_num}", type: "#{DOC_REL}/table", target: "../tables/table#{@table_count}.xml" } end end sheet_pivot_tables = sheet[:pivot_tables] || [] unless sheet_pivot_tables.empty? sheet_pivot_tables.each do @pivot_cache_count += 1 @pivot_table_count += 1 pt_rid_num = sheet_rels.size + 1 sheet_rels << { id: "rId#{pt_rid_num}", type: "#{DOC_REL}/pivotTable", target: "../pivotTables/pivotTable#{@pivot_table_count}.xml" } end end bg_img = sheet[:background_image] || sheet[:background_picture] || sheet[:picture] if bg_img bg_data = if bg_img.is_a?(Hash) bg_img[:file_data] || bg_img[:data] else (bg_img.respond_to?(:data) ? bg_img.data : bg_img) end # The data is the image itself and is never read as a path (ADR 0012). bg_ext = if bg_img.is_a?(Hash) (bg_img[:ext] || (bg_img[:filename] && File.extname(bg_img[:filename]).delete_prefix(".")) || "png").to_s.downcase elsif bg_img.respond_to?(:filename) File.extname(bg_img.filename).delete_prefix(".").downcase else "png" end if bg_data && !bg_data.empty? bg_ext = media_extension(bg_ext) p_rid_num = sheet_rels.size + 1 picture_rid = "rId#{p_rid_num}" sheet_rels << { id: picture_rid, type: "#{DOC_REL}/image", target: "../media/sheet#{idx + 1}_bg.#{bg_ext}" } sheet[:_bg_media] = { path: "xl/media/sheet#{idx + 1}_bg.#{bg_ext}", data: bg_data.b, ext: bg_ext } end end sheet[:_prepared_rels] = sheet_rels sheet[:_enriched_hyperlinks] = enriched_hyperlinks { drawing_rid: drawing_rid, vml_rid: vml_rid, table_start_rid: table_start_rid, picture_rid: picture_rid, enriched_hyperlinks: enriched_hyperlinks, sheet_rels: sheet_rels } end
(untyped zip) → untyped
Source
# File lib/xlsxrb/ooxml/workbook_writer.rb, line 251 def write_auxiliary_parts(zip) drawing_idx = 0 chart_idx = 0 media_idx = 0 comment_idx = 0 tc_idx = 0 tc_writer = nil table_idx = 0 pivot_cache_idx = 0 pivot_table_idx = 0 @sheets.each_with_index do |sheet, idx| sheet_images = sheet[:images] || [] sheet_charts = sheet[:charts] || [] sheet_shapes = sheet[:shapes] || [] sheet_comments = sheet[:comments] || [] sheet_tables = sheet[:tables] || [] has_drawing = sheet_images.any? || sheet_charts.any? || sheet_shapes.any? has_comments = sheet_comments.any? sheet_rels = sheet[:_prepared_rels] || [] if has_drawing drawing_idx += 1 drawing_rels_data = [] drawing_parts = [] chart_writer = Xlsxrb::Ooxml::Writer.new chart_writer.add_sheet(sheet[:name]) unless sheet[:name] == "Sheet1" (sheet[:rows] || []).each do |row| cells = row.is_a?(Hash) ? row[:cells] : row.cells (cells || []).each do |cell| ref = cell.is_a?(Hash) ? cell[:ref] : cell.ref value = cell.is_a?(Hash) ? cell[:value] : cell.value chart_writer.set_cell(ref, value, sheet: sheet[:name]) end end sheet[:cells]&.each do |ref, value| chart_writer.set_cell(ref, value, sheet: sheet[:name]) end sheet_images.each do |img| # One media file per image: a sheet can hold several. media_idx += 1 media_name = "image#{media_idx}.#{media_extension(img[:ext])}" zip.add_binary_entry("xl/media/#{media_name}", img[:file_data]) drawing_rels_data << { type: :image, target: "../media/#{media_name}" } drawing_parts << { kind: :pic, img: img, rid_index: drawing_rels_data.size } end sheet_charts.each do |chart_options| chart_writer.add_chart(**chart_options) end processed_charts = chart_writer.charts processed_charts.each do |chart| chart_idx += 1 chart_path = "xl/charts/chart#{chart_idx}.xml" zip.add_entry(chart_path, chart_writer.send(:generate_chart_xml, chart)) drawing_rels_data << { type: :chart, target: "../charts/chart#{chart_idx}.xml" } drawing_parts << { kind: :chart, chart: chart, rid_index: drawing_rels_data.size } end sheet_shapes.each_with_index do |shape, si| drawing_parts << { kind: :sp, shape: shape, id: drawing_parts.size + si + 2 } end drawing_xml = chart_writer.send(:generate_drawing_xml, drawing_parts) zip.add_entry("xl/drawings/drawing#{drawing_idx}.xml", drawing_xml) unless drawing_rels_data.empty? drawing_rels_xml = chart_writer.send(:generate_drawing_rels, drawing_rels_data) zip.add_entry("xl/drawings/_rels/drawing#{drawing_idx}.xml.rels", drawing_rels_xml) end end if has_comments comment_idx += 1 comment_writer = Xlsxrb::Ooxml::Writer.new sheet_comments.each do |c| c = c.to_h if c.is_a?(Elements::MetadataRecord) comment_writer.add_comment(c[:cell] || c[:ref], c[:text], author: c[:author] || "Author", visible: c[:visible] == true, anchor: c[:anchor]) end zip.add_entry("xl/comments#{comment_idx}.xml", comment_writer.send(:generate_comments_xml, comment_writer.comments)) zip.add_entry("xl/drawings/vmlDrawing#{comment_idx}.vml", comment_writer.send(:generate_vml_drawing_xml, comment_writer.comments)) end sheet_threaded_comments = sheet[:threaded_comments] || [] if sheet_threaded_comments.any? tc_idx += 1 tc_writer ||= Xlsxrb::Ooxml::Writer.new sheet_key = "__tc_#{tc_idx}" tc_writer.add_sheet(sheet_key) sheet_threaded_comments.each do |c| c = c.to_h if c.is_a?(Elements::MetadataRecord) tc_writer.add_threaded_comment( c[:cell] || c[:ref], c[:text], author: c[:author] || "Author", timestamp: c[:timestamp] || c[:dt] || c[:created_at], id: c[:id], parent_id: c[:parent_id], done: c.key?(:done) ? c[:done] : c[:resolved], user_id: c[:user_id], provider_id: c[:provider_id], sheet: sheet_key ) end zip.add_entry( "xl/threadedComments/threadedComment#{tc_idx}.xml", tc_writer.send(:generate_threaded_comments_xml, tc_writer.threaded_comments(sheet: sheet_key)) ) end sheet_tables.each do |tbl| table_idx += 1 zip.add_entry("xl/tables/table#{table_idx}.xml", build_table_xml(tbl, table_idx)) end sheet_pivot_tables = sheet[:pivot_tables] || [] unless sheet_pivot_tables.empty? # The writer only collects the definitions here, so its default sheet holds them # whatever the worksheet is called; source ranges name their own sheet. pivot_writer = Xlsxrb::Ooxml::Writer.new sheet_pivot_tables.each do |pt| source_ref, args = pivot_table_arguments(pt) pivot_writer.add_pivot_table(source_ref, **args) end pivot_data = pivot_writer.pivot_tables pivot_data.each do |pt_data| pivot_cache_idx += 1 pivot_table_idx += 1 zip.add_entry("xl/pivotCache/pivotCacheDefinition#{pivot_cache_idx}.xml", pivot_writer.send(:generate_pivot_cache_definition_xml, pt_data, pivot_cache_idx)) zip.add_entry("xl/pivotCache/pivotCacheRecords#{pivot_cache_idx}.xml", pivot_writer.send(:generate_pivot_cache_records_xml, pt_data)) zip.add_entry("xl/pivotTables/pivotTable#{pivot_table_idx}.xml", pivot_writer.send(:generate_pivot_table_xml, pt_data, pivot_cache_idx)) zip.add_entry("xl/pivotCache/_rels/pivotCacheDefinition#{pivot_cache_idx}.xml.rels", pivot_writer.send(:generate_pivot_cache_rels, pivot_cache_idx)) zip.add_entry("xl/pivotTables/_rels/pivotTable#{pivot_table_idx}.xml.rels", pivot_writer.send(:generate_pivot_table_rels, pivot_cache_idx)) end end zip.add_binary_entry(sheet[:_bg_media][:path], sheet[:_bg_media][:data]) if sheet[:_bg_media] zip.add_entry("xl/worksheets/_rels/sheet#{idx + 1}.xml.rels", build_sheet_rels_from_list(sheet_rels)) unless sheet_rels.empty? end return unless tc_writer @persons = tc_writer.persons return unless @persons.any? zip.add_entry("xl/persons/person.xml", tc_writer.send(:generate_persons_xml, @persons)) end
(untyped zip) → untyped
Source
# File lib/xlsxrb/ooxml/workbook_writer.rb, line 410 def write_package_metadata(zip) zip.add_entry("[Content_Types].xml", build_content_types) zip.add_entry("_rels/.rels", build_root_rels) zip.add_entry("xl/workbook.xml", build_workbook_xml) zip.add_entry("xl/_rels/workbook.xml.rels", build_workbook_rels) zip.add_entry("xl/styles.xml", build_styles_xml) write_shared_strings_xml(zip) unless @shared_strings.empty? # Document properties zip.add_entry("docProps/core.xml", build_core_properties_xml) unless @core_properties.empty? zip.add_entry("docProps/app.xml", build_app_properties_xml) unless @app_properties.empty? zip.add_entry("docProps/custom.xml", build_custom_properties_xml) unless @custom_properties.empty? end
(untyped zip) → untyped
Source
# File lib/xlsxrb/ooxml/workbook_writer.rb, line 424 def write_package_parts(zip) preprocess_conditional_formats! write_auxiliary_parts(zip) write_package_metadata(zip) end
(untyped target) → untyped
Source
# File lib/xlsxrb/ooxml/workbook_writer.rb, line 70 def write_to(target) raise ArgumentError, "Workbook must contain at least one sheet (Excel limitation)" if @sheets.empty? preprocess_conditional_formats! active_tab_idx = resolve_active_tab_index effective_active_idx = active_tab_idx || 0 ZipWriter.open(target, compression_level: @compression_level) do |zip| @sheets.each_with_index do |sheet, idx| info = prepare_sheet_auxiliary(sheet, idx) sheet_tables = sheet[:tables] || [] sv = sheet[:sheet_view] sv = sv.to_h if sv && !sv.is_a?(Hash) if idx == effective_active_idx sv = (sv || {}).merge(tab_selected: true) elsif sv&.key?(:tab_selected) sv = sv.dup sv.delete(:tab_selected) end zip.start_entry("xl/worksheets/sheet#{idx + 1}.xml") write_worksheet_xml( ZipEntryIO.new(zip), sheet, drawing_rid: info[:drawing_rid], picture_rid: info[:picture_rid], sheet_protection: sheet[:sheet_protection], auto_filter: sheet[:auto_filter], filter_columns: sheet[:filter_columns], sort_state: sheet[:sort_state], merge_cells: sheet[:merge_cells], conditional_formats: sheet[:conditional_formats], data_validations: sheet[:data_validations], hyperlinks: info[:enriched_hyperlinks].empty? ? nil : info[:enriched_hyperlinks], print_options: sheet[:print_options], page_margins: sheet[:page_margins], page_setup: sheet[:page_setup], header_footer: sheet[:header_footer], row_breaks: sheet[:row_breaks], column_breaks: sheet[:column_breaks], freeze_pane: sheet[:freeze_pane], split_pane: sheet[:split_pane], selection: sheet[:selection], sheet_view: sv, sheet_properties: sheet[:sheet_properties], sheet_format: sheet[:sheet_format], protected_ranges: sheet[:protected_ranges], cell_watches: sheet[:cell_watches], ignored_errors: sheet[:ignored_errors], phonetic_pr: sheet[:phonetic_properties] || sheet[:phonetic_pr], sheet_calc_pr: sheet[:sheet_calc_pr], tables: sheet_tables, table_start_rid: info[:table_start_rid], legacy_drawing_rid: info[:vml_rid], sparkline_groups: sheet[:sparkline_groups] ) zip.finish_entry end write_package_parts(zip) end end