class Xlsxrb::Ooxml::Reader::PivotTableListener
SAX2 listener for parsing pivotTable XML.
Attributes
Public Class Methods
Source
# File lib/xlsxrb/ooxml/reader.rb, line 7699 def initialize @pivot_table = nil @fields = [] @row_fields = [] @col_fields = [] @data_fields = [] @inside_row_fields = false @inside_col_fields = false @inside_data_fields = false @inside_pivot_field = false @inside_items = false @current_items = [] end
Public Instance Methods
# File lib/xlsxrb/ooxml/reader.rb, line 7846 def end_element(_uri, local_name, qname) name = element_name(local_name, qname) case name when "pivotField" @current_field[:items] = @current_items unless @current_items.empty? @fields << @current_field @inside_pivot_field = false when "items" @inside_items = false when "pivotTableDefinition" if @pivot_table @pivot_table[:fields] = @fields @pivot_table[:row_fields] = @row_fields @pivot_table[:col_fields] = @col_fields @pivot_table[:data_fields] = @data_fields end when "rowFields" @inside_row_fields = false when "colFields" @inside_col_fields = false when "dataFields" @inside_data_fields = false end end
# File lib/xlsxrb/ooxml/reader.rb, line 7713 def start_element(_uri, local_name, qname, attributes) name = element_name(local_name, qname) case name when "pivotTableDefinition" @pivot_table = { name: attributes["name"], cache_id: attributes["cacheId"]&.to_i } @pivot_table[:data_caption] = attributes["dataCaption"] if attributes["dataCaption"] @pivot_table[:data_on_rows] = attributes["dataOnRows"] == "1" if attributes["dataOnRows"] @pivot_table[:row_grand_totals] = attributes["rowGrandTotals"] != "0" if attributes["rowGrandTotals"] @pivot_table[:col_grand_totals] = attributes["colGrandTotals"] != "0" if attributes["colGrandTotals"] @pivot_table[:compact] = attributes["compact"] != "0" if attributes["compact"] @pivot_table[:outline] = attributes["outline"] != "0" if attributes["outline"] @pivot_table[:outline_data] = attributes["outlineData"] == "1" if attributes["outlineData"] @pivot_table[:compact_data] = attributes["compactData"] != "0" if attributes["compactData"] @pivot_table[:show_headers] = attributes["showHeaders"] != "0" if attributes["showHeaders"] @pivot_table[:show_multiple_label] = attributes["showMultipleLabel"] != "0" if attributes["showMultipleLabel"] @pivot_table[:show_data_drop_down] = attributes["showDataDropDown"] != "0" if attributes["showDataDropDown"] @pivot_table[:grand_total_caption] = attributes["grandTotalCaption"] if attributes["grandTotalCaption"] @pivot_table[:error_caption] = attributes["errorCaption"] if attributes["errorCaption"] @pivot_table[:show_error] = attributes["showError"] == "1" if attributes["showError"] @pivot_table[:missing_caption] = attributes["missingCaption"] if attributes["missingCaption"] @pivot_table[:show_missing] = attributes["showMissing"] != "0" if attributes["showMissing"] @pivot_table[:tag] = attributes["tag"] if attributes["tag"] @pivot_table[:indent] = attributes["indent"]&.to_i if attributes["indent"] @pivot_table[:published] = attributes["published"] == "1" if attributes["published"] @pivot_table[:edit_data] = attributes["editData"] == "1" if attributes["editData"] @pivot_table[:disable_field_list] = attributes["disableFieldList"] == "1" if attributes["disableFieldList"] @pivot_table[:visual_totals] = attributes["visualTotals"] != "0" if attributes["visualTotals"] @pivot_table[:print_drill] = attributes["printDrill"] == "1" if attributes["printDrill"] @pivot_table[:created_version] = attributes["createdVersion"]&.to_i if attributes["createdVersion"] @pivot_table[:updated_version] = attributes["updatedVersion"]&.to_i if attributes["updatedVersion"] @pivot_table[:min_refreshable_version] = attributes["minRefreshableVersion"]&.to_i if attributes["minRefreshableVersion"] %w[applyNumberFormats applyBorderFormats applyFontFormats applyPatternFormats applyAlignmentFormats applyWidthHeightFormats].each do |attr| next if attributes[attr].nil? key = attr.gsub(/[A-Z]/) { |m| "_#{m.downcase}" }.to_sym @pivot_table[key] = %w[1 true].include?(attributes[attr]) end mff = attributes["multipleFieldFilters"] @pivot_table[:multiple_field_filters] = mff != "0" unless mff.nil? sdr = attributes["showDrill"] @pivot_table[:show_drill] = sdr != "0" unless sdr.nil? sdt = attributes["showDataTips"] @pivot_table[:show_data_tips] = sdt != "0" unless sdt.nil? edr = attributes["enableDrill"] @pivot_table[:enable_drill] = edr != "0" unless edr.nil? smpt = attributes["showMemberPropertyTips"] @pivot_table[:show_member_property_tips] = smpt != "0" unless smpt.nil? ipt = attributes["itemPrintTitles"] @pivot_table[:item_print_titles] = ipt == "1" unless ipt.nil? fpt = attributes["fieldPrintTitles"] @pivot_table[:field_print_titles] = fpt == "1" unless fpt.nil? pf = attributes["preserveFormatting"] @pivot_table[:preserve_formatting] = pf != "0" unless pf.nil? potd = attributes["pageOverThenDown"] @pivot_table[:page_over_then_down] = potd == "1" unless potd.nil? pw = attributes["pageWrap"] @pivot_table[:page_wrap] = pw.to_i if pw when "location" @pivot_table[:ref] = attributes["ref"] if @pivot_table @pivot_table[:row_page_count] = attributes["rowPageCount"]&.to_i if attributes["rowPageCount"] @pivot_table[:col_page_count] = attributes["colPageCount"]&.to_i if attributes["colPageCount"] when "pivotField" @inside_pivot_field = true @current_field = {} @current_field[:axis] = attributes["axis"] if attributes["axis"] @current_field[:data_field] = true if attributes["dataField"] == "1" @current_field[:name] = attributes["name"] if attributes["name"] @current_field[:show_all] = attributes["showAll"] != "0" if attributes["showAll"] @current_field[:compact] = attributes["compact"] != "0" if attributes["compact"] @current_field[:outline] = attributes["outline"] != "0" if attributes["outline"] @current_field[:subtotal_top] = attributes["subtotalTop"] != "0" if attributes["subtotalTop"] @current_field[:num_fmt_id] = attributes["numFmtId"]&.to_i if attributes["numFmtId"] @current_field[:sort_type] = attributes["sortType"] if attributes["sortType"] ds = attributes["defaultSubtotal"] @current_field[:default_subtotal] = ds != "0" unless ds.nil? @current_field[:insert_blank_row] = true if attributes["insertBlankRow"] == "1" @current_field[:insert_page_break] = true if attributes["insertPageBreak"] == "1" @current_field[:include_new_items_in_filter] = true if attributes["includeNewItemsInFilter"] == "1" @current_items = [] when "items" @inside_items = true if @inside_pivot_field when "item" if @inside_items item_type = attributes["t"] item_x = attributes["x"]&.to_i @current_items << { x: item_x, t: item_type } if item_type || item_x end when "rowFields" @inside_row_fields = true when "colFields" @inside_col_fields = true when "dataFields" @inside_data_fields = true when "field" idx = attributes["x"]&.to_i @row_fields << idx if @inside_row_fields && idx @col_fields << idx if @inside_col_fields && idx when "dataField" if @inside_data_fields df = { name: attributes["name"], fld: attributes["fld"]&.to_i, subtotal: attributes["subtotal"] || "sum" } df[:show_data_as] = attributes["showDataAs"] if attributes["showDataAs"] df[:base_field] = attributes["baseField"]&.to_i if attributes["baseField"] df[:base_item] = attributes["baseItem"]&.to_i if attributes["baseItem"] df[:num_fmt_id] = attributes["numFmtId"]&.to_i if attributes["numFmtId"] @data_fields << df end when "pivotTableStyleInfo" if @pivot_table psi = {} psi[:name] = attributes["name"] if attributes["name"] srh = attributes["showRowHeaders"] psi[:show_row_headers] = srh == "1" unless srh.nil? sch = attributes["showColHeaders"] psi[:show_col_headers] = sch == "1" unless sch.nil? srs = attributes["showRowStripes"] psi[:show_row_stripes] = srs == "1" unless srs.nil? scs = attributes["showColStripes"] psi[:show_col_stripes] = scs == "1" unless scs.nil? slc = attributes["showLastColumn"] psi[:show_last_column] = slc == "1" unless slc.nil? @pivot_table[:pivot_table_style] = psi end end end