summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-09-27 19:22:44 +0900
committerRandy Morgan <[email protected]>2012-09-27 19:22:44 +0900
commitf8177db2c790d7a2e832d0ddf80f7cc1cde677a5 (patch)
tree1b4edc2f30ffb7b043ce32796597d6357155eb14 /lib
parent8ba1ebe7903e54a142f14c95af2e4e283e5aefc9 (diff)
downloadcaxlsx-f8177db2c790d7a2e832d0ddf80f7cc1cde677a5.tar.gz
caxlsx-f8177db2c790d7a2e832d0ddf80f7cc1cde677a5.zip
Completed first run of auto_filter filter_columns and value based filters.
This is not ready for release - needs more testing and docs!
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx.rb1
-rw-r--r--lib/axlsx/workbook/workbook.rb1
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb16
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb6
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter/filters.rb13
-rw-r--r--lib/axlsx/workbook/worksheet/sheet_calc_pr.rb13
-rw-r--r--lib/axlsx/workbook/worksheet/sheet_pr.rb2
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb6
8 files changed, 56 insertions, 2 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb
index 85a7b7c9..e0cbf4e8 100644
--- a/lib/axlsx.rb
+++ b/lib/axlsx.rb
@@ -65,6 +65,7 @@ module Axlsx
# 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
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb
index cdc73360..f587c34c 100644
--- a/lib/axlsx/workbook/workbook.rb
+++ b/lib/axlsx/workbook/workbook.rb
@@ -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'
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb b/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
index 4cf98ad7..0dd57283 100644
--- a/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
+++ b/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
@@ -47,6 +47,22 @@ module Axlsx
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='')
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb b/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb
index 6e8db16e..34ee2507 100644
--- a/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb
+++ b/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb
@@ -58,6 +58,12 @@ module Axlsx
@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)
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/filters.rb b/lib/axlsx/workbook/worksheet/auto_filter/filters.rb
index 5b46e087..49d8fce8 100644
--- a/lib/axlsx/workbook/worksheet/auto_filter/filters.rb
+++ b/lib/axlsx/workbook/worksheet/auto_filter/filters.rb
@@ -33,6 +33,19 @@ module Axlsx
# 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 ||= []
diff --git a/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb b/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb
new file mode 100644
index 00000000..ddbb8741
--- /dev/null
+++ b/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb
@@ -0,0 +1,13 @@
+module Axlsx
+
+ class SheetCalcPr
+
+ def initialize
+
+ end
+
+ def to_xml_string(str='')
+ str << '<sheetCalcPr fullCalcOnLoad="1"/>'
+ end
+ end
+end
diff --git a/lib/axlsx/workbook/worksheet/sheet_pr.rb b/lib/axlsx/workbook/worksheet/sheet_pr.rb
index 33239868..a13c906e 100644
--- a/lib/axlsx/workbook/worksheet/sheet_pr.rb
+++ b/lib/axlsx/workbook/worksheet/sheet_pr.rb
@@ -112,7 +112,7 @@ module Axlsx
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
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index e122a588..6c190470 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -43,6 +43,9 @@ module Axlsx
@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
@@ -491,6 +494,7 @@ module Axlsx
# 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|
@@ -576,7 +580,7 @@ module Axlsx
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,