Source for file Functions.php
Documentation is available at Functions.php
* Copyright (c) 2006 - 2011 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Calculation
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.6, 2011-02-27
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/../../');
require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
define('M_2DIVPI', 0.63661977236758134307553505349006);
define('MAX_ITERATIONS', 256);
define('PRECISION', 8.88E-016);
* PHPExcel_Calculation_Functions
* @package PHPExcel_Calculation
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
const COMPATIBILITY_EXCEL = 'Excel';
const COMPATIBILITY_GNUMERIC = 'Gnumeric';
const COMPATIBILITY_OPENOFFICE = 'OpenOfficeCalc';
const RETURNDATE_PHP_NUMERIC = 'P';
const RETURNDATE_PHP_OBJECT = 'O';
const RETURNDATE_EXCEL = 'E';
* Compatibility mode to use for error checking and responses
protected static $compatibilityMode = self::COMPATIBILITY_EXCEL;
* Data Type to use when returning date values
protected static $ReturnDateType = self::RETURNDATE_EXCEL;
protected static $_errorCodes = array( 'null' => '#NULL!',
'divisionbyzero' => '#DIV/0!',
'gettingdata' => '#GETTING_DATA'
* Set the Compatibility Mode
* @category Function Configuration
* @param string $compatibilityMode Compatibility Mode
* PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL 'Excel'
* PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
* PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
* @return boolean (Success or Failure)
if (($compatibilityMode == self::COMPATIBILITY_EXCEL) ||
($compatibilityMode == self::COMPATIBILITY_GNUMERIC) ||
($compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
self::$compatibilityMode = $compatibilityMode;
} // function setCompatibilityMode()
* Return the current Compatibility Mode
* @category Function Configuration
* @return string Compatibility Mode
* Possible Return values are:
* PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL 'Excel'
* PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
* PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
return self::$compatibilityMode;
} // function getCompatibilityMode()
* Set the Return Date Format used by functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object)
* @category Function Configuration
* @param string $returnDateType Return Date Format
* PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC 'P'
* PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT 'O'
* PHPExcel_Calculation_Functions::RETURNDATE_EXCEL 'E'
* @return boolean Success or failure
if (($returnDateType == self::RETURNDATE_PHP_NUMERIC) ||
($returnDateType == self::RETURNDATE_PHP_OBJECT) ||
($returnDateType == self::RETURNDATE_EXCEL)) {
self::$ReturnDateType = $returnDateType;
} // function setReturnDateType()
* Return the current Return Date Format for functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object)
* @category Function Configuration
* @return string Return Date Format
* Possible Return values are:
* PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC 'P'
* PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT 'O'
* PHPExcel_Calculation_Functions::RETURNDATE_EXCEL 'E'
return self::$ReturnDateType;
} // function getReturnDateType()
* @category Error Returns
* @return string #Not Yet Implemented
public static function DUMMY() {
return '#Not Yet Implemented';
* @category Error Returns
* @return string #Not Yet Implemented
public static function DIV0() {
return self::$_errorCodes['divisionbyzero'];
* Returns the error value #N/A
* #N/A is the error value that means "no value is available."
* @category Logical Functions
public static function NA() {
return self::$_errorCodes['na'];
* Returns the error value #NUM!
* @category Error Returns
public static function NaN() {
return self::$_errorCodes['num'];
* Returns the error value #NAME?
* @category Error Returns
public static function NAME() {
return self::$_errorCodes['name'];
* Returns the error value #REF!
* @category Error Returns
public static function REF() {
return self::$_errorCodes['reference'];
* Returns the error value #NULL!
* @category Error Returns
public static function NULL() {
return self::$_errorCodes['null'];
* Returns the error value #VALUE!
* @category Error Returns
public static function VALUE() {
return self::$_errorCodes['value'];
return ((substr_count($idx,'.') <= 1) || (preg_match('/\.[A-Z]/',$idx) > 0));
public static function isValue($idx) {
if (!in_array($condition{0},array('>', '<', '='))) {
list (,$operator,$operand) = $matches;
return $operator. $operand;
} // function _ifCondition()
* @param mixed $value Value to check
$value = self::flattenSingleValue($value);
foreach(self::$_errorCodes as $errorCode) {
if ($value == $errorCode) {
return self::$_errorCodes['na'];
} // function ERROR_TYPE()
* @param mixed $value Value to check
public static function IS_BLANK($value= null) {
$value = self::flattenSingleValue($value);
* @param mixed $value Value to check
public static function IS_ERR($value = '') {
$value = self::flattenSingleValue($value);
return self::IS_ERROR($value) && (!self::IS_NA($value));
* @param mixed $value Value to check
public static function IS_ERROR($value = '') {
$value = self::flattenSingleValue($value);
* @param mixed $value Value to check
public static function IS_NA($value = '') {
$value = self::flattenSingleValue($value);
return ($value === self::$_errorCodes['na']);
* @param mixed $value Value to check
public static function IS_EVEN($value = 0) {
$value = self::flattenSingleValue($value);
return self::$_errorCodes['value'];
return ($value % 2 == 0);
* @param mixed $value Value to check
public static function IS_ODD($value = null) {
$value = self::flattenSingleValue($value);
return self::$_errorCodes['value'];
return (abs($value) % 2 == 1);
* @param mixed $value Value to check
public static function IS_NUMBER($value = 0) {
$value = self::flattenSingleValue($value);
} // function IS_NUMBER()
* @param mixed $value Value to check
public static function IS_LOGICAL($value = true) {
$value = self::flattenSingleValue($value);
} // function IS_LOGICAL()
* @param mixed $value Value to check
public static function IS_TEXT($value = '') {
$value = self::flattenSingleValue($value);
* @param mixed $value Value to check
return !self::IS_TEXT($value);
} // function IS_NONTEXT()
* @return string Version information
return 'PHPExcel 1.7.6, 2011-02-27';
* Returns a value converted to a number
* @param value The value you want converted
* @return number N converts values listed in the following table
* If value is or refers to N returns
* A date The serial number of that date
* An error value The error value
public static function N($value) {
if ((strlen($value) > 0) && ($value{0} == '#')) {
* Returns a number that identifies the type of a value
* @param value The value you want tested
* @return number N converts values listed in the following table
* If value is or refers to N returns
public static function TYPE($value) {
$value = self::flattenArrayIndexed($value);
// Range of cells is an error
if (self::isCellValue($a)) {
} elseif (self::isMatrixValue($a)) {
} elseif(count($value) == 0) {
$value = self::flattenSingleValue($value);
if ((strlen($value) > 0) && ($value{0} == '#')) {
* Convert a multi-dimensional array to a simple 1-dimensional array
* @param array $array Array to be flattened
* @return array Flattened array
foreach ($array as $value) {
foreach ($value as $val) {
} // function flattenArray()
* Convert a multi-dimensional array to a simple 1-dimensional array, but retain an element of indexing
* @param array $array Array to be flattened
* @return array Flattened array
foreach ($array as $k1 => $value) {
foreach ($value as $k2 => $val) {
foreach ($val as $k3 => $v) {
$arrayValues[$k1. '.'. $k2. '.'. $k3] = $v;
$arrayValues[$k1. '.'. $k2] = $val;
$arrayValues[$k1] = $value;
} // function flattenArrayIndexed()
* Convert an array to a single scalar value by extracting the first element
* @param mixed $value Array or scalar value
} // function flattenSingleValue()
} // class PHPExcel_Calculation_Functions
// There are a few mathematical functions that aren't available on all versions of PHP for all platforms
// These functions aren't available in Windows implementations of PHP prior to version 5.3.0
// So we test if they do exist for this version of PHP/operating platform; and if not we create them
return 2 * log(sqrt(($x + 1) / 2) + sqrt(($x - 1) / 2));
return log($x + sqrt(1 + $x * $x));
return (log(1 + $x) - log(1 - $x)) / 2;
$regex = array( '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?(?:#([0-9]+))?',
'(?:\.([0-9]+))?([in%])/'
trigger_error("No format specified or invalid format", E_USER_WARNING);
$flags = array( 'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ? $match[1] : ' ',
'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ? $match[0] : '+',
$width = trim($fmatch[2]) ? (int) $fmatch[2] : 0;
$left = trim($fmatch[3]) ? (int) $fmatch[3] : 0;
$right = trim($fmatch[4]) ? (int) $fmatch[4] : $locale['int_frac_digits'];
$conversion = $fmatch[5];
$letter = $positive ? 'p' : 'n';
$prefix = $suffix = $cprefix = $csuffix = $signal = '';
$signal = $locale['negative_sign'];
case $locale['n_sign_posn'] == 0 || $flags['usesignal'] == '(':
case $locale['n_sign_posn'] == 1:
case $locale['n_sign_posn'] == 2:
case $locale['n_sign_posn'] == 3:
case $locale['n_sign_posn'] == 4:
if (!$flags['nosimbol']) {
$currency .= ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']);
$currency = iconv('ISO-8859-1','UTF-8',$currency);
$space = $locale["{ $letter}_sep_by_space"] ? ' ' : '';
$number = number_format($number, $right, $locale['mon_decimal_point'], $flags['nogroup'] ? '' : $locale['mon_thousands_sep'] );
$number = explode($locale['mon_decimal_point'], $number);
if ($left > 0 && $left > $n) {
$number[0] .= str_repeat($flags['fillchar'], $left - $n);
$number[0] = str_repeat($flags['fillchar'], $left - $n) . $number[0];
$number = implode($locale['mon_decimal_point'], $number);
if ($locale["{ $letter}_cs_precedes"]) {
$number = $prefix . $currency . $space . $number . $suffix;
$number = $prefix . $number . $space . $currency . $suffix;
$number = str_pad($number, $width, $flags['fillchar'], $flags['isleft'] ? STR_PAD_RIGHT : STR_PAD_LEFT);
} // function money_format()
// Strangely, PHP doesn't have a mb_str_replace multibyte function
// As we'll only ever use this function with UTF-8 characters, we can simply "hard-code" the character set
foreach($subject as $key => $val) {
foreach((array) $search as $key => $s) {
|