Skip to content

Commit 4d92e77

Browse files
author
Mark Baker
committed
Eliminate need for use of money_format() function; various fixes to HLOOKUP, VLOOKUP and DOLLAR functions + unit tests
1 parent 83bd690 commit 4d92e77

File tree

6 files changed

+37
-101
lines changed

6 files changed

+37
-101
lines changed

Classes/PHPExcel/Calculation/Functions.php

-94
Original file line numberDiff line numberDiff line change
@@ -689,100 +689,6 @@ function atanh($x) {
689689
} // function atanh()
690690
}
691691

692-
if (!function_exists('money_format')) {
693-
function money_format($format, $number) {
694-
$regex = array( '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?(?:#([0-9]+))?',
695-
'(?:\.([0-9]+))?([in%])/'
696-
);
697-
$regex = implode('', $regex);
698-
if (setlocale(LC_MONETARY, null) == '') {
699-
setlocale(LC_MONETARY, '');
700-
}
701-
$locale = localeconv();
702-
$number = floatval($number);
703-
if (!preg_match($regex, $format, $fmatch)) {
704-
trigger_error("No format specified or invalid format", E_USER_WARNING);
705-
return $number;
706-
}
707-
$flags = array( 'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ? $match[1] : ' ',
708-
'nogroup' => preg_match('/\^/', $fmatch[1]) > 0,
709-
'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ? $match[0] : '+',
710-
'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0,
711-
'isleft' => preg_match('/\-/', $fmatch[1]) > 0
712-
);
713-
$width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
714-
$left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
715-
$right = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
716-
$conversion = $fmatch[5];
717-
$positive = true;
718-
if ($number < 0) {
719-
$positive = false;
720-
$number *= -1;
721-
}
722-
$letter = $positive ? 'p' : 'n';
723-
$prefix = $suffix = $cprefix = $csuffix = $signal = '';
724-
if (!$positive) {
725-
$signal = $locale['negative_sign'];
726-
switch (true) {
727-
case $locale['n_sign_posn'] == 0 || $flags['usesignal'] == '(':
728-
$prefix = '(';
729-
$suffix = ')';
730-
break;
731-
case $locale['n_sign_posn'] == 1:
732-
$prefix = $signal;
733-
break;
734-
case $locale['n_sign_posn'] == 2:
735-
$suffix = $signal;
736-
break;
737-
case $locale['n_sign_posn'] == 3:
738-
$cprefix = $signal;
739-
break;
740-
case $locale['n_sign_posn'] == 4:
741-
$csuffix = $signal;
742-
break;
743-
}
744-
}
745-
if (!$flags['nosimbol']) {
746-
$currency = $cprefix;
747-
$currency .= ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']);
748-
$currency .= $csuffix;
749-
$currency = iconv('ISO-8859-1','UTF-8',$currency);
750-
} else {
751-
$currency = '';
752-
}
753-
$space = $locale["{$letter}_sep_by_space"] ? ' ' : '';
754-
755-
if (!isset($locale['mon_decimal_point']) || empty($locale['mon_decimal_point'])) {
756-
$locale['mon_decimal_point'] = (!isset($locale['decimal_point']) || empty($locale['decimal_point'])) ?
757-
$locale['decimal_point'] :
758-
'.';
759-
}
760-
761-
$number = number_format($number, $right, $locale['mon_decimal_point'], $flags['nogroup'] ? '' : $locale['mon_thousands_sep'] );
762-
$number = explode($locale['mon_decimal_point'], $number);
763-
764-
$n = strlen($prefix) + strlen($currency);
765-
if ($left > 0 && $left > $n) {
766-
if ($flags['isleft']) {
767-
$number[0] .= str_repeat($flags['fillchar'], $left - $n);
768-
} else {
769-
$number[0] = str_repeat($flags['fillchar'], $left - $n) . $number[0];
770-
}
771-
}
772-
$number = implode($locale['mon_decimal_point'], $number);
773-
if ($locale["{$letter}_cs_precedes"]) {
774-
$number = $prefix . $currency . $space . $number . $suffix;
775-
} else {
776-
$number = $prefix . $number . $space . $currency . $suffix;
777-
}
778-
if ($width > 0) {
779-
$number = str_pad($number, $width, $flags['fillchar'], $flags['isleft'] ? STR_PAD_RIGHT : STR_PAD_LEFT);
780-
}
781-
$format = str_replace($fmatch[0], $number, $format);
782-
return $format;
783-
} // function money_format()
784-
}
785-
786692

787693
//
788694
// Strangely, PHP doesn't have a mb_str_replace multibyte function

