Discard Autofilters in Excel2007 Reader when filter range isn't a valid range

This commit is contained in:
MarkBaker 2014-11-11 23:18:13 +00:00
parent 37b4d18d45
commit fdefb8e392
3 changed files with 107 additions and 104 deletions

View File

@ -1006,108 +1006,106 @@ class PHPExcel_Reader_Excel2007 extends PHPExcel_Reader_Abstract implements PHPE
} }
if ($xmlSheet && $xmlSheet->autoFilter && !$this->_readDataOnly) { if ($xmlSheet && $xmlSheet->autoFilter && !$this->_readDataOnly) {
$autoFilter = $docSheet->getAutoFilter();
$autoFilterRange = (string) $xmlSheet->autoFilter["ref"]; $autoFilterRange = (string) $xmlSheet->autoFilter["ref"];
if (strpos($autoFilterRange, ':') === false) { if (strpos($autoFilterRange, ':') !== false) {
$autoFilterRange = "${autoFilterRange}:${autoFilterRange}"; $autoFilter = $docSheet->getAutoFilter();
} $autoFilter->setRange($autoFilterRange);
$autoFilter->setRange($autoFilterRange);
foreach ($xmlSheet->autoFilter->filterColumn as $filterColumn) { foreach ($xmlSheet->autoFilter->filterColumn as $filterColumn) {
$column = $autoFilter->getColumnByOffset((integer) $filterColumn["colId"]); $column = $autoFilter->getColumnByOffset((integer) $filterColumn["colId"]);
// Check for standard filters // Check for standard filters
if ($filterColumn->filters) { if ($filterColumn->filters) {
$column->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_FILTER); $column->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_FILTER);
$filters = $filterColumn->filters; $filters = $filterColumn->filters;
if ((isset($filters["blank"])) && ($filters["blank"] == 1)) { if ((isset($filters["blank"])) && ($filters["blank"] == 1)) {
$column->createRule()->setRule( $column->createRule()->setRule(
NULL, // Operator is undefined, but always treated as EQUAL NULL, // Operator is undefined, but always treated as EQUAL
'' ''
) )
->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_FILTER); ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_FILTER);
} }
// Standard filters are always an OR join, so no join rule needs to be set // Standard filters are always an OR join, so no join rule needs to be set
// Entries can be either filter elements // Entries can be either filter elements
foreach ($filters->filter as $filterRule) { foreach ($filters->filter as $filterRule) {
$column->createRule()->setRule( $column->createRule()->setRule(
NULL, // Operator is undefined, but always treated as EQUAL NULL, // Operator is undefined, but always treated as EQUAL
(string) $filterRule["val"] (string) $filterRule["val"]
) )
->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_FILTER); ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_FILTER);
} }
// Or Date Group elements // Or Date Group elements
foreach ($filters->dateGroupItem as $dateGroupItem) { foreach ($filters->dateGroupItem as $dateGroupItem) {
$column->createRule()->setRule( $column->createRule()->setRule(
NULL, // Operator is undefined, but always treated as EQUAL NULL, // Operator is undefined, but always treated as EQUAL
array( array(
'year' => (string) $dateGroupItem["year"], 'year' => (string) $dateGroupItem["year"],
'month' => (string) $dateGroupItem["month"], 'month' => (string) $dateGroupItem["month"],
'day' => (string) $dateGroupItem["day"], 'day' => (string) $dateGroupItem["day"],
'hour' => (string) $dateGroupItem["hour"], 'hour' => (string) $dateGroupItem["hour"],
'minute' => (string) $dateGroupItem["minute"], 'minute' => (string) $dateGroupItem["minute"],
'second' => (string) $dateGroupItem["second"], 'second' => (string) $dateGroupItem["second"],
), ),
(string) $dateGroupItem["dateTimeGrouping"] (string) $dateGroupItem["dateTimeGrouping"]
) )
->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DATEGROUP); ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DATEGROUP);
} }
} }
// Check for custom filters // Check for custom filters
if ($filterColumn->customFilters) { if ($filterColumn->customFilters) {
$column->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $column->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER);
$customFilters = $filterColumn->customFilters; $customFilters = $filterColumn->customFilters;
// Custom filters can an AND or an OR join; // Custom filters can an AND or an OR join;
// and there should only ever be one or two entries // and there should only ever be one or two entries
if ((isset($customFilters["and"])) && ($customFilters["and"] == 1)) { if ((isset($customFilters["and"])) && ($customFilters["and"] == 1)) {
$column->setJoin(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_AND); $column->setJoin(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_AND);
} }
foreach ($customFilters->customFilter as $filterRule) { foreach ($customFilters->customFilter as $filterRule) {
$column->createRule()->setRule( $column->createRule()->setRule(
(string) $filterRule["operator"], (string) $filterRule["operator"],
(string) $filterRule["val"] (string) $filterRule["val"]
) )
->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER);
} }
} }
// Check for dynamic filters // Check for dynamic filters
if ($filterColumn->dynamicFilter) { if ($filterColumn->dynamicFilter) {
$column->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $column->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER);
// We should only ever have one dynamic filter // We should only ever have one dynamic filter
foreach ($filterColumn->dynamicFilter as $filterRule) { foreach ($filterColumn->dynamicFilter as $filterRule) {
$column->createRule()->setRule( $column->createRule()->setRule(
NULL, // Operator is undefined, but always treated as EQUAL NULL, // Operator is undefined, but always treated as EQUAL
(string) $filterRule["val"], (string) $filterRule["val"],
(string) $filterRule["type"] (string) $filterRule["type"]
) )
->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER);
if (isset($filterRule["val"])) { if (isset($filterRule["val"])) {
$column->setAttribute('val',(string) $filterRule["val"]); $column->setAttribute('val',(string) $filterRule["val"]);
} }
if (isset($filterRule["maxVal"])) { if (isset($filterRule["maxVal"])) {
$column->setAttribute('maxVal',(string) $filterRule["maxVal"]); $column->setAttribute('maxVal',(string) $filterRule["maxVal"]);
} }
} }
} }
// Check for dynamic filters // Check for dynamic filters
if ($filterColumn->top10) { if ($filterColumn->top10) {
$column->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_TOPTENFILTER); $column->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_TOPTENFILTER);
// We should only ever have one top10 filter // We should only ever have one top10 filter
foreach ($filterColumn->top10 as $filterRule) { foreach ($filterColumn->top10 as $filterRule) {
$column->createRule()->setRule( $column->createRule()->setRule(
(((isset($filterRule["percent"])) && ($filterRule["percent"] == 1)) (((isset($filterRule["percent"])) && ($filterRule["percent"] == 1))
? PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT ? PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT
: PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE : PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE
), ),
(string) $filterRule["val"], (string) $filterRule["val"],
(((isset($filterRule["top"])) && ($filterRule["top"] == 1)) (((isset($filterRule["top"])) && ($filterRule["top"] == 1))
? PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP ? PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP
: PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM : PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM
) )
) )
->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_TOPTENFILTER); ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_TOPTENFILTER);
} }
} }
}
} }
} }
@ -1584,10 +1582,9 @@ class PHPExcel_Reader_Excel2007 extends PHPExcel_Reader_Abstract implements PHPE
$extractedRange = explode(',', $extractedRange); $extractedRange = explode(',', $extractedRange);
foreach ($extractedRange as $range) { foreach ($extractedRange as $range) {
$autoFilterRange = $range; $autoFilterRange = $range;
if (strpos($autoFilterRange, ':') === false) { if (strpos($autoFilterRange, ':') !== false) {
$autoFilterRange = "${autoFilterRange}:${autoFilterRange}"; $docSheet->getAutoFilter()->setRange($autoFilterRange);
} }
$docSheet->getAutoFilter()->setRange($autoFilterRange);
} }
} }
break; break;

