30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
35 require(PHPEXCEL_ROOT .
'PHPExcel/Autoloader.php');
67 public static function CELL_ADDRESS(
$row, $column, $relativity=1, $referenceStyle=True, $sheetText=
'') {
73 if ((
$row < 1) || ($column < 1)) {
77 if ($sheetText >
'') {
78 if (strpos($sheetText,
' ') !== False) { $sheetText =
"'".$sheetText.
"'"; }
81 if ((!is_bool($referenceStyle)) || $referenceStyle) {
82 $rowRelative = $columnRelative =
'$';
84 if (($relativity == 2) || ($relativity == 4)) { $columnRelative =
''; }
85 if (($relativity == 3) || ($relativity == 4)) { $rowRelative =
''; }
86 return $sheetText.$columnRelative.$column.$rowRelative.$row;
88 if (($relativity == 2) || ($relativity == 4)) { $column =
'['.$column.
']'; }
89 if (($relativity == 3) || ($relativity == 4)) {
$row =
'['.$row.
']'; }
90 return $sheetText.
'R'.
$row.
'C'.$column;
106 public static function COLUMN($cellAddress=Null) {
107 if (is_null($cellAddress) || trim($cellAddress) ===
'') {
return 0; }
109 if (is_array($cellAddress)) {
110 foreach($cellAddress as $columnKey => $value) {
111 $columnKey = preg_replace(
'/[^a-z]/i',
'',$columnKey);
115 if (strpos($cellAddress,
'!') !==
false) {
116 list($sheet,$cellAddress) = explode(
'!',$cellAddress);
118 if (strpos($cellAddress,
':') !==
false) {
119 list($startAddress,$endAddress) = explode(
':',$cellAddress);
120 $startAddress = preg_replace(
'/[^a-z]/i',
'',$startAddress);
121 $endAddress = preg_replace(
'/[^a-z]/i',
'',$endAddress);
122 $returnValue = array();
125 }
while ($startAddress++ != $endAddress);
128 $cellAddress = preg_replace(
'/[^a-z]/i',
'',$cellAddress);
143 public static function COLUMNS($cellAddress=Null) {
144 if (is_null($cellAddress) || $cellAddress ===
'') {
146 } elseif (!is_array($cellAddress)) {
150 $x = array_keys($cellAddress);
151 $x = array_shift(
$x);
152 $isMatrix = (is_numeric(
$x));
174 public static function ROW($cellAddress=Null) {
175 if (is_null($cellAddress) || trim($cellAddress) ===
'') {
return 0; }
177 if (is_array($cellAddress)) {
178 foreach($cellAddress as $columnKey => $rowValue) {
179 foreach($rowValue as $rowKey => $cellValue) {
180 return (integer) preg_replace(
'/[^0-9]/i',
'',$rowKey);
184 if (strpos($cellAddress,
'!') !==
false) {
185 list($sheet,$cellAddress) = explode(
'!',$cellAddress);
187 if (strpos($cellAddress,
':') !==
false) {
188 list($startAddress,$endAddress) = explode(
':',$cellAddress);
189 $startAddress = preg_replace(
'/[^0-9]/',
'',$startAddress);
190 $endAddress = preg_replace(
'/[^0-9]/',
'',$endAddress);
191 $returnValue = array();
193 $returnValue[][] = (integer) $startAddress;
194 }
while ($startAddress++ != $endAddress);
197 list($cellAddress) = explode(
':',$cellAddress);
198 return (integer) preg_replace(
'/[^0-9]/',
'',$cellAddress);
212 public static function ROWS($cellAddress=Null) {
213 if (is_null($cellAddress) || $cellAddress ===
'') {
215 } elseif (!is_array($cellAddress)) {
219 $i = array_keys($cellAddress);
220 $isMatrix = (is_numeric(array_shift($i)));
244 $args = func_get_args();
245 $pCell = array_pop($args);
250 if ((!is_object($pCell)) || (trim($linkURL) ==
'')) {
254 if ((is_object($displayName)) || trim($displayName) ==
'') {
255 $displayName = $linkURL;
258 $pCell->getHyperlink()->setUrl($linkURL);
274 if (is_null($cellAddress) || $cellAddress ===
'') {
278 $cellAddress1 = $cellAddress;
279 $cellAddress2 = NULL;
280 if (strpos($cellAddress,
':') !==
false) {
281 list($cellAddress1,$cellAddress2) = explode(
':',$cellAddress);
289 if (strpos($cellAddress,
'!') !==
false) {
290 list($sheetName,$cellAddress) = explode(
'!',$cellAddress);
291 $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
293 $pSheet = $pCell->getParent();
322 public static function OFFSET($cellAddress=Null,$rows=0,$columns=0,$height=null,$width=null) {
327 if ($cellAddress == Null) {
331 $args = func_get_args();
332 $pCell = array_pop($args);
333 if (!is_object($pCell)) {
338 if (strpos($cellAddress,
"!")) {
339 list($sheetName,$cellAddress) = explode(
"!",$cellAddress);
341 if (strpos($cellAddress,
":")) {
342 list($startCell,$endCell) = explode(
":",$cellAddress);
344 $startCell = $endCell = $cellAddress;
349 $startCellRow += $rows;
351 $startCellColumn += $columns;
353 if (($startCellRow <= 0) || ($startCellColumn < 0)) {
357 if (($width != null) && (!is_object($width))) {
358 $endCellColumn = $startCellColumn + $width - 1;
360 $endCellColumn += $columns;
364 if (($height != null) && (!is_object($height))) {
365 $endCellRow = $startCellRow + $height - 1;
367 $endCellRow += $rows;
370 if (($endCellRow <= 0) || ($endCellColumn < 0)) {
375 $cellAddress = $startCellColumn.$startCellRow;
376 if (($startCellColumn != $endCellColumn) || ($startCellRow != $endCellRow)) {
377 $cellAddress .=
':'.$endCellColumn.$endCellRow;
380 if ($sheetName !== null) {
381 $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
383 $pSheet = $pCell->getParent();
391 $chooseArgs = func_get_args();
393 $entryCount = count($chooseArgs) - 1;
395 if(is_array($chosenEntry)) {
396 $chosenEntry = array_shift($chosenEntry);
398 if ((is_numeric($chosenEntry)) && (!is_bool($chosenEntry))) {
403 $chosenEntry = floor($chosenEntry);
404 if (($chosenEntry <= 0) || ($chosenEntry > $entryCount)) {
408 if (is_array($chooseArgs[$chosenEntry])) {
411 return $chooseArgs[$chosenEntry];
426 public static function MATCH($lookup_value, $lookup_array, $match_type=1) {
431 $lookup_value = strtolower($lookup_value);
434 if ((!is_numeric($lookup_value)) && (!is_string($lookup_value)) && (!is_bool($lookup_value))) {
439 if (($match_type !== 0) && ($match_type !== -1) && ($match_type !== 1)) {
444 $lookupArraySize = count($lookup_array);
445 if ($lookupArraySize <= 0) {
450 foreach($lookup_array as $i => $lookupArrayValue) {
452 if ((!is_numeric($lookupArrayValue)) && (!is_string($lookupArrayValue)) &&
453 (!is_bool($lookupArrayValue)) && (!is_null($lookupArrayValue))) {
457 if (is_string($lookupArrayValue)) {
458 $lookup_array[$i] = strtolower($lookupArrayValue);
460 if ((is_null($lookupArrayValue)) && (($match_type == 1) || ($match_type == -1))) {
461 $lookup_array = array_slice($lookup_array,0,$i-1);
466 if ($match_type == 1) {
467 asort($lookup_array);
468 $keySet = array_keys($lookup_array);
469 } elseif($match_type == -1) {
470 arsort($lookup_array);
471 $keySet = array_keys($lookup_array);
480 foreach($lookup_array as $i => $lookupArrayValue) {
481 if (($match_type == 0) && ($lookupArrayValue == $lookup_value)) {
484 } elseif (($match_type == -1) && ($lookupArrayValue <= $lookup_value)) {
491 $i = array_search($i,$keySet);
499 return $keySet[$i-1]+1;
501 } elseif (($match_type == 1) && ($lookupArrayValue >= $lookup_value)) {
508 $i = array_search($i,$keySet);
516 return $keySet[$i-1]+1;
537 public static function INDEX($arrayValues,$rowNum = 0,$columnNum = 0) {
539 if (($rowNum < 0) || ($columnNum < 0)) {
543 if (!is_array($arrayValues)) {
547 $rowKeys = array_keys($arrayValues);
548 $columnKeys = @array_keys($arrayValues[$rowKeys[0]]);
550 if ($columnNum > count($columnKeys)) {
552 } elseif ($columnNum == 0) {
556 $rowNum = $rowKeys[--$rowNum];
557 $returnArray = array();
558 foreach($arrayValues as $arrayColumn) {
559 if (is_array($arrayColumn)) {
560 if (isset($arrayColumn[$rowNum])) {
561 $returnArray[] = $arrayColumn[$rowNum];
563 return $arrayValues[$rowNum];
566 return $arrayValues[$rowNum];
571 $columnNum = $columnKeys[--$columnNum];
572 if ($rowNum > count($rowKeys)) {
574 } elseif ($rowNum == 0) {
575 return $arrayValues[$columnNum];
577 $rowNum = $rowKeys[--$rowNum];
579 return $arrayValues[$rowNum][$columnNum];
592 $returnMatrix = array();
593 if (!is_array($matrixData)) { $matrixData = array(array($matrixData)); }
596 foreach($matrixData as $matrixRow) {
598 foreach($matrixRow as $matrixCell) {
599 $returnMatrix[
$row][$column] = $matrixCell;
604 return $returnMatrix;
610 $firstColumn = array_shift(
$f);
611 if (strtolower($a[$firstColumn]) == strtolower($b[$firstColumn])) {
614 return (strtolower($a[$firstColumn]) < strtolower($b[$firstColumn])) ? -1 : 1;
627 public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match=
true) {
633 if ($index_number < 1) {
638 if ((!is_array($lookup_array)) || (count($lookup_array) < 1)) {
641 $f = array_keys($lookup_array);
642 $firstRow = array_pop(
$f);
643 if ((!is_array($lookup_array[$firstRow])) || ($index_number > count($lookup_array[$firstRow]))) {
646 $columnKeys = array_keys($lookup_array[$firstRow]);
647 $returnColumn = $columnKeys[--$index_number];
648 $firstColumn = array_shift($columnKeys);
652 if (!$not_exact_match) {
653 uasort($lookup_array,array(
'self',
'_vlookupSort'));
656 $rowNumber = $rowValue = False;
657 foreach($lookup_array as $rowKey => $rowData) {
658 if (strtolower($rowData[$firstColumn]) > strtolower($lookup_value)) {
661 $rowNumber = $rowKey;
662 $rowValue = $rowData[$firstColumn];
665 if ($rowNumber !==
false) {
666 if ((!$not_exact_match) && ($rowValue != $lookup_value)) {
671 return $lookup_array[$rowNumber][$returnColumn];
687 public static function LOOKUP($lookup_value, $lookup_vector, $result_vector=null) {
690 if (!is_array($lookup_vector)) {
693 $lookupRows = count($lookup_vector);
694 $l = array_keys($lookup_vector);
695 $l = array_shift(
$l);
696 $lookupColumns = count($lookup_vector[
$l]);
697 if ((($lookupRows == 1) && ($lookupColumns > 1)) || (($lookupRows == 2) && ($lookupColumns != 2))) {
699 $lookupRows = count($lookup_vector);
700 $l = array_keys($lookup_vector);
701 $lookupColumns = count($lookup_vector[array_shift($l)]);
704 if (is_null($result_vector)) {
705 $result_vector = $lookup_vector;
707 $resultRows = count($result_vector);
708 $l = array_keys($result_vector);
709 $l = array_shift($l);
710 $resultColumns = count($result_vector[$l]);
711 if ((($resultRows == 1) && ($resultColumns > 1)) || (($resultRows == 2) && ($resultColumns != 2))) {
713 $resultRows = count($result_vector);
714 $r = array_keys($result_vector);
715 $resultColumns = count($result_vector[array_shift($r)]);
718 if ($lookupRows == 2) {
719 $result_vector = array_pop($lookup_vector);
720 $lookup_vector = array_shift($lookup_vector);
722 if ($lookupColumns != 2) {
723 foreach($lookup_vector as &$value) {
724 if (is_array($value)) {
725 $k = array_keys($value);
726 $key1 = $key2 = array_shift($k);
728 $dataValue1 = $value[$key1];
732 $dataValue1 = $value;
734 $dataValue2 = array_shift($result_vector);
735 if (is_array($dataValue2)) {
736 $dataValue2 = array_shift($dataValue2);
738 $value = array($key1 => $dataValue1, $key2 => $dataValue2);