From 1725c622f3c2cf2b08271ca3025bbec0bcf27f0c Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Tue, 27 Mar 2012 20:34:00 +0000 Subject: [PATCH] Formula examples git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@88518 2327b42d-5241-43d6-9e2a-de5ac946f064 --- .../Calculations/Database/DAVERAGE.php | 15 +-- .../Examples/Calculations/Database/DCOUNT.php | 91 +++++++++++++++++++ .../Examples/Calculations/DateTime/DATE.php | 84 +++++++++++++++++ .../Calculations/DateTime/DATEVALUE.php | 77 ++++++++++++++++ .../Calculations/DateTime/TIMEVALUE.php | 73 +++++++++++++++ 5 files changed, 334 insertions(+), 6 deletions(-) create mode 100644 Documentation/Examples/Calculations/Database/DCOUNT.php create mode 100644 Documentation/Examples/Calculations/DateTime/DATE.php create mode 100644 Documentation/Examples/Calculations/DateTime/DATEVALUE.php create mode 100644 Documentation/Examples/Calculations/DateTime/TIMEVALUE.php diff --git a/Documentation/Examples/Calculations/Database/DAVERAGE.php b/Documentation/Examples/Calculations/Database/DAVERAGE.php index b6f3597..387e1a9 100644 --- a/Documentation/Examples/Calculations/Database/DAVERAGE.php +++ b/Documentation/Examples/Calculations/Database/DAVERAGE.php @@ -11,13 +11,13 @@ date_default_timezone_set('Europe/London'); -PHPExcel Reader Example #01 +PHPExcel Calculation Examples -

PHPExcel Calculation Example

-

Database Formulae

+

PHPExcel Calculation Example

+

Database Formulae

DAVERAGE

getActiveSheet(); +// Add some data $database = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit' ), array( 'Apple', 18, 20, 14, 105.00 ), array( 'Pear', 12, 12, 10, 96.00 ), @@ -40,7 +41,7 @@ $database = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit' ), array( 'Pear', 9, 8, 8, 76.80 ), array( 'Apple', 8, 9, 6, 45.00 ), ); -$criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ), +$criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ), array( '="=Apple"', '>10', NULL, NULL, NULL, '<16' ), array( '="=Pear"', NULL, NULL, NULL, NULL, NULL ) ); @@ -48,7 +49,7 @@ $criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', ' $worksheet->fromArray( $criteria, NULL, 'A1' ); $worksheet->fromArray( $database, NULL, 'A4' ); -$worksheet->setCellValue('A12', 'The Average yield of apple trees over 10\' in height'); +$worksheet->setCellValue('A12', 'The Average yield of Apple trees over 10\' in height'); $worksheet->setCellValue('B12', '=DAVERAGE(A4:E10,"Yield",A1:B2)'); $worksheet->setCellValue('A13', 'The Average age of all Apple and Pear trees in the orchard'); @@ -66,6 +67,8 @@ var_dump($databaseData); echo '
'; + +// Test the formulae echo '

Criteria

'; $criteriaData = $worksheet->rangeToArray('A1:B2',null,true,true,true); diff --git a/Documentation/Examples/Calculations/Database/DCOUNT.php b/Documentation/Examples/Calculations/Database/DCOUNT.php new file mode 100644 index 0000000..bc2a1df --- /dev/null +++ b/Documentation/Examples/Calculations/Database/DCOUNT.php @@ -0,0 +1,91 @@ + + + + + +PHPExcel Calculation Examples + + + + +

PHPExcel Calculation Example

+

Database Formulae

+

DCOUNT

+getActiveSheet(); + +// Add some data +$database = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit' ), + array( 'Apple', 18, 20, 14, 105.00 ), + array( 'Pear', 12, 12, 10, 96.00 ), + array( 'Cherry', 13, 14, 9, 105.00 ), + array( 'Apple', 14, 15, 10, 75.00 ), + array( 'Pear', 9, 8, 8, 76.80 ), + array( 'Apple', 8, 9, 6, 45.00 ), + ); +$criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ), + array( '="=Apple"', '>10', NULL, NULL, NULL, '<16' ), + array( '="=Pear"', NULL, NULL, NULL, NULL, NULL ) + ); + +$worksheet->fromArray( $criteria, NULL, 'A1' ); +$worksheet->fromArray( $database, NULL, 'A4' ); + +$worksheet->setCellValue('A12', 'The Number of Apple trees over 10\' in height'); +$worksheet->setCellValue('B12', '=DCOUNT(A4:E10,"Yield",A1:B2)'); + +$worksheet->setCellValue('A13', 'The Number of Apple and Pear trees in the orchard'); +$worksheet->setCellValue('B13', '=DCOUNT(A4:E10,3,A1:A3)'); + + +echo '
'; + + +echo '

Database

'; + +$databaseData = $worksheet->rangeToArray('A4:E10',null,true,true,true); +var_dump($databaseData); + + +echo '
'; + + +// Test the formulae +echo '

Criteria

'; + +$criteriaData = $worksheet->rangeToArray('A1:B2',null,true,true,true); +var_dump($criteriaData); + +echo $worksheet->getCell("A12")->getValue() .'
'; +echo 'DCOUNT() Result is ' . $worksheet->getCell("B12")->getCalculatedValue() .'

'; + +echo '

Criteria

'; + +$criteriaData = $worksheet->rangeToArray('A1:A3',null,true,true,true); +var_dump($criteriaData); + +echo $worksheet->getCell("A13")->getValue() .'
'; +echo 'DCOUNT() Result is ' . $worksheet->getCell("B13")->getCalculatedValue(); + + +?> + + \ No newline at end of file diff --git a/Documentation/Examples/Calculations/DateTime/DATE.php b/Documentation/Examples/Calculations/DateTime/DATE.php new file mode 100644 index 0000000..431b267 --- /dev/null +++ b/Documentation/Examples/Calculations/DateTime/DATE.php @@ -0,0 +1,84 @@ + + + + + +PHPExcel Calculation Examples + + + + +