Classes/PHPExcel/Calculation/LookupRef.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,11 @@ public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not
735735
return PHPExcel_Calculation_Functions::NA();
736736
} else {
737737
// otherwise return the appropriate value
738-
return $lookup_array[$rowNumber][$returnColumn];
738+
$result = $lookup_array[$rowNumber][$returnColumn];
739+
if ((is_numeric($lookup_value) && is_numeric($result)) ||
740+
(!is_numeric($lookup_value) && !is_numeric($result))) {
741+
return $result;
742+
}
739743
}
740744
}
741745

@@ -798,7 +802,8 @@ public static function HLOOKUP($lookup_value, $lookup_array, $index_number, $not
798802
return PHPExcel_Calculation_Functions::NA();
799803
} else {
800804
// otherwise return the appropriate value
801-
return $lookup_array[$returnColumn][$rowNumber];
805+
$result = $lookup_array[$returnColumn][$rowNumber];
806+
return $result;
802807
}
803808
}
804809

Classes/PHPExcel/Calculation/TextData.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,17 @@ public static function DOLLAR($value = 0, $decimals = 2) {
208208
}
209209
$decimals = floor($decimals);
210210

211+
$mask = '$#,##0';
211212
if ($decimals > 0) {
212-
return money_format('%.'.$decimals.'n',$value);
213+
$mask .= '.' . str_repeat('0',$decimals);
213214
} else {
214215
$round = pow(10,abs($decimals));
215216
if ($value < 0) { $round = 0-$round; }
216-
$value = PHPExcel_Calculation_MathTrig::MROUND($value,$round);
217-
// The implementation of money_format used if the standard PHP function is not available can't handle decimal places of 0,
218-
// so we display to 1 dp and chop off that character and the decimal separator using substr
219-
return substr(money_format('%.1n',$value),0,-2);
217+
$value = PHPExcel_Calculation_MathTrig::MROUND($value, $round);
220218
}
219+
220+
return PHPExcel_Style_NumberFormat::toFormattedString($value, $mask);
221+
221222
} // function DOLLAR()
222223

223224

unitTests/Classes/PHPExcel/Calculation/LookupRefTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,20 @@ public function providerHLOOKUP()
3131
return new testDataFileIterator('rawTestData/Calculation/LookupRef/HLOOKUP.data');
3232
}
3333

34+
/**
35+
* @dataProvider providerVLOOKUP
36+
*/
37+
public function testVLOOKUP()
38+
{
39+
$args = func_get_args();
40+
$expectedResult = array_pop($args);
41+
$result = call_user_func_array(array('PHPExcel_Calculation_LookupRef','VLOOKUP'),$args);
42+
$this->assertEquals($expectedResult, $result);
43+
}
44+
45+
public function providerVLOOKUP()
46+
{
47+
return new testDataFileIterator('rawTestData/Calculation/LookupRef/VLOOKUP.data');
48+
}
49+
3450
}

unitTests/Classes/PHPExcel/Style/NumberFormatTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public function setUp()
1212
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
1313
}
1414
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
15+
16+
PHPExcel_Shared_String::setDecimalSeparator('.');
17+
PHPExcel_Shared_String::setThousandsSeparator(',');
1518
}
1619

1720
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1, {"Density"|"Viscosity"|"Temperature";0.457|3.55|500;0.525|3.25|400;0.616|2.93|300;0.675|2.75|250;0.746|2.57|200;0.835|2.38|150;0.946|2.17|100;1.09|1.95|50;1.29|1.71|0}, 2, FALSE, 2.17
2+
1, {"Density"|"Viscosity"|"Temperature";0.457|3.55|500;0.525|3.25|400;0.616|2.93|300;0.675|2.75|250;0.746|2.57|200;0.835|2.38|150;0.946|2.17|100;1.09|1.95|50;1.29|1.71|0}, 3, TRUE, 100
3+
.7, {"Density"|"Viscosity"|"Temperature";0.457|3.55|500;0.525|3.25|400;0.616|2.93|300;0.675|2.75|250;0.746|2.57|200;0.835|2.38|150;0.946|2.17|100;1.09|1.95|50;1.29|1.71|0}, 3, FALSE, "#N/A"
4+
0.1, {"Density"|"Viscosity"|"Temperature";0.457|3.55|500;0.525|3.25|400;0.616|2.93|300;0.675|2.75|250;0.746|2.57|200;0.835|2.38|150;0.946|2.17|100;1.09|1.95|50;1.29|1.71|0}, 2, TRUE, "#N/A"
5+
2, {"Density"|"Viscosity"|"Temperature";0.457|3.55|500;0.525|3.25|400;0.616|2.93|300;0.675|2.75|250;0.746|2.57|200;0.835|2.38|150;0.946|2.17|100;1.09|1.95|50;1.29|1.71|0}, 2, TRUE, 1.71

0 commit comments

Comments
 (0)