Autofilter expressions - TopTen, and minor patches

This commit is contained in:
Mark Baker 2012-08-24 23:38:18 +01:00
parent ef3ea158e4
commit e419b92765
4 changed files with 55 additions and 24 deletions

View File

@ -426,12 +426,6 @@ class PHPExcel_Worksheet_AutoFilter
return FALSE; return FALSE;
} }
private static function _filterTypeTopTenFilters($cellValue,$testSet)
{
echo 'CALLING _filterTypeTopTenFilters',PHP_EOL;
return TRUE;
}
/** /**
* Search/Replace arrays to convert Excel wildcard syntax to a regexp syntax for preg_matching * Search/Replace arrays to convert Excel wildcard syntax to a regexp syntax for preg_matching
* *
@ -448,7 +442,7 @@ class PHPExcel_Worksheet_AutoFilter
* @param PHPExcel_Worksheet_AutoFilter_Column $filterColumn * @param PHPExcel_Worksheet_AutoFilter_Column $filterColumn
* @return mixed[] * @return mixed[]
*/ */
private function _dynamicFilterDateRange($dynamicRuleType, $filterColumn) private function _dynamicFilterDateRange($dynamicRuleType, &$filterColumn)
{ {
$rDateType = PHPExcel_Calculation_Functions::getReturnDateType(); $rDateType = PHPExcel_Calculation_Functions::getReturnDateType();
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC); PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
@ -541,7 +535,7 @@ class PHPExcel_Worksheet_AutoFilter
// Set the filter column rule attributes ready for writing // Set the filter column rule attributes ready for writing
$filterColumn->setAttributes(array( 'val' => $val, $filterColumn->setAttributes(array( 'val' => $val,
'maxVal', $maxVal 'maxVal' => $maxVal
) )
); );
@ -562,6 +556,22 @@ class PHPExcel_Worksheet_AutoFilter
); );
} }
private function _calculateTopTenValue($columnID,$startRow,$endRow,$ruleType,$ruleValue) {
$range = $columnID.$startRow.':'.$columnID.$endRow;
$dataValues = PHPExcel_Calculation_Functions::flattenArray(
$this->_workSheet->rangeToArray($range,NULL,TRUE,FALSE)
);
$dataValues = array_filter($dataValues);
if ($ruleType == PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP) {
rsort($dataValues);
} else {
sort($dataValues);
}
return array_pop(array_slice($dataValues,0,$ruleValue));
}
/** /**
* Apply the AutoFilter rules to the AutoFilter Range * Apply the AutoFilter rules to the AutoFilter Range
* *
@ -573,7 +583,7 @@ class PHPExcel_Worksheet_AutoFilter
list($rangeStart,$rangeEnd) = PHPExcel_Cell::rangeBoundaries($this->_range); list($rangeStart,$rangeEnd) = PHPExcel_Cell::rangeBoundaries($this->_range);
// The heading row should always be visible // The heading row should always be visible
echo 'AutoFilter Heading Row ',$rangeStart[1],' is always SHOWN',PHP_EOL; // echo 'AutoFilter Heading Row ',$rangeStart[1],' is always SHOWN',PHP_EOL;
$this->_workSheet->getRowDimension($rangeStart[1])->setVisible(TRUE); $this->_workSheet->getRowDimension($rangeStart[1])->setVisible(TRUE);
$columnFilterTests = array(); $columnFilterTests = array();
@ -717,32 +727,49 @@ class PHPExcel_Worksheet_AutoFilter
$ruleValues = array(); $ruleValues = array();
$dataRowCount = $rangeEnd[1] - $rangeStart[1]; $dataRowCount = $rangeEnd[1] - $rangeStart[1];
foreach($rules as $rule) { foreach($rules as $rule) {
var_dump($rule);
// We should only ever have one Dynamic Filter Rule anyway // We should only ever have one Dynamic Filter Rule anyway
$toptenRuleType = $rule->getGrouping(); $toptenRuleType = $rule->getGrouping();
$ruleValue = $rule->getValue(); $ruleValue = $rule->getValue();
$ruleOperator = $rule->getOperator();
} }
var_dump($toptenRuleType); if ($ruleOperator === PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT) {
var_dump($ruleValue); $ruleValue = floor($ruleValue * ($dataRowCount / 100));
}
if ($ruleValue < 1) $ruleValue = 1;
if ($ruleValue > 500) $ruleValue = 500;
$maxVal = $this->_calculateTopTenValue($columnID,$rangeStart[1]+1,$rangeEnd[1],$toptenRuleType,$ruleValue);
$operator = ($toptenRuleType == PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP)
? PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL
: PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL;
$ruleValues[] = array( 'operator' => $operator,
'value' => $maxVal
);
$columnFilterTests[$columnID] = array( $columnFilterTests[$columnID] = array(
'method' => '_filterTypeTopTenFilters', 'method' => '_filterTestInCustomDataSet',
'arguments' => $ruleValues 'arguments' => array( 'filterRules' => $ruleValues,
'join' => PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_OR
)
);
$filterColumn->setAttributes(
array('maxVal' => $maxVal)
); );
break; break;
} }
} }
echo 'Column Filter Test CRITERIA',PHP_EOL; // echo 'Column Filter Test CRITERIA',PHP_EOL;
var_dump($columnFilterTests); // var_dump($columnFilterTests);
//
// Execute the column tests for each row in the autoFilter range to determine show/hide, // Execute the column tests for each row in the autoFilter range to determine show/hide,
for ($row = $rangeStart[1]+1; $row <= $rangeEnd[1]; ++$row) { for ($row = $rangeStart[1]+1; $row <= $rangeEnd[1]; ++$row) {
echo 'Testing Row = ',$row,PHP_EOL; // echo 'Testing Row = ',$row,PHP_EOL;
$result = TRUE; $result = TRUE;
foreach($columnFilterTests as $columnID => $columnFilterTest) { foreach($columnFilterTests as $columnID => $columnFilterTest) {
echo 'Testing cell ',$columnID.$row,PHP_EOL; // echo 'Testing cell ',$columnID.$row,PHP_EOL;
$cellValue = $this->_workSheet->getCell($columnID.$row)->getCalculatedValue(); $cellValue = $this->_workSheet->getCell($columnID.$row)->getCalculatedValue();
echo 'Value is ',$cellValue,PHP_EOL; // echo 'Value is ',$cellValue,PHP_EOL;
// Execute the filter test // Execute the filter test
$result = $result && $result = $result &&
call_user_func_array( call_user_func_array(
@ -752,13 +779,13 @@ var_dump($ruleValue);
$columnFilterTest['arguments'] $columnFilterTest['arguments']
) )
); );
echo (($result) ? 'VALID' : 'INVALID'),PHP_EOL; // echo (($result) ? 'VALID' : 'INVALID'),PHP_EOL;
// If filter test has resulted in FALSE, exit the loop straightaway rather than running any more tests // If filter test has resulted in FALSE, exit the loop straightaway rather than running any more tests
if (!$result) if (!$result)
break; break;
} }
// Set show/hide for the row based on the result of the autoFilter result // Set show/hide for the row based on the result of the autoFilter result
echo (($result) ? 'SHOW' : 'HIDE'),PHP_EOL; // echo (($result) ? 'SHOW' : 'HIDE'),PHP_EOL;
$this->_workSheet->getRowDimension($row)->setVisible($result); $this->_workSheet->getRowDimension($row)->setVisible($result);
} }

View File

@ -197,7 +197,7 @@ class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_Write
$objWriter->writeAttribute('calcId', '124519'); $objWriter->writeAttribute('calcId', '124519');
$objWriter->writeAttribute('calcMode', 'auto'); $objWriter->writeAttribute('calcMode', 'auto');
// fullCalcOnLoad isn't needed if we've recalculating for the save // fullCalcOnLoad isn't needed if we've recalculating for the save
$objWriter->writeAttribute('fullCalcOnLoad', ($recalcRequired) ? '1' : '0'); $objWriter->writeAttribute('fullCalcOnLoad', ($recalcRequired) ? '0' : '1');
$objWriter->endElement(); $objWriter->endElement();
} }

View File

@ -151,7 +151,7 @@ class PHPExcel_Writer_Excel2007_Worksheet extends PHPExcel_Writer_Excel2007_Writ
$autoFilterRange = $pSheet->getAutoFilter()->getRange(); $autoFilterRange = $pSheet->getAutoFilter()->getRange();
if (!empty($autoFilterRange)) { if (!empty($autoFilterRange)) {
$objWriter->writeAttribute('filterMode', 1); $objWriter->writeAttribute('filterMode', 1);
// $pSheet->getAutoFilter()->showHideRows(); $pSheet->getAutoFilter()->showHideRows();
} }
// tabColor // tabColor

View File

@ -28,6 +28,10 @@
/** Error reporting */ /** Error reporting */
error_reporting(E_ALL); error_reporting(E_ALL);
if (PHP_SAPI != 'cli') {
die ('This script executes all tests, and should only be run from the command line');
}
// List of tests // List of tests
$aTests = array( $aTests = array(
'01simple.php' '01simple.php'