PHPExcel Calculation Example

+

Data/Time Formulae

+

DATE

+getActiveSheet(); + +// Add some data +$testDates = array( array(2012,3,26), array(2012,2,29), array(2012,4,1), array(2012,12,25), + array(2012,10,31), array(2012,11,5), array(2012,1,1), array(2012,3,17), + array(2011,2,29), array(7,5,3), array(2012,13,1), array(2012,11,45), + array(2012,0,0), array(2012,1,0), array(2012,0,1), + array(2012,-2,2), array(2012,2,-2), array(2012,-2,-2), + ); +$testDateCount = count($testDates); + +$worksheet->fromArray($testDates,NULL,'A1',true); + +for ($row = 1; $row <= $testDateCount; ++$row) { + $worksheet->setCellValue('D'.$row, '=DATE(A'.$row.',B'.$row.',C'.$row.')'); + $worksheet->setCellValue('E'.$row, '=D'.$row); +} +$worksheet->getStyle('E1:E'.$testDateCount) + ->getNumberFormat() + ->setFormatCode('yyyy-mmm-dd'); + + +echo '
'; + + +// Test the formulae +?> + + + + + + + + + + + + + '; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + } + ?> +
Date ValueFormulaExcel DateStampFormatted DateStamp
YearMonthDay
' , $worksheet->getCell('A'.$row)->getFormattedValue() , '' , $worksheet->getCell('B'.$row)->getFormattedValue() , '' , $worksheet->getCell('C'.$row)->getFormattedValue() , '' , $worksheet->getCell('D'.$row)->getValue() , '' , $worksheet->getCell('D'.$row)->getFormattedValue() , '' , $worksheet->getCell('E'.$row)->getFormattedValue() , '
\ No newline at end of file diff --git a/Documentation/Examples/Calculations/DateTime/DATEVALUE.php b/Documentation/Examples/Calculations/DateTime/DATEVALUE.php new file mode 100644 index 0000000..3af84ab --- /dev/null +++ b/Documentation/Examples/Calculations/DateTime/DATEVALUE.php @@ -0,0 +1,77 @@ + + + + + +PHPExcel Calculation Examples + + + + +

PHPExcel Calculation Example

+

Data/Time Formulae

+

DATEVALUE

+getActiveSheet(); + +// Add some data +$testDates = array( '26 March 2012', '29 Feb 2012', 'April 1, 2012', '25/12/2012', + '2012-Oct-31', '5th November', 'January 1st', 'April 2012', + '17-03', '03-2012', '29 Feb 2011', '03-05-07', + '03-MAY-07', '03-13-07', + ); +$testDateCount = count($testDates); + +for($row = 1; $row <= $testDateCount; ++$row) { + $worksheet->setCellValue('A'.$row, $testDates[$row-1]); + $worksheet->setCellValue('B'.$row, '=DATEVALUE(A'.$row.')'); + $worksheet->setCellValue('C'.$row, '=B'.$row); +} + +$worksheet->getStyle('C1:C'.$testDateCount) + ->getNumberFormat() + ->setFormatCode('yyyy-mmm-dd'); + + +echo '
'; + + +// Test the formulae +?> +

Warning: The PHPExcel DATEVALUE() function accepts a wider range of date formats than MS Excel's DATEFORMAT() function.

+ + + + + + + + '; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + } + ?> +
Date StringFormulaExcel DateStampFormatted DateStamp
' , $worksheet->getCell('A'.$row)->getFormattedValue() , '' , $worksheet->getCell('B'.$row)->getValue() , '' , $worksheet->getCell('B'.$row)->getFormattedValue() , '' , $worksheet->getCell('C'.$row)->getFormattedValue() , '
\ No newline at end of file diff --git a/Documentation/Examples/Calculations/DateTime/TIMEVALUE.php b/Documentation/Examples/Calculations/DateTime/TIMEVALUE.php new file mode 100644 index 0000000..d7ed2cd --- /dev/null +++ b/Documentation/Examples/Calculations/DateTime/TIMEVALUE.php @@ -0,0 +1,73 @@ + + + + + +PHPExcel Calculation Examples + + + + +

PHPExcel Calculation Example

+

Data/Time Formulae

+

TIMEVALUE

+getActiveSheet(); + +// Add some data +$testDates = array( '3:15', '13:15', '15:15:15', '3:15 AM', '3:15 PM', '5PM', '9:15AM', '13:15AM' + ); +$testDateCount = count($testDates); + +for($row = 1; $row <= $testDateCount; ++$row) { + $worksheet->setCellValue('A'.$row, $testDates[$row-1]); + $worksheet->setCellValue('B'.$row, '=TIMEVALUE(A'.$row.')'); + $worksheet->setCellValue('C'.$row, '=B'.$row); +} + +$worksheet->getStyle('C1:C'.$testDateCount) + ->getNumberFormat() + ->setFormatCode('hh:mm:ss'); + + +echo '
'; + + +// Test the formulae +?> + + + + + + + + '; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + } + ?> +
Time StringFormulaExcel TimeStampFormatted TimeStamp
' , $worksheet->getCell('A'.$row)->getFormattedValue() , '' , $worksheet->getCell('B'.$row)->getValue() , '' , $worksheet->getCell('B'.$row)->getFormattedValue() , '' , $worksheet->getCell('C'.$row)->getFormattedValue() , '
\ No newline at end of file