Skip to content

Commit

Permalink
Completed first run of auto_filter filter_columns and value based fil…
Browse files Browse the repository at this point in the history
…ters.

This is not ready for release - needs more testing and docs!
  • Loading branch information
randym committed Sep 27, 2012
1 parent 8ba1ebe commit f8177db
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/axlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def self.coder
# returns the x, y position of a cell
def self.name_to_indices(name)
raise ArgumentError, 'invalid cell name' unless name.size > 1
# capitalization?!?
v = name[/[A-Z]+/].reverse.chars.reduce({:base=>1, :i=>0}) do |val, c|
val[:i] += ((c.bytes.first - 64) * val[:base]); val[:base] *= 26; val
end
Expand Down
1 change: 1 addition & 0 deletions lib/axlsx/workbook/workbook.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
module Axlsx
require 'axlsx/workbook/worksheet/sheet_calc_pr.rb'
require 'axlsx/workbook/worksheet/auto_filter/auto_filter.rb'
require 'axlsx/workbook/worksheet/date_time_converter.rb'
require 'axlsx/workbook/worksheet/protected_range.rb'
Expand Down
16 changes: 16 additions & 0 deletions lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ def add_column(col_id, filter_type, options = {})
columns.last
end

# actually performs the filtering of rows who's cells do not
# match the filter.
def apply
first_cell, last_cell = range.split(':')
start_point = Axlsx::name_to_indices(first_cell)
end_point = Axlsx::name_to_indices(last_cell)
# The +1 is so we skip the header row with the filter drop downs
rows = worksheet.rows[(start_point.last+1)..end_point.last]
column_offset = start_point.first
columns.each do |column|
rows.each do |row|
next if row.hidden
column.apply(row, column_offset)
end
end
end
# serialize the object
# @return [String]
def to_xml_string(str='')
Expand Down
6 changes: 6 additions & 0 deletions lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def col_id=(column_index)
@col_id = column_index
end

# Apply the filters for this column
# @param [Array] row A row from a worksheet that needs to be
# filtered.
def apply(row, offset)
row.hidden = @filter.apply(row.cells[offset+col_id.to_i])
end
# @param [Boolean] hidden Flag indicating whether the AutoFilter button for this column is hidden.
# @return [Boolean]
def hidden_button=(hidden)
Expand Down
13 changes: 13 additions & 0 deletions lib/axlsx/workbook/worksheet/auto_filter/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ def initialize(options={})
# even when those dates are not using the same calendar system / date formatting.
attr_reader :calendar_type

# Tells us if the row of the cell provided should be filterd as it
# does not meet any of the specified filter_items or
# date_group_items restrictions.
# @param [Cell] cell The cell to test against items
# TODO implement this for date filters as well!
def apply(cell)
return false unless cell
filter_items.each do |filter|
return false if cell.value == filter.val
end
true
end

# The filter values in this filters object
def filter_items
@filter_items ||= []
Expand Down
13 changes: 13 additions & 0 deletions lib/axlsx/workbook/worksheet/sheet_calc_pr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Axlsx

class SheetCalcPr

def initialize

end

def to_xml_string(str='')
str << '<sheetCalcPr fullCalcOnLoad="1"/>'
end
end
end
2 changes: 1 addition & 1 deletion lib/axlsx/workbook/worksheet/sheet_pr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def update_properties
page_setup_pr.fit_to_page = worksheet.fit_to_page?
if worksheet.auto_filter.columns.size > 0
self.filter_mode = 1
self.enable_format_conditions_calculation = 0
self.enable_format_conditions_calculation = 1
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/axlsx/workbook/worksheet/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def name
@name ||= "Sheet" + (index+1).to_s
end

def sheet_calc_pr
@sheet_calc_pr ||= SheetCalcPr.new
end
# The sheet protection object for this workbook
# @return [SheetProtection]
# @see SheetProtection
Expand Down Expand Up @@ -491,6 +494,7 @@ def row_style(index, style, options={})
# This intentionally does not use nokogiri for performance reasons
# @return [String]
def to_xml_string
auto_filter.apply
str = '<?xml version="1.0" encoding="UTF-8"?>'
str << worksheet_node
serializable_parts.each do |item|
Expand Down Expand Up @@ -576,7 +580,7 @@ def validate_sheet_name(name)

def serializable_parts
[sheet_pr, dimension, sheet_view, column_info,
sheet_data, @sheet_protection, protected_ranges,
sheet_data, sheet_calc_pr, @sheet_protection, protected_ranges,
auto_filter, merged_cells, conditional_formattings,
data_validations, hyperlinks, print_options, page_margins,
page_setup, worksheet_drawing, worksheet_comments,
Expand Down

0 comments on commit f8177db

Please sign in to comment.