class Xlsxrb::Ooxml::WorksheetWriter
Generates worksheet XML for a list of rows. Supports streaming: rows can be written one at a time.
Constants
- COLUMN_LETTERS
- DOC_REL_NS
- INTEGER_STRINGS
- SSML_NS
Attributes
(untyped resolver) → void
Sets what gives the style id for a Time-like value in a styled cell, so that a style without a number format can be given its date-time variant. @api private
Public Class Methods
(untyped io, ?date1904: untyped) → untyped
Source
# File lib/xlsxrb/ooxml/worksheet_writer.rb, line 47 def initialize(io, date1904: false) @io = io @date1904 = date1904 ? true : false @builder = XmlBuilder.new(@io) @row_buffer = String.new(capacity: 65_536) @style_attrs = {} # style id => '" s="<id>' fragment, built once per id @started = false @finished = false @column_style_ids = nil # column index => resolved style id, see column_style_ids @column_style_ids_map = nil @time_like_style_resolver = nil @strict_excel_mode = false end
Public Instance Methods
(?drawing_rid: untyped, ?picture_rid: untyped, ?sheet_protection: untyped, ?protected_ranges: untyped, ?auto_filter: untyped, ?filter_columns: untyped, ?sort_state: untyped, ?merge_cells: untyped, ?conditional_formats: untyped, ?data_validations: untyped, ?hyperlinks: untyped, ?print_options: untyped, ?page_margins: untyped, ?page_setup: untyped, ?header_footer: untyped, ?row_breaks: untyped, ?col_breaks: untyped, ?column_breaks: untyped, ?cell_watches: untyped, ?ignored_errors: untyped, ?tables: untyped, ?table_start_rid: untyped, ?legacy_drawing_rid: untyped, ?sparkline_groups: untyped, ?phonetic_pr: untyped, ?sheet_calc_pr: untyped) → untyped
Source
# File lib/xlsxrb/ooxml/worksheet_writer.rb, line 613 def finish(drawing_rid: nil, picture_rid: nil, sheet_protection: nil, protected_ranges: nil, auto_filter: nil, filter_columns: nil, sort_state: nil, merge_cells: nil, conditional_formats: nil, data_validations: nil, hyperlinks: nil, print_options: nil, page_margins: nil, page_setup: nil, header_footer: nil, row_breaks: nil, col_breaks: nil, column_breaks: nil, cell_watches: nil, ignored_errors: nil, tables: nil, table_start_rid: nil, legacy_drawing_rid: nil, sparkline_groups: nil, phonetic_pr: nil, sheet_calc_pr: nil) return if @finished start unless @started @finished = true if @row_buffer && !@row_buffer.empty? @io.write(@row_buffer) @row_buffer.clear end @builder.close_tag("sheetData") # Elements must appear in OOXML specification order after sheetData sheet_protection = sheet_protection.to_h if sheet_protection.is_a?(Elements::MetadataRecord) print_options = print_options.to_h if print_options.is_a?(Elements::MetadataRecord) page_margins = page_margins.to_h if page_margins.is_a?(Elements::MetadataRecord) page_setup = page_setup.to_h if page_setup.is_a?(Elements::MetadataRecord) header_footer = header_footer.to_h if header_footer.is_a?(Elements::MetadataRecord) phonetic_pr = phonetic_pr.to_h if phonetic_pr.is_a?(Elements::MetadataRecord) sheet_calc_pr = sheet_calc_pr.to_h if sheet_calc_pr.is_a?(Elements::MetadataRecord) calc_pr = sheet_calc_pr || (@sheet_properties if @sheet_properties && !@sheet_properties[:full_calc_on_load].nil?) write_sheet_calc_pr(calc_pr) if calc_pr write_sheet_protection(sheet_protection) if sheet_protection write_protected_ranges(protected_ranges) if protected_ranges && !protected_ranges.empty? write_auto_filter(auto_filter, filter_columns, sort_state) if auto_filter write_merge_cells(merge_cells) if merge_cells && !merge_cells.empty? write_phonetic_pr(phonetic_pr) if phonetic_pr write_conditional_formatting(conditional_formats) if conditional_formats && !conditional_formats.empty? write_data_validations(data_validations) if data_validations && !data_validations.empty? write_hyperlinks(hyperlinks) if hyperlinks && !hyperlinks.empty? write_print_options(print_options) if print_options && !print_options.empty? write_page_margins(page_margins) if page_margins write_page_setup(page_setup) if page_setup && !page_setup.empty? write_header_footer(header_footer) if header_footer && !header_footer.empty? write_row_breaks(row_breaks) if row_breaks && !row_breaks.empty? cbreaks = column_breaks || col_breaks write_col_breaks(cbreaks) if cbreaks && !cbreaks.empty? write_cell_watches(cell_watches) if cell_watches && !cell_watches.empty? write_ignored_errors(ignored_errors) if ignored_errors && !ignored_errors.empty? @builder.empty_tag("drawing", { "r:id": drawing_rid }) if drawing_rid @builder.empty_tag("legacyDrawing", { "r:id": legacy_drawing_rid }) if legacy_drawing_rid @builder.empty_tag("picture", { "r:id": picture_rid }) if picture_rid write_table_parts(tables, table_start_rid) if tables && !tables.empty? write_sparklines(sparkline_groups) if sparkline_groups && !sparkline_groups.empty? @builder.close_tag("worksheet") end
Write the worksheet footer. Call once after all rows. Options for post-sheetData elements (in OOXML order):
() → void
Source
# File lib/xlsxrb/ooxml/worksheet_writer.rb, line 85 def forget_column_style_ids @column_style_ids = nil end
Drops the resolved column style ids, for a caller that changed the style id a name in the style map stands for. @api private
(untyped col_index, untyped style_id) → untyped
Source
# File lib/xlsxrb/ooxml/worksheet_writer.rb, line 75 def set_column_style(col_index, style_id) @column_styles ||= {} @column_styles[col_index] = style_id @column_style_ids = nil end
(?columns: untyped, ?sheet_properties: untyped, ?freeze_pane: untyped, ?split_pane: untyped, ?selection: untyped, ?sheet_view: untyped, ?sheet_format: untyped) → untyped
Source
# File lib/xlsxrb/ooxml/worksheet_writer.rb, line 96 def start(columns: [], sheet_properties: nil, freeze_pane: nil, split_pane: nil, selection: nil, sheet_view: nil, sheet_format: nil) return if @started @started = true sheet_properties = sheet_properties.to_h if sheet_properties.is_a?(Elements::MetadataRecord) sheet_format = sheet_format.to_h if sheet_format.is_a?(Elements::MetadataRecord) freeze_pane = freeze_pane.to_h if freeze_pane.is_a?(Elements::MetadataRecord) split_pane = split_pane.to_h if split_pane.is_a?(Elements::MetadataRecord) selection = selection.to_h if selection.is_a?(Elements::MetadataRecord) sheet_view = sheet_view.to_h if sheet_view.is_a?(Elements::MetadataRecord) @sheet_properties = sheet_properties @sheet_format = sheet_format @builder.declaration @builder.open_tag("worksheet", { xmlns: SSML_NS, "xmlns:r": DOC_REL_NS }) @column_styles ||= {} if columns && !columns.empty? columns.each do |col| if col.is_a?(Elements::Column) s = col.style_index min = col.unmapped_data[:min] ? col.unmapped_data[:min] - 1 : col.index max = col.unmapped_data[:max] ? col.unmapped_data[:max] - 1 : col.index else s = col[:style_index] || col[:style] min = col[:min] ? col[:min] - 1 : (col[:index] || 0) max = col[:max] ? col[:max] - 1 : (col[:index] || min) end next unless s (min..max).each { |i| @column_styles[i] = s } end end @column_styles = nil if @column_styles.empty? write_sheet_properties(sheet_properties) if sheet_properties && !sheet_properties.empty? write_sheet_views(freeze_pane: freeze_pane, split_pane: split_pane, selection: selection, sheet_view: sheet_view) if freeze_pane || split_pane || selection || (sheet_view && !sheet_view.empty?) write_sheet_format_pr(sheet_format) if sheet_format && !sheet_format.empty? write_columns(columns) unless columns.empty? @builder.open_tag("sheetData") end
Write the worksheet header. Call once before writing rows. Options for pre-sheetData elements:
sheet_properties: Hash of sheet-level properties (:tab_color, etc.)
freeze_pane: { row:, col:, state: :frozen }
split_pane: { x_split:, y_split:, top_left_cell: }
selection: { active_cell:, sqref:, pane: }
sheet_view: Hash of sheet view properties
(bool strict) → void
Source
# File lib/xlsxrb/ooxml/worksheet_writer.rb, line 65 def strict_excel_mode=(strict) @strict_excel_mode = strict ? true : false end
Makes the row writers reject text longer than a cell holds (32,767 characters), as the strict Excel mode of the caller asks. @api private
(untyped row_index, untyped cells, ?attrs: untyped, ?unmapped: untyped, ?sst_index: untyped) → untyped
Source
# File lib/xlsxrb/ooxml/worksheet_writer.rb, line 139 def write_row(row_index, cells, attrs: {}, unmapped: [], sst_index: nil) start unless @started row_num = row_index + 1 row_num_str = row_num.to_s buf = @row_buffer ||= String.new(capacity: 65_536) buf << '<row r="' << row_num_str << '"' buf << ' s="' << attrs[:style_index].to_s << '" customFormat="1"' if attrs[:style_index].is_a?(Integer) if attrs[:height] buf << ' ht="' << escape_xml(attrs[:height]) << '" customHeight="1"' elsif attrs[:custom_height] buf << ' customHeight="1"' end buf << ' hidden="1"' if attrs[:hidden] buf << ' collapsed="1"' if attrs[:collapsed] buf << ' outlineLevel="' << escape_xml(attrs[:outline_level]) << '"' if attrs[:outline_level] buf << ">" row_style = resolve_style_id(attrs[:style_index], nil) style_attrs = @style_attrs cells.each do |cell| if cell.is_a?(Elements::Cell) value = cell.value cell_style_id = cell.style || cell.style_index col_ref = cell.ref || "#{column_letter(cell.column_index)}#{row_num_str}" col_idx = cell.column_index formula = cell.formula formula_ca = false cell_type_val = nil elsif cell.is_a?(Hash) value = cell[:value] cell_style_id = cell[:style_index] col_idx = cell[:column_index] col_ref = if cell[:ref] escape_xml(cell[:ref]) elsif col_idx "#{column_letter(col_idx)}#{row_num_str}" end formula = cell[:formula] formula_ca = cell[:formula_ca] cell_type_val = cell[:type] && escape_xml(cell[:type]) else value = cell cell_style_id = nil col_idx = nil col_ref = nil formula = nil formula_ca = false cell_type_val = nil end cell_style_id = resolve_style_id(cell_style_id, nil) style_id = cell_style_id.nil? ? (row_style || (@column_styles && col_idx && resolve_style_id(@column_styles[col_idx], nil))) : cell_style_id style_id = nil unless style_id.is_a?(Integer) raise ArgumentError, "Non-finite Float #{value} is not supported in XLSX" if value.is_a?(Float) && !value.finite? # Fast path for common cells, styled or not if !formula && !cell_type_val # '" s="<id>' to append after the cell reference, or "" for an unstyled cell s_attr = style_id ? (style_attrs[style_id] ||= -%(" s="#{style_id})) : "" case value when Integer, Float buf << '<c r="' << col_ref << s_attr << '"><v>' << value.to_s << "</v></c>" next when String if sst_index && (idx = sst_index[value]) buf << '<c r="' << col_ref << s_attr << '" t="s"><v>' << idx.to_s << "</v></c>" next end when true buf << '<c r="' << col_ref << s_attr << '" t="b"><v>1</v></c>' next when false buf << '<c r="' << col_ref << s_attr << '" t="b"><v>0</v></c>' next when nil # An empty cell keeps only its own style, not the row or column style. if cell_style_id.is_a?(Integer) buf << '<c r="' << col_ref << '" s="' << cell_style_id.to_s << '"/>' else buf << '<c r="' << col_ref << '"/>' end next when DateTime serial = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904) buf << '<c r="' << col_ref << s_attr << '"><v>' << serial.to_s << "</v></c>" next when Date serial = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904) buf << '<c r="' << col_ref << s_attr << '"><v>' << serial.to_s << "</v></c>" next end end if value.nil? && formula.nil? if cell_style_id.is_a?(Integer) buf << '<c r="' << col_ref << '" s="' << cell_style_id.to_s << '"/>' else buf << '<c r="' << col_ref << '"/>' end next end xml_val = value type = cell_type_val formula_type = nil formula_ref = nil formula_si = nil if formula if formula.is_a?(Xlsxrb::Elements::Formula) formula_expr = formula.expression formula_ca = formula.calculate_always? formula_type = formula.type formula_ref = formula.ref formula_si = formula.shared_index xml_val = formula.cached_value.nil? ? value : formula.cached_value elsif formula.is_a?(Hash) formula_expr = formula[:formula] || formula[:expression] formula_ca = formula[:calculate_always] formula_type = formula[:type] formula_ref = formula[:ref] formula_si = formula[:si] || formula[:shared_index] xml_val = if formula.key?(:value) formula[:value] elsif formula.key?(:cached_value) formula[:cached_value] else value end else formula_expr = formula xml_val = value end formula_expr = formula_expr[1..] if formula_expr.is_a?(String) && formula_expr.start_with?("=") end raise ArgumentError, "Non-finite Float #{xml_val} is not supported in XLSX" if xml_val.is_a?(Float) && !xml_val.finite? if formula_expr case xml_val when true type = "b" xml_val = "1" when false type = "b" xml_val = "0" when Xlsxrb::Elements::CellError type = "e" xml_val = xml_val.code end elsif !type case value when String if sst_index && (idx = sst_index[value]) xml_val = idx type = "s" else type = "inlineStr" end when Xlsxrb::Elements::RichText if sst_index && (idx = sst_index[value]) xml_val = idx type = "s" else type = "inlineStr" xml_val = value end when true xml_val = "1" type = "b" when false xml_val = "0" type = "b" when Time, DateTime xml_val = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904) when Date xml_val = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904) when TimeLike # after Date, so Date values skip TimeLike.=== xml_val = Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904) when BigDecimal xml_val = value.to_s("F") when Xlsxrb::Elements::CellError xml_val = value.code type = "e" end end buf << '<c r="' << col_ref << '"' buf << ' s="' << style_id.to_s << '"' if style_id buf << ' t="' << type << '"' if type if type == "inlineStr" if xml_val.is_a?(Xlsxrb::Elements::RichText) write_inline_rich_text(buf, xml_val) else buf << "><is><t>" << escape_text(xml_val.to_s) << "</t></is></c>" end elsif formula_expr || (formula_type && formula_type.to_sym == :shared && !formula_si.nil?) f_attrs = +"" if formula_type case formula_type.to_sym when :array f_attrs << ' t="array"' f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref when :shared f_attrs << ' t="shared"' f_attrs << %( si="#{escape_xml(formula_si)}") if formula_si f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref end end f_attrs << ' ca="1"' if formula_ca if formula_expr && !formula_expr.empty? && !(formula_type&.to_sym == :shared && formula_ref.nil?) buf << "><f#{f_attrs}>" << escape_xml(formula_expr) << "</f>" else buf << "><f#{f_attrs}/>" end if xml_val.nil? buf << "<v>0</v></c>" else v_str = xml_val.to_s v_str = v_str.dup.force_encoding(Encoding::UTF_8) if v_str.encoding == Encoding::BINARY v_str = if type == "str" || (type != "e" && xml_val.is_a?(String)) escape_text(v_str) elsif xml_val.is_a?(Numeric) v_str else escape_xml(v_str) end buf << "<v>" << v_str << "</v></c>" end else v_str = xml_val.to_s v_str = v_str.dup.force_encoding(Encoding::UTF_8) if v_str.encoding == Encoding::BINARY v_str = if type == "str" escape_text(v_str) elsif xml_val.is_a?(Numeric) || xml_val.is_a?(BigDecimal) v_str else escape_xml(v_str) end buf << "><v>" << v_str << "</v></c>" end end buf << "</row>" if buf.bytesize >= 32_768 @io.write(buf) buf.clear end unmapped.each { |node| @builder.write_unmapped(node) } end
Write a single row. Automatically calls start if needed.
(untyped row_index, untyped values, ?styles: untyped, ?style_map: untyped, ?sst: untyped, ?sst_index: untyped, ?attrs: untyped) → untyped
Source
# File lib/xlsxrb/ooxml/worksheet_writer.rb, line 395 def write_row_values(row_index, values, styles: nil, style_map: nil, sst: nil, sst_index: nil, attrs: nil) start unless @started row_num = row_index + 1 row_num_str = row_num < 65_536 ? INTEGER_STRINGS[row_num] : row_num.to_s buf = @row_buffer ||= String.new(capacity: 65_536) if styles.nil? && attrs.nil? && @column_styles.nil? buf << '<row r="' << row_num_str << '">' col_index = 0 max_len = values.length while col_index < max_len value = values[col_index] col_ref = COLUMN_LETTERS[col_index] || column_letter(col_index) col_index += 1 next if value.nil? case value when Integer val_str = value >= 0 && value < 65_536 ? INTEGER_STRINGS[value] : value.to_s buf << '<c r="' << col_ref << row_num_str << '"><v>' << val_str << "</v></c>" when Float raise ArgumentError, "Non-finite Float #{value} is not supported in XLSX" unless value.finite? buf << '<c r="' << col_ref << row_num_str << '"><v>' << value.to_s << "</v></c>" when String DslHelpers.check_cell_text_length!(value) if @strict_excel_mode && value.bytesize > DslHelpers::MAX_CELL_TEXT_LENGTH if sst_index idx = (sst_index[value] ||= begin sst << value sst.size - 1 end) idx_str = idx < 65_536 ? INTEGER_STRINGS[idx] : idx.to_s buf << '<c r="' << col_ref << row_num_str << '" t="s"><v>' << idx_str << "</v></c>" else buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"><is><t>' << escape_text(value) << "</t></is></c>" end when true buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>1</v></c>' when false buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>0</v></c>' when Time, DateTime time_style_id = style_map ? style_map["__xlsxrb_time"] : nil buf << if time_style_id.is_a?(Integer) "<c r=\"#{col_ref}#{row_num_str}\" s=\"#{time_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>" else "<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>" end when Date date_style_id = style_map ? style_map["__xlsxrb_date"] : nil buf << if date_style_id.is_a?(Integer) "<c r=\"#{col_ref}#{row_num_str}\" s=\"#{date_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)}</v></c>" else "<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)}</v></c>" end when TimeLike # after Date, so Date values skip TimeLike.=== time_style_id = style_map ? style_map["__xlsxrb_time"] : nil buf << if time_style_id.is_a?(Integer) "<c r=\"#{col_ref}#{row_num_str}\" s=\"#{time_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904)}</v></c>" else "<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904)}</v></c>" end when Xlsxrb::Elements::Formula raise ArgumentError, "Non-finite Float #{value.cached_value} is not supported in XLSX" if value.cached_value.is_a?(Float) && !value.cached_value.finite? formula_expr = value.expression formula_expr = formula_expr[1..] if formula_expr.start_with?("=") f_attrs = +"" if value.type case value.type.to_sym when :array f_attrs << ' t="array"' f_attrs << %( ref="#{escape_xml(value.ref)}") if value.ref when :shared f_attrs << ' t="shared"' f_attrs << %( si="#{escape_xml(value.shared_index)}") if value.shared_index f_attrs << %( ref="#{escape_xml(value.ref)}") if value.ref end end f_attrs << ' ca="1"' if value.calculate_always? c_type = "" case value.cached_value when true c_type = ' t="b"' v_val = "1" when false c_type = ' t="b"' v_val = "0" when Xlsxrb::Elements::CellError c_type = ' t="e"' v_val = escape_xml(value.cached_value.code) when nil v_val = 0 else v_val = value.cached_value.is_a?(Numeric) ? value.cached_value : escape_xml(value.cached_value) end f_tag = if formula_expr && !formula_expr.empty? && !(value.type&.to_sym == :shared && value.ref.nil?) "<f#{f_attrs}>#{escape_xml(formula_expr)}</f>" else "<f#{f_attrs}/>" end buf << "<c r=\"#{col_ref}#{row_num_str}\"#{c_type}>#{f_tag}<v>#{v_val}</v></c>" when Hash if value.key?(:formula) || value.key?(:expression) || (value[:type]&.to_sym == :shared && (value.key?(:shared_index) || value.key?(:si))) formula_expr = value[:formula] || value[:expression] formula_expr = formula_expr[1..] if formula_expr.is_a?(String) && formula_expr.start_with?("=") f_attrs = +"" f_type = value[:type] || (:shared if value.key?(:shared_index) || value.key?(:si)) if f_type case f_type.to_sym when :array f_attrs << ' t="array"' f_attrs << %( ref="#{escape_xml(value[:ref])}") if value[:ref] when :shared f_attrs << ' t="shared"' si = value[:si] || value[:shared_index] f_attrs << %( si="#{escape_xml(si)}") if si f_attrs << %( ref="#{escape_xml(value[:ref])}") if value[:ref] end end f_attrs << ' ca="1"' if value[:calculate_always] xml_val = if value.key?(:value) && !value[:value].nil? value[:value] elsif value.key?(:cached_value) && !value[:cached_value].nil? value[:cached_value] else 0 end raise ArgumentError, "Non-finite Float #{xml_val} is not supported in XLSX" if xml_val.is_a?(Float) && !xml_val.finite? c_type = "" case xml_val when true c_type = ' t="b"' xml_val = "1" when false c_type = ' t="b"' xml_val = "0" when Xlsxrb::Elements::CellError c_type = ' t="e"' xml_val = xml_val.code end xml_val = escape_xml(xml_val) unless xml_val.is_a?(Numeric) f_tag = if formula_expr && !formula_expr.empty? && !(f_type&.to_sym == :shared && value[:ref].nil?) "<f#{f_attrs}>#{escape_xml(formula_expr)}</f>" else "<f#{f_attrs}/>" end buf << "<c r=\"#{col_ref}#{row_num_str}\"#{c_type}>#{f_tag}<v>#{xml_val}</v></c>" end when Xlsxrb::Elements::CellError buf << "<c r=\"#{col_ref}#{row_num_str}\" t=\"e\"><v>#{escape_xml(value.code)}</v></c>" when BigDecimal buf << "<c r=\"#{col_ref}#{row_num_str}\"><v>#{value.to_s("F")}</v></c>" else if sst_index idx = (sst_index[value] ||= begin sst << value sst.size - 1 end) idx_str = idx < 65_536 ? INTEGER_STRINGS[idx] : idx.to_s buf << "<c r=\"#{col_ref}#{row_num_str}\" t=\"s\"><v>#{idx_str}</v></c>" elsif value.is_a?(Xlsxrb::Elements::RichText) buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"' write_inline_rich_text(buf, value) else buf << "<c r=\"#{col_ref}#{row_num_str}\" t=\"inlineStr\"><is><t>#{escape_text(value.to_s)}</t></is></c>" end end end buf << "</row>" return unless buf.bytesize >= 32_768 @io.write(buf) buf.clear return end is_styles_collection = styles && (styles.is_a?(Array) || styles.is_a?(Hash)) single_style_id = resolve_style_id(styles, style_map) unless is_styles_collection row_style = attrs ? attrs[:style_index] : nil row_style_id = resolve_style_id(row_style, style_map) buf << '<row r="' << row_num_str << '"' buf << ' s="' << row_style_id.to_s << '" customFormat="1"' if row_style_id.is_a?(Integer) if attrs if attrs[:height] buf << ' ht="' << escape_xml(attrs[:height]) << '" customHeight="1"' elsif attrs[:custom_height] buf << ' customHeight="1"' end buf << ' hidden="1"' if attrs[:hidden] buf << ' collapsed="1"' if attrs[:collapsed] buf << ' outlineLevel="' << escape_xml(attrs[:outline_level]) << '"' if attrs[:outline_level] end buf << ">" max_len = values.length if is_styles_collection styles_len = styles.is_a?(Array) ? styles.length : (styles.keys.max || -1) + 1 max_len = [max_len, styles_len].max end write_styled_cells(buf, values, max_len, row_num_str, styles, is_styles_collection, single_style_id, row_style_id, style_map, sst, sst_index) buf << "</row>" return unless buf.bytesize >= 32_768 @io.write(buf) buf.clear end
Highly optimized row writing for StreamWriter that avoids allocating intermediate Hashes.