View File

@ -633,7 +633,11 @@ class PHPExcel_Worksheet_AutoFilter
); );
} else { } else {
// Filter on date group values // Filter on date group values
$arguments = array(); $arguments = array(
'date' => array(),
'time' => array(),
'dateTime' => array(),
);
foreach($ruleDataSet as $ruleValue) { foreach($ruleDataSet as $ruleValue) {
$date = $time = ''; $date = $time = '';
if ((isset($ruleValue[PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DATEGROUP_YEAR])) && if ((isset($ruleValue[PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DATEGROUP_YEAR])) &&

View File

@ -33,10 +33,12 @@ Planned for v1.8.1
- Bugfix: (MBaker) Work Item GH-384 - DOM loadHTMLFile() failing with options flags when using PHP < 5.4.0 - Bugfix: (MBaker) Work Item GH-384 - DOM loadHTMLFile() failing with options flags when using PHP < 5.4.0
- Bugfix: (MBaker) - Fix for percentage operator in formulae for BIFF Writer - Bugfix: (MBaker) - Fix for percentage operator in formulae for BIFF Writer
- Bugfix: (MBaker) - Fix to getStyle() call for cell object - Bugfix: (MBaker) - Fix to getStyle() call for cell object
- Bugfix: (MBaker) - Discard Autofilters in Excel2007 Reader when filter range isn't a valid range
- Bugfix: (masanaikeshima) Work Item GH-426 - Pie chart won't work in Excel 2013 - Bugfix: (masanaikeshima) Work Item GH-426 - Pie chart won't work in Excel 2013
- General: (MBaker) - Small performance improvement for autosize columns - General: (MBaker) - Small performance improvement for autosize columns
- General: (frost-nzcr4) Work Item GH-379 - Change the getter/setter for zeroHeight to camel case - General: (frost-nzcr4) Work Item GH-379 - Change the getter/setter for zeroHeight to camel case
- General: (MBaker) Work Item GH-394 - DefaultValueBinder is too much aggressive when converting string to numeric - General: (MBaker) Work Item GH-394 - DefaultValueBinder is too much aggressive when converting string to numeric
- General: (MBaker) - Default precalculate formulas to false for writers
- Feature: (WiktrzGE) Work Item GH-404 - Methods to manage most of the existing options for Chart Axis, Major Grid-lines and Minor Grid-lines - Feature: (WiktrzGE) Work Item GH-404 - Methods to manage most of the existing options for Chart Axis, Major Grid-lines and Minor Grid-lines
- Feature: (frost-nzcr4) Work Item GH-403 - ODS read/write comments in the cell - Feature: (frost-nzcr4) Work Item GH-403 - ODS read/write comments in the cell
- Feature: (CQD) Work Item GH-389 - Additional Mac CJK codepage definitions - Feature: (CQD) Work Item GH-389 - Additional Mac CJK codepage definitions