| '.$langs->trans("Filter").' | |||||||||
| '.$langs->trans("ThirdParty").' | '; + print ' | ||||||||
| '.$langs->trans("ThirdParty").' | '; if ($mode == 'customer') $filter='s.client in (1,2,3)'; if ($mode == 'supplier') $filter='s.fournisseur = 1'; print $form->select_company($socid,'socid',$filter,1,0,0,array(),0,'','style="width: 95%"'); print ' | ||||||||
| '.$langs->trans("CreatedBy").' | '; + print ' | ||||||||
| '.$langs->trans("CreatedBy").' | '; print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300'); print ' | ||||||||
| '.$langs->trans("Year").' | '; + print ' | ||||||||
| '.$langs->trans("Year").' | ';
if (! in_array($year,$arrayyears)) $arrayyears[$year]=$year;
if (! in_array($nowyear,$arrayyears)) $arrayyears[$nowyear]=$nowyear;
arsort($arrayyears);
diff --git a/htdocs/expensereport/list.php b/htdocs/expensereport/list.php
index 34a28442e45..7e94a10418a 100644
--- a/htdocs/expensereport/list.php
+++ b/htdocs/expensereport/list.php
@@ -3,9 +3,9 @@
* Copyright (C) 2004-2017 Laurent Destailleur | ';
+ print ' | ';
$usertmp->id=$obj->id_user;
$usertmp->lastname=$obj->lastname;
$usertmp->firstname=$obj->firstname;
@@ -805,8 +805,8 @@ if ($resql)
$i++;
if ($i == 1)
{
- if ($num < $limit && empty($offset)) print ' | '.$langs->trans("Total").' | ';
- else print ''.$langs->trans("Totalforthispage").' | ';
+ if ($num < $limit && empty($offset)) print ''.$langs->trans("Total").' | ';
+ else print ''.$langs->trans("Totalforthispage").' | ';
}
elseif ($totalarray['totalhtfield'] == $i) print ''.price($totalarray['totalht']).' | ';
elseif ($totalarray['totalvatfield'] == $i) print ''.price($totalarray['totalvat']).' | ';
diff --git a/htdocs/expensereport/stats/index.php b/htdocs/expensereport/stats/index.php
index c192fb83447..08af170f5e7 100644
--- a/htdocs/expensereport/stats/index.php
+++ b/htdocs/expensereport/stats/index.php
@@ -2,7 +2,7 @@
/* Copyright (C) 2003-2006 Rodolphe Quiedeville |
| '.$langs->trans("Status").' | '; +print ' | ||||||||
| '.$langs->trans("Status").' | '; $liststatus=$tmpexpensereport->statuts; print $form->selectarray('object_status', $liststatus, GETPOST('object_status'), -4, 0, 0, '', 1); print ' | ||||||||
| '.$langs->trans("AvailableFormats").' | '; - print ''.$langs->trans("LibraryUsed").' | '; - print ''.$langs->trans("LibraryVersion").' | '; - print '
| '.$langs->trans("AvailableFormats").' | '; + $htmltabloflibs.= ''.$langs->trans("LibraryUsed").' | '; + $htmltabloflibs.= ''.$langs->trans("LibraryVersion").' | '; + $htmltabloflibs.= '
| '.img_picto_common($key,$objmodelexport->getPictoForKey($key)).' '; + $htmltabloflibs.= ' | ||
| '.img_picto_common($key,$objmodelexport->getPictoForKey($key)).' '; $text=$objmodelexport->getDriverDescForKey($key); $label=$listeall[$key]; - print $form->textwithpicto($label,$text).' | '; - print ''.$objmodelexport->getLibLabelForKey($key).' | '; - print ''.$objmodelexport->getLibVersionForKey($key).' | '; - print ''.$objmodelexport->getLibLabelForKey($key).' | '; + $htmltabloflibs.= ''.$objmodelexport->getLibVersionForKey($key).' | '; + $htmltabloflibs.= ''."\n"; } - print '
| '.$langs->trans("Filter").' | ||||||||||||||||||||||||||||||||
| '.$langs->trans("ThirdParty").' | '; + print ' | |||||||||||||||||||||||||||||||
| '.$langs->trans("ThirdParty").' | '; $filter = 's.client in (1,2,3)'; print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, '', 'style="width: 95%"'); print ' | |||||||||||||||||||||||||||||||
| '.$langs->trans("CreatedBy").' | '; + print ' | |||||||||||||||||||||||||||||||
| '.$langs->trans("CreatedBy").' | '; print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300'); // Status - print ' | |||||||||||||||||||||||||||||||
| '.$langs->trans("Status").' | '; + print ' | |||||||||||||||||||||||||||||||
| '.$langs->trans("Status").' | '; $tmp = $objectstatic->LibStatut(0); // To load $this->statuts_short $liststatus=$objectstatic->statuts_short; if (empty($conf->global->FICHINTER_CLASSIFY_BILLED)) unset($liststatus[2]); // Option deprecated. In a future, billed must be managed with a dedicated field to 0 or 1 print $form->selectarray('object_status', $liststatus, $object_status, 1, 0, 0, '', 1); print ' | |||||||||||||||||||||||||||||||
| '.$langs->trans("Year").' | '; + print ' | |||||||||||||||||||||||||||||||
| '.$langs->trans("Year").' | ';
if (! in_array($year,$arrayyears)) $arrayyears[$year]=$year;
if (! in_array($nowyear,$arrayyears)) $arrayyears[$nowyear]=$nowyear;
arsort($arrayyears);
diff --git a/htdocs/filefunc.inc.php b/htdocs/filefunc.inc.php
index 70fdcfba305..4edfa3d0623 100644
--- a/htdocs/filefunc.inc.php
+++ b/htdocs/filefunc.inc.php
@@ -251,6 +251,7 @@ if (! defined('FPDI_PATH')) { define('FPDI_PATH', (empty($
if (! defined('TCPDI_PATH')) { define('TCPDI_PATH', (empty($dolibarr_lib_TCPDI_PATH))?DOL_DOCUMENT_ROOT.'/includes/tcpdi/':$dolibarr_lib_TCPDI_PATH.'/'); }
if (! defined('NUSOAP_PATH')) { define('NUSOAP_PATH', (!isset($dolibarr_lib_NUSOAP_PATH))?DOL_DOCUMENT_ROOT.'/includes/nusoap/lib/':(empty($dolibarr_lib_NUSOAP_PATH)?'':$dolibarr_lib_NUSOAP_PATH.'/')); }
if (! defined('PHPEXCEL_PATH')) { define('PHPEXCEL_PATH', (!isset($dolibarr_lib_PHPEXCEL_PATH))?DOL_DOCUMENT_ROOT.'/includes/phpoffice/phpexcel/Classes/':(empty($dolibarr_lib_PHPEXCEL_PATH)?'':$dolibarr_lib_PHPEXCEL_PATH.'/')); }
+if (! defined('PHPEXCELNEW_PATH')) { define('PHPEXCELNEW_PATH', (!isset($dolibarr_lib_PHPEXCELNEW_PATH))?DOL_DOCUMENT_ROOT.'/includes/phpoffice/PhpSpreadsheet/':(empty($dolibarr_lib_PHPEXCELNEW_PATH)?'':$dolibarr_lib_PHPEXCELNEW_PATH.'/')); }
if (! defined('GEOIP_PATH')) { define('GEOIP_PATH', (!isset($dolibarr_lib_GEOIP_PATH))?DOL_DOCUMENT_ROOT.'/includes/geoip/':(empty($dolibarr_lib_GEOIP_PATH)?'':$dolibarr_lib_GEOIP_PATH.'/')); }
if (! defined('ODTPHP_PATH')) { define('ODTPHP_PATH', (!isset($dolibarr_lib_ODTPHP_PATH))?DOL_DOCUMENT_ROOT.'/includes/odtphp/':(empty($dolibarr_lib_ODTPHP_PATH)?'':$dolibarr_lib_ODTPHP_PATH.'/')); }
if (! defined('ODTPHP_PATHTOPCLZIP')) { define('ODTPHP_PATHTOPCLZIP', (!isset($dolibarr_lib_ODTPHP_PATHTOPCLZIP))?DOL_DOCUMENT_ROOT.'/includes/odtphp/zip/pclzip/':(empty($dolibarr_lib_ODTPHP_PATHTOPCLZIP)?'':$dolibarr_lib_ODTPHP_PATHTOPCLZIP.'/')); }
diff --git a/htdocs/fourn/commande/list.php b/htdocs/fourn/commande/list.php
index b4892d3d89f..4aa18b6d40e 100644
--- a/htdocs/fourn/commande/list.php
+++ b/htdocs/fourn/commande/list.php
@@ -1,5 +1,5 @@
+/* Copyright (C) 2001-2006 Rodolphe Quiedeville | '.$langs->trans("Total").' | ';
- else print ''.$langs->trans("Totalforthispage").' | ';
+ if ($num < $limit) print ''.$langs->trans("Total").' | ';
+ else print ''.$langs->trans("Totalforthispage").' | ';
}
elseif ($totalarray['totalhtfield'] == $i) print ''.price($totalarray['totalht']).' | ';
elseif ($totalarray['totalvatfield'] == $i) print ''.price($totalarray['totalvat']).' | ';
diff --git a/htdocs/fourn/facture/list.php b/htdocs/fourn/facture/list.php
index 9286c038507..4fab329142c 100644
--- a/htdocs/fourn/facture/list.php
+++ b/htdocs/fourn/facture/list.php
@@ -1,8 +1,8 @@
+/* Copyright (C) 2002-2006 Rodolphe Quiedeville '.$langs->trans("Total").' | ';
- else print ''.$langs->trans("Totalforthispage").' | ';
+ if ($num < $limit && empty($offset)) print ''.$langs->trans("Total").' | ';
+ else print ''.$langs->trans("Totalforthispage").' | ';
}
elseif ($totalarray['totalhtfield'] == $i) print ''.price($totalarray['totalht']).' | ';
elseif ($totalarray['totalvatfield'] == $i) print ''.price($totalarray['totalvat']).' | ';
diff --git a/htdocs/fourn/index.php b/htdocs/fourn/index.php
index 5812553db5c..d60519dda04 100644
--- a/htdocs/fourn/index.php
+++ b/htdocs/fourn/index.php
@@ -203,7 +203,7 @@ if (! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture-
$i++;
}
- print ''.$langs->trans("Total").' | ';
+ print ''.$langs->trans("Total").' | ';
print ''.price($tot_ttc).' | ';
print ''.img_object($langs->trans("ShowSupplier"),"company").'';
print " socid."\">".$obj->name." | \n";
- print ''.$obj->code_fournisseur.' | ';
+ print ''.$obj->code_fournisseur.' | ';
print ''.dol_print_date($db->jdate($obj->tms),'day').' | ';
print "'.$establishmentstatic->getNomUrl(1).' | ';
- print ''.$obj->address.' | ';
- print ''.$obj->zip.' | ';
- print ''.$obj->town.' | ';
+ print ''.$obj->address.' | ';
+ print ''.$obj->zip.' | ';
+ print ''.$obj->town.' | ';
print '';
print $establishmentstatic->getLibStatut(5);
diff --git a/htdocs/includes/Psr/autoloader.php b/htdocs/includes/Psr/autoloader.php
new file mode 100644
index 00000000000..1270188ebfd
--- /dev/null
+++ b/htdocs/includes/Psr/autoloader.php
@@ -0,0 +1,10 @@
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+> of this software and associated documentation files (the "Software"), to deal
+> in the Software without restriction, including without limitation the rights
+> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+> copies of the Software, and to permit persons to whom the Software is
+> furnished to do so, subject to the following conditions:
+>
+> The above copyright notice and this permission notice shall be included in
+> all copies or substantial portions of the Software.
+>
+> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+> THE SOFTWARE.
diff --git a/htdocs/includes/Psr/simple-cache/README.md b/htdocs/includes/Psr/simple-cache/README.md
new file mode 100644
index 00000000000..43641d175cc
--- /dev/null
+++ b/htdocs/includes/Psr/simple-cache/README.md
@@ -0,0 +1,8 @@
+PHP FIG Simple Cache PSR
+========================
+
+This repository holds all interfaces related to PSR-16.
+
+Note that this is not a cache implementation of its own. It is merely an interface that describes a cache implementation. See [the specification](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-16-simple-cache.md) for more details.
+
+You can find implementations of the specification by looking for packages providing the [psr/simple-cache-implementation](https://packagist.org/providers/psr/simple-cache-implementation) virtual package.
diff --git a/htdocs/includes/Psr/simple-cache/composer.json b/htdocs/includes/Psr/simple-cache/composer.json
new file mode 100644
index 00000000000..2978fa559a7
--- /dev/null
+++ b/htdocs/includes/Psr/simple-cache/composer.json
@@ -0,0 +1,25 @@
+{
+ "name": "psr/simple-cache",
+ "description": "Common interfaces for simple caching",
+ "keywords": ["psr", "psr-16", "cache", "simple-cache", "caching"],
+ "license": "MIT",
+ "authors": [
+ {
+ "name": "PHP-FIG",
+ "homepage": "http://www.php-fig.org/"
+ }
+ ],
+ "require": {
+ "php": ">=5.3.0"
+ },
+ "autoload": {
+ "psr-4": {
+ "Psr\\SimpleCache\\": "src/"
+ }
+ },
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.0.x-dev"
+ }
+ }
+}
diff --git a/htdocs/includes/Psr/simple-cache/src/CacheException.php b/htdocs/includes/Psr/simple-cache/src/CacheException.php
new file mode 100644
index 00000000000..eba53815c0c
--- /dev/null
+++ b/htdocs/includes/Psr/simple-cache/src/CacheException.php
@@ -0,0 +1,10 @@
+ value pairs. Cache keys that do not exist or are stale will have $default as value.
+ *
+ * @throws \Psr\SimpleCache\InvalidArgumentException
+ * MUST be thrown if $keys is neither an array nor a Traversable,
+ * or if any of the $keys are not a legal value.
+ */
+ public function getMultiple($keys, $default = null);
+
+ /**
+ * Persists a set of key => value pairs in the cache, with an optional TTL.
+ *
+ * @param iterable $values A list of key => value pairs for a multiple-set operation.
+ * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
+ * the driver supports TTL then the library may set a default value
+ * for it or let the driver take care of that.
+ *
+ * @return bool True on success and false on failure.
+ *
+ * @throws \Psr\SimpleCache\InvalidArgumentException
+ * MUST be thrown if $values is neither an array nor a Traversable,
+ * or if any of the $values are not a legal value.
+ */
+ public function setMultiple($values, $ttl = null);
+
+ /**
+ * Deletes multiple cache items in a single operation.
+ *
+ * @param iterable $keys A list of string-based keys to be deleted.
+ *
+ * @return bool True if the items were successfully removed. False if there was an error.
+ *
+ * @throws \Psr\SimpleCache\InvalidArgumentException
+ * MUST be thrown if $keys is neither an array nor a Traversable,
+ * or if any of the $keys are not a legal value.
+ */
+ public function deleteMultiple($keys);
+
+ /**
+ * Determines whether an item is present in the cache.
+ *
+ * NOTE: It is recommended that has() is only to be used for cache warming type purposes
+ * and not to be used within your live applications operations for get/set, as this method
+ * is subject to a race condition where your has() will return true and immediately after,
+ * another script can remove it making the state of your app out of date.
+ *
+ * @param string $key The cache item key.
+ *
+ * @return bool
+ *
+ * @throws \Psr\SimpleCache\InvalidArgumentException
+ * MUST be thrown if the $key string is not a legal value.
+ */
+ public function has($key);
+}
diff --git a/htdocs/includes/Psr/simple-cache/src/InvalidArgumentException.php b/htdocs/includes/Psr/simple-cache/src/InvalidArgumentException.php
new file mode 100644
index 00000000000..6a9524a20c0
--- /dev/null
+++ b/htdocs/includes/Psr/simple-cache/src/InvalidArgumentException.php
@@ -0,0 +1,13 @@
+=-]*)|(\'[^\']*\')|(\"[^\"]*\"))!)?\$?([a-z]{1,3})\$?(\d{1,7})';
+ // Named Range of cells
+ const CALCULATION_REGEXP_NAMEDRANGE = '((([^\s,!&%^\/\*\+<>=-]*)|(\'[^\']*\')|(\"[^\"]*\"))!)?([_A-Z][_A-Z0-9\.]*)';
+ // Error
+ const CALCULATION_REGEXP_ERROR = '\#[A-Z][A-Z0_\/]*[!\?]?';
+
+ /** constants */
+ const RETURN_ARRAY_AS_ERROR = 'error';
+ const RETURN_ARRAY_AS_VALUE = 'value';
+ const RETURN_ARRAY_AS_ARRAY = 'array';
+
+ private static $returnArrayAsType = self::RETURN_ARRAY_AS_VALUE;
+
+ /**
+ * Instance of this class.
+ *
+ * @var Calculation
+ */
+ private static $instance;
+
+ /**
+ * Instance of the spreadsheet this Calculation Engine is using.
+ *
+ * @var Spreadsheet
+ */
+ private $spreadsheet;
+
+ /**
+ * Calculation cache.
+ *
+ * @var array
+ */
+ private $calculationCache = [];
+
+ /**
+ * Calculation cache enabled.
+ *
+ * @var bool
+ */
+ private $calculationCacheEnabled = true;
+
+ /**
+ * List of operators that can be used within formulae
+ * The true/false value indicates whether it is a binary operator or a unary operator.
+ *
+ * @var array
+ */
+ private static $operators = [
+ '+' => true, '-' => true, '*' => true, '/' => true,
+ '^' => true, '&' => true, '%' => false, '~' => false,
+ '>' => true, '<' => true, '=' => true, '>=' => true,
+ '<=' => true, '<>' => true, '|' => true, ':' => true,
+ ];
+
+ /**
+ * List of binary operators (those that expect two operands).
+ *
+ * @var array
+ */
+ private static $binaryOperators = [
+ '+' => true, '-' => true, '*' => true, '/' => true,
+ '^' => true, '&' => true, '>' => true, '<' => true,
+ '=' => true, '>=' => true, '<=' => true, '<>' => true,
+ '|' => true, ':' => true,
+ ];
+
+ /**
+ * The debug log generated by the calculation engine.
+ *
+ * @var Logger
+ */
+ private $debugLog;
+
+ /**
+ * Flag to determine how formula errors should be handled
+ * If true, then a user error will be triggered
+ * If false, then an exception will be thrown.
+ *
+ * @var bool
+ */
+ public $suppressFormulaErrors = false;
+
+ /**
+ * Error message for any error that was raised/thrown by the calculation engine.
+ *
+ * @var string
+ */
+ public $formulaError;
+
+ /**
+ * An array of the nested cell references accessed by the calculation engine, used for the debug log.
+ *
+ * @var array of string
+ */
+ private $cyclicReferenceStack;
+
+ private $cellStack = [];
+
+ /**
+ * Current iteration counter for cyclic formulae
+ * If the value is 0 (or less) then cyclic formulae will throw an exception,
+ * otherwise they will iterate to the limit defined here before returning a result.
+ *
+ * @var int
+ */
+ private $cyclicFormulaCounter = 1;
+
+ private $cyclicFormulaCell = '';
+
+ /**
+ * Number of iterations for cyclic formulae.
+ *
+ * @var int
+ */
+ public $cyclicFormulaCount = 1;
+
+ /**
+ * Epsilon Precision used for comparisons in calculations.
+ *
+ * @var float
+ */
+ private $delta = 0.1e-12;
+
+ /**
+ * The current locale setting.
+ *
+ * @var string
+ */
+ private static $localeLanguage = 'en_us'; // US English (default locale)
+
+ /**
+ * List of available locale settings
+ * Note that this is read for the locale subdirectory only when requested.
+ *
+ * @var string[]
+ */
+ private static $validLocaleLanguages = [
+ 'en', // English (default language)
+ ];
+
+ /**
+ * Locale-specific argument separator for function arguments.
+ *
+ * @var string
+ */
+ private static $localeArgumentSeparator = ',';
+
+ private static $localeFunctions = [];
+
+ /**
+ * Locale-specific translations for Excel constants (True, False and Null).
+ *
+ * @var string[]
+ */
+ public static $localeBoolean = [
+ 'TRUE' => 'TRUE',
+ 'FALSE' => 'FALSE',
+ 'NULL' => 'NULL',
+ ];
+
+ /**
+ * Excel constant string translations to their PHP equivalents
+ * Constant conversion from text name/value to actual (datatyped) value.
+ *
+ * @var string[]
+ */
+ private static $excelConstants = [
+ 'TRUE' => true,
+ 'FALSE' => false,
+ 'NULL' => null,
+ ];
+
+ // PhpSpreadsheet functions
+ private static $phpSpreadsheetFunctions = [
+ 'ABS' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'abs',
+ 'argumentCount' => '1',
+ ],
+ 'ACCRINT' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'ACCRINT'],
+ 'argumentCount' => '4-7',
+ ],
+ 'ACCRINTM' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'ACCRINTM'],
+ 'argumentCount' => '3-5',
+ ],
+ 'ACOS' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'acos',
+ 'argumentCount' => '1',
+ ],
+ 'ACOSH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'acosh',
+ 'argumentCount' => '1',
+ ],
+ 'ACOT' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'ACOT'],
+ 'argumentCount' => '1',
+ ],
+ 'ACOTH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'ACOTH'],
+ 'argumentCount' => '1',
+ ],
+ 'ADDRESS' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'cellAddress'],
+ 'argumentCount' => '2-5',
+ ],
+ 'AMORDEGRC' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'AMORDEGRC'],
+ 'argumentCount' => '6,7',
+ ],
+ 'AMORLINC' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'AMORLINC'],
+ 'argumentCount' => '6,7',
+ ],
+ 'AND' => [
+ 'category' => Category::CATEGORY_LOGICAL,
+ 'functionCall' => [Logical::class, 'logicalAnd'],
+ 'argumentCount' => '1+',
+ ],
+ 'AREAS' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '1',
+ ],
+ 'ASC' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '1',
+ ],
+ 'ASIN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'asin',
+ 'argumentCount' => '1',
+ ],
+ 'ASINH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'asinh',
+ 'argumentCount' => '1',
+ ],
+ 'ATAN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'atan',
+ 'argumentCount' => '1',
+ ],
+ 'ATAN2' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'ATAN2'],
+ 'argumentCount' => '2',
+ ],
+ 'ATANH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'atanh',
+ 'argumentCount' => '1',
+ ],
+ 'AVEDEV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'AVEDEV'],
+ 'argumentCount' => '1+',
+ ],
+ 'AVERAGE' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'AVERAGE'],
+ 'argumentCount' => '1+',
+ ],
+ 'AVERAGEA' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'AVERAGEA'],
+ 'argumentCount' => '1+',
+ ],
+ 'AVERAGEIF' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'AVERAGEIF'],
+ 'argumentCount' => '2,3',
+ ],
+ 'AVERAGEIFS' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '3+',
+ ],
+ 'BAHTTEXT' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '1',
+ ],
+ 'BESSELI' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BESSELI'],
+ 'argumentCount' => '2',
+ ],
+ 'BESSELJ' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BESSELJ'],
+ 'argumentCount' => '2',
+ ],
+ 'BESSELK' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BESSELK'],
+ 'argumentCount' => '2',
+ ],
+ 'BESSELY' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BESSELY'],
+ 'argumentCount' => '2',
+ ],
+ 'BETADIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'BETADIST'],
+ 'argumentCount' => '3-5',
+ ],
+ 'BETAINV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'BETAINV'],
+ 'argumentCount' => '3-5',
+ ],
+ 'BIN2DEC' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BINTODEC'],
+ 'argumentCount' => '1',
+ ],
+ 'BIN2HEX' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BINTOHEX'],
+ 'argumentCount' => '1,2',
+ ],
+ 'BIN2OCT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BINTOOCT'],
+ 'argumentCount' => '1,2',
+ ],
+ 'BINOMDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'BINOMDIST'],
+ 'argumentCount' => '4',
+ ],
+ 'BITAND' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BITAND'],
+ 'argumentCount' => '2',
+ ],
+ 'BITOR' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BITOR'],
+ 'argumentCount' => '2',
+ ],
+ 'BITXOR' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BITOR'],
+ 'argumentCount' => '2',
+ ],
+ 'BITLSHIFT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BITLSHIFT'],
+ 'argumentCount' => '2',
+ ],
+ 'BITRSHIFT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'BITRSHIFT'],
+ 'argumentCount' => '2',
+ ],
+ 'CEILING' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'CEILING'],
+ 'argumentCount' => '2',
+ ],
+ 'CELL' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '1,2',
+ ],
+ 'CHAR' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'CHARACTER'],
+ 'argumentCount' => '1',
+ ],
+ 'CHIDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'CHIDIST'],
+ 'argumentCount' => '2',
+ ],
+ 'CHIINV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'CHIINV'],
+ 'argumentCount' => '2',
+ ],
+ 'CHITEST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '2',
+ ],
+ 'CHOOSE' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'CHOOSE'],
+ 'argumentCount' => '2+',
+ ],
+ 'CLEAN' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'TRIMNONPRINTABLE'],
+ 'argumentCount' => '1',
+ ],
+ 'CODE' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'ASCIICODE'],
+ 'argumentCount' => '1',
+ ],
+ 'COLUMN' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'COLUMN'],
+ 'argumentCount' => '-1',
+ 'passByReference' => [true],
+ ],
+ 'COLUMNS' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'COLUMNS'],
+ 'argumentCount' => '1',
+ ],
+ 'COMBIN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'COMBIN'],
+ 'argumentCount' => '2',
+ ],
+ 'COMPLEX' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'COMPLEX'],
+ 'argumentCount' => '2,3',
+ ],
+ 'CONCAT' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'CONCATENATE'],
+ 'argumentCount' => '1+',
+ ],
+ 'CONCATENATE' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'CONCATENATE'],
+ 'argumentCount' => '1+',
+ ],
+ 'CONFIDENCE' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'CONFIDENCE'],
+ 'argumentCount' => '3',
+ ],
+ 'CONVERT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'CONVERTUOM'],
+ 'argumentCount' => '3',
+ ],
+ 'CORREL' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'CORREL'],
+ 'argumentCount' => '2',
+ ],
+ 'COS' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'cos',
+ 'argumentCount' => '1',
+ ],
+ 'COSH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'cosh',
+ 'argumentCount' => '1',
+ ],
+ 'COT' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'COT'],
+ 'argumentCount' => '1',
+ ],
+ 'COTH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'COTH'],
+ 'argumentCount' => '1',
+ ],
+ 'COUNT' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'COUNT'],
+ 'argumentCount' => '1+',
+ ],
+ 'COUNTA' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'COUNTA'],
+ 'argumentCount' => '1+',
+ ],
+ 'COUNTBLANK' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'COUNTBLANK'],
+ 'argumentCount' => '1',
+ ],
+ 'COUNTIF' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'COUNTIF'],
+ 'argumentCount' => '2',
+ ],
+ 'COUNTIFS' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '2+',
+ ],
+ 'COUPDAYBS' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'COUPDAYBS'],
+ 'argumentCount' => '3,4',
+ ],
+ 'COUPDAYS' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'COUPDAYS'],
+ 'argumentCount' => '3,4',
+ ],
+ 'COUPDAYSNC' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'COUPDAYSNC'],
+ 'argumentCount' => '3,4',
+ ],
+ 'COUPNCD' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'COUPNCD'],
+ 'argumentCount' => '3,4',
+ ],
+ 'COUPNUM' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'COUPNUM'],
+ 'argumentCount' => '3,4',
+ ],
+ 'COUPPCD' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'COUPPCD'],
+ 'argumentCount' => '3,4',
+ ],
+ 'COVAR' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'COVAR'],
+ 'argumentCount' => '2',
+ ],
+ 'CRITBINOM' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'CRITBINOM'],
+ 'argumentCount' => '3',
+ ],
+ 'CSC' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'CSC'],
+ 'argumentCount' => '1',
+ ],
+ 'CSCH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'CSCH'],
+ 'argumentCount' => '1',
+ ],
+ 'CUBEKPIMEMBER' => [
+ 'category' => Category::CATEGORY_CUBE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '?',
+ ],
+ 'CUBEMEMBER' => [
+ 'category' => Category::CATEGORY_CUBE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '?',
+ ],
+ 'CUBEMEMBERPROPERTY' => [
+ 'category' => Category::CATEGORY_CUBE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '?',
+ ],
+ 'CUBERANKEDMEMBER' => [
+ 'category' => Category::CATEGORY_CUBE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '?',
+ ],
+ 'CUBESET' => [
+ 'category' => Category::CATEGORY_CUBE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '?',
+ ],
+ 'CUBESETCOUNT' => [
+ 'category' => Category::CATEGORY_CUBE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '?',
+ ],
+ 'CUBEVALUE' => [
+ 'category' => Category::CATEGORY_CUBE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '?',
+ ],
+ 'CUMIPMT' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'CUMIPMT'],
+ 'argumentCount' => '6',
+ ],
+ 'CUMPRINC' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'CUMPRINC'],
+ 'argumentCount' => '6',
+ ],
+ 'DATE' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'DATE'],
+ 'argumentCount' => '3',
+ ],
+ 'DATEDIF' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'DATEDIF'],
+ 'argumentCount' => '2,3',
+ ],
+ 'DATEVALUE' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'DATEVALUE'],
+ 'argumentCount' => '1',
+ ],
+ 'DAVERAGE' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DAVERAGE'],
+ 'argumentCount' => '3',
+ ],
+ 'DAY' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'DAYOFMONTH'],
+ 'argumentCount' => '1',
+ ],
+ 'DAYS' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'DAYS'],
+ 'argumentCount' => '2',
+ ],
+ 'DAYS360' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'DAYS360'],
+ 'argumentCount' => '2,3',
+ ],
+ 'DB' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'DB'],
+ 'argumentCount' => '4,5',
+ ],
+ 'DCOUNT' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DCOUNT'],
+ 'argumentCount' => '3',
+ ],
+ 'DCOUNTA' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DCOUNTA'],
+ 'argumentCount' => '3',
+ ],
+ 'DDB' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'DDB'],
+ 'argumentCount' => '4,5',
+ ],
+ 'DEC2BIN' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'DECTOBIN'],
+ 'argumentCount' => '1,2',
+ ],
+ 'DEC2HEX' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'DECTOHEX'],
+ 'argumentCount' => '1,2',
+ ],
+ 'DEC2OCT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'DECTOOCT'],
+ 'argumentCount' => '1,2',
+ ],
+ 'DEGREES' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'rad2deg',
+ 'argumentCount' => '1',
+ ],
+ 'DELTA' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'DELTA'],
+ 'argumentCount' => '1,2',
+ ],
+ 'DEVSQ' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'DEVSQ'],
+ 'argumentCount' => '1+',
+ ],
+ 'DGET' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DGET'],
+ 'argumentCount' => '3',
+ ],
+ 'DISC' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'DISC'],
+ 'argumentCount' => '4,5',
+ ],
+ 'DMAX' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DMAX'],
+ 'argumentCount' => '3',
+ ],
+ 'DMIN' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DMIN'],
+ 'argumentCount' => '3',
+ ],
+ 'DOLLAR' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'DOLLAR'],
+ 'argumentCount' => '1,2',
+ ],
+ 'DOLLARDE' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'DOLLARDE'],
+ 'argumentCount' => '2',
+ ],
+ 'DOLLARFR' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'DOLLARFR'],
+ 'argumentCount' => '2',
+ ],
+ 'DPRODUCT' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DPRODUCT'],
+ 'argumentCount' => '3',
+ ],
+ 'DSTDEV' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DSTDEV'],
+ 'argumentCount' => '3',
+ ],
+ 'DSTDEVP' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DSTDEVP'],
+ 'argumentCount' => '3',
+ ],
+ 'DSUM' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DSUM'],
+ 'argumentCount' => '3',
+ ],
+ 'DURATION' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '5,6',
+ ],
+ 'DVAR' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DVAR'],
+ 'argumentCount' => '3',
+ ],
+ 'DVARP' => [
+ 'category' => Category::CATEGORY_DATABASE,
+ 'functionCall' => [Database::class, 'DVARP'],
+ 'argumentCount' => '3',
+ ],
+ 'EDATE' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'EDATE'],
+ 'argumentCount' => '2',
+ ],
+ 'EFFECT' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'EFFECT'],
+ 'argumentCount' => '2',
+ ],
+ 'EOMONTH' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'EOMONTH'],
+ 'argumentCount' => '2',
+ ],
+ 'ERF' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'ERF'],
+ 'argumentCount' => '1,2',
+ ],
+ 'ERF.PRECISE' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'ERFPRECISE'],
+ 'argumentCount' => '1',
+ ],
+ 'ERFC' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'ERFC'],
+ 'argumentCount' => '1',
+ ],
+ 'ERFC.PRECISE' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'ERFC'],
+ 'argumentCount' => '1',
+ ],
+ 'ERROR.TYPE' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'errorType'],
+ 'argumentCount' => '1',
+ ],
+ 'EVEN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'EVEN'],
+ 'argumentCount' => '1',
+ ],
+ 'EXACT' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'EXACT'],
+ 'argumentCount' => '2',
+ ],
+ 'EXP' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'exp',
+ 'argumentCount' => '1',
+ ],
+ 'EXPONDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'EXPONDIST'],
+ 'argumentCount' => '3',
+ ],
+ 'FACT' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'FACT'],
+ 'argumentCount' => '1',
+ ],
+ 'FACTDOUBLE' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'FACTDOUBLE'],
+ 'argumentCount' => '1',
+ ],
+ 'FALSE' => [
+ 'category' => Category::CATEGORY_LOGICAL,
+ 'functionCall' => [Logical::class, 'FALSE'],
+ 'argumentCount' => '0',
+ ],
+ 'FDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '3',
+ ],
+ 'FIND' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'SEARCHSENSITIVE'],
+ 'argumentCount' => '2,3',
+ ],
+ 'FINDB' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'SEARCHSENSITIVE'],
+ 'argumentCount' => '2,3',
+ ],
+ 'FINV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '3',
+ ],
+ 'FISHER' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'FISHER'],
+ 'argumentCount' => '1',
+ ],
+ 'FISHERINV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'FISHERINV'],
+ 'argumentCount' => '1',
+ ],
+ 'FIXED' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'FIXEDFORMAT'],
+ 'argumentCount' => '1-3',
+ ],
+ 'FLOOR' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'FLOOR'],
+ 'argumentCount' => '2',
+ ],
+ 'FORECAST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'FORECAST'],
+ 'argumentCount' => '3',
+ ],
+ 'FORMULATEXT' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'FORMULATEXT'],
+ 'argumentCount' => '1',
+ 'passCellReference' => true,
+ 'passByReference' => [true],
+ ],
+ 'FREQUENCY' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '2',
+ ],
+ 'FTEST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '2',
+ ],
+ 'FV' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'FV'],
+ 'argumentCount' => '3-5',
+ ],
+ 'FVSCHEDULE' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'FVSCHEDULE'],
+ 'argumentCount' => '2',
+ ],
+ 'GAMMADIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'GAMMADIST'],
+ 'argumentCount' => '4',
+ ],
+ 'GAMMAINV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'GAMMAINV'],
+ 'argumentCount' => '3',
+ ],
+ 'GAMMALN' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'GAMMALN'],
+ 'argumentCount' => '1',
+ ],
+ 'GCD' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'GCD'],
+ 'argumentCount' => '1+',
+ ],
+ 'GEOMEAN' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'GEOMEAN'],
+ 'argumentCount' => '1+',
+ ],
+ 'GESTEP' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'GESTEP'],
+ 'argumentCount' => '1,2',
+ ],
+ 'GETPIVOTDATA' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '2+',
+ ],
+ 'GROWTH' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'GROWTH'],
+ 'argumentCount' => '1-4',
+ ],
+ 'HARMEAN' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'HARMEAN'],
+ 'argumentCount' => '1+',
+ ],
+ 'HEX2BIN' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'HEXTOBIN'],
+ 'argumentCount' => '1,2',
+ ],
+ 'HEX2DEC' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'HEXTODEC'],
+ 'argumentCount' => '1',
+ ],
+ 'HEX2OCT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'HEXTOOCT'],
+ 'argumentCount' => '1,2',
+ ],
+ 'HLOOKUP' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'HLOOKUP'],
+ 'argumentCount' => '3,4',
+ ],
+ 'HOUR' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'HOUROFDAY'],
+ 'argumentCount' => '1',
+ ],
+ 'HYPERLINK' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'HYPERLINK'],
+ 'argumentCount' => '1,2',
+ 'passCellReference' => true,
+ ],
+ 'HYPGEOMDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'HYPGEOMDIST'],
+ 'argumentCount' => '4',
+ ],
+ 'IF' => [
+ 'category' => Category::CATEGORY_LOGICAL,
+ 'functionCall' => [Logical::class, 'statementIf'],
+ 'argumentCount' => '1-3',
+ ],
+ 'IFERROR' => [
+ 'category' => Category::CATEGORY_LOGICAL,
+ 'functionCall' => [Logical::class, 'IFERROR'],
+ 'argumentCount' => '2',
+ ],
+ 'IMABS' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMABS'],
+ 'argumentCount' => '1',
+ ],
+ 'IMAGINARY' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMAGINARY'],
+ 'argumentCount' => '1',
+ ],
+ 'IMARGUMENT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMARGUMENT'],
+ 'argumentCount' => '1',
+ ],
+ 'IMCONJUGATE' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMCONJUGATE'],
+ 'argumentCount' => '1',
+ ],
+ 'IMCOS' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMCOS'],
+ 'argumentCount' => '1',
+ ],
+ 'IMCOSH' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMCOSH'],
+ 'argumentCount' => '1',
+ ],
+ 'IMCOT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMCOT'],
+ 'argumentCount' => '1',
+ ],
+ 'IMCSC' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMCSC'],
+ 'argumentCount' => '1',
+ ],
+ 'IMCSCH' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMCSCH'],
+ 'argumentCount' => '1',
+ ],
+ 'IMDIV' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMDIV'],
+ 'argumentCount' => '2',
+ ],
+ 'IMEXP' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMEXP'],
+ 'argumentCount' => '1',
+ ],
+ 'IMLN' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMLN'],
+ 'argumentCount' => '1',
+ ],
+ 'IMLOG10' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMLOG10'],
+ 'argumentCount' => '1',
+ ],
+ 'IMLOG2' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMLOG2'],
+ 'argumentCount' => '1',
+ ],
+ 'IMPOWER' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMPOWER'],
+ 'argumentCount' => '2',
+ ],
+ 'IMPRODUCT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMPRODUCT'],
+ 'argumentCount' => '1+',
+ ],
+ 'IMREAL' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMREAL'],
+ 'argumentCount' => '1',
+ ],
+ 'IMSEC' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMSEC'],
+ 'argumentCount' => '1',
+ ],
+ 'IMSECH' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMSECH'],
+ 'argumentCount' => '1',
+ ],
+ 'IMSIN' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMSIN'],
+ 'argumentCount' => '1',
+ ],
+ 'IMSINH' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMSINH'],
+ 'argumentCount' => '1',
+ ],
+ 'IMSQRT' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMSQRT'],
+ 'argumentCount' => '1',
+ ],
+ 'IMSUB' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMSUB'],
+ 'argumentCount' => '2',
+ ],
+ 'IMSUM' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMSUM'],
+ 'argumentCount' => '1+',
+ ],
+ 'IMTAN' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'IMTAN'],
+ 'argumentCount' => '1',
+ ],
+ 'INDEX' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'INDEX'],
+ 'argumentCount' => '1-4',
+ ],
+ 'INDIRECT' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'INDIRECT'],
+ 'argumentCount' => '1,2',
+ 'passCellReference' => true,
+ ],
+ 'INFO' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '1',
+ ],
+ 'INT' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'INT'],
+ 'argumentCount' => '1',
+ ],
+ 'INTERCEPT' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'INTERCEPT'],
+ 'argumentCount' => '2',
+ ],
+ 'INTRATE' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'INTRATE'],
+ 'argumentCount' => '4,5',
+ ],
+ 'IPMT' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'IPMT'],
+ 'argumentCount' => '4-6',
+ ],
+ 'IRR' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'IRR'],
+ 'argumentCount' => '1,2',
+ ],
+ 'ISBLANK' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isBlank'],
+ 'argumentCount' => '1',
+ ],
+ 'ISERR' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isErr'],
+ 'argumentCount' => '1',
+ ],
+ 'ISERROR' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isError'],
+ 'argumentCount' => '1',
+ ],
+ 'ISEVEN' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isEven'],
+ 'argumentCount' => '1',
+ ],
+ 'ISFORMULA' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isFormula'],
+ 'argumentCount' => '1',
+ 'passCellReference' => true,
+ 'passByReference' => [true],
+ ],
+ 'ISLOGICAL' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isLogical'],
+ 'argumentCount' => '1',
+ ],
+ 'ISNA' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isNa'],
+ 'argumentCount' => '1',
+ ],
+ 'ISNONTEXT' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isNonText'],
+ 'argumentCount' => '1',
+ ],
+ 'ISNUMBER' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isNumber'],
+ 'argumentCount' => '1',
+ ],
+ 'ISODD' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isOdd'],
+ 'argumentCount' => '1',
+ ],
+ 'ISOWEEKNUM' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'ISOWEEKNUM'],
+ 'argumentCount' => '1',
+ ],
+ 'ISPMT' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'ISPMT'],
+ 'argumentCount' => '4',
+ ],
+ 'ISREF' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '1',
+ ],
+ 'ISTEXT' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'isText'],
+ 'argumentCount' => '1',
+ ],
+ 'JIS' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '1',
+ ],
+ 'KURT' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'KURT'],
+ 'argumentCount' => '1+',
+ ],
+ 'LARGE' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'LARGE'],
+ 'argumentCount' => '2',
+ ],
+ 'LCM' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'LCM'],
+ 'argumentCount' => '1+',
+ ],
+ 'LEFT' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'LEFT'],
+ 'argumentCount' => '1,2',
+ ],
+ 'LEFTB' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'LEFT'],
+ 'argumentCount' => '1,2',
+ ],
+ 'LEN' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'STRINGLENGTH'],
+ 'argumentCount' => '1',
+ ],
+ 'LENB' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'STRINGLENGTH'],
+ 'argumentCount' => '1',
+ ],
+ 'LINEST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'LINEST'],
+ 'argumentCount' => '1-4',
+ ],
+ 'LN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'log',
+ 'argumentCount' => '1',
+ ],
+ 'LOG' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'logBase'],
+ 'argumentCount' => '1,2',
+ ],
+ 'LOG10' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'log10',
+ 'argumentCount' => '1',
+ ],
+ 'LOGEST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'LOGEST'],
+ 'argumentCount' => '1-4',
+ ],
+ 'LOGINV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'LOGINV'],
+ 'argumentCount' => '3',
+ ],
+ 'LOGNORMDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'LOGNORMDIST'],
+ 'argumentCount' => '3',
+ ],
+ 'LOOKUP' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'LOOKUP'],
+ 'argumentCount' => '2,3',
+ ],
+ 'LOWER' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'LOWERCASE'],
+ 'argumentCount' => '1',
+ ],
+ 'MATCH' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'MATCH'],
+ 'argumentCount' => '2,3',
+ ],
+ 'MAX' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'MAX'],
+ 'argumentCount' => '1+',
+ ],
+ 'MAXA' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'MAXA'],
+ 'argumentCount' => '1+',
+ ],
+ 'MAXIF' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'MAXIF'],
+ 'argumentCount' => '2+',
+ ],
+ 'MDETERM' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'MDETERM'],
+ 'argumentCount' => '1',
+ ],
+ 'MDURATION' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '5,6',
+ ],
+ 'MEDIAN' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'MEDIAN'],
+ 'argumentCount' => '1+',
+ ],
+ 'MEDIANIF' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '2+',
+ ],
+ 'MID' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'MID'],
+ 'argumentCount' => '3',
+ ],
+ 'MIDB' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'MID'],
+ 'argumentCount' => '3',
+ ],
+ 'MIN' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'MIN'],
+ 'argumentCount' => '1+',
+ ],
+ 'MINA' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'MINA'],
+ 'argumentCount' => '1+',
+ ],
+ 'MINIF' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'MINIF'],
+ 'argumentCount' => '2+',
+ ],
+ 'MINUTE' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'MINUTE'],
+ 'argumentCount' => '1',
+ ],
+ 'MINVERSE' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'MINVERSE'],
+ 'argumentCount' => '1',
+ ],
+ 'MIRR' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'MIRR'],
+ 'argumentCount' => '3',
+ ],
+ 'MMULT' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'MMULT'],
+ 'argumentCount' => '2',
+ ],
+ 'MOD' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'MOD'],
+ 'argumentCount' => '2',
+ ],
+ 'MODE' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'MODE'],
+ 'argumentCount' => '1+',
+ ],
+ 'MODE.SNGL' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'MODE'],
+ 'argumentCount' => '1+',
+ ],
+ 'MONTH' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'MONTHOFYEAR'],
+ 'argumentCount' => '1',
+ ],
+ 'MROUND' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'MROUND'],
+ 'argumentCount' => '2',
+ ],
+ 'MULTINOMIAL' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'MULTINOMIAL'],
+ 'argumentCount' => '1+',
+ ],
+ 'N' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'n'],
+ 'argumentCount' => '1',
+ ],
+ 'NA' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'NA'],
+ 'argumentCount' => '0',
+ ],
+ 'NEGBINOMDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'NEGBINOMDIST'],
+ 'argumentCount' => '3',
+ ],
+ 'NETWORKDAYS' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'NETWORKDAYS'],
+ 'argumentCount' => '2+',
+ ],
+ 'NOMINAL' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'NOMINAL'],
+ 'argumentCount' => '2',
+ ],
+ 'NORMDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'NORMDIST'],
+ 'argumentCount' => '4',
+ ],
+ 'NORMINV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'NORMINV'],
+ 'argumentCount' => '3',
+ ],
+ 'NORMSDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'NORMSDIST'],
+ 'argumentCount' => '1',
+ ],
+ 'NORMSINV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'NORMSINV'],
+ 'argumentCount' => '1',
+ ],
+ 'NOT' => [
+ 'category' => Category::CATEGORY_LOGICAL,
+ 'functionCall' => [Logical::class, 'NOT'],
+ 'argumentCount' => '1',
+ ],
+ 'NOW' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'DATETIMENOW'],
+ 'argumentCount' => '0',
+ ],
+ 'NPER' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'NPER'],
+ 'argumentCount' => '3-5',
+ ],
+ 'NPV' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'NPV'],
+ 'argumentCount' => '2+',
+ ],
+ 'NUMBERVALUE' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'NUMBERVALUE'],
+ 'argumentCount' => '1+',
+ ],
+ 'OCT2BIN' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'OCTTOBIN'],
+ 'argumentCount' => '1,2',
+ ],
+ 'OCT2DEC' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'OCTTODEC'],
+ 'argumentCount' => '1',
+ ],
+ 'OCT2HEX' => [
+ 'category' => Category::CATEGORY_ENGINEERING,
+ 'functionCall' => [Engineering::class, 'OCTTOHEX'],
+ 'argumentCount' => '1,2',
+ ],
+ 'ODD' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'ODD'],
+ 'argumentCount' => '1',
+ ],
+ 'ODDFPRICE' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '8,9',
+ ],
+ 'ODDFYIELD' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '8,9',
+ ],
+ 'ODDLPRICE' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '7,8',
+ ],
+ 'ODDLYIELD' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '7,8',
+ ],
+ 'OFFSET' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'OFFSET'],
+ 'argumentCount' => '3-5',
+ 'passCellReference' => true,
+ 'passByReference' => [true],
+ ],
+ 'OR' => [
+ 'category' => Category::CATEGORY_LOGICAL,
+ 'functionCall' => [Logical::class, 'logicalOr'],
+ 'argumentCount' => '1+',
+ ],
+ 'PDURATION' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'PDURATION'],
+ 'argumentCount' => '3',
+ ],
+ 'PEARSON' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'CORREL'],
+ 'argumentCount' => '2',
+ ],
+ 'PERCENTILE' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'PERCENTILE'],
+ 'argumentCount' => '2',
+ ],
+ 'PERCENTRANK' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'PERCENTRANK'],
+ 'argumentCount' => '2,3',
+ ],
+ 'PERMUT' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'PERMUT'],
+ 'argumentCount' => '2',
+ ],
+ 'PHONETIC' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '1',
+ ],
+ 'PI' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'pi',
+ 'argumentCount' => '0',
+ ],
+ 'PMT' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'PMT'],
+ 'argumentCount' => '3-5',
+ ],
+ 'POISSON' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'POISSON'],
+ 'argumentCount' => '3',
+ ],
+ 'POWER' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'POWER'],
+ 'argumentCount' => '2',
+ ],
+ 'PPMT' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'PPMT'],
+ 'argumentCount' => '4-6',
+ ],
+ 'PRICE' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'PRICE'],
+ 'argumentCount' => '6,7',
+ ],
+ 'PRICEDISC' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'PRICEDISC'],
+ 'argumentCount' => '4,5',
+ ],
+ 'PRICEMAT' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'PRICEMAT'],
+ 'argumentCount' => '5,6',
+ ],
+ 'PROB' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '3,4',
+ ],
+ 'PRODUCT' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'PRODUCT'],
+ 'argumentCount' => '1+',
+ ],
+ 'PROPER' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'PROPERCASE'],
+ 'argumentCount' => '1',
+ ],
+ 'PV' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'PV'],
+ 'argumentCount' => '3-5',
+ ],
+ 'QUARTILE' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'QUARTILE'],
+ 'argumentCount' => '2',
+ ],
+ 'QUOTIENT' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'QUOTIENT'],
+ 'argumentCount' => '2',
+ ],
+ 'RADIANS' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'deg2rad',
+ 'argumentCount' => '1',
+ ],
+ 'RAND' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'RAND'],
+ 'argumentCount' => '0',
+ ],
+ 'RANDBETWEEN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'RAND'],
+ 'argumentCount' => '2',
+ ],
+ 'RANK' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'RANK'],
+ 'argumentCount' => '2,3',
+ ],
+ 'RATE' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'RATE'],
+ 'argumentCount' => '3-6',
+ ],
+ 'RECEIVED' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'RECEIVED'],
+ 'argumentCount' => '4-5',
+ ],
+ 'REPLACE' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'REPLACE'],
+ 'argumentCount' => '4',
+ ],
+ 'REPLACEB' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'REPLACE'],
+ 'argumentCount' => '4',
+ ],
+ 'REPT' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => 'str_repeat',
+ 'argumentCount' => '2',
+ ],
+ 'RIGHT' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'RIGHT'],
+ 'argumentCount' => '1,2',
+ ],
+ 'RIGHTB' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'RIGHT'],
+ 'argumentCount' => '1,2',
+ ],
+ 'ROMAN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'ROMAN'],
+ 'argumentCount' => '1,2',
+ ],
+ 'ROUND' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'round',
+ 'argumentCount' => '2',
+ ],
+ 'ROUNDDOWN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'ROUNDDOWN'],
+ 'argumentCount' => '2',
+ ],
+ 'ROUNDUP' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'ROUNDUP'],
+ 'argumentCount' => '2',
+ ],
+ 'ROW' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'ROW'],
+ 'argumentCount' => '-1',
+ 'passByReference' => [true],
+ ],
+ 'ROWS' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'ROWS'],
+ 'argumentCount' => '1',
+ ],
+ 'RRI' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'RRI'],
+ 'argumentCount' => '3',
+ ],
+ 'RSQ' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'RSQ'],
+ 'argumentCount' => '2',
+ ],
+ 'RTD' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '1+',
+ ],
+ 'SEARCH' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'SEARCHINSENSITIVE'],
+ 'argumentCount' => '2,3',
+ ],
+ 'SEARCHB' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'SEARCHINSENSITIVE'],
+ 'argumentCount' => '2,3',
+ ],
+ 'SEC' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SEC'],
+ 'argumentCount' => '1',
+ ],
+ 'SECH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SECH'],
+ 'argumentCount' => '1',
+ ],
+ 'SECOND' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'SECOND'],
+ 'argumentCount' => '1',
+ ],
+ 'SERIESSUM' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SERIESSUM'],
+ 'argumentCount' => '4',
+ ],
+ 'SIGN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SIGN'],
+ 'argumentCount' => '1',
+ ],
+ 'SIN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'sin',
+ 'argumentCount' => '1',
+ ],
+ 'SINH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'sinh',
+ 'argumentCount' => '1',
+ ],
+ 'SKEW' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'SKEW'],
+ 'argumentCount' => '1+',
+ ],
+ 'SLN' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'SLN'],
+ 'argumentCount' => '3',
+ ],
+ 'SLOPE' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'SLOPE'],
+ 'argumentCount' => '2',
+ ],
+ 'SMALL' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'SMALL'],
+ 'argumentCount' => '2',
+ ],
+ 'SQRT' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'sqrt',
+ 'argumentCount' => '1',
+ ],
+ 'SQRTPI' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SQRTPI'],
+ 'argumentCount' => '1',
+ ],
+ 'STANDARDIZE' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'STANDARDIZE'],
+ 'argumentCount' => '3',
+ ],
+ 'STDEV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'STDEV'],
+ 'argumentCount' => '1+',
+ ],
+ 'STDEV.S' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'STDEV'],
+ 'argumentCount' => '1+',
+ ],
+ 'STDEV.P' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'STDEVP'],
+ 'argumentCount' => '1+',
+ ],
+ 'STDEVA' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'STDEVA'],
+ 'argumentCount' => '1+',
+ ],
+ 'STDEVP' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'STDEVP'],
+ 'argumentCount' => '1+',
+ ],
+ 'STDEVPA' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'STDEVPA'],
+ 'argumentCount' => '1+',
+ ],
+ 'STEYX' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'STEYX'],
+ 'argumentCount' => '2',
+ ],
+ 'SUBSTITUTE' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'SUBSTITUTE'],
+ 'argumentCount' => '3,4',
+ ],
+ 'SUBTOTAL' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SUBTOTAL'],
+ 'argumentCount' => '2+',
+ 'passCellReference' => true,
+ ],
+ 'SUM' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SUM'],
+ 'argumentCount' => '1+',
+ ],
+ 'SUMIF' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SUMIF'],
+ 'argumentCount' => '2,3',
+ ],
+ 'SUMIFS' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SUMIFS'],
+ 'argumentCount' => '3+',
+ ],
+ 'SUMPRODUCT' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SUMPRODUCT'],
+ 'argumentCount' => '1+',
+ ],
+ 'SUMSQ' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SUMSQ'],
+ 'argumentCount' => '1+',
+ ],
+ 'SUMX2MY2' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SUMX2MY2'],
+ 'argumentCount' => '2',
+ ],
+ 'SUMX2PY2' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SUMX2PY2'],
+ 'argumentCount' => '2',
+ ],
+ 'SUMXMY2' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'SUMXMY2'],
+ 'argumentCount' => '2',
+ ],
+ 'SYD' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'SYD'],
+ 'argumentCount' => '4',
+ ],
+ 'T' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'RETURNSTRING'],
+ 'argumentCount' => '1',
+ ],
+ 'TAN' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'tan',
+ 'argumentCount' => '1',
+ ],
+ 'TANH' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => 'tanh',
+ 'argumentCount' => '1',
+ ],
+ 'TBILLEQ' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'TBILLEQ'],
+ 'argumentCount' => '3',
+ ],
+ 'TBILLPRICE' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'TBILLPRICE'],
+ 'argumentCount' => '3',
+ ],
+ 'TBILLYIELD' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'TBILLYIELD'],
+ 'argumentCount' => '3',
+ ],
+ 'TDIST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'TDIST'],
+ 'argumentCount' => '3',
+ ],
+ 'TEXT' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'TEXTFORMAT'],
+ 'argumentCount' => '2',
+ ],
+ 'TEXTJOIN' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'TEXTJOIN'],
+ 'argumentCount' => '3+',
+ ],
+ 'TIME' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'TIME'],
+ 'argumentCount' => '3',
+ ],
+ 'TIMEVALUE' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'TIMEVALUE'],
+ 'argumentCount' => '1',
+ ],
+ 'TINV' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'TINV'],
+ 'argumentCount' => '2',
+ ],
+ 'TODAY' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'DATENOW'],
+ 'argumentCount' => '0',
+ ],
+ 'TRANSPOSE' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'TRANSPOSE'],
+ 'argumentCount' => '1',
+ ],
+ 'TREND' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'TREND'],
+ 'argumentCount' => '1-4',
+ ],
+ 'TRIM' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'TRIMSPACES'],
+ 'argumentCount' => '1',
+ ],
+ 'TRIMMEAN' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'TRIMMEAN'],
+ 'argumentCount' => '2',
+ ],
+ 'TRUE' => [
+ 'category' => Category::CATEGORY_LOGICAL,
+ 'functionCall' => [Logical::class, 'TRUE'],
+ 'argumentCount' => '0',
+ ],
+ 'TRUNC' => [
+ 'category' => Category::CATEGORY_MATH_AND_TRIG,
+ 'functionCall' => [MathTrig::class, 'TRUNC'],
+ 'argumentCount' => '1,2',
+ ],
+ 'TTEST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '4',
+ ],
+ 'TYPE' => [
+ 'category' => Category::CATEGORY_INFORMATION,
+ 'functionCall' => [Functions::class, 'TYPE'],
+ 'argumentCount' => '1',
+ ],
+ 'UNICHAR' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'CHARACTER'],
+ 'argumentCount' => '1',
+ ],
+ 'UNICODE' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'ASCIICODE'],
+ 'argumentCount' => '1',
+ ],
+ 'UPPER' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'UPPERCASE'],
+ 'argumentCount' => '1',
+ ],
+ 'USDOLLAR' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '2',
+ ],
+ 'VALUE' => [
+ 'category' => Category::CATEGORY_TEXT_AND_DATA,
+ 'functionCall' => [TextData::class, 'VALUE'],
+ 'argumentCount' => '1',
+ ],
+ 'VAR' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'VARFunc'],
+ 'argumentCount' => '1+',
+ ],
+ 'VAR.P' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'VARP'],
+ 'argumentCount' => '1+',
+ ],
+ 'VAR.S' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'VARFunc'],
+ 'argumentCount' => '1+',
+ ],
+ 'VARA' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'VARA'],
+ 'argumentCount' => '1+',
+ ],
+ 'VARP' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'VARP'],
+ 'argumentCount' => '1+',
+ ],
+ 'VARPA' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'VARPA'],
+ 'argumentCount' => '1+',
+ ],
+ 'VDB' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '5-7',
+ ],
+ 'VLOOKUP' => [
+ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
+ 'functionCall' => [LookupRef::class, 'VLOOKUP'],
+ 'argumentCount' => '3,4',
+ ],
+ 'WEEKDAY' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'WEEKDAY'],
+ 'argumentCount' => '1,2',
+ ],
+ 'WEEKNUM' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'WEEKNUM'],
+ 'argumentCount' => '1,2',
+ ],
+ 'WEIBULL' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'WEIBULL'],
+ 'argumentCount' => '4',
+ ],
+ 'WORKDAY' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'WORKDAY'],
+ 'argumentCount' => '2+',
+ ],
+ 'XIRR' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'XIRR'],
+ 'argumentCount' => '2,3',
+ ],
+ 'XNPV' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'XNPV'],
+ 'argumentCount' => '3',
+ ],
+ 'XOR' => [
+ 'category' => Category::CATEGORY_LOGICAL,
+ 'functionCall' => [Logical::class, 'logicalXor'],
+ 'argumentCount' => '1+',
+ ],
+ 'YEAR' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'YEAR'],
+ 'argumentCount' => '1',
+ ],
+ 'YEARFRAC' => [
+ 'category' => Category::CATEGORY_DATE_AND_TIME,
+ 'functionCall' => [DateTime::class, 'YEARFRAC'],
+ 'argumentCount' => '2,3',
+ ],
+ 'YIELD' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Functions::class, 'DUMMY'],
+ 'argumentCount' => '6,7',
+ ],
+ 'YIELDDISC' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'YIELDDISC'],
+ 'argumentCount' => '4,5',
+ ],
+ 'YIELDMAT' => [
+ 'category' => Category::CATEGORY_FINANCIAL,
+ 'functionCall' => [Financial::class, 'YIELDMAT'],
+ 'argumentCount' => '5,6',
+ ],
+ 'ZTEST' => [
+ 'category' => Category::CATEGORY_STATISTICAL,
+ 'functionCall' => [Statistical::class, 'ZTEST'],
+ 'argumentCount' => '2-3',
+ ],
+ ];
+
+ // Internal functions used for special control purposes
+ private static $controlFunctions = [
+ 'MKMATRIX' => [
+ 'argumentCount' => '*',
+ 'functionCall' => 'self::mkMatrix',
+ ],
+ ];
+
+ public function __construct(Spreadsheet $spreadsheet = null)
+ {
+ $this->delta = 1 * pow(10, 0 - ini_get('precision'));
+
+ $this->spreadsheet = $spreadsheet;
+ $this->cyclicReferenceStack = new CyclicReferenceStack();
+ $this->debugLog = new Logger($this->cyclicReferenceStack);
+ }
+
+ private static function loadLocales()
+ {
+ $localeFileDirectory = __DIR__ . '/locale/';
+ foreach (glob($localeFileDirectory . '/*', GLOB_ONLYDIR) as $filename) {
+ $filename = substr($filename, strlen($localeFileDirectory) + 1);
+ if ($filename != 'en') {
+ self::$validLocaleLanguages[] = $filename;
+ }
+ }
+ }
+
+ /**
+ * Get an instance of this class.
+ *
+ * @param Spreadsheet $spreadsheet Injected spreadsheet for working with a PhpSpreadsheet Spreadsheet object,
+ * or NULL to create a standalone claculation engine
+ *
+ * @return Calculation
+ */
+ public static function getInstance(Spreadsheet $spreadsheet = null)
+ {
+ if ($spreadsheet !== null) {
+ $instance = $spreadsheet->getCalculationEngine();
+ if (isset($instance)) {
+ return $instance;
+ }
+ }
+
+ if (!isset(self::$instance) || (self::$instance === null)) {
+ self::$instance = new self();
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Flush the calculation cache for any existing instance of this class
+ * but only if a Calculation instance exists.
+ */
+ public function flushInstance()
+ {
+ $this->clearCalculationCache();
+ }
+
+ /**
+ * Get the Logger for this calculation engine instance.
+ *
+ * @return Logger
+ */
+ public function getDebugLog()
+ {
+ return $this->debugLog;
+ }
+
+ /**
+ * __clone implementation. Cloning should not be allowed in a Singleton!
+ *
+ * @throws Exception
+ */
+ final public function __clone()
+ {
+ throw new Exception('Cloning the calculation engine is not allowed!');
+ }
+
+ /**
+ * Return the locale-specific translation of TRUE.
+ *
+ * @return string locale-specific translation of TRUE
+ */
+ public static function getTRUE()
+ {
+ return self::$localeBoolean['TRUE'];
+ }
+
+ /**
+ * Return the locale-specific translation of FALSE.
+ *
+ * @return string locale-specific translation of FALSE
+ */
+ public static function getFALSE()
+ {
+ return self::$localeBoolean['FALSE'];
+ }
+
+ /**
+ * Set the Array Return Type (Array or Value of first element in the array).
+ *
+ * @param string $returnType Array return type
+ *
+ * @return bool Success or failure
+ */
+ public static function setArrayReturnType($returnType)
+ {
+ if (($returnType == self::RETURN_ARRAY_AS_VALUE) ||
+ ($returnType == self::RETURN_ARRAY_AS_ERROR) ||
+ ($returnType == self::RETURN_ARRAY_AS_ARRAY)) {
+ self::$returnArrayAsType = $returnType;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return the Array Return Type (Array or Value of first element in the array).
+ *
+ * @return string $returnType Array return type
+ */
+ public static function getArrayReturnType()
+ {
+ return self::$returnArrayAsType;
+ }
+
+ /**
+ * Is calculation caching enabled?
+ *
+ * @return bool
+ */
+ public function getCalculationCacheEnabled()
+ {
+ return $this->calculationCacheEnabled;
+ }
+
+ /**
+ * Enable/disable calculation cache.
+ *
+ * @param bool $pValue
+ */
+ public function setCalculationCacheEnabled($pValue)
+ {
+ $this->calculationCacheEnabled = $pValue;
+ $this->clearCalculationCache();
+ }
+
+ /**
+ * Enable calculation cache.
+ */
+ public function enableCalculationCache()
+ {
+ $this->setCalculationCacheEnabled(true);
+ }
+
+ /**
+ * Disable calculation cache.
+ */
+ public function disableCalculationCache()
+ {
+ $this->setCalculationCacheEnabled(false);
+ }
+
+ /**
+ * Clear calculation cache.
+ */
+ public function clearCalculationCache()
+ {
+ $this->calculationCache = [];
+ }
+
+ /**
+ * Clear calculation cache for a specified worksheet.
+ *
+ * @param string $worksheetName
+ */
+ public function clearCalculationCacheForWorksheet($worksheetName)
+ {
+ if (isset($this->calculationCache[$worksheetName])) {
+ unset($this->calculationCache[$worksheetName]);
+ }
+ }
+
+ /**
+ * Rename calculation cache for a specified worksheet.
+ *
+ * @param string $fromWorksheetName
+ * @param string $toWorksheetName
+ */
+ public function renameCalculationCacheForWorksheet($fromWorksheetName, $toWorksheetName)
+ {
+ if (isset($this->calculationCache[$fromWorksheetName])) {
+ $this->calculationCache[$toWorksheetName] = &$this->calculationCache[$fromWorksheetName];
+ unset($this->calculationCache[$fromWorksheetName]);
+ }
+ }
+
+ /**
+ * Get the currently defined locale code.
+ *
+ * @return string
+ */
+ public function getLocale()
+ {
+ return self::$localeLanguage;
+ }
+
+ /**
+ * Set the locale code.
+ *
+ * @param string $locale The locale to use for formula translation, eg: 'en_us'
+ *
+ * @return bool
+ */
+ public function setLocale($locale)
+ {
+ // Identify our locale and language
+ $language = $locale = strtolower($locale);
+ if (strpos($locale, '_') !== false) {
+ list($language) = explode('_', $locale);
+ }
+
+ if (count(self::$validLocaleLanguages) == 1) {
+ self::loadLocales();
+ }
+ // Test whether we have any language data for this language (any locale)
+ if (in_array($language, self::$validLocaleLanguages)) {
+ // initialise language/locale settings
+ self::$localeFunctions = [];
+ self::$localeArgumentSeparator = ',';
+ self::$localeBoolean = ['TRUE' => 'TRUE', 'FALSE' => 'FALSE', 'NULL' => 'NULL'];
+ // Default is English, if user isn't requesting english, then read the necessary data from the locale files
+ if ($locale != 'en_us') {
+ // Search for a file with a list of function names for locale
+ $functionNamesFile = __DIR__ . '/locale/' . str_replace('_', DIRECTORY_SEPARATOR, $locale) . DIRECTORY_SEPARATOR . 'functions';
+ if (!file_exists($functionNamesFile)) {
+ // If there isn't a locale specific function file, look for a language specific function file
+ $functionNamesFile = __DIR__ . '/locale/' . $language . DIRECTORY_SEPARATOR . 'functions';
+ if (!file_exists($functionNamesFile)) {
+ return false;
+ }
+ }
+ // Retrieve the list of locale or language specific function names
+ $localeFunctions = file($functionNamesFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ foreach ($localeFunctions as $localeFunction) {
+ list($localeFunction) = explode('##', $localeFunction); // Strip out comments
+ if (strpos($localeFunction, '=') !== false) {
+ list($fName, $lfName) = explode('=', $localeFunction);
+ $fName = trim($fName);
+ $lfName = trim($lfName);
+ if ((isset(self::$phpSpreadsheetFunctions[$fName])) && ($lfName != '') && ($fName != $lfName)) {
+ self::$localeFunctions[$fName] = $lfName;
+ }
+ }
+ }
+ // Default the TRUE and FALSE constants to the locale names of the TRUE() and FALSE() functions
+ if (isset(self::$localeFunctions['TRUE'])) {
+ self::$localeBoolean['TRUE'] = self::$localeFunctions['TRUE'];
+ }
+ if (isset(self::$localeFunctions['FALSE'])) {
+ self::$localeBoolean['FALSE'] = self::$localeFunctions['FALSE'];
+ }
+
+ $configFile = __DIR__ . '/locale/' . str_replace('_', DIRECTORY_SEPARATOR, $locale) . DIRECTORY_SEPARATOR . 'config';
+ if (!file_exists($configFile)) {
+ $configFile = __DIR__ . '/locale/' . $language . DIRECTORY_SEPARATOR . 'config';
+ }
+ if (file_exists($configFile)) {
+ $localeSettings = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ foreach ($localeSettings as $localeSetting) {
+ list($localeSetting) = explode('##', $localeSetting); // Strip out comments
+ if (strpos($localeSetting, '=') !== false) {
+ list($settingName, $settingValue) = explode('=', $localeSetting);
+ $settingName = strtoupper(trim($settingName));
+ switch ($settingName) {
+ case 'ARGUMENTSEPARATOR':
+ self::$localeArgumentSeparator = trim($settingValue);
+
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ self::$functionReplaceFromExcel = self::$functionReplaceToExcel =
+ self::$functionReplaceFromLocale = self::$functionReplaceToLocale = null;
+ self::$localeLanguage = $locale;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * @param string $fromSeparator
+ * @param string $toSeparator
+ * @param string $formula
+ * @param bool $inBraces
+ *
+ * @return string
+ */
+ public static function translateSeparator($fromSeparator, $toSeparator, $formula, &$inBraces)
+ {
+ $strlen = mb_strlen($formula);
+ for ($i = 0; $i < $strlen; ++$i) {
+ $chr = mb_substr($formula, $i, 1);
+ switch ($chr) {
+ case '{':
+ $inBraces = true;
+
+ break;
+ case '}':
+ $inBraces = false;
+
+ break;
+ case $fromSeparator:
+ if (!$inBraces) {
+ $formula = mb_substr($formula, 0, $i) . $toSeparator . mb_substr($formula, $i + 1);
+ }
+ }
+ }
+
+ return $formula;
+ }
+
+ /**
+ * @param string[] $from
+ * @param string[] $to
+ * @param string $formula
+ * @param string $fromSeparator
+ * @param string $toSeparator
+ *
+ * @return string
+ */
+ private static function translateFormula(array $from, array $to, $formula, $fromSeparator, $toSeparator)
+ {
+ // Convert any Excel function names to the required language
+ if (self::$localeLanguage !== 'en_us') {
+ $inBraces = false;
+ // If there is the possibility of braces within a quoted string, then we don't treat those as matrix indicators
+ if (strpos($formula, '"') !== false) {
+ // So instead we skip replacing in any quoted strings by only replacing in every other array element after we've exploded
+ // the formula
+ $temp = explode('"', $formula);
+ $i = false;
+ foreach ($temp as &$value) {
+ // Only count/replace in alternating array entries
+ if ($i = !$i) {
+ $value = preg_replace($from, $to, $value);
+ $value = self::translateSeparator($fromSeparator, $toSeparator, $value, $inBraces);
+ }
+ }
+ unset($value);
+ // Then rebuild the formula string
+ $formula = implode('"', $temp);
+ } else {
+ // If there's no quoted strings, then we do a simple count/replace
+ $formula = preg_replace($from, $to, $formula);
+ $formula = self::translateSeparator($fromSeparator, $toSeparator, $formula, $inBraces);
+ }
+ }
+
+ return $formula;
+ }
+
+ private static $functionReplaceFromExcel = null;
+
+ private static $functionReplaceToLocale = null;
+
+ public function _translateFormulaToLocale($formula)
+ {
+ if (self::$functionReplaceFromExcel === null) {
+ self::$functionReplaceFromExcel = [];
+ foreach (array_keys(self::$localeFunctions) as $excelFunctionName) {
+ self::$functionReplaceFromExcel[] = '/(@?[^\w\.])' . preg_quote($excelFunctionName, '/') . '([\s]*\()/Ui';
+ }
+ foreach (array_keys(self::$localeBoolean) as $excelBoolean) {
+ self::$functionReplaceFromExcel[] = '/(@?[^\w\.])' . preg_quote($excelBoolean, '/') . '([^\w\.])/Ui';
+ }
+ }
+
+ if (self::$functionReplaceToLocale === null) {
+ self::$functionReplaceToLocale = [];
+ foreach (self::$localeFunctions as $localeFunctionName) {
+ self::$functionReplaceToLocale[] = '$1' . trim($localeFunctionName) . '$2';
+ }
+ foreach (self::$localeBoolean as $localeBoolean) {
+ self::$functionReplaceToLocale[] = '$1' . trim($localeBoolean) . '$2';
+ }
+ }
+
+ return self::translateFormula(self::$functionReplaceFromExcel, self::$functionReplaceToLocale, $formula, ',', self::$localeArgumentSeparator);
+ }
+
+ private static $functionReplaceFromLocale = null;
+
+ private static $functionReplaceToExcel = null;
+
+ public function _translateFormulaToEnglish($formula)
+ {
+ if (self::$functionReplaceFromLocale === null) {
+ self::$functionReplaceFromLocale = [];
+ foreach (self::$localeFunctions as $localeFunctionName) {
+ self::$functionReplaceFromLocale[] = '/(@?[^\w\.])' . preg_quote($localeFunctionName, '/') . '([\s]*\()/Ui';
+ }
+ foreach (self::$localeBoolean as $excelBoolean) {
+ self::$functionReplaceFromLocale[] = '/(@?[^\w\.])' . preg_quote($excelBoolean, '/') . '([^\w\.])/Ui';
+ }
+ }
+
+ if (self::$functionReplaceToExcel === null) {
+ self::$functionReplaceToExcel = [];
+ foreach (array_keys(self::$localeFunctions) as $excelFunctionName) {
+ self::$functionReplaceToExcel[] = '$1' . trim($excelFunctionName) . '$2';
+ }
+ foreach (array_keys(self::$localeBoolean) as $excelBoolean) {
+ self::$functionReplaceToExcel[] = '$1' . trim($excelBoolean) . '$2';
+ }
+ }
+
+ return self::translateFormula(self::$functionReplaceFromLocale, self::$functionReplaceToExcel, $formula, self::$localeArgumentSeparator, ',');
+ }
+
+ public static function localeFunc($function)
+ {
+ if (self::$localeLanguage !== 'en_us') {
+ $functionName = trim($function, '(');
+ if (isset(self::$localeFunctions[$functionName])) {
+ $brace = ($functionName != $function);
+ $function = self::$localeFunctions[$functionName];
+ if ($brace) {
+ $function .= '(';
+ }
+ }
+ }
+
+ return $function;
+ }
+
+ /**
+ * Wrap string values in quotes.
+ *
+ * @param mixed $value
+ *
+ * @return mixed
+ */
+ public static function wrapResult($value)
+ {
+ if (is_string($value)) {
+ // Error values cannot be "wrapped"
+ if (preg_match('/^' . self::CALCULATION_REGEXP_ERROR . '$/i', $value, $match)) {
+ // Return Excel errors "as is"
+ return $value;
+ }
+ // Return strings wrapped in quotes
+ return '"' . $value . '"';
+ // Convert numeric errors to NaN error
+ } elseif ((is_float($value)) && ((is_nan($value)) || (is_infinite($value)))) {
+ return Functions::NAN();
+ }
+
+ return $value;
+ }
+
+ /**
+ * Remove quotes used as a wrapper to identify string values.
+ *
+ * @param mixed $value
+ *
+ * @return mixed
+ */
+ public static function unwrapResult($value)
+ {
+ if (is_string($value)) {
+ if ((isset($value[0])) && ($value[0] == '"') && (substr($value, -1) == '"')) {
+ return substr($value, 1, -1);
+ }
+ // Convert numeric errors to NAN error
+ } elseif ((is_float($value)) && ((is_nan($value)) || (is_infinite($value)))) {
+ return Functions::NAN();
+ }
+
+ return $value;
+ }
+
+ /**
+ * Calculate cell value (using formula from a cell ID)
+ * Retained for backward compatibility.
+ *
+ * @param Cell $pCell Cell to calculate
+ *
+ * @throws Exception
+ *
+ * @return mixed
+ */
+ public function calculate(Cell $pCell = null)
+ {
+ try {
+ return $this->calculateCellValue($pCell);
+ } catch (\Exception $e) {
+ throw new Exception($e->getMessage());
+ }
+ }
+
+ /**
+ * Calculate the value of a cell formula.
+ *
+ * @param Cell $pCell Cell to calculate
+ * @param bool $resetLog Flag indicating whether the debug log should be reset or not
+ *
+ * @throws Exception
+ *
+ * @return mixed
+ */
+ public function calculateCellValue(Cell $pCell = null, $resetLog = true)
+ {
+ if ($pCell === null) {
+ return null;
+ }
+
+ $returnArrayAsType = self::$returnArrayAsType;
+ if ($resetLog) {
+ // Initialise the logging settings if requested
+ $this->formulaError = null;
+ $this->debugLog->clearLog();
+ $this->cyclicReferenceStack->clear();
+ $this->cyclicFormulaCounter = 1;
+
+ self::$returnArrayAsType = self::RETURN_ARRAY_AS_ARRAY;
+ }
+
+ // Execute the calculation for the cell formula
+ $this->cellStack[] = [
+ 'sheet' => $pCell->getWorksheet()->getTitle(),
+ 'cell' => $pCell->getCoordinate(),
+ ];
+
+ try {
+ $result = self::unwrapResult($this->_calculateFormulaValue($pCell->getValue(), $pCell->getCoordinate(), $pCell));
+ $cellAddress = array_pop($this->cellStack);
+ $this->spreadsheet->getSheetByName($cellAddress['sheet'])->getCell($cellAddress['cell']);
+ } catch (\Exception $e) {
+ $cellAddress = array_pop($this->cellStack);
+ $this->spreadsheet->getSheetByName($cellAddress['sheet'])->getCell($cellAddress['cell']);
+
+ throw new Exception($e->getMessage());
+ }
+
+ if ((is_array($result)) && (self::$returnArrayAsType != self::RETURN_ARRAY_AS_ARRAY)) {
+ self::$returnArrayAsType = $returnArrayAsType;
+ $testResult = Functions::flattenArray($result);
+ if (self::$returnArrayAsType == self::RETURN_ARRAY_AS_ERROR) {
+ return Functions::VALUE();
+ }
+ // If there's only a single cell in the array, then we allow it
+ if (count($testResult) != 1) {
+ // If keys are numeric, then it's a matrix result rather than a cell range result, so we permit it
+ $r = array_keys($result);
+ $r = array_shift($r);
+ if (!is_numeric($r)) {
+ return Functions::VALUE();
+ }
+ if (is_array($result[$r])) {
+ $c = array_keys($result[$r]);
+ $c = array_shift($c);
+ if (!is_numeric($c)) {
+ return Functions::VALUE();
+ }
+ }
+ }
+ $result = array_shift($testResult);
+ }
+ self::$returnArrayAsType = $returnArrayAsType;
+
+ if ($result === null) {
+ return 0;
+ } elseif ((is_float($result)) && ((is_nan($result)) || (is_infinite($result)))) {
+ return Functions::NAN();
+ }
+
+ return $result;
+ }
+
+ /**
+ * Validate and parse a formula string.
+ *
+ * @param string $formula Formula to parse
+ *
+ * @return array|bool
+ */
+ public function parseFormula($formula)
+ {
+ // Basic validation that this is indeed a formula
+ // We return an empty array if not
+ $formula = trim($formula);
+ if ((!isset($formula[0])) || ($formula[0] != '=')) {
+ return [];
+ }
+ $formula = ltrim(substr($formula, 1));
+ if (!isset($formula[0])) {
+ return [];
+ }
+
+ // Parse the formula and return the token stack
+ return $this->_parseFormula($formula);
+ }
+
+ /**
+ * Calculate the value of a formula.
+ *
+ * @param string $formula Formula to parse
+ * @param string $cellID Address of the cell to calculate
+ * @param Cell $pCell Cell to calculate
+ *
+ * @throws Exception
+ *
+ * @return mixed
+ */
+ public function calculateFormula($formula, $cellID = null, Cell $pCell = null)
+ {
+ // Initialise the logging settings
+ $this->formulaError = null;
+ $this->debugLog->clearLog();
+ $this->cyclicReferenceStack->clear();
+
+ if ($this->spreadsheet !== null && $cellID === null && $pCell === null) {
+ $cellID = 'A1';
+ $pCell = $this->spreadsheet->getActiveSheet()->getCell($cellID);
+ } else {
+ // Disable calculation cacheing because it only applies to cell calculations, not straight formulae
+ // But don't actually flush any cache
+ $resetCache = $this->getCalculationCacheEnabled();
+ $this->calculationCacheEnabled = false;
+ }
+
+ // Execute the calculation
+ try {
+ $result = self::unwrapResult($this->_calculateFormulaValue($formula, $cellID, $pCell));
+ } catch (\Exception $e) {
+ throw new Exception($e->getMessage());
+ }
+
+ if ($this->spreadsheet === null) {
+ // Reset calculation cacheing to its previous state
+ $this->calculationCacheEnabled = $resetCache;
+ }
+
+ return $result;
+ }
+
+ /**
+ * @param string $cellReference
+ * @param mixed $cellValue
+ *
+ * @return bool
+ */
+ public function getValueFromCache($cellReference, &$cellValue)
+ {
+ // Is calculation cacheing enabled?
+ // Is the value present in calculation cache?
+ $this->debugLog->writeDebugLog('Testing cache value for cell ', $cellReference);
+ if (($this->calculationCacheEnabled) && (isset($this->calculationCache[$cellReference]))) {
+ $this->debugLog->writeDebugLog('Retrieving value for cell ', $cellReference, ' from cache');
+ // Return the cached result
+ $cellValue = $this->calculationCache[$cellReference];
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * @param string $cellReference
+ * @param mixed $cellValue
+ */
+ public function saveValueToCache($cellReference, $cellValue)
+ {
+ if ($this->calculationCacheEnabled) {
+ $this->calculationCache[$cellReference] = $cellValue;
+ }
+ }
+
+ /**
+ * Parse a cell formula and calculate its value.
+ *
+ * @param string $formula The formula to parse and calculate
+ * @param string $cellID The ID (e.g. A3) of the cell that we are calculating
+ * @param Cell $pCell Cell to calculate
+ *
+ * @throws Exception
+ *
+ * @return mixed
+ */
+ public function _calculateFormulaValue($formula, $cellID = null, Cell $pCell = null)
+ {
+ $cellValue = null;
+
+ // Basic validation that this is indeed a formula
+ // We simply return the cell value if not
+ $formula = trim($formula);
+ if ($formula[0] != '=') {
+ return self::wrapResult($formula);
+ }
+ $formula = ltrim(substr($formula, 1));
+ if (!isset($formula[0])) {
+ return self::wrapResult($formula);
+ }
+
+ $pCellParent = ($pCell !== null) ? $pCell->getWorksheet() : null;
+ $wsTitle = ($pCellParent !== null) ? $pCellParent->getTitle() : "\x00Wrk";
+ $wsCellReference = $wsTitle . '!' . $cellID;
+
+ if (($cellID !== null) && ($this->getValueFromCache($wsCellReference, $cellValue))) {
+ return $cellValue;
+ }
+
+ if (($wsTitle[0] !== "\x00") && ($this->cyclicReferenceStack->onStack($wsCellReference))) {
+ if ($this->cyclicFormulaCount <= 0) {
+ $this->cyclicFormulaCell = '';
+
+ return $this->raiseFormulaError('Cyclic Reference in Formula');
+ } elseif ($this->cyclicFormulaCell === $wsCellReference) {
+ ++$this->cyclicFormulaCounter;
+ if ($this->cyclicFormulaCounter >= $this->cyclicFormulaCount) {
+ $this->cyclicFormulaCell = '';
+
+ return $cellValue;
+ }
+ } elseif ($this->cyclicFormulaCell == '') {
+ if ($this->cyclicFormulaCounter >= $this->cyclicFormulaCount) {
+ return $cellValue;
+ }
+ $this->cyclicFormulaCell = $wsCellReference;
+ }
+ }
+
+ // Parse the formula onto the token stack and calculate the value
+ $this->cyclicReferenceStack->push($wsCellReference);
+ $cellValue = $this->processTokenStack($this->_parseFormula($formula, $pCell), $cellID, $pCell);
+ $this->cyclicReferenceStack->pop();
+
+ // Save to calculation cache
+ if ($cellID !== null) {
+ $this->saveValueToCache($wsCellReference, $cellValue);
+ }
+
+ // Return the calculated value
+ return $cellValue;
+ }
+
+ /**
+ * Ensure that paired matrix operands are both matrices and of the same size.
+ *
+ * @param mixed &$operand1 First matrix operand
+ * @param mixed &$operand2 Second matrix operand
+ * @param int $resize Flag indicating whether the matrices should be resized to match
+ * and (if so), whether the smaller dimension should grow or the
+ * larger should shrink.
+ * 0 = no resize
+ * 1 = shrink to fit
+ * 2 = extend to fit
+ *
+ * @return array
+ */
+ private static function checkMatrixOperands(&$operand1, &$operand2, $resize = 1)
+ {
+ // Examine each of the two operands, and turn them into an array if they aren't one already
+ // Note that this function should only be called if one or both of the operand is already an array
+ if (!is_array($operand1)) {
+ list($matrixRows, $matrixColumns) = self::getMatrixDimensions($operand2);
+ $operand1 = array_fill(0, $matrixRows, array_fill(0, $matrixColumns, $operand1));
+ $resize = 0;
+ } elseif (!is_array($operand2)) {
+ list($matrixRows, $matrixColumns) = self::getMatrixDimensions($operand1);
+ $operand2 = array_fill(0, $matrixRows, array_fill(0, $matrixColumns, $operand2));
+ $resize = 0;
+ }
+
+ list($matrix1Rows, $matrix1Columns) = self::getMatrixDimensions($operand1);
+ list($matrix2Rows, $matrix2Columns) = self::getMatrixDimensions($operand2);
+ if (($matrix1Rows == $matrix2Columns) && ($matrix2Rows == $matrix1Columns)) {
+ $resize = 1;
+ }
+
+ if ($resize == 2) {
+ // Given two matrices of (potentially) unequal size, convert the smaller in each dimension to match the larger
+ self::resizeMatricesExtend($operand1, $operand2, $matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns);
+ } elseif ($resize == 1) {
+ // Given two matrices of (potentially) unequal size, convert the larger in each dimension to match the smaller
+ self::resizeMatricesShrink($operand1, $operand2, $matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns);
+ }
+
+ return [$matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns];
+ }
+
+ /**
+ * Read the dimensions of a matrix, and re-index it with straight numeric keys starting from row 0, column 0.
+ *
+ * @param array &$matrix matrix operand
+ *
+ * @return int[] An array comprising the number of rows, and number of columns
+ */
+ public static function getMatrixDimensions(array &$matrix)
+ {
+ $matrixRows = count($matrix);
+ $matrixColumns = 0;
+ foreach ($matrix as $rowKey => $rowValue) {
+ if (!is_array($rowValue)) {
+ $matrix[$rowKey] = [$rowValue];
+ $matrixColumns = max(1, $matrixColumns);
+ } else {
+ $matrix[$rowKey] = array_values($rowValue);
+ $matrixColumns = max(count($rowValue), $matrixColumns);
+ }
+ }
+ $matrix = array_values($matrix);
+
+ return [$matrixRows, $matrixColumns];
+ }
+
+ /**
+ * Ensure that paired matrix operands are both matrices of the same size.
+ *
+ * @param mixed &$matrix1 First matrix operand
+ * @param mixed &$matrix2 Second matrix operand
+ * @param int $matrix1Rows Row size of first matrix operand
+ * @param int $matrix1Columns Column size of first matrix operand
+ * @param int $matrix2Rows Row size of second matrix operand
+ * @param int $matrix2Columns Column size of second matrix operand
+ */
+ private static function resizeMatricesShrink(&$matrix1, &$matrix2, $matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns)
+ {
+ if (($matrix2Columns < $matrix1Columns) || ($matrix2Rows < $matrix1Rows)) {
+ if ($matrix2Rows < $matrix1Rows) {
+ for ($i = $matrix2Rows; $i < $matrix1Rows; ++$i) {
+ unset($matrix1[$i]);
+ }
+ }
+ if ($matrix2Columns < $matrix1Columns) {
+ for ($i = 0; $i < $matrix1Rows; ++$i) {
+ for ($j = $matrix2Columns; $j < $matrix1Columns; ++$j) {
+ unset($matrix1[$i][$j]);
+ }
+ }
+ }
+ }
+
+ if (($matrix1Columns < $matrix2Columns) || ($matrix1Rows < $matrix2Rows)) {
+ if ($matrix1Rows < $matrix2Rows) {
+ for ($i = $matrix1Rows; $i < $matrix2Rows; ++$i) {
+ unset($matrix2[$i]);
+ }
+ }
+ if ($matrix1Columns < $matrix2Columns) {
+ for ($i = 0; $i < $matrix2Rows; ++$i) {
+ for ($j = $matrix1Columns; $j < $matrix2Columns; ++$j) {
+ unset($matrix2[$i][$j]);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Ensure that paired matrix operands are both matrices of the same size.
+ *
+ * @param mixed &$matrix1 First matrix operand
+ * @param mixed &$matrix2 Second matrix operand
+ * @param int $matrix1Rows Row size of first matrix operand
+ * @param int $matrix1Columns Column size of first matrix operand
+ * @param int $matrix2Rows Row size of second matrix operand
+ * @param int $matrix2Columns Column size of second matrix operand
+ */
+ private static function resizeMatricesExtend(&$matrix1, &$matrix2, $matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns)
+ {
+ if (($matrix2Columns < $matrix1Columns) || ($matrix2Rows < $matrix1Rows)) {
+ if ($matrix2Columns < $matrix1Columns) {
+ for ($i = 0; $i < $matrix2Rows; ++$i) {
+ $x = $matrix2[$i][$matrix2Columns - 1];
+ for ($j = $matrix2Columns; $j < $matrix1Columns; ++$j) {
+ $matrix2[$i][$j] = $x;
+ }
+ }
+ }
+ if ($matrix2Rows < $matrix1Rows) {
+ $x = $matrix2[$matrix2Rows - 1];
+ for ($i = 0; $i < $matrix1Rows; ++$i) {
+ $matrix2[$i] = $x;
+ }
+ }
+ }
+
+ if (($matrix1Columns < $matrix2Columns) || ($matrix1Rows < $matrix2Rows)) {
+ if ($matrix1Columns < $matrix2Columns) {
+ for ($i = 0; $i < $matrix1Rows; ++$i) {
+ $x = $matrix1[$i][$matrix1Columns - 1];
+ for ($j = $matrix1Columns; $j < $matrix2Columns; ++$j) {
+ $matrix1[$i][$j] = $x;
+ }
+ }
+ }
+ if ($matrix1Rows < $matrix2Rows) {
+ $x = $matrix1[$matrix1Rows - 1];
+ for ($i = 0; $i < $matrix2Rows; ++$i) {
+ $matrix1[$i] = $x;
+ }
+ }
+ }
+ }
+
+ /**
+ * Format details of an operand for display in the log (based on operand type).
+ *
+ * @param mixed $value First matrix operand
+ *
+ * @return mixed
+ */
+ private function showValue($value)
+ {
+ if ($this->debugLog->getWriteDebugLog()) {
+ $testArray = Functions::flattenArray($value);
+ if (count($testArray) == 1) {
+ $value = array_pop($testArray);
+ }
+
+ if (is_array($value)) {
+ $returnMatrix = [];
+ $pad = $rpad = ', ';
+ foreach ($value as $row) {
+ if (is_array($row)) {
+ $returnMatrix[] = implode($pad, array_map([$this, 'showValue'], $row));
+ $rpad = '; ';
+ } else {
+ $returnMatrix[] = $this->showValue($row);
+ }
+ }
+
+ return '{ ' . implode($rpad, $returnMatrix) . ' }';
+ } elseif (is_string($value) && (trim($value, '"') == $value)) {
+ return '"' . $value . '"';
+ } elseif (is_bool($value)) {
+ return ($value) ? self::$localeBoolean['TRUE'] : self::$localeBoolean['FALSE'];
+ }
+ }
+
+ return Functions::flattenSingleValue($value);
+ }
+
+ /**
+ * Format type and details of an operand for display in the log (based on operand type).
+ *
+ * @param mixed $value First matrix operand
+ *
+ * @return null|string
+ */
+ private function showTypeDetails($value)
+ {
+ if ($this->debugLog->getWriteDebugLog()) {
+ $testArray = Functions::flattenArray($value);
+ if (count($testArray) == 1) {
+ $value = array_pop($testArray);
+ }
+
+ if ($value === null) {
+ return 'a NULL value';
+ } elseif (is_float($value)) {
+ $typeString = 'a floating point number';
+ } elseif (is_int($value)) {
+ $typeString = 'an integer number';
+ } elseif (is_bool($value)) {
+ $typeString = 'a boolean';
+ } elseif (is_array($value)) {
+ $typeString = 'a matrix';
+ } else {
+ if ($value == '') {
+ return 'an empty string';
+ } elseif ($value[0] == '#') {
+ return 'a ' . $value . ' error';
+ }
+ $typeString = 'a string';
+ }
+
+ return $typeString . ' with a value of ' . $this->showValue($value);
+ }
+ }
+
+ /**
+ * @param string $formula
+ *
+ * @return string
+ */
+ private function convertMatrixReferences($formula)
+ {
+ static $matrixReplaceFrom = ['{', ';', '}'];
+ static $matrixReplaceTo = ['MKMATRIX(MKMATRIX(', '),MKMATRIX(', '))'];
+
+ // Convert any Excel matrix references to the MKMATRIX() function
+ if (strpos($formula, '{') !== false) {
+ // If there is the possibility of braces within a quoted string, then we don't treat those as matrix indicators
+ if (strpos($formula, '"') !== false) {
+ // So instead we skip replacing in any quoted strings by only replacing in every other array element after we've exploded
+ // the formula
+ $temp = explode('"', $formula);
+ // Open and Closed counts used for trapping mismatched braces in the formula
+ $openCount = $closeCount = 0;
+ $i = false;
+ foreach ($temp as &$value) {
+ // Only count/replace in alternating array entries
+ if ($i = !$i) {
+ $openCount += substr_count($value, '{');
+ $closeCount += substr_count($value, '}');
+ $value = str_replace($matrixReplaceFrom, $matrixReplaceTo, $value);
+ }
+ }
+ unset($value);
+ // Then rebuild the formula string
+ $formula = implode('"', $temp);
+ } else {
+ // If there's no quoted strings, then we do a simple count/replace
+ $openCount = substr_count($formula, '{');
+ $closeCount = substr_count($formula, '}');
+ $formula = str_replace($matrixReplaceFrom, $matrixReplaceTo, $formula);
+ }
+ // Trap for mismatched braces and trigger an appropriate error
+ if ($openCount < $closeCount) {
+ if ($openCount > 0) {
+ return $this->raiseFormulaError("Formula Error: Mismatched matrix braces '}'");
+ }
+
+ return $this->raiseFormulaError("Formula Error: Unexpected '}' encountered");
+ } elseif ($openCount > $closeCount) {
+ if ($closeCount > 0) {
+ return $this->raiseFormulaError("Formula Error: Mismatched matrix braces '{'");
+ }
+
+ return $this->raiseFormulaError("Formula Error: Unexpected '{' encountered");
+ }
+ }
+
+ return $formula;
+ }
+
+ private static function mkMatrix(...$args)
+ {
+ return $args;
+ }
+
+ // Binary Operators
+ // These operators always work on two values
+ // Array key is the operator, the value indicates whether this is a left or right associative operator
+ private static $operatorAssociativity = [
+ '^' => 0, // Exponentiation
+ '*' => 0, '/' => 0, // Multiplication and Division
+ '+' => 0, '-' => 0, // Addition and Subtraction
+ '&' => 0, // Concatenation
+ '|' => 0, ':' => 0, // Intersect and Range
+ '>' => 0, '<' => 0, '=' => 0, '>=' => 0, '<=' => 0, '<>' => 0, // Comparison
+ ];
+
+ // Comparison (Boolean) Operators
+ // These operators work on two values, but always return a boolean result
+ private static $comparisonOperators = ['>' => true, '<' => true, '=' => true, '>=' => true, '<=' => true, '<>' => true];
+
+ // Operator Precedence
+ // This list includes all valid operators, whether binary (including boolean) or unary (such as %)
+ // Array key is the operator, the value is its precedence
+ private static $operatorPrecedence = [
+ ':' => 8, // Range
+ '|' => 7, // Intersect
+ '~' => 6, // Negation
+ '%' => 5, // Percentage
+ '^' => 4, // Exponentiation
+ '*' => 3, '/' => 3, // Multiplication and Division
+ '+' => 2, '-' => 2, // Addition and Subtraction
+ '&' => 1, // Concatenation
+ '>' => 0, '<' => 0, '=' => 0, '>=' => 0, '<=' => 0, '<>' => 0, // Comparison
+ ];
+
+ // Convert infix to postfix notation
+
+ /**
+ * @param string $formula
+ * @param null|\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell
+ *
+ * @return bool
+ */
+ private function _parseFormula($formula, Cell $pCell = null)
+ {
+ if (($formula = $this->convertMatrixReferences(trim($formula))) === false) {
+ return false;
+ }
+
+ // If we're using cell caching, then $pCell may well be flushed back to the cache (which detaches the parent worksheet),
+ // so we store the parent worksheet so that we can re-attach it when necessary
+ $pCellParent = ($pCell !== null) ? $pCell->getWorksheet() : null;
+
+ $regexpMatchString = '/^(' . self::CALCULATION_REGEXP_FUNCTION .
+ '|' . self::CALCULATION_REGEXP_CELLREF .
+ '|' . self::CALCULATION_REGEXP_NUMBER .
+ '|' . self::CALCULATION_REGEXP_STRING .
+ '|' . self::CALCULATION_REGEXP_OPENBRACE .
+ '|' . self::CALCULATION_REGEXP_NAMEDRANGE .
+ '|' . self::CALCULATION_REGEXP_ERROR .
+ ')/si';
+
+ // Start with initialisation
+ $index = 0;
+ $stack = new Stack();
+ $output = [];
+ $expectingOperator = false; // We use this test in syntax-checking the expression to determine when a
+ // - is a negation or + is a positive operator rather than an operation
+ $expectingOperand = false; // We use this test in syntax-checking the expression to determine whether an operand
+ // should be null in a function call
+ // The guts of the lexical parser
+ // Loop through the formula extracting each operator and operand in turn
+ while (true) {
+ $opCharacter = $formula[$index]; // Get the first character of the value at the current index position
+ if ((isset(self::$comparisonOperators[$opCharacter])) && (strlen($formula) > $index) && (isset(self::$comparisonOperators[$formula[$index + 1]]))) {
+ $opCharacter .= $formula[++$index];
+ }
+
+ // Find out if we're currently at the beginning of a number, variable, cell reference, function, parenthesis or operand
+ $isOperandOrFunction = preg_match($regexpMatchString, substr($formula, $index), $match);
+
+ if ($opCharacter == '-' && !$expectingOperator) { // Is it a negation instead of a minus?
+ $stack->push('Unary Operator', '~'); // Put a negation on the stack
+ ++$index; // and drop the negation symbol
+ } elseif ($opCharacter == '%' && $expectingOperator) {
+ $stack->push('Unary Operator', '%'); // Put a percentage on the stack
+ ++$index;
+ } elseif ($opCharacter == '+' && !$expectingOperator) { // Positive (unary plus rather than binary operator plus) can be discarded?
+ ++$index; // Drop the redundant plus symbol
+ } elseif ((($opCharacter == '~') || ($opCharacter == '|')) && (!$isOperandOrFunction)) { // We have to explicitly deny a tilde or pipe, because they are legal
+ return $this->raiseFormulaError("Formula Error: Illegal character '~'"); // on the stack but not in the input expression
+ } elseif ((isset(self::$operators[$opCharacter]) or $isOperandOrFunction) && $expectingOperator) { // Are we putting an operator on the stack?
+ while ($stack->count() > 0 &&
+ ($o2 = $stack->last()) &&
+ isset(self::$operators[$o2['value']]) &&
+ @(self::$operatorAssociativity[$opCharacter] ? self::$operatorPrecedence[$opCharacter] < self::$operatorPrecedence[$o2['value']] : self::$operatorPrecedence[$opCharacter] <= self::$operatorPrecedence[$o2['value']])) {
+ $output[] = $stack->pop(); // Swap operands and higher precedence operators from the stack to the output
+ }
+ $stack->push('Binary Operator', $opCharacter); // Finally put our current operator onto the stack
+ ++$index;
+ $expectingOperator = false;
+ } elseif ($opCharacter == ')' && $expectingOperator) { // Are we expecting to close a parenthesis?
+ $expectingOperand = false;
+ while (($o2 = $stack->pop()) && $o2['value'] != '(') { // Pop off the stack back to the last (
+ if ($o2 === null) {
+ return $this->raiseFormulaError('Formula Error: Unexpected closing brace ")"');
+ }
+ $output[] = $o2;
+ }
+ $d = $stack->last(2);
+ if (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $d['value'], $matches)) { // Did this parenthesis just close a function?
+ $functionName = $matches[1]; // Get the function name
+ $d = $stack->pop();
+ $argumentCount = $d['value']; // See how many arguments there were (argument count is the next value stored on the stack)
+ $output[] = $d; // Dump the argument count on the output
+ $output[] = $stack->pop(); // Pop the function and push onto the output
+ if (isset(self::$controlFunctions[$functionName])) {
+ $expectedArgumentCount = self::$controlFunctions[$functionName]['argumentCount'];
+ $functionCall = self::$controlFunctions[$functionName]['functionCall'];
+ } elseif (isset(self::$phpSpreadsheetFunctions[$functionName])) {
+ $expectedArgumentCount = self::$phpSpreadsheetFunctions[$functionName]['argumentCount'];
+ $functionCall = self::$phpSpreadsheetFunctions[$functionName]['functionCall'];
+ } else { // did we somehow push a non-function on the stack? this should never happen
+ return $this->raiseFormulaError('Formula Error: Internal error, non-function on stack');
+ }
+ // Check the argument count
+ $argumentCountError = false;
+ if (is_numeric($expectedArgumentCount)) {
+ if ($expectedArgumentCount < 0) {
+ if ($argumentCount > abs($expectedArgumentCount)) {
+ $argumentCountError = true;
+ $expectedArgumentCountString = 'no more than ' . abs($expectedArgumentCount);
+ }
+ } else {
+ if ($argumentCount != $expectedArgumentCount) {
+ $argumentCountError = true;
+ $expectedArgumentCountString = $expectedArgumentCount;
+ }
+ }
+ } elseif ($expectedArgumentCount != '*') {
+ $isOperandOrFunction = preg_match('/(\d*)([-+,])(\d*)/', $expectedArgumentCount, $argMatch);
+ switch ($argMatch[2]) {
+ case '+':
+ if ($argumentCount < $argMatch[1]) {
+ $argumentCountError = true;
+ $expectedArgumentCountString = $argMatch[1] . ' or more ';
+ }
+
+ break;
+ case '-':
+ if (($argumentCount < $argMatch[1]) || ($argumentCount > $argMatch[3])) {
+ $argumentCountError = true;
+ $expectedArgumentCountString = 'between ' . $argMatch[1] . ' and ' . $argMatch[3];
+ }
+
+ break;
+ case ',':
+ if (($argumentCount != $argMatch[1]) && ($argumentCount != $argMatch[3])) {
+ $argumentCountError = true;
+ $expectedArgumentCountString = 'either ' . $argMatch[1] . ' or ' . $argMatch[3];
+ }
+
+ break;
+ }
+ }
+ if ($argumentCountError) {
+ return $this->raiseFormulaError("Formula Error: Wrong number of arguments for $functionName() function: $argumentCount given, " . $expectedArgumentCountString . ' expected');
+ }
+ }
+ ++$index;
+ } elseif ($opCharacter == ',') { // Is this the separator for function arguments?
+ while (($o2 = $stack->pop()) && $o2['value'] != '(') { // Pop off the stack back to the last (
+ if ($o2 === null) {
+ return $this->raiseFormulaError('Formula Error: Unexpected ,');
+ }
+ $output[] = $o2; // pop the argument expression stuff and push onto the output
+ }
+ // If we've a comma when we're expecting an operand, then what we actually have is a null operand;
+ // so push a null onto the stack
+ if (($expectingOperand) || (!$expectingOperator)) {
+ $output[] = ['type' => 'NULL Value', 'value' => self::$excelConstants['NULL'], 'reference' => null];
+ }
+ // make sure there was a function
+ $d = $stack->last(2);
+ if (!preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $d['value'], $matches)) {
+ return $this->raiseFormulaError('Formula Error: Unexpected ,');
+ }
+ $d = $stack->pop();
+ $stack->push($d['type'], ++$d['value'], $d['reference']); // increment the argument count
+ $stack->push('Brace', '('); // put the ( back on, we'll need to pop back to it again
+ $expectingOperator = false;
+ $expectingOperand = true;
+ ++$index;
+ } elseif ($opCharacter == '(' && !$expectingOperator) {
+ $stack->push('Brace', '(');
+ ++$index;
+ } elseif ($isOperandOrFunction && !$expectingOperator) { // do we now have a function/variable/number?
+ $expectingOperator = true;
+ $expectingOperand = false;
+ $val = $match[1];
+ $length = strlen($val);
+
+ if (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $val, $matches)) {
+ $val = preg_replace('/\s/u', '', $val);
+ if (isset(self::$phpSpreadsheetFunctions[strtoupper($matches[1])]) || isset(self::$controlFunctions[strtoupper($matches[1])])) { // it's a function
+ $stack->push('Function', strtoupper($val));
+ $ax = preg_match('/^\s*(\s*\))/ui', substr($formula, $index + $length), $amatch);
+ if ($ax) {
+ $stack->push('Operand Count for Function ' . strtoupper($val) . ')', 0);
+ $expectingOperator = true;
+ } else {
+ $stack->push('Operand Count for Function ' . strtoupper($val) . ')', 1);
+ $expectingOperator = false;
+ }
+ $stack->push('Brace', '(');
+ } else { // it's a var w/ implicit multiplication
+ $output[] = ['type' => 'Value', 'value' => $matches[1], 'reference' => null];
+ }
+ } elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $val, $matches)) {
+ // Watch for this case-change when modifying to allow cell references in different worksheets...
+ // Should only be applied to the actual cell column, not the worksheet name
+
+ // If the last entry on the stack was a : operator, then we have a cell range reference
+ $testPrevOp = $stack->last(1);
+ if ($testPrevOp['value'] == ':') {
+ // If we have a worksheet reference, then we're playing with a 3D reference
+ if ($matches[2] == '') {
+ // Otherwise, we 'inherit' the worksheet reference from the start cell reference
+ // The start of the cell range reference should be the last entry in $output
+ $startCellRef = $output[count($output) - 1]['value'];
+ preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $startCellRef, $startMatches);
+ if ($startMatches[2] > '') {
+ $val = $startMatches[2] . '!' . $val;
+ }
+ } else {
+ return $this->raiseFormulaError('3D Range references are not yet supported');
+ }
+ }
+
+ $output[] = ['type' => 'Cell Reference', 'value' => $val, 'reference' => $val];
+ } else { // it's a variable, constant, string, number or boolean
+ // If the last entry on the stack was a : operator, then we may have a row or column range reference
+ $testPrevOp = $stack->last(1);
+ if ($testPrevOp['value'] == ':') {
+ $startRowColRef = $output[count($output) - 1]['value'];
+ list($rangeWS1, $startRowColRef) = Worksheet::extractSheetTitle($startRowColRef, true);
+ if ($rangeWS1 != '') {
+ $rangeWS1 .= '!';
+ }
+ list($rangeWS2, $val) = Worksheet::extractSheetTitle($val, true);
+ if ($rangeWS2 != '') {
+ $rangeWS2 .= '!';
+ } else {
+ $rangeWS2 = $rangeWS1;
+ }
+ if ((is_int($startRowColRef)) && (ctype_digit($val)) &&
+ ($startRowColRef <= 1048576) && ($val <= 1048576)) {
+ // Row range
+ $endRowColRef = ($pCellParent !== null) ? $pCellParent->getHighestColumn() : 'XFD'; // Max 16,384 columns for Excel2007
+ $output[count($output) - 1]['value'] = $rangeWS1 . 'A' . $startRowColRef;
+ $val = $rangeWS2 . $endRowColRef . $val;
+ } elseif ((ctype_alpha($startRowColRef)) && (ctype_alpha($val)) &&
+ (strlen($startRowColRef) <= 3) && (strlen($val) <= 3)) {
+ // Column range
+ $endRowColRef = ($pCellParent !== null) ? $pCellParent->getHighestRow() : 1048576; // Max 1,048,576 rows for Excel2007
+ $output[count($output) - 1]['value'] = $rangeWS1 . strtoupper($startRowColRef) . '1';
+ $val = $rangeWS2 . $val . $endRowColRef;
+ }
+ }
+
+ $localeConstant = false;
+ if ($opCharacter == '"') {
+ // UnEscape any quotes within the string
+ $val = self::wrapResult(str_replace('""', '"', self::unwrapResult($val)));
+ } elseif (is_numeric($val)) {
+ if ((strpos($val, '.') !== false) || (stripos($val, 'e') !== false) || ($val > PHP_INT_MAX) || ($val < -PHP_INT_MAX)) {
+ $val = (float) $val;
+ } else {
+ $val = (int) $val;
+ }
+ } elseif (isset(self::$excelConstants[trim(strtoupper($val))])) {
+ $excelConstant = trim(strtoupper($val));
+ $val = self::$excelConstants[$excelConstant];
+ } elseif (($localeConstant = array_search(trim(strtoupper($val)), self::$localeBoolean)) !== false) {
+ $val = self::$excelConstants[$localeConstant];
+ }
+ $details = ['type' => 'Value', 'value' => $val, 'reference' => null];
+ if ($localeConstant) {
+ $details['localeValue'] = $localeConstant;
+ }
+ $output[] = $details;
+ }
+ $index += $length;
+ } elseif ($opCharacter == '$') { // absolute row or column range
+ ++$index;
+ } elseif ($opCharacter == ')') { // miscellaneous error checking
+ if ($expectingOperand) {
+ $output[] = ['type' => 'NULL Value', 'value' => self::$excelConstants['NULL'], 'reference' => null];
+ $expectingOperand = false;
+ $expectingOperator = true;
+ } else {
+ return $this->raiseFormulaError("Formula Error: Unexpected ')'");
+ }
+ } elseif (isset(self::$operators[$opCharacter]) && !$expectingOperator) {
+ return $this->raiseFormulaError("Formula Error: Unexpected operator '$opCharacter'");
+ } else { // I don't even want to know what you did to get here
+ return $this->raiseFormulaError('Formula Error: An unexpected error occured');
+ }
+ // Test for end of formula string
+ if ($index == strlen($formula)) {
+ // Did we end with an operator?.
+ // Only valid for the % unary operator
+ if ((isset(self::$operators[$opCharacter])) && ($opCharacter != '%')) {
+ return $this->raiseFormulaError("Formula Error: Operator '$opCharacter' has no operands");
+ }
+
+ break;
+ }
+ // Ignore white space
+ while (($formula[$index] == "\n") || ($formula[$index] == "\r")) {
+ ++$index;
+ }
+ if ($formula[$index] == ' ') {
+ while ($formula[$index] == ' ') {
+ ++$index;
+ }
+ // If we're expecting an operator, but only have a space between the previous and next operands (and both are
+ // Cell References) then we have an INTERSECTION operator
+ if (($expectingOperator) && (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '.*/Ui', substr($formula, $index), $match)) &&
+ ($output[count($output) - 1]['type'] == 'Cell Reference')) {
+ while ($stack->count() > 0 &&
+ ($o2 = $stack->last()) &&
+ isset(self::$operators[$o2['value']]) &&
+ @(self::$operatorAssociativity[$opCharacter] ? self::$operatorPrecedence[$opCharacter] < self::$operatorPrecedence[$o2['value']] : self::$operatorPrecedence[$opCharacter] <= self::$operatorPrecedence[$o2['value']])) {
+ $output[] = $stack->pop(); // Swap operands and higher precedence operators from the stack to the output
+ }
+ $stack->push('Binary Operator', '|'); // Put an Intersect Operator on the stack
+ $expectingOperator = false;
+ }
+ }
+ }
+
+ while (($op = $stack->pop()) !== null) { // pop everything off the stack and push onto output
+ if ((is_array($op) && $op['value'] == '(') || ($op === '(')) {
+ return $this->raiseFormulaError("Formula Error: Expecting ')'"); // if there are any opening braces on the stack, then braces were unbalanced
+ }
+ $output[] = $op;
+ }
+
+ return $output;
+ }
+
+ private static function dataTestReference(&$operandData)
+ {
+ $operand = $operandData['value'];
+ if (($operandData['reference'] === null) && (is_array($operand))) {
+ $rKeys = array_keys($operand);
+ $rowKey = array_shift($rKeys);
+ $cKeys = array_keys(array_keys($operand[$rowKey]));
+ $colKey = array_shift($cKeys);
+ if (ctype_upper($colKey)) {
+ $operandData['reference'] = $colKey . $rowKey;
+ }
+ }
+
+ return $operand;
+ }
+
+ // evaluate postfix notation
+
+ /**
+ * @param mixed $tokens
+ * @param null|string $cellID
+ * @param null|Cell $pCell
+ *
+ * @return bool
+ */
+ private function processTokenStack($tokens, $cellID = null, Cell $pCell = null)
+ {
+ if ($tokens == false) {
+ return false;
+ }
+
+ // If we're using cell caching, then $pCell may well be flushed back to the cache (which detaches the parent cell collection),
+ // so we store the parent cell collection so that we can re-attach it when necessary
+ $pCellWorksheet = ($pCell !== null) ? $pCell->getWorksheet() : null;
+ $pCellParent = ($pCell !== null) ? $pCell->getParent() : null;
+ $stack = new Stack();
+
+ // Loop through each token in turn
+ foreach ($tokens as $tokenData) {
+ $token = $tokenData['value'];
+ // if the token is a binary operator, pop the top two values off the stack, do the operation, and push the result back on the stack
+ if (isset(self::$binaryOperators[$token])) {
+ // We must have two operands, error if we don't
+ if (($operand2Data = $stack->pop()) === null) {
+ return $this->raiseFormulaError('Internal error - Operand value missing from stack');
+ }
+ if (($operand1Data = $stack->pop()) === null) {
+ return $this->raiseFormulaError('Internal error - Operand value missing from stack');
+ }
+
+ $operand1 = self::dataTestReference($operand1Data);
+ $operand2 = self::dataTestReference($operand2Data);
+
+ // Log what we're doing
+ if ($token == ':') {
+ $this->debugLog->writeDebugLog('Evaluating Range ', $this->showValue($operand1Data['reference']), ' ', $token, ' ', $this->showValue($operand2Data['reference']));
+ } else {
+ $this->debugLog->writeDebugLog('Evaluating ', $this->showValue($operand1), ' ', $token, ' ', $this->showValue($operand2));
+ }
+
+ // Process the operation in the appropriate manner
+ switch ($token) {
+ // Comparison (Boolean) Operators
+ case '>': // Greater than
+ case '<': // Less than
+ case '>=': // Greater than or Equal to
+ case '<=': // Less than or Equal to
+ case '=': // Equality
+ case '<>': // Inequality
+ $this->executeBinaryComparisonOperation($cellID, $operand1, $operand2, $token, $stack);
+
+ break;
+ // Binary Operators
+ case ':': // Range
+ if (strpos($operand1Data['reference'], '!') !== false) {
+ list($sheet1, $operand1Data['reference']) = Worksheet::extractSheetTitle($operand1Data['reference'], true);
+ } else {
+ $sheet1 = ($pCellParent !== null) ? $pCellWorksheet->getTitle() : '';
+ }
+
+ list($sheet2, $operand2Data['reference']) = Worksheet::extractSheetTitle($operand2Data['reference'], true);
+ if (empty($sheet2)) {
+ $sheet2 = $sheet1;
+ }
+
+ if ($sheet1 == $sheet2) {
+ if ($operand1Data['reference'] === null) {
+ if ((trim($operand1Data['value']) != '') && (is_numeric($operand1Data['value']))) {
+ $operand1Data['reference'] = $pCell->getColumn() . $operand1Data['value'];
+ } elseif (trim($operand1Data['reference']) == '') {
+ $operand1Data['reference'] = $pCell->getCoordinate();
+ } else {
+ $operand1Data['reference'] = $operand1Data['value'] . $pCell->getRow();
+ }
+ }
+ if ($operand2Data['reference'] === null) {
+ if ((trim($operand2Data['value']) != '') && (is_numeric($operand2Data['value']))) {
+ $operand2Data['reference'] = $pCell->getColumn() . $operand2Data['value'];
+ } elseif (trim($operand2Data['reference']) == '') {
+ $operand2Data['reference'] = $pCell->getCoordinate();
+ } else {
+ $operand2Data['reference'] = $operand2Data['value'] . $pCell->getRow();
+ }
+ }
+
+ $oData = array_merge(explode(':', $operand1Data['reference']), explode(':', $operand2Data['reference']));
+ $oCol = $oRow = [];
+ foreach ($oData as $oDatum) {
+ $oCR = Coordinate::coordinateFromString($oDatum);
+ $oCol[] = Coordinate::columnIndexFromString($oCR[0]) - 1;
+ $oRow[] = $oCR[1];
+ }
+ $cellRef = Coordinate::stringFromColumnIndex(min($oCol) + 1) . min($oRow) . ':' . Coordinate::stringFromColumnIndex(max($oCol) + 1) . max($oRow);
+ if ($pCellParent !== null) {
+ $cellValue = $this->extractCellRange($cellRef, $this->spreadsheet->getSheetByName($sheet1), false);
+ } else {
+ return $this->raiseFormulaError('Unable to access Cell Reference');
+ }
+ $stack->push('Cell Reference', $cellValue, $cellRef);
+ } else {
+ $stack->push('Error', Functions::REF(), null);
+ }
+
+ break;
+ case '+': // Addition
+ $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'plusEquals', $stack);
+
+ break;
+ case '-': // Subtraction
+ $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'minusEquals', $stack);
+
+ break;
+ case '*': // Multiplication
+ $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'arrayTimesEquals', $stack);
+
+ break;
+ case '/': // Division
+ $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'arrayRightDivide', $stack);
+
+ break;
+ case '^': // Exponential
+ $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'power', $stack);
+
+ break;
+ case '&': // Concatenation
+ // If either of the operands is a matrix, we need to treat them both as matrices
+ // (converting the other operand to a matrix if need be); then perform the required
+ // matrix operation
+ if (is_bool($operand1)) {
+ $operand1 = ($operand1) ? self::$localeBoolean['TRUE'] : self::$localeBoolean['FALSE'];
+ }
+ if (is_bool($operand2)) {
+ $operand2 = ($operand2) ? self::$localeBoolean['TRUE'] : self::$localeBoolean['FALSE'];
+ }
+ if ((is_array($operand1)) || (is_array($operand2))) {
+ // Ensure that both operands are arrays/matrices
+ self::checkMatrixOperands($operand1, $operand2, 2);
+
+ try {
+ // Convert operand 1 from a PHP array to a matrix
+ $matrix = new Shared\JAMA\Matrix($operand1);
+ // Perform the required operation against the operand 1 matrix, passing in operand 2
+ $matrixResult = $matrix->concat($operand2);
+ $result = $matrixResult->getArray();
+ } catch (\Exception $ex) {
+ $this->debugLog->writeDebugLog('JAMA Matrix Exception: ', $ex->getMessage());
+ $result = '#VALUE!';
+ }
+ } else {
+ $result = '"' . str_replace('""', '"', self::unwrapResult($operand1) . self::unwrapResult($operand2)) . '"';
+ }
+ $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
+ $stack->push('Value', $result);
+
+ break;
+ case '|': // Intersect
+ $rowIntersect = array_intersect_key($operand1, $operand2);
+ $cellIntersect = $oCol = $oRow = [];
+ foreach (array_keys($rowIntersect) as $row) {
+ $oRow[] = $row;
+ foreach ($rowIntersect[$row] as $col => $data) {
+ $oCol[] = Coordinate::columnIndexFromString($col) - 1;
+ $cellIntersect[$row] = array_intersect_key($operand1[$row], $operand2[$row]);
+ }
+ }
+ $cellRef = Coordinate::stringFromColumnIndex(min($oCol) + 1) . min($oRow) . ':' . Coordinate::stringFromColumnIndex(max($oCol) + 1) . max($oRow);
+ $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($cellIntersect));
+ $stack->push('Value', $cellIntersect, $cellRef);
+
+ break;
+ }
+
+ // if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
+ } elseif (($token === '~') || ($token === '%')) {
+ if (($arg = $stack->pop()) === null) {
+ return $this->raiseFormulaError('Internal error - Operand value missing from stack');
+ }
+ $arg = $arg['value'];
+ if ($token === '~') {
+ $this->debugLog->writeDebugLog('Evaluating Negation of ', $this->showValue($arg));
+ $multiplier = -1;
+ } else {
+ $this->debugLog->writeDebugLog('Evaluating Percentile of ', $this->showValue($arg));
+ $multiplier = 0.01;
+ }
+ if (is_array($arg)) {
+ self::checkMatrixOperands($arg, $multiplier, 2);
+
+ try {
+ $matrix1 = new Shared\JAMA\Matrix($arg);
+ $matrixResult = $matrix1->arrayTimesEquals($multiplier);
+ $result = $matrixResult->getArray();
+ } catch (\Exception $ex) {
+ $this->debugLog->writeDebugLog('JAMA Matrix Exception: ', $ex->getMessage());
+ $result = '#VALUE!';
+ }
+ $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
+ $stack->push('Value', $result);
+ } else {
+ $this->executeNumericBinaryOperation($multiplier, $arg, '*', 'arrayTimesEquals', $stack);
+ }
+ } elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $token, $matches)) {
+ $cellRef = null;
+ if (isset($matches[8])) {
+ if ($pCell === null) {
+ // We can't access the range, so return a REF error
+ $cellValue = Functions::REF();
+ } else {
+ $cellRef = $matches[6] . $matches[7] . ':' . $matches[9] . $matches[10];
+ if ($matches[2] > '') {
+ $matches[2] = trim($matches[2], "\"'");
+ if ((strpos($matches[2], '[') !== false) || (strpos($matches[2], ']') !== false)) {
+ // It's a Reference to an external spreadsheet (not currently supported)
+ return $this->raiseFormulaError('Unable to access External Workbook');
+ }
+ $matches[2] = trim($matches[2], "\"'");
+ $this->debugLog->writeDebugLog('Evaluating Cell Range ', $cellRef, ' in worksheet ', $matches[2]);
+ if ($pCellParent !== null) {
+ $cellValue = $this->extractCellRange($cellRef, $this->spreadsheet->getSheetByName($matches[2]), false);
+ } else {
+ return $this->raiseFormulaError('Unable to access Cell Reference');
+ }
+ $this->debugLog->writeDebugLog('Evaluation Result for cells ', $cellRef, ' in worksheet ', $matches[2], ' is ', $this->showTypeDetails($cellValue));
+ } else {
+ $this->debugLog->writeDebugLog('Evaluating Cell Range ', $cellRef, ' in current worksheet');
+ if ($pCellParent !== null) {
+ $cellValue = $this->extractCellRange($cellRef, $pCellWorksheet, false);
+ } else {
+ return $this->raiseFormulaError('Unable to access Cell Reference');
+ }
+ $this->debugLog->writeDebugLog('Evaluation Result for cells ', $cellRef, ' is ', $this->showTypeDetails($cellValue));
+ }
+ }
+ } else {
+ if ($pCell === null) {
+ // We can't access the cell, so return a REF error
+ $cellValue = Functions::REF();
+ } else {
+ $cellRef = $matches[6] . $matches[7];
+ if ($matches[2] > '') {
+ $matches[2] = trim($matches[2], "\"'");
+ if ((strpos($matches[2], '[') !== false) || (strpos($matches[2], ']') !== false)) {
+ // It's a Reference to an external spreadsheet (not currently supported)
+ return $this->raiseFormulaError('Unable to access External Workbook');
+ }
+ $this->debugLog->writeDebugLog('Evaluating Cell ', $cellRef, ' in worksheet ', $matches[2]);
+ if ($pCellParent !== null) {
+ $cellSheet = $this->spreadsheet->getSheetByName($matches[2]);
+ if ($cellSheet && $cellSheet->cellExists($cellRef)) {
+ $cellValue = $this->extractCellRange($cellRef, $this->spreadsheet->getSheetByName($matches[2]), false);
+ $pCell->attach($pCellParent);
+ } else {
+ $cellValue = null;
+ }
+ } else {
+ return $this->raiseFormulaError('Unable to access Cell Reference');
+ }
+ $this->debugLog->writeDebugLog('Evaluation Result for cell ', $cellRef, ' in worksheet ', $matches[2], ' is ', $this->showTypeDetails($cellValue));
+ } else {
+ $this->debugLog->writeDebugLog('Evaluating Cell ', $cellRef, ' in current worksheet');
+ if ($pCellParent->has($cellRef)) {
+ $cellValue = $this->extractCellRange($cellRef, $pCellWorksheet, false);
+ $pCell->attach($pCellParent);
+ } else {
+ $cellValue = null;
+ }
+ $this->debugLog->writeDebugLog('Evaluation Result for cell ', $cellRef, ' is ', $this->showTypeDetails($cellValue));
+ }
+ }
+ }
+ $stack->push('Value', $cellValue, $cellRef);
+
+ // if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
+ } elseif (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $token, $matches)) {
+ $functionName = $matches[1];
+ $argCount = $stack->pop();
+ $argCount = $argCount['value'];
+ if ($functionName != 'MKMATRIX') {
+ $this->debugLog->writeDebugLog('Evaluating Function ', self::localeFunc($functionName), '() with ', (($argCount == 0) ? 'no' : $argCount), ' argument', (($argCount == 1) ? '' : 's'));
+ }
+ if ((isset(self::$phpSpreadsheetFunctions[$functionName])) || (isset(self::$controlFunctions[$functionName]))) { // function
+ if (isset(self::$phpSpreadsheetFunctions[$functionName])) {
+ $functionCall = self::$phpSpreadsheetFunctions[$functionName]['functionCall'];
+ $passByReference = isset(self::$phpSpreadsheetFunctions[$functionName]['passByReference']);
+ $passCellReference = isset(self::$phpSpreadsheetFunctions[$functionName]['passCellReference']);
+ } elseif (isset(self::$controlFunctions[$functionName])) {
+ $functionCall = self::$controlFunctions[$functionName]['functionCall'];
+ $passByReference = isset(self::$controlFunctions[$functionName]['passByReference']);
+ $passCellReference = isset(self::$controlFunctions[$functionName]['passCellReference']);
+ }
+ // get the arguments for this function
+ $args = $argArrayVals = [];
+ for ($i = 0; $i < $argCount; ++$i) {
+ $arg = $stack->pop();
+ $a = $argCount - $i - 1;
+ if (($passByReference) &&
+ (isset(self::$phpSpreadsheetFunctions[$functionName]['passByReference'][$a])) &&
+ (self::$phpSpreadsheetFunctions[$functionName]['passByReference'][$a])) {
+ if ($arg['reference'] === null) {
+ $args[] = $cellID;
+ if ($functionName != 'MKMATRIX') {
+ $argArrayVals[] = $this->showValue($cellID);
+ }
+ } else {
+ $args[] = $arg['reference'];
+ if ($functionName != 'MKMATRIX') {
+ $argArrayVals[] = $this->showValue($arg['reference']);
+ }
+ }
+ } else {
+ $args[] = self::unwrapResult($arg['value']);
+ if ($functionName != 'MKMATRIX') {
+ $argArrayVals[] = $this->showValue($arg['value']);
+ }
+ }
+ }
+ // Reverse the order of the arguments
+ krsort($args);
+
+ if (($passByReference) && ($argCount == 0)) {
+ $args[] = $cellID;
+ $argArrayVals[] = $this->showValue($cellID);
+ }
+
+ if ($functionName != 'MKMATRIX') {
+ if ($this->debugLog->getWriteDebugLog()) {
+ krsort($argArrayVals);
+ $this->debugLog->writeDebugLog('Evaluating ', self::localeFunc($functionName), '( ', implode(self::$localeArgumentSeparator . ' ', Functions::flattenArray($argArrayVals)), ' )');
+ }
+ }
+
+ // Process the argument with the appropriate function call
+ $args = $this->addCellReference($args, $passCellReference, $functionCall, $pCell);
+
+ if (!is_array($functionCall)) {
+ foreach ($args as &$arg) {
+ $arg = Functions::flattenSingleValue($arg);
+ }
+ unset($arg);
+ }
+ $result = call_user_func_array($functionCall, $args);
+
+ if ($functionName != 'MKMATRIX') {
+ $this->debugLog->writeDebugLog('Evaluation Result for ', self::localeFunc($functionName), '() function call is ', $this->showTypeDetails($result));
+ }
+ $stack->push('Value', self::wrapResult($result));
+ }
+ } else {
+ // if the token is a number, boolean, string or an Excel error, push it onto the stack
+ if (isset(self::$excelConstants[strtoupper($token)])) {
+ $excelConstant = strtoupper($token);
+ $stack->push('Constant Value', self::$excelConstants[$excelConstant]);
+ $this->debugLog->writeDebugLog('Evaluating Constant ', $excelConstant, ' as ', $this->showTypeDetails(self::$excelConstants[$excelConstant]));
+ } elseif ((is_numeric($token)) || ($token === null) || (is_bool($token)) || ($token == '') || ($token[0] == '"') || ($token[0] == '#')) {
+ $stack->push('Value', $token);
+ // if the token is a named range, push the named range name onto the stack
+ } elseif (preg_match('/^' . self::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $token, $matches)) {
+ $namedRange = $matches[6];
+ $this->debugLog->writeDebugLog('Evaluating Named Range ', $namedRange);
+
+ $cellValue = $this->extractNamedRange($namedRange, ((null !== $pCell) ? $pCellWorksheet : null), false);
+ $pCell->attach($pCellParent);
+ $this->debugLog->writeDebugLog('Evaluation Result for named range ', $namedRange, ' is ', $this->showTypeDetails($cellValue));
+ $stack->push('Named Range', $cellValue, $namedRange);
+ } else {
+ return $this->raiseFormulaError("undefined variable '$token'");
+ }
+ }
+ }
+ // when we're out of tokens, the stack should have a single element, the final result
+ if ($stack->count() != 1) {
+ return $this->raiseFormulaError('internal error');
+ }
+ $output = $stack->pop();
+ $output = $output['value'];
+
+ return $output;
+ }
+
+ private function validateBinaryOperand(&$operand, &$stack)
+ {
+ if (is_array($operand)) {
+ if ((count($operand, COUNT_RECURSIVE) - count($operand)) == 1) {
+ do {
+ $operand = array_pop($operand);
+ } while (is_array($operand));
+ }
+ }
+ // Numbers, matrices and booleans can pass straight through, as they're already valid
+ if (is_string($operand)) {
+ // We only need special validations for the operand if it is a string
+ // Start by stripping off the quotation marks we use to identify true excel string values internally
+ if ($operand > '' && $operand[0] == '"') {
+ $operand = self::unwrapResult($operand);
+ }
+ // If the string is a numeric value, we treat it as a numeric, so no further testing
+ if (!is_numeric($operand)) {
+ // If not a numeric, test to see if the value is an Excel error, and so can't be used in normal binary operations
+ if ($operand > '' && $operand[0] == '#') {
+ $stack->push('Value', $operand);
+ $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($operand));
+
+ return false;
+ } elseif (!Shared\StringHelper::convertToNumberIfFraction($operand)) {
+ // If not a numeric or a fraction, then it's a text string, and so can't be used in mathematical binary operations
+ $stack->push('Value', '#VALUE!');
+ $this->debugLog->writeDebugLog('Evaluation Result is a ', $this->showTypeDetails('#VALUE!'));
+
+ return false;
+ }
+ }
+ }
+
+ // return a true if the value of the operand is one that we can use in normal binary operations
+ return true;
+ }
+
+ /**
+ * @param null|string $cellID
+ * @param mixed $operand1
+ * @param mixed $operand2
+ * @param string $operation
+ * @param Stack $stack
+ * @param bool $recursingArrays
+ *
+ * @return bool
+ */
+ private function executeBinaryComparisonOperation($cellID, $operand1, $operand2, $operation, Stack &$stack, $recursingArrays = false)
+ {
+ // If we're dealing with matrix operations, we want a matrix result
+ if ((is_array($operand1)) || (is_array($operand2))) {
+ $result = [];
+ if ((is_array($operand1)) && (!is_array($operand2))) {
+ foreach ($operand1 as $x => $operandData) {
+ $this->debugLog->writeDebugLog('Evaluating Comparison ', $this->showValue($operandData), ' ', $operation, ' ', $this->showValue($operand2));
+ $this->executeBinaryComparisonOperation($cellID, $operandData, $operand2, $operation, $stack);
+ $r = $stack->pop();
+ $result[$x] = $r['value'];
+ }
+ } elseif ((!is_array($operand1)) && (is_array($operand2))) {
+ foreach ($operand2 as $x => $operandData) {
+ $this->debugLog->writeDebugLog('Evaluating Comparison ', $this->showValue($operand1), ' ', $operation, ' ', $this->showValue($operandData));
+ $this->executeBinaryComparisonOperation($cellID, $operand1, $operandData, $operation, $stack);
+ $r = $stack->pop();
+ $result[$x] = $r['value'];
+ }
+ } else {
+ if (!$recursingArrays) {
+ self::checkMatrixOperands($operand1, $operand2, 2);
+ }
+ foreach ($operand1 as $x => $operandData) {
+ $this->debugLog->writeDebugLog('Evaluating Comparison ', $this->showValue($operandData), ' ', $operation, ' ', $this->showValue($operand2[$x]));
+ $this->executeBinaryComparisonOperation($cellID, $operandData, $operand2[$x], $operation, $stack, true);
+ $r = $stack->pop();
+ $result[$x] = $r['value'];
+ }
+ }
+ // Log the result details
+ $this->debugLog->writeDebugLog('Comparison Evaluation Result is ', $this->showTypeDetails($result));
+ // And push the result onto the stack
+ $stack->push('Array', $result);
+
+ return true;
+ }
+
+ // Simple validate the two operands if they are string values
+ if (is_string($operand1) && $operand1 > '' && $operand1[0] == '"') {
+ $operand1 = self::unwrapResult($operand1);
+ }
+ if (is_string($operand2) && $operand2 > '' && $operand2[0] == '"') {
+ $operand2 = self::unwrapResult($operand2);
+ }
+
+ // Use case insensitive comparaison if not OpenOffice mode
+ if (Functions::getCompatibilityMode() != Functions::COMPATIBILITY_OPENOFFICE) {
+ if (is_string($operand1)) {
+ $operand1 = strtoupper($operand1);
+ }
+ if (is_string($operand2)) {
+ $operand2 = strtoupper($operand2);
+ }
+ }
+
+ $useLowercaseFirstComparison = is_string($operand1) && is_string($operand2) && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE;
+
+ // execute the necessary operation
+ switch ($operation) {
+ // Greater than
+ case '>':
+ if ($useLowercaseFirstComparison) {
+ $result = $this->strcmpLowercaseFirst($operand1, $operand2) > 0;
+ } else {
+ $result = ($operand1 > $operand2);
+ }
+
+ break;
+ // Less than
+ case '<':
+ if ($useLowercaseFirstComparison) {
+ $result = $this->strcmpLowercaseFirst($operand1, $operand2) < 0;
+ } else {
+ $result = ($operand1 < $operand2);
+ }
+
+ break;
+ // Equality
+ case '=':
+ if (is_numeric($operand1) && is_numeric($operand2)) {
+ $result = (abs($operand1 - $operand2) < $this->delta);
+ } else {
+ $result = strcmp($operand1, $operand2) == 0;
+ }
+
+ break;
+ // Greater than or equal
+ case '>=':
+ if (is_numeric($operand1) && is_numeric($operand2)) {
+ $result = ((abs($operand1 - $operand2) < $this->delta) || ($operand1 > $operand2));
+ } elseif ($useLowercaseFirstComparison) {
+ $result = $this->strcmpLowercaseFirst($operand1, $operand2) >= 0;
+ } else {
+ $result = strcmp($operand1, $operand2) >= 0;
+ }
+
+ break;
+ // Less than or equal
+ case '<=':
+ if (is_numeric($operand1) && is_numeric($operand2)) {
+ $result = ((abs($operand1 - $operand2) < $this->delta) || ($operand1 < $operand2));
+ } elseif ($useLowercaseFirstComparison) {
+ $result = $this->strcmpLowercaseFirst($operand1, $operand2) <= 0;
+ } else {
+ $result = strcmp($operand1, $operand2) <= 0;
+ }
+
+ break;
+ // Inequality
+ case '<>':
+ if (is_numeric($operand1) && is_numeric($operand2)) {
+ $result = (abs($operand1 - $operand2) > 1E-14);
+ } else {
+ $result = strcmp($operand1, $operand2) != 0;
+ }
+
+ break;
+ }
+
+ // Log the result details
+ $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
+ // And push the result onto the stack
+ $stack->push('Value', $result);
+
+ return true;
+ }
+
+ /**
+ * Compare two strings in the same way as strcmp() except that lowercase come before uppercase letters.
+ *
+ * @param string $str1 First string value for the comparison
+ * @param string $str2 Second string value for the comparison
+ *
+ * @return int
+ */
+ private function strcmpLowercaseFirst($str1, $str2)
+ {
+ $inversedStr1 = Shared\StringHelper::strCaseReverse($str1);
+ $inversedStr2 = Shared\StringHelper::strCaseReverse($str2);
+
+ return strcmp($inversedStr1, $inversedStr2);
+ }
+
+ /**
+ * @param mixed $operand1
+ * @param mixed $operand2
+ * @param mixed $operation
+ * @param string $matrixFunction
+ * @param mixed $stack
+ *
+ * @return bool
+ */
+ private function executeNumericBinaryOperation($operand1, $operand2, $operation, $matrixFunction, &$stack)
+ {
+ // Validate the two operands
+ if (!$this->validateBinaryOperand($operand1, $stack)) {
+ return false;
+ }
+ if (!$this->validateBinaryOperand($operand2, $stack)) {
+ return false;
+ }
+
+ // If either of the operands is a matrix, we need to treat them both as matrices
+ // (converting the other operand to a matrix if need be); then perform the required
+ // matrix operation
+ if ((is_array($operand1)) || (is_array($operand2))) {
+ // Ensure that both operands are arrays/matrices of the same size
+ self::checkMatrixOperands($operand1, $operand2, 2);
+
+ try {
+ // Convert operand 1 from a PHP array to a matrix
+ $matrix = new Shared\JAMA\Matrix($operand1);
+ // Perform the required operation against the operand 1 matrix, passing in operand 2
+ $matrixResult = $matrix->$matrixFunction($operand2);
+ $result = $matrixResult->getArray();
+ } catch (\Exception $ex) {
+ $this->debugLog->writeDebugLog('JAMA Matrix Exception: ', $ex->getMessage());
+ $result = '#VALUE!';
+ }
+ } else {
+ if ((Functions::getCompatibilityMode() != Functions::COMPATIBILITY_OPENOFFICE) &&
+ ((is_string($operand1) && !is_numeric($operand1) && strlen($operand1) > 0) ||
+ (is_string($operand2) && !is_numeric($operand2) && strlen($operand2) > 0))) {
+ $result = Functions::VALUE();
+ } else {
+ // If we're dealing with non-matrix operations, execute the necessary operation
+ switch ($operation) {
+ // Addition
+ case '+':
+ $result = $operand1 + $operand2;
+
+ break;
+ // Subtraction
+ case '-':
+ $result = $operand1 - $operand2;
+
+ break;
+ // Multiplication
+ case '*':
+ $result = $operand1 * $operand2;
+
+ break;
+ // Division
+ case '/':
+ if ($operand2 == 0) {
+ // Trap for Divide by Zero error
+ $stack->push('Value', '#DIV/0!');
+ $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails('#DIV/0!'));
+
+ return false;
+ }
+ $result = $operand1 / $operand2;
+
+ break;
+ // Power
+ case '^':
+ $result = pow($operand1, $operand2);
+
+ break;
+ }
+ }
+ }
+
+ // Log the result details
+ $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
+ // And push the result onto the stack
+ $stack->push('Value', $result);
+
+ return true;
+ }
+
+ // trigger an error, but nicely, if need be
+ protected function raiseFormulaError($errorMessage)
+ {
+ $this->formulaError = $errorMessage;
+ $this->cyclicReferenceStack->clear();
+ if (!$this->suppressFormulaErrors) {
+ throw new Exception($errorMessage);
+ }
+ trigger_error($errorMessage, E_USER_ERROR);
+
+ return false;
+ }
+
+ /**
+ * Extract range values.
+ *
+ * @param string &$pRange String based range representation
+ * @param Worksheet $pSheet Worksheet
+ * @param bool $resetLog Flag indicating whether calculation log should be reset or not
+ *
+ * @return mixed Array of values in range if range contains more than one element. Otherwise, a single value is returned.
+ */
+ public function extractCellRange(&$pRange = 'A1', Worksheet $pSheet = null, $resetLog = true)
+ {
+ // Return value
+ $returnValue = [];
+
+ if ($pSheet !== null) {
+ $pSheetName = $pSheet->getTitle();
+ if (strpos($pRange, '!') !== false) {
+ list($pSheetName, $pRange) = Worksheet::extractSheetTitle($pRange, true);
+ $pSheet = $this->spreadsheet->getSheetByName($pSheetName);
+ }
+
+ // Extract range
+ $aReferences = Coordinate::extractAllCellReferencesInRange($pRange);
+ $pRange = $pSheetName . '!' . $pRange;
+ if (!isset($aReferences[1])) {
+ $currentCol = '';
+ $currentRow = 0;
+ // Single cell in range
+ sscanf($aReferences[0], '%[A-Z]%d', $currentCol, $currentRow);
+ if ($pSheet->cellExists($aReferences[0])) {
+ $returnValue[$currentRow][$currentCol] = $pSheet->getCell($aReferences[0])->getCalculatedValue($resetLog);
+ } else {
+ $returnValue[$currentRow][$currentCol] = null;
+ }
+ } else {
+ // Extract cell data for all cells in the range
+ foreach ($aReferences as $reference) {
+ $currentCol = '';
+ $currentRow = 0;
+ // Extract range
+ sscanf($reference, '%[A-Z]%d', $currentCol, $currentRow);
+ if ($pSheet->cellExists($reference)) {
+ $returnValue[$currentRow][$currentCol] = $pSheet->getCell($reference)->getCalculatedValue($resetLog);
+ } else {
+ $returnValue[$currentRow][$currentCol] = null;
+ }
+ }
+ }
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * Extract range values.
+ *
+ * @param string &$pRange String based range representation
+ * @param Worksheet $pSheet Worksheet
+ * @param bool $resetLog Flag indicating whether calculation log should be reset or not
+ *
+ * @return mixed Array of values in range if range contains more than one element. Otherwise, a single value is returned.
+ */
+ public function extractNamedRange(&$pRange = 'A1', Worksheet $pSheet = null, $resetLog = true)
+ {
+ // Return value
+ $returnValue = [];
+
+ if ($pSheet !== null) {
+ $pSheetName = $pSheet->getTitle();
+ if (strpos($pRange, '!') !== false) {
+ list($pSheetName, $pRange) = Worksheet::extractSheetTitle($pRange, true);
+ $pSheet = $this->spreadsheet->getSheetByName($pSheetName);
+ }
+
+ // Named range?
+ $namedRange = NamedRange::resolveRange($pRange, $pSheet);
+ if ($namedRange !== null) {
+ $pSheet = $namedRange->getWorksheet();
+ $pRange = $namedRange->getRange();
+ $splitRange = Coordinate::splitRange($pRange);
+ // Convert row and column references
+ if (ctype_alpha($splitRange[0][0])) {
+ $pRange = $splitRange[0][0] . '1:' . $splitRange[0][1] . $namedRange->getWorksheet()->getHighestRow();
+ } elseif (ctype_digit($splitRange[0][0])) {
+ $pRange = 'A' . $splitRange[0][0] . ':' . $namedRange->getWorksheet()->getHighestColumn() . $splitRange[0][1];
+ }
+ } else {
+ return Functions::REF();
+ }
+
+ // Extract range
+ $aReferences = Coordinate::extractAllCellReferencesInRange($pRange);
+ if (!isset($aReferences[1])) {
+ // Single cell (or single column or row) in range
+ list($currentCol, $currentRow) = Coordinate::coordinateFromString($aReferences[0]);
+ if ($pSheet->cellExists($aReferences[0])) {
+ $returnValue[$currentRow][$currentCol] = $pSheet->getCell($aReferences[0])->getCalculatedValue($resetLog);
+ } else {
+ $returnValue[$currentRow][$currentCol] = null;
+ }
+ } else {
+ // Extract cell data for all cells in the range
+ foreach ($aReferences as $reference) {
+ // Extract range
+ list($currentCol, $currentRow) = Coordinate::coordinateFromString($reference);
+ if ($pSheet->cellExists($reference)) {
+ $returnValue[$currentRow][$currentCol] = $pSheet->getCell($reference)->getCalculatedValue($resetLog);
+ } else {
+ $returnValue[$currentRow][$currentCol] = null;
+ }
+ }
+ }
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * Is a specific function implemented?
+ *
+ * @param string $pFunction Function Name
+ *
+ * @return bool
+ */
+ public function isImplemented($pFunction)
+ {
+ $pFunction = strtoupper($pFunction);
+ $notImplemented = !isset(self::$phpSpreadsheetFunctions[$pFunction]) || (is_array(self::$phpSpreadsheetFunctions[$pFunction]['functionCall']) && self::$phpSpreadsheetFunctions[$pFunction]['functionCall'][1] === 'DUMMY');
+
+ return !$notImplemented;
+ }
+
+ /**
+ * Get a list of all implemented functions as an array of function objects.
+ *
+ * @return array of Category
+ */
+ public function getFunctions()
+ {
+ return self::$phpSpreadsheetFunctions;
+ }
+
+ /**
+ * Get a list of implemented Excel function names.
+ *
+ * @return array
+ */
+ public function getImplementedFunctionNames()
+ {
+ $returnValue = [];
+ foreach (self::$phpSpreadsheetFunctions as $functionName => $function) {
+ if ($this->isImplemented($functionName)) {
+ $returnValue[] = $functionName;
+ }
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * Add cell reference if needed while making sure that it is the last argument.
+ *
+ * @param array $args
+ * @param bool $passCellReference
+ * @param array|string $functionCall
+ * @param null|Cell $pCell
+ *
+ * @return array
+ */
+ private function addCellReference(array $args, $passCellReference, $functionCall, Cell $pCell = null)
+ {
+ if ($passCellReference) {
+ if (is_array($functionCall)) {
+ $className = $functionCall[0];
+ $methodName = $functionCall[1];
+
+ $reflectionMethod = new \ReflectionMethod($className, $methodName);
+ $argumentCount = count($reflectionMethod->getParameters());
+ while (count($args) < $argumentCount - 1) {
+ $args[] = null;
+ }
+ }
+
+ $args[] = $pCell;
+ }
+
+ return $args;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Category.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Category.php
new file mode 100644
index 00000000000..7574cb4766a
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Category.php
@@ -0,0 +1,19 @@
+ $criteriaName) {
+ $testCondition = [];
+ $testConditionCount = 0;
+ foreach ($criteria as $row => $criterion) {
+ if ($criterion[$key] > '') {
+ $testCondition[] = '[:' . $criteriaName . ']' . Functions::ifCondition($criterion[$key]);
+ ++$testConditionCount;
+ }
+ }
+ if ($testConditionCount > 1) {
+ $testConditions[] = 'OR(' . implode(',', $testCondition) . ')';
+ ++$testConditionsCount;
+ } elseif ($testConditionCount == 1) {
+ $testConditions[] = $testCondition[0];
+ ++$testConditionsCount;
+ }
+ }
+
+ if ($testConditionsCount > 1) {
+ $testConditionSet = 'AND(' . implode(',', $testConditions) . ')';
+ } elseif ($testConditionsCount == 1) {
+ $testConditionSet = $testConditions[0];
+ }
+
+ // Loop through each row of the database
+ foreach ($database as $dataRow => $dataValues) {
+ // Substitute actual values from the database row for our [:placeholders]
+ $testConditionList = $testConditionSet;
+ foreach ($criteriaNames as $key => $criteriaName) {
+ $k = array_search($criteriaName, $fieldNames);
+ if (isset($dataValues[$k])) {
+ $dataValue = $dataValues[$k];
+ $dataValue = (is_string($dataValue)) ? Calculation::wrapResult(strtoupper($dataValue)) : $dataValue;
+ $testConditionList = str_replace('[:' . $criteriaName . ']', $dataValue, $testConditionList);
+ }
+ }
+ // evaluate the criteria against the row data
+ $result = Calculation::getInstance()->_calculateFormulaValue('=' . $testConditionList);
+ // If the row failed to meet the criteria, remove it from the database
+ if (!$result) {
+ unset($database[$dataRow]);
+ }
+ }
+
+ return $database;
+ }
+
+ private static function getFilteredColumn($database, $field, $criteria)
+ {
+ // reduce the database to a set of rows that match all the criteria
+ $database = self::filter($database, $criteria);
+ // extract an array of values for the requested column
+ $colData = [];
+ foreach ($database as $row) {
+ $colData[] = $row[$field];
+ }
+
+ return $colData;
+ }
+
+ /**
+ * DAVERAGE.
+ *
+ * Averages the values in a column of a list or database that match conditions you specify.
+ *
+ * Excel Function:
+ * DAVERAGE(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return float
+ */
+ public static function DAVERAGE($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return Statistical::AVERAGE(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DCOUNT.
+ *
+ * Counts the cells that contain numbers in a column of a list or database that match conditions
+ * that you specify.
+ *
+ * Excel Function:
+ * DCOUNT(database,[field],criteria)
+ *
+ * Excel Function:
+ * DAVERAGE(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return int
+ *
+ * @TODO The field argument is optional. If field is omitted, DCOUNT counts all records in the
+ * database that match the criteria.
+ */
+ public static function DCOUNT($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return Statistical::COUNT(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DCOUNTA.
+ *
+ * Counts the nonblank cells in a column of a list or database that match conditions that you specify.
+ *
+ * Excel Function:
+ * DCOUNTA(database,[field],criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return int
+ *
+ * @TODO The field argument is optional. If field is omitted, DCOUNTA counts all records in the
+ * database that match the criteria.
+ */
+ public static function DCOUNTA($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // reduce the database to a set of rows that match all the criteria
+ $database = self::filter($database, $criteria);
+ // extract an array of values for the requested column
+ $colData = [];
+ foreach ($database as $row) {
+ $colData[] = $row[$field];
+ }
+
+ // Return
+ return Statistical::COUNTA(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DGET.
+ *
+ * Extracts a single value from a column of a list or database that matches conditions that you
+ * specify.
+ *
+ * Excel Function:
+ * DGET(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return mixed
+ */
+ public static function DGET($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ $colData = self::getFilteredColumn($database, $field, $criteria);
+ if (count($colData) > 1) {
+ return Functions::NAN();
+ }
+
+ return $colData[0];
+ }
+
+ /**
+ * DMAX.
+ *
+ * Returns the largest number in a column of a list or database that matches conditions you that
+ * specify.
+ *
+ * Excel Function:
+ * DMAX(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return float
+ */
+ public static function DMAX($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return Statistical::MAX(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DMIN.
+ *
+ * Returns the smallest number in a column of a list or database that matches conditions you that
+ * specify.
+ *
+ * Excel Function:
+ * DMIN(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return float
+ */
+ public static function DMIN($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return Statistical::MIN(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DPRODUCT.
+ *
+ * Multiplies the values in a column of a list or database that match conditions that you specify.
+ *
+ * Excel Function:
+ * DPRODUCT(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return float
+ */
+ public static function DPRODUCT($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return MathTrig::PRODUCT(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DSTDEV.
+ *
+ * Estimates the standard deviation of a population based on a sample by using the numbers in a
+ * column of a list or database that match conditions that you specify.
+ *
+ * Excel Function:
+ * DSTDEV(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return float
+ */
+ public static function DSTDEV($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return Statistical::STDEV(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DSTDEVP.
+ *
+ * Calculates the standard deviation of a population based on the entire population by using the
+ * numbers in a column of a list or database that match conditions that you specify.
+ *
+ * Excel Function:
+ * DSTDEVP(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return float
+ */
+ public static function DSTDEVP($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return Statistical::STDEVP(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DSUM.
+ *
+ * Adds the numbers in a column of a list or database that match conditions that you specify.
+ *
+ * Excel Function:
+ * DSUM(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return float
+ */
+ public static function DSUM($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return MathTrig::SUM(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DVAR.
+ *
+ * Estimates the variance of a population based on a sample by using the numbers in a column
+ * of a list or database that match conditions that you specify.
+ *
+ * Excel Function:
+ * DVAR(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return float
+ */
+ public static function DVAR($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return Statistical::VARFunc(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+
+ /**
+ * DVARP.
+ *
+ * Calculates the variance of a population based on the entire population by using the numbers
+ * in a column of a list or database that match conditions that you specify.
+ *
+ * Excel Function:
+ * DVARP(database,field,criteria)
+ *
+ * @category Database Functions
+ *
+ * @param mixed[] $database The range of cells that makes up the list or database.
+ * A database is a list of related data in which rows of related
+ * information are records, and columns of data are fields. The
+ * first row of the list contains labels for each column.
+ * @param int|string $field Indicates which column is used in the function. Enter the
+ * column label enclosed between double quotation marks, such as
+ * "Age" or "Yield," or a number (without quotation marks) that
+ * represents the position of the column within the list: 1 for
+ * the first column, 2 for the second column, and so on.
+ * @param mixed[] $criteria The range of cells that contains the conditions you specify.
+ * You can use any range for the criteria argument, as long as it
+ * includes at least one column label and at least one cell below
+ * the column label in which you specify a condition for the
+ * column.
+ *
+ * @return float
+ */
+ public static function DVARP($database, $field, $criteria)
+ {
+ $field = self::fieldExtract($database, $field);
+ if ($field === null) {
+ return null;
+ }
+
+ // Return
+ return Statistical::VARP(
+ self::getFilteredColumn($database, $field, $criteria)
+ );
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/DateTime.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/DateTime.php
new file mode 100644
index 00000000000..3c39db2af7f
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/DateTime.php
@@ -0,0 +1,1649 @@
+format('m');
+ $oYear = (int) $PHPDateObject->format('Y');
+
+ $adjustmentMonthsString = (string) $adjustmentMonths;
+ if ($adjustmentMonths > 0) {
+ $adjustmentMonthsString = '+' . $adjustmentMonths;
+ }
+ if ($adjustmentMonths != 0) {
+ $PHPDateObject->modify($adjustmentMonthsString . ' months');
+ }
+ $nMonth = (int) $PHPDateObject->format('m');
+ $nYear = (int) $PHPDateObject->format('Y');
+
+ $monthDiff = ($nMonth - $oMonth) + (($nYear - $oYear) * 12);
+ if ($monthDiff != $adjustmentMonths) {
+ $adjustDays = (int) $PHPDateObject->format('d');
+ $adjustDaysString = '-' . $adjustDays . ' days';
+ $PHPDateObject->modify($adjustDaysString);
+ }
+
+ return $PHPDateObject;
+ }
+
+ /**
+ * DATETIMENOW.
+ *
+ * Returns the current date and time.
+ * The NOW function is useful when you need to display the current date and time on a worksheet or
+ * calculate a value based on the current date and time, and have that value updated each time you
+ * open the worksheet.
+ *
+ * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the date
+ * and time format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
+ *
+ * Excel Function:
+ * NOW()
+ *
+ * @category Date/Time Functions
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function DATETIMENOW()
+ {
+ $saveTimeZone = date_default_timezone_get();
+ date_default_timezone_set('UTC');
+ $retValue = false;
+ switch (Functions::getReturnDateType()) {
+ case Functions::RETURNDATE_EXCEL:
+ $retValue = (float) Date::PHPToExcel(time());
+
+ break;
+ case Functions::RETURNDATE_PHP_NUMERIC:
+ $retValue = (int) time();
+
+ break;
+ case Functions::RETURNDATE_PHP_OBJECT:
+ $retValue = new \DateTime();
+
+ break;
+ }
+ date_default_timezone_set($saveTimeZone);
+
+ return $retValue;
+ }
+
+ /**
+ * DATENOW.
+ *
+ * Returns the current date.
+ * The NOW function is useful when you need to display the current date and time on a worksheet or
+ * calculate a value based on the current date and time, and have that value updated each time you
+ * open the worksheet.
+ *
+ * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the date
+ * and time format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
+ *
+ * Excel Function:
+ * TODAY()
+ *
+ * @category Date/Time Functions
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function DATENOW()
+ {
+ $saveTimeZone = date_default_timezone_get();
+ date_default_timezone_set('UTC');
+ $retValue = false;
+ $excelDateTime = floor(Date::PHPToExcel(time()));
+ switch (Functions::getReturnDateType()) {
+ case Functions::RETURNDATE_EXCEL:
+ $retValue = (float) $excelDateTime;
+
+ break;
+ case Functions::RETURNDATE_PHP_NUMERIC:
+ $retValue = (int) Date::excelToTimestamp($excelDateTime);
+
+ break;
+ case Functions::RETURNDATE_PHP_OBJECT:
+ $retValue = Date::excelToDateTimeObject($excelDateTime);
+
+ break;
+ }
+ date_default_timezone_set($saveTimeZone);
+
+ return $retValue;
+ }
+
+ /**
+ * DATE.
+ *
+ * The DATE function returns a value that represents a particular date.
+ *
+ * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the date
+ * format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
+ *
+ * Excel Function:
+ * DATE(year,month,day)
+ *
+ * PhpSpreadsheet is a lot more forgiving than MS Excel when passing non numeric values to this function.
+ * A Month name or abbreviation (English only at this point) such as 'January' or 'Jan' will still be accepted,
+ * as will a day value with a suffix (e.g. '21st' rather than simply 21); again only English language.
+ *
+ * @category Date/Time Functions
+ *
+ * @param int $year The value of the year argument can include one to four digits.
+ * Excel interprets the year argument according to the configured
+ * date system: 1900 or 1904.
+ * If year is between 0 (zero) and 1899 (inclusive), Excel adds that
+ * value to 1900 to calculate the year. For example, DATE(108,1,2)
+ * returns January 2, 2008 (1900+108).
+ * If year is between 1900 and 9999 (inclusive), Excel uses that
+ * value as the year. For example, DATE(2008,1,2) returns January 2,
+ * 2008.
+ * If year is less than 0 or is 10000 or greater, Excel returns the
+ * #NUM! error value.
+ * @param int $month A positive or negative integer representing the month of the year
+ * from 1 to 12 (January to December).
+ * If month is greater than 12, month adds that number of months to
+ * the first month in the year specified. For example, DATE(2008,14,2)
+ * returns the serial number representing February 2, 2009.
+ * If month is less than 1, month subtracts the magnitude of that
+ * number of months, plus 1, from the first month in the year
+ * specified. For example, DATE(2008,-3,2) returns the serial number
+ * representing September 2, 2007.
+ * @param int $day A positive or negative integer representing the day of the month
+ * from 1 to 31.
+ * If day is greater than the number of days in the month specified,
+ * day adds that number of days to the first day in the month. For
+ * example, DATE(2008,1,35) returns the serial number representing
+ * February 4, 2008.
+ * If day is less than 1, day subtracts the magnitude that number of
+ * days, plus one, from the first day of the month specified. For
+ * example, DATE(2008,1,-15) returns the serial number representing
+ * December 16, 2007.
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function DATE($year = 0, $month = 1, $day = 1)
+ {
+ $year = Functions::flattenSingleValue($year);
+ $month = Functions::flattenSingleValue($month);
+ $day = Functions::flattenSingleValue($day);
+
+ if (($month !== null) && (!is_numeric($month))) {
+ $month = Date::monthStringToNumber($month);
+ }
+
+ if (($day !== null) && (!is_numeric($day))) {
+ $day = Date::dayStringToNumber($day);
+ }
+
+ $year = ($year !== null) ? StringHelper::testStringAsNumeric($year) : 0;
+ $month = ($month !== null) ? StringHelper::testStringAsNumeric($month) : 0;
+ $day = ($day !== null) ? StringHelper::testStringAsNumeric($day) : 0;
+ if ((!is_numeric($year)) ||
+ (!is_numeric($month)) ||
+ (!is_numeric($day))) {
+ return Functions::VALUE();
+ }
+ $year = (int) $year;
+ $month = (int) $month;
+ $day = (int) $day;
+
+ $baseYear = Date::getExcelCalendar();
+ // Validate parameters
+ if ($year < ($baseYear - 1900)) {
+ return Functions::NAN();
+ }
+ if ((($baseYear - 1900) != 0) && ($year < $baseYear) && ($year >= 1900)) {
+ return Functions::NAN();
+ }
+
+ if (($year < $baseYear) && ($year >= ($baseYear - 1900))) {
+ $year += 1900;
+ }
+
+ if ($month < 1) {
+ // Handle year/month adjustment if month < 1
+ --$month;
+ $year += ceil($month / 12) - 1;
+ $month = 13 - abs($month % 12);
+ } elseif ($month > 12) {
+ // Handle year/month adjustment if month > 12
+ $year += floor($month / 12);
+ $month = ($month % 12);
+ }
+
+ // Re-validate the year parameter after adjustments
+ if (($year < $baseYear) || ($year >= 10000)) {
+ return Functions::NAN();
+ }
+
+ // Execute function
+ $excelDateValue = Date::formattedPHPToExcel($year, $month, $day);
+ switch (Functions::getReturnDateType()) {
+ case Functions::RETURNDATE_EXCEL:
+ return (float) $excelDateValue;
+ case Functions::RETURNDATE_PHP_NUMERIC:
+ return (int) Date::excelToTimestamp($excelDateValue);
+ case Functions::RETURNDATE_PHP_OBJECT:
+ return Date::excelToDateTimeObject($excelDateValue);
+ }
+ }
+
+ /**
+ * TIME.
+ *
+ * The TIME function returns a value that represents a particular time.
+ *
+ * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the time
+ * format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
+ *
+ * Excel Function:
+ * TIME(hour,minute,second)
+ *
+ * @category Date/Time Functions
+ *
+ * @param int $hour A number from 0 (zero) to 32767 representing the hour.
+ * Any value greater than 23 will be divided by 24 and the remainder
+ * will be treated as the hour value. For example, TIME(27,0,0) =
+ * TIME(3,0,0) = .125 or 3:00 AM.
+ * @param int $minute A number from 0 to 32767 representing the minute.
+ * Any value greater than 59 will be converted to hours and minutes.
+ * For example, TIME(0,750,0) = TIME(12,30,0) = .520833 or 12:30 PM.
+ * @param int $second A number from 0 to 32767 representing the second.
+ * Any value greater than 59 will be converted to hours, minutes,
+ * and seconds. For example, TIME(0,0,2000) = TIME(0,33,22) = .023148
+ * or 12:33:20 AM
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function TIME($hour = 0, $minute = 0, $second = 0)
+ {
+ $hour = Functions::flattenSingleValue($hour);
+ $minute = Functions::flattenSingleValue($minute);
+ $second = Functions::flattenSingleValue($second);
+
+ if ($hour == '') {
+ $hour = 0;
+ }
+ if ($minute == '') {
+ $minute = 0;
+ }
+ if ($second == '') {
+ $second = 0;
+ }
+
+ if ((!is_numeric($hour)) || (!is_numeric($minute)) || (!is_numeric($second))) {
+ return Functions::VALUE();
+ }
+ $hour = (int) $hour;
+ $minute = (int) $minute;
+ $second = (int) $second;
+
+ if ($second < 0) {
+ $minute += floor($second / 60);
+ $second = 60 - abs($second % 60);
+ if ($second == 60) {
+ $second = 0;
+ }
+ } elseif ($second >= 60) {
+ $minute += floor($second / 60);
+ $second = $second % 60;
+ }
+ if ($minute < 0) {
+ $hour += floor($minute / 60);
+ $minute = 60 - abs($minute % 60);
+ if ($minute == 60) {
+ $minute = 0;
+ }
+ } elseif ($minute >= 60) {
+ $hour += floor($minute / 60);
+ $minute = $minute % 60;
+ }
+
+ if ($hour > 23) {
+ $hour = $hour % 24;
+ } elseif ($hour < 0) {
+ return Functions::NAN();
+ }
+
+ // Execute function
+ switch (Functions::getReturnDateType()) {
+ case Functions::RETURNDATE_EXCEL:
+ $date = 0;
+ $calendar = Date::getExcelCalendar();
+ if ($calendar != Date::CALENDAR_WINDOWS_1900) {
+ $date = 1;
+ }
+
+ return (float) Date::formattedPHPToExcel($calendar, 1, $date, $hour, $minute, $second);
+ case Functions::RETURNDATE_PHP_NUMERIC:
+ return (int) Date::excelToTimestamp(Date::formattedPHPToExcel(1970, 1, 1, $hour, $minute, $second)); // -2147468400; // -2147472000 + 3600
+ case Functions::RETURNDATE_PHP_OBJECT:
+ $dayAdjust = 0;
+ if ($hour < 0) {
+ $dayAdjust = floor($hour / 24);
+ $hour = 24 - abs($hour % 24);
+ if ($hour == 24) {
+ $hour = 0;
+ }
+ } elseif ($hour >= 24) {
+ $dayAdjust = floor($hour / 24);
+ $hour = $hour % 24;
+ }
+ $phpDateObject = new \DateTime('1900-01-01 ' . $hour . ':' . $minute . ':' . $second);
+ if ($dayAdjust != 0) {
+ $phpDateObject->modify($dayAdjust . ' days');
+ }
+
+ return $phpDateObject;
+ }
+ }
+
+ /**
+ * DATEVALUE.
+ *
+ * Returns a value that represents a particular date.
+ * Use DATEVALUE to convert a date represented by a text string to an Excel or PHP date/time stamp
+ * value.
+ *
+ * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the date
+ * format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
+ *
+ * Excel Function:
+ * DATEVALUE(dateValue)
+ *
+ * @category Date/Time Functions
+ *
+ * @param string $dateValue Text that represents a date in a Microsoft Excel date format.
+ * For example, "1/30/2008" or "30-Jan-2008" are text strings within
+ * quotation marks that represent dates. Using the default date
+ * system in Excel for Windows, date_text must represent a date from
+ * January 1, 1900, to December 31, 9999. Using the default date
+ * system in Excel for the Macintosh, date_text must represent a date
+ * from January 1, 1904, to December 31, 9999. DATEVALUE returns the
+ * #VALUE! error value if date_text is out of this range.
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function DATEVALUE($dateValue = 1)
+ {
+ $dateValueOrig = $dateValue;
+ $dateValue = trim(Functions::flattenSingleValue($dateValue), '"');
+ // Strip any ordinals because they're allowed in Excel (English only)
+ $dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui', '$1$3', $dateValue);
+ // Convert separators (/ . or space) to hyphens (should also handle dot used for ordinals in some countries, e.g. Denmark, Germany)
+ $dateValue = str_replace(['/', '.', '-', ' '], ' ', $dateValue);
+
+ $yearFound = false;
+ $t1 = explode(' ', $dateValue);
+ foreach ($t1 as &$t) {
+ if ((is_numeric($t)) && ($t > 31)) {
+ if ($yearFound) {
+ return Functions::VALUE();
+ }
+ if ($t < 100) {
+ $t += 1900;
+ }
+ $yearFound = true;
+ }
+ }
+ if ((count($t1) == 1) && (strpos($t, ':') != false)) {
+ // We've been fed a time value without any date
+ return 0.0;
+ } elseif (count($t1) == 2) {
+ // We only have two parts of the date: either day/month or month/year
+ if ($yearFound) {
+ array_unshift($t1, 1);
+ } else {
+ if ($t1[1] > 29) {
+ $t1[1] += 1900;
+ array_unshift($t1, 1);
+ } else {
+ $t1[] = date('Y');
+ }
+ }
+ }
+ unset($t);
+ $dateValue = implode(' ', $t1);
+
+ $PHPDateArray = date_parse($dateValue);
+ if (($PHPDateArray === false) || ($PHPDateArray['error_count'] > 0)) {
+ $testVal1 = strtok($dateValue, '- ');
+ if ($testVal1 !== false) {
+ $testVal2 = strtok('- ');
+ if ($testVal2 !== false) {
+ $testVal3 = strtok('- ');
+ if ($testVal3 === false) {
+ $testVal3 = strftime('%Y');
+ }
+ } else {
+ return Functions::VALUE();
+ }
+ } else {
+ return Functions::VALUE();
+ }
+ if ($testVal1 < 31 && $testVal2 < 12 && $testVal3 < 12 && strlen($testVal3) == 2) {
+ $testVal3 += 2000;
+ }
+ $PHPDateArray = date_parse($testVal1 . '-' . $testVal2 . '-' . $testVal3);
+ if (($PHPDateArray === false) || ($PHPDateArray['error_count'] > 0)) {
+ $PHPDateArray = date_parse($testVal2 . '-' . $testVal1 . '-' . $testVal3);
+ if (($PHPDateArray === false) || ($PHPDateArray['error_count'] > 0)) {
+ return Functions::VALUE();
+ }
+ }
+ }
+
+ if (($PHPDateArray !== false) && ($PHPDateArray['error_count'] == 0)) {
+ // Execute function
+ if ($PHPDateArray['year'] == '') {
+ $PHPDateArray['year'] = strftime('%Y');
+ }
+ if ($PHPDateArray['year'] < 1900) {
+ return Functions::VALUE();
+ }
+ if ($PHPDateArray['month'] == '') {
+ $PHPDateArray['month'] = strftime('%m');
+ }
+ if ($PHPDateArray['day'] == '') {
+ $PHPDateArray['day'] = strftime('%d');
+ }
+ if (!checkdate($PHPDateArray['month'], $PHPDateArray['day'], $PHPDateArray['year'])) {
+ return Functions::VALUE();
+ }
+ $excelDateValue = floor(
+ Date::formattedPHPToExcel(
+ $PHPDateArray['year'],
+ $PHPDateArray['month'],
+ $PHPDateArray['day'],
+ $PHPDateArray['hour'],
+ $PHPDateArray['minute'],
+ $PHPDateArray['second']
+ )
+ );
+ switch (Functions::getReturnDateType()) {
+ case Functions::RETURNDATE_EXCEL:
+ return (float) $excelDateValue;
+ case Functions::RETURNDATE_PHP_NUMERIC:
+ return (int) Date::excelToTimestamp($excelDateValue);
+ case Functions::RETURNDATE_PHP_OBJECT:
+ return new \DateTime($PHPDateArray['year'] . '-' . $PHPDateArray['month'] . '-' . $PHPDateArray['day'] . ' 00:00:00');
+ }
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * TIMEVALUE.
+ *
+ * Returns a value that represents a particular time.
+ * Use TIMEVALUE to convert a time represented by a text string to an Excel or PHP date/time stamp
+ * value.
+ *
+ * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the time
+ * format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
+ *
+ * Excel Function:
+ * TIMEVALUE(timeValue)
+ *
+ * @category Date/Time Functions
+ *
+ * @param string $timeValue A text string that represents a time in any one of the Microsoft
+ * Excel time formats; for example, "6:45 PM" and "18:45" text strings
+ * within quotation marks that represent time.
+ * Date information in time_text is ignored.
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function TIMEVALUE($timeValue)
+ {
+ $timeValue = trim(Functions::flattenSingleValue($timeValue), '"');
+ $timeValue = str_replace(['/', '.'], '-', $timeValue);
+
+ $arraySplit = preg_split('/[\/:\-\s]/', $timeValue);
+ if ((count($arraySplit) == 2 || count($arraySplit) == 3) && $arraySplit[0] > 24) {
+ $arraySplit[0] = ($arraySplit[0] % 24);
+ $timeValue = implode(':', $arraySplit);
+ }
+
+ $PHPDateArray = date_parse($timeValue);
+ if (($PHPDateArray !== false) && ($PHPDateArray['error_count'] == 0)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ $excelDateValue = Date::formattedPHPToExcel(
+ $PHPDateArray['year'],
+ $PHPDateArray['month'],
+ $PHPDateArray['day'],
+ $PHPDateArray['hour'],
+ $PHPDateArray['minute'],
+ $PHPDateArray['second']
+ );
+ } else {
+ $excelDateValue = Date::formattedPHPToExcel(1900, 1, 1, $PHPDateArray['hour'], $PHPDateArray['minute'], $PHPDateArray['second']) - 1;
+ }
+
+ switch (Functions::getReturnDateType()) {
+ case Functions::RETURNDATE_EXCEL:
+ return (float) $excelDateValue;
+ case Functions::RETURNDATE_PHP_NUMERIC:
+ return (int) $phpDateValue = Date::excelToTimestamp($excelDateValue + 25569) - 3600;
+ case Functions::RETURNDATE_PHP_OBJECT:
+ return new \DateTime('1900-01-01 ' . $PHPDateArray['hour'] . ':' . $PHPDateArray['minute'] . ':' . $PHPDateArray['second']);
+ }
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * DATEDIF.
+ *
+ * @param mixed $startDate Excel date serial value, PHP date/time stamp, PHP DateTime object
+ * or a standard date string
+ * @param mixed $endDate Excel date serial value, PHP date/time stamp, PHP DateTime object
+ * or a standard date string
+ * @param string $unit
+ *
+ * @return int|string Interval between the dates
+ */
+ public static function DATEDIF($startDate = 0, $endDate = 0, $unit = 'D')
+ {
+ $startDate = Functions::flattenSingleValue($startDate);
+ $endDate = Functions::flattenSingleValue($endDate);
+ $unit = strtoupper(Functions::flattenSingleValue($unit));
+
+ if (is_string($startDate = self::getDateValue($startDate))) {
+ return Functions::VALUE();
+ }
+ if (is_string($endDate = self::getDateValue($endDate))) {
+ return Functions::VALUE();
+ }
+
+ // Validate parameters
+ if ($startDate > $endDate) {
+ return Functions::NAN();
+ }
+
+ // Execute function
+ $difference = $endDate - $startDate;
+
+ $PHPStartDateObject = Date::excelToDateTimeObject($startDate);
+ $startDays = $PHPStartDateObject->format('j');
+ $startMonths = $PHPStartDateObject->format('n');
+ $startYears = $PHPStartDateObject->format('Y');
+
+ $PHPEndDateObject = Date::excelToDateTimeObject($endDate);
+ $endDays = $PHPEndDateObject->format('j');
+ $endMonths = $PHPEndDateObject->format('n');
+ $endYears = $PHPEndDateObject->format('Y');
+
+ $retVal = Functions::NAN();
+ switch ($unit) {
+ case 'D':
+ $retVal = (int) $difference;
+
+ break;
+ case 'M':
+ $retVal = (int) ($endMonths - $startMonths) + ((int) ($endYears - $startYears) * 12);
+ // We're only interested in full months
+ if ($endDays < $startDays) {
+ --$retVal;
+ }
+
+ break;
+ case 'Y':
+ $retVal = (int) ($endYears - $startYears);
+ // We're only interested in full months
+ if ($endMonths < $startMonths) {
+ --$retVal;
+ } elseif (($endMonths == $startMonths) && ($endDays < $startDays)) {
+ // Remove start month
+ --$retVal;
+ // Remove end month
+ --$retVal;
+ }
+
+ break;
+ case 'MD':
+ if ($endDays < $startDays) {
+ $retVal = $endDays;
+ $PHPEndDateObject->modify('-' . $endDays . ' days');
+ $adjustDays = $PHPEndDateObject->format('j');
+ $retVal += ($adjustDays - $startDays);
+ } else {
+ $retVal = $endDays - $startDays;
+ }
+
+ break;
+ case 'YM':
+ $retVal = (int) ($endMonths - $startMonths);
+ if ($retVal < 0) {
+ $retVal += 12;
+ }
+ // We're only interested in full months
+ if ($endDays < $startDays) {
+ --$retVal;
+ }
+
+ break;
+ case 'YD':
+ $retVal = (int) $difference;
+ if ($endYears > $startYears) {
+ $isLeapStartYear = $PHPStartDateObject->format('L');
+ $wasLeapEndYear = $PHPEndDateObject->format('L');
+
+ // Adjust end year to be as close as possible as start year
+ while ($PHPEndDateObject >= $PHPStartDateObject) {
+ $PHPEndDateObject->modify('-1 year');
+ $endYears = $PHPEndDateObject->format('Y');
+ }
+ $PHPEndDateObject->modify('+1 year');
+
+ // Get the result
+ $retVal = $PHPEndDateObject->diff($PHPStartDateObject)->days;
+
+ // Adjust for leap years cases
+ $isLeapEndYear = $PHPEndDateObject->format('L');
+ $limit = new \DateTime($PHPEndDateObject->format('Y-02-29'));
+ if (!$isLeapStartYear && !$wasLeapEndYear && $isLeapEndYear && $PHPEndDateObject >= $limit) {
+ --$retVal;
+ }
+ }
+
+ break;
+ default:
+ $retVal = Functions::VALUE();
+ }
+
+ return $retVal;
+ }
+
+ /**
+ * DAYS.
+ *
+ * Returns the number of days between two dates
+ *
+ * Excel Function:
+ * DAYS(endDate, startDate)
+ *
+ * @category Date/Time Functions
+ *
+ * @param \DateTimeImmutable|float|int|string $endDate Excel date serial value (float),
+ * PHP date timestamp (integer), PHP DateTime object, or a standard date string
+ * @param \DateTimeImmutable|float|int|string $startDate Excel date serial value (float),
+ * PHP date timestamp (integer), PHP DateTime object, or a standard date string
+ *
+ * @return int|string Number of days between start date and end date or an error
+ */
+ public static function DAYS($endDate = 0, $startDate = 0)
+ {
+ $startDate = Functions::flattenSingleValue($startDate);
+ $endDate = Functions::flattenSingleValue($endDate);
+
+ $startDate = self::getDateValue($startDate);
+ if (is_string($startDate)) {
+ return Functions::VALUE();
+ }
+
+ $endDate = self::getDateValue($endDate);
+ if (is_string($endDate)) {
+ return Functions::VALUE();
+ }
+
+ // Execute function
+ $PHPStartDateObject = Date::excelToDateTimeObject($startDate);
+ $PHPEndDateObject = Date::excelToDateTimeObject($endDate);
+
+ $diff = $PHPStartDateObject->diff($PHPEndDateObject);
+ $days = $diff->days;
+
+ if ($diff->invert) {
+ $days = -$days;
+ }
+
+ return $days;
+ }
+
+ /**
+ * DAYS360.
+ *
+ * Returns the number of days between two dates based on a 360-day year (twelve 30-day months),
+ * which is used in some accounting calculations. Use this function to help compute payments if
+ * your accounting system is based on twelve 30-day months.
+ *
+ * Excel Function:
+ * DAYS360(startDate,endDate[,method])
+ *
+ * @category Date/Time Functions
+ *
+ * @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param mixed $endDate Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param bool $method US or European Method
+ * FALSE or omitted: U.S. (NASD) method. If the starting date is
+ * the last day of a month, it becomes equal to the 30th of the
+ * same month. If the ending date is the last day of a month and
+ * the starting date is earlier than the 30th of a month, the
+ * ending date becomes equal to the 1st of the next month;
+ * otherwise the ending date becomes equal to the 30th of the
+ * same month.
+ * TRUE: European method. Starting dates and ending dates that
+ * occur on the 31st of a month become equal to the 30th of the
+ * same month.
+ *
+ * @return int|string Number of days between start date and end date
+ */
+ public static function DAYS360($startDate = 0, $endDate = 0, $method = false)
+ {
+ $startDate = Functions::flattenSingleValue($startDate);
+ $endDate = Functions::flattenSingleValue($endDate);
+
+ if (is_string($startDate = self::getDateValue($startDate))) {
+ return Functions::VALUE();
+ }
+ if (is_string($endDate = self::getDateValue($endDate))) {
+ return Functions::VALUE();
+ }
+
+ if (!is_bool($method)) {
+ return Functions::VALUE();
+ }
+
+ // Execute function
+ $PHPStartDateObject = Date::excelToDateTimeObject($startDate);
+ $startDay = $PHPStartDateObject->format('j');
+ $startMonth = $PHPStartDateObject->format('n');
+ $startYear = $PHPStartDateObject->format('Y');
+
+ $PHPEndDateObject = Date::excelToDateTimeObject($endDate);
+ $endDay = $PHPEndDateObject->format('j');
+ $endMonth = $PHPEndDateObject->format('n');
+ $endYear = $PHPEndDateObject->format('Y');
+
+ return self::dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, !$method);
+ }
+
+ /**
+ * YEARFRAC.
+ *
+ * Calculates the fraction of the year represented by the number of whole days between two dates
+ * (the start_date and the end_date).
+ * Use the YEARFRAC worksheet function to identify the proportion of a whole year's benefits or
+ * obligations to assign to a specific term.
+ *
+ * Excel Function:
+ * YEARFRAC(startDate,endDate[,method])
+ *
+ * @category Date/Time Functions
+ *
+ * @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param mixed $endDate Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param int $method Method used for the calculation
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float fraction of the year
+ */
+ public static function YEARFRAC($startDate = 0, $endDate = 0, $method = 0)
+ {
+ $startDate = Functions::flattenSingleValue($startDate);
+ $endDate = Functions::flattenSingleValue($endDate);
+ $method = Functions::flattenSingleValue($method);
+
+ if (is_string($startDate = self::getDateValue($startDate))) {
+ return Functions::VALUE();
+ }
+ if (is_string($endDate = self::getDateValue($endDate))) {
+ return Functions::VALUE();
+ }
+
+ if (((is_numeric($method)) && (!is_string($method))) || ($method == '')) {
+ switch ($method) {
+ case 0:
+ return self::DAYS360($startDate, $endDate) / 360;
+ case 1:
+ $days = self::DATEDIF($startDate, $endDate);
+ $startYear = self::YEAR($startDate);
+ $endYear = self::YEAR($endDate);
+ $years = $endYear - $startYear + 1;
+ $leapDays = 0;
+ if ($years == 1) {
+ if (self::isLeapYear($endYear)) {
+ $startMonth = self::MONTHOFYEAR($startDate);
+ $endMonth = self::MONTHOFYEAR($endDate);
+ $endDay = self::DAYOFMONTH($endDate);
+ if (($startMonth < 3) ||
+ (($endMonth * 100 + $endDay) >= (2 * 100 + 29))) {
+ $leapDays += 1;
+ }
+ }
+ } else {
+ for ($year = $startYear; $year <= $endYear; ++$year) {
+ if ($year == $startYear) {
+ $startMonth = self::MONTHOFYEAR($startDate);
+ $startDay = self::DAYOFMONTH($startDate);
+ if ($startMonth < 3) {
+ $leapDays += (self::isLeapYear($year)) ? 1 : 0;
+ }
+ } elseif ($year == $endYear) {
+ $endMonth = self::MONTHOFYEAR($endDate);
+ $endDay = self::DAYOFMONTH($endDate);
+ if (($endMonth * 100 + $endDay) >= (2 * 100 + 29)) {
+ $leapDays += (self::isLeapYear($year)) ? 1 : 0;
+ }
+ } else {
+ $leapDays += (self::isLeapYear($year)) ? 1 : 0;
+ }
+ }
+ if ($years == 2) {
+ if (($leapDays == 0) && (self::isLeapYear($startYear)) && ($days > 365)) {
+ $leapDays = 1;
+ } elseif ($days < 366) {
+ $years = 1;
+ }
+ }
+ $leapDays /= $years;
+ }
+
+ return $days / (365 + $leapDays);
+ case 2:
+ return self::DATEDIF($startDate, $endDate) / 360;
+ case 3:
+ return self::DATEDIF($startDate, $endDate) / 365;
+ case 4:
+ return self::DAYS360($startDate, $endDate, true) / 360;
+ }
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * NETWORKDAYS.
+ *
+ * Returns the number of whole working days between start_date and end_date. Working days
+ * exclude weekends and any dates identified in holidays.
+ * Use NETWORKDAYS to calculate employee benefits that accrue based on the number of days
+ * worked during a specific term.
+ *
+ * Excel Function:
+ * NETWORKDAYS(startDate,endDate[,holidays[,holiday[,...]]])
+ *
+ * @category Date/Time Functions
+ *
+ * @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param mixed $endDate Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ *
+ * @return int|string Interval between the dates
+ */
+ public static function NETWORKDAYS($startDate, $endDate, ...$dateArgs)
+ {
+ // Retrieve the mandatory start and end date that are referenced in the function definition
+ $startDate = Functions::flattenSingleValue($startDate);
+ $endDate = Functions::flattenSingleValue($endDate);
+ // Get the optional days
+ $dateArgs = Functions::flattenArray($dateArgs);
+
+ // Validate the start and end dates
+ if (is_string($startDate = $sDate = self::getDateValue($startDate))) {
+ return Functions::VALUE();
+ }
+ $startDate = (float) floor($startDate);
+ if (is_string($endDate = $eDate = self::getDateValue($endDate))) {
+ return Functions::VALUE();
+ }
+ $endDate = (float) floor($endDate);
+
+ if ($sDate > $eDate) {
+ $startDate = $eDate;
+ $endDate = $sDate;
+ }
+
+ // Execute function
+ $startDoW = 6 - self::WEEKDAY($startDate, 2);
+ if ($startDoW < 0) {
+ $startDoW = 0;
+ }
+ $endDoW = self::WEEKDAY($endDate, 2);
+ if ($endDoW >= 6) {
+ $endDoW = 0;
+ }
+
+ $wholeWeekDays = floor(($endDate - $startDate) / 7) * 5;
+ $partWeekDays = $endDoW + $startDoW;
+ if ($partWeekDays > 5) {
+ $partWeekDays -= 5;
+ }
+
+ // Test any extra holiday parameters
+ $holidayCountedArray = [];
+ foreach ($dateArgs as $holidayDate) {
+ if (is_string($holidayDate = self::getDateValue($holidayDate))) {
+ return Functions::VALUE();
+ }
+ if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
+ if ((self::WEEKDAY($holidayDate, 2) < 6) && (!in_array($holidayDate, $holidayCountedArray))) {
+ --$partWeekDays;
+ $holidayCountedArray[] = $holidayDate;
+ }
+ }
+ }
+
+ if ($sDate > $eDate) {
+ return 0 - ($wholeWeekDays + $partWeekDays);
+ }
+
+ return $wholeWeekDays + $partWeekDays;
+ }
+
+ /**
+ * WORKDAY.
+ *
+ * Returns the date that is the indicated number of working days before or after a date (the
+ * starting date). Working days exclude weekends and any dates identified as holidays.
+ * Use WORKDAY to exclude weekends or holidays when you calculate invoice due dates, expected
+ * delivery times, or the number of days of work performed.
+ *
+ * Excel Function:
+ * WORKDAY(startDate,endDays[,holidays[,holiday[,...]]])
+ *
+ * @category Date/Time Functions
+ *
+ * @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param int $endDays The number of nonweekend and nonholiday days before or after
+ * startDate. A positive value for days yields a future date; a
+ * negative value yields a past date.
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function WORKDAY($startDate, $endDays, ...$dateArgs)
+ {
+ // Retrieve the mandatory start date and days that are referenced in the function definition
+ $startDate = Functions::flattenSingleValue($startDate);
+ $endDays = Functions::flattenSingleValue($endDays);
+ // Get the optional days
+ $dateArgs = Functions::flattenArray($dateArgs);
+
+ if ((is_string($startDate = self::getDateValue($startDate))) || (!is_numeric($endDays))) {
+ return Functions::VALUE();
+ }
+ $startDate = (float) floor($startDate);
+ $endDays = (int) floor($endDays);
+ // If endDays is 0, we always return startDate
+ if ($endDays == 0) {
+ return $startDate;
+ }
+
+ $decrementing = $endDays < 0;
+
+ // Adjust the start date if it falls over a weekend
+
+ $startDoW = self::WEEKDAY($startDate, 3);
+ if (self::WEEKDAY($startDate, 3) >= 5) {
+ $startDate += ($decrementing) ? -$startDoW + 4 : 7 - $startDoW;
+ ($decrementing) ? $endDays++ : $endDays--;
+ }
+
+ // Add endDays
+ $endDate = (float) $startDate + ((int) ($endDays / 5) * 7) + ($endDays % 5);
+
+ // Adjust the calculated end date if it falls over a weekend
+ $endDoW = self::WEEKDAY($endDate, 3);
+ if ($endDoW >= 5) {
+ $endDate += ($decrementing) ? -$endDoW + 4 : 7 - $endDoW;
+ }
+
+ // Test any extra holiday parameters
+ if (!empty($dateArgs)) {
+ $holidayCountedArray = $holidayDates = [];
+ foreach ($dateArgs as $holidayDate) {
+ if (($holidayDate !== null) && (trim($holidayDate) > '')) {
+ if (is_string($holidayDate = self::getDateValue($holidayDate))) {
+ return Functions::VALUE();
+ }
+ if (self::WEEKDAY($holidayDate, 3) < 5) {
+ $holidayDates[] = $holidayDate;
+ }
+ }
+ }
+ if ($decrementing) {
+ rsort($holidayDates, SORT_NUMERIC);
+ } else {
+ sort($holidayDates, SORT_NUMERIC);
+ }
+ foreach ($holidayDates as $holidayDate) {
+ if ($decrementing) {
+ if (($holidayDate <= $startDate) && ($holidayDate >= $endDate)) {
+ if (!in_array($holidayDate, $holidayCountedArray)) {
+ --$endDate;
+ $holidayCountedArray[] = $holidayDate;
+ }
+ }
+ } else {
+ if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
+ if (!in_array($holidayDate, $holidayCountedArray)) {
+ ++$endDate;
+ $holidayCountedArray[] = $holidayDate;
+ }
+ }
+ }
+ // Adjust the calculated end date if it falls over a weekend
+ $endDoW = self::WEEKDAY($endDate, 3);
+ if ($endDoW >= 5) {
+ $endDate += ($decrementing) ? -$endDoW + 4 : 7 - $endDoW;
+ }
+ }
+ }
+
+ switch (Functions::getReturnDateType()) {
+ case Functions::RETURNDATE_EXCEL:
+ return (float) $endDate;
+ case Functions::RETURNDATE_PHP_NUMERIC:
+ return (int) Date::excelToTimestamp($endDate);
+ case Functions::RETURNDATE_PHP_OBJECT:
+ return Date::excelToDateTimeObject($endDate);
+ }
+ }
+
+ /**
+ * DAYOFMONTH.
+ *
+ * Returns the day of the month, for a specified date. The day is given as an integer
+ * ranging from 1 to 31.
+ *
+ * Excel Function:
+ * DAY(dateValue)
+ *
+ * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ *
+ * @return int|string Day of the month
+ */
+ public static function DAYOFMONTH($dateValue = 1)
+ {
+ $dateValue = Functions::flattenSingleValue($dateValue);
+
+ if ($dateValue === null) {
+ $dateValue = 1;
+ } elseif (is_string($dateValue = self::getDateValue($dateValue))) {
+ return Functions::VALUE();
+ }
+
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
+ if ($dateValue < 0.0) {
+ return Functions::NAN();
+ } elseif ($dateValue < 1.0) {
+ return 0;
+ }
+ }
+
+ // Execute function
+ $PHPDateObject = Date::excelToDateTimeObject($dateValue);
+
+ return (int) $PHPDateObject->format('j');
+ }
+
+ /**
+ * WEEKDAY.
+ *
+ * Returns the day of the week for a specified date. The day is given as an integer
+ * ranging from 0 to 7 (dependent on the requested style).
+ *
+ * Excel Function:
+ * WEEKDAY(dateValue[,style])
+ *
+ * @param int $dateValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param int $style A number that determines the type of return value
+ * 1 or omitted Numbers 1 (Sunday) through 7 (Saturday).
+ * 2 Numbers 1 (Monday) through 7 (Sunday).
+ * 3 Numbers 0 (Monday) through 6 (Sunday).
+ *
+ * @return int|string Day of the week value
+ */
+ public static function WEEKDAY($dateValue = 1, $style = 1)
+ {
+ $dateValue = Functions::flattenSingleValue($dateValue);
+ $style = Functions::flattenSingleValue($style);
+
+ if (!is_numeric($style)) {
+ return Functions::VALUE();
+ } elseif (($style < 1) || ($style > 3)) {
+ return Functions::NAN();
+ }
+ $style = floor($style);
+
+ if ($dateValue === null) {
+ $dateValue = 1;
+ } elseif (is_string($dateValue = self::getDateValue($dateValue))) {
+ return Functions::VALUE();
+ } elseif ($dateValue < 0.0) {
+ return Functions::NAN();
+ }
+
+ // Execute function
+ $PHPDateObject = Date::excelToDateTimeObject($dateValue);
+ $DoW = $PHPDateObject->format('w');
+
+ $firstDay = 1;
+ switch ($style) {
+ case 1:
+ ++$DoW;
+
+ break;
+ case 2:
+ if ($DoW == 0) {
+ $DoW = 7;
+ }
+
+ break;
+ case 3:
+ if ($DoW == 0) {
+ $DoW = 7;
+ }
+ $firstDay = 0;
+ --$DoW;
+
+ break;
+ }
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
+ // Test for Excel's 1900 leap year, and introduce the error as required
+ if (($PHPDateObject->format('Y') == 1900) && ($PHPDateObject->format('n') <= 2)) {
+ --$DoW;
+ if ($DoW < $firstDay) {
+ $DoW += 7;
+ }
+ }
+ }
+
+ return (int) $DoW;
+ }
+
+ /**
+ * WEEKNUM.
+ *
+ * Returns the week of the year for a specified date.
+ * The WEEKNUM function considers the week containing January 1 to be the first week of the year.
+ * However, there is a European standard that defines the first week as the one with the majority
+ * of days (four or more) falling in the new year. This means that for years in which there are
+ * three days or less in the first week of January, the WEEKNUM function returns week numbers
+ * that are incorrect according to the European standard.
+ *
+ * Excel Function:
+ * WEEKNUM(dateValue[,style])
+ *
+ * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param int $method Week begins on Sunday or Monday
+ * 1 or omitted Week begins on Sunday.
+ * 2 Week begins on Monday.
+ *
+ * @return int|string Week Number
+ */
+ public static function WEEKNUM($dateValue = 1, $method = 1)
+ {
+ $dateValue = Functions::flattenSingleValue($dateValue);
+ $method = Functions::flattenSingleValue($method);
+
+ if (!is_numeric($method)) {
+ return Functions::VALUE();
+ } elseif (($method < 1) || ($method > 2)) {
+ return Functions::NAN();
+ }
+ $method = floor($method);
+
+ if ($dateValue === null) {
+ $dateValue = 1;
+ } elseif (is_string($dateValue = self::getDateValue($dateValue))) {
+ return Functions::VALUE();
+ } elseif ($dateValue < 0.0) {
+ return Functions::NAN();
+ }
+
+ // Execute function
+ $PHPDateObject = Date::excelToDateTimeObject($dateValue);
+ $dayOfYear = $PHPDateObject->format('z');
+ $PHPDateObject->modify('-' . $dayOfYear . ' days');
+ $firstDayOfFirstWeek = $PHPDateObject->format('w');
+ $daysInFirstWeek = (6 - $firstDayOfFirstWeek + $method) % 7;
+ $interval = $dayOfYear - $daysInFirstWeek;
+ $weekOfYear = floor($interval / 7) + 1;
+
+ if ($daysInFirstWeek) {
+ ++$weekOfYear;
+ }
+
+ return (int) $weekOfYear;
+ }
+
+ /**
+ * ISOWEEKNUM.
+ *
+ * Returns the ISO 8601 week number of the year for a specified date.
+ *
+ * Excel Function:
+ * ISOWEEKNUM(dateValue)
+ *
+ * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ *
+ * @return int|string Week Number
+ */
+ public static function ISOWEEKNUM($dateValue = 1)
+ {
+ $dateValue = Functions::flattenSingleValue($dateValue);
+
+ if ($dateValue === null) {
+ $dateValue = 1;
+ } elseif (is_string($dateValue = self::getDateValue($dateValue))) {
+ return Functions::VALUE();
+ } elseif ($dateValue < 0.0) {
+ return Functions::NAN();
+ }
+
+ // Execute function
+ $PHPDateObject = Date::excelToDateTimeObject($dateValue);
+
+ return (int) $PHPDateObject->format('W');
+ }
+
+ /**
+ * MONTHOFYEAR.
+ *
+ * Returns the month of a date represented by a serial number.
+ * The month is given as an integer, ranging from 1 (January) to 12 (December).
+ *
+ * Excel Function:
+ * MONTH(dateValue)
+ *
+ * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ *
+ * @return int|string Month of the year
+ */
+ public static function MONTHOFYEAR($dateValue = 1)
+ {
+ $dateValue = Functions::flattenSingleValue($dateValue);
+
+ if (empty($dateValue)) {
+ $dateValue = 1;
+ }
+ if (is_string($dateValue = self::getDateValue($dateValue))) {
+ return Functions::VALUE();
+ } elseif ($dateValue < 0.0) {
+ return Functions::NAN();
+ }
+
+ // Execute function
+ $PHPDateObject = Date::excelToDateTimeObject($dateValue);
+
+ return (int) $PHPDateObject->format('n');
+ }
+
+ /**
+ * YEAR.
+ *
+ * Returns the year corresponding to a date.
+ * The year is returned as an integer in the range 1900-9999.
+ *
+ * Excel Function:
+ * YEAR(dateValue)
+ *
+ * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ *
+ * @return int|string Year
+ */
+ public static function YEAR($dateValue = 1)
+ {
+ $dateValue = Functions::flattenSingleValue($dateValue);
+
+ if ($dateValue === null) {
+ $dateValue = 1;
+ } elseif (is_string($dateValue = self::getDateValue($dateValue))) {
+ return Functions::VALUE();
+ } elseif ($dateValue < 0.0) {
+ return Functions::NAN();
+ }
+
+ // Execute function
+ $PHPDateObject = Date::excelToDateTimeObject($dateValue);
+
+ return (int) $PHPDateObject->format('Y');
+ }
+
+ /**
+ * HOUROFDAY.
+ *
+ * Returns the hour of a time value.
+ * The hour is given as an integer, ranging from 0 (12:00 A.M.) to 23 (11:00 P.M.).
+ *
+ * Excel Function:
+ * HOUR(timeValue)
+ *
+ * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard time string
+ *
+ * @return int|string Hour
+ */
+ public static function HOUROFDAY($timeValue = 0)
+ {
+ $timeValue = Functions::flattenSingleValue($timeValue);
+
+ if (!is_numeric($timeValue)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
+ $testVal = strtok($timeValue, '/-: ');
+ if (strlen($testVal) < strlen($timeValue)) {
+ return Functions::VALUE();
+ }
+ }
+ $timeValue = self::getTimeValue($timeValue);
+ if (is_string($timeValue)) {
+ return Functions::VALUE();
+ }
+ }
+ // Execute function
+ if ($timeValue >= 1) {
+ $timeValue = fmod($timeValue, 1);
+ } elseif ($timeValue < 0.0) {
+ return Functions::NAN();
+ }
+ $timeValue = Date::excelToTimestamp($timeValue);
+
+ return (int) gmdate('G', $timeValue);
+ }
+
+ /**
+ * MINUTE.
+ *
+ * Returns the minutes of a time value.
+ * The minute is given as an integer, ranging from 0 to 59.
+ *
+ * Excel Function:
+ * MINUTE(timeValue)
+ *
+ * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard time string
+ *
+ * @return int|string Minute
+ */
+ public static function MINUTE($timeValue = 0)
+ {
+ $timeValue = $timeTester = Functions::flattenSingleValue($timeValue);
+
+ if (!is_numeric($timeValue)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
+ $testVal = strtok($timeValue, '/-: ');
+ if (strlen($testVal) < strlen($timeValue)) {
+ return Functions::VALUE();
+ }
+ }
+ $timeValue = self::getTimeValue($timeValue);
+ if (is_string($timeValue)) {
+ return Functions::VALUE();
+ }
+ }
+ // Execute function
+ if ($timeValue >= 1) {
+ $timeValue = fmod($timeValue, 1);
+ } elseif ($timeValue < 0.0) {
+ return Functions::NAN();
+ }
+ $timeValue = Date::excelToTimestamp($timeValue);
+
+ return (int) gmdate('i', $timeValue);
+ }
+
+ /**
+ * SECOND.
+ *
+ * Returns the seconds of a time value.
+ * The second is given as an integer in the range 0 (zero) to 59.
+ *
+ * Excel Function:
+ * SECOND(timeValue)
+ *
+ * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard time string
+ *
+ * @return int|string Second
+ */
+ public static function SECOND($timeValue = 0)
+ {
+ $timeValue = Functions::flattenSingleValue($timeValue);
+
+ if (!is_numeric($timeValue)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
+ $testVal = strtok($timeValue, '/-: ');
+ if (strlen($testVal) < strlen($timeValue)) {
+ return Functions::VALUE();
+ }
+ }
+ $timeValue = self::getTimeValue($timeValue);
+ if (is_string($timeValue)) {
+ return Functions::VALUE();
+ }
+ }
+ // Execute function
+ if ($timeValue >= 1) {
+ $timeValue = fmod($timeValue, 1);
+ } elseif ($timeValue < 0.0) {
+ return Functions::NAN();
+ }
+ $timeValue = Date::excelToTimestamp($timeValue);
+
+ return (int) gmdate('s', $timeValue);
+ }
+
+ /**
+ * EDATE.
+ *
+ * Returns the serial number that represents the date that is the indicated number of months
+ * before or after a specified date (the start_date).
+ * Use EDATE to calculate maturity dates or due dates that fall on the same day of the month
+ * as the date of issue.
+ *
+ * Excel Function:
+ * EDATE(dateValue,adjustmentMonths)
+ *
+ * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param int $adjustmentMonths The number of months before or after start_date.
+ * A positive value for months yields a future date;
+ * a negative value yields a past date.
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function EDATE($dateValue = 1, $adjustmentMonths = 0)
+ {
+ $dateValue = Functions::flattenSingleValue($dateValue);
+ $adjustmentMonths = Functions::flattenSingleValue($adjustmentMonths);
+
+ if (!is_numeric($adjustmentMonths)) {
+ return Functions::VALUE();
+ }
+ $adjustmentMonths = floor($adjustmentMonths);
+
+ if (is_string($dateValue = self::getDateValue($dateValue))) {
+ return Functions::VALUE();
+ }
+
+ // Execute function
+ $PHPDateObject = self::adjustDateByMonths($dateValue, $adjustmentMonths);
+
+ switch (Functions::getReturnDateType()) {
+ case Functions::RETURNDATE_EXCEL:
+ return (float) Date::PHPToExcel($PHPDateObject);
+ case Functions::RETURNDATE_PHP_NUMERIC:
+ return (int) Date::excelToTimestamp(Date::PHPToExcel($PHPDateObject));
+ case Functions::RETURNDATE_PHP_OBJECT:
+ return $PHPDateObject;
+ }
+ }
+
+ /**
+ * EOMONTH.
+ *
+ * Returns the date value for the last day of the month that is the indicated number of months
+ * before or after start_date.
+ * Use EOMONTH to calculate maturity dates or due dates that fall on the last day of the month.
+ *
+ * Excel Function:
+ * EOMONTH(dateValue,adjustmentMonths)
+ *
+ * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
+ * PHP DateTime object, or a standard date string
+ * @param int $adjustmentMonths The number of months before or after start_date.
+ * A positive value for months yields a future date;
+ * a negative value yields a past date.
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function EOMONTH($dateValue = 1, $adjustmentMonths = 0)
+ {
+ $dateValue = Functions::flattenSingleValue($dateValue);
+ $adjustmentMonths = Functions::flattenSingleValue($adjustmentMonths);
+
+ if (!is_numeric($adjustmentMonths)) {
+ return Functions::VALUE();
+ }
+ $adjustmentMonths = floor($adjustmentMonths);
+
+ if (is_string($dateValue = self::getDateValue($dateValue))) {
+ return Functions::VALUE();
+ }
+
+ // Execute function
+ $PHPDateObject = self::adjustDateByMonths($dateValue, $adjustmentMonths + 1);
+ $adjustDays = (int) $PHPDateObject->format('d');
+ $adjustDaysString = '-' . $adjustDays . ' days';
+ $PHPDateObject->modify($adjustDaysString);
+
+ switch (Functions::getReturnDateType()) {
+ case Functions::RETURNDATE_EXCEL:
+ return (float) Date::PHPToExcel($PHPDateObject);
+ case Functions::RETURNDATE_PHP_NUMERIC:
+ return (int) Date::excelToTimestamp(Date::PHPToExcel($PHPDateObject));
+ case Functions::RETURNDATE_PHP_OBJECT:
+ return $PHPDateObject;
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Engine/CyclicReferenceStack.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Engine/CyclicReferenceStack.php
new file mode 100644
index 00000000000..5a54d83ac03
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Engine/CyclicReferenceStack.php
@@ -0,0 +1,73 @@
+stack);
+ }
+
+ /**
+ * Push a new entry onto the stack.
+ *
+ * @param mixed $value
+ */
+ public function push($value)
+ {
+ $this->stack[$value] = $value;
+ }
+
+ /**
+ * Pop the last entry from the stack.
+ *
+ * @return mixed
+ */
+ public function pop()
+ {
+ return array_pop($this->stack);
+ }
+
+ /**
+ * Test to see if a specified entry exists on the stack.
+ *
+ * @param mixed $value The value to test
+ *
+ * @return bool
+ */
+ public function onStack($value)
+ {
+ return isset($this->stack[$value]);
+ }
+
+ /**
+ * Clear the stack.
+ */
+ public function clear()
+ {
+ $this->stack = [];
+ }
+
+ /**
+ * Return an array of all entries on the stack.
+ *
+ * @return mixed[]
+ */
+ public function showStack()
+ {
+ return $this->stack;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Engine/Logger.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Engine/Logger.php
new file mode 100644
index 00000000000..6793dade78e
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Engine/Logger.php
@@ -0,0 +1,128 @@
+cellStack = $stack;
+ }
+
+ /**
+ * Enable/Disable Calculation engine logging.
+ *
+ * @param bool $pValue
+ */
+ public function setWriteDebugLog($pValue)
+ {
+ $this->writeDebugLog = $pValue;
+ }
+
+ /**
+ * Return whether calculation engine logging is enabled or disabled.
+ *
+ * @return bool
+ */
+ public function getWriteDebugLog()
+ {
+ return $this->writeDebugLog;
+ }
+
+ /**
+ * Enable/Disable echoing of debug log information.
+ *
+ * @param bool $pValue
+ */
+ public function setEchoDebugLog($pValue)
+ {
+ $this->echoDebugLog = $pValue;
+ }
+
+ /**
+ * Return whether echoing of debug log information is enabled or disabled.
+ *
+ * @return bool
+ */
+ public function getEchoDebugLog()
+ {
+ return $this->echoDebugLog;
+ }
+
+ /**
+ * Write an entry to the calculation engine debug log.
+ */
+ public function writeDebugLog(...$args)
+ {
+ // Only write the debug log if logging is enabled
+ if ($this->writeDebugLog) {
+ $message = implode($args);
+ $cellReference = implode(' -> ', $this->cellStack->showStack());
+ if ($this->echoDebugLog) {
+ echo $cellReference,
+ ($this->cellStack->count() > 0 ? ' => ' : ''),
+ $message,
+ PHP_EOL;
+ }
+ $this->debugLog[] = $cellReference .
+ ($this->cellStack->count() > 0 ? ' => ' : '') .
+ $message;
+ }
+ }
+
+ /**
+ * Clear the calculation engine debug log.
+ */
+ public function clearLog()
+ {
+ $this->debugLog = [];
+ }
+
+ /**
+ * Return the calculation engine debug log.
+ *
+ * @return string[]
+ */
+ public function getLog()
+ {
+ return $this->debugLog;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Engineering.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Engineering.php
new file mode 100644
index 00000000000..3f1b48bd74c
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Engineering.php
@@ -0,0 +1,2807 @@
+ ['Group' => 'Mass', 'Unit Name' => 'Gram', 'AllowPrefix' => true],
+ 'sg' => ['Group' => 'Mass', 'Unit Name' => 'Slug', 'AllowPrefix' => false],
+ 'lbm' => ['Group' => 'Mass', 'Unit Name' => 'Pound mass (avoirdupois)', 'AllowPrefix' => false],
+ 'u' => ['Group' => 'Mass', 'Unit Name' => 'U (atomic mass unit)', 'AllowPrefix' => true],
+ 'ozm' => ['Group' => 'Mass', 'Unit Name' => 'Ounce mass (avoirdupois)', 'AllowPrefix' => false],
+ 'm' => ['Group' => 'Distance', 'Unit Name' => 'Meter', 'AllowPrefix' => true],
+ 'mi' => ['Group' => 'Distance', 'Unit Name' => 'Statute mile', 'AllowPrefix' => false],
+ 'Nmi' => ['Group' => 'Distance', 'Unit Name' => 'Nautical mile', 'AllowPrefix' => false],
+ 'in' => ['Group' => 'Distance', 'Unit Name' => 'Inch', 'AllowPrefix' => false],
+ 'ft' => ['Group' => 'Distance', 'Unit Name' => 'Foot', 'AllowPrefix' => false],
+ 'yd' => ['Group' => 'Distance', 'Unit Name' => 'Yard', 'AllowPrefix' => false],
+ 'ang' => ['Group' => 'Distance', 'Unit Name' => 'Angstrom', 'AllowPrefix' => true],
+ 'Pica' => ['Group' => 'Distance', 'Unit Name' => 'Pica (1/72 in)', 'AllowPrefix' => false],
+ 'yr' => ['Group' => 'Time', 'Unit Name' => 'Year', 'AllowPrefix' => false],
+ 'day' => ['Group' => 'Time', 'Unit Name' => 'Day', 'AllowPrefix' => false],
+ 'hr' => ['Group' => 'Time', 'Unit Name' => 'Hour', 'AllowPrefix' => false],
+ 'mn' => ['Group' => 'Time', 'Unit Name' => 'Minute', 'AllowPrefix' => false],
+ 'sec' => ['Group' => 'Time', 'Unit Name' => 'Second', 'AllowPrefix' => true],
+ 'Pa' => ['Group' => 'Pressure', 'Unit Name' => 'Pascal', 'AllowPrefix' => true],
+ 'p' => ['Group' => 'Pressure', 'Unit Name' => 'Pascal', 'AllowPrefix' => true],
+ 'atm' => ['Group' => 'Pressure', 'Unit Name' => 'Atmosphere', 'AllowPrefix' => true],
+ 'at' => ['Group' => 'Pressure', 'Unit Name' => 'Atmosphere', 'AllowPrefix' => true],
+ 'mmHg' => ['Group' => 'Pressure', 'Unit Name' => 'mm of Mercury', 'AllowPrefix' => true],
+ 'N' => ['Group' => 'Force', 'Unit Name' => 'Newton', 'AllowPrefix' => true],
+ 'dyn' => ['Group' => 'Force', 'Unit Name' => 'Dyne', 'AllowPrefix' => true],
+ 'dy' => ['Group' => 'Force', 'Unit Name' => 'Dyne', 'AllowPrefix' => true],
+ 'lbf' => ['Group' => 'Force', 'Unit Name' => 'Pound force', 'AllowPrefix' => false],
+ 'J' => ['Group' => 'Energy', 'Unit Name' => 'Joule', 'AllowPrefix' => true],
+ 'e' => ['Group' => 'Energy', 'Unit Name' => 'Erg', 'AllowPrefix' => true],
+ 'c' => ['Group' => 'Energy', 'Unit Name' => 'Thermodynamic calorie', 'AllowPrefix' => true],
+ 'cal' => ['Group' => 'Energy', 'Unit Name' => 'IT calorie', 'AllowPrefix' => true],
+ 'eV' => ['Group' => 'Energy', 'Unit Name' => 'Electron volt', 'AllowPrefix' => true],
+ 'ev' => ['Group' => 'Energy', 'Unit Name' => 'Electron volt', 'AllowPrefix' => true],
+ 'HPh' => ['Group' => 'Energy', 'Unit Name' => 'Horsepower-hour', 'AllowPrefix' => false],
+ 'hh' => ['Group' => 'Energy', 'Unit Name' => 'Horsepower-hour', 'AllowPrefix' => false],
+ 'Wh' => ['Group' => 'Energy', 'Unit Name' => 'Watt-hour', 'AllowPrefix' => true],
+ 'wh' => ['Group' => 'Energy', 'Unit Name' => 'Watt-hour', 'AllowPrefix' => true],
+ 'flb' => ['Group' => 'Energy', 'Unit Name' => 'Foot-pound', 'AllowPrefix' => false],
+ 'BTU' => ['Group' => 'Energy', 'Unit Name' => 'BTU', 'AllowPrefix' => false],
+ 'btu' => ['Group' => 'Energy', 'Unit Name' => 'BTU', 'AllowPrefix' => false],
+ 'HP' => ['Group' => 'Power', 'Unit Name' => 'Horsepower', 'AllowPrefix' => false],
+ 'h' => ['Group' => 'Power', 'Unit Name' => 'Horsepower', 'AllowPrefix' => false],
+ 'W' => ['Group' => 'Power', 'Unit Name' => 'Watt', 'AllowPrefix' => true],
+ 'w' => ['Group' => 'Power', 'Unit Name' => 'Watt', 'AllowPrefix' => true],
+ 'T' => ['Group' => 'Magnetism', 'Unit Name' => 'Tesla', 'AllowPrefix' => true],
+ 'ga' => ['Group' => 'Magnetism', 'Unit Name' => 'Gauss', 'AllowPrefix' => true],
+ 'C' => ['Group' => 'Temperature', 'Unit Name' => 'Celsius', 'AllowPrefix' => false],
+ 'cel' => ['Group' => 'Temperature', 'Unit Name' => 'Celsius', 'AllowPrefix' => false],
+ 'F' => ['Group' => 'Temperature', 'Unit Name' => 'Fahrenheit', 'AllowPrefix' => false],
+ 'fah' => ['Group' => 'Temperature', 'Unit Name' => 'Fahrenheit', 'AllowPrefix' => false],
+ 'K' => ['Group' => 'Temperature', 'Unit Name' => 'Kelvin', 'AllowPrefix' => false],
+ 'kel' => ['Group' => 'Temperature', 'Unit Name' => 'Kelvin', 'AllowPrefix' => false],
+ 'tsp' => ['Group' => 'Liquid', 'Unit Name' => 'Teaspoon', 'AllowPrefix' => false],
+ 'tbs' => ['Group' => 'Liquid', 'Unit Name' => 'Tablespoon', 'AllowPrefix' => false],
+ 'oz' => ['Group' => 'Liquid', 'Unit Name' => 'Fluid Ounce', 'AllowPrefix' => false],
+ 'cup' => ['Group' => 'Liquid', 'Unit Name' => 'Cup', 'AllowPrefix' => false],
+ 'pt' => ['Group' => 'Liquid', 'Unit Name' => 'U.S. Pint', 'AllowPrefix' => false],
+ 'us_pt' => ['Group' => 'Liquid', 'Unit Name' => 'U.S. Pint', 'AllowPrefix' => false],
+ 'uk_pt' => ['Group' => 'Liquid', 'Unit Name' => 'U.K. Pint', 'AllowPrefix' => false],
+ 'qt' => ['Group' => 'Liquid', 'Unit Name' => 'Quart', 'AllowPrefix' => false],
+ 'gal' => ['Group' => 'Liquid', 'Unit Name' => 'Gallon', 'AllowPrefix' => false],
+ 'l' => ['Group' => 'Liquid', 'Unit Name' => 'Litre', 'AllowPrefix' => true],
+ 'lt' => ['Group' => 'Liquid', 'Unit Name' => 'Litre', 'AllowPrefix' => true],
+ ];
+
+ /**
+ * Details of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM().
+ *
+ * @var mixed[]
+ */
+ private static $conversionMultipliers = [
+ 'Y' => ['multiplier' => 1E24, 'name' => 'yotta'],
+ 'Z' => ['multiplier' => 1E21, 'name' => 'zetta'],
+ 'E' => ['multiplier' => 1E18, 'name' => 'exa'],
+ 'P' => ['multiplier' => 1E15, 'name' => 'peta'],
+ 'T' => ['multiplier' => 1E12, 'name' => 'tera'],
+ 'G' => ['multiplier' => 1E9, 'name' => 'giga'],
+ 'M' => ['multiplier' => 1E6, 'name' => 'mega'],
+ 'k' => ['multiplier' => 1E3, 'name' => 'kilo'],
+ 'h' => ['multiplier' => 1E2, 'name' => 'hecto'],
+ 'e' => ['multiplier' => 1E1, 'name' => 'deka'],
+ 'd' => ['multiplier' => 1E-1, 'name' => 'deci'],
+ 'c' => ['multiplier' => 1E-2, 'name' => 'centi'],
+ 'm' => ['multiplier' => 1E-3, 'name' => 'milli'],
+ 'u' => ['multiplier' => 1E-6, 'name' => 'micro'],
+ 'n' => ['multiplier' => 1E-9, 'name' => 'nano'],
+ 'p' => ['multiplier' => 1E-12, 'name' => 'pico'],
+ 'f' => ['multiplier' => 1E-15, 'name' => 'femto'],
+ 'a' => ['multiplier' => 1E-18, 'name' => 'atto'],
+ 'z' => ['multiplier' => 1E-21, 'name' => 'zepto'],
+ 'y' => ['multiplier' => 1E-24, 'name' => 'yocto'],
+ ];
+
+ /**
+ * Details of the Units of measure conversion factors, organised by group.
+ *
+ * @var mixed[]
+ */
+ private static $unitConversions = [
+ 'Mass' => [
+ 'g' => [
+ 'g' => 1.0,
+ 'sg' => 6.85220500053478E-05,
+ 'lbm' => 2.20462291469134E-03,
+ 'u' => 6.02217000000000E+23,
+ 'ozm' => 3.52739718003627E-02,
+ ],
+ 'sg' => [
+ 'g' => 1.45938424189287E+04,
+ 'sg' => 1.0,
+ 'lbm' => 3.21739194101647E+01,
+ 'u' => 8.78866000000000E+27,
+ 'ozm' => 5.14782785944229E+02,
+ ],
+ 'lbm' => [
+ 'g' => 4.5359230974881148E+02,
+ 'sg' => 3.10810749306493E-02,
+ 'lbm' => 1.0,
+ 'u' => 2.73161000000000E+26,
+ 'ozm' => 1.60000023429410E+01,
+ ],
+ 'u' => [
+ 'g' => 1.66053100460465E-24,
+ 'sg' => 1.13782988532950E-28,
+ 'lbm' => 3.66084470330684E-27,
+ 'u' => 1.0,
+ 'ozm' => 5.85735238300524E-26,
+ ],
+ 'ozm' => [
+ 'g' => 2.83495152079732E+01,
+ 'sg' => 1.94256689870811E-03,
+ 'lbm' => 6.24999908478882E-02,
+ 'u' => 1.70725600000000E+25,
+ 'ozm' => 1.0,
+ ],
+ ],
+ 'Distance' => [
+ 'm' => [
+ 'm' => 1.0,
+ 'mi' => 6.21371192237334E-04,
+ 'Nmi' => 5.39956803455724E-04,
+ 'in' => 3.93700787401575E+01,
+ 'ft' => 3.28083989501312E+00,
+ 'yd' => 1.09361329797891E+00,
+ 'ang' => 1.00000000000000E+10,
+ 'Pica' => 2.83464566929116E+03,
+ ],
+ 'mi' => [
+ 'm' => 1.60934400000000E+03,
+ 'mi' => 1.0,
+ 'Nmi' => 8.68976241900648E-01,
+ 'in' => 6.33600000000000E+04,
+ 'ft' => 5.28000000000000E+03,
+ 'yd' => 1.76000000000000E+03,
+ 'ang' => 1.60934400000000E+13,
+ 'Pica' => 4.56191999999971E+06,
+ ],
+ 'Nmi' => [
+ 'm' => 1.85200000000000E+03,
+ 'mi' => 1.15077944802354E+00,
+ 'Nmi' => 1.0,
+ 'in' => 7.29133858267717E+04,
+ 'ft' => 6.07611548556430E+03,
+ 'yd' => 2.02537182785694E+03,
+ 'ang' => 1.85200000000000E+13,
+ 'Pica' => 5.24976377952723E+06,
+ ],
+ 'in' => [
+ 'm' => 2.54000000000000E-02,
+ 'mi' => 1.57828282828283E-05,
+ 'Nmi' => 1.37149028077754E-05,
+ 'in' => 1.0,
+ 'ft' => 8.33333333333333E-02,
+ 'yd' => 2.77777777686643E-02,
+ 'ang' => 2.54000000000000E+08,
+ 'Pica' => 7.19999999999955E+01,
+ ],
+ 'ft' => [
+ 'm' => 3.04800000000000E-01,
+ 'mi' => 1.89393939393939E-04,
+ 'Nmi' => 1.64578833693305E-04,
+ 'in' => 1.20000000000000E+01,
+ 'ft' => 1.0,
+ 'yd' => 3.33333333223972E-01,
+ 'ang' => 3.04800000000000E+09,
+ 'Pica' => 8.63999999999946E+02,
+ ],
+ 'yd' => [
+ 'm' => 9.14400000300000E-01,
+ 'mi' => 5.68181818368230E-04,
+ 'Nmi' => 4.93736501241901E-04,
+ 'in' => 3.60000000118110E+01,
+ 'ft' => 3.00000000000000E+00,
+ 'yd' => 1.0,
+ 'ang' => 9.14400000300000E+09,
+ 'Pica' => 2.59200000085023E+03,
+ ],
+ 'ang' => [
+ 'm' => 1.00000000000000E-10,
+ 'mi' => 6.21371192237334E-14,
+ 'Nmi' => 5.39956803455724E-14,
+ 'in' => 3.93700787401575E-09,
+ 'ft' => 3.28083989501312E-10,
+ 'yd' => 1.09361329797891E-10,
+ 'ang' => 1.0,
+ 'Pica' => 2.83464566929116E-07,
+ ],
+ 'Pica' => [
+ 'm' => 3.52777777777800E-04,
+ 'mi' => 2.19205948372629E-07,
+ 'Nmi' => 1.90484761219114E-07,
+ 'in' => 1.38888888888898E-02,
+ 'ft' => 1.15740740740748E-03,
+ 'yd' => 3.85802469009251E-04,
+ 'ang' => 3.52777777777800E+06,
+ 'Pica' => 1.0,
+ ],
+ ],
+ 'Time' => [
+ 'yr' => [
+ 'yr' => 1.0,
+ 'day' => 365.25,
+ 'hr' => 8766.0,
+ 'mn' => 525960.0,
+ 'sec' => 31557600.0,
+ ],
+ 'day' => [
+ 'yr' => 2.73785078713210E-03,
+ 'day' => 1.0,
+ 'hr' => 24.0,
+ 'mn' => 1440.0,
+ 'sec' => 86400.0,
+ ],
+ 'hr' => [
+ 'yr' => 1.14077116130504E-04,
+ 'day' => 4.16666666666667E-02,
+ 'hr' => 1.0,
+ 'mn' => 60.0,
+ 'sec' => 3600.0,
+ ],
+ 'mn' => [
+ 'yr' => 1.90128526884174E-06,
+ 'day' => 6.94444444444444E-04,
+ 'hr' => 1.66666666666667E-02,
+ 'mn' => 1.0,
+ 'sec' => 60.0,
+ ],
+ 'sec' => [
+ 'yr' => 3.16880878140289E-08,
+ 'day' => 1.15740740740741E-05,
+ 'hr' => 2.77777777777778E-04,
+ 'mn' => 1.66666666666667E-02,
+ 'sec' => 1.0,
+ ],
+ ],
+ 'Pressure' => [
+ 'Pa' => [
+ 'Pa' => 1.0,
+ 'p' => 1.0,
+ 'atm' => 9.86923299998193E-06,
+ 'at' => 9.86923299998193E-06,
+ 'mmHg' => 7.50061707998627E-03,
+ ],
+ 'p' => [
+ 'Pa' => 1.0,
+ 'p' => 1.0,
+ 'atm' => 9.86923299998193E-06,
+ 'at' => 9.86923299998193E-06,
+ 'mmHg' => 7.50061707998627E-03,
+ ],
+ 'atm' => [
+ 'Pa' => 1.01324996583000E+05,
+ 'p' => 1.01324996583000E+05,
+ 'atm' => 1.0,
+ 'at' => 1.0,
+ 'mmHg' => 760.0,
+ ],
+ 'at' => [
+ 'Pa' => 1.01324996583000E+05,
+ 'p' => 1.01324996583000E+05,
+ 'atm' => 1.0,
+ 'at' => 1.0,
+ 'mmHg' => 760.0,
+ ],
+ 'mmHg' => [
+ 'Pa' => 1.33322363925000E+02,
+ 'p' => 1.33322363925000E+02,
+ 'atm' => 1.31578947368421E-03,
+ 'at' => 1.31578947368421E-03,
+ 'mmHg' => 1.0,
+ ],
+ ],
+ 'Force' => [
+ 'N' => [
+ 'N' => 1.0,
+ 'dyn' => 1.0E+5,
+ 'dy' => 1.0E+5,
+ 'lbf' => 2.24808923655339E-01,
+ ],
+ 'dyn' => [
+ 'N' => 1.0E-5,
+ 'dyn' => 1.0,
+ 'dy' => 1.0,
+ 'lbf' => 2.24808923655339E-06,
+ ],
+ 'dy' => [
+ 'N' => 1.0E-5,
+ 'dyn' => 1.0,
+ 'dy' => 1.0,
+ 'lbf' => 2.24808923655339E-06,
+ ],
+ 'lbf' => [
+ 'N' => 4.448222,
+ 'dyn' => 4.448222E+5,
+ 'dy' => 4.448222E+5,
+ 'lbf' => 1.0,
+ ],
+ ],
+ 'Energy' => [
+ 'J' => [
+ 'J' => 1.0,
+ 'e' => 9.99999519343231E+06,
+ 'c' => 2.39006249473467E-01,
+ 'cal' => 2.38846190642017E-01,
+ 'eV' => 6.24145700000000E+18,
+ 'ev' => 6.24145700000000E+18,
+ 'HPh' => 3.72506430801000E-07,
+ 'hh' => 3.72506430801000E-07,
+ 'Wh' => 2.77777916238711E-04,
+ 'wh' => 2.77777916238711E-04,
+ 'flb' => 2.37304222192651E+01,
+ 'BTU' => 9.47815067349015E-04,
+ 'btu' => 9.47815067349015E-04,
+ ],
+ 'e' => [
+ 'J' => 1.00000048065700E-07,
+ 'e' => 1.0,
+ 'c' => 2.39006364353494E-08,
+ 'cal' => 2.38846305445111E-08,
+ 'eV' => 6.24146000000000E+11,
+ 'ev' => 6.24146000000000E+11,
+ 'HPh' => 3.72506609848824E-14,
+ 'hh' => 3.72506609848824E-14,
+ 'Wh' => 2.77778049754611E-11,
+ 'wh' => 2.77778049754611E-11,
+ 'flb' => 2.37304336254586E-06,
+ 'BTU' => 9.47815522922962E-11,
+ 'btu' => 9.47815522922962E-11,
+ ],
+ 'c' => [
+ 'J' => 4.18399101363672E+00,
+ 'e' => 4.18398900257312E+07,
+ 'c' => 1.0,
+ 'cal' => 9.99330315287563E-01,
+ 'eV' => 2.61142000000000E+19,
+ 'ev' => 2.61142000000000E+19,
+ 'HPh' => 1.55856355899327E-06,
+ 'hh' => 1.55856355899327E-06,
+ 'Wh' => 1.16222030532950E-03,
+ 'wh' => 1.16222030532950E-03,
+ 'flb' => 9.92878733152102E+01,
+ 'BTU' => 3.96564972437776E-03,
+ 'btu' => 3.96564972437776E-03,
+ ],
+ 'cal' => [
+ 'J' => 4.18679484613929E+00,
+ 'e' => 4.18679283372801E+07,
+ 'c' => 1.00067013349059E+00,
+ 'cal' => 1.0,
+ 'eV' => 2.61317000000000E+19,
+ 'ev' => 2.61317000000000E+19,
+ 'HPh' => 1.55960800463137E-06,
+ 'hh' => 1.55960800463137E-06,
+ 'Wh' => 1.16299914807955E-03,
+ 'wh' => 1.16299914807955E-03,
+ 'flb' => 9.93544094443283E+01,
+ 'BTU' => 3.96830723907002E-03,
+ 'btu' => 3.96830723907002E-03,
+ ],
+ 'eV' => [
+ 'J' => 1.60219000146921E-19,
+ 'e' => 1.60218923136574E-12,
+ 'c' => 3.82933423195043E-20,
+ 'cal' => 3.82676978535648E-20,
+ 'eV' => 1.0,
+ 'ev' => 1.0,
+ 'HPh' => 5.96826078912344E-26,
+ 'hh' => 5.96826078912344E-26,
+ 'Wh' => 4.45053000026614E-23,
+ 'wh' => 4.45053000026614E-23,
+ 'flb' => 3.80206452103492E-18,
+ 'BTU' => 1.51857982414846E-22,
+ 'btu' => 1.51857982414846E-22,
+ ],
+ 'ev' => [
+ 'J' => 1.60219000146921E-19,
+ 'e' => 1.60218923136574E-12,
+ 'c' => 3.82933423195043E-20,
+ 'cal' => 3.82676978535648E-20,
+ 'eV' => 1.0,
+ 'ev' => 1.0,
+ 'HPh' => 5.96826078912344E-26,
+ 'hh' => 5.96826078912344E-26,
+ 'Wh' => 4.45053000026614E-23,
+ 'wh' => 4.45053000026614E-23,
+ 'flb' => 3.80206452103492E-18,
+ 'BTU' => 1.51857982414846E-22,
+ 'btu' => 1.51857982414846E-22,
+ ],
+ 'HPh' => [
+ 'J' => 2.68451741316170E+06,
+ 'e' => 2.68451612283024E+13,
+ 'c' => 6.41616438565991E+05,
+ 'cal' => 6.41186757845835E+05,
+ 'eV' => 1.67553000000000E+25,
+ 'ev' => 1.67553000000000E+25,
+ 'HPh' => 1.0,
+ 'hh' => 1.0,
+ 'Wh' => 7.45699653134593E+02,
+ 'wh' => 7.45699653134593E+02,
+ 'flb' => 6.37047316692964E+07,
+ 'BTU' => 2.54442605275546E+03,
+ 'btu' => 2.54442605275546E+03,
+ ],
+ 'hh' => [
+ 'J' => 2.68451741316170E+06,
+ 'e' => 2.68451612283024E+13,
+ 'c' => 6.41616438565991E+05,
+ 'cal' => 6.41186757845835E+05,
+ 'eV' => 1.67553000000000E+25,
+ 'ev' => 1.67553000000000E+25,
+ 'HPh' => 1.0,
+ 'hh' => 1.0,
+ 'Wh' => 7.45699653134593E+02,
+ 'wh' => 7.45699653134593E+02,
+ 'flb' => 6.37047316692964E+07,
+ 'BTU' => 2.54442605275546E+03,
+ 'btu' => 2.54442605275546E+03,
+ ],
+ 'Wh' => [
+ 'J' => 3.59999820554720E+03,
+ 'e' => 3.59999647518369E+10,
+ 'c' => 8.60422069219046E+02,
+ 'cal' => 8.59845857713046E+02,
+ 'eV' => 2.24692340000000E+22,
+ 'ev' => 2.24692340000000E+22,
+ 'HPh' => 1.34102248243839E-03,
+ 'hh' => 1.34102248243839E-03,
+ 'Wh' => 1.0,
+ 'wh' => 1.0,
+ 'flb' => 8.54294774062316E+04,
+ 'BTU' => 3.41213254164705E+00,
+ 'btu' => 3.41213254164705E+00,
+ ],
+ 'wh' => [
+ 'J' => 3.59999820554720E+03,
+ 'e' => 3.59999647518369E+10,
+ 'c' => 8.60422069219046E+02,
+ 'cal' => 8.59845857713046E+02,
+ 'eV' => 2.24692340000000E+22,
+ 'ev' => 2.24692340000000E+22,
+ 'HPh' => 1.34102248243839E-03,
+ 'hh' => 1.34102248243839E-03,
+ 'Wh' => 1.0,
+ 'wh' => 1.0,
+ 'flb' => 8.54294774062316E+04,
+ 'BTU' => 3.41213254164705E+00,
+ 'btu' => 3.41213254164705E+00,
+ ],
+ 'flb' => [
+ 'J' => 4.21400003236424E-02,
+ 'e' => 4.21399800687660E+05,
+ 'c' => 1.00717234301644E-02,
+ 'cal' => 1.00649785509554E-02,
+ 'eV' => 2.63015000000000E+17,
+ 'ev' => 2.63015000000000E+17,
+ 'HPh' => 1.56974211145130E-08,
+ 'hh' => 1.56974211145130E-08,
+ 'Wh' => 1.17055614802000E-05,
+ 'wh' => 1.17055614802000E-05,
+ 'flb' => 1.0,
+ 'BTU' => 3.99409272448406E-05,
+ 'btu' => 3.99409272448406E-05,
+ ],
+ 'BTU' => [
+ 'J' => 1.05505813786749E+03,
+ 'e' => 1.05505763074665E+10,
+ 'c' => 2.52165488508168E+02,
+ 'cal' => 2.51996617135510E+02,
+ 'eV' => 6.58510000000000E+21,
+ 'ev' => 6.58510000000000E+21,
+ 'HPh' => 3.93015941224568E-04,
+ 'hh' => 3.93015941224568E-04,
+ 'Wh' => 2.93071851047526E-01,
+ 'wh' => 2.93071851047526E-01,
+ 'flb' => 2.50369750774671E+04,
+ 'BTU' => 1.0,
+ 'btu' => 1.0,
+ ],
+ 'btu' => [
+ 'J' => 1.05505813786749E+03,
+ 'e' => 1.05505763074665E+10,
+ 'c' => 2.52165488508168E+02,
+ 'cal' => 2.51996617135510E+02,
+ 'eV' => 6.58510000000000E+21,
+ 'ev' => 6.58510000000000E+21,
+ 'HPh' => 3.93015941224568E-04,
+ 'hh' => 3.93015941224568E-04,
+ 'Wh' => 2.93071851047526E-01,
+ 'wh' => 2.93071851047526E-01,
+ 'flb' => 2.50369750774671E+04,
+ 'BTU' => 1.0,
+ 'btu' => 1.0,
+ ],
+ ],
+ 'Power' => [
+ 'HP' => [
+ 'HP' => 1.0,
+ 'h' => 1.0,
+ 'W' => 7.45701000000000E+02,
+ 'w' => 7.45701000000000E+02,
+ ],
+ 'h' => [
+ 'HP' => 1.0,
+ 'h' => 1.0,
+ 'W' => 7.45701000000000E+02,
+ 'w' => 7.45701000000000E+02,
+ ],
+ 'W' => [
+ 'HP' => 1.34102006031908E-03,
+ 'h' => 1.34102006031908E-03,
+ 'W' => 1.0,
+ 'w' => 1.0,
+ ],
+ 'w' => [
+ 'HP' => 1.34102006031908E-03,
+ 'h' => 1.34102006031908E-03,
+ 'W' => 1.0,
+ 'w' => 1.0,
+ ],
+ ],
+ 'Magnetism' => [
+ 'T' => [
+ 'T' => 1.0,
+ 'ga' => 10000.0,
+ ],
+ 'ga' => [
+ 'T' => 0.0001,
+ 'ga' => 1.0,
+ ],
+ ],
+ 'Liquid' => [
+ 'tsp' => [
+ 'tsp' => 1.0,
+ 'tbs' => 3.33333333333333E-01,
+ 'oz' => 1.66666666666667E-01,
+ 'cup' => 2.08333333333333E-02,
+ 'pt' => 1.04166666666667E-02,
+ 'us_pt' => 1.04166666666667E-02,
+ 'uk_pt' => 8.67558516821960E-03,
+ 'qt' => 5.20833333333333E-03,
+ 'gal' => 1.30208333333333E-03,
+ 'l' => 4.92999408400710E-03,
+ 'lt' => 4.92999408400710E-03,
+ ],
+ 'tbs' => [
+ 'tsp' => 3.00000000000000E+00,
+ 'tbs' => 1.0,
+ 'oz' => 5.00000000000000E-01,
+ 'cup' => 6.25000000000000E-02,
+ 'pt' => 3.12500000000000E-02,
+ 'us_pt' => 3.12500000000000E-02,
+ 'uk_pt' => 2.60267555046588E-02,
+ 'qt' => 1.56250000000000E-02,
+ 'gal' => 3.90625000000000E-03,
+ 'l' => 1.47899822520213E-02,
+ 'lt' => 1.47899822520213E-02,
+ ],
+ 'oz' => [
+ 'tsp' => 6.00000000000000E+00,
+ 'tbs' => 2.00000000000000E+00,
+ 'oz' => 1.0,
+ 'cup' => 1.25000000000000E-01,
+ 'pt' => 6.25000000000000E-02,
+ 'us_pt' => 6.25000000000000E-02,
+ 'uk_pt' => 5.20535110093176E-02,
+ 'qt' => 3.12500000000000E-02,
+ 'gal' => 7.81250000000000E-03,
+ 'l' => 2.95799645040426E-02,
+ 'lt' => 2.95799645040426E-02,
+ ],
+ 'cup' => [
+ 'tsp' => 4.80000000000000E+01,
+ 'tbs' => 1.60000000000000E+01,
+ 'oz' => 8.00000000000000E+00,
+ 'cup' => 1.0,
+ 'pt' => 5.00000000000000E-01,
+ 'us_pt' => 5.00000000000000E-01,
+ 'uk_pt' => 4.16428088074541E-01,
+ 'qt' => 2.50000000000000E-01,
+ 'gal' => 6.25000000000000E-02,
+ 'l' => 2.36639716032341E-01,
+ 'lt' => 2.36639716032341E-01,
+ ],
+ 'pt' => [
+ 'tsp' => 9.60000000000000E+01,
+ 'tbs' => 3.20000000000000E+01,
+ 'oz' => 1.60000000000000E+01,
+ 'cup' => 2.00000000000000E+00,
+ 'pt' => 1.0,
+ 'us_pt' => 1.0,
+ 'uk_pt' => 8.32856176149081E-01,
+ 'qt' => 5.00000000000000E-01,
+ 'gal' => 1.25000000000000E-01,
+ 'l' => 4.73279432064682E-01,
+ 'lt' => 4.73279432064682E-01,
+ ],
+ 'us_pt' => [
+ 'tsp' => 9.60000000000000E+01,
+ 'tbs' => 3.20000000000000E+01,
+ 'oz' => 1.60000000000000E+01,
+ 'cup' => 2.00000000000000E+00,
+ 'pt' => 1.0,
+ 'us_pt' => 1.0,
+ 'uk_pt' => 8.32856176149081E-01,
+ 'qt' => 5.00000000000000E-01,
+ 'gal' => 1.25000000000000E-01,
+ 'l' => 4.73279432064682E-01,
+ 'lt' => 4.73279432064682E-01,
+ ],
+ 'uk_pt' => [
+ 'tsp' => 1.15266000000000E+02,
+ 'tbs' => 3.84220000000000E+01,
+ 'oz' => 1.92110000000000E+01,
+ 'cup' => 2.40137500000000E+00,
+ 'pt' => 1.20068750000000E+00,
+ 'us_pt' => 1.20068750000000E+00,
+ 'uk_pt' => 1.0,
+ 'qt' => 6.00343750000000E-01,
+ 'gal' => 1.50085937500000E-01,
+ 'l' => 5.68260698087162E-01,
+ 'lt' => 5.68260698087162E-01,
+ ],
+ 'qt' => [
+ 'tsp' => 1.92000000000000E+02,
+ 'tbs' => 6.40000000000000E+01,
+ 'oz' => 3.20000000000000E+01,
+ 'cup' => 4.00000000000000E+00,
+ 'pt' => 2.00000000000000E+00,
+ 'us_pt' => 2.00000000000000E+00,
+ 'uk_pt' => 1.66571235229816E+00,
+ 'qt' => 1.0,
+ 'gal' => 2.50000000000000E-01,
+ 'l' => 9.46558864129363E-01,
+ 'lt' => 9.46558864129363E-01,
+ ],
+ 'gal' => [
+ 'tsp' => 7.68000000000000E+02,
+ 'tbs' => 2.56000000000000E+02,
+ 'oz' => 1.28000000000000E+02,
+ 'cup' => 1.60000000000000E+01,
+ 'pt' => 8.00000000000000E+00,
+ 'us_pt' => 8.00000000000000E+00,
+ 'uk_pt' => 6.66284940919265E+00,
+ 'qt' => 4.00000000000000E+00,
+ 'gal' => 1.0,
+ 'l' => 3.78623545651745E+00,
+ 'lt' => 3.78623545651745E+00,
+ ],
+ 'l' => [
+ 'tsp' => 2.02840000000000E+02,
+ 'tbs' => 6.76133333333333E+01,
+ 'oz' => 3.38066666666667E+01,
+ 'cup' => 4.22583333333333E+00,
+ 'pt' => 2.11291666666667E+00,
+ 'us_pt' => 2.11291666666667E+00,
+ 'uk_pt' => 1.75975569552166E+00,
+ 'qt' => 1.05645833333333E+00,
+ 'gal' => 2.64114583333333E-01,
+ 'l' => 1.0,
+ 'lt' => 1.0,
+ ],
+ 'lt' => [
+ 'tsp' => 2.02840000000000E+02,
+ 'tbs' => 6.76133333333333E+01,
+ 'oz' => 3.38066666666667E+01,
+ 'cup' => 4.22583333333333E+00,
+ 'pt' => 2.11291666666667E+00,
+ 'us_pt' => 2.11291666666667E+00,
+ 'uk_pt' => 1.75975569552166E+00,
+ 'qt' => 1.05645833333333E+00,
+ 'gal' => 2.64114583333333E-01,
+ 'l' => 1.0,
+ 'lt' => 1.0,
+ ],
+ ],
+ ];
+
+ /**
+ * parseComplex.
+ *
+ * Parses a complex number into its real and imaginary parts, and an I or J suffix
+ *
+ * @deprecated 2.0.0 No longer used by internal code. Please use the Complex\Complex class instead
+ *
+ * @param string $complexNumber The complex number
+ *
+ * @return mixed[] Indexed on "real", "imaginary" and "suffix"
+ */
+ public static function parseComplex($complexNumber)
+ {
+ $complex = new Complex($complexNumber);
+
+ return [
+ 'real' => $complex->getReal(),
+ 'imaginary' => $complex->getImaginary(),
+ 'suffix' => $complex->getSuffix(),
+ ];
+ }
+
+ /**
+ * Formats a number base string value with leading zeroes.
+ *
+ * @param string $xVal The "number" to pad
+ * @param int $places The length that we want to pad this value
+ *
+ * @return string The padded "number"
+ */
+ private static function nbrConversionFormat($xVal, $places)
+ {
+ if ($places !== null) {
+ if (is_numeric($places)) {
+ $places = (int) $places;
+ } else {
+ return Functions::VALUE();
+ }
+ if ($places < 0) {
+ return Functions::NAN();
+ }
+ if (strlen($xVal) <= $places) {
+ return substr(str_pad($xVal, $places, '0', STR_PAD_LEFT), -10);
+ }
+
+ return Functions::NAN();
+ }
+
+ return substr($xVal, -10);
+ }
+
+ /**
+ * BESSELI.
+ *
+ * Returns the modified Bessel function In(x), which is equivalent to the Bessel function evaluated
+ * for purely imaginary arguments
+ *
+ * Excel Function:
+ * BESSELI(x,ord)
+ *
+ * @category Engineering Functions
+ *
+ * @param float $x The value at which to evaluate the function.
+ * If x is nonnumeric, BESSELI returns the #VALUE! error value.
+ * @param int $ord The order of the Bessel function.
+ * If ord is not an integer, it is truncated.
+ * If $ord is nonnumeric, BESSELI returns the #VALUE! error value.
+ * If $ord < 0, BESSELI returns the #NUM! error value.
+ *
+ * @return float
+ */
+ public static function BESSELI($x, $ord)
+ {
+ $x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
+ $ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
+
+ if ((is_numeric($x)) && (is_numeric($ord))) {
+ $ord = floor($ord);
+ if ($ord < 0) {
+ return Functions::NAN();
+ }
+
+ if (abs($x) <= 30) {
+ $fResult = $fTerm = pow($x / 2, $ord) / MathTrig::FACT($ord);
+ $ordK = 1;
+ $fSqrX = ($x * $x) / 4;
+ do {
+ $fTerm *= $fSqrX;
+ $fTerm /= ($ordK * ($ordK + $ord));
+ $fResult += $fTerm;
+ } while ((abs($fTerm) > 1e-12) && (++$ordK < 100));
+ } else {
+ $f_2_PI = 2 * M_PI;
+
+ $fXAbs = abs($x);
+ $fResult = exp($fXAbs) / sqrt($f_2_PI * $fXAbs);
+ if (($ord & 1) && ($x < 0)) {
+ $fResult = -$fResult;
+ }
+ }
+
+ return (is_nan($fResult)) ? Functions::NAN() : $fResult;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * BESSELJ.
+ *
+ * Returns the Bessel function
+ *
+ * Excel Function:
+ * BESSELJ(x,ord)
+ *
+ * @category Engineering Functions
+ *
+ * @param float $x The value at which to evaluate the function.
+ * If x is nonnumeric, BESSELJ returns the #VALUE! error value.
+ * @param int $ord The order of the Bessel function. If n is not an integer, it is truncated.
+ * If $ord is nonnumeric, BESSELJ returns the #VALUE! error value.
+ * If $ord < 0, BESSELJ returns the #NUM! error value.
+ *
+ * @return float
+ */
+ public static function BESSELJ($x, $ord)
+ {
+ $x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
+ $ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
+
+ if ((is_numeric($x)) && (is_numeric($ord))) {
+ $ord = floor($ord);
+ if ($ord < 0) {
+ return Functions::NAN();
+ }
+
+ $fResult = 0;
+ if (abs($x) <= 30) {
+ $fResult = $fTerm = pow($x / 2, $ord) / MathTrig::FACT($ord);
+ $ordK = 1;
+ $fSqrX = ($x * $x) / -4;
+ do {
+ $fTerm *= $fSqrX;
+ $fTerm /= ($ordK * ($ordK + $ord));
+ $fResult += $fTerm;
+ } while ((abs($fTerm) > 1e-12) && (++$ordK < 100));
+ } else {
+ $f_PI_DIV_2 = M_PI / 2;
+ $f_PI_DIV_4 = M_PI / 4;
+
+ $fXAbs = abs($x);
+ $fResult = sqrt(Functions::M_2DIVPI / $fXAbs) * cos($fXAbs - $ord * $f_PI_DIV_2 - $f_PI_DIV_4);
+ if (($ord & 1) && ($x < 0)) {
+ $fResult = -$fResult;
+ }
+ }
+
+ return (is_nan($fResult)) ? Functions::NAN() : $fResult;
+ }
+
+ return Functions::VALUE();
+ }
+
+ private static function besselK0($fNum)
+ {
+ if ($fNum <= 2) {
+ $fNum2 = $fNum * 0.5;
+ $y = ($fNum2 * $fNum2);
+ $fRet = -log($fNum2) * self::BESSELI($fNum, 0) +
+ (-0.57721566 + $y * (0.42278420 + $y * (0.23069756 + $y * (0.3488590e-1 + $y * (0.262698e-2 + $y *
+ (0.10750e-3 + $y * 0.74e-5))))));
+ } else {
+ $y = 2 / $fNum;
+ $fRet = exp(-$fNum) / sqrt($fNum) *
+ (1.25331414 + $y * (-0.7832358e-1 + $y * (0.2189568e-1 + $y * (-0.1062446e-1 + $y *
+ (0.587872e-2 + $y * (-0.251540e-2 + $y * 0.53208e-3))))));
+ }
+
+ return $fRet;
+ }
+
+ private static function besselK1($fNum)
+ {
+ if ($fNum <= 2) {
+ $fNum2 = $fNum * 0.5;
+ $y = ($fNum2 * $fNum2);
+ $fRet = log($fNum2) * self::BESSELI($fNum, 1) +
+ (1 + $y * (0.15443144 + $y * (-0.67278579 + $y * (-0.18156897 + $y * (-0.1919402e-1 + $y *
+ (-0.110404e-2 + $y * (-0.4686e-4))))))) / $fNum;
+ } else {
+ $y = 2 / $fNum;
+ $fRet = exp(-$fNum) / sqrt($fNum) *
+ (1.25331414 + $y * (0.23498619 + $y * (-0.3655620e-1 + $y * (0.1504268e-1 + $y * (-0.780353e-2 + $y *
+ (0.325614e-2 + $y * (-0.68245e-3)))))));
+ }
+
+ return $fRet;
+ }
+
+ /**
+ * BESSELK.
+ *
+ * Returns the modified Bessel function Kn(x), which is equivalent to the Bessel functions evaluated
+ * for purely imaginary arguments.
+ *
+ * Excel Function:
+ * BESSELK(x,ord)
+ *
+ * @category Engineering Functions
+ *
+ * @param float $x The value at which to evaluate the function.
+ * If x is nonnumeric, BESSELK returns the #VALUE! error value.
+ * @param int $ord The order of the Bessel function. If n is not an integer, it is truncated.
+ * If $ord is nonnumeric, BESSELK returns the #VALUE! error value.
+ * If $ord < 0, BESSELK returns the #NUM! error value.
+ *
+ * @return float
+ */
+ public static function BESSELK($x, $ord)
+ {
+ $x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
+ $ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
+
+ if ((is_numeric($x)) && (is_numeric($ord))) {
+ if (($ord < 0) || ($x == 0.0)) {
+ return Functions::NAN();
+ }
+
+ switch (floor($ord)) {
+ case 0:
+ $fBk = self::besselK0($x);
+
+ break;
+ case 1:
+ $fBk = self::besselK1($x);
+
+ break;
+ default:
+ $fTox = 2 / $x;
+ $fBkm = self::besselK0($x);
+ $fBk = self::besselK1($x);
+ for ($n = 1; $n < $ord; ++$n) {
+ $fBkp = $fBkm + $n * $fTox * $fBk;
+ $fBkm = $fBk;
+ $fBk = $fBkp;
+ }
+ }
+
+ return (is_nan($fBk)) ? Functions::NAN() : $fBk;
+ }
+
+ return Functions::VALUE();
+ }
+
+ private static function besselY0($fNum)
+ {
+ if ($fNum < 8.0) {
+ $y = ($fNum * $fNum);
+ $f1 = -2957821389.0 + $y * (7062834065.0 + $y * (-512359803.6 + $y * (10879881.29 + $y * (-86327.92757 + $y * 228.4622733))));
+ $f2 = 40076544269.0 + $y * (745249964.8 + $y * (7189466.438 + $y * (47447.26470 + $y * (226.1030244 + $y))));
+ $fRet = $f1 / $f2 + 0.636619772 * self::BESSELJ($fNum, 0) * log($fNum);
+ } else {
+ $z = 8.0 / $fNum;
+ $y = ($z * $z);
+ $xx = $fNum - 0.785398164;
+ $f1 = 1 + $y * (-0.1098628627e-2 + $y * (0.2734510407e-4 + $y * (-0.2073370639e-5 + $y * 0.2093887211e-6)));
+ $f2 = -0.1562499995e-1 + $y * (0.1430488765e-3 + $y * (-0.6911147651e-5 + $y * (0.7621095161e-6 + $y * (-0.934945152e-7))));
+ $fRet = sqrt(0.636619772 / $fNum) * (sin($xx) * $f1 + $z * cos($xx) * $f2);
+ }
+
+ return $fRet;
+ }
+
+ private static function besselY1($fNum)
+ {
+ if ($fNum < 8.0) {
+ $y = ($fNum * $fNum);
+ $f1 = $fNum * (-0.4900604943e13 + $y * (0.1275274390e13 + $y * (-0.5153438139e11 + $y * (0.7349264551e9 + $y *
+ (-0.4237922726e7 + $y * 0.8511937935e4)))));
+ $f2 = 0.2499580570e14 + $y * (0.4244419664e12 + $y * (0.3733650367e10 + $y * (0.2245904002e8 + $y *
+ (0.1020426050e6 + $y * (0.3549632885e3 + $y)))));
+ $fRet = $f1 / $f2 + 0.636619772 * (self::BESSELJ($fNum, 1) * log($fNum) - 1 / $fNum);
+ } else {
+ $fRet = sqrt(0.636619772 / $fNum) * sin($fNum - 2.356194491);
+ }
+
+ return $fRet;
+ }
+
+ /**
+ * BESSELY.
+ *
+ * Returns the Bessel function, which is also called the Weber function or the Neumann function.
+ *
+ * Excel Function:
+ * BESSELY(x,ord)
+ *
+ * @category Engineering Functions
+ *
+ * @param float $x The value at which to evaluate the function.
+ * If x is nonnumeric, BESSELK returns the #VALUE! error value.
+ * @param int $ord The order of the Bessel function. If n is not an integer, it is truncated.
+ * If $ord is nonnumeric, BESSELK returns the #VALUE! error value.
+ * If $ord < 0, BESSELK returns the #NUM! error value.
+ *
+ * @return float
+ */
+ public static function BESSELY($x, $ord)
+ {
+ $x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
+ $ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
+
+ if ((is_numeric($x)) && (is_numeric($ord))) {
+ if (($ord < 0) || ($x == 0.0)) {
+ return Functions::NAN();
+ }
+
+ switch (floor($ord)) {
+ case 0:
+ $fBy = self::besselY0($x);
+
+ break;
+ case 1:
+ $fBy = self::besselY1($x);
+
+ break;
+ default:
+ $fTox = 2 / $x;
+ $fBym = self::besselY0($x);
+ $fBy = self::besselY1($x);
+ for ($n = 1; $n < $ord; ++$n) {
+ $fByp = $n * $fTox * $fBy - $fBym;
+ $fBym = $fBy;
+ $fBy = $fByp;
+ }
+ }
+
+ return (is_nan($fBy)) ? Functions::NAN() : $fBy;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * BINTODEC.
+ *
+ * Return a binary value as decimal.
+ *
+ * Excel Function:
+ * BIN2DEC(x)
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The binary number (as a string) that you want to convert. The number
+ * cannot contain more than 10 characters (10 bits). The most significant
+ * bit of number is the sign bit. The remaining 9 bits are magnitude bits.
+ * Negative numbers are represented using two's-complement notation.
+ * If number is not a valid binary number, or if number contains more than
+ * 10 characters (10 bits), BIN2DEC returns the #NUM! error value.
+ *
+ * @return string
+ */
+ public static function BINTODEC($x)
+ {
+ $x = Functions::flattenSingleValue($x);
+
+ if (is_bool($x)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ $x = (int) $x;
+ } else {
+ return Functions::VALUE();
+ }
+ }
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
+ $x = floor($x);
+ }
+ $x = (string) $x;
+ if (strlen($x) > preg_match_all('/[01]/', $x, $out)) {
+ return Functions::NAN();
+ }
+ if (strlen($x) > 10) {
+ return Functions::NAN();
+ } elseif (strlen($x) == 10) {
+ // Two's Complement
+ $x = substr($x, -9);
+
+ return '-' . (512 - bindec($x));
+ }
+
+ return bindec($x);
+ }
+
+ /**
+ * BINTOHEX.
+ *
+ * Return a binary value as hex.
+ *
+ * Excel Function:
+ * BIN2HEX(x[,places])
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The binary number (as a string) that you want to convert. The number
+ * cannot contain more than 10 characters (10 bits). The most significant
+ * bit of number is the sign bit. The remaining 9 bits are magnitude bits.
+ * Negative numbers are represented using two's-complement notation.
+ * If number is not a valid binary number, or if number contains more than
+ * 10 characters (10 bits), BIN2HEX returns the #NUM! error value.
+ * @param int $places The number of characters to use. If places is omitted, BIN2HEX uses the
+ * minimum number of characters necessary. Places is useful for padding the
+ * return value with leading 0s (zeros).
+ * If places is not an integer, it is truncated.
+ * If places is nonnumeric, BIN2HEX returns the #VALUE! error value.
+ * If places is negative, BIN2HEX returns the #NUM! error value.
+ *
+ * @return string
+ */
+ public static function BINTOHEX($x, $places = null)
+ {
+ $x = Functions::flattenSingleValue($x);
+ $places = Functions::flattenSingleValue($places);
+
+ // Argument X
+ if (is_bool($x)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ $x = (int) $x;
+ } else {
+ return Functions::VALUE();
+ }
+ }
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
+ $x = floor($x);
+ }
+ $x = (string) $x;
+ if (strlen($x) > preg_match_all('/[01]/', $x, $out)) {
+ return Functions::NAN();
+ }
+ if (strlen($x) > 10) {
+ return Functions::NAN();
+ } elseif (strlen($x) == 10) {
+ // Two's Complement
+ return str_repeat('F', 8) . substr(strtoupper(dechex(bindec(substr($x, -9)))), -2);
+ }
+ $hexVal = (string) strtoupper(dechex(bindec($x)));
+
+ return self::nbrConversionFormat($hexVal, $places);
+ }
+
+ /**
+ * BINTOOCT.
+ *
+ * Return a binary value as octal.
+ *
+ * Excel Function:
+ * BIN2OCT(x[,places])
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The binary number (as a string) that you want to convert. The number
+ * cannot contain more than 10 characters (10 bits). The most significant
+ * bit of number is the sign bit. The remaining 9 bits are magnitude bits.
+ * Negative numbers are represented using two's-complement notation.
+ * If number is not a valid binary number, or if number contains more than
+ * 10 characters (10 bits), BIN2OCT returns the #NUM! error value.
+ * @param int $places The number of characters to use. If places is omitted, BIN2OCT uses the
+ * minimum number of characters necessary. Places is useful for padding the
+ * return value with leading 0s (zeros).
+ * If places is not an integer, it is truncated.
+ * If places is nonnumeric, BIN2OCT returns the #VALUE! error value.
+ * If places is negative, BIN2OCT returns the #NUM! error value.
+ *
+ * @return string
+ */
+ public static function BINTOOCT($x, $places = null)
+ {
+ $x = Functions::flattenSingleValue($x);
+ $places = Functions::flattenSingleValue($places);
+
+ if (is_bool($x)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ $x = (int) $x;
+ } else {
+ return Functions::VALUE();
+ }
+ }
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
+ $x = floor($x);
+ }
+ $x = (string) $x;
+ if (strlen($x) > preg_match_all('/[01]/', $x, $out)) {
+ return Functions::NAN();
+ }
+ if (strlen($x) > 10) {
+ return Functions::NAN();
+ } elseif (strlen($x) == 10) {
+ // Two's Complement
+ return str_repeat('7', 7) . substr(strtoupper(decoct(bindec(substr($x, -9)))), -3);
+ }
+ $octVal = (string) decoct(bindec($x));
+
+ return self::nbrConversionFormat($octVal, $places);
+ }
+
+ /**
+ * DECTOBIN.
+ *
+ * Return a decimal value as binary.
+ *
+ * Excel Function:
+ * DEC2BIN(x[,places])
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The decimal integer you want to convert. If number is negative,
+ * valid place values are ignored and DEC2BIN returns a 10-character
+ * (10-bit) binary number in which the most significant bit is the sign
+ * bit. The remaining 9 bits are magnitude bits. Negative numbers are
+ * represented using two's-complement notation.
+ * If number < -512 or if number > 511, DEC2BIN returns the #NUM! error
+ * value.
+ * If number is nonnumeric, DEC2BIN returns the #VALUE! error value.
+ * If DEC2BIN requires more than places characters, it returns the #NUM!
+ * error value.
+ * @param int $places The number of characters to use. If places is omitted, DEC2BIN uses
+ * the minimum number of characters necessary. Places is useful for
+ * padding the return value with leading 0s (zeros).
+ * If places is not an integer, it is truncated.
+ * If places is nonnumeric, DEC2BIN returns the #VALUE! error value.
+ * If places is zero or negative, DEC2BIN returns the #NUM! error value.
+ *
+ * @return string
+ */
+ public static function DECTOBIN($x, $places = null)
+ {
+ $x = Functions::flattenSingleValue($x);
+ $places = Functions::flattenSingleValue($places);
+
+ if (is_bool($x)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ $x = (int) $x;
+ } else {
+ return Functions::VALUE();
+ }
+ }
+ $x = (string) $x;
+ if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) {
+ return Functions::VALUE();
+ }
+
+ $x = (string) floor($x);
+ if ($x < -512 || $x > 511) {
+ return Functions::NAN();
+ }
+
+ $r = decbin($x);
+ // Two's Complement
+ $r = substr($r, -10);
+ if (strlen($r) >= 11) {
+ return Functions::NAN();
+ }
+
+ return self::nbrConversionFormat($r, $places);
+ }
+
+ /**
+ * DECTOHEX.
+ *
+ * Return a decimal value as hex.
+ *
+ * Excel Function:
+ * DEC2HEX(x[,places])
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The decimal integer you want to convert. If number is negative,
+ * places is ignored and DEC2HEX returns a 10-character (40-bit)
+ * hexadecimal number in which the most significant bit is the sign
+ * bit. The remaining 39 bits are magnitude bits. Negative numbers
+ * are represented using two's-complement notation.
+ * If number < -549,755,813,888 or if number > 549,755,813,887,
+ * DEC2HEX returns the #NUM! error value.
+ * If number is nonnumeric, DEC2HEX returns the #VALUE! error value.
+ * If DEC2HEX requires more than places characters, it returns the
+ * #NUM! error value.
+ * @param int $places The number of characters to use. If places is omitted, DEC2HEX uses
+ * the minimum number of characters necessary. Places is useful for
+ * padding the return value with leading 0s (zeros).
+ * If places is not an integer, it is truncated.
+ * If places is nonnumeric, DEC2HEX returns the #VALUE! error value.
+ * If places is zero or negative, DEC2HEX returns the #NUM! error value.
+ *
+ * @return string
+ */
+ public static function DECTOHEX($x, $places = null)
+ {
+ $x = Functions::flattenSingleValue($x);
+ $places = Functions::flattenSingleValue($places);
+
+ if (is_bool($x)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ $x = (int) $x;
+ } else {
+ return Functions::VALUE();
+ }
+ }
+ $x = (string) $x;
+ if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) {
+ return Functions::VALUE();
+ }
+ $x = (string) floor($x);
+ $r = strtoupper(dechex($x));
+ if (strlen($r) == 8) {
+ // Two's Complement
+ $r = 'FF' . $r;
+ }
+
+ return self::nbrConversionFormat($r, $places);
+ }
+
+ /**
+ * DECTOOCT.
+ *
+ * Return an decimal value as octal.
+ *
+ * Excel Function:
+ * DEC2OCT(x[,places])
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The decimal integer you want to convert. If number is negative,
+ * places is ignored and DEC2OCT returns a 10-character (30-bit)
+ * octal number in which the most significant bit is the sign bit.
+ * The remaining 29 bits are magnitude bits. Negative numbers are
+ * represented using two's-complement notation.
+ * If number < -536,870,912 or if number > 536,870,911, DEC2OCT
+ * returns the #NUM! error value.
+ * If number is nonnumeric, DEC2OCT returns the #VALUE! error value.
+ * If DEC2OCT requires more than places characters, it returns the
+ * #NUM! error value.
+ * @param int $places The number of characters to use. If places is omitted, DEC2OCT uses
+ * the minimum number of characters necessary. Places is useful for
+ * padding the return value with leading 0s (zeros).
+ * If places is not an integer, it is truncated.
+ * If places is nonnumeric, DEC2OCT returns the #VALUE! error value.
+ * If places is zero or negative, DEC2OCT returns the #NUM! error value.
+ *
+ * @return string
+ */
+ public static function DECTOOCT($x, $places = null)
+ {
+ $xorig = $x;
+ $x = Functions::flattenSingleValue($x);
+ $places = Functions::flattenSingleValue($places);
+
+ if (is_bool($x)) {
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ $x = (int) $x;
+ } else {
+ return Functions::VALUE();
+ }
+ }
+ $x = (string) $x;
+ if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) {
+ return Functions::VALUE();
+ }
+ $x = (string) floor($x);
+ $r = decoct($x);
+ if (strlen($r) == 11) {
+ // Two's Complement
+ $r = substr($r, -10);
+ }
+
+ return self::nbrConversionFormat($r, $places);
+ }
+
+ /**
+ * HEXTOBIN.
+ *
+ * Return a hex value as binary.
+ *
+ * Excel Function:
+ * HEX2BIN(x[,places])
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x the hexadecimal number you want to convert.
+ * Number cannot contain more than 10 characters.
+ * The most significant bit of number is the sign bit (40th bit from the right).
+ * The remaining 9 bits are magnitude bits.
+ * Negative numbers are represented using two's-complement notation.
+ * If number is negative, HEX2BIN ignores places and returns a 10-character binary number.
+ * If number is negative, it cannot be less than FFFFFFFE00,
+ * and if number is positive, it cannot be greater than 1FF.
+ * If number is not a valid hexadecimal number, HEX2BIN returns the #NUM! error value.
+ * If HEX2BIN requires more than places characters, it returns the #NUM! error value.
+ * @param int $places The number of characters to use. If places is omitted,
+ * HEX2BIN uses the minimum number of characters necessary. Places
+ * is useful for padding the return value with leading 0s (zeros).
+ * If places is not an integer, it is truncated.
+ * If places is nonnumeric, HEX2BIN returns the #VALUE! error value.
+ * If places is negative, HEX2BIN returns the #NUM! error value.
+ *
+ * @return string
+ */
+ public static function HEXTOBIN($x, $places = null)
+ {
+ $x = Functions::flattenSingleValue($x);
+ $places = Functions::flattenSingleValue($places);
+
+ if (is_bool($x)) {
+ return Functions::VALUE();
+ }
+ $x = (string) $x;
+ if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
+ return Functions::NAN();
+ }
+
+ return self::DECTOBIN(self::HEXTODEC($x), $places);
+ }
+
+ /**
+ * HEXTODEC.
+ *
+ * Return a hex value as decimal.
+ *
+ * Excel Function:
+ * HEX2DEC(x)
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The hexadecimal number you want to convert. This number cannot
+ * contain more than 10 characters (40 bits). The most significant
+ * bit of number is the sign bit. The remaining 39 bits are magnitude
+ * bits. Negative numbers are represented using two's-complement
+ * notation.
+ * If number is not a valid hexadecimal number, HEX2DEC returns the
+ * #NUM! error value.
+ *
+ * @return string
+ */
+ public static function HEXTODEC($x)
+ {
+ $x = Functions::flattenSingleValue($x);
+
+ if (is_bool($x)) {
+ return Functions::VALUE();
+ }
+ $x = (string) $x;
+ if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
+ return Functions::NAN();
+ }
+
+ if (strlen($x) > 10) {
+ return Functions::NAN();
+ }
+
+ $binX = '';
+ foreach (str_split($x) as $char) {
+ $binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
+ }
+ if (strlen($binX) == 40 && $binX[0] == '1') {
+ for ($i = 0; $i < 40; ++$i) {
+ $binX[$i] = ($binX[$i] == '1' ? '0' : '1');
+ }
+
+ return (bindec($binX) + 1) * -1;
+ }
+
+ return bindec($binX);
+ }
+
+ /**
+ * HEXTOOCT.
+ *
+ * Return a hex value as octal.
+ *
+ * Excel Function:
+ * HEX2OCT(x[,places])
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The hexadecimal number you want to convert. Number cannot
+ * contain more than 10 characters. The most significant bit of
+ * number is the sign bit. The remaining 39 bits are magnitude
+ * bits. Negative numbers are represented using two's-complement
+ * notation.
+ * If number is negative, HEX2OCT ignores places and returns a
+ * 10-character octal number.
+ * If number is negative, it cannot be less than FFE0000000, and
+ * if number is positive, it cannot be greater than 1FFFFFFF.
+ * If number is not a valid hexadecimal number, HEX2OCT returns
+ * the #NUM! error value.
+ * If HEX2OCT requires more than places characters, it returns
+ * the #NUM! error value.
+ * @param int $places The number of characters to use. If places is omitted, HEX2OCT
+ * uses the minimum number of characters necessary. Places is
+ * useful for padding the return value with leading 0s (zeros).
+ * If places is not an integer, it is truncated.
+ * If places is nonnumeric, HEX2OCT returns the #VALUE! error
+ * value.
+ * If places is negative, HEX2OCT returns the #NUM! error value.
+ *
+ * @return string
+ */
+ public static function HEXTOOCT($x, $places = null)
+ {
+ $x = Functions::flattenSingleValue($x);
+ $places = Functions::flattenSingleValue($places);
+
+ if (is_bool($x)) {
+ return Functions::VALUE();
+ }
+ $x = (string) $x;
+ if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
+ return Functions::NAN();
+ }
+
+ $decimal = self::HEXTODEC($x);
+ if ($decimal < -536870912 || $decimal > 536870911) {
+ return Functions::NAN();
+ }
+
+ return self::DECTOOCT($decimal, $places);
+ }
+
+ /**
+ * OCTTOBIN.
+ *
+ * Return an octal value as binary.
+ *
+ * Excel Function:
+ * OCT2BIN(x[,places])
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The octal number you want to convert. Number may not
+ * contain more than 10 characters. The most significant
+ * bit of number is the sign bit. The remaining 29 bits
+ * are magnitude bits. Negative numbers are represented
+ * using two's-complement notation.
+ * If number is negative, OCT2BIN ignores places and returns
+ * a 10-character binary number.
+ * If number is negative, it cannot be less than 7777777000,
+ * and if number is positive, it cannot be greater than 777.
+ * If number is not a valid octal number, OCT2BIN returns
+ * the #NUM! error value.
+ * If OCT2BIN requires more than places characters, it
+ * returns the #NUM! error value.
+ * @param int $places The number of characters to use. If places is omitted,
+ * OCT2BIN uses the minimum number of characters necessary.
+ * Places is useful for padding the return value with
+ * leading 0s (zeros).
+ * If places is not an integer, it is truncated.
+ * If places is nonnumeric, OCT2BIN returns the #VALUE!
+ * error value.
+ * If places is negative, OCT2BIN returns the #NUM! error
+ * value.
+ *
+ * @return string
+ */
+ public static function OCTTOBIN($x, $places = null)
+ {
+ $x = Functions::flattenSingleValue($x);
+ $places = Functions::flattenSingleValue($places);
+
+ if (is_bool($x)) {
+ return Functions::VALUE();
+ }
+ $x = (string) $x;
+ if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) {
+ return Functions::NAN();
+ }
+
+ return self::DECTOBIN(self::OCTTODEC($x), $places);
+ }
+
+ /**
+ * OCTTODEC.
+ *
+ * Return an octal value as decimal.
+ *
+ * Excel Function:
+ * OCT2DEC(x)
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The octal number you want to convert. Number may not contain
+ * more than 10 octal characters (30 bits). The most significant
+ * bit of number is the sign bit. The remaining 29 bits are
+ * magnitude bits. Negative numbers are represented using
+ * two's-complement notation.
+ * If number is not a valid octal number, OCT2DEC returns the
+ * #NUM! error value.
+ *
+ * @return string
+ */
+ public static function OCTTODEC($x)
+ {
+ $x = Functions::flattenSingleValue($x);
+
+ if (is_bool($x)) {
+ return Functions::VALUE();
+ }
+ $x = (string) $x;
+ if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) {
+ return Functions::NAN();
+ }
+ $binX = '';
+ foreach (str_split($x) as $char) {
+ $binX .= str_pad(decbin((int) $char), 3, '0', STR_PAD_LEFT);
+ }
+ if (strlen($binX) == 30 && $binX[0] == '1') {
+ for ($i = 0; $i < 30; ++$i) {
+ $binX[$i] = ($binX[$i] == '1' ? '0' : '1');
+ }
+
+ return (bindec($binX) + 1) * -1;
+ }
+
+ return bindec($binX);
+ }
+
+ /**
+ * OCTTOHEX.
+ *
+ * Return an octal value as hex.
+ *
+ * Excel Function:
+ * OCT2HEX(x[,places])
+ *
+ * @category Engineering Functions
+ *
+ * @param string $x The octal number you want to convert. Number may not contain
+ * more than 10 octal characters (30 bits). The most significant
+ * bit of number is the sign bit. The remaining 29 bits are
+ * magnitude bits. Negative numbers are represented using
+ * two's-complement notation.
+ * If number is negative, OCT2HEX ignores places and returns a
+ * 10-character hexadecimal number.
+ * If number is not a valid octal number, OCT2HEX returns the
+ * #NUM! error value.
+ * If OCT2HEX requires more than places characters, it returns
+ * the #NUM! error value.
+ * @param int $places The number of characters to use. If places is omitted, OCT2HEX
+ * uses the minimum number of characters necessary. Places is useful
+ * for padding the return value with leading 0s (zeros).
+ * If places is not an integer, it is truncated.
+ * If places is nonnumeric, OCT2HEX returns the #VALUE! error value.
+ * If places is negative, OCT2HEX returns the #NUM! error value.
+ *
+ * @return string
+ */
+ public static function OCTTOHEX($x, $places = null)
+ {
+ $x = Functions::flattenSingleValue($x);
+ $places = Functions::flattenSingleValue($places);
+
+ if (is_bool($x)) {
+ return Functions::VALUE();
+ }
+ $x = (string) $x;
+ if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) {
+ return Functions::NAN();
+ }
+ $hexVal = strtoupper(dechex(self::OCTTODEC($x)));
+
+ return self::nbrConversionFormat($hexVal, $places);
+ }
+
+ /**
+ * COMPLEX.
+ *
+ * Converts real and imaginary coefficients into a complex number of the form x +/- yi or x +/- yj.
+ *
+ * Excel Function:
+ * COMPLEX(realNumber,imaginary[,suffix])
+ *
+ * @category Engineering Functions
+ *
+ * @param float $realNumber the real coefficient of the complex number
+ * @param float $imaginary the imaginary coefficient of the complex number
+ * @param string $suffix The suffix for the imaginary component of the complex number.
+ * If omitted, the suffix is assumed to be "i".
+ *
+ * @return string
+ */
+ public static function COMPLEX($realNumber = 0.0, $imaginary = 0.0, $suffix = 'i')
+ {
+ $realNumber = ($realNumber === null) ? 0.0 : Functions::flattenSingleValue($realNumber);
+ $imaginary = ($imaginary === null) ? 0.0 : Functions::flattenSingleValue($imaginary);
+ $suffix = ($suffix === null) ? 'i' : Functions::flattenSingleValue($suffix);
+
+ if (((is_numeric($realNumber)) && (is_numeric($imaginary))) &&
+ (($suffix == 'i') || ($suffix == 'j') || ($suffix == ''))
+ ) {
+ $complex = new Complex($realNumber, $imaginary, $suffix);
+
+ return (string) $complex;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * IMAGINARY.
+ *
+ * Returns the imaginary coefficient of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMAGINARY(complexNumber)
+ *
+ * @category Engineering Functions
+ *
+ * @param string $complexNumber the complex number for which you want the imaginary
+ * coefficient
+ *
+ * @return float
+ */
+ public static function IMAGINARY($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (new Complex($complexNumber))->getImaginary();
+ }
+
+ /**
+ * IMREAL.
+ *
+ * Returns the real coefficient of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMREAL(complexNumber)
+ *
+ * @category Engineering Functions
+ *
+ * @param string $complexNumber the complex number for which you want the real coefficient
+ *
+ * @return float
+ */
+ public static function IMREAL($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (new Complex($complexNumber))->getReal();
+ }
+
+ /**
+ * IMABS.
+ *
+ * Returns the absolute value (modulus) of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMABS(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the absolute value
+ *
+ * @return float
+ */
+ public static function IMABS($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (new Complex($complexNumber))->abs();
+ }
+
+ /**
+ * IMARGUMENT.
+ *
+ * Returns the argument theta of a complex number, i.e. the angle in radians from the real
+ * axis to the representation of the number in polar coordinates.
+ *
+ * Excel Function:
+ * IMARGUMENT(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the argument theta
+ *
+ * @return float|string
+ */
+ public static function IMARGUMENT($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ $complex = new Complex($complexNumber);
+ if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
+ return Functions::DIV0();
+ }
+
+ return $complex->argument();
+ }
+
+ /**
+ * IMCONJUGATE.
+ *
+ * Returns the complex conjugate of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMCONJUGATE(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the conjugate
+ *
+ * @return string
+ */
+ public static function IMCONJUGATE($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->conjugate();
+ }
+
+ /**
+ * IMCOS.
+ *
+ * Returns the cosine of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMCOS(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the cosine
+ *
+ * @return float|string
+ */
+ public static function IMCOS($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->cos();
+ }
+
+ /**
+ * IMCOSH.
+ *
+ * Returns the hyperbolic cosine of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMCOSH(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the hyperbolic cosine
+ *
+ * @return float|string
+ */
+ public static function IMCOSH($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->cosh();
+ }
+
+ /**
+ * IMCOT.
+ *
+ * Returns the cotangent of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMCOT(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the cotangent
+ *
+ * @return float|string
+ */
+ public static function IMCOT($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->cot();
+ }
+
+ /**
+ * IMCSC.
+ *
+ * Returns the cosecant of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMCSC(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the cosecant
+ *
+ * @return float|string
+ */
+ public static function IMCSC($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->csc();
+ }
+
+ /**
+ * IMCSCH.
+ *
+ * Returns the hyperbolic cosecant of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMCSCH(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the hyperbolic cosecant
+ *
+ * @return float|string
+ */
+ public static function IMCSCH($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->csch();
+ }
+
+ /**
+ * IMSIN.
+ *
+ * Returns the sine of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMSIN(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the sine
+ *
+ * @return float|string
+ */
+ public static function IMSIN($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->sin();
+ }
+
+ /**
+ * IMSINH.
+ *
+ * Returns the hyperbolic sine of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMSINH(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the hyperbolic sine
+ *
+ * @return float|string
+ */
+ public static function IMSINH($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->sinh();
+ }
+
+ /**
+ * IMSEC.
+ *
+ * Returns the secant of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMSEC(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the secant
+ *
+ * @return float|string
+ */
+ public static function IMSEC($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->sec();
+ }
+
+ /**
+ * IMSECH.
+ *
+ * Returns the hyperbolic secant of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMSECH(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the hyperbolic secant
+ *
+ * @return float|string
+ */
+ public static function IMSECH($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->sech();
+ }
+
+ /**
+ * IMTAN.
+ *
+ * Returns the tangent of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMTAN(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the tangent
+ *
+ * @return float|string
+ */
+ public static function IMTAN($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->tan();
+ }
+
+ /**
+ * IMSQRT.
+ *
+ * Returns the square root of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMSQRT(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the square root
+ *
+ * @return string
+ */
+ public static function IMSQRT($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ $theta = self::IMARGUMENT($complexNumber);
+ if ($theta === Functions::DIV0()) {
+ return '0';
+ }
+
+ return (string) (new Complex($complexNumber))->sqrt();
+ }
+
+ /**
+ * IMLN.
+ *
+ * Returns the natural logarithm of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMLN(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the natural logarithm
+ *
+ * @return string
+ */
+ public static function IMLN($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ $complex = new Complex($complexNumber);
+ if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
+ return Functions::NAN();
+ }
+
+ return (string) (new Complex($complexNumber))->ln();
+ }
+
+ /**
+ * IMLOG10.
+ *
+ * Returns the common logarithm (base 10) of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMLOG10(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the common logarithm
+ *
+ * @return string
+ */
+ public static function IMLOG10($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ $complex = new Complex($complexNumber);
+ if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
+ return Functions::NAN();
+ }
+
+ return (string) (new Complex($complexNumber))->log10();
+ }
+
+ /**
+ * IMLOG2.
+ *
+ * Returns the base-2 logarithm of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMLOG2(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the base-2 logarithm
+ *
+ * @return string
+ */
+ public static function IMLOG2($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ $complex = new Complex($complexNumber);
+ if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
+ return Functions::NAN();
+ }
+
+ return (string) (new Complex($complexNumber))->log2();
+ }
+
+ /**
+ * IMEXP.
+ *
+ * Returns the exponential of a complex number in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMEXP(complexNumber)
+ *
+ * @param string $complexNumber the complex number for which you want the exponential
+ *
+ * @return string
+ */
+ public static function IMEXP($complexNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+
+ return (string) (new Complex($complexNumber))->exp();
+ }
+
+ /**
+ * IMPOWER.
+ *
+ * Returns a complex number in x + yi or x + yj text format raised to a power.
+ *
+ * Excel Function:
+ * IMPOWER(complexNumber,realNumber)
+ *
+ * @param string $complexNumber the complex number you want to raise to a power
+ * @param float $realNumber the power to which you want to raise the complex number
+ *
+ * @return string
+ */
+ public static function IMPOWER($complexNumber, $realNumber)
+ {
+ $complexNumber = Functions::flattenSingleValue($complexNumber);
+ $realNumber = Functions::flattenSingleValue($realNumber);
+
+ if (!is_numeric($realNumber)) {
+ return Functions::VALUE();
+ }
+
+ return (string) (new Complex($complexNumber))->pow($realNumber);
+ }
+
+ /**
+ * IMDIV.
+ *
+ * Returns the quotient of two complex numbers in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMDIV(complexDividend,complexDivisor)
+ *
+ * @param string $complexDividend the complex numerator or dividend
+ * @param string $complexDivisor the complex denominator or divisor
+ *
+ * @return string
+ */
+ public static function IMDIV($complexDividend, $complexDivisor)
+ {
+ $complexDividend = Functions::flattenSingleValue($complexDividend);
+ $complexDivisor = Functions::flattenSingleValue($complexDivisor);
+
+ try {
+ return (string) (new Complex($complexDividend))->divideby(new Complex($complexDivisor));
+ } catch (ComplexException $e) {
+ return Functions::NAN();
+ }
+ }
+
+ /**
+ * IMSUB.
+ *
+ * Returns the difference of two complex numbers in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMSUB(complexNumber1,complexNumber2)
+ *
+ * @param string $complexNumber1 the complex number from which to subtract complexNumber2
+ * @param string $complexNumber2 the complex number to subtract from complexNumber1
+ *
+ * @return string
+ */
+ public static function IMSUB($complexNumber1, $complexNumber2)
+ {
+ $complexNumber1 = Functions::flattenSingleValue($complexNumber1);
+ $complexNumber2 = Functions::flattenSingleValue($complexNumber2);
+
+ try {
+ return (string) (new Complex($complexNumber1))->subtract(new Complex($complexNumber2));
+ } catch (ComplexException $e) {
+ return Functions::NAN();
+ }
+ }
+
+ /**
+ * IMSUM.
+ *
+ * Returns the sum of two or more complex numbers in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMSUM(complexNumber[,complexNumber[,...]])
+ *
+ * @param string ...$complexNumbers Series of complex numbers to add
+ *
+ * @return string
+ */
+ public static function IMSUM(...$complexNumbers)
+ {
+ // Return value
+ $returnValue = new Complex(0.0);
+ $aArgs = Functions::flattenArray($complexNumbers);
+
+ try {
+ // Loop through the arguments
+ foreach ($aArgs as $complex) {
+ $returnValue = $returnValue->add(new Complex($complex));
+ }
+ } catch (ComplexException $e) {
+ return Functions::NAN();
+ }
+
+ return (string) $returnValue;
+ }
+
+ /**
+ * IMPRODUCT.
+ *
+ * Returns the product of two or more complex numbers in x + yi or x + yj text format.
+ *
+ * Excel Function:
+ * IMPRODUCT(complexNumber[,complexNumber[,...]])
+ *
+ * @param string ...$complexNumbers Series of complex numbers to multiply
+ *
+ * @return string
+ */
+ public static function IMPRODUCT(...$complexNumbers)
+ {
+ // Return value
+ $returnValue = new Complex(1.0);
+ $aArgs = Functions::flattenArray($complexNumbers);
+
+ try {
+ // Loop through the arguments
+ foreach ($aArgs as $complex) {
+ $returnValue = $returnValue->multiply(new Complex($complex));
+ }
+ } catch (ComplexException $e) {
+ return Functions::NAN();
+ }
+
+ return (string) $returnValue;
+ }
+
+ /**
+ * DELTA.
+ *
+ * Tests whether two values are equal. Returns 1 if number1 = number2; returns 0 otherwise.
+ * Use this function to filter a set of values. For example, by summing several DELTA
+ * functions you calculate the count of equal pairs. This function is also known as the
+ * Kronecker Delta function.
+ *
+ * Excel Function:
+ * DELTA(a[,b])
+ *
+ * @param float $a the first number
+ * @param float $b The second number. If omitted, b is assumed to be zero.
+ *
+ * @return int
+ */
+ public static function DELTA($a, $b = 0)
+ {
+ $a = Functions::flattenSingleValue($a);
+ $b = Functions::flattenSingleValue($b);
+
+ return (int) ($a == $b);
+ }
+
+ /**
+ * GESTEP.
+ *
+ * Excel Function:
+ * GESTEP(number[,step])
+ *
+ * Returns 1 if number >= step; returns 0 (zero) otherwise
+ * Use this function to filter a set of values. For example, by summing several GESTEP
+ * functions you calculate the count of values that exceed a threshold.
+ *
+ * @param float $number the value to test against step
+ * @param float $step The threshold value.
+ * If you omit a value for step, GESTEP uses zero.
+ *
+ * @return int
+ */
+ public static function GESTEP($number, $step = 0)
+ {
+ $number = Functions::flattenSingleValue($number);
+ $step = Functions::flattenSingleValue($step);
+
+ return (int) ($number >= $step);
+ }
+
+ //
+ // Private method to calculate the erf value
+ //
+ private static $twoSqrtPi = 1.128379167095512574;
+
+ public static function erfVal($x)
+ {
+ if (abs($x) > 2.2) {
+ return 1 - self::erfcVal($x);
+ }
+ $sum = $term = $x;
+ $xsqr = ($x * $x);
+ $j = 1;
+ do {
+ $term *= $xsqr / $j;
+ $sum -= $term / (2 * $j + 1);
+ ++$j;
+ $term *= $xsqr / $j;
+ $sum += $term / (2 * $j + 1);
+ ++$j;
+ if ($sum == 0.0) {
+ break;
+ }
+ } while (abs($term / $sum) > Functions::PRECISION);
+
+ return self::$twoSqrtPi * $sum;
+ }
+
+ /**
+ * Validate arguments passed to the bitwise functions.
+ *
+ * @param mixed $value
+ *
+ * @throws Exception
+ *
+ * @return int
+ */
+ private static function validateBitwiseArgument($value)
+ {
+ $value = Functions::flattenSingleValue($value);
+
+ if (is_int($value)) {
+ return $value;
+ } elseif (is_numeric($value)) {
+ if ($value == (int) ($value)) {
+ $value = (int) ($value);
+ if (($value > pow(2, 48) - 1) || ($value < 0)) {
+ throw new Exception(Functions::NAN());
+ }
+
+ return $value;
+ }
+
+ throw new Exception(Functions::NAN());
+ }
+
+ throw new Exception(Functions::VALUE());
+ }
+
+ /**
+ * BITAND.
+ *
+ * Returns the bitwise AND of two integer values.
+ *
+ * Excel Function:
+ * BITAND(number1, number2)
+ *
+ * @category Engineering Functions
+ *
+ * @param int $number1
+ * @param int $number2
+ *
+ * @return int|string
+ */
+ public static function BITAND($number1, $number2)
+ {
+ try {
+ $number1 = self::validateBitwiseArgument($number1);
+ $number2 = self::validateBitwiseArgument($number2);
+ } catch (Exception $e) {
+ return $e->getMessage();
+ }
+
+ return $number1 & $number2;
+ }
+
+ /**
+ * BITOR.
+ *
+ * Returns the bitwise OR of two integer values.
+ *
+ * Excel Function:
+ * BITOR(number1, number2)
+ *
+ * @category Engineering Functions
+ *
+ * @param int $number1
+ * @param int $number2
+ *
+ * @return int|string
+ */
+ public static function BITOR($number1, $number2)
+ {
+ try {
+ $number1 = self::validateBitwiseArgument($number1);
+ $number2 = self::validateBitwiseArgument($number2);
+ } catch (Exception $e) {
+ return $e->getMessage();
+ }
+
+ return $number1 | $number2;
+ }
+
+ /**
+ * BITXOR.
+ *
+ * Returns the bitwise XOR of two integer values.
+ *
+ * Excel Function:
+ * BITXOR(number1, number2)
+ *
+ * @category Engineering Functions
+ *
+ * @param int $number1
+ * @param int $number2
+ *
+ * @return int|string
+ */
+ public static function BITXOR($number1, $number2)
+ {
+ try {
+ $number1 = self::validateBitwiseArgument($number1);
+ $number2 = self::validateBitwiseArgument($number2);
+ } catch (Exception $e) {
+ return $e->getMessage();
+ }
+
+ return $number1 ^ $number2;
+ }
+
+ /**
+ * BITLSHIFT.
+ *
+ * Returns the number value shifted left by shift_amount bits.
+ *
+ * Excel Function:
+ * BITLSHIFT(number, shift_amount)
+ *
+ * @category Engineering Functions
+ *
+ * @param int $number
+ * @param int $shiftAmount
+ *
+ * @return int|string
+ */
+ public static function BITLSHIFT($number, $shiftAmount)
+ {
+ try {
+ $number = self::validateBitwiseArgument($number);
+ } catch (Exception $e) {
+ return $e->getMessage();
+ }
+
+ $shiftAmount = Functions::flattenSingleValue($shiftAmount);
+
+ $result = $number << $shiftAmount;
+ if ($result > pow(2, 48) - 1) {
+ return Functions::NAN();
+ }
+
+ return $result;
+ }
+
+ /**
+ * BITRSHIFT.
+ *
+ * Returns the number value shifted right by shift_amount bits.
+ *
+ * Excel Function:
+ * BITRSHIFT(number, shift_amount)
+ *
+ * @category Engineering Functions
+ *
+ * @param int $number
+ * @param int $shiftAmount
+ *
+ * @return int|string
+ */
+ public static function BITRSHIFT($number, $shiftAmount)
+ {
+ try {
+ $number = self::validateBitwiseArgument($number);
+ } catch (Exception $e) {
+ return $e->getMessage();
+ }
+
+ $shiftAmount = Functions::flattenSingleValue($shiftAmount);
+
+ return $number >> $shiftAmount;
+ }
+
+ /**
+ * ERF.
+ *
+ * Returns the error function integrated between the lower and upper bound arguments.
+ *
+ * Note: In Excel 2007 or earlier, if you input a negative value for the upper or lower bound arguments,
+ * the function would return a #NUM! error. However, in Excel 2010, the function algorithm was
+ * improved, so that it can now calculate the function for both positive and negative ranges.
+ * PhpSpreadsheet follows Excel 2010 behaviour, and accepts negative arguments.
+ *
+ * Excel Function:
+ * ERF(lower[,upper])
+ *
+ * @param float $lower lower bound for integrating ERF
+ * @param float $upper upper bound for integrating ERF.
+ * If omitted, ERF integrates between zero and lower_limit
+ *
+ * @return float|string
+ */
+ public static function ERF($lower, $upper = null)
+ {
+ $lower = Functions::flattenSingleValue($lower);
+ $upper = Functions::flattenSingleValue($upper);
+
+ if (is_numeric($lower)) {
+ if ($upper === null) {
+ return self::erfVal($lower);
+ }
+ if (is_numeric($upper)) {
+ return self::erfVal($upper) - self::erfVal($lower);
+ }
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * ERFPRECISE.
+ *
+ * Returns the error function integrated between the lower and upper bound arguments.
+ *
+ * Excel Function:
+ * ERF.PRECISE(limit)
+ *
+ * @param float $limit bound for integrating ERF
+ *
+ * @return float|string
+ */
+ public static function ERFPRECISE($limit)
+ {
+ $limit = Functions::flattenSingleValue($limit);
+
+ return self::ERF($limit);
+ }
+
+ //
+ // Private method to calculate the erfc value
+ //
+ private static $oneSqrtPi = 0.564189583547756287;
+
+ private static function erfcVal($x)
+ {
+ if (abs($x) < 2.2) {
+ return 1 - self::erfVal($x);
+ }
+ if ($x < 0) {
+ return 2 - self::ERFC(-$x);
+ }
+ $a = $n = 1;
+ $b = $c = $x;
+ $d = ($x * $x) + 0.5;
+ $q1 = $q2 = $b / $d;
+ $t = 0;
+ do {
+ $t = $a * $n + $b * $x;
+ $a = $b;
+ $b = $t;
+ $t = $c * $n + $d * $x;
+ $c = $d;
+ $d = $t;
+ $n += 0.5;
+ $q1 = $q2;
+ $q2 = $b / $d;
+ } while ((abs($q1 - $q2) / $q2) > Functions::PRECISION);
+
+ return self::$oneSqrtPi * exp(-$x * $x) * $q2;
+ }
+
+ /**
+ * ERFC.
+ *
+ * Returns the complementary ERF function integrated between x and infinity
+ *
+ * Note: In Excel 2007 or earlier, if you input a negative value for the lower bound argument,
+ * the function would return a #NUM! error. However, in Excel 2010, the function algorithm was
+ * improved, so that it can now calculate the function for both positive and negative x values.
+ * PhpSpreadsheet follows Excel 2010 behaviour, and accepts nagative arguments.
+ *
+ * Excel Function:
+ * ERFC(x)
+ *
+ * @param float $x The lower bound for integrating ERFC
+ *
+ * @return float|string
+ */
+ public static function ERFC($x)
+ {
+ $x = Functions::flattenSingleValue($x);
+
+ if (is_numeric($x)) {
+ return self::erfcVal($x);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * getConversionGroups
+ * Returns a list of the different conversion groups for UOM conversions.
+ *
+ * @return array
+ */
+ public static function getConversionGroups()
+ {
+ $conversionGroups = [];
+ foreach (self::$conversionUnits as $conversionUnit) {
+ $conversionGroups[] = $conversionUnit['Group'];
+ }
+
+ return array_merge(array_unique($conversionGroups));
+ }
+
+ /**
+ * getConversionGroupUnits
+ * Returns an array of units of measure, for a specified conversion group, or for all groups.
+ *
+ * @param string $group The group whose units of measure you want to retrieve
+ *
+ * @return array
+ */
+ public static function getConversionGroupUnits($group = null)
+ {
+ $conversionGroups = [];
+ foreach (self::$conversionUnits as $conversionUnit => $conversionGroup) {
+ if (($group === null) || ($conversionGroup['Group'] == $group)) {
+ $conversionGroups[$conversionGroup['Group']][] = $conversionUnit;
+ }
+ }
+
+ return $conversionGroups;
+ }
+
+ /**
+ * getConversionGroupUnitDetails.
+ *
+ * @param string $group The group whose units of measure you want to retrieve
+ *
+ * @return array
+ */
+ public static function getConversionGroupUnitDetails($group = null)
+ {
+ $conversionGroups = [];
+ foreach (self::$conversionUnits as $conversionUnit => $conversionGroup) {
+ if (($group === null) || ($conversionGroup['Group'] == $group)) {
+ $conversionGroups[$conversionGroup['Group']][] = [
+ 'unit' => $conversionUnit,
+ 'description' => $conversionGroup['Unit Name'],
+ ];
+ }
+ }
+
+ return $conversionGroups;
+ }
+
+ /**
+ * getConversionMultipliers
+ * Returns an array of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM().
+ *
+ * @return array of mixed
+ */
+ public static function getConversionMultipliers()
+ {
+ return self::$conversionMultipliers;
+ }
+
+ /**
+ * CONVERTUOM.
+ *
+ * Converts a number from one measurement system to another.
+ * For example, CONVERT can translate a table of distances in miles to a table of distances
+ * in kilometers.
+ *
+ * Excel Function:
+ * CONVERT(value,fromUOM,toUOM)
+ *
+ * @param float $value the value in fromUOM to convert
+ * @param string $fromUOM the units for value
+ * @param string $toUOM the units for the result
+ *
+ * @return float
+ */
+ public static function CONVERTUOM($value, $fromUOM, $toUOM)
+ {
+ $value = Functions::flattenSingleValue($value);
+ $fromUOM = Functions::flattenSingleValue($fromUOM);
+ $toUOM = Functions::flattenSingleValue($toUOM);
+
+ if (!is_numeric($value)) {
+ return Functions::VALUE();
+ }
+ $fromMultiplier = 1.0;
+ if (isset(self::$conversionUnits[$fromUOM])) {
+ $unitGroup1 = self::$conversionUnits[$fromUOM]['Group'];
+ } else {
+ $fromMultiplier = substr($fromUOM, 0, 1);
+ $fromUOM = substr($fromUOM, 1);
+ if (isset(self::$conversionMultipliers[$fromMultiplier])) {
+ $fromMultiplier = self::$conversionMultipliers[$fromMultiplier]['multiplier'];
+ } else {
+ return Functions::NA();
+ }
+ if ((isset(self::$conversionUnits[$fromUOM])) && (self::$conversionUnits[$fromUOM]['AllowPrefix'])) {
+ $unitGroup1 = self::$conversionUnits[$fromUOM]['Group'];
+ } else {
+ return Functions::NA();
+ }
+ }
+ $value *= $fromMultiplier;
+
+ $toMultiplier = 1.0;
+ if (isset(self::$conversionUnits[$toUOM])) {
+ $unitGroup2 = self::$conversionUnits[$toUOM]['Group'];
+ } else {
+ $toMultiplier = substr($toUOM, 0, 1);
+ $toUOM = substr($toUOM, 1);
+ if (isset(self::$conversionMultipliers[$toMultiplier])) {
+ $toMultiplier = self::$conversionMultipliers[$toMultiplier]['multiplier'];
+ } else {
+ return Functions::NA();
+ }
+ if ((isset(self::$conversionUnits[$toUOM])) && (self::$conversionUnits[$toUOM]['AllowPrefix'])) {
+ $unitGroup2 = self::$conversionUnits[$toUOM]['Group'];
+ } else {
+ return Functions::NA();
+ }
+ }
+ if ($unitGroup1 != $unitGroup2) {
+ return Functions::NA();
+ }
+
+ if (($fromUOM == $toUOM) && ($fromMultiplier == $toMultiplier)) {
+ // We've already factored $fromMultiplier into the value, so we need
+ // to reverse it again
+ return $value / $fromMultiplier;
+ } elseif ($unitGroup1 == 'Temperature') {
+ if (($fromUOM == 'F') || ($fromUOM == 'fah')) {
+ if (($toUOM == 'F') || ($toUOM == 'fah')) {
+ return $value;
+ }
+ $value = (($value - 32) / 1.8);
+ if (($toUOM == 'K') || ($toUOM == 'kel')) {
+ $value += 273.15;
+ }
+
+ return $value;
+ } elseif ((($fromUOM == 'K') || ($fromUOM == 'kel')) &&
+ (($toUOM == 'K') || ($toUOM == 'kel'))
+ ) {
+ return $value;
+ } elseif ((($fromUOM == 'C') || ($fromUOM == 'cel')) &&
+ (($toUOM == 'C') || ($toUOM == 'cel'))
+ ) {
+ return $value;
+ }
+ if (($toUOM == 'F') || ($toUOM == 'fah')) {
+ if (($fromUOM == 'K') || ($fromUOM == 'kel')) {
+ $value -= 273.15;
+ }
+
+ return ($value * 1.8) + 32;
+ }
+ if (($toUOM == 'C') || ($toUOM == 'cel')) {
+ return $value - 273.15;
+ }
+
+ return $value + 273.15;
+ }
+
+ return ($value * self::$unitConversions[$unitGroup1][$fromUOM][$toUOM]) / $toMultiplier;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Exception.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Exception.php
new file mode 100644
index 00000000000..fccf0af70fd
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Exception.php
@@ -0,0 +1,26 @@
+line = $line;
+ $e->file = $file;
+
+ throw $e;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/ExceptionHandler.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/ExceptionHandler.php
new file mode 100644
index 00000000000..41e51d4aea6
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/ExceptionHandler.php
@@ -0,0 +1,22 @@
+format('d') == $testDate->format('t');
+ }
+
+ private static function couponFirstPeriodDate($settlement, $maturity, $frequency, $next)
+ {
+ $months = 12 / $frequency;
+
+ $result = Date::excelToDateTimeObject($maturity);
+ $eom = self::isLastDayOfMonth($result);
+
+ while ($settlement < Date::PHPToExcel($result)) {
+ $result->modify('-' . $months . ' months');
+ }
+ if ($next) {
+ $result->modify('+' . $months . ' months');
+ }
+
+ if ($eom) {
+ $result->modify('-1 day');
+ }
+
+ return Date::PHPToExcel($result);
+ }
+
+ private static function isValidFrequency($frequency)
+ {
+ if (($frequency == 1) || ($frequency == 2) || ($frequency == 4)) {
+ return true;
+ }
+ if ((Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) &&
+ (($frequency == 6) || ($frequency == 12))) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * daysPerYear.
+ *
+ * Returns the number of days in a specified year, as defined by the "basis" value
+ *
+ * @param int|string $year The year against which we're testing
+ * @param int|string $basis The type of day count:
+ * 0 or omitted US (NASD) 360
+ * 1 Actual (365 or 366 in a leap year)
+ * 2 360
+ * 3 365
+ * 4 European 360
+ *
+ * @return int
+ */
+ private static function daysPerYear($year, $basis = 0)
+ {
+ switch ($basis) {
+ case 0:
+ case 2:
+ case 4:
+ $daysPerYear = 360;
+
+ break;
+ case 3:
+ $daysPerYear = 365;
+
+ break;
+ case 1:
+ $daysPerYear = (DateTime::isLeapYear($year)) ? 366 : 365;
+
+ break;
+ default:
+ return Functions::NAN();
+ }
+
+ return $daysPerYear;
+ }
+
+ private static function interestAndPrincipal($rate = 0, $per = 0, $nper = 0, $pv = 0, $fv = 0, $type = 0)
+ {
+ $pmt = self::PMT($rate, $nper, $pv, $fv, $type);
+ $capital = $pv;
+ for ($i = 1; $i <= $per; ++$i) {
+ $interest = ($type && $i == 1) ? 0 : -$capital * $rate;
+ $principal = $pmt - $interest;
+ $capital += $principal;
+ }
+
+ return [$interest, $principal];
+ }
+
+ /**
+ * ACCRINT.
+ *
+ * Returns the accrued interest for a security that pays periodic interest.
+ *
+ * Excel Function:
+ * ACCRINT(issue,firstinterest,settlement,rate,par,frequency[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param mixed $issue the security's issue date
+ * @param mixed $firstinterest the security's first interest date
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue date
+ * when the security is traded to the buyer.
+ * @param float $rate the security's annual coupon rate
+ * @param float $par The security's par value.
+ * If you omit par, ACCRINT uses $1,000.
+ * @param int $frequency the number of coupon payments per year.
+ * Valid frequency values are:
+ * 1 Annual
+ * 2 Semi-Annual
+ * 4 Quarterly
+ * If working in Gnumeric Mode, the following frequency options are
+ * also available
+ * 6 Bimonthly
+ * 12 Monthly
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float|string
+ */
+ public static function ACCRINT($issue, $firstinterest, $settlement, $rate, $par = 1000, $frequency = 1, $basis = 0)
+ {
+ $issue = Functions::flattenSingleValue($issue);
+ $firstinterest = Functions::flattenSingleValue($firstinterest);
+ $settlement = Functions::flattenSingleValue($settlement);
+ $rate = Functions::flattenSingleValue($rate);
+ $par = ($par === null) ? 1000 : Functions::flattenSingleValue($par);
+ $frequency = ($frequency === null) ? 1 : Functions::flattenSingleValue($frequency);
+ $basis = ($basis === null) ? 0 : Functions::flattenSingleValue($basis);
+
+ // Validate
+ if ((is_numeric($rate)) && (is_numeric($par))) {
+ $rate = (float) $rate;
+ $par = (float) $par;
+ if (($rate <= 0) || ($par <= 0)) {
+ return Functions::NAN();
+ }
+ $daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis);
+ if (!is_numeric($daysBetweenIssueAndSettlement)) {
+ // return date error
+ return $daysBetweenIssueAndSettlement;
+ }
+
+ return $par * $rate * $daysBetweenIssueAndSettlement;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * ACCRINTM.
+ *
+ * Returns the accrued interest for a security that pays interest at maturity.
+ *
+ * Excel Function:
+ * ACCRINTM(issue,settlement,rate[,par[,basis]])
+ *
+ * @category Financial Functions
+ *
+ * @param mixed $issue The security's issue date
+ * @param mixed $settlement The security's settlement (or maturity) date
+ * @param float $rate The security's annual coupon rate
+ * @param float $par The security's par value.
+ * If you omit par, ACCRINT uses $1,000.
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float|string
+ */
+ public static function ACCRINTM($issue, $settlement, $rate, $par = 1000, $basis = 0)
+ {
+ $issue = Functions::flattenSingleValue($issue);
+ $settlement = Functions::flattenSingleValue($settlement);
+ $rate = Functions::flattenSingleValue($rate);
+ $par = ($par === null) ? 1000 : Functions::flattenSingleValue($par);
+ $basis = ($basis === null) ? 0 : Functions::flattenSingleValue($basis);
+
+ // Validate
+ if ((is_numeric($rate)) && (is_numeric($par))) {
+ $rate = (float) $rate;
+ $par = (float) $par;
+ if (($rate <= 0) || ($par <= 0)) {
+ return Functions::NAN();
+ }
+ $daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis);
+ if (!is_numeric($daysBetweenIssueAndSettlement)) {
+ // return date error
+ return $daysBetweenIssueAndSettlement;
+ }
+
+ return $par * $rate * $daysBetweenIssueAndSettlement;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * AMORDEGRC.
+ *
+ * Returns the depreciation for each accounting period.
+ * This function is provided for the French accounting system. If an asset is purchased in
+ * the middle of the accounting period, the prorated depreciation is taken into account.
+ * The function is similar to AMORLINC, except that a depreciation coefficient is applied in
+ * the calculation depending on the life of the assets.
+ * This function will return the depreciation until the last period of the life of the assets
+ * or until the cumulated value of depreciation is greater than the cost of the assets minus
+ * the salvage value.
+ *
+ * Excel Function:
+ * AMORDEGRC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param float $cost The cost of the asset
+ * @param mixed $purchased Date of the purchase of the asset
+ * @param mixed $firstPeriod Date of the end of the first period
+ * @param mixed $salvage The salvage value at the end of the life of the asset
+ * @param float $period The period
+ * @param float $rate Rate of depreciation
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float
+ */
+ public static function AMORDEGRC($cost, $purchased, $firstPeriod, $salvage, $period, $rate, $basis = 0)
+ {
+ $cost = Functions::flattenSingleValue($cost);
+ $purchased = Functions::flattenSingleValue($purchased);
+ $firstPeriod = Functions::flattenSingleValue($firstPeriod);
+ $salvage = Functions::flattenSingleValue($salvage);
+ $period = floor(Functions::flattenSingleValue($period));
+ $rate = Functions::flattenSingleValue($rate);
+ $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
+
+ // The depreciation coefficients are:
+ // Life of assets (1/rate) Depreciation coefficient
+ // Less than 3 years 1
+ // Between 3 and 4 years 1.5
+ // Between 5 and 6 years 2
+ // More than 6 years 2.5
+ $fUsePer = 1.0 / $rate;
+ if ($fUsePer < 3.0) {
+ $amortiseCoeff = 1.0;
+ } elseif ($fUsePer < 5.0) {
+ $amortiseCoeff = 1.5;
+ } elseif ($fUsePer <= 6.0) {
+ $amortiseCoeff = 2.0;
+ } else {
+ $amortiseCoeff = 2.5;
+ }
+
+ $rate *= $amortiseCoeff;
+ $fNRate = round(DateTime::YEARFRAC($purchased, $firstPeriod, $basis) * $rate * $cost, 0);
+ $cost -= $fNRate;
+ $fRest = $cost - $salvage;
+
+ for ($n = 0; $n < $period; ++$n) {
+ $fNRate = round($rate * $cost, 0);
+ $fRest -= $fNRate;
+
+ if ($fRest < 0.0) {
+ switch ($period - $n) {
+ case 0:
+ case 1:
+ return round($cost * 0.5, 0);
+ default:
+ return 0.0;
+ }
+ }
+ $cost -= $fNRate;
+ }
+
+ return $fNRate;
+ }
+
+ /**
+ * AMORLINC.
+ *
+ * Returns the depreciation for each accounting period.
+ * This function is provided for the French accounting system. If an asset is purchased in
+ * the middle of the accounting period, the prorated depreciation is taken into account.
+ *
+ * Excel Function:
+ * AMORLINC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param float $cost The cost of the asset
+ * @param mixed $purchased Date of the purchase of the asset
+ * @param mixed $firstPeriod Date of the end of the first period
+ * @param mixed $salvage The salvage value at the end of the life of the asset
+ * @param float $period The period
+ * @param float $rate Rate of depreciation
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float
+ */
+ public static function AMORLINC($cost, $purchased, $firstPeriod, $salvage, $period, $rate, $basis = 0)
+ {
+ $cost = Functions::flattenSingleValue($cost);
+ $purchased = Functions::flattenSingleValue($purchased);
+ $firstPeriod = Functions::flattenSingleValue($firstPeriod);
+ $salvage = Functions::flattenSingleValue($salvage);
+ $period = Functions::flattenSingleValue($period);
+ $rate = Functions::flattenSingleValue($rate);
+ $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
+
+ $fOneRate = $cost * $rate;
+ $fCostDelta = $cost - $salvage;
+ // Note, quirky variation for leap years on the YEARFRAC for this function
+ $purchasedYear = DateTime::YEAR($purchased);
+ $yearFrac = DateTime::YEARFRAC($purchased, $firstPeriod, $basis);
+
+ if (($basis == 1) && ($yearFrac < 1) && (DateTime::isLeapYear($purchasedYear))) {
+ $yearFrac *= 365 / 366;
+ }
+
+ $f0Rate = $yearFrac * $rate * $cost;
+ $nNumOfFullPeriods = (int) (($cost - $salvage - $f0Rate) / $fOneRate);
+
+ if ($period == 0) {
+ return $f0Rate;
+ } elseif ($period <= $nNumOfFullPeriods) {
+ return $fOneRate;
+ } elseif ($period == ($nNumOfFullPeriods + 1)) {
+ return $fCostDelta - $fOneRate * $nNumOfFullPeriods - $f0Rate;
+ }
+
+ return 0.0;
+ }
+
+ /**
+ * COUPDAYBS.
+ *
+ * Returns the number of days from the beginning of the coupon period to the settlement date.
+ *
+ * Excel Function:
+ * COUPDAYBS(settlement,maturity,frequency[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue
+ * date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param int $frequency the number of coupon payments per year.
+ * Valid frequency values are:
+ * 1 Annual
+ * 2 Semi-Annual
+ * 4 Quarterly
+ * If working in Gnumeric Mode, the following frequency options are
+ * also available
+ * 6 Bimonthly
+ * 12 Monthly
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float|string
+ */
+ public static function COUPDAYBS($settlement, $maturity, $frequency, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $frequency = (int) Functions::flattenSingleValue($frequency);
+ $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
+
+ if (is_string($settlement = DateTime::getDateValue($settlement))) {
+ return Functions::VALUE();
+ }
+ if (is_string($maturity = DateTime::getDateValue($maturity))) {
+ return Functions::VALUE();
+ }
+
+ if (($settlement > $maturity) ||
+ (!self::isValidFrequency($frequency)) ||
+ (($basis < 0) || ($basis > 4))) {
+ return Functions::NAN();
+ }
+
+ $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
+ $prev = self::couponFirstPeriodDate($settlement, $maturity, $frequency, false);
+
+ return DateTime::YEARFRAC($prev, $settlement, $basis) * $daysPerYear;
+ }
+
+ /**
+ * COUPDAYS.
+ *
+ * Returns the number of days in the coupon period that contains the settlement date.
+ *
+ * Excel Function:
+ * COUPDAYS(settlement,maturity,frequency[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue
+ * date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param mixed $frequency the number of coupon payments per year.
+ * Valid frequency values are:
+ * 1 Annual
+ * 2 Semi-Annual
+ * 4 Quarterly
+ * If working in Gnumeric Mode, the following frequency options are
+ * also available
+ * 6 Bimonthly
+ * 12 Monthly
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float|string
+ */
+ public static function COUPDAYS($settlement, $maturity, $frequency, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $frequency = (int) Functions::flattenSingleValue($frequency);
+ $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
+
+ if (is_string($settlement = DateTime::getDateValue($settlement))) {
+ return Functions::VALUE();
+ }
+ if (is_string($maturity = DateTime::getDateValue($maturity))) {
+ return Functions::VALUE();
+ }
+
+ if (($settlement > $maturity) ||
+ (!self::isValidFrequency($frequency)) ||
+ (($basis < 0) || ($basis > 4))) {
+ return Functions::NAN();
+ }
+
+ switch ($basis) {
+ case 3:
+ // Actual/365
+ return 365 / $frequency;
+ case 1:
+ // Actual/actual
+ if ($frequency == 1) {
+ $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
+
+ return $daysPerYear / $frequency;
+ }
+ $prev = self::couponFirstPeriodDate($settlement, $maturity, $frequency, false);
+ $next = self::couponFirstPeriodDate($settlement, $maturity, $frequency, true);
+
+ return $next - $prev;
+ default:
+ // US (NASD) 30/360, Actual/360 or European 30/360
+ return 360 / $frequency;
+ }
+ }
+
+ /**
+ * COUPDAYSNC.
+ *
+ * Returns the number of days from the settlement date to the next coupon date.
+ *
+ * Excel Function:
+ * COUPDAYSNC(settlement,maturity,frequency[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue
+ * date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param mixed $frequency the number of coupon payments per year.
+ * Valid frequency values are:
+ * 1 Annual
+ * 2 Semi-Annual
+ * 4 Quarterly
+ * If working in Gnumeric Mode, the following frequency options are
+ * also available
+ * 6 Bimonthly
+ * 12 Monthly
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float|string
+ */
+ public static function COUPDAYSNC($settlement, $maturity, $frequency, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $frequency = (int) Functions::flattenSingleValue($frequency);
+ $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
+
+ if (is_string($settlement = DateTime::getDateValue($settlement))) {
+ return Functions::VALUE();
+ }
+ if (is_string($maturity = DateTime::getDateValue($maturity))) {
+ return Functions::VALUE();
+ }
+
+ if (($settlement > $maturity) ||
+ (!self::isValidFrequency($frequency)) ||
+ (($basis < 0) || ($basis > 4))) {
+ return Functions::NAN();
+ }
+
+ $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
+ $next = self::couponFirstPeriodDate($settlement, $maturity, $frequency, true);
+
+ return DateTime::YEARFRAC($settlement, $next, $basis) * $daysPerYear;
+ }
+
+ /**
+ * COUPNCD.
+ *
+ * Returns the next coupon date after the settlement date.
+ *
+ * Excel Function:
+ * COUPNCD(settlement,maturity,frequency[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue
+ * date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param mixed $frequency the number of coupon payments per year.
+ * Valid frequency values are:
+ * 1 Annual
+ * 2 Semi-Annual
+ * 4 Quarterly
+ * If working in Gnumeric Mode, the following frequency options are
+ * also available
+ * 6 Bimonthly
+ * 12 Monthly
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function COUPNCD($settlement, $maturity, $frequency, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $frequency = (int) Functions::flattenSingleValue($frequency);
+ $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
+
+ if (is_string($settlement = DateTime::getDateValue($settlement))) {
+ return Functions::VALUE();
+ }
+ if (is_string($maturity = DateTime::getDateValue($maturity))) {
+ return Functions::VALUE();
+ }
+
+ if (($settlement > $maturity) ||
+ (!self::isValidFrequency($frequency)) ||
+ (($basis < 0) || ($basis > 4))) {
+ return Functions::NAN();
+ }
+
+ return self::couponFirstPeriodDate($settlement, $maturity, $frequency, true);
+ }
+
+ /**
+ * COUPNUM.
+ *
+ * Returns the number of coupons payable between the settlement date and maturity date,
+ * rounded up to the nearest whole coupon.
+ *
+ * Excel Function:
+ * COUPNUM(settlement,maturity,frequency[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue
+ * date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param mixed $frequency the number of coupon payments per year.
+ * Valid frequency values are:
+ * 1 Annual
+ * 2 Semi-Annual
+ * 4 Quarterly
+ * If working in Gnumeric Mode, the following frequency options are
+ * also available
+ * 6 Bimonthly
+ * 12 Monthly
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return int|string
+ */
+ public static function COUPNUM($settlement, $maturity, $frequency, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $frequency = (int) Functions::flattenSingleValue($frequency);
+ $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
+
+ if (is_string($settlement = DateTime::getDateValue($settlement))) {
+ return Functions::VALUE();
+ }
+ if (is_string($maturity = DateTime::getDateValue($maturity))) {
+ return Functions::VALUE();
+ }
+
+ if (($settlement > $maturity) ||
+ (!self::isValidFrequency($frequency)) ||
+ (($basis < 0) || ($basis > 4))) {
+ return Functions::NAN();
+ }
+
+ $settlement = self::couponFirstPeriodDate($settlement, $maturity, $frequency, true);
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis) * 365;
+
+ switch ($frequency) {
+ case 1: // annual payments
+ return ceil($daysBetweenSettlementAndMaturity / 360);
+ case 2: // half-yearly
+ return ceil($daysBetweenSettlementAndMaturity / 180);
+ case 4: // quarterly
+ return ceil($daysBetweenSettlementAndMaturity / 90);
+ case 6: // bimonthly
+ return ceil($daysBetweenSettlementAndMaturity / 60);
+ case 12: // monthly
+ return ceil($daysBetweenSettlementAndMaturity / 30);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * COUPPCD.
+ *
+ * Returns the previous coupon date before the settlement date.
+ *
+ * Excel Function:
+ * COUPPCD(settlement,maturity,frequency[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue
+ * date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param mixed $frequency the number of coupon payments per year.
+ * Valid frequency values are:
+ * 1 Annual
+ * 2 Semi-Annual
+ * 4 Quarterly
+ * If working in Gnumeric Mode, the following frequency options are
+ * also available
+ * 6 Bimonthly
+ * 12 Monthly
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
+ * depending on the value of the ReturnDateType flag
+ */
+ public static function COUPPCD($settlement, $maturity, $frequency, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $frequency = (int) Functions::flattenSingleValue($frequency);
+ $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
+
+ if (is_string($settlement = DateTime::getDateValue($settlement))) {
+ return Functions::VALUE();
+ }
+ if (is_string($maturity = DateTime::getDateValue($maturity))) {
+ return Functions::VALUE();
+ }
+
+ if (($settlement > $maturity) ||
+ (!self::isValidFrequency($frequency)) ||
+ (($basis < 0) || ($basis > 4))) {
+ return Functions::NAN();
+ }
+
+ return self::couponFirstPeriodDate($settlement, $maturity, $frequency, false);
+ }
+
+ /**
+ * CUMIPMT.
+ *
+ * Returns the cumulative interest paid on a loan between the start and end periods.
+ *
+ * Excel Function:
+ * CUMIPMT(rate,nper,pv,start,end[,type])
+ *
+ * @category Financial Functions
+ *
+ * @param float $rate The Interest rate
+ * @param int $nper The total number of payment periods
+ * @param float $pv Present Value
+ * @param int $start The first period in the calculation.
+ * Payment periods are numbered beginning with 1.
+ * @param int $end the last period in the calculation
+ * @param int $type A number 0 or 1 and indicates when payments are due:
+ * 0 or omitted At the end of the period.
+ * 1 At the beginning of the period.
+ *
+ * @return float|string
+ */
+ public static function CUMIPMT($rate, $nper, $pv, $start, $end, $type = 0)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ $nper = (int) Functions::flattenSingleValue($nper);
+ $pv = Functions::flattenSingleValue($pv);
+ $start = (int) Functions::flattenSingleValue($start);
+ $end = (int) Functions::flattenSingleValue($end);
+ $type = (int) Functions::flattenSingleValue($type);
+
+ // Validate parameters
+ if ($type != 0 && $type != 1) {
+ return Functions::NAN();
+ }
+ if ($start < 1 || $start > $end) {
+ return Functions::VALUE();
+ }
+
+ // Calculate
+ $interest = 0;
+ for ($per = $start; $per <= $end; ++$per) {
+ $interest += self::IPMT($rate, $per, $nper, $pv, 0, $type);
+ }
+
+ return $interest;
+ }
+
+ /**
+ * CUMPRINC.
+ *
+ * Returns the cumulative principal paid on a loan between the start and end periods.
+ *
+ * Excel Function:
+ * CUMPRINC(rate,nper,pv,start,end[,type])
+ *
+ * @category Financial Functions
+ *
+ * @param float $rate The Interest rate
+ * @param int $nper The total number of payment periods
+ * @param float $pv Present Value
+ * @param int $start The first period in the calculation.
+ * Payment periods are numbered beginning with 1.
+ * @param int $end the last period in the calculation
+ * @param int $type A number 0 or 1 and indicates when payments are due:
+ * 0 or omitted At the end of the period.
+ * 1 At the beginning of the period.
+ *
+ * @return float|string
+ */
+ public static function CUMPRINC($rate, $nper, $pv, $start, $end, $type = 0)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ $nper = (int) Functions::flattenSingleValue($nper);
+ $pv = Functions::flattenSingleValue($pv);
+ $start = (int) Functions::flattenSingleValue($start);
+ $end = (int) Functions::flattenSingleValue($end);
+ $type = (int) Functions::flattenSingleValue($type);
+
+ // Validate parameters
+ if ($type != 0 && $type != 1) {
+ return Functions::NAN();
+ }
+ if ($start < 1 || $start > $end) {
+ return Functions::VALUE();
+ }
+
+ // Calculate
+ $principal = 0;
+ for ($per = $start; $per <= $end; ++$per) {
+ $principal += self::PPMT($rate, $per, $nper, $pv, 0, $type);
+ }
+
+ return $principal;
+ }
+
+ /**
+ * DB.
+ *
+ * Returns the depreciation of an asset for a specified period using the
+ * fixed-declining balance method.
+ * This form of depreciation is used if you want to get a higher depreciation value
+ * at the beginning of the depreciation (as opposed to linear depreciation). The
+ * depreciation value is reduced with every depreciation period by the depreciation
+ * already deducted from the initial cost.
+ *
+ * Excel Function:
+ * DB(cost,salvage,life,period[,month])
+ *
+ * @category Financial Functions
+ *
+ * @param float $cost Initial cost of the asset
+ * @param float $salvage Value at the end of the depreciation.
+ * (Sometimes called the salvage value of the asset)
+ * @param int $life Number of periods over which the asset is depreciated.
+ * (Sometimes called the useful life of the asset)
+ * @param int $period The period for which you want to calculate the
+ * depreciation. Period must use the same units as life.
+ * @param int $month Number of months in the first year. If month is omitted,
+ * it defaults to 12.
+ *
+ * @return float|string
+ */
+ public static function DB($cost, $salvage, $life, $period, $month = 12)
+ {
+ $cost = Functions::flattenSingleValue($cost);
+ $salvage = Functions::flattenSingleValue($salvage);
+ $life = Functions::flattenSingleValue($life);
+ $period = Functions::flattenSingleValue($period);
+ $month = Functions::flattenSingleValue($month);
+
+ // Validate
+ if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period)) && (is_numeric($month))) {
+ $cost = (float) $cost;
+ $salvage = (float) $salvage;
+ $life = (int) $life;
+ $period = (int) $period;
+ $month = (int) $month;
+ if ($cost == 0) {
+ return 0.0;
+ } elseif (($cost < 0) || (($salvage / $cost) < 0) || ($life <= 0) || ($period < 1) || ($month < 1)) {
+ return Functions::NAN();
+ }
+ // Set Fixed Depreciation Rate
+ $fixedDepreciationRate = 1 - pow(($salvage / $cost), (1 / $life));
+ $fixedDepreciationRate = round($fixedDepreciationRate, 3);
+
+ // Loop through each period calculating the depreciation
+ $previousDepreciation = 0;
+ for ($per = 1; $per <= $period; ++$per) {
+ if ($per == 1) {
+ $depreciation = $cost * $fixedDepreciationRate * $month / 12;
+ } elseif ($per == ($life + 1)) {
+ $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate * (12 - $month) / 12;
+ } else {
+ $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate;
+ }
+ $previousDepreciation += $depreciation;
+ }
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
+ $depreciation = round($depreciation, 2);
+ }
+
+ return $depreciation;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * DDB.
+ *
+ * Returns the depreciation of an asset for a specified period using the
+ * double-declining balance method or some other method you specify.
+ *
+ * Excel Function:
+ * DDB(cost,salvage,life,period[,factor])
+ *
+ * @category Financial Functions
+ *
+ * @param float $cost Initial cost of the asset
+ * @param float $salvage Value at the end of the depreciation.
+ * (Sometimes called the salvage value of the asset)
+ * @param int $life Number of periods over which the asset is depreciated.
+ * (Sometimes called the useful life of the asset)
+ * @param int $period The period for which you want to calculate the
+ * depreciation. Period must use the same units as life.
+ * @param float $factor The rate at which the balance declines.
+ * If factor is omitted, it is assumed to be 2 (the
+ * double-declining balance method).
+ *
+ * @return float|string
+ */
+ public static function DDB($cost, $salvage, $life, $period, $factor = 2.0)
+ {
+ $cost = Functions::flattenSingleValue($cost);
+ $salvage = Functions::flattenSingleValue($salvage);
+ $life = Functions::flattenSingleValue($life);
+ $period = Functions::flattenSingleValue($period);
+ $factor = Functions::flattenSingleValue($factor);
+
+ // Validate
+ if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period)) && (is_numeric($factor))) {
+ $cost = (float) $cost;
+ $salvage = (float) $salvage;
+ $life = (int) $life;
+ $period = (int) $period;
+ $factor = (float) $factor;
+ if (($cost <= 0) || (($salvage / $cost) < 0) || ($life <= 0) || ($period < 1) || ($factor <= 0.0) || ($period > $life)) {
+ return Functions::NAN();
+ }
+ // Set Fixed Depreciation Rate
+ $fixedDepreciationRate = 1 - pow(($salvage / $cost), (1 / $life));
+ $fixedDepreciationRate = round($fixedDepreciationRate, 3);
+
+ // Loop through each period calculating the depreciation
+ $previousDepreciation = 0;
+ for ($per = 1; $per <= $period; ++$per) {
+ $depreciation = min(($cost - $previousDepreciation) * ($factor / $life), ($cost - $salvage - $previousDepreciation));
+ $previousDepreciation += $depreciation;
+ }
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
+ $depreciation = round($depreciation, 2);
+ }
+
+ return $depreciation;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * DISC.
+ *
+ * Returns the discount rate for a security.
+ *
+ * Excel Function:
+ * DISC(settlement,maturity,price,redemption[,basis])
+ *
+ * @category Financial Functions
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue
+ * date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param int $price The security's price per $100 face value
+ * @param int $redemption The security's redemption value per $100 face value
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float|string
+ */
+ public static function DISC($settlement, $maturity, $price, $redemption, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $price = Functions::flattenSingleValue($price);
+ $redemption = Functions::flattenSingleValue($redemption);
+ $basis = Functions::flattenSingleValue($basis);
+
+ // Validate
+ if ((is_numeric($price)) && (is_numeric($redemption)) && (is_numeric($basis))) {
+ $price = (float) $price;
+ $redemption = (float) $redemption;
+ $basis = (int) $basis;
+ if (($price <= 0) || ($redemption <= 0)) {
+ return Functions::NAN();
+ }
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
+ if (!is_numeric($daysBetweenSettlementAndMaturity)) {
+ // return date error
+ return $daysBetweenSettlementAndMaturity;
+ }
+
+ return (1 - $price / $redemption) / $daysBetweenSettlementAndMaturity;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * DOLLARDE.
+ *
+ * Converts a dollar price expressed as an integer part and a fraction
+ * part into a dollar price expressed as a decimal number.
+ * Fractional dollar numbers are sometimes used for security prices.
+ *
+ * Excel Function:
+ * DOLLARDE(fractional_dollar,fraction)
+ *
+ * @category Financial Functions
+ *
+ * @param float $fractional_dollar Fractional Dollar
+ * @param int $fraction Fraction
+ *
+ * @return float|string
+ */
+ public static function DOLLARDE($fractional_dollar = null, $fraction = 0)
+ {
+ $fractional_dollar = Functions::flattenSingleValue($fractional_dollar);
+ $fraction = (int) Functions::flattenSingleValue($fraction);
+
+ // Validate parameters
+ if ($fractional_dollar === null || $fraction < 0) {
+ return Functions::NAN();
+ }
+ if ($fraction == 0) {
+ return Functions::DIV0();
+ }
+
+ $dollars = floor($fractional_dollar);
+ $cents = fmod($fractional_dollar, 1);
+ $cents /= $fraction;
+ $cents *= pow(10, ceil(log10($fraction)));
+
+ return $dollars + $cents;
+ }
+
+ /**
+ * DOLLARFR.
+ *
+ * Converts a dollar price expressed as a decimal number into a dollar price
+ * expressed as a fraction.
+ * Fractional dollar numbers are sometimes used for security prices.
+ *
+ * Excel Function:
+ * DOLLARFR(decimal_dollar,fraction)
+ *
+ * @category Financial Functions
+ *
+ * @param float $decimal_dollar Decimal Dollar
+ * @param int $fraction Fraction
+ *
+ * @return float|string
+ */
+ public static function DOLLARFR($decimal_dollar = null, $fraction = 0)
+ {
+ $decimal_dollar = Functions::flattenSingleValue($decimal_dollar);
+ $fraction = (int) Functions::flattenSingleValue($fraction);
+
+ // Validate parameters
+ if ($decimal_dollar === null || $fraction < 0) {
+ return Functions::NAN();
+ }
+ if ($fraction == 0) {
+ return Functions::DIV0();
+ }
+
+ $dollars = floor($decimal_dollar);
+ $cents = fmod($decimal_dollar, 1);
+ $cents *= $fraction;
+ $cents *= pow(10, -ceil(log10($fraction)));
+
+ return $dollars + $cents;
+ }
+
+ /**
+ * EFFECT.
+ *
+ * Returns the effective interest rate given the nominal rate and the number of
+ * compounding payments per year.
+ *
+ * Excel Function:
+ * EFFECT(nominal_rate,npery)
+ *
+ * @category Financial Functions
+ *
+ * @param float $nominal_rate Nominal interest rate
+ * @param int $npery Number of compounding payments per year
+ *
+ * @return float|string
+ */
+ public static function EFFECT($nominal_rate = 0, $npery = 0)
+ {
+ $nominal_rate = Functions::flattenSingleValue($nominal_rate);
+ $npery = (int) Functions::flattenSingleValue($npery);
+
+ // Validate parameters
+ if ($nominal_rate <= 0 || $npery < 1) {
+ return Functions::NAN();
+ }
+
+ return pow((1 + $nominal_rate / $npery), $npery) - 1;
+ }
+
+ /**
+ * FV.
+ *
+ * Returns the Future Value of a cash flow with constant payments and interest rate (annuities).
+ *
+ * Excel Function:
+ * FV(rate,nper,pmt[,pv[,type]])
+ *
+ * @category Financial Functions
+ *
+ * @param float $rate The interest rate per period
+ * @param int $nper Total number of payment periods in an annuity
+ * @param float $pmt The payment made each period: it cannot change over the
+ * life of the annuity. Typically, pmt contains principal
+ * and interest but no other fees or taxes.
+ * @param float $pv present Value, or the lump-sum amount that a series of
+ * future payments is worth right now
+ * @param int $type A number 0 or 1 and indicates when payments are due:
+ * 0 or omitted At the end of the period.
+ * 1 At the beginning of the period.
+ *
+ * @return float|string
+ */
+ public static function FV($rate = 0, $nper = 0, $pmt = 0, $pv = 0, $type = 0)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ $nper = Functions::flattenSingleValue($nper);
+ $pmt = Functions::flattenSingleValue($pmt);
+ $pv = Functions::flattenSingleValue($pv);
+ $type = Functions::flattenSingleValue($type);
+
+ // Validate parameters
+ if ($type != 0 && $type != 1) {
+ return Functions::NAN();
+ }
+
+ // Calculate
+ if ($rate !== null && $rate != 0) {
+ return -$pv * pow(1 + $rate, $nper) - $pmt * (1 + $rate * $type) * (pow(1 + $rate, $nper) - 1) / $rate;
+ }
+
+ return -$pv - $pmt * $nper;
+ }
+
+ /**
+ * FVSCHEDULE.
+ *
+ * Returns the future value of an initial principal after applying a series of compound interest rates.
+ * Use FVSCHEDULE to calculate the future value of an investment with a variable or adjustable rate.
+ *
+ * Excel Function:
+ * FVSCHEDULE(principal,schedule)
+ *
+ * @param float $principal the present value
+ * @param float[] $schedule an array of interest rates to apply
+ *
+ * @return float
+ */
+ public static function FVSCHEDULE($principal, $schedule)
+ {
+ $principal = Functions::flattenSingleValue($principal);
+ $schedule = Functions::flattenArray($schedule);
+
+ foreach ($schedule as $rate) {
+ $principal *= 1 + $rate;
+ }
+
+ return $principal;
+ }
+
+ /**
+ * INTRATE.
+ *
+ * Returns the interest rate for a fully invested security.
+ *
+ * Excel Function:
+ * INTRATE(settlement,maturity,investment,redemption[,basis])
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param int $investment the amount invested in the security
+ * @param int $redemption the amount to be received at maturity
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float|string
+ */
+ public static function INTRATE($settlement, $maturity, $investment, $redemption, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $investment = Functions::flattenSingleValue($investment);
+ $redemption = Functions::flattenSingleValue($redemption);
+ $basis = Functions::flattenSingleValue($basis);
+
+ // Validate
+ if ((is_numeric($investment)) && (is_numeric($redemption)) && (is_numeric($basis))) {
+ $investment = (float) $investment;
+ $redemption = (float) $redemption;
+ $basis = (int) $basis;
+ if (($investment <= 0) || ($redemption <= 0)) {
+ return Functions::NAN();
+ }
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
+ if (!is_numeric($daysBetweenSettlementAndMaturity)) {
+ // return date error
+ return $daysBetweenSettlementAndMaturity;
+ }
+
+ return (($redemption / $investment) - 1) / ($daysBetweenSettlementAndMaturity);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * IPMT.
+ *
+ * Returns the interest payment for a given period for an investment based on periodic, constant payments and a constant interest rate.
+ *
+ * Excel Function:
+ * IPMT(rate,per,nper,pv[,fv][,type])
+ *
+ * @param float $rate Interest rate per period
+ * @param int $per Period for which we want to find the interest
+ * @param int $nper Number of periods
+ * @param float $pv Present Value
+ * @param float $fv Future Value
+ * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
+ *
+ * @return float|string
+ */
+ public static function IPMT($rate, $per, $nper, $pv, $fv = 0, $type = 0)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ $per = (int) Functions::flattenSingleValue($per);
+ $nper = (int) Functions::flattenSingleValue($nper);
+ $pv = Functions::flattenSingleValue($pv);
+ $fv = Functions::flattenSingleValue($fv);
+ $type = (int) Functions::flattenSingleValue($type);
+
+ // Validate parameters
+ if ($type != 0 && $type != 1) {
+ return Functions::NAN();
+ }
+ if ($per <= 0 || $per > $nper) {
+ return Functions::VALUE();
+ }
+
+ // Calculate
+ $interestAndPrincipal = self::interestAndPrincipal($rate, $per, $nper, $pv, $fv, $type);
+
+ return $interestAndPrincipal[0];
+ }
+
+ /**
+ * IRR.
+ *
+ * Returns the internal rate of return for a series of cash flows represented by the numbers in values.
+ * These cash flows do not have to be even, as they would be for an annuity. However, the cash flows must occur
+ * at regular intervals, such as monthly or annually. The internal rate of return is the interest rate received
+ * for an investment consisting of payments (negative values) and income (positive values) that occur at regular
+ * periods.
+ *
+ * Excel Function:
+ * IRR(values[,guess])
+ *
+ * @param float[] $values An array or a reference to cells that contain numbers for which you want
+ * to calculate the internal rate of return.
+ * Values must contain at least one positive value and one negative value to
+ * calculate the internal rate of return.
+ * @param float $guess A number that you guess is close to the result of IRR
+ *
+ * @return float|string
+ */
+ public static function IRR($values, $guess = 0.1)
+ {
+ if (!is_array($values)) {
+ return Functions::VALUE();
+ }
+ $values = Functions::flattenArray($values);
+ $guess = Functions::flattenSingleValue($guess);
+
+ // create an initial range, with a root somewhere between 0 and guess
+ $x1 = 0.0;
+ $x2 = $guess;
+ $f1 = self::NPV($x1, $values);
+ $f2 = self::NPV($x2, $values);
+ for ($i = 0; $i < self::FINANCIAL_MAX_ITERATIONS; ++$i) {
+ if (($f1 * $f2) < 0.0) {
+ break;
+ }
+ if (abs($f1) < abs($f2)) {
+ $f1 = self::NPV($x1 += 1.6 * ($x1 - $x2), $values);
+ } else {
+ $f2 = self::NPV($x2 += 1.6 * ($x2 - $x1), $values);
+ }
+ }
+ if (($f1 * $f2) > 0.0) {
+ return Functions::VALUE();
+ }
+
+ $f = self::NPV($x1, $values);
+ if ($f < 0.0) {
+ $rtb = $x1;
+ $dx = $x2 - $x1;
+ } else {
+ $rtb = $x2;
+ $dx = $x1 - $x2;
+ }
+
+ for ($i = 0; $i < self::FINANCIAL_MAX_ITERATIONS; ++$i) {
+ $dx *= 0.5;
+ $x_mid = $rtb + $dx;
+ $f_mid = self::NPV($x_mid, $values);
+ if ($f_mid <= 0.0) {
+ $rtb = $x_mid;
+ }
+ if ((abs($f_mid) < self::FINANCIAL_PRECISION) || (abs($dx) < self::FINANCIAL_PRECISION)) {
+ return $x_mid;
+ }
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * ISPMT.
+ *
+ * Returns the interest payment for an investment based on an interest rate and a constant payment schedule.
+ *
+ * Excel Function:
+ * =ISPMT(interest_rate, period, number_payments, PV)
+ *
+ * interest_rate is the interest rate for the investment
+ *
+ * period is the period to calculate the interest rate. It must be betweeen 1 and number_payments.
+ *
+ * number_payments is the number of payments for the annuity
+ *
+ * PV is the loan amount or present value of the payments
+ */
+ public static function ISPMT(...$args)
+ {
+ // Return value
+ $returnValue = 0;
+
+ // Get the parameters
+ $aArgs = Functions::flattenArray($args);
+ $interestRate = array_shift($aArgs);
+ $period = array_shift($aArgs);
+ $numberPeriods = array_shift($aArgs);
+ $principleRemaining = array_shift($aArgs);
+
+ // Calculate
+ $principlePayment = ($principleRemaining * 1.0) / ($numberPeriods * 1.0);
+ for ($i = 0; $i <= $period; ++$i) {
+ $returnValue = $interestRate * $principleRemaining * -1;
+ $principleRemaining -= $principlePayment;
+ // principle needs to be 0 after the last payment, don't let floating point screw it up
+ if ($i == $numberPeriods) {
+ $returnValue = 0;
+ }
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * MIRR.
+ *
+ * Returns the modified internal rate of return for a series of periodic cash flows. MIRR considers both
+ * the cost of the investment and the interest received on reinvestment of cash.
+ *
+ * Excel Function:
+ * MIRR(values,finance_rate, reinvestment_rate)
+ *
+ * @param float[] $values An array or a reference to cells that contain a series of payments and
+ * income occurring at regular intervals.
+ * Payments are negative value, income is positive values.
+ * @param float $finance_rate The interest rate you pay on the money used in the cash flows
+ * @param float $reinvestment_rate The interest rate you receive on the cash flows as you reinvest them
+ *
+ * @return float|string
+ */
+ public static function MIRR($values, $finance_rate, $reinvestment_rate)
+ {
+ if (!is_array($values)) {
+ return Functions::VALUE();
+ }
+ $values = Functions::flattenArray($values);
+ $finance_rate = Functions::flattenSingleValue($finance_rate);
+ $reinvestment_rate = Functions::flattenSingleValue($reinvestment_rate);
+ $n = count($values);
+
+ $rr = 1.0 + $reinvestment_rate;
+ $fr = 1.0 + $finance_rate;
+
+ $npv_pos = $npv_neg = 0.0;
+ foreach ($values as $i => $v) {
+ if ($v >= 0) {
+ $npv_pos += $v / pow($rr, $i);
+ } else {
+ $npv_neg += $v / pow($fr, $i);
+ }
+ }
+
+ if (($npv_neg == 0) || ($npv_pos == 0) || ($reinvestment_rate <= -1)) {
+ return Functions::VALUE();
+ }
+
+ $mirr = pow((-$npv_pos * pow($rr, $n))
+ / ($npv_neg * ($rr)), (1.0 / ($n - 1))) - 1.0;
+
+ return is_finite($mirr) ? $mirr : Functions::VALUE();
+ }
+
+ /**
+ * NOMINAL.
+ *
+ * Returns the nominal interest rate given the effective rate and the number of compounding payments per year.
+ *
+ * @param float $effect_rate Effective interest rate
+ * @param int $npery Number of compounding payments per year
+ *
+ * @return float|string
+ */
+ public static function NOMINAL($effect_rate = 0, $npery = 0)
+ {
+ $effect_rate = Functions::flattenSingleValue($effect_rate);
+ $npery = (int) Functions::flattenSingleValue($npery);
+
+ // Validate parameters
+ if ($effect_rate <= 0 || $npery < 1) {
+ return Functions::NAN();
+ }
+
+ // Calculate
+ return $npery * (pow($effect_rate + 1, 1 / $npery) - 1);
+ }
+
+ /**
+ * NPER.
+ *
+ * Returns the number of periods for a cash flow with constant periodic payments (annuities), and interest rate.
+ *
+ * @param float $rate Interest rate per period
+ * @param int $pmt Periodic payment (annuity)
+ * @param float $pv Present Value
+ * @param float $fv Future Value
+ * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
+ *
+ * @return float|string
+ */
+ public static function NPER($rate = 0, $pmt = 0, $pv = 0, $fv = 0, $type = 0)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ $pmt = Functions::flattenSingleValue($pmt);
+ $pv = Functions::flattenSingleValue($pv);
+ $fv = Functions::flattenSingleValue($fv);
+ $type = Functions::flattenSingleValue($type);
+
+ // Validate parameters
+ if ($type != 0 && $type != 1) {
+ return Functions::NAN();
+ }
+
+ // Calculate
+ if ($rate !== null && $rate != 0) {
+ if ($pmt == 0 && $pv == 0) {
+ return Functions::NAN();
+ }
+
+ return log(($pmt * (1 + $rate * $type) / $rate - $fv) / ($pv + $pmt * (1 + $rate * $type) / $rate)) / log(1 + $rate);
+ }
+ if ($pmt == 0) {
+ return Functions::NAN();
+ }
+
+ return (-$pv - $fv) / $pmt;
+ }
+
+ /**
+ * NPV.
+ *
+ * Returns the Net Present Value of a cash flow series given a discount rate.
+ *
+ * @return float
+ */
+ public static function NPV(...$args)
+ {
+ // Return value
+ $returnValue = 0;
+
+ // Loop through arguments
+ $aArgs = Functions::flattenArray($args);
+
+ // Calculate
+ $rate = array_shift($aArgs);
+ $countArgs = count($aArgs);
+ for ($i = 1; $i <= $countArgs; ++$i) {
+ // Is it a numeric value?
+ if (is_numeric($aArgs[$i - 1])) {
+ $returnValue += $aArgs[$i - 1] / pow(1 + $rate, $i);
+ }
+ }
+
+ // Return
+ return $returnValue;
+ }
+
+ /**
+ * PDURATION.
+ *
+ * Calculates the number of periods required for an investment to reach a specified value.
+ *
+ * @param float $rate Interest rate per period
+ * @param float $pv Present Value
+ * @param float $fv Future Value
+ *
+ * @return float|string
+ */
+ public static function PDURATION($rate = 0, $pv = 0, $fv = 0)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ $pv = Functions::flattenSingleValue($pv);
+ $fv = Functions::flattenSingleValue($fv);
+
+ // Validate parameters
+ if (!is_numeric($rate) || !is_numeric($pv) || !is_numeric($fv)) {
+ return Functions::VALUE();
+ } elseif ($rate <= 0.0 || $pv <= 0.0 || $fv <= 0.0) {
+ return Functions::NAN();
+ }
+
+ return (log($fv) - log($pv)) / log(1 + $rate);
+ }
+
+ /**
+ * PMT.
+ *
+ * Returns the constant payment (annuity) for a cash flow with a constant interest rate.
+ *
+ * @param float $rate Interest rate per period
+ * @param int $nper Number of periods
+ * @param float $pv Present Value
+ * @param float $fv Future Value
+ * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
+ *
+ * @return float
+ */
+ public static function PMT($rate = 0, $nper = 0, $pv = 0, $fv = 0, $type = 0)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ $nper = Functions::flattenSingleValue($nper);
+ $pv = Functions::flattenSingleValue($pv);
+ $fv = Functions::flattenSingleValue($fv);
+ $type = Functions::flattenSingleValue($type);
+
+ // Validate parameters
+ if ($type != 0 && $type != 1) {
+ return Functions::NAN();
+ }
+
+ // Calculate
+ if ($rate !== null && $rate != 0) {
+ return (-$fv - $pv * pow(1 + $rate, $nper)) / (1 + $rate * $type) / ((pow(1 + $rate, $nper) - 1) / $rate);
+ }
+
+ return (-$pv - $fv) / $nper;
+ }
+
+ /**
+ * PPMT.
+ *
+ * Returns the interest payment for a given period for an investment based on periodic, constant payments and a constant interest rate.
+ *
+ * @param float $rate Interest rate per period
+ * @param int $per Period for which we want to find the interest
+ * @param int $nper Number of periods
+ * @param float $pv Present Value
+ * @param float $fv Future Value
+ * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
+ *
+ * @return float
+ */
+ public static function PPMT($rate, $per, $nper, $pv, $fv = 0, $type = 0)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ $per = (int) Functions::flattenSingleValue($per);
+ $nper = (int) Functions::flattenSingleValue($nper);
+ $pv = Functions::flattenSingleValue($pv);
+ $fv = Functions::flattenSingleValue($fv);
+ $type = (int) Functions::flattenSingleValue($type);
+
+ // Validate parameters
+ if ($type != 0 && $type != 1) {
+ return Functions::NAN();
+ }
+ if ($per <= 0 || $per > $nper) {
+ return Functions::VALUE();
+ }
+
+ // Calculate
+ $interestAndPrincipal = self::interestAndPrincipal($rate, $per, $nper, $pv, $fv, $type);
+
+ return $interestAndPrincipal[1];
+ }
+
+ public static function PRICE($settlement, $maturity, $rate, $yield, $redemption, $frequency, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $rate = (float) Functions::flattenSingleValue($rate);
+ $yield = (float) Functions::flattenSingleValue($yield);
+ $redemption = (float) Functions::flattenSingleValue($redemption);
+ $frequency = (int) Functions::flattenSingleValue($frequency);
+ $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
+
+ if (is_string($settlement = DateTime::getDateValue($settlement))) {
+ return Functions::VALUE();
+ }
+ if (is_string($maturity = DateTime::getDateValue($maturity))) {
+ return Functions::VALUE();
+ }
+
+ if (($settlement > $maturity) ||
+ (!self::isValidFrequency($frequency)) ||
+ (($basis < 0) || ($basis > 4))) {
+ return Functions::NAN();
+ }
+
+ $dsc = self::COUPDAYSNC($settlement, $maturity, $frequency, $basis);
+ $e = self::COUPDAYS($settlement, $maturity, $frequency, $basis);
+ $n = self::COUPNUM($settlement, $maturity, $frequency, $basis);
+ $a = self::COUPDAYBS($settlement, $maturity, $frequency, $basis);
+
+ $baseYF = 1.0 + ($yield / $frequency);
+ $rfp = 100 * ($rate / $frequency);
+ $de = $dsc / $e;
+
+ $result = $redemption / pow($baseYF, (--$n + $de));
+ for ($k = 0; $k <= $n; ++$k) {
+ $result += $rfp / (pow($baseYF, ($k + $de)));
+ }
+ $result -= $rfp * ($a / $e);
+
+ return $result;
+ }
+
+ /**
+ * PRICEDISC.
+ *
+ * Returns the price per $100 face value of a discounted security.
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param int $discount The security's discount rate
+ * @param int $redemption The security's redemption value per $100 face value
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float
+ */
+ public static function PRICEDISC($settlement, $maturity, $discount, $redemption, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $discount = (float) Functions::flattenSingleValue($discount);
+ $redemption = (float) Functions::flattenSingleValue($redemption);
+ $basis = (int) Functions::flattenSingleValue($basis);
+
+ // Validate
+ if ((is_numeric($discount)) && (is_numeric($redemption)) && (is_numeric($basis))) {
+ if (($discount <= 0) || ($redemption <= 0)) {
+ return Functions::NAN();
+ }
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
+ if (!is_numeric($daysBetweenSettlementAndMaturity)) {
+ // return date error
+ return $daysBetweenSettlementAndMaturity;
+ }
+
+ return $redemption * (1 - $discount * $daysBetweenSettlementAndMaturity);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * PRICEMAT.
+ *
+ * Returns the price per $100 face value of a security that pays interest at maturity.
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security's settlement date is the date after the issue date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param mixed $issue The security's issue date
+ * @param int $rate The security's interest rate at date of issue
+ * @param int $yield The security's annual yield
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float
+ */
+ public static function PRICEMAT($settlement, $maturity, $issue, $rate, $yield, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $issue = Functions::flattenSingleValue($issue);
+ $rate = Functions::flattenSingleValue($rate);
+ $yield = Functions::flattenSingleValue($yield);
+ $basis = (int) Functions::flattenSingleValue($basis);
+
+ // Validate
+ if (is_numeric($rate) && is_numeric($yield)) {
+ if (($rate <= 0) || ($yield <= 0)) {
+ return Functions::NAN();
+ }
+ $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
+ if (!is_numeric($daysPerYear)) {
+ return $daysPerYear;
+ }
+ $daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis);
+ if (!is_numeric($daysBetweenIssueAndSettlement)) {
+ // return date error
+ return $daysBetweenIssueAndSettlement;
+ }
+ $daysBetweenIssueAndSettlement *= $daysPerYear;
+ $daysBetweenIssueAndMaturity = DateTime::YEARFRAC($issue, $maturity, $basis);
+ if (!is_numeric($daysBetweenIssueAndMaturity)) {
+ // return date error
+ return $daysBetweenIssueAndMaturity;
+ }
+ $daysBetweenIssueAndMaturity *= $daysPerYear;
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
+ if (!is_numeric($daysBetweenSettlementAndMaturity)) {
+ // return date error
+ return $daysBetweenSettlementAndMaturity;
+ }
+ $daysBetweenSettlementAndMaturity *= $daysPerYear;
+
+ return (100 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate * 100)) /
+ (1 + (($daysBetweenSettlementAndMaturity / $daysPerYear) * $yield)) -
+ (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate * 100);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * PV.
+ *
+ * Returns the Present Value of a cash flow with constant payments and interest rate (annuities).
+ *
+ * @param float $rate Interest rate per period
+ * @param int $nper Number of periods
+ * @param float $pmt Periodic payment (annuity)
+ * @param float $fv Future Value
+ * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
+ *
+ * @return float
+ */
+ public static function PV($rate = 0, $nper = 0, $pmt = 0, $fv = 0, $type = 0)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ $nper = Functions::flattenSingleValue($nper);
+ $pmt = Functions::flattenSingleValue($pmt);
+ $fv = Functions::flattenSingleValue($fv);
+ $type = Functions::flattenSingleValue($type);
+
+ // Validate parameters
+ if ($type != 0 && $type != 1) {
+ return Functions::NAN();
+ }
+
+ // Calculate
+ if ($rate !== null && $rate != 0) {
+ return (-$pmt * (1 + $rate * $type) * ((pow(1 + $rate, $nper) - 1) / $rate) - $fv) / pow(1 + $rate, $nper);
+ }
+
+ return -$fv - $pmt * $nper;
+ }
+
+ /**
+ * RATE.
+ *
+ * Returns the interest rate per period of an annuity.
+ * RATE is calculated by iteration and can have zero or more solutions.
+ * If the successive results of RATE do not converge to within 0.0000001 after 20 iterations,
+ * RATE returns the #NUM! error value.
+ *
+ * Excel Function:
+ * RATE(nper,pmt,pv[,fv[,type[,guess]]])
+ *
+ * @category Financial Functions
+ *
+ * @param float $nper The total number of payment periods in an annuity
+ * @param float $pmt The payment made each period and cannot change over the life
+ * of the annuity.
+ * Typically, pmt includes principal and interest but no other
+ * fees or taxes.
+ * @param float $pv The present value - the total amount that a series of future
+ * payments is worth now
+ * @param float $fv The future value, or a cash balance you want to attain after
+ * the last payment is made. If fv is omitted, it is assumed
+ * to be 0 (the future value of a loan, for example, is 0).
+ * @param int $type A number 0 or 1 and indicates when payments are due:
+ * 0 or omitted At the end of the period.
+ * 1 At the beginning of the period.
+ * @param float $guess Your guess for what the rate will be.
+ * If you omit guess, it is assumed to be 10 percent.
+ *
+ * @return float
+ */
+ public static function RATE($nper, $pmt, $pv, $fv = 0.0, $type = 0, $guess = 0.1)
+ {
+ $nper = (int) Functions::flattenSingleValue($nper);
+ $pmt = Functions::flattenSingleValue($pmt);
+ $pv = Functions::flattenSingleValue($pv);
+ $fv = ($fv === null) ? 0.0 : Functions::flattenSingleValue($fv);
+ $type = ($type === null) ? 0 : (int) Functions::flattenSingleValue($type);
+ $guess = ($guess === null) ? 0.1 : Functions::flattenSingleValue($guess);
+
+ $rate = $guess;
+ if (abs($rate) < self::FINANCIAL_PRECISION) {
+ $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv;
+ } else {
+ $f = exp($nper * log(1 + $rate));
+ $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
+ }
+ $y0 = $pv + $pmt * $nper + $fv;
+ $y1 = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
+
+ // find root by secant method
+ $i = $x0 = 0.0;
+ $x1 = $rate;
+ while ((abs($y0 - $y1) > self::FINANCIAL_PRECISION) && ($i < self::FINANCIAL_MAX_ITERATIONS)) {
+ $rate = ($y1 * $x0 - $y0 * $x1) / ($y1 - $y0);
+ $x0 = $x1;
+ $x1 = $rate;
+ if (($nper * abs($pmt)) > ($pv - $fv)) {
+ $x1 = abs($x1);
+ }
+ if (abs($rate) < self::FINANCIAL_PRECISION) {
+ $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv;
+ } else {
+ $f = exp($nper * log(1 + $rate));
+ $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
+ }
+
+ $y0 = $y1;
+ $y1 = $y;
+ ++$i;
+ }
+
+ return $rate;
+ }
+
+ /**
+ * RECEIVED.
+ *
+ * Returns the price per $100 face value of a discounted security.
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security settlement date is the date after the issue date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param int $investment The amount invested in the security
+ * @param int $discount The security's discount rate
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float
+ */
+ public static function RECEIVED($settlement, $maturity, $investment, $discount, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $investment = (float) Functions::flattenSingleValue($investment);
+ $discount = (float) Functions::flattenSingleValue($discount);
+ $basis = (int) Functions::flattenSingleValue($basis);
+
+ // Validate
+ if ((is_numeric($investment)) && (is_numeric($discount)) && (is_numeric($basis))) {
+ if (($investment <= 0) || ($discount <= 0)) {
+ return Functions::NAN();
+ }
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
+ if (!is_numeric($daysBetweenSettlementAndMaturity)) {
+ // return date error
+ return $daysBetweenSettlementAndMaturity;
+ }
+
+ return $investment / (1 - ($discount * $daysBetweenSettlementAndMaturity));
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * RRI.
+ *
+ * Calculates the interest rate required for an investment to grow to a specified future value .
+ *
+ * @param float $nper The number of periods over which the investment is made
+ * @param float $pv Present Value
+ * @param float $fv Future Value
+ *
+ * @return float|string
+ */
+ public static function RRI($nper = 0, $pv = 0, $fv = 0)
+ {
+ $nper = Functions::flattenSingleValue($nper);
+ $pv = Functions::flattenSingleValue($pv);
+ $fv = Functions::flattenSingleValue($fv);
+
+ // Validate parameters
+ if (!is_numeric($nper) || !is_numeric($pv) || !is_numeric($fv)) {
+ return Functions::VALUE();
+ } elseif ($nper <= 0.0 || $pv <= 0.0 || $fv < 0.0) {
+ return Functions::NAN();
+ }
+
+ return pow($fv / $pv, 1 / $nper) - 1;
+ }
+
+ /**
+ * SLN.
+ *
+ * Returns the straight-line depreciation of an asset for one period
+ *
+ * @param mixed $cost Initial cost of the asset
+ * @param mixed $salvage Value at the end of the depreciation
+ * @param mixed $life Number of periods over which the asset is depreciated
+ *
+ * @return float|string
+ */
+ public static function SLN($cost, $salvage, $life)
+ {
+ $cost = Functions::flattenSingleValue($cost);
+ $salvage = Functions::flattenSingleValue($salvage);
+ $life = Functions::flattenSingleValue($life);
+
+ // Calculate
+ if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life))) {
+ if ($life < 0) {
+ return Functions::NAN();
+ }
+
+ return ($cost - $salvage) / $life;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * SYD.
+ *
+ * Returns the sum-of-years' digits depreciation of an asset for a specified period.
+ *
+ * @param mixed $cost Initial cost of the asset
+ * @param mixed $salvage Value at the end of the depreciation
+ * @param mixed $life Number of periods over which the asset is depreciated
+ * @param mixed $period Period
+ *
+ * @return float|string
+ */
+ public static function SYD($cost, $salvage, $life, $period)
+ {
+ $cost = Functions::flattenSingleValue($cost);
+ $salvage = Functions::flattenSingleValue($salvage);
+ $life = Functions::flattenSingleValue($life);
+ $period = Functions::flattenSingleValue($period);
+
+ // Calculate
+ if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period))) {
+ if (($life < 1) || ($period > $life)) {
+ return Functions::NAN();
+ }
+
+ return (($cost - $salvage) * ($life - $period + 1) * 2) / ($life * ($life + 1));
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * TBILLEQ.
+ *
+ * Returns the bond-equivalent yield for a Treasury bill.
+ *
+ * @param mixed $settlement The Treasury bill's settlement date.
+ * The Treasury bill's settlement date is the date after the issue date when the Treasury bill is traded to the buyer.
+ * @param mixed $maturity The Treasury bill's maturity date.
+ * The maturity date is the date when the Treasury bill expires.
+ * @param int $discount The Treasury bill's discount rate
+ *
+ * @return float
+ */
+ public static function TBILLEQ($settlement, $maturity, $discount)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $discount = Functions::flattenSingleValue($discount);
+
+ // Use TBILLPRICE for validation
+ $testValue = self::TBILLPRICE($settlement, $maturity, $discount);
+ if (is_string($testValue)) {
+ return $testValue;
+ }
+
+ if (is_string($maturity = DateTime::getDateValue($maturity))) {
+ return Functions::VALUE();
+ }
+
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ ++$maturity;
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity) * 360;
+ } else {
+ $daysBetweenSettlementAndMaturity = (DateTime::getDateValue($maturity) - DateTime::getDateValue($settlement));
+ }
+
+ return (365 * $discount) / (360 - $discount * $daysBetweenSettlementAndMaturity);
+ }
+
+ /**
+ * TBILLPRICE.
+ *
+ * Returns the yield for a Treasury bill.
+ *
+ * @param mixed $settlement The Treasury bill's settlement date.
+ * The Treasury bill's settlement date is the date after the issue date when the Treasury bill is traded to the buyer.
+ * @param mixed $maturity The Treasury bill's maturity date.
+ * The maturity date is the date when the Treasury bill expires.
+ * @param int $discount The Treasury bill's discount rate
+ *
+ * @return float
+ */
+ public static function TBILLPRICE($settlement, $maturity, $discount)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $discount = Functions::flattenSingleValue($discount);
+
+ if (is_string($maturity = DateTime::getDateValue($maturity))) {
+ return Functions::VALUE();
+ }
+
+ // Validate
+ if (is_numeric($discount)) {
+ if ($discount <= 0) {
+ return Functions::NAN();
+ }
+
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ ++$maturity;
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity) * 360;
+ if (!is_numeric($daysBetweenSettlementAndMaturity)) {
+ // return date error
+ return $daysBetweenSettlementAndMaturity;
+ }
+ } else {
+ $daysBetweenSettlementAndMaturity = (DateTime::getDateValue($maturity) - DateTime::getDateValue($settlement));
+ }
+
+ if ($daysBetweenSettlementAndMaturity > 360) {
+ return Functions::NAN();
+ }
+
+ $price = 100 * (1 - (($discount * $daysBetweenSettlementAndMaturity) / 360));
+ if ($price <= 0) {
+ return Functions::NAN();
+ }
+
+ return $price;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * TBILLYIELD.
+ *
+ * Returns the yield for a Treasury bill.
+ *
+ * @param mixed $settlement The Treasury bill's settlement date.
+ * The Treasury bill's settlement date is the date after the issue date when the Treasury bill is traded to the buyer.
+ * @param mixed $maturity The Treasury bill's maturity date.
+ * The maturity date is the date when the Treasury bill expires.
+ * @param int $price The Treasury bill's price per $100 face value
+ *
+ * @return float
+ */
+ public static function TBILLYIELD($settlement, $maturity, $price)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $price = Functions::flattenSingleValue($price);
+
+ // Validate
+ if (is_numeric($price)) {
+ if ($price <= 0) {
+ return Functions::NAN();
+ }
+
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
+ ++$maturity;
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity) * 360;
+ if (!is_numeric($daysBetweenSettlementAndMaturity)) {
+ // return date error
+ return $daysBetweenSettlementAndMaturity;
+ }
+ } else {
+ $daysBetweenSettlementAndMaturity = (DateTime::getDateValue($maturity) - DateTime::getDateValue($settlement));
+ }
+
+ if ($daysBetweenSettlementAndMaturity > 360) {
+ return Functions::NAN();
+ }
+
+ return ((100 - $price) / $price) * (360 / $daysBetweenSettlementAndMaturity);
+ }
+
+ return Functions::VALUE();
+ }
+
+ public static function XIRR($values, $dates, $guess = 0.1)
+ {
+ if ((!is_array($values)) && (!is_array($dates))) {
+ return Functions::VALUE();
+ }
+ $values = Functions::flattenArray($values);
+ $dates = Functions::flattenArray($dates);
+ $guess = Functions::flattenSingleValue($guess);
+ if (count($values) != count($dates)) {
+ return Functions::NAN();
+ }
+
+ // create an initial range, with a root somewhere between 0 and guess
+ $x1 = 0.0;
+ $x2 = $guess;
+ $f1 = self::XNPV($x1, $values, $dates);
+ $f2 = self::XNPV($x2, $values, $dates);
+ for ($i = 0; $i < self::FINANCIAL_MAX_ITERATIONS; ++$i) {
+ if (($f1 * $f2) < 0.0) {
+ break;
+ } elseif (abs($f1) < abs($f2)) {
+ $f1 = self::XNPV($x1 += 1.6 * ($x1 - $x2), $values, $dates);
+ } else {
+ $f2 = self::XNPV($x2 += 1.6 * ($x2 - $x1), $values, $dates);
+ }
+ }
+ if (($f1 * $f2) > 0.0) {
+ return Functions::VALUE();
+ }
+
+ $f = self::XNPV($x1, $values, $dates);
+ if ($f < 0.0) {
+ $rtb = $x1;
+ $dx = $x2 - $x1;
+ } else {
+ $rtb = $x2;
+ $dx = $x1 - $x2;
+ }
+
+ for ($i = 0; $i < self::FINANCIAL_MAX_ITERATIONS; ++$i) {
+ $dx *= 0.5;
+ $x_mid = $rtb + $dx;
+ $f_mid = self::XNPV($x_mid, $values, $dates);
+ if ($f_mid <= 0.0) {
+ $rtb = $x_mid;
+ }
+ if ((abs($f_mid) < self::FINANCIAL_PRECISION) || (abs($dx) < self::FINANCIAL_PRECISION)) {
+ return $x_mid;
+ }
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * XNPV.
+ *
+ * Returns the net present value for a schedule of cash flows that is not necessarily periodic.
+ * To calculate the net present value for a series of cash flows that is periodic, use the NPV function.
+ *
+ * Excel Function:
+ * =XNPV(rate,values,dates)
+ *
+ * @param float $rate the discount rate to apply to the cash flows
+ * @param array of float $values A series of cash flows that corresponds to a schedule of payments in dates.
+ * The first payment is optional and corresponds to a cost or payment that occurs at the beginning of the investment.
+ * If the first value is a cost or payment, it must be a negative value. All succeeding payments are discounted based on a 365-day year.
+ * The series of values must contain at least one positive value and one negative value.
+ * @param array of mixed $dates A schedule of payment dates that corresponds to the cash flow payments.
+ * The first payment date indicates the beginning of the schedule of payments.
+ * All other dates must be later than this date, but they may occur in any order.
+ *
+ * @return float
+ */
+ public static function XNPV($rate, $values, $dates)
+ {
+ $rate = Functions::flattenSingleValue($rate);
+ if (!is_numeric($rate)) {
+ return Functions::VALUE();
+ }
+ if ((!is_array($values)) || (!is_array($dates))) {
+ return Functions::VALUE();
+ }
+ $values = Functions::flattenArray($values);
+ $dates = Functions::flattenArray($dates);
+ $valCount = count($values);
+ if ($valCount != count($dates)) {
+ return Functions::NAN();
+ }
+ if ((min($values) > 0) || (max($values) < 0)) {
+ return Functions::VALUE();
+ }
+
+ $xnpv = 0.0;
+ for ($i = 0; $i < $valCount; ++$i) {
+ if (!is_numeric($values[$i])) {
+ return Functions::VALUE();
+ }
+ $xnpv += $values[$i] / pow(1 + $rate, DateTime::DATEDIF($dates[0], $dates[$i], 'd') / 365);
+ }
+
+ return (is_finite($xnpv)) ? $xnpv : Functions::VALUE();
+ }
+
+ /**
+ * YIELDDISC.
+ *
+ * Returns the annual yield of a security that pays interest at maturity.
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security's settlement date is the date after the issue date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param int $price The security's price per $100 face value
+ * @param int $redemption The security's redemption value per $100 face value
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float
+ */
+ public static function YIELDDISC($settlement, $maturity, $price, $redemption, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $price = Functions::flattenSingleValue($price);
+ $redemption = Functions::flattenSingleValue($redemption);
+ $basis = (int) Functions::flattenSingleValue($basis);
+
+ // Validate
+ if (is_numeric($price) && is_numeric($redemption)) {
+ if (($price <= 0) || ($redemption <= 0)) {
+ return Functions::NAN();
+ }
+ $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
+ if (!is_numeric($daysPerYear)) {
+ return $daysPerYear;
+ }
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
+ if (!is_numeric($daysBetweenSettlementAndMaturity)) {
+ // return date error
+ return $daysBetweenSettlementAndMaturity;
+ }
+ $daysBetweenSettlementAndMaturity *= $daysPerYear;
+
+ return (($redemption - $price) / $price) * ($daysPerYear / $daysBetweenSettlementAndMaturity);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * YIELDMAT.
+ *
+ * Returns the annual yield of a security that pays interest at maturity.
+ *
+ * @param mixed $settlement The security's settlement date.
+ * The security's settlement date is the date after the issue date when the security is traded to the buyer.
+ * @param mixed $maturity The security's maturity date.
+ * The maturity date is the date when the security expires.
+ * @param mixed $issue The security's issue date
+ * @param int $rate The security's interest rate at date of issue
+ * @param int $price The security's price per $100 face value
+ * @param int $basis The type of day count to use.
+ * 0 or omitted US (NASD) 30/360
+ * 1 Actual/actual
+ * 2 Actual/360
+ * 3 Actual/365
+ * 4 European 30/360
+ *
+ * @return float
+ */
+ public static function YIELDMAT($settlement, $maturity, $issue, $rate, $price, $basis = 0)
+ {
+ $settlement = Functions::flattenSingleValue($settlement);
+ $maturity = Functions::flattenSingleValue($maturity);
+ $issue = Functions::flattenSingleValue($issue);
+ $rate = Functions::flattenSingleValue($rate);
+ $price = Functions::flattenSingleValue($price);
+ $basis = (int) Functions::flattenSingleValue($basis);
+
+ // Validate
+ if (is_numeric($rate) && is_numeric($price)) {
+ if (($rate <= 0) || ($price <= 0)) {
+ return Functions::NAN();
+ }
+ $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
+ if (!is_numeric($daysPerYear)) {
+ return $daysPerYear;
+ }
+ $daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis);
+ if (!is_numeric($daysBetweenIssueAndSettlement)) {
+ // return date error
+ return $daysBetweenIssueAndSettlement;
+ }
+ $daysBetweenIssueAndSettlement *= $daysPerYear;
+ $daysBetweenIssueAndMaturity = DateTime::YEARFRAC($issue, $maturity, $basis);
+ if (!is_numeric($daysBetweenIssueAndMaturity)) {
+ // return date error
+ return $daysBetweenIssueAndMaturity;
+ }
+ $daysBetweenIssueAndMaturity *= $daysPerYear;
+ $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
+ if (!is_numeric($daysBetweenSettlementAndMaturity)) {
+ // return date error
+ return $daysBetweenSettlementAndMaturity;
+ }
+ $daysBetweenSettlementAndMaturity *= $daysPerYear;
+
+ return ((1 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate) - (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) /
+ (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) *
+ ($daysPerYear / $daysBetweenSettlementAndMaturity);
+ }
+
+ return Functions::VALUE();
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/FormulaParser.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/FormulaParser.php
new file mode 100644
index 00000000000..9b3c66e9e86
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/FormulaParser.php
@@ -0,0 +1,623 @@
+<';
+ const OPERATORS_POSTFIX = '%';
+
+ /**
+ * Formula.
+ *
+ * @var string
+ */
+ private $formula;
+
+ /**
+ * Tokens.
+ *
+ * @var FormulaToken[]
+ */
+ private $tokens = [];
+
+ /**
+ * Create a new FormulaParser.
+ *
+ * @param string $pFormula Formula to parse
+ *
+ * @throws Exception
+ */
+ public function __construct($pFormula = '')
+ {
+ // Check parameters
+ if ($pFormula === null) {
+ throw new Exception('Invalid parameter passed: formula');
+ }
+
+ // Initialise values
+ $this->formula = trim($pFormula);
+ // Parse!
+ $this->parseToTokens();
+ }
+
+ /**
+ * Get Formula.
+ *
+ * @return string
+ */
+ public function getFormula()
+ {
+ return $this->formula;
+ }
+
+ /**
+ * Get Token.
+ *
+ * @param int $pId Token id
+ *
+ * @throws Exception
+ *
+ * @return string
+ */
+ public function getToken($pId = 0)
+ {
+ if (isset($this->tokens[$pId])) {
+ return $this->tokens[$pId];
+ }
+
+ throw new Exception("Token with id $pId does not exist.");
+ }
+
+ /**
+ * Get Token count.
+ *
+ * @return int
+ */
+ public function getTokenCount()
+ {
+ return count($this->tokens);
+ }
+
+ /**
+ * Get Tokens.
+ *
+ * @return FormulaToken[]
+ */
+ public function getTokens()
+ {
+ return $this->tokens;
+ }
+
+ /**
+ * Parse to tokens.
+ */
+ private function parseToTokens()
+ {
+ // No attempt is made to verify formulas; assumes formulas are derived from Excel, where
+ // they can only exist if valid; stack overflows/underflows sunk as nulls without exceptions.
+
+ // Check if the formula has a valid starting =
+ $formulaLength = strlen($this->formula);
+ if ($formulaLength < 2 || $this->formula[0] != '=') {
+ return;
+ }
+
+ // Helper variables
+ $tokens1 = $tokens2 = $stack = [];
+ $inString = $inPath = $inRange = $inError = false;
+ $token = $previousToken = $nextToken = null;
+
+ $index = 1;
+ $value = '';
+
+ $ERRORS = ['#NULL!', '#DIV/0!', '#VALUE!', '#REF!', '#NAME?', '#NUM!', '#N/A'];
+ $COMPARATORS_MULTI = ['>=', '<=', '<>'];
+
+ while ($index < $formulaLength) {
+ // state-dependent character evaluation (order is important)
+
+ // double-quoted strings
+ // embeds are doubled
+ // end marks token
+ if ($inString) {
+ if ($this->formula[$index] == self::QUOTE_DOUBLE) {
+ if ((($index + 2) <= $formulaLength) && ($this->formula[$index + 1] == self::QUOTE_DOUBLE)) {
+ $value .= self::QUOTE_DOUBLE;
+ ++$index;
+ } else {
+ $inString = false;
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND, FormulaToken::TOKEN_SUBTYPE_TEXT);
+ $value = '';
+ }
+ } else {
+ $value .= $this->formula[$index];
+ }
+ ++$index;
+
+ continue;
+ }
+
+ // single-quoted strings (links)
+ // embeds are double
+ // end does not mark a token
+ if ($inPath) {
+ if ($this->formula[$index] == self::QUOTE_SINGLE) {
+ if ((($index + 2) <= $formulaLength) && ($this->formula[$index + 1] == self::QUOTE_SINGLE)) {
+ $value .= self::QUOTE_SINGLE;
+ ++$index;
+ } else {
+ $inPath = false;
+ }
+ } else {
+ $value .= $this->formula[$index];
+ }
+ ++$index;
+
+ continue;
+ }
+
+ // bracked strings (R1C1 range index or linked workbook name)
+ // no embeds (changed to "()" by Excel)
+ // end does not mark a token
+ if ($inRange) {
+ if ($this->formula[$index] == self::BRACKET_CLOSE) {
+ $inRange = false;
+ }
+ $value .= $this->formula[$index];
+ ++$index;
+
+ continue;
+ }
+
+ // error values
+ // end marks a token, determined from absolute list of values
+ if ($inError) {
+ $value .= $this->formula[$index];
+ ++$index;
+ if (in_array($value, $ERRORS)) {
+ $inError = false;
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND, FormulaToken::TOKEN_SUBTYPE_ERROR);
+ $value = '';
+ }
+
+ continue;
+ }
+
+ // scientific notation check
+ if (strpos(self::OPERATORS_SN, $this->formula[$index]) !== false) {
+ if (strlen($value) > 1) {
+ if (preg_match('/^[1-9]{1}(\\.\\d+)?E{1}$/', $this->formula[$index]) != 0) {
+ $value .= $this->formula[$index];
+ ++$index;
+
+ continue;
+ }
+ }
+ }
+
+ // independent character evaluation (order not important)
+
+ // establish state-dependent character evaluations
+ if ($this->formula[$index] == self::QUOTE_DOUBLE) {
+ if (strlen($value) > 0) {
+ // unexpected
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_UNKNOWN);
+ $value = '';
+ }
+ $inString = true;
+ ++$index;
+
+ continue;
+ }
+
+ if ($this->formula[$index] == self::QUOTE_SINGLE) {
+ if (strlen($value) > 0) {
+ // unexpected
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_UNKNOWN);
+ $value = '';
+ }
+ $inPath = true;
+ ++$index;
+
+ continue;
+ }
+
+ if ($this->formula[$index] == self::BRACKET_OPEN) {
+ $inRange = true;
+ $value .= self::BRACKET_OPEN;
+ ++$index;
+
+ continue;
+ }
+
+ if ($this->formula[$index] == self::ERROR_START) {
+ if (strlen($value) > 0) {
+ // unexpected
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_UNKNOWN);
+ $value = '';
+ }
+ $inError = true;
+ $value .= self::ERROR_START;
+ ++$index;
+
+ continue;
+ }
+
+ // mark start and end of arrays and array rows
+ if ($this->formula[$index] == self::BRACE_OPEN) {
+ if (strlen($value) > 0) {
+ // unexpected
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_UNKNOWN);
+ $value = '';
+ }
+
+ $tmp = new FormulaToken('ARRAY', FormulaToken::TOKEN_TYPE_FUNCTION, FormulaToken::TOKEN_SUBTYPE_START);
+ $tokens1[] = $tmp;
+ $stack[] = clone $tmp;
+
+ $tmp = new FormulaToken('ARRAYROW', FormulaToken::TOKEN_TYPE_FUNCTION, FormulaToken::TOKEN_SUBTYPE_START);
+ $tokens1[] = $tmp;
+ $stack[] = clone $tmp;
+
+ ++$index;
+
+ continue;
+ }
+
+ if ($this->formula[$index] == self::SEMICOLON) {
+ if (strlen($value) > 0) {
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND);
+ $value = '';
+ }
+
+ $tmp = array_pop($stack);
+ $tmp->setValue('');
+ $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP);
+ $tokens1[] = $tmp;
+
+ $tmp = new FormulaToken(',', FormulaToken::TOKEN_TYPE_ARGUMENT);
+ $tokens1[] = $tmp;
+
+ $tmp = new FormulaToken('ARRAYROW', FormulaToken::TOKEN_TYPE_FUNCTION, FormulaToken::TOKEN_SUBTYPE_START);
+ $tokens1[] = $tmp;
+ $stack[] = clone $tmp;
+
+ ++$index;
+
+ continue;
+ }
+
+ if ($this->formula[$index] == self::BRACE_CLOSE) {
+ if (strlen($value) > 0) {
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND);
+ $value = '';
+ }
+
+ $tmp = array_pop($stack);
+ $tmp->setValue('');
+ $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP);
+ $tokens1[] = $tmp;
+
+ $tmp = array_pop($stack);
+ $tmp->setValue('');
+ $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP);
+ $tokens1[] = $tmp;
+
+ ++$index;
+
+ continue;
+ }
+
+ // trim white-space
+ if ($this->formula[$index] == self::WHITESPACE) {
+ if (strlen($value) > 0) {
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND);
+ $value = '';
+ }
+ $tokens1[] = new FormulaToken('', FormulaToken::TOKEN_TYPE_WHITESPACE);
+ ++$index;
+ while (($this->formula[$index] == self::WHITESPACE) && ($index < $formulaLength)) {
+ ++$index;
+ }
+
+ continue;
+ }
+
+ // multi-character comparators
+ if (($index + 2) <= $formulaLength) {
+ if (in_array(substr($this->formula, $index, 2), $COMPARATORS_MULTI)) {
+ if (strlen($value) > 0) {
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND);
+ $value = '';
+ }
+ $tokens1[] = new FormulaToken(substr($this->formula, $index, 2), FormulaToken::TOKEN_TYPE_OPERATORINFIX, FormulaToken::TOKEN_SUBTYPE_LOGICAL);
+ $index += 2;
+
+ continue;
+ }
+ }
+
+ // standard infix operators
+ if (strpos(self::OPERATORS_INFIX, $this->formula[$index]) !== false) {
+ if (strlen($value) > 0) {
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND);
+ $value = '';
+ }
+ $tokens1[] = new FormulaToken($this->formula[$index], FormulaToken::TOKEN_TYPE_OPERATORINFIX);
+ ++$index;
+
+ continue;
+ }
+
+ // standard postfix operators (only one)
+ if (strpos(self::OPERATORS_POSTFIX, $this->formula[$index]) !== false) {
+ if (strlen($value) > 0) {
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND);
+ $value = '';
+ }
+ $tokens1[] = new FormulaToken($this->formula[$index], FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX);
+ ++$index;
+
+ continue;
+ }
+
+ // start subexpression or function
+ if ($this->formula[$index] == self::PAREN_OPEN) {
+ if (strlen($value) > 0) {
+ $tmp = new FormulaToken($value, FormulaToken::TOKEN_TYPE_FUNCTION, FormulaToken::TOKEN_SUBTYPE_START);
+ $tokens1[] = $tmp;
+ $stack[] = clone $tmp;
+ $value = '';
+ } else {
+ $tmp = new FormulaToken('', FormulaToken::TOKEN_TYPE_SUBEXPRESSION, FormulaToken::TOKEN_SUBTYPE_START);
+ $tokens1[] = $tmp;
+ $stack[] = clone $tmp;
+ }
+ ++$index;
+
+ continue;
+ }
+
+ // function, subexpression, or array parameters, or operand unions
+ if ($this->formula[$index] == self::COMMA) {
+ if (strlen($value) > 0) {
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND);
+ $value = '';
+ }
+
+ $tmp = array_pop($stack);
+ $tmp->setValue('');
+ $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP);
+ $stack[] = $tmp;
+
+ if ($tmp->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) {
+ $tokens1[] = new FormulaToken(',', FormulaToken::TOKEN_TYPE_OPERATORINFIX, FormulaToken::TOKEN_SUBTYPE_UNION);
+ } else {
+ $tokens1[] = new FormulaToken(',', FormulaToken::TOKEN_TYPE_ARGUMENT);
+ }
+ ++$index;
+
+ continue;
+ }
+
+ // stop subexpression
+ if ($this->formula[$index] == self::PAREN_CLOSE) {
+ if (strlen($value) > 0) {
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND);
+ $value = '';
+ }
+
+ $tmp = array_pop($stack);
+ $tmp->setValue('');
+ $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP);
+ $tokens1[] = $tmp;
+
+ ++$index;
+
+ continue;
+ }
+
+ // token accumulation
+ $value .= $this->formula[$index];
+ ++$index;
+ }
+
+ // dump remaining accumulation
+ if (strlen($value) > 0) {
+ $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND);
+ }
+
+ // move tokenList to new set, excluding unnecessary white-space tokens and converting necessary ones to intersections
+ $tokenCount = count($tokens1);
+ for ($i = 0; $i < $tokenCount; ++$i) {
+ $token = $tokens1[$i];
+ if (isset($tokens1[$i - 1])) {
+ $previousToken = $tokens1[$i - 1];
+ } else {
+ $previousToken = null;
+ }
+ if (isset($tokens1[$i + 1])) {
+ $nextToken = $tokens1[$i + 1];
+ } else {
+ $nextToken = null;
+ }
+
+ if ($token === null) {
+ continue;
+ }
+
+ if ($token->getTokenType() != FormulaToken::TOKEN_TYPE_WHITESPACE) {
+ $tokens2[] = $token;
+
+ continue;
+ }
+
+ if ($previousToken === null) {
+ continue;
+ }
+
+ if (!(
+ (($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) && ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) ||
+ (($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) ||
+ ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND)
+ )) {
+ continue;
+ }
+
+ if ($nextToken === null) {
+ continue;
+ }
+
+ if (!(
+ (($nextToken->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) && ($nextToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_START)) ||
+ (($nextToken->getTokenType() == FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($nextToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_START)) ||
+ ($nextToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND)
+ )) {
+ continue;
+ }
+
+ $tokens2[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERATORINFIX, FormulaToken::TOKEN_SUBTYPE_INTERSECTION);
+ }
+
+ // move tokens to final list, switching infix "-" operators to prefix when appropriate, switching infix "+" operators
+ // to noop when appropriate, identifying operand and infix-operator subtypes, and pulling "@" from function names
+ $this->tokens = [];
+
+ $tokenCount = count($tokens2);
+ for ($i = 0; $i < $tokenCount; ++$i) {
+ $token = $tokens2[$i];
+ if (isset($tokens2[$i - 1])) {
+ $previousToken = $tokens2[$i - 1];
+ } else {
+ $previousToken = null;
+ }
+ if (isset($tokens2[$i + 1])) {
+ $nextToken = $tokens2[$i + 1];
+ } else {
+ $nextToken = null;
+ }
+
+ if ($token === null) {
+ continue;
+ }
+
+ if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getValue() == '-') {
+ if ($i == 0) {
+ $token->setTokenType(FormulaToken::TOKEN_TYPE_OPERATORPREFIX);
+ } elseif ((($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) &&
+ ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) ||
+ (($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_SUBEXPRESSION) &&
+ ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) ||
+ ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX) ||
+ ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND)) {
+ $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_MATH);
+ } else {
+ $token->setTokenType(FormulaToken::TOKEN_TYPE_OPERATORPREFIX);
+ }
+
+ $this->tokens[] = $token;
+
+ continue;
+ }
+
+ if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getValue() == '+') {
+ if ($i == 0) {
+ continue;
+ } elseif ((($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) &&
+ ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) ||
+ (($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_SUBEXPRESSION) &&
+ ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) ||
+ ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX) ||
+ ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND)) {
+ $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_MATH);
+ } else {
+ continue;
+ }
+
+ $this->tokens[] = $token;
+
+ continue;
+ }
+
+ if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORINFIX &&
+ $token->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_NOTHING) {
+ if (strpos('<>=', substr($token->getValue(), 0, 1)) !== false) {
+ $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_LOGICAL);
+ } elseif ($token->getValue() == '&') {
+ $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_CONCATENATION);
+ } else {
+ $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_MATH);
+ }
+
+ $this->tokens[] = $token;
+
+ continue;
+ }
+
+ if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND &&
+ $token->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_NOTHING) {
+ if (!is_numeric($token->getValue())) {
+ if (strtoupper($token->getValue()) == 'TRUE' || strtoupper($token->getValue()) == 'FALSE') {
+ $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_LOGICAL);
+ } else {
+ $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_RANGE);
+ }
+ } else {
+ $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_NUMBER);
+ }
+
+ $this->tokens[] = $token;
+
+ continue;
+ }
+
+ if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) {
+ if (strlen($token->getValue()) > 0) {
+ if (substr($token->getValue(), 0, 1) == '@') {
+ $token->setValue(substr($token->getValue(), 1));
+ }
+ }
+ }
+
+ $this->tokens[] = $token;
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/FormulaToken.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/FormulaToken.php
new file mode 100644
index 00000000000..66618d4a0c0
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/FormulaToken.php
@@ -0,0 +1,150 @@
+value = $pValue;
+ $this->tokenType = $pTokenType;
+ $this->tokenSubType = $pTokenSubType;
+ }
+
+ /**
+ * Get Value.
+ *
+ * @return string
+ */
+ public function getValue()
+ {
+ return $this->value;
+ }
+
+ /**
+ * Set Value.
+ *
+ * @param string $value
+ */
+ public function setValue($value)
+ {
+ $this->value = $value;
+ }
+
+ /**
+ * Get Token Type (represented by TOKEN_TYPE_*).
+ *
+ * @return string
+ */
+ public function getTokenType()
+ {
+ return $this->tokenType;
+ }
+
+ /**
+ * Set Token Type (represented by TOKEN_TYPE_*).
+ *
+ * @param string $value
+ */
+ public function setTokenType($value)
+ {
+ $this->tokenType = $value;
+ }
+
+ /**
+ * Get Token SubType (represented by TOKEN_SUBTYPE_*).
+ *
+ * @return string
+ */
+ public function getTokenSubType()
+ {
+ return $this->tokenSubType;
+ }
+
+ /**
+ * Set Token SubType (represented by TOKEN_SUBTYPE_*).
+ *
+ * @param string $value
+ */
+ public function setTokenSubType($value)
+ {
+ $this->tokenSubType = $value;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Functions.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Functions.php
new file mode 100644
index 00000000000..0a607c702ab
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Functions.php
@@ -0,0 +1,673 @@
+ '#NULL!',
+ 'divisionbyzero' => '#DIV/0!',
+ 'value' => '#VALUE!',
+ 'reference' => '#REF!',
+ 'name' => '#NAME?',
+ 'num' => '#NUM!',
+ 'na' => '#N/A',
+ 'gettingdata' => '#GETTING_DATA',
+ ];
+
+ /**
+ * Set the Compatibility Mode.
+ *
+ * @category Function Configuration
+ *
+ * @param string $compatibilityMode Compatibility Mode
+ * Permitted values are:
+ * Functions::COMPATIBILITY_EXCEL 'Excel'
+ * Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
+ * Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
+ *
+ * @return bool (Success or Failure)
+ */
+ public static function setCompatibilityMode($compatibilityMode)
+ {
+ if (($compatibilityMode == self::COMPATIBILITY_EXCEL) ||
+ ($compatibilityMode == self::COMPATIBILITY_GNUMERIC) ||
+ ($compatibilityMode == self::COMPATIBILITY_OPENOFFICE)
+ ) {
+ self::$compatibilityMode = $compatibilityMode;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return the current Compatibility Mode.
+ *
+ * @category Function Configuration
+ *
+ * @return string Compatibility Mode
+ * Possible Return values are:
+ * Functions::COMPATIBILITY_EXCEL 'Excel'
+ * Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
+ * Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
+ */
+ public static function getCompatibilityMode()
+ {
+ return self::$compatibilityMode;
+ }
+
+ /**
+ * 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
+ * Permitted values are:
+ * Functions::RETURNDATE_PHP_NUMERIC 'P'
+ * Functions::RETURNDATE_PHP_OBJECT 'O'
+ * Functions::RETURNDATE_EXCEL 'E'
+ *
+ * @return bool Success or failure
+ */
+ public static function setReturnDateType($returnDateType)
+ {
+ if (($returnDateType == self::RETURNDATE_PHP_NUMERIC) ||
+ ($returnDateType == self::RETURNDATE_PHP_OBJECT) ||
+ ($returnDateType == self::RETURNDATE_EXCEL)
+ ) {
+ self::$returnDateType = $returnDateType;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * 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:
+ * Functions::RETURNDATE_PHP_NUMERIC 'P'
+ * Functions::RETURNDATE_PHP_OBJECT 'O'
+ * Functions::RETURNDATE_EXCEL 'E'
+ */
+ public static function getReturnDateType()
+ {
+ return self::$returnDateType;
+ }
+
+ /**
+ * DUMMY.
+ *
+ * @category Error Returns
+ *
+ * @return string #Not Yet Implemented
+ */
+ public static function DUMMY()
+ {
+ return '#Not Yet Implemented';
+ }
+
+ /**
+ * DIV0.
+ *
+ * @category Error Returns
+ *
+ * @return string #Not Yet Implemented
+ */
+ public static function DIV0()
+ {
+ return self::$errorCodes['divisionbyzero'];
+ }
+
+ /**
+ * NA.
+ *
+ * Excel Function:
+ * =NA()
+ *
+ * Returns the error value #N/A
+ * #N/A is the error value that means "no value is available."
+ *
+ * @category Logical Functions
+ *
+ * @return string #N/A!
+ */
+ public static function NA()
+ {
+ return self::$errorCodes['na'];
+ }
+
+ /**
+ * NaN.
+ *
+ * Returns the error value #NUM!
+ *
+ * @category Error Returns
+ *
+ * @return string #NUM!
+ */
+ public static function NAN()
+ {
+ return self::$errorCodes['num'];
+ }
+
+ /**
+ * NAME.
+ *
+ * Returns the error value #NAME?
+ *
+ * @category Error Returns
+ *
+ * @return string #NAME?
+ */
+ public static function NAME()
+ {
+ return self::$errorCodes['name'];
+ }
+
+ /**
+ * REF.
+ *
+ * Returns the error value #REF!
+ *
+ * @category Error Returns
+ *
+ * @return string #REF!
+ */
+ public static function REF()
+ {
+ return self::$errorCodes['reference'];
+ }
+
+ /**
+ * NULL.
+ *
+ * Returns the error value #NULL!
+ *
+ * @category Error Returns
+ *
+ * @return string #NULL!
+ */
+ public static function null()
+ {
+ return self::$errorCodes['null'];
+ }
+
+ /**
+ * VALUE.
+ *
+ * Returns the error value #VALUE!
+ *
+ * @category Error Returns
+ *
+ * @return string #VALUE!
+ */
+ public static function VALUE()
+ {
+ return self::$errorCodes['value'];
+ }
+
+ public static function isMatrixValue($idx)
+ {
+ return (substr_count($idx, '.') <= 1) || (preg_match('/\.[A-Z]/', $idx) > 0);
+ }
+
+ public static function isValue($idx)
+ {
+ return substr_count($idx, '.') == 0;
+ }
+
+ public static function isCellValue($idx)
+ {
+ return substr_count($idx, '.') > 1;
+ }
+
+ public static function ifCondition($condition)
+ {
+ $condition = self::flattenSingleValue($condition);
+ if (!isset($condition[0]) && !is_numeric($condition)) {
+ $condition = '=""';
+ }
+ if (!in_array($condition[0], ['>', '<', '='])) {
+ if (!is_numeric($condition)) {
+ $condition = Calculation::wrapResult(strtoupper($condition));
+ }
+
+ return '=' . $condition;
+ }
+ preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches);
+ list(, $operator, $operand) = $matches;
+
+ if (!is_numeric($operand)) {
+ $operand = str_replace('"', '""', $operand);
+ $operand = Calculation::wrapResult(strtoupper($operand));
+ }
+
+ return $operator . $operand;
+ }
+
+ /**
+ * ERROR_TYPE.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool
+ */
+ public static function errorType($value = '')
+ {
+ $value = self::flattenSingleValue($value);
+
+ $i = 1;
+ foreach (self::$errorCodes as $errorCode) {
+ if ($value === $errorCode) {
+ return $i;
+ }
+ ++$i;
+ }
+
+ return self::NA();
+ }
+
+ /**
+ * IS_BLANK.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool
+ */
+ public static function isBlank($value = null)
+ {
+ if ($value !== null) {
+ $value = self::flattenSingleValue($value);
+ }
+
+ return $value === null;
+ }
+
+ /**
+ * IS_ERR.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool
+ */
+ public static function isErr($value = '')
+ {
+ $value = self::flattenSingleValue($value);
+
+ return self::isError($value) && (!self::isNa(($value)));
+ }
+
+ /**
+ * IS_ERROR.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool
+ */
+ public static function isError($value = '')
+ {
+ $value = self::flattenSingleValue($value);
+
+ if (!is_string($value)) {
+ return false;
+ }
+
+ return in_array($value, self::$errorCodes);
+ }
+
+ /**
+ * IS_NA.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool
+ */
+ public static function isNa($value = '')
+ {
+ $value = self::flattenSingleValue($value);
+
+ return $value === self::NA();
+ }
+
+ /**
+ * IS_EVEN.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool|string
+ */
+ public static function isEven($value = null)
+ {
+ $value = self::flattenSingleValue($value);
+
+ if ($value === null) {
+ return self::NAME();
+ } elseif ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
+ return self::VALUE();
+ }
+
+ return $value % 2 == 0;
+ }
+
+ /**
+ * IS_ODD.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool|string
+ */
+ public static function isOdd($value = null)
+ {
+ $value = self::flattenSingleValue($value);
+
+ if ($value === null) {
+ return self::NAME();
+ } elseif ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
+ return self::VALUE();
+ }
+
+ return abs($value) % 2 == 1;
+ }
+
+ /**
+ * IS_NUMBER.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool
+ */
+ public static function isNumber($value = null)
+ {
+ $value = self::flattenSingleValue($value);
+
+ if (is_string($value)) {
+ return false;
+ }
+
+ return is_numeric($value);
+ }
+
+ /**
+ * IS_LOGICAL.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool
+ */
+ public static function isLogical($value = null)
+ {
+ $value = self::flattenSingleValue($value);
+
+ return is_bool($value);
+ }
+
+ /**
+ * IS_TEXT.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool
+ */
+ public static function isText($value = null)
+ {
+ $value = self::flattenSingleValue($value);
+
+ return is_string($value) && !self::isError($value);
+ }
+
+ /**
+ * IS_NONTEXT.
+ *
+ * @param mixed $value Value to check
+ *
+ * @return bool
+ */
+ public static function isNonText($value = null)
+ {
+ return !self::isText($value);
+ }
+
+ /**
+ * N.
+ *
+ * Returns a value converted to a number
+ *
+ * @param null|mixed $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 number That number
+ * A date The serial number of that date
+ * TRUE 1
+ * FALSE 0
+ * An error value The error value
+ * Anything else 0
+ */
+ public static function n($value = null)
+ {
+ while (is_array($value)) {
+ $value = array_shift($value);
+ }
+
+ switch (gettype($value)) {
+ case 'double':
+ case 'float':
+ case 'integer':
+ return $value;
+ case 'boolean':
+ return (int) $value;
+ case 'string':
+ // Errors
+ if ((strlen($value) > 0) && ($value[0] == '#')) {
+ return $value;
+ }
+
+ break;
+ }
+
+ return 0;
+ }
+
+ /**
+ * TYPE.
+ *
+ * Returns a number that identifies the type of a value
+ *
+ * @param null|mixed $value The value you want tested
+ *
+ * @return number N converts values listed in the following table
+ * If value is or refers to N returns
+ * A number 1
+ * Text 2
+ * Logical Value 4
+ * An error value 16
+ * Array or Matrix 64
+ */
+ public static function TYPE($value = null)
+ {
+ $value = self::flattenArrayIndexed($value);
+ if (is_array($value) && (count($value) > 1)) {
+ end($value);
+ $a = key($value);
+ // Range of cells is an error
+ if (self::isCellValue($a)) {
+ return 16;
+ // Test for Matrix
+ } elseif (self::isMatrixValue($a)) {
+ return 64;
+ }
+ } elseif (empty($value)) {
+ // Empty Cell
+ return 1;
+ }
+ $value = self::flattenSingleValue($value);
+
+ if (($value === null) || (is_float($value)) || (is_int($value))) {
+ return 1;
+ } elseif (is_bool($value)) {
+ return 4;
+ } elseif (is_array($value)) {
+ return 64;
+ } elseif (is_string($value)) {
+ // Errors
+ if ((strlen($value) > 0) && ($value[0] == '#')) {
+ return 16;
+ }
+
+ return 2;
+ }
+
+ return 0;
+ }
+
+ /**
+ * Convert a multi-dimensional array to a simple 1-dimensional array.
+ *
+ * @param array $array Array to be flattened
+ *
+ * @return array Flattened array
+ */
+ public static function flattenArray($array)
+ {
+ if (!is_array($array)) {
+ return (array) $array;
+ }
+
+ $arrayValues = [];
+ foreach ($array as $value) {
+ if (is_array($value)) {
+ foreach ($value as $val) {
+ if (is_array($val)) {
+ foreach ($val as $v) {
+ $arrayValues[] = $v;
+ }
+ } else {
+ $arrayValues[] = $val;
+ }
+ }
+ } else {
+ $arrayValues[] = $value;
+ }
+ }
+
+ return $arrayValues;
+ }
+
+ /**
+ * 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
+ */
+ public static function flattenArrayIndexed($array)
+ {
+ if (!is_array($array)) {
+ return (array) $array;
+ }
+
+ $arrayValues = [];
+ foreach ($array as $k1 => $value) {
+ if (is_array($value)) {
+ foreach ($value as $k2 => $val) {
+ if (is_array($val)) {
+ foreach ($val as $k3 => $v) {
+ $arrayValues[$k1 . '.' . $k2 . '.' . $k3] = $v;
+ }
+ } else {
+ $arrayValues[$k1 . '.' . $k2] = $val;
+ }
+ }
+ } else {
+ $arrayValues[$k1] = $value;
+ }
+ }
+
+ return $arrayValues;
+ }
+
+ /**
+ * Convert an array to a single scalar value by extracting the first element.
+ *
+ * @param mixed $value Array or scalar value
+ *
+ * @return mixed
+ */
+ public static function flattenSingleValue($value = '')
+ {
+ while (is_array($value)) {
+ $value = array_pop($value);
+ }
+
+ return $value;
+ }
+
+ /**
+ * ISFORMULA.
+ *
+ * @param mixed $cellReference The cell to check
+ * @param Cell $pCell The current cell (containing this formula)
+ *
+ * @return bool|string
+ */
+ public static function isFormula($cellReference = '', Cell $pCell = null)
+ {
+ if ($pCell === null) {
+ return self::REF();
+ }
+
+ preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches);
+
+ $cellReference = $matches[6] . $matches[7];
+ $worksheetName = trim($matches[3], "'");
+
+ $worksheet = (!empty($worksheetName))
+ ? $pCell->getWorksheet()->getParent()->getSheetByName($worksheetName)
+ : $pCell->getWorksheet();
+
+ return $worksheet->getCell($cellReference)->isFormula();
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Logical.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Logical.php
new file mode 100644
index 00000000000..c36e3fca74f
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Logical.php
@@ -0,0 +1,296 @@
+ 0) && ($returnValue == $argCount);
+ }
+
+ /**
+ * LOGICAL_OR.
+ *
+ * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
+ *
+ * Excel Function:
+ * =OR(logical1[,logical2[, ...]])
+ *
+ * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
+ * or references that contain logical values.
+ *
+ * Boolean arguments are treated as True or False as appropriate
+ * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
+ * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
+ * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
+ *
+ * @category Logical Functions
+ *
+ * @param mixed $args Data values
+ *
+ * @return bool|string the logical OR of the arguments
+ */
+ public static function logicalOr(...$args)
+ {
+ $args = Functions::flattenArray($args);
+
+ if (count($args) == 0) {
+ return Functions::VALUE();
+ }
+
+ $args = array_filter($args, function ($value) {
+ return $value !== null || (is_string($value) && trim($value) == '');
+ });
+
+ $returnValue = self::countTrueValues($args);
+ if (is_string($returnValue)) {
+ return $returnValue;
+ }
+
+ return $returnValue > 0;
+ }
+
+ /**
+ * LOGICAL_XOR.
+ *
+ * Returns the Exclusive Or logical operation for one or more supplied conditions.
+ * i.e. the Xor function returns TRUE if an odd number of the supplied conditions evaluate to TRUE, and FALSE otherwise.
+ *
+ * Excel Function:
+ * =XOR(logical1[,logical2[, ...]])
+ *
+ * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
+ * or references that contain logical values.
+ *
+ * Boolean arguments are treated as True or False as appropriate
+ * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
+ * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
+ * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
+ *
+ * @category Logical Functions
+ *
+ * @param mixed $args Data values
+ *
+ * @return bool|string the logical XOR of the arguments
+ */
+ public static function logicalXor(...$args)
+ {
+ $args = Functions::flattenArray($args);
+
+ if (count($args) == 0) {
+ return Functions::VALUE();
+ }
+
+ $args = array_filter($args, function ($value) {
+ return $value !== null || (is_string($value) && trim($value) == '');
+ });
+
+ $returnValue = self::countTrueValues($args);
+ if (is_string($returnValue)) {
+ return $returnValue;
+ }
+
+ return $returnValue % 2 == 1;
+ }
+
+ /**
+ * NOT.
+ *
+ * Returns the boolean inverse of the argument.
+ *
+ * Excel Function:
+ * =NOT(logical)
+ *
+ * The argument must evaluate to a logical value such as TRUE or FALSE
+ *
+ * Boolean arguments are treated as True or False as appropriate
+ * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
+ * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
+ * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
+ *
+ * @category Logical Functions
+ *
+ * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE
+ *
+ * @return bool|string the boolean inverse of the argument
+ */
+ public static function NOT($logical = false)
+ {
+ $logical = Functions::flattenSingleValue($logical);
+
+ if (is_string($logical)) {
+ $logical = strtoupper($logical);
+ if (($logical == 'TRUE') || ($logical == Calculation::getTRUE())) {
+ return false;
+ } elseif (($logical == 'FALSE') || ($logical == Calculation::getFALSE())) {
+ return true;
+ }
+
+ return Functions::VALUE();
+ }
+
+ return !$logical;
+ }
+
+ /**
+ * STATEMENT_IF.
+ *
+ * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.
+ *
+ * Excel Function:
+ * =IF(condition[,returnIfTrue[,returnIfFalse]])
+ *
+ * Condition is any value or expression that can be evaluated to TRUE or FALSE.
+ * For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
+ * the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
+ * This argument can use any comparison calculation operator.
+ * ReturnIfTrue is the value that is returned if condition evaluates to TRUE.
+ * For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,
+ * then the IF function returns the text "Within budget"
+ * If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use
+ * the logical value TRUE for this argument.
+ * ReturnIfTrue can be another formula.
+ * ReturnIfFalse is the value that is returned if condition evaluates to FALSE.
+ * For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,
+ * then the IF function returns the text "Over budget".
+ * If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.
+ * If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.
+ * ReturnIfFalse can be another formula.
+ *
+ * @category Logical Functions
+ *
+ * @param mixed $condition Condition to evaluate
+ * @param mixed $returnIfTrue Value to return when condition is true
+ * @param mixed $returnIfFalse Optional value to return when condition is false
+ *
+ * @return mixed The value of returnIfTrue or returnIfFalse determined by condition
+ */
+ public static function statementIf($condition = true, $returnIfTrue = 0, $returnIfFalse = false)
+ {
+ $condition = ($condition === null) ? true : (bool) Functions::flattenSingleValue($condition);
+ $returnIfTrue = ($returnIfTrue === null) ? 0 : Functions::flattenSingleValue($returnIfTrue);
+ $returnIfFalse = ($returnIfFalse === null) ? false : Functions::flattenSingleValue($returnIfFalse);
+
+ return ($condition) ? $returnIfTrue : $returnIfFalse;
+ }
+
+ /**
+ * IFERROR.
+ *
+ * Excel Function:
+ * =IFERROR(testValue,errorpart)
+ *
+ * @category Logical Functions
+ *
+ * @param mixed $testValue Value to check, is also the value returned when no error
+ * @param mixed $errorpart Value to return when testValue is an error condition
+ *
+ * @return mixed The value of errorpart or testValue determined by error condition
+ */
+ public static function IFERROR($testValue = '', $errorpart = '')
+ {
+ $testValue = ($testValue === null) ? '' : Functions::flattenSingleValue($testValue);
+ $errorpart = ($errorpart === null) ? '' : Functions::flattenSingleValue($errorpart);
+
+ return self::statementIf(Functions::isError($testValue), $errorpart, $testValue);
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/LookupRef.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/LookupRef.php
new file mode 100644
index 00000000000..2a3c5582108
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/LookupRef.php
@@ -0,0 +1,914 @@
+ '') {
+ if (strpos($sheetText, ' ') !== false) {
+ $sheetText = "'" . $sheetText . "'";
+ }
+ $sheetText .= '!';
+ }
+ if ((!is_bool($referenceStyle)) || $referenceStyle) {
+ $rowRelative = $columnRelative = '$';
+ $column = Coordinate::stringFromColumnIndex($column);
+ if (($relativity == 2) || ($relativity == 4)) {
+ $columnRelative = '';
+ }
+ if (($relativity == 3) || ($relativity == 4)) {
+ $rowRelative = '';
+ }
+
+ return $sheetText . $columnRelative . $column . $rowRelative . $row;
+ }
+ if (($relativity == 2) || ($relativity == 4)) {
+ $column = '[' . $column . ']';
+ }
+ if (($relativity == 3) || ($relativity == 4)) {
+ $row = '[' . $row . ']';
+ }
+
+ return $sheetText . 'R' . $row . 'C' . $column;
+ }
+
+ /**
+ * COLUMN.
+ *
+ * Returns the column number of the given cell reference
+ * If the cell reference is a range of cells, COLUMN returns the column numbers of each column in the reference as a horizontal array.
+ * If cell reference is omitted, and the function is being called through the calculation engine, then it is assumed to be the
+ * reference of the cell in which the COLUMN function appears; otherwise this function returns 0.
+ *
+ * Excel Function:
+ * =COLUMN([cellAddress])
+ *
+ * @param null|array|string $cellAddress A reference to a range of cells for which you want the column numbers
+ *
+ * @return int|int[]
+ */
+ public static function COLUMN($cellAddress = null)
+ {
+ if ($cellAddress === null || trim($cellAddress) === '') {
+ return 0;
+ }
+
+ if (is_array($cellAddress)) {
+ foreach ($cellAddress as $columnKey => $value) {
+ $columnKey = preg_replace('/[^a-z]/i', '', $columnKey);
+
+ return (int) Coordinate::columnIndexFromString($columnKey);
+ }
+ } else {
+ list($sheet, $cellAddress) = Worksheet::extractSheetTitle($cellAddress, true);
+ if (strpos($cellAddress, ':') !== false) {
+ list($startAddress, $endAddress) = explode(':', $cellAddress);
+ $startAddress = preg_replace('/[^a-z]/i', '', $startAddress);
+ $endAddress = preg_replace('/[^a-z]/i', '', $endAddress);
+ $returnValue = [];
+ do {
+ $returnValue[] = (int) Coordinate::columnIndexFromString($startAddress);
+ } while ($startAddress++ != $endAddress);
+
+ return $returnValue;
+ }
+ $cellAddress = preg_replace('/[^a-z]/i', '', $cellAddress);
+
+ return (int) Coordinate::columnIndexFromString($cellAddress);
+ }
+ }
+
+ /**
+ * COLUMNS.
+ *
+ * Returns the number of columns in an array or reference.
+ *
+ * Excel Function:
+ * =COLUMNS(cellAddress)
+ *
+ * @param null|array|string $cellAddress An array or array formula, or a reference to a range of cells for which you want the number of columns
+ *
+ * @return int The number of columns in cellAddress
+ */
+ public static function COLUMNS($cellAddress = null)
+ {
+ if ($cellAddress === null || $cellAddress === '') {
+ return 1;
+ } elseif (!is_array($cellAddress)) {
+ return Functions::VALUE();
+ }
+
+ reset($cellAddress);
+ $isMatrix = (is_numeric(key($cellAddress)));
+ list($columns, $rows) = Calculation::getMatrixDimensions($cellAddress);
+
+ if ($isMatrix) {
+ return $rows;
+ }
+
+ return $columns;
+ }
+
+ /**
+ * ROW.
+ *
+ * Returns the row number of the given cell reference
+ * If the cell reference is a range of cells, ROW returns the row numbers of each row in the reference as a vertical array.
+ * If cell reference is omitted, and the function is being called through the calculation engine, then it is assumed to be the
+ * reference of the cell in which the ROW function appears; otherwise this function returns 0.
+ *
+ * Excel Function:
+ * =ROW([cellAddress])
+ *
+ * @param null|array|string $cellAddress A reference to a range of cells for which you want the row numbers
+ *
+ * @return int or array of integer
+ */
+ public static function ROW($cellAddress = null)
+ {
+ if ($cellAddress === null || trim($cellAddress) === '') {
+ return 0;
+ }
+
+ if (is_array($cellAddress)) {
+ foreach ($cellAddress as $columnKey => $rowValue) {
+ foreach ($rowValue as $rowKey => $cellValue) {
+ return (int) preg_replace('/\D/', '', $rowKey);
+ }
+ }
+ } else {
+ list($sheet, $cellAddress) = Worksheet::extractSheetTitle($cellAddress, true);
+ if (strpos($cellAddress, ':') !== false) {
+ list($startAddress, $endAddress) = explode(':', $cellAddress);
+ $startAddress = preg_replace('/\D/', '', $startAddress);
+ $endAddress = preg_replace('/\D/', '', $endAddress);
+ $returnValue = [];
+ do {
+ $returnValue[][] = (int) $startAddress;
+ } while ($startAddress++ != $endAddress);
+
+ return $returnValue;
+ }
+ list($cellAddress) = explode(':', $cellAddress);
+
+ return (int) preg_replace('/\D/', '', $cellAddress);
+ }
+ }
+
+ /**
+ * ROWS.
+ *
+ * Returns the number of rows in an array or reference.
+ *
+ * Excel Function:
+ * =ROWS(cellAddress)
+ *
+ * @param null|array|string $cellAddress An array or array formula, or a reference to a range of cells for which you want the number of rows
+ *
+ * @return int The number of rows in cellAddress
+ */
+ public static function ROWS($cellAddress = null)
+ {
+ if ($cellAddress === null || $cellAddress === '') {
+ return 1;
+ } elseif (!is_array($cellAddress)) {
+ return Functions::VALUE();
+ }
+
+ reset($cellAddress);
+ $isMatrix = (is_numeric(key($cellAddress)));
+ list($columns, $rows) = Calculation::getMatrixDimensions($cellAddress);
+
+ if ($isMatrix) {
+ return $columns;
+ }
+
+ return $rows;
+ }
+
+ /**
+ * HYPERLINK.
+ *
+ * Excel Function:
+ * =HYPERLINK(linkURL,displayName)
+ *
+ * @category Logical Functions
+ *
+ * @param string $linkURL Value to check, is also the value returned when no error
+ * @param string $displayName Value to return when testValue is an error condition
+ * @param Cell $pCell The cell to set the hyperlink in
+ *
+ * @return mixed The value of $displayName (or $linkURL if $displayName was blank)
+ */
+ public static function HYPERLINK($linkURL = '', $displayName = null, Cell $pCell = null)
+ {
+ $linkURL = ($linkURL === null) ? '' : Functions::flattenSingleValue($linkURL);
+ $displayName = ($displayName === null) ? '' : Functions::flattenSingleValue($displayName);
+
+ if ((!is_object($pCell)) || (trim($linkURL) == '')) {
+ return Functions::REF();
+ }
+
+ if ((is_object($displayName)) || trim($displayName) == '') {
+ $displayName = $linkURL;
+ }
+
+ $pCell->getHyperlink()->setUrl($linkURL);
+ $pCell->getHyperlink()->setTooltip($displayName);
+
+ return $displayName;
+ }
+
+ /**
+ * INDIRECT.
+ *
+ * Returns the reference specified by a text string.
+ * References are immediately evaluated to display their contents.
+ *
+ * Excel Function:
+ * =INDIRECT(cellAddress)
+ *
+ * NOTE - INDIRECT() does not yet support the optional a1 parameter introduced in Excel 2010
+ *
+ * @param null|array|string $cellAddress $cellAddress The cell address of the current cell (containing this formula)
+ * @param Cell $pCell The current cell (containing this formula)
+ *
+ * @return mixed The cells referenced by cellAddress
+ *
+ * @todo Support for the optional a1 parameter introduced in Excel 2010
+ */
+ public static function INDIRECT($cellAddress = null, Cell $pCell = null)
+ {
+ $cellAddress = Functions::flattenSingleValue($cellAddress);
+ if ($cellAddress === null || $cellAddress === '') {
+ return Functions::REF();
+ }
+
+ $cellAddress1 = $cellAddress;
+ $cellAddress2 = null;
+ if (strpos($cellAddress, ':') !== false) {
+ list($cellAddress1, $cellAddress2) = explode(':', $cellAddress);
+ }
+
+ if ((!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellAddress1, $matches)) ||
+ (($cellAddress2 !== null) && (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellAddress2, $matches)))) {
+ if (!preg_match('/^' . Calculation::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $cellAddress1, $matches)) {
+ return Functions::REF();
+ }
+
+ if (strpos($cellAddress, '!') !== false) {
+ list($sheetName, $cellAddress) = Worksheet::extractSheetTitle($cellAddress, true);
+ $sheetName = trim($sheetName, "'");
+ $pSheet = $pCell->getWorksheet()->getParent()->getSheetByName($sheetName);
+ } else {
+ $pSheet = $pCell->getWorksheet();
+ }
+
+ return Calculation::getInstance()->extractNamedRange($cellAddress, $pSheet, false);
+ }
+
+ if (strpos($cellAddress, '!') !== false) {
+ list($sheetName, $cellAddress) = Worksheet::extractSheetTitle($cellAddress, true);
+ $sheetName = trim($sheetName, "'");
+ $pSheet = $pCell->getWorksheet()->getParent()->getSheetByName($sheetName);
+ } else {
+ $pSheet = $pCell->getWorksheet();
+ }
+
+ return Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, false);
+ }
+
+ /**
+ * OFFSET.
+ *
+ * Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells.
+ * The reference that is returned can be a single cell or a range of cells. You can specify the number of rows and
+ * the number of columns to be returned.
+ *
+ * Excel Function:
+ * =OFFSET(cellAddress, rows, cols, [height], [width])
+ *
+ * @param null|string $cellAddress The reference from which you want to base the offset. Reference must refer to a cell or
+ * range of adjacent cells; otherwise, OFFSET returns the #VALUE! error value.
+ * @param mixed $rows The number of rows, up or down, that you want the upper-left cell to refer to.
+ * Using 5 as the rows argument specifies that the upper-left cell in the reference is
+ * five rows below reference. Rows can be positive (which means below the starting reference)
+ * or negative (which means above the starting reference).
+ * @param mixed $columns The number of columns, to the left or right, that you want the upper-left cell of the result
+ * to refer to. Using 5 as the cols argument specifies that the upper-left cell in the
+ * reference is five columns to the right of reference. Cols can be positive (which means
+ * to the right of the starting reference) or negative (which means to the left of the
+ * starting reference).
+ * @param mixed $height The height, in number of rows, that you want the returned reference to be. Height must be a positive number.
+ * @param mixed $width The width, in number of columns, that you want the returned reference to be. Width must be a positive number.
+ * @param null|Cell $pCell
+ *
+ * @return string A reference to a cell or range of cells
+ */
+ public static function OFFSET($cellAddress = null, $rows = 0, $columns = 0, $height = null, $width = null, Cell $pCell = null)
+ {
+ $rows = Functions::flattenSingleValue($rows);
+ $columns = Functions::flattenSingleValue($columns);
+ $height = Functions::flattenSingleValue($height);
+ $width = Functions::flattenSingleValue($width);
+ if ($cellAddress === null) {
+ return 0;
+ }
+
+ if (!is_object($pCell)) {
+ return Functions::REF();
+ }
+
+ $sheetName = null;
+ if (strpos($cellAddress, '!')) {
+ list($sheetName, $cellAddress) = Worksheet::extractSheetTitle($cellAddress, true);
+ $sheetName = trim($sheetName, "'");
+ }
+ if (strpos($cellAddress, ':')) {
+ list($startCell, $endCell) = explode(':', $cellAddress);
+ } else {
+ $startCell = $endCell = $cellAddress;
+ }
+ list($startCellColumn, $startCellRow) = Coordinate::coordinateFromString($startCell);
+ list($endCellColumn, $endCellRow) = Coordinate::coordinateFromString($endCell);
+
+ $startCellRow += $rows;
+ $startCellColumn = Coordinate::columnIndexFromString($startCellColumn) - 1;
+ $startCellColumn += $columns;
+
+ if (($startCellRow <= 0) || ($startCellColumn < 0)) {
+ return Functions::REF();
+ }
+ $endCellColumn = Coordinate::columnIndexFromString($endCellColumn) - 1;
+ if (($width != null) && (!is_object($width))) {
+ $endCellColumn = $startCellColumn + $width - 1;
+ } else {
+ $endCellColumn += $columns;
+ }
+ $startCellColumn = Coordinate::stringFromColumnIndex($startCellColumn + 1);
+
+ if (($height != null) && (!is_object($height))) {
+ $endCellRow = $startCellRow + $height - 1;
+ } else {
+ $endCellRow += $rows;
+ }
+
+ if (($endCellRow <= 0) || ($endCellColumn < 0)) {
+ return Functions::REF();
+ }
+ $endCellColumn = Coordinate::stringFromColumnIndex($endCellColumn + 1);
+
+ $cellAddress = $startCellColumn . $startCellRow;
+ if (($startCellColumn != $endCellColumn) || ($startCellRow != $endCellRow)) {
+ $cellAddress .= ':' . $endCellColumn . $endCellRow;
+ }
+
+ if ($sheetName !== null) {
+ $pSheet = $pCell->getWorksheet()->getParent()->getSheetByName($sheetName);
+ } else {
+ $pSheet = $pCell->getWorksheet();
+ }
+
+ return Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, false);
+ }
+
+ /**
+ * CHOOSE.
+ *
+ * Uses lookup_value to return a value from the list of value arguments.
+ * Use CHOOSE to select one of up to 254 values based on the lookup_value.
+ *
+ * Excel Function:
+ * =CHOOSE(index_num, value1, [value2], ...)
+ *
+ * @param mixed $index_num Specifies which value argument is selected.
+ * Index_num must be a number between 1 and 254, or a formula or reference to a cell containing a number
+ * between 1 and 254.
+ * @param mixed $value1 ... Value1 is required, subsequent values are optional.
+ * Between 1 to 254 value arguments from which CHOOSE selects a value or an action to perform based on
+ * index_num. The arguments can be numbers, cell references, defined names, formulas, functions, or
+ * text.
+ *
+ * @return mixed The selected value
+ */
+ public static function CHOOSE(...$chooseArgs)
+ {
+ $chosenEntry = Functions::flattenArray(array_shift($chooseArgs));
+ $entryCount = count($chooseArgs) - 1;
+
+ if (is_array($chosenEntry)) {
+ $chosenEntry = array_shift($chosenEntry);
+ }
+ if ((is_numeric($chosenEntry)) && (!is_bool($chosenEntry))) {
+ --$chosenEntry;
+ } else {
+ return Functions::VALUE();
+ }
+ $chosenEntry = floor($chosenEntry);
+ if (($chosenEntry < 0) || ($chosenEntry > $entryCount)) {
+ return Functions::VALUE();
+ }
+
+ if (is_array($chooseArgs[$chosenEntry])) {
+ return Functions::flattenArray($chooseArgs[$chosenEntry]);
+ }
+
+ return $chooseArgs[$chosenEntry];
+ }
+
+ /**
+ * MATCH.
+ *
+ * The MATCH function searches for a specified item in a range of cells
+ *
+ * Excel Function:
+ * =MATCH(lookup_value, lookup_array, [match_type])
+ *
+ * @param mixed $lookupValue The value that you want to match in lookup_array
+ * @param mixed $lookupArray The range of cells being searched
+ * @param mixed $matchType The number -1, 0, or 1. -1 means above, 0 means exact match, 1 means below. If match_type is 1 or -1, the list has to be ordered.
+ *
+ * @return int The relative position of the found item
+ */
+ public static function MATCH($lookupValue, $lookupArray, $matchType = 1)
+ {
+ $lookupArray = Functions::flattenArray($lookupArray);
+ $lookupValue = Functions::flattenSingleValue($lookupValue);
+ $matchType = ($matchType === null) ? 1 : (int) Functions::flattenSingleValue($matchType);
+
+ // MATCH is not case sensitive
+ $lookupValue = strtolower($lookupValue);
+
+ // Lookup_value type has to be number, text, or logical values
+ if ((!is_numeric($lookupValue)) && (!is_string($lookupValue)) && (!is_bool($lookupValue))) {
+ return Functions::NA();
+ }
+
+ // Match_type is 0, 1 or -1
+ if (($matchType !== 0) && ($matchType !== -1) && ($matchType !== 1)) {
+ return Functions::NA();
+ }
+
+ // Lookup_array should not be empty
+ $lookupArraySize = count($lookupArray);
+ if ($lookupArraySize <= 0) {
+ return Functions::NA();
+ }
+
+ // Lookup_array should contain only number, text, or logical values, or empty (null) cells
+ foreach ($lookupArray as $i => $lookupArrayValue) {
+ // check the type of the value
+ if ((!is_numeric($lookupArrayValue)) && (!is_string($lookupArrayValue)) &&
+ (!is_bool($lookupArrayValue)) && ($lookupArrayValue !== null)
+ ) {
+ return Functions::NA();
+ }
+ // Convert strings to lowercase for case-insensitive testing
+ if (is_string($lookupArrayValue)) {
+ $lookupArray[$i] = strtolower($lookupArrayValue);
+ }
+ if (($lookupArrayValue === null) && (($matchType == 1) || ($matchType == -1))) {
+ $lookupArray = array_slice($lookupArray, 0, $i - 1);
+ }
+ }
+
+ if ($matchType == 1) {
+ // If match_type is 1 the list has to be processed from last to first
+
+ $lookupArray = array_reverse($lookupArray);
+ $keySet = array_reverse(array_keys($lookupArray));
+ }
+
+ // **
+ // find the match
+ // **
+
+ if ($matchType == 0 || $matchType == 1) {
+ foreach ($lookupArray as $i => $lookupArrayValue) {
+ if (($matchType == 0) && ($lookupArrayValue == $lookupValue)) {
+ // exact match
+ return ++$i;
+ } elseif (($matchType == 1) && ($lookupArrayValue <= $lookupValue)) {
+ $i = array_search($i, $keySet);
+
+ // The current value is the (first) match
+ return $i + 1;
+ }
+ }
+ } else {
+ // matchType = -1
+
+ // "Special" case: since the array it's supposed to be ordered in descending order, the
+ // Excel algorithm gives up immediately if the first element is smaller than the searched value
+ if ($lookupArray[0] < $lookupValue) {
+ return Functions::NA();
+ }
+
+ $maxValueKey = null;
+
+ // The basic algorithm is:
+ // Iterate and keep the highest match until the next element is smaller than the searched value.
+ // Return immediately if perfect match is found
+ foreach ($lookupArray as $i => $lookupArrayValue) {
+ if ($lookupArrayValue == $lookupValue) {
+ // Another "special" case. If a perfect match is found,
+ // the algorithm gives up immediately
+ return $i + 1;
+ } elseif ($lookupArrayValue >= $lookupValue) {
+ $maxValueKey = $i + 1;
+ }
+ }
+
+ if ($maxValueKey !== null) {
+ return $maxValueKey;
+ }
+ }
+
+ // Unsuccessful in finding a match, return #N/A error value
+ return Functions::NA();
+ }
+
+ /**
+ * INDEX.
+ *
+ * Uses an index to choose a value from a reference or array
+ *
+ * Excel Function:
+ * =INDEX(range_array, row_num, [column_num])
+ *
+ * @param mixed $arrayValues A range of cells or an array constant
+ * @param mixed $rowNum The row in array from which to return a value. If row_num is omitted, column_num is required.
+ * @param mixed $columnNum The column in array from which to return a value. If column_num is omitted, row_num is required.
+ *
+ * @return mixed the value of a specified cell or array of cells
+ */
+ public static function INDEX($arrayValues, $rowNum = 0, $columnNum = 0)
+ {
+ $rowNum = Functions::flattenSingleValue($rowNum);
+ $columnNum = Functions::flattenSingleValue($columnNum);
+
+ if (($rowNum < 0) || ($columnNum < 0)) {
+ return Functions::VALUE();
+ }
+
+ if (!is_array($arrayValues) || ($rowNum > count($arrayValues))) {
+ return Functions::REF();
+ }
+
+ $rowKeys = array_keys($arrayValues);
+ $columnKeys = @array_keys($arrayValues[$rowKeys[0]]);
+
+ if ($columnNum > count($columnKeys)) {
+ return Functions::VALUE();
+ } elseif ($columnNum == 0) {
+ if ($rowNum == 0) {
+ return $arrayValues;
+ }
+ $rowNum = $rowKeys[--$rowNum];
+ $returnArray = [];
+ foreach ($arrayValues as $arrayColumn) {
+ if (is_array($arrayColumn)) {
+ if (isset($arrayColumn[$rowNum])) {
+ $returnArray[] = $arrayColumn[$rowNum];
+ } else {
+ return [$rowNum => $arrayValues[$rowNum]];
+ }
+ } else {
+ return $arrayValues[$rowNum];
+ }
+ }
+
+ return $returnArray;
+ }
+ $columnNum = $columnKeys[--$columnNum];
+ if ($rowNum > count($rowKeys)) {
+ return Functions::VALUE();
+ } elseif ($rowNum == 0) {
+ return $arrayValues[$columnNum];
+ }
+ $rowNum = $rowKeys[--$rowNum];
+
+ return $arrayValues[$rowNum][$columnNum];
+ }
+
+ /**
+ * TRANSPOSE.
+ *
+ * @param array $matrixData A matrix of values
+ *
+ * @return array
+ *
+ * Unlike the Excel TRANSPOSE function, which will only work on a single row or column, this function will transpose a full matrix
+ */
+ public static function TRANSPOSE($matrixData)
+ {
+ $returnMatrix = [];
+ if (!is_array($matrixData)) {
+ $matrixData = [[$matrixData]];
+ }
+
+ $column = 0;
+ foreach ($matrixData as $matrixRow) {
+ $row = 0;
+ foreach ($matrixRow as $matrixCell) {
+ $returnMatrix[$row][$column] = $matrixCell;
+ ++$row;
+ }
+ ++$column;
+ }
+
+ return $returnMatrix;
+ }
+
+ private static function vlookupSort($a, $b)
+ {
+ reset($a);
+ $firstColumn = key($a);
+ if (($aLower = strtolower($a[$firstColumn])) == ($bLower = strtolower($b[$firstColumn]))) {
+ return 0;
+ }
+
+ return ($aLower < $bLower) ? -1 : 1;
+ }
+
+ /**
+ * VLOOKUP
+ * The VLOOKUP function searches for value in the left-most column of lookup_array and returns the value in the same row based on the index_number.
+ *
+ * @param mixed $lookup_value The value that you want to match in lookup_array
+ * @param mixed $lookup_array The range of cells being searched
+ * @param mixed $index_number The column number in table_array from which the matching value must be returned. The first column is 1.
+ * @param mixed $not_exact_match determines if you are looking for an exact match based on lookup_value
+ *
+ * @return mixed The value of the found cell
+ */
+ public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match = true)
+ {
+ $lookup_value = Functions::flattenSingleValue($lookup_value);
+ $index_number = Functions::flattenSingleValue($index_number);
+ $not_exact_match = Functions::flattenSingleValue($not_exact_match);
+
+ // index_number must be greater than or equal to 1
+ if ($index_number < 1) {
+ return Functions::VALUE();
+ }
+
+ // index_number must be less than or equal to the number of columns in lookup_array
+ if ((!is_array($lookup_array)) || (empty($lookup_array))) {
+ return Functions::REF();
+ }
+ $f = array_keys($lookup_array);
+ $firstRow = array_pop($f);
+ if ((!is_array($lookup_array[$firstRow])) || ($index_number > count($lookup_array[$firstRow]))) {
+ return Functions::REF();
+ }
+ $columnKeys = array_keys($lookup_array[$firstRow]);
+ $returnColumn = $columnKeys[--$index_number];
+ $firstColumn = array_shift($columnKeys);
+
+ if (!$not_exact_match) {
+ uasort($lookup_array, ['self', 'vlookupSort']);
+ }
+
+ $rowNumber = $rowValue = false;
+ foreach ($lookup_array as $rowKey => $rowData) {
+ // break if we have passed possible keys
+ if ((is_numeric($lookup_value) && is_numeric($rowData[$firstColumn]) && ($rowData[$firstColumn] > $lookup_value)) ||
+ (!is_numeric($lookup_value) && !is_numeric($rowData[$firstColumn]) && (strtolower($rowData[$firstColumn]) > strtolower($lookup_value)))) {
+ break;
+ }
+ // remember the last key, but only if datatypes match
+ if ((is_numeric($lookup_value) && is_numeric($rowData[$firstColumn])) ||
+ (!is_numeric($lookup_value) && !is_numeric($rowData[$firstColumn]))) {
+ if ($not_exact_match) {
+ $rowNumber = $rowKey;
+ $rowValue = $rowData[$firstColumn];
+
+ continue;
+ } elseif ((strtolower($rowData[$firstColumn]) == strtolower($lookup_value))
+ // Spreadsheets software returns first exact match,
+ // we have sorted and we might have broken key orders
+ // we want the first one (by its initial index)
+ && (($rowNumber == false) || ($rowKey < $rowNumber))
+ ) {
+ $rowNumber = $rowKey;
+ $rowValue = $rowData[$firstColumn];
+ }
+ }
+ }
+
+ if ($rowNumber !== false) {
+ // return the appropriate value
+ return $lookup_array[$rowNumber][$returnColumn];
+ }
+
+ return Functions::NA();
+ }
+
+ /**
+ * HLOOKUP
+ * The HLOOKUP function searches for value in the top-most row of lookup_array and returns the value in the same column based on the index_number.
+ *
+ * @param mixed $lookup_value The value that you want to match in lookup_array
+ * @param mixed $lookup_array The range of cells being searched
+ * @param mixed $index_number The row number in table_array from which the matching value must be returned. The first row is 1.
+ * @param mixed $not_exact_match determines if you are looking for an exact match based on lookup_value
+ *
+ * @return mixed The value of the found cell
+ */
+ public static function HLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match = true)
+ {
+ $lookup_value = Functions::flattenSingleValue($lookup_value);
+ $index_number = Functions::flattenSingleValue($index_number);
+ $not_exact_match = Functions::flattenSingleValue($not_exact_match);
+
+ // index_number must be greater than or equal to 1
+ if ($index_number < 1) {
+ return Functions::VALUE();
+ }
+
+ // index_number must be less than or equal to the number of columns in lookup_array
+ if ((!is_array($lookup_array)) || (empty($lookup_array))) {
+ return Functions::REF();
+ }
+ $f = array_keys($lookup_array);
+ $firstRow = array_pop($f);
+ if ((!is_array($lookup_array[$firstRow])) || ($index_number > count($lookup_array))) {
+ return Functions::REF();
+ }
+
+ $firstkey = $f[0] - 1;
+ $returnColumn = $firstkey + $index_number;
+ $firstColumn = array_shift($f);
+ $rowNumber = null;
+ foreach ($lookup_array[$firstColumn] as $rowKey => $rowData) {
+ // break if we have passed possible keys
+ $bothNumeric = is_numeric($lookup_value) && is_numeric($rowData);
+ $bothNotNumeric = !is_numeric($lookup_value) && !is_numeric($rowData);
+ if (($bothNumeric && $rowData > $lookup_value) ||
+ ($bothNotNumeric && strtolower($rowData) > strtolower($lookup_value))) {
+ break;
+ }
+
+ // Remember the last key, but only if datatypes match (as in VLOOKUP)
+ if ($bothNumeric || $bothNotNumeric) {
+ if ($not_exact_match) {
+ $rowNumber = $rowKey;
+
+ continue;
+ } elseif (strtolower($rowData) === strtolower($lookup_value)
+ && ($rowNumber === null || $rowKey < $rowNumber)
+ ) {
+ $rowNumber = $rowKey;
+ }
+ }
+ }
+
+ if ($rowNumber !== null) {
+ // otherwise return the appropriate value
+ return $lookup_array[$returnColumn][$rowNumber];
+ }
+
+ return Functions::NA();
+ }
+
+ /**
+ * LOOKUP
+ * The LOOKUP function searches for value either from a one-row or one-column range or from an array.
+ *
+ * @param mixed $lookup_value The value that you want to match in lookup_array
+ * @param mixed $lookup_vector The range of cells being searched
+ * @param null|mixed $result_vector The column from which the matching value must be returned
+ *
+ * @return mixed The value of the found cell
+ */
+ public static function LOOKUP($lookup_value, $lookup_vector, $result_vector = null)
+ {
+ $lookup_value = Functions::flattenSingleValue($lookup_value);
+
+ if (!is_array($lookup_vector)) {
+ return Functions::NA();
+ }
+ $hasResultVector = isset($result_vector);
+ $lookupRows = count($lookup_vector);
+ $l = array_keys($lookup_vector);
+ $l = array_shift($l);
+ $lookupColumns = count($lookup_vector[$l]);
+ // we correctly orient our results
+ if (($lookupRows === 1 && $lookupColumns > 1) || (!$hasResultVector && $lookupRows === 2 && $lookupColumns !== 2)) {
+ $lookup_vector = self::TRANSPOSE($lookup_vector);
+ $lookupRows = count($lookup_vector);
+ $l = array_keys($lookup_vector);
+ $lookupColumns = count($lookup_vector[array_shift($l)]);
+ }
+
+ if ($result_vector === null) {
+ $result_vector = $lookup_vector;
+ }
+ $resultRows = count($result_vector);
+ $l = array_keys($result_vector);
+ $l = array_shift($l);
+ $resultColumns = count($result_vector[$l]);
+ // we correctly orient our results
+ if ($resultRows === 1 && $resultColumns > 1) {
+ $result_vector = self::TRANSPOSE($result_vector);
+ $resultRows = count($result_vector);
+ $r = array_keys($result_vector);
+ $resultColumns = count($result_vector[array_shift($r)]);
+ }
+
+ if ($lookupRows === 2 && !$hasResultVector) {
+ $result_vector = array_pop($lookup_vector);
+ $lookup_vector = array_shift($lookup_vector);
+ }
+
+ if ($lookupColumns !== 2) {
+ foreach ($lookup_vector as &$value) {
+ if (is_array($value)) {
+ $k = array_keys($value);
+ $key1 = $key2 = array_shift($k);
+ ++$key2;
+ $dataValue1 = $value[$key1];
+ } else {
+ $key1 = 0;
+ $key2 = 1;
+ $dataValue1 = $value;
+ }
+ $dataValue2 = array_shift($result_vector);
+ if (is_array($dataValue2)) {
+ $dataValue2 = array_shift($dataValue2);
+ }
+ $value = [$key1 => $dataValue1, $key2 => $dataValue2];
+ }
+ unset($value);
+ }
+
+ return self::VLOOKUP($lookup_value, $lookup_vector, 2);
+ }
+
+ /**
+ * FORMULATEXT.
+ *
+ * @param mixed $cellReference The cell to check
+ * @param Cell $pCell The current cell (containing this formula)
+ *
+ * @return string
+ */
+ public static function FORMULATEXT($cellReference = '', Cell $pCell = null)
+ {
+ if ($pCell === null) {
+ return Functions::REF();
+ }
+
+ preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches);
+
+ $cellReference = $matches[6] . $matches[7];
+ $worksheetName = trim($matches[3], "'");
+ $worksheet = (!empty($worksheetName))
+ ? $pCell->getWorksheet()->getParent()->getSheetByName($worksheetName)
+ : $pCell->getWorksheet();
+
+ if (!$worksheet->getCell($cellReference)->isFormula()) {
+ return Functions::NA();
+ }
+
+ return $worksheet->getCell($cellReference)->getValue();
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/MathTrig.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/MathTrig.php
new file mode 100644
index 00000000000..9170196bae5
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/MathTrig.php
@@ -0,0 +1,1649 @@
+ 1; --$i) {
+ if (($value % $i) == 0) {
+ $factorArray = array_merge($factorArray, self::factors($value / $i));
+ $factorArray = array_merge($factorArray, self::factors($i));
+ if ($i <= sqrt($value)) {
+ break;
+ }
+ }
+ }
+ if (!empty($factorArray)) {
+ rsort($factorArray);
+
+ return $factorArray;
+ }
+
+ return [(int) $value];
+ }
+
+ private static function romanCut($num, $n)
+ {
+ return ($num - ($num % $n)) / $n;
+ }
+
+ /**
+ * ATAN2.
+ *
+ * This function calculates the arc tangent of the two variables x and y. It is similar to
+ * calculating the arc tangent of y ÷ x, except that the signs of both arguments are used
+ * to determine the quadrant of the result.
+ * The arctangent is the angle from the x-axis to a line containing the origin (0, 0) and a
+ * point with coordinates (xCoordinate, yCoordinate). The angle is given in radians between
+ * -pi and pi, excluding -pi.
+ *
+ * Note that the Excel ATAN2() function accepts its arguments in the reverse order to the standard
+ * PHP atan2() function, so we need to reverse them here before calling the PHP atan() function.
+ *
+ * Excel Function:
+ * ATAN2(xCoordinate,yCoordinate)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param float $xCoordinate the x-coordinate of the point
+ * @param float $yCoordinate the y-coordinate of the point
+ *
+ * @return float the inverse tangent of the specified x- and y-coordinates
+ */
+ public static function ATAN2($xCoordinate = null, $yCoordinate = null)
+ {
+ $xCoordinate = Functions::flattenSingleValue($xCoordinate);
+ $yCoordinate = Functions::flattenSingleValue($yCoordinate);
+
+ $xCoordinate = ($xCoordinate !== null) ? $xCoordinate : 0.0;
+ $yCoordinate = ($yCoordinate !== null) ? $yCoordinate : 0.0;
+
+ if (((is_numeric($xCoordinate)) || (is_bool($xCoordinate))) &&
+ ((is_numeric($yCoordinate))) || (is_bool($yCoordinate))) {
+ $xCoordinate = (float) $xCoordinate;
+ $yCoordinate = (float) $yCoordinate;
+
+ if (($xCoordinate == 0) && ($yCoordinate == 0)) {
+ return Functions::DIV0();
+ }
+
+ return atan2($yCoordinate, $xCoordinate);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * CEILING.
+ *
+ * Returns number rounded up, away from zero, to the nearest multiple of significance.
+ * For example, if you want to avoid using pennies in your prices and your product is
+ * priced at $4.42, use the formula =CEILING(4.42,0.05) to round prices up to the
+ * nearest nickel.
+ *
+ * Excel Function:
+ * CEILING(number[,significance])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param float $number the number you want to round
+ * @param float $significance the multiple to which you want to round
+ *
+ * @return float Rounded Number
+ */
+ public static function CEILING($number, $significance = null)
+ {
+ $number = Functions::flattenSingleValue($number);
+ $significance = Functions::flattenSingleValue($significance);
+
+ if (($significance === null) &&
+ (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC)) {
+ $significance = $number / abs($number);
+ }
+
+ if ((is_numeric($number)) && (is_numeric($significance))) {
+ if (($number == 0.0) || ($significance == 0.0)) {
+ return 0.0;
+ } elseif (self::SIGN($number) == self::SIGN($significance)) {
+ return ceil($number / $significance) * $significance;
+ }
+
+ return Functions::NAN();
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * COMBIN.
+ *
+ * Returns the number of combinations for a given number of items. Use COMBIN to
+ * determine the total possible number of groups for a given number of items.
+ *
+ * Excel Function:
+ * COMBIN(numObjs,numInSet)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param int $numObjs Number of different objects
+ * @param int $numInSet Number of objects in each combination
+ *
+ * @return int Number of combinations
+ */
+ public static function COMBIN($numObjs, $numInSet)
+ {
+ $numObjs = Functions::flattenSingleValue($numObjs);
+ $numInSet = Functions::flattenSingleValue($numInSet);
+
+ if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
+ if ($numObjs < $numInSet) {
+ return Functions::NAN();
+ } elseif ($numInSet < 0) {
+ return Functions::NAN();
+ }
+
+ return round(self::FACT($numObjs) / self::FACT($numObjs - $numInSet)) / self::FACT($numInSet);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * EVEN.
+ *
+ * Returns number rounded up to the nearest even integer.
+ * You can use this function for processing items that come in twos. For example,
+ * a packing crate accepts rows of one or two items. The crate is full when
+ * the number of items, rounded up to the nearest two, matches the crate's
+ * capacity.
+ *
+ * Excel Function:
+ * EVEN(number)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param float $number Number to round
+ *
+ * @return int Rounded Number
+ */
+ public static function EVEN($number)
+ {
+ $number = Functions::flattenSingleValue($number);
+
+ if ($number === null) {
+ return 0;
+ } elseif (is_bool($number)) {
+ $number = (int) $number;
+ }
+
+ if (is_numeric($number)) {
+ $significance = 2 * self::SIGN($number);
+
+ return (int) self::CEILING($number, $significance);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * FACT.
+ *
+ * Returns the factorial of a number.
+ * The factorial of a number is equal to 1*2*3*...* number.
+ *
+ * Excel Function:
+ * FACT(factVal)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param float $factVal Factorial Value
+ *
+ * @return int Factorial
+ */
+ public static function FACT($factVal)
+ {
+ $factVal = Functions::flattenSingleValue($factVal);
+
+ if (is_numeric($factVal)) {
+ if ($factVal < 0) {
+ return Functions::NAN();
+ }
+ $factLoop = floor($factVal);
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
+ if ($factVal > $factLoop) {
+ return Functions::NAN();
+ }
+ }
+
+ $factorial = 1;
+ while ($factLoop > 1) {
+ $factorial *= $factLoop--;
+ }
+
+ return $factorial;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * FACTDOUBLE.
+ *
+ * Returns the double factorial of a number.
+ *
+ * Excel Function:
+ * FACTDOUBLE(factVal)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param float $factVal Factorial Value
+ *
+ * @return int Double Factorial
+ */
+ public static function FACTDOUBLE($factVal)
+ {
+ $factLoop = Functions::flattenSingleValue($factVal);
+
+ if (is_numeric($factLoop)) {
+ $factLoop = floor($factLoop);
+ if ($factVal < 0) {
+ return Functions::NAN();
+ }
+ $factorial = 1;
+ while ($factLoop > 1) {
+ $factorial *= $factLoop--;
+ --$factLoop;
+ }
+
+ return $factorial;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * FLOOR.
+ *
+ * Rounds number down, toward zero, to the nearest multiple of significance.
+ *
+ * Excel Function:
+ * FLOOR(number[,significance])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param float $number Number to round
+ * @param float $significance Significance
+ *
+ * @return float Rounded Number
+ */
+ public static function FLOOR($number, $significance = null)
+ {
+ $number = Functions::flattenSingleValue($number);
+ $significance = Functions::flattenSingleValue($significance);
+
+ if (($significance === null) &&
+ (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC)) {
+ $significance = $number / abs($number);
+ }
+
+ if ((is_numeric($number)) && (is_numeric($significance))) {
+ if ($significance == 0.0) {
+ return Functions::DIV0();
+ } elseif ($number == 0.0) {
+ return 0.0;
+ } elseif (self::SIGN($number) == self::SIGN($significance)) {
+ return floor($number / $significance) * $significance;
+ }
+
+ return Functions::NAN();
+ }
+
+ return Functions::VALUE();
+ }
+
+ private static function evaluateGCD($a, $b)
+ {
+ return $b ? self::evaluateGCD($b, $a % $b) : $a;
+ }
+
+ /**
+ * GCD.
+ *
+ * Returns the greatest common divisor of a series of numbers.
+ * The greatest common divisor is the largest integer that divides both
+ * number1 and number2 without a remainder.
+ *
+ * Excel Function:
+ * GCD(number1[,number2[, ...]])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param mixed ...$args Data values
+ *
+ * @return int Greatest Common Divisor
+ */
+ public static function GCD(...$args)
+ {
+ $args = Functions::flattenArray($args);
+ // Loop through arguments
+ foreach (Functions::flattenArray($args) as $value) {
+ if (!is_numeric($value)) {
+ return Functions::VALUE();
+ } elseif ($value < 0) {
+ return Functions::NAN();
+ }
+ }
+
+ $gcd = (int) array_pop($args);
+ do {
+ $gcd = self::evaluateGCD($gcd, (int) array_pop($args));
+ } while (!empty($args));
+
+ return $gcd;
+ }
+
+ /**
+ * INT.
+ *
+ * Casts a floating point value to an integer
+ *
+ * Excel Function:
+ * INT(number)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param float $number Number to cast to an integer
+ *
+ * @return int Integer value
+ */
+ public static function INT($number)
+ {
+ $number = Functions::flattenSingleValue($number);
+
+ if ($number === null) {
+ return 0;
+ } elseif (is_bool($number)) {
+ return (int) $number;
+ }
+ if (is_numeric($number)) {
+ return (int) floor($number);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * LCM.
+ *
+ * Returns the lowest common multiplier of a series of numbers
+ * The least common multiple is the smallest positive integer that is a multiple
+ * of all integer arguments number1, number2, and so on. Use LCM to add fractions
+ * with different denominators.
+ *
+ * Excel Function:
+ * LCM(number1[,number2[, ...]])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param mixed ...$args Data values
+ *
+ * @return int Lowest Common Multiplier
+ */
+ public static function LCM(...$args)
+ {
+ $returnValue = 1;
+ $allPoweredFactors = [];
+ // Loop through arguments
+ foreach (Functions::flattenArray($args) as $value) {
+ if (!is_numeric($value)) {
+ return Functions::VALUE();
+ }
+ if ($value == 0) {
+ return 0;
+ } elseif ($value < 0) {
+ return Functions::NAN();
+ }
+ $myFactors = self::factors(floor($value));
+ $myCountedFactors = array_count_values($myFactors);
+ $myPoweredFactors = [];
+ foreach ($myCountedFactors as $myCountedFactor => $myCountedPower) {
+ $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor, $myCountedPower);
+ }
+ foreach ($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
+ if (isset($allPoweredFactors[$myPoweredValue])) {
+ if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
+ $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
+ }
+ } else {
+ $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
+ }
+ }
+ }
+ foreach ($allPoweredFactors as $allPoweredFactor) {
+ $returnValue *= (int) $allPoweredFactor;
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * LOG_BASE.
+ *
+ * Returns the logarithm of a number to a specified base. The default base is 10.
+ *
+ * Excel Function:
+ * LOG(number[,base])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param float $number The positive real number for which you want the logarithm
+ * @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10.
+ *
+ * @return float
+ */
+ public static function logBase($number = null, $base = 10)
+ {
+ $number = Functions::flattenSingleValue($number);
+ $base = ($base === null) ? 10 : (float) Functions::flattenSingleValue($base);
+
+ if ((!is_numeric($base)) || (!is_numeric($number))) {
+ return Functions::VALUE();
+ }
+ if (($base <= 0) || ($number <= 0)) {
+ return Functions::NAN();
+ }
+
+ return log($number, $base);
+ }
+
+ /**
+ * MDETERM.
+ *
+ * Returns the matrix determinant of an array.
+ *
+ * Excel Function:
+ * MDETERM(array)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param array $matrixValues A matrix of values
+ *
+ * @return float
+ */
+ public static function MDETERM($matrixValues)
+ {
+ $matrixData = [];
+ if (!is_array($matrixValues)) {
+ $matrixValues = [[$matrixValues]];
+ }
+
+ $row = $maxColumn = 0;
+ foreach ($matrixValues as $matrixRow) {
+ if (!is_array($matrixRow)) {
+ $matrixRow = [$matrixRow];
+ }
+ $column = 0;
+ foreach ($matrixRow as $matrixCell) {
+ if ((is_string($matrixCell)) || ($matrixCell === null)) {
+ return Functions::VALUE();
+ }
+ $matrixData[$row][$column] = $matrixCell;
+ ++$column;
+ }
+ if ($column > $maxColumn) {
+ $maxColumn = $column;
+ }
+ ++$row;
+ }
+
+ $matrix = new Matrix($matrixData);
+ if (!$matrix->isSquare()) {
+ return Functions::VALUE();
+ }
+
+ try {
+ return $matrix->determinant();
+ } catch (MatrixException $ex) {
+ return Functions::VALUE();
+ }
+ }
+
+ /**
+ * MINVERSE.
+ *
+ * Returns the inverse matrix for the matrix stored in an array.
+ *
+ * Excel Function:
+ * MINVERSE(array)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param array $matrixValues A matrix of values
+ *
+ * @return array
+ */
+ public static function MINVERSE($matrixValues)
+ {
+ $matrixData = [];
+ if (!is_array($matrixValues)) {
+ $matrixValues = [[$matrixValues]];
+ }
+
+ $row = $maxColumn = 0;
+ foreach ($matrixValues as $matrixRow) {
+ if (!is_array($matrixRow)) {
+ $matrixRow = [$matrixRow];
+ }
+ $column = 0;
+ foreach ($matrixRow as $matrixCell) {
+ if ((is_string($matrixCell)) || ($matrixCell === null)) {
+ return Functions::VALUE();
+ }
+ $matrixData[$row][$column] = $matrixCell;
+ ++$column;
+ }
+ if ($column > $maxColumn) {
+ $maxColumn = $column;
+ }
+ ++$row;
+ }
+
+ $matrix = new Matrix($matrixData);
+ if (!$matrix->isSquare()) {
+ return Functions::VALUE();
+ }
+
+ if ($matrix->determinant() == 0.0) {
+ return Functions::NAN();
+ }
+
+ try {
+ return $matrix->inverse()->toArray();
+ } catch (MatrixException $ex) {
+ return Functions::VALUE();
+ }
+ }
+
+ /**
+ * MMULT.
+ *
+ * @param array $matrixData1 A matrix of values
+ * @param array $matrixData2 A matrix of values
+ *
+ * @return array
+ */
+ public static function MMULT($matrixData1, $matrixData2)
+ {
+ $matrixAData = $matrixBData = [];
+ if (!is_array($matrixData1)) {
+ $matrixData1 = [[$matrixData1]];
+ }
+ if (!is_array($matrixData2)) {
+ $matrixData2 = [[$matrixData2]];
+ }
+
+ try {
+ $rowA = 0;
+ foreach ($matrixData1 as $matrixRow) {
+ if (!is_array($matrixRow)) {
+ $matrixRow = [$matrixRow];
+ }
+ $columnA = 0;
+ foreach ($matrixRow as $matrixCell) {
+ if ((!is_numeric($matrixCell)) || ($matrixCell === null)) {
+ return Functions::VALUE();
+ }
+ $matrixAData[$rowA][$columnA] = $matrixCell;
+ ++$columnA;
+ }
+ ++$rowA;
+ }
+ $matrixA = new Matrix($matrixAData);
+ $rowB = 0;
+ foreach ($matrixData2 as $matrixRow) {
+ if (!is_array($matrixRow)) {
+ $matrixRow = [$matrixRow];
+ }
+ $columnB = 0;
+ foreach ($matrixRow as $matrixCell) {
+ if ((!is_numeric($matrixCell)) || ($matrixCell === null)) {
+ return Functions::VALUE();
+ }
+ $matrixBData[$rowB][$columnB] = $matrixCell;
+ ++$columnB;
+ }
+ ++$rowB;
+ }
+ $matrixB = new Matrix($matrixBData);
+
+ if ($columnA != $rowB) {
+ return Functions::VALUE();
+ }
+
+ return $matrixA->multiply($matrixB)->toArray();
+ } catch (MatrixException $ex) {
+ return Functions::VALUE();
+ }
+ }
+
+ /**
+ * MOD.
+ *
+ * @param int $a Dividend
+ * @param int $b Divisor
+ *
+ * @return int Remainder
+ */
+ public static function MOD($a = 1, $b = 1)
+ {
+ $a = (float) Functions::flattenSingleValue($a);
+ $b = (float) Functions::flattenSingleValue($b);
+
+ if ($b == 0.0) {
+ return Functions::DIV0();
+ } elseif (($a < 0.0) && ($b > 0.0)) {
+ return $b - fmod(abs($a), $b);
+ } elseif (($a > 0.0) && ($b < 0.0)) {
+ return $b + fmod($a, abs($b));
+ }
+
+ return fmod($a, $b);
+ }
+
+ /**
+ * MROUND.
+ *
+ * Rounds a number to the nearest multiple of a specified value
+ *
+ * @param float $number Number to round
+ * @param int $multiple Multiple to which you want to round $number
+ *
+ * @return float Rounded Number
+ */
+ public static function MROUND($number, $multiple)
+ {
+ $number = Functions::flattenSingleValue($number);
+ $multiple = Functions::flattenSingleValue($multiple);
+
+ if ((is_numeric($number)) && (is_numeric($multiple))) {
+ if ($multiple == 0) {
+ return 0;
+ }
+ if ((self::SIGN($number)) == (self::SIGN($multiple))) {
+ $multiplier = 1 / $multiple;
+
+ return round($number * $multiplier) / $multiplier;
+ }
+
+ return Functions::NAN();
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * MULTINOMIAL.
+ *
+ * Returns the ratio of the factorial of a sum of values to the product of factorials.
+ *
+ * @param array of mixed Data Series
+ *
+ * @return float
+ */
+ public static function MULTINOMIAL(...$args)
+ {
+ $summer = 0;
+ $divisor = 1;
+ // Loop through arguments
+ foreach (Functions::flattenArray($args) as $arg) {
+ // Is it a numeric value?
+ if (is_numeric($arg)) {
+ if ($arg < 1) {
+ return Functions::NAN();
+ }
+ $summer += floor($arg);
+ $divisor *= self::FACT($arg);
+ } else {
+ return Functions::VALUE();
+ }
+ }
+
+ // Return
+ if ($summer > 0) {
+ $summer = self::FACT($summer);
+
+ return $summer / $divisor;
+ }
+
+ return 0;
+ }
+
+ /**
+ * ODD.
+ *
+ * Returns number rounded up to the nearest odd integer.
+ *
+ * @param float $number Number to round
+ *
+ * @return int Rounded Number
+ */
+ public static function ODD($number)
+ {
+ $number = Functions::flattenSingleValue($number);
+
+ if ($number === null) {
+ return 1;
+ } elseif (is_bool($number)) {
+ return 1;
+ } elseif (is_numeric($number)) {
+ $significance = self::SIGN($number);
+ if ($significance == 0) {
+ return 1;
+ }
+
+ $result = self::CEILING($number, $significance);
+ if ($result == self::EVEN($result)) {
+ $result += $significance;
+ }
+
+ return (int) $result;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * POWER.
+ *
+ * Computes x raised to the power y.
+ *
+ * @param float $x
+ * @param float $y
+ *
+ * @return float
+ */
+ public static function POWER($x = 0, $y = 2)
+ {
+ $x = Functions::flattenSingleValue($x);
+ $y = Functions::flattenSingleValue($y);
+
+ // Validate parameters
+ if ($x == 0.0 && $y == 0.0) {
+ return Functions::NAN();
+ } elseif ($x == 0.0 && $y < 0.0) {
+ return Functions::DIV0();
+ }
+
+ // Return
+ $result = pow($x, $y);
+
+ return (!is_nan($result) && !is_infinite($result)) ? $result : Functions::NAN();
+ }
+
+ /**
+ * PRODUCT.
+ *
+ * PRODUCT returns the product of all the values and cells referenced in the argument list.
+ *
+ * Excel Function:
+ * PRODUCT(value1[,value2[, ...]])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param mixed ...$args Data values
+ *
+ * @return float
+ */
+ public static function PRODUCT(...$args)
+ {
+ // Return value
+ $returnValue = null;
+
+ // Loop through arguments
+ foreach (Functions::flattenArray($args) as $arg) {
+ // Is it a numeric value?
+ if ((is_numeric($arg)) && (!is_string($arg))) {
+ if ($returnValue === null) {
+ $returnValue = $arg;
+ } else {
+ $returnValue *= $arg;
+ }
+ }
+ }
+
+ // Return
+ if ($returnValue === null) {
+ return 0;
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * QUOTIENT.
+ *
+ * QUOTIENT function returns the integer portion of a division. Numerator is the divided number
+ * and denominator is the divisor.
+ *
+ * Excel Function:
+ * QUOTIENT(value1[,value2[, ...]])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param mixed ...$args Data values
+ *
+ * @return float
+ */
+ public static function QUOTIENT(...$args)
+ {
+ // Return value
+ $returnValue = null;
+
+ // Loop through arguments
+ foreach (Functions::flattenArray($args) as $arg) {
+ // Is it a numeric value?
+ if ((is_numeric($arg)) && (!is_string($arg))) {
+ if ($returnValue === null) {
+ $returnValue = ($arg == 0) ? 0 : $arg;
+ } else {
+ if (($returnValue == 0) || ($arg == 0)) {
+ $returnValue = 0;
+ } else {
+ $returnValue /= $arg;
+ }
+ }
+ }
+ }
+
+ // Return
+ return (int) $returnValue;
+ }
+
+ /**
+ * RAND.
+ *
+ * @param int $min Minimal value
+ * @param int $max Maximal value
+ *
+ * @return int Random number
+ */
+ public static function RAND($min = 0, $max = 0)
+ {
+ $min = Functions::flattenSingleValue($min);
+ $max = Functions::flattenSingleValue($max);
+
+ if ($min == 0 && $max == 0) {
+ return (mt_rand(0, 10000000)) / 10000000;
+ }
+
+ return mt_rand($min, $max);
+ }
+
+ public static function ROMAN($aValue, $style = 0)
+ {
+ $aValue = Functions::flattenSingleValue($aValue);
+ $style = ($style === null) ? 0 : (int) Functions::flattenSingleValue($style);
+ if ((!is_numeric($aValue)) || ($aValue < 0) || ($aValue >= 4000)) {
+ return Functions::VALUE();
+ }
+ $aValue = (int) $aValue;
+ if ($aValue == 0) {
+ return '';
+ }
+
+ $mill = ['', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM'];
+ $cent = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'];
+ $tens = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'];
+ $ones = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
+
+ $roman = '';
+ while ($aValue > 5999) {
+ $roman .= 'M';
+ $aValue -= 1000;
+ }
+ $m = self::romanCut($aValue, 1000);
+ $aValue %= 1000;
+ $c = self::romanCut($aValue, 100);
+ $aValue %= 100;
+ $t = self::romanCut($aValue, 10);
+ $aValue %= 10;
+
+ return $roman . $mill[$m] . $cent[$c] . $tens[$t] . $ones[$aValue];
+ }
+
+ /**
+ * ROUNDUP.
+ *
+ * Rounds a number up to a specified number of decimal places
+ *
+ * @param float $number Number to round
+ * @param int $digits Number of digits to which you want to round $number
+ *
+ * @return float Rounded Number
+ */
+ public static function ROUNDUP($number, $digits)
+ {
+ $number = Functions::flattenSingleValue($number);
+ $digits = Functions::flattenSingleValue($digits);
+
+ if ((is_numeric($number)) && (is_numeric($digits))) {
+ $significance = pow(10, (int) $digits);
+ if ($number < 0.0) {
+ return floor($number * $significance) / $significance;
+ }
+
+ return ceil($number * $significance) / $significance;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * ROUNDDOWN.
+ *
+ * Rounds a number down to a specified number of decimal places
+ *
+ * @param float $number Number to round
+ * @param int $digits Number of digits to which you want to round $number
+ *
+ * @return float Rounded Number
+ */
+ public static function ROUNDDOWN($number, $digits)
+ {
+ $number = Functions::flattenSingleValue($number);
+ $digits = Functions::flattenSingleValue($digits);
+
+ if ((is_numeric($number)) && (is_numeric($digits))) {
+ $significance = pow(10, (int) $digits);
+ if ($number < 0.0) {
+ return ceil($number * $significance) / $significance;
+ }
+
+ return floor($number * $significance) / $significance;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * SERIESSUM.
+ *
+ * Returns the sum of a power series
+ *
+ * @param float $x Input value to the power series
+ * @param float $n Initial power to which you want to raise $x
+ * @param float $m Step by which to increase $n for each term in the series
+ * @param array of mixed Data Series
+ *
+ * @return float
+ */
+ public static function SERIESSUM(...$args)
+ {
+ $returnValue = 0;
+
+ // Loop through arguments
+ $aArgs = Functions::flattenArray($args);
+
+ $x = array_shift($aArgs);
+ $n = array_shift($aArgs);
+ $m = array_shift($aArgs);
+
+ if ((is_numeric($x)) && (is_numeric($n)) && (is_numeric($m))) {
+ // Calculate
+ $i = 0;
+ foreach ($aArgs as $arg) {
+ // Is it a numeric value?
+ if ((is_numeric($arg)) && (!is_string($arg))) {
+ $returnValue += $arg * pow($x, $n + ($m * $i++));
+ } else {
+ return Functions::VALUE();
+ }
+ }
+
+ return $returnValue;
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * SIGN.
+ *
+ * Determines the sign of a number. Returns 1 if the number is positive, zero (0)
+ * if the number is 0, and -1 if the number is negative.
+ *
+ * @param float $number Number to round
+ *
+ * @return int sign value
+ */
+ public static function SIGN($number)
+ {
+ $number = Functions::flattenSingleValue($number);
+
+ if (is_bool($number)) {
+ return (int) $number;
+ }
+ if (is_numeric($number)) {
+ if ($number == 0.0) {
+ return 0;
+ }
+
+ return $number / abs($number);
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * SQRTPI.
+ *
+ * Returns the square root of (number * pi).
+ *
+ * @param float $number Number
+ *
+ * @return float Square Root of Number * Pi
+ */
+ public static function SQRTPI($number)
+ {
+ $number = Functions::flattenSingleValue($number);
+
+ if (is_numeric($number)) {
+ if ($number < 0) {
+ return Functions::NAN();
+ }
+
+ return sqrt($number * M_PI);
+ }
+
+ return Functions::VALUE();
+ }
+
+ protected static function filterHiddenArgs($cellReference, $args)
+ {
+ return array_filter(
+ $args,
+ function ($index) use ($cellReference) {
+ list(, $row, $column) = explode('.', $index);
+
+ return $cellReference->getWorksheet()->getRowDimension($row)->getVisible() &&
+ $cellReference->getWorksheet()->getColumnDimension($column)->getVisible();
+ },
+ ARRAY_FILTER_USE_KEY
+ );
+ }
+
+ protected static function filterFormulaArgs($cellReference, $args)
+ {
+ return array_filter(
+ $args,
+ function ($index) use ($cellReference) {
+ list(, $row, $column) = explode('.', $index);
+ if ($cellReference->getWorksheet()->cellExists($column . $row)) {
+ //take this cell out if it contains the SUBTOTAL or AGGREGATE functions in a formula
+ $isFormula = $cellReference->getWorksheet()->getCell($column . $row)->isFormula();
+ $cellFormula = !preg_match('/^=.*\b(SUBTOTAL|AGGREGATE)\s*\(/i', $cellReference->getWorksheet()->getCell($column . $row)->getValue());
+
+ return !$isFormula || $cellFormula;
+ }
+
+ return true;
+ },
+ ARRAY_FILTER_USE_KEY
+ );
+ }
+
+ /**
+ * SUBTOTAL.
+ *
+ * Returns a subtotal in a list or database.
+ *
+ * @param int the number 1 to 11 that specifies which function to
+ * use in calculating subtotals within a range
+ * list
+ * Numbers 101 to 111 shadow the functions of 1 to 11
+ * but ignore any values in the range that are
+ * in hidden rows or columns
+ * @param array of mixed Data Series
+ *
+ * @return float
+ */
+ public static function SUBTOTAL(...$args)
+ {
+ $cellReference = array_pop($args);
+ $aArgs = Functions::flattenArrayIndexed($args);
+ $subtotal = array_shift($aArgs);
+
+ // Calculate
+ if ((is_numeric($subtotal)) && (!is_string($subtotal))) {
+ if ($subtotal > 100) {
+ $aArgs = self::filterHiddenArgs($cellReference, $aArgs);
+ $subtotal -= 100;
+ }
+
+ $aArgs = self::filterFormulaArgs($cellReference, $aArgs);
+ switch ($subtotal) {
+ case 1:
+ return Statistical::AVERAGE($aArgs);
+ case 2:
+ return Statistical::COUNT($aArgs);
+ case 3:
+ return Statistical::COUNTA($aArgs);
+ case 4:
+ return Statistical::MAX($aArgs);
+ case 5:
+ return Statistical::MIN($aArgs);
+ case 6:
+ return self::PRODUCT($aArgs);
+ case 7:
+ return Statistical::STDEV($aArgs);
+ case 8:
+ return Statistical::STDEVP($aArgs);
+ case 9:
+ return self::SUM($aArgs);
+ case 10:
+ return Statistical::VARFunc($aArgs);
+ case 11:
+ return Statistical::VARP($aArgs);
+ }
+ }
+
+ return Functions::VALUE();
+ }
+
+ /**
+ * SUM.
+ *
+ * SUM computes the sum of all the values and cells referenced in the argument list.
+ *
+ * Excel Function:
+ * SUM(value1[,value2[, ...]])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param mixed ...$args Data values
+ *
+ * @return float
+ */
+ public static function SUM(...$args)
+ {
+ $returnValue = 0;
+
+ // Loop through the arguments
+ foreach (Functions::flattenArray($args) as $arg) {
+ // Is it a numeric value?
+ if ((is_numeric($arg)) && (!is_string($arg))) {
+ $returnValue += $arg;
+ }
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * SUMIF.
+ *
+ * Counts the number of cells that contain numbers within the list of arguments
+ *
+ * Excel Function:
+ * SUMIF(value1[,value2[, ...]],condition)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param mixed $aArgs Data values
+ * @param string $condition the criteria that defines which cells will be summed
+ * @param mixed $sumArgs
+ *
+ * @return float
+ */
+ public static function SUMIF($aArgs, $condition, $sumArgs = [])
+ {
+ $returnValue = 0;
+
+ $aArgs = Functions::flattenArray($aArgs);
+ $sumArgs = Functions::flattenArray($sumArgs);
+ if (empty($sumArgs)) {
+ $sumArgs = $aArgs;
+ }
+ $condition = Functions::ifCondition($condition);
+ // Loop through arguments
+ foreach ($aArgs as $key => $arg) {
+ if (!is_numeric($arg)) {
+ $arg = str_replace('"', '""', $arg);
+ $arg = Calculation::wrapResult(strtoupper($arg));
+ }
+
+ $testCondition = '=' . $arg . $condition;
+
+ if (is_numeric($sumArgs[$key]) &&
+ Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
+ // Is it a value within our criteria and only numeric can be added to the result
+ $returnValue += $sumArgs[$key];
+ }
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * SUMIFS.
+ *
+ * Counts the number of cells that contain numbers within the list of arguments
+ *
+ * Excel Function:
+ * SUMIFS(value1[,value2[, ...]],condition)
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param mixed $args Data values
+ * @param string $condition the criteria that defines which cells will be summed
+ *
+ * @return float
+ */
+ public static function SUMIFS(...$args)
+ {
+ $arrayList = $args;
+
+ // Return value
+ $returnValue = 0;
+
+ $sumArgs = Functions::flattenArray(array_shift($arrayList));
+ $aArgsArray = [];
+ $conditions = [];
+
+ while (count($arrayList) > 0) {
+ $aArgsArray[] = Functions::flattenArray(array_shift($arrayList));
+ $conditions[] = Functions::ifCondition(array_shift($arrayList));
+ }
+
+ // Loop through each sum and see if arguments and conditions are true
+ foreach ($sumArgs as $index => $value) {
+ $valid = true;
+
+ foreach ($conditions as $cidx => $condition) {
+ $arg = $aArgsArray[$cidx][$index];
+
+ // Loop through arguments
+ if (!is_numeric($arg)) {
+ $arg = Calculation::wrapResult(strtoupper($arg));
+ }
+ $testCondition = '=' . $arg . $condition;
+ if (!Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
+ // Is not a value within our criteria
+ $valid = false;
+
+ break; // if false found, don't need to check other conditions
+ }
+ }
+
+ if ($valid) {
+ $returnValue += $value;
+ }
+ }
+
+ // Return
+ return $returnValue;
+ }
+
+ /**
+ * SUMPRODUCT.
+ *
+ * Excel Function:
+ * SUMPRODUCT(value1[,value2[, ...]])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param mixed ...$args Data values
+ *
+ * @return float
+ */
+ public static function SUMPRODUCT(...$args)
+ {
+ $arrayList = $args;
+
+ $wrkArray = Functions::flattenArray(array_shift($arrayList));
+ $wrkCellCount = count($wrkArray);
+
+ for ($i = 0; $i < $wrkCellCount; ++$i) {
+ if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
+ $wrkArray[$i] = 0;
+ }
+ }
+
+ foreach ($arrayList as $matrixData) {
+ $array2 = Functions::flattenArray($matrixData);
+ $count = count($array2);
+ if ($wrkCellCount != $count) {
+ return Functions::VALUE();
+ }
+
+ foreach ($array2 as $i => $val) {
+ if ((!is_numeric($val)) || (is_string($val))) {
+ $val = 0;
+ }
+ $wrkArray[$i] *= $val;
+ }
+ }
+
+ return array_sum($wrkArray);
+ }
+
+ /**
+ * SUMSQ.
+ *
+ * SUMSQ returns the sum of the squares of the arguments
+ *
+ * Excel Function:
+ * SUMSQ(value1[,value2[, ...]])
+ *
+ * @category Mathematical and Trigonometric Functions
+ *
+ * @param mixed ...$args Data values
+ *
+ * @return float
+ */
+ public static function SUMSQ(...$args)
+ {
+ $returnValue = 0;
+
+ // Loop through arguments
+ foreach (Functions::flattenArray($args) as $arg) {
+ // Is it a numeric value?
+ if ((is_numeric($arg)) && (!is_string($arg))) {
+ $returnValue += ($arg * $arg);
+ }
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * SUMX2MY2.
+ *
+ * @param mixed[] $matrixData1 Matrix #1
+ * @param mixed[] $matrixData2 Matrix #2
+ *
+ * @return float
+ */
+ public static function SUMX2MY2($matrixData1, $matrixData2)
+ {
+ $array1 = Functions::flattenArray($matrixData1);
+ $array2 = Functions::flattenArray($matrixData2);
+ $count = min(count($array1), count($array2));
+
+ $result = 0;
+ for ($i = 0; $i < $count; ++$i) {
+ if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
+ ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
+ $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * SUMX2PY2.
+ *
+ * @param mixed[] $matrixData1 Matrix #1
+ * @param mixed[] $matrixData2 Matrix #2
+ *
+ * @return float
+ */
+ public static function SUMX2PY2($matrixData1, $matrixData2)
+ {
+ $array1 = Functions::flattenArray($matrixData1);
+ $array2 = Functions::flattenArray($matrixData2);
+ $count = min(count($array1), count($array2));
+
+ $result = 0;
+ for ($i = 0; $i < $count; ++$i) {
+ if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
+ ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
+ $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * SUMXMY2.
+ *
+ * @param mixed[] $matrixData1 Matrix #1
+ * @param mixed[] $matrixData2 Matrix #2
+ *
+ * @return float
+ */
+ public static function SUMXMY2($matrixData1, $matrixData2)
+ {
+ $array1 = Functions::flattenArray($matrixData1);
+ $array2 = Functions::flattenArray($matrixData2);
+ $count = min(count($array1), count($array2));
+
+ $result = 0;
+ for ($i = 0; $i < $count; ++$i) {
+ if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
+ ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
+ $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * TRUNC.
+ *
+ * Truncates value to the number of fractional digits by number_digits.
+ *
+ * @param float $value
+ * @param int $digits
+ *
+ * @return float Truncated value
+ */
+ public static function TRUNC($value = 0, $digits = 0)
+ {
+ $value = Functions::flattenSingleValue($value);
+ $digits = Functions::flattenSingleValue($digits);
+
+ // Validate parameters
+ if ((!is_numeric($value)) || (!is_numeric($digits))) {
+ return Functions::VALUE();
+ }
+ $digits = floor($digits);
+
+ // Truncate
+ $adjust = pow(10, $digits);
+
+ if (($digits > 0) && (rtrim((int) ((abs($value) - abs((int) $value)) * $adjust), '0') < $adjust / 10)) {
+ return $value;
+ }
+
+ return ((int) ($value * $adjust)) / $adjust;
+ }
+
+ /**
+ * SEC.
+ *
+ * Returns the secant of an angle.
+ *
+ * @param float $angle Number
+ *
+ * @return float|string The secant of the angle
+ */
+ public static function SEC($angle)
+ {
+ $angle = Functions::flattenSingleValue($angle);
+
+ if (!is_numeric($angle)) {
+ return Functions::VALUE();
+ }
+
+ $result = cos($angle);
+
+ return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
+ }
+
+ /**
+ * SECH.
+ *
+ * Returns the hyperbolic secant of an angle.
+ *
+ * @param float $angle Number
+ *
+ * @return float|string The hyperbolic secant of the angle
+ */
+ public static function SECH($angle)
+ {
+ $angle = Functions::flattenSingleValue($angle);
+
+ if (!is_numeric($angle)) {
+ return Functions::VALUE();
+ }
+
+ $result = cosh($angle);
+
+ return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
+ }
+
+ /**
+ * CSC.
+ *
+ * Returns the cosecant of an angle.
+ *
+ * @param float $angle Number
+ *
+ * @return float|string The cosecant of the angle
+ */
+ public static function CSC($angle)
+ {
+ $angle = Functions::flattenSingleValue($angle);
+
+ if (!is_numeric($angle)) {
+ return Functions::VALUE();
+ }
+
+ $result = sin($angle);
+
+ return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
+ }
+
+ /**
+ * CSCH.
+ *
+ * Returns the hyperbolic cosecant of an angle.
+ *
+ * @param float $angle Number
+ *
+ * @return float|string The hyperbolic cosecant of the angle
+ */
+ public static function CSCH($angle)
+ {
+ $angle = Functions::flattenSingleValue($angle);
+
+ if (!is_numeric($angle)) {
+ return Functions::VALUE();
+ }
+
+ $result = sinh($angle);
+
+ return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
+ }
+
+ /**
+ * COT.
+ *
+ * Returns the cotangent of an angle.
+ *
+ * @param float $angle Number
+ *
+ * @return float|string The cotangent of the angle
+ */
+ public static function COT($angle)
+ {
+ $angle = Functions::flattenSingleValue($angle);
+
+ if (!is_numeric($angle)) {
+ return Functions::VALUE();
+ }
+
+ $result = tan($angle);
+
+ return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
+ }
+
+ /**
+ * COTH.
+ *
+ * Returns the hyperbolic cotangent of an angle.
+ *
+ * @param float $angle Number
+ *
+ * @return float|string The hyperbolic cotangent of the angle
+ */
+ public static function COTH($angle)
+ {
+ $angle = Functions::flattenSingleValue($angle);
+
+ if (!is_numeric($angle)) {
+ return Functions::VALUE();
+ }
+
+ $result = tanh($angle);
+
+ return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
+ }
+
+ /**
+ * ACOT.
+ *
+ * Returns the arccotangent of a number.
+ *
+ * @param float $number Number
+ *
+ * @return float|string The arccotangent of the number
+ */
+ public static function ACOT($number)
+ {
+ $number = Functions::flattenSingleValue($number);
+
+ if (!is_numeric($number)) {
+ return Functions::VALUE();
+ }
+
+ return (M_PI / 2) - atan($number);
+ }
+
+ /**
+ * ACOTH.
+ *
+ * Returns the hyperbolic arccotangent of a number.
+ *
+ * @param float $number Number
+ *
+ * @return float|string The hyperbolic arccotangent of the number
+ */
+ public static function ACOTH($number)
+ {
+ $number = Functions::flattenSingleValue($number);
+
+ if (!is_numeric($number)) {
+ return Functions::VALUE();
+ }
+
+ $result = log(($number + 1) / ($number - 1)) / 2;
+
+ return is_nan($result) ? Functions::NAN() : $result;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Statistical.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Statistical.php
new file mode 100644
index 00000000000..395b46ae7ca
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Calculation/Statistical.php
@@ -0,0 +1,3621 @@
+ $value) {
+ if ((is_bool($value)) || (is_string($value)) || ($value === null)) {
+ unset($array1[$key], $array2[$key]);
+ }
+ }
+ foreach ($array2 as $key => $value) {
+ if ((is_bool($value)) || (is_string($value)) || ($value === null)) {
+ unset($array1[$key], $array2[$key]);
+ }
+ }
+ $array1 = array_merge($array1);
+ $array2 = array_merge($array2);
+
+ return true;
+ }
+
+ /**
+ * Incomplete beta function.
+ *
+ * @author Jaco van Kooten
+ * @author Paul Meagher
+ *
+ * The computation is based on formulas from Numerical Recipes, Chapter 6.4 (W.H. Press et al, 1992).
+ *
+ * @param mixed $x require 0<=x<=1
+ * @param mixed $p require p>0
+ * @param mixed $q require q>0
+ *
+ * @return float 0 if x<0, p<=0, q<=0 or p+q>2.55E305 and 1 if x>1 to avoid errors and over/underflow
+ */
+ private static function incompleteBeta($x, $p, $q)
+ {
+ if ($x <= 0.0) {
+ return 0.0;
+ } elseif ($x >= 1.0) {
+ return 1.0;
+ } elseif (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > self::LOG_GAMMA_X_MAX_VALUE)) {
+ return 0.0;
+ }
+ $beta_gam = exp((0 - self::logBeta($p, $q)) + $p * log($x) + $q * log(1.0 - $x));
+ if ($x < ($p + 1.0) / ($p + $q + 2.0)) {
+ return $beta_gam * self::betaFraction($x, $p, $q) / $p;
+ }
+
+ return 1.0 - ($beta_gam * self::betaFraction(1 - $x, $q, $p) / $q);
+ }
+
+ // Function cache for logBeta function
+ private static $logBetaCacheP = 0.0;
+
+ private static $logBetaCacheQ = 0.0;
+
+ private static $logBetaCacheResult = 0.0;
+
+ /**
+ * The natural logarithm of the beta function.
+ *
+ * @param mixed $p require p>0
+ * @param mixed $q require q>0
+ *
+ * @return float 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow
+ *
+ * @author Jaco van Kooten
+ */
+ private static function logBeta($p, $q)
+ {
+ if ($p != self::$logBetaCacheP || $q != self::$logBetaCacheQ) {
+ self::$logBetaCacheP = $p;
+ self::$logBetaCacheQ = $q;
+ if (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > self::LOG_GAMMA_X_MAX_VALUE)) {
+ self::$logBetaCacheResult = 0.0;
+ } else {
+ self::$logBetaCacheResult = self::logGamma($p) + self::logGamma($q) - self::logGamma($p + $q);
+ }
+ }
+
+ return self::$logBetaCacheResult;
+ }
+
+ /**
+ * Evaluates of continued fraction part of incomplete beta function.
+ * Based on an idea from Numerical Recipes (W.H. Press et al, 1992).
+ *
+ * @author Jaco van Kooten
+ *
+ * @param mixed $x
+ * @param mixed $p
+ * @param mixed $q
+ *
+ * @return float
+ */
+ private static function betaFraction($x, $p, $q)
+ {
+ $c = 1.0;
+ $sum_pq = $p + $q;
+ $p_plus = $p + 1.0;
+ $p_minus = $p - 1.0;
+ $h = 1.0 - $sum_pq * $x / $p_plus;
+ if (abs($h) < self::XMININ) {
+ $h = self::XMININ;
+ }
+ $h = 1.0 / $h;
+ $frac = $h;
+ $m = 1;
+ $delta = 0.0;
+ while ($m <= self::MAX_ITERATIONS && abs($delta - 1.0) > Functions::PRECISION) {
+ $m2 = 2 * $m;
+ // even index for d
+ $d = $m * ($q - $m) * $x / (($p_minus + $m2) * ($p + $m2));
+ $h = 1.0 + $d * $h;
+ if (abs($h) < self::XMININ) {
+ $h = self::XMININ;
+ }
+ $h = 1.0 / $h;
+ $c = 1.0 + $d / $c;
+ if (abs($c) < self::XMININ) {
+ $c = self::XMININ;
+ }
+ $frac *= $h * $c;
+ // odd index for d
+ $d = -($p + $m) * ($sum_pq + $m) * $x / (($p + $m2) * ($p_plus + $m2));
+ $h = 1.0 + $d * $h;
+ if (abs($h) < self::XMININ) {
+ $h = self::XMININ;
+ }
+ $h = 1.0 / $h;
+ $c = 1.0 + $d / $c;
+ if (abs($c) < self::XMININ) {
+ $c = self::XMININ;
+ }
+ $delta = $h * $c;
+ $frac *= $delta;
+ ++$m;
+ }
+
+ return $frac;
+ }
+
+ /**
+ * logGamma function.
+ *
+ * @version 1.1
+ *
+ * @author Jaco van Kooten
+ *
+ * Original author was Jaco van Kooten. Ported to PHP by Paul Meagher.
+ *
+ * The natural logarithm of the gamma function. | + * Based on public domain NETLIB (Fortran) code by W. J. Cody and L. Stoltz + * Applied Mathematics Division + * Argonne National Laboratory + * Argonne, IL 60439 + * + * References: + *
+ * From the original documentation: + * + *+ * This routine calculates the LOG(GAMMA) function for a positive real argument X. + * Computation is based on an algorithm outlined in references 1 and 2. + * The program uses rational functions that theoretically approximate LOG(GAMMA) + * to at least 18 significant decimal digits. The approximation for X > 12 is from + * reference 3, while approximations for X < 12.0 are similar to those in reference + * 1, but are unpublished. The accuracy achieved depends on the arithmetic system, + * the compiler, the intrinsic functions, and proper selection of the + * machine-dependent constants. + * + *
+ * Error returns: '; + + return false; + } else { + return $this->renderCombinationChart($groupCount, $dimensions, $outputDestination); + } + } + + switch ($chartType) { + case 'area3DChart': + $dimensions = '3d'; + // no break + case 'areaChart': + $this->renderAreaChart($groupCount, $dimensions); + + break; + case 'bar3DChart': + $dimensions = '3d'; + // no break + case 'barChart': + $this->renderBarChart($groupCount, $dimensions); + + break; + case 'line3DChart': + $dimensions = '3d'; + // no break + case 'lineChart': + $this->renderLineChart($groupCount, $dimensions); + + break; + case 'pie3DChart': + $dimensions = '3d'; + // no break + case 'pieChart': + $this->renderPieChart($groupCount, $dimensions, false, false); + + break; + case 'doughnut3DChart': + $dimensions = '3d'; + // no break + case 'doughnutChart': + $this->renderPieChart($groupCount, $dimensions, true, true); + + break; + case 'scatterChart': + $this->renderScatterChart($groupCount); + + break; + case 'bubbleChart': + $this->renderBubbleChart($groupCount); + + break; + case 'radarChart': + $this->renderRadarChart($groupCount); + + break; + case 'surface3DChart': + $dimensions = '3d'; + // no break + case 'surfaceChart': + $this->renderContourChart($groupCount, $dimensions); + + break; + case 'stockChart': + $this->renderStockChart($groupCount); + + break; + default: + echo $chartType . ' is not yet implemented '; + + return false; + } + $this->renderLegend(); + + $this->graph->Stroke($outputDestination); + + return true; + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Chart/Renderer/PHP Charting Libraries.txt b/htdocs/includes/phpoffice/PhpSpreadsheet/Chart/Renderer/PHP Charting Libraries.txt new file mode 100644 index 00000000000..4abab7a257d --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Chart/Renderer/PHP Charting Libraries.txt @@ -0,0 +1,20 @@ +ChartDirector + https://www.advsofteng.com/cdphp.html + +GraPHPite + http://graphpite.sourceforge.net/ + +JpGraph + http://www.aditus.nu/jpgraph/ + +LibChart + https://naku.dohcrew.com/libchart/pages/introduction/ + +pChart + http://pchart.sourceforge.net/ + +TeeChart + https://www.steema.com/ + +PHPGraphLib + http://www.ebrueggeman.com/phpgraphlib \ No newline at end of file diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Chart/Renderer/Polyfill.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Chart/Renderer/Polyfill.php new file mode 100644 index 00000000000..7fa383944c9 --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Chart/Renderer/Polyfill.php @@ -0,0 +1,9 @@ +caption = $caption; + $this->layout = $layout; + } + + /** + * Get caption. + * + * @return string + */ + public function getCaption() + { + return $this->caption; + } + + /** + * Set caption. + * + * @param string $caption + * + * @return Title + */ + public function setCaption($caption) + { + $this->caption = $caption; + + return $this; + } + + /** + * Get Layout. + * + * @return Layout + */ + public function getLayout() + { + return $this->layout; + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Collection/Cells.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Collection/Cells.php new file mode 100644 index 00000000000..80a43220988 --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Collection/Cells.php @@ -0,0 +1,506 @@ +parent = $parent; + $this->cache = $cache; + $this->cachePrefix = $this->getUniqueID(); + } + + /** + * Return the parent worksheet for this cell collection. + * + * @return Worksheet + */ + public function getParent() + { + return $this->parent; + } + + /** + * Whether the collection holds a cell for the given coordinate. + * + * @param string $pCoord Coordinate of the cell to check + * + * @return bool + */ + public function has($pCoord) + { + if ($pCoord === $this->currentCoordinate) { + return true; + } + + // Check if the requested entry exists in the index + return isset($this->index[$pCoord]); + } + + /** + * Add or update a cell in the collection. + * + * @param Cell $cell Cell to update + * + * @throws PhpSpreadsheetException + * + * @return Cell + */ + public function update(Cell $cell) + { + return $this->add($cell->getCoordinate(), $cell); + } + + /** + * Delete a cell in cache identified by coordinate. + * + * @param string $pCoord Coordinate of the cell to delete + */ + public function delete($pCoord) + { + if ($pCoord === $this->currentCoordinate && $this->currentCell !== null) { + $this->currentCell->detach(); + $this->currentCoordinate = null; + $this->currentCell = null; + $this->currentCellIsDirty = false; + } + + unset($this->index[$pCoord]); + + // Delete the entry from cache + $this->cache->delete($this->cachePrefix . $pCoord); + } + + /** + * Get a list of all cell coordinates currently held in the collection. + * + * @return string[] + */ + public function getCoordinates() + { + return array_keys($this->index); + } + + /** + * Get a sorted list of all cell coordinates currently held in the collection by row and column. + * + * @return string[] + */ + public function getSortedCoordinates() + { + $sortKeys = []; + foreach ($this->getCoordinates() as $coord) { + $column = ''; + $row = 0; + sscanf($coord, '%[A-Z]%d', $column, $row); + $sortKeys[sprintf('%09d%3s', $row, $column)] = $coord; + } + ksort($sortKeys); + + return array_values($sortKeys); + } + + /** + * Get highest worksheet column and highest row that have cell records. + * + * @return array Highest column name and highest row number + */ + public function getHighestRowAndColumn() + { + // Lookup highest column and highest row + $col = ['A' => '1A']; + $row = [1]; + foreach ($this->getCoordinates() as $coord) { + $c = ''; + $r = 0; + sscanf($coord, '%[A-Z]%d', $c, $r); + $row[$r] = $r; + $col[$c] = strlen($c) . $c; + } + + // Determine highest column and row + $highestRow = max($row); + $highestColumn = substr(max($col), 1); + + return [ + 'row' => $highestRow, + 'column' => $highestColumn, + ]; + } + + /** + * Return the cell coordinate of the currently active cell object. + * + * @return string + */ + public function getCurrentCoordinate() + { + return $this->currentCoordinate; + } + + /** + * Return the column coordinate of the currently active cell object. + * + * @return string + */ + public function getCurrentColumn() + { + $column = ''; + $row = 0; + + sscanf($this->currentCoordinate, '%[A-Z]%d', $column, $row); + + return $column; + } + + /** + * Return the row coordinate of the currently active cell object. + * + * @return int + */ + public function getCurrentRow() + { + $column = ''; + $row = 0; + + sscanf($this->currentCoordinate, '%[A-Z]%d', $column, $row); + + return (int) $row; + } + + /** + * Get highest worksheet column. + * + * @param string $row Return the highest column for the specified row, + * or the highest column of any row if no row number is passed + * + * @return string Highest column name + */ + public function getHighestColumn($row = null) + { + if ($row == null) { + $colRow = $this->getHighestRowAndColumn(); + + return $colRow['column']; + } + + $columnList = [1]; + foreach ($this->getCoordinates() as $coord) { + $c = ''; + $r = 0; + + sscanf($coord, '%[A-Z]%d', $c, $r); + if ($r != $row) { + continue; + } + $columnList[] = Coordinate::columnIndexFromString($c); + } + + return Coordinate::stringFromColumnIndex(max($columnList) + 1); + } + + /** + * Get highest worksheet row. + * + * @param string $column Return the highest row for the specified column, + * or the highest row of any column if no column letter is passed + * + * @return int Highest row number + */ + public function getHighestRow($column = null) + { + if ($column == null) { + $colRow = $this->getHighestRowAndColumn(); + + return $colRow['row']; + } + + $rowList = [0]; + foreach ($this->getCoordinates() as $coord) { + $c = ''; + $r = 0; + + sscanf($coord, '%[A-Z]%d', $c, $r); + if ($c != $column) { + continue; + } + $rowList[] = $r; + } + + return max($rowList); + } + + /** + * Generate a unique ID for cache referencing. + * + * @return string Unique Reference + */ + private function getUniqueID() + { + return uniqid('phpspreadsheet.', true) . '.'; + } + + /** + * Clone the cell collection. + * + * @param Worksheet $parent The new worksheet that we're copying to + * + * @return self + */ + public function cloneCellCollection(Worksheet $parent) + { + $this->storeCurrentCell(); + $newCollection = clone $this; + + $newCollection->parent = $parent; + if (($newCollection->currentCell !== null) && (is_object($newCollection->currentCell))) { + $newCollection->currentCell->attach($this); + } + + // Get old values + $oldKeys = $newCollection->getAllCacheKeys(); + $oldValues = $newCollection->cache->getMultiple($oldKeys); + $newValues = []; + $oldCachePrefix = $newCollection->cachePrefix; + + // Change prefix + $newCollection->cachePrefix = $newCollection->getUniqueID(); + foreach ($oldValues as $oldKey => $value) { + $newValues[str_replace($oldCachePrefix, $newCollection->cachePrefix, $oldKey)] = clone $value; + } + + // Store new values + $stored = $newCollection->cache->setMultiple($newValues); + if (!$stored) { + $newCollection->__destruct(); + + throw new PhpSpreadsheetException('Failed to copy cells in cache'); + } + + return $newCollection; + } + + /** + * Remove a row, deleting all cells in that row. + * + * @param string $row Row number to remove + */ + public function removeRow($row) + { + foreach ($this->getCoordinates() as $coord) { + $c = ''; + $r = 0; + + sscanf($coord, '%[A-Z]%d', $c, $r); + if ($r == $row) { + $this->delete($coord); + } + } + } + + /** + * Remove a column, deleting all cells in that column. + * + * @param string $column Column ID to remove + */ + public function removeColumn($column) + { + foreach ($this->getCoordinates() as $coord) { + $c = ''; + $r = 0; + + sscanf($coord, '%[A-Z]%d', $c, $r); + if ($c == $column) { + $this->delete($coord); + } + } + } + + /** + * Store cell data in cache for the current cell object if it's "dirty", + * and the 'nullify' the current cell object. + * + * @throws PhpSpreadsheetException + */ + private function storeCurrentCell() + { + if ($this->currentCellIsDirty && !empty($this->currentCoordinate)) { + $this->currentCell->detach(); + + $stored = $this->cache->set($this->cachePrefix . $this->currentCoordinate, $this->currentCell); + if (!$stored) { + $this->__destruct(); + + throw new PhpSpreadsheetException("Failed to store cell {$this->currentCoordinate} in cache"); + } + $this->currentCellIsDirty = false; + } + + $this->currentCoordinate = null; + $this->currentCell = null; + } + + /** + * Add or update a cell identified by its coordinate into the collection. + * + * @param string $pCoord Coordinate of the cell to update + * @param Cell $cell Cell to update + * + * @throws PhpSpreadsheetException + * + * @return \PhpOffice\PhpSpreadsheet\Cell\Cell + */ + public function add($pCoord, Cell $cell) + { + if ($pCoord !== $this->currentCoordinate) { + $this->storeCurrentCell(); + } + $this->index[$pCoord] = true; + + $this->currentCoordinate = $pCoord; + $this->currentCell = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Get cell at a specific coordinate. + * + * @param string $pCoord Coordinate of the cell + * + * @throws PhpSpreadsheetException + * + * @return \PhpOffice\PhpSpreadsheet\Cell\Cell Cell that was found, or null if not found + */ + public function get($pCoord) + { + if ($pCoord === $this->currentCoordinate) { + return $this->currentCell; + } + $this->storeCurrentCell(); + + // Return null if requested entry doesn't exist in collection + if (!$this->has($pCoord)) { + return null; + } + + // Check if the entry that has been requested actually exists + $cell = $this->cache->get($this->cachePrefix . $pCoord); + if ($cell === null) { + throw new PhpSpreadsheetException("Cell entry {$pCoord} no longer exists in cache. This probably means that the cache was cleared by someone else."); + } + + // Set current entry to the requested entry + $this->currentCoordinate = $pCoord; + $this->currentCell = $cell; + // Re-attach this as the cell's parent + $this->currentCell->attach($this); + + // Return requested entry + return $this->currentCell; + } + + /** + * Clear the cell collection and disconnect from our parent. + */ + public function unsetWorksheetCells() + { + if ($this->currentCell !== null) { + $this->currentCell->detach(); + $this->currentCell = null; + $this->currentCoordinate = null; + } + + // Flush the cache + $this->__destruct(); + + $this->index = []; + + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + } + + /** + * Destroy this cell collection. + */ + public function __destruct() + { + $this->cache->deleteMultiple($this->getAllCacheKeys()); + } + + /** + * Returns all known cache keys. + * + * @return \Generator|string[] + */ + private function getAllCacheKeys() + { + foreach ($this->getCoordinates() as $coordinate) { + yield $this->cachePrefix . $coordinate; + } + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Collection/CellsFactory.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Collection/CellsFactory.php new file mode 100644 index 00000000000..46d3cf7e14e --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Collection/CellsFactory.php @@ -0,0 +1,23 @@ +cache = []; + + return true; + } + + public function delete($key) + { + unset($this->cache[$key]); + + return true; + } + + public function deleteMultiple($keys) + { + foreach ($keys as $key) { + $this->delete($key); + } + + return true; + } + + public function get($key, $default = null) + { + if ($this->has($key)) { + return $this->cache[$key]; + } + + return $default; + } + + public function getMultiple($keys, $default = null) + { + $results = []; + foreach ($keys as $key) { + $results[$key] = $this->get($key, $default); + } + + return $results; + } + + public function has($key) + { + return array_key_exists($key, $this->cache); + } + + public function set($key, $value, $ttl = null) + { + $this->cache[$key] = $value; + + return true; + } + + public function setMultiple($values, $ttl = null) + { + foreach ($values as $key => $value) { + $this->set($key, $value); + } + + return true; + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Comment.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Comment.php new file mode 100644 index 00000000000..1b5ab1fd2c1 --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Comment.php @@ -0,0 +1,331 @@ +author = 'Author'; + $this->text = new RichText(); + $this->fillColor = new Style\Color('FFFFFFE1'); + $this->alignment = Style\Alignment::HORIZONTAL_GENERAL; + } + + /** + * Get Author. + * + * @return string + */ + public function getAuthor() + { + return $this->author; + } + + /** + * Set Author. + * + * @param string $author + * + * @return Comment + */ + public function setAuthor($author) + { + $this->author = $author; + + return $this; + } + + /** + * Get Rich text comment. + * + * @return RichText + */ + public function getText() + { + return $this->text; + } + + /** + * Set Rich text comment. + * + * @param RichText $pValue + * + * @return Comment + */ + public function setText(RichText $pValue) + { + $this->text = $pValue; + + return $this; + } + + /** + * Get comment width (CSS style, i.e. XXpx or YYpt). + * + * @return string + */ + public function getWidth() + { + return $this->width; + } + + /** + * Set comment width (CSS style, i.e. XXpx or YYpt). + * + * @param string $width + * + * @return Comment + */ + public function setWidth($width) + { + $this->width = $width; + + return $this; + } + + /** + * Get comment height (CSS style, i.e. XXpx or YYpt). + * + * @return string + */ + public function getHeight() + { + return $this->height; + } + + /** + * Set comment height (CSS style, i.e. XXpx or YYpt). + * + * @param string $value + * + * @return Comment + */ + public function setHeight($value) + { + $this->height = $value; + + return $this; + } + + /** + * Get left margin (CSS style, i.e. XXpx or YYpt). + * + * @return string + */ + public function getMarginLeft() + { + return $this->marginLeft; + } + + /** + * Set left margin (CSS style, i.e. XXpx or YYpt). + * + * @param string $value + * + * @return Comment + */ + public function setMarginLeft($value) + { + $this->marginLeft = $value; + + return $this; + } + + /** + * Get top margin (CSS style, i.e. XXpx or YYpt). + * + * @return string + */ + public function getMarginTop() + { + return $this->marginTop; + } + + /** + * Set top margin (CSS style, i.e. XXpx or YYpt). + * + * @param string $value + * + * @return Comment + */ + public function setMarginTop($value) + { + $this->marginTop = $value; + + return $this; + } + + /** + * Is the comment visible by default? + * + * @return bool + */ + public function getVisible() + { + return $this->visible; + } + + /** + * Set comment default visibility. + * + * @param bool $value + * + * @return Comment + */ + public function setVisible($value) + { + $this->visible = $value; + + return $this; + } + + /** + * Get fill color. + * + * @return Style\Color + */ + public function getFillColor() + { + return $this->fillColor; + } + + /** + * Set Alignment. + * + * @param string $alignment see Style\Alignment::HORIZONTAL_* + * + * @return Comment + */ + public function setAlignment($alignment) + { + $this->alignment = $alignment; + + return $this; + } + + /** + * Get Alignment. + * + * @return string + */ + public function getAlignment() + { + return $this->alignment; + } + + /** + * Get hash code. + * + * @return string Hash code + */ + public function getHashCode() + { + return md5( + $this->author . + $this->text->getHashCode() . + $this->width . + $this->height . + $this->marginLeft . + $this->marginTop . + ($this->visible ? 1 : 0) . + $this->fillColor->getHashCode() . + $this->alignment . + __CLASS__ + ); + } + + /** + * Implement PHP __clone to create a deep clone, not just a shallow copy. + */ + public function __clone() + { + $vars = get_object_vars($this); + foreach ($vars as $key => $value) { + if (is_object($value)) { + $this->$key = clone $value; + } else { + $this->$key = $value; + } + } + } + + /** + * Convert to string. + * + * @return string + */ + public function __toString() + { + return $this->text->getPlainText(); + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Document/Properties.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Document/Properties.php new file mode 100644 index 00000000000..bbac96d92a8 --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Document/Properties.php @@ -0,0 +1,629 @@ +lastModifiedBy = $this->creator; + $this->created = time(); + $this->modified = time(); + } + + /** + * Get Creator. + * + * @return string + */ + public function getCreator() + { + return $this->creator; + } + + /** + * Set Creator. + * + * @param string $creator + * + * @return Properties + */ + public function setCreator($creator) + { + $this->creator = $creator; + + return $this; + } + + /** + * Get Last Modified By. + * + * @return string + */ + public function getLastModifiedBy() + { + return $this->lastModifiedBy; + } + + /** + * Set Last Modified By. + * + * @param string $pValue + * + * @return Properties + */ + public function setLastModifiedBy($pValue) + { + $this->lastModifiedBy = $pValue; + + return $this; + } + + /** + * Get Created. + * + * @return int + */ + public function getCreated() + { + return $this->created; + } + + /** + * Set Created. + * + * @param int|string $time + * + * @return Properties + */ + public function setCreated($time) + { + if ($time === null) { + $time = time(); + } elseif (is_string($time)) { + if (is_numeric($time)) { + $time = (int) $time; + } else { + $time = strtotime($time); + } + } + + $this->created = $time; + + return $this; + } + + /** + * Get Modified. + * + * @return int + */ + public function getModified() + { + return $this->modified; + } + + /** + * Set Modified. + * + * @param int|string $time + * + * @return Properties + */ + public function setModified($time) + { + if ($time === null) { + $time = time(); + } elseif (is_string($time)) { + if (is_numeric($time)) { + $time = (int) $time; + } else { + $time = strtotime($time); + } + } + + $this->modified = $time; + + return $this; + } + + /** + * Get Title. + * + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * Set Title. + * + * @param string $title + * + * @return Properties + */ + public function setTitle($title) + { + $this->title = $title; + + return $this; + } + + /** + * Get Description. + * + * @return string + */ + public function getDescription() + { + return $this->description; + } + + /** + * Set Description. + * + * @param string $description + * + * @return Properties + */ + public function setDescription($description) + { + $this->description = $description; + + return $this; + } + + /** + * Get Subject. + * + * @return string + */ + public function getSubject() + { + return $this->subject; + } + + /** + * Set Subject. + * + * @param string $subject + * + * @return Properties + */ + public function setSubject($subject) + { + $this->subject = $subject; + + return $this; + } + + /** + * Get Keywords. + * + * @return string + */ + public function getKeywords() + { + return $this->keywords; + } + + /** + * Set Keywords. + * + * @param string $keywords + * + * @return Properties + */ + public function setKeywords($keywords) + { + $this->keywords = $keywords; + + return $this; + } + + /** + * Get Category. + * + * @return string + */ + public function getCategory() + { + return $this->category; + } + + /** + * Set Category. + * + * @param string $category + * + * @return Properties + */ + public function setCategory($category) + { + $this->category = $category; + + return $this; + } + + /** + * Get Company. + * + * @return string + */ + public function getCompany() + { + return $this->company; + } + + /** + * Set Company. + * + * @param string $company + * + * @return Properties + */ + public function setCompany($company) + { + $this->company = $company; + + return $this; + } + + /** + * Get Manager. + * + * @return string + */ + public function getManager() + { + return $this->manager; + } + + /** + * Set Manager. + * + * @param string $manager + * + * @return Properties + */ + public function setManager($manager) + { + $this->manager = $manager; + + return $this; + } + + /** + * Get a List of Custom Property Names. + * + * @return array of string + */ + public function getCustomProperties() + { + return array_keys($this->customProperties); + } + + /** + * Check if a Custom Property is defined. + * + * @param string $propertyName + * + * @return bool + */ + public function isCustomPropertySet($propertyName) + { + return isset($this->customProperties[$propertyName]); + } + + /** + * Get a Custom Property Value. + * + * @param string $propertyName + * + * @return string + */ + public function getCustomPropertyValue($propertyName) + { + if (isset($this->customProperties[$propertyName])) { + return $this->customProperties[$propertyName]['value']; + } + } + + /** + * Get a Custom Property Type. + * + * @param string $propertyName + * + * @return string + */ + public function getCustomPropertyType($propertyName) + { + if (isset($this->customProperties[$propertyName])) { + return $this->customProperties[$propertyName]['type']; + } + } + + /** + * Set a Custom Property. + * + * @param string $propertyName + * @param mixed $propertyValue + * @param string $propertyType + * 'i' : Integer + * 'f' : Floating Point + * 's' : String + * 'd' : Date/Time + * 'b' : Boolean + * + * @return Properties + */ + public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null) + { + if (($propertyType === null) || (!in_array($propertyType, [self::PROPERTY_TYPE_INTEGER, + self::PROPERTY_TYPE_FLOAT, + self::PROPERTY_TYPE_STRING, + self::PROPERTY_TYPE_DATE, + self::PROPERTY_TYPE_BOOLEAN, ]))) { + if ($propertyValue === null) { + $propertyType = self::PROPERTY_TYPE_STRING; + } elseif (is_float($propertyValue)) { + $propertyType = self::PROPERTY_TYPE_FLOAT; + } elseif (is_int($propertyValue)) { + $propertyType = self::PROPERTY_TYPE_INTEGER; + } elseif (is_bool($propertyValue)) { + $propertyType = self::PROPERTY_TYPE_BOOLEAN; + } else { + $propertyType = self::PROPERTY_TYPE_STRING; + } + } + + $this->customProperties[$propertyName] = [ + 'value' => $propertyValue, + 'type' => $propertyType, + ]; + + return $this; + } + + /** + * Implement PHP __clone to create a deep clone, not just a shallow copy. + */ + public function __clone() + { + $vars = get_object_vars($this); + foreach ($vars as $key => $value) { + if (is_object($value)) { + $this->$key = clone $value; + } else { + $this->$key = $value; + } + } + } + + public static function convertProperty($propertyValue, $propertyType) + { + switch ($propertyType) { + case 'empty': // Empty + return ''; + + break; + case 'null': // Null + return null; + + break; + case 'i1': // 1-Byte Signed Integer + case 'i2': // 2-Byte Signed Integer + case 'i4': // 4-Byte Signed Integer + case 'i8': // 8-Byte Signed Integer + case 'int': // Integer + return (int) $propertyValue; + + break; + case 'ui1': // 1-Byte Unsigned Integer + case 'ui2': // 2-Byte Unsigned Integer + case 'ui4': // 4-Byte Unsigned Integer + case 'ui8': // 8-Byte Unsigned Integer + case 'uint': // Unsigned Integer + return abs((int) $propertyValue); + + break; + case 'r4': // 4-Byte Real Number + case 'r8': // 8-Byte Real Number + case 'decimal': // Decimal + return (float) $propertyValue; + + break; + case 'lpstr': // LPSTR + case 'lpwstr': // LPWSTR + case 'bstr': // Basic String + return $propertyValue; + + break; + case 'date': // Date and Time + case 'filetime': // File Time + return strtotime($propertyValue); + + break; + case 'bool': // Boolean + return $propertyValue == 'true'; + + break; + case 'cy': // Currency + case 'error': // Error Status Code + case 'vector': // Vector + case 'array': // Array + case 'blob': // Binary Blob + case 'oblob': // Binary Blob Object + case 'stream': // Binary Stream + case 'ostream': // Binary Stream Object + case 'storage': // Binary Storage + case 'ostorage': // Binary Storage Object + case 'vstream': // Binary Versioned Stream + case 'clsid': // Class ID + case 'cf': // Clipboard Data + return $propertyValue; + + break; + } + + return $propertyValue; + } + + public static function convertPropertyType($propertyType) + { + switch ($propertyType) { + case 'i1': // 1-Byte Signed Integer + case 'i2': // 2-Byte Signed Integer + case 'i4': // 4-Byte Signed Integer + case 'i8': // 8-Byte Signed Integer + case 'int': // Integer + case 'ui1': // 1-Byte Unsigned Integer + case 'ui2': // 2-Byte Unsigned Integer + case 'ui4': // 4-Byte Unsigned Integer + case 'ui8': // 8-Byte Unsigned Integer + case 'uint': // Unsigned Integer + return self::PROPERTY_TYPE_INTEGER; + + break; + case 'r4': // 4-Byte Real Number + case 'r8': // 8-Byte Real Number + case 'decimal': // Decimal + return self::PROPERTY_TYPE_FLOAT; + + break; + case 'empty': // Empty + case 'null': // Null + case 'lpstr': // LPSTR + case 'lpwstr': // LPWSTR + case 'bstr': // Basic String + return self::PROPERTY_TYPE_STRING; + + break; + case 'date': // Date and Time + case 'filetime': // File Time + return self::PROPERTY_TYPE_DATE; + + break; + case 'bool': // Boolean + return self::PROPERTY_TYPE_BOOLEAN; + + break; + case 'cy': // Currency + case 'error': // Error Status Code + case 'vector': // Vector + case 'array': // Array + case 'blob': // Binary Blob + case 'oblob': // Binary Blob Object + case 'stream': // Binary Stream + case 'ostream': // Binary Stream Object + case 'storage': // Binary Storage + case 'ostorage': // Binary Storage Object + case 'vstream': // Binary Versioned Stream + case 'clsid': // Class ID + case 'cf': // Clipboard Data + return self::PROPERTY_TYPE_UNKNOWN; + + break; + } + + return self::PROPERTY_TYPE_UNKNOWN; + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Document/Security.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Document/Security.php new file mode 100644 index 00000000000..1682678c2e7 --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Document/Security.php @@ -0,0 +1,205 @@ +lockRevision || + $this->lockStructure || + $this->lockWindows; + } + + /** + * Get LockRevision. + * + * @return bool + */ + public function getLockRevision() + { + return $this->lockRevision; + } + + /** + * Set LockRevision. + * + * @param bool $pValue + * + * @return Security + */ + public function setLockRevision($pValue) + { + $this->lockRevision = $pValue; + + return $this; + } + + /** + * Get LockStructure. + * + * @return bool + */ + public function getLockStructure() + { + return $this->lockStructure; + } + + /** + * Set LockStructure. + * + * @param bool $pValue + * + * @return Security + */ + public function setLockStructure($pValue) + { + $this->lockStructure = $pValue; + + return $this; + } + + /** + * Get LockWindows. + * + * @return bool + */ + public function getLockWindows() + { + return $this->lockWindows; + } + + /** + * Set LockWindows. + * + * @param bool $pValue + * + * @return Security + */ + public function setLockWindows($pValue) + { + $this->lockWindows = $pValue; + + return $this; + } + + /** + * Get RevisionsPassword (hashed). + * + * @return string + */ + public function getRevisionsPassword() + { + return $this->revisionsPassword; + } + + /** + * Set RevisionsPassword. + * + * @param string $pValue + * @param bool $pAlreadyHashed If the password has already been hashed, set this to true + * + * @return Security + */ + public function setRevisionsPassword($pValue, $pAlreadyHashed = false) + { + if (!$pAlreadyHashed) { + $pValue = PasswordHasher::hashPassword($pValue); + } + $this->revisionsPassword = $pValue; + + return $this; + } + + /** + * Get WorkbookPassword (hashed). + * + * @return string + */ + public function getWorkbookPassword() + { + return $this->workbookPassword; + } + + /** + * Set WorkbookPassword. + * + * @param string $pValue + * @param bool $pAlreadyHashed If the password has already been hashed, set this to true + * + * @return Security + */ + public function setWorkbookPassword($pValue, $pAlreadyHashed = false) + { + if (!$pAlreadyHashed) { + $pValue = PasswordHasher::hashPassword($pValue); + } + $this->workbookPassword = $pValue; + + return $this; + } + + /** + * Implement PHP __clone to create a deep clone, not just a shallow copy. + */ + public function __clone() + { + $vars = get_object_vars($this); + foreach ($vars as $key => $value) { + if (is_object($value)) { + $this->$key = clone $value; + } else { + $this->$key = $value; + } + } + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Exception.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Exception.php new file mode 100644 index 00000000000..9c5ab30ee05 --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Exception.php @@ -0,0 +1,7 @@ +addFromSource($pSource); + } + } + + /** + * Add HashTable items from source. + * + * @param IComparable[] $pSource Source array to create HashTable from + * + * @throws Exception + */ + public function addFromSource(array $pSource = null) + { + // Check if an array was passed + if ($pSource == null) { + return; + } + + foreach ($pSource as $item) { + $this->add($item); + } + } + + /** + * Add HashTable item. + * + * @param IComparable $pSource Item to add + */ + public function add(IComparable $pSource) + { + $hash = $pSource->getHashCode(); + if (!isset($this->items[$hash])) { + $this->items[$hash] = $pSource; + $this->keyMap[count($this->items) - 1] = $hash; + } + } + + /** + * Remove HashTable item. + * + * @param IComparable $pSource Item to remove + */ + public function remove(IComparable $pSource) + { + $hash = $pSource->getHashCode(); + if (isset($this->items[$hash])) { + unset($this->items[$hash]); + + $deleteKey = -1; + foreach ($this->keyMap as $key => $value) { + if ($deleteKey >= 0) { + $this->keyMap[$key - 1] = $value; + } + + if ($value == $hash) { + $deleteKey = $key; + } + } + unset($this->keyMap[count($this->keyMap) - 1]); + } + } + + /** + * Clear HashTable. + */ + public function clear() + { + $this->items = []; + $this->keyMap = []; + } + + /** + * Count. + * + * @return int + */ + public function count() + { + return count($this->items); + } + + /** + * Get index for hash code. + * + * @param string $pHashCode + * + * @return int Index + */ + public function getIndexForHashCode($pHashCode) + { + return array_search($pHashCode, $this->keyMap); + } + + /** + * Get by index. + * + * @param int $pIndex + * + * @return IComparable + */ + public function getByIndex($pIndex) + { + if (isset($this->keyMap[$pIndex])) { + return $this->getByHashCode($this->keyMap[$pIndex]); + } + + return null; + } + + /** + * Get by hashcode. + * + * @param string $pHashCode + * + * @return IComparable + */ + public function getByHashCode($pHashCode) + { + if (isset($this->items[$pHashCode])) { + return $this->items[$pHashCode]; + } + + return null; + } + + /** + * HashTable to array. + * + * @return IComparable[] + */ + public function toArray() + { + return $this->items; + } + + /** + * Implement PHP __clone to create a deep clone, not just a shallow copy. + */ + public function __clone() + { + $vars = get_object_vars($this); + foreach ($vars as $key => $value) { + if (is_object($value)) { + $this->$key = clone $value; + } + } + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Helper/Html.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Helper/Html.php new file mode 100644 index 00000000000..eaf73028fa7 --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Helper/Html.php @@ -0,0 +1,841 @@ + 'f0f8ff', + 'antiquewhite' => 'faebd7', + 'antiquewhite1' => 'ffefdb', + 'antiquewhite2' => 'eedfcc', + 'antiquewhite3' => 'cdc0b0', + 'antiquewhite4' => '8b8378', + 'aqua' => '00ffff', + 'aquamarine1' => '7fffd4', + 'aquamarine2' => '76eec6', + 'aquamarine4' => '458b74', + 'azure1' => 'f0ffff', + 'azure2' => 'e0eeee', + 'azure3' => 'c1cdcd', + 'azure4' => '838b8b', + 'beige' => 'f5f5dc', + 'bisque1' => 'ffe4c4', + 'bisque2' => 'eed5b7', + 'bisque3' => 'cdb79e', + 'bisque4' => '8b7d6b', + 'black' => '000000', + 'blanchedalmond' => 'ffebcd', + 'blue' => '0000ff', + 'blue1' => '0000ff', + 'blue2' => '0000ee', + 'blue4' => '00008b', + 'blueviolet' => '8a2be2', + 'brown' => 'a52a2a', + 'brown1' => 'ff4040', + 'brown2' => 'ee3b3b', + 'brown3' => 'cd3333', + 'brown4' => '8b2323', + 'burlywood' => 'deb887', + 'burlywood1' => 'ffd39b', + 'burlywood2' => 'eec591', + 'burlywood3' => 'cdaa7d', + 'burlywood4' => '8b7355', + 'cadetblue' => '5f9ea0', + 'cadetblue1' => '98f5ff', + 'cadetblue2' => '8ee5ee', + 'cadetblue3' => '7ac5cd', + 'cadetblue4' => '53868b', + 'chartreuse1' => '7fff00', + 'chartreuse2' => '76ee00', + 'chartreuse3' => '66cd00', + 'chartreuse4' => '458b00', + 'chocolate' => 'd2691e', + 'chocolate1' => 'ff7f24', + 'chocolate2' => 'ee7621', + 'chocolate3' => 'cd661d', + 'coral' => 'ff7f50', + 'coral1' => 'ff7256', + 'coral2' => 'ee6a50', + 'coral3' => 'cd5b45', + 'coral4' => '8b3e2f', + 'cornflowerblue' => '6495ed', + 'cornsilk1' => 'fff8dc', + 'cornsilk2' => 'eee8cd', + 'cornsilk3' => 'cdc8b1', + 'cornsilk4' => '8b8878', + 'cyan1' => '00ffff', + 'cyan2' => '00eeee', + 'cyan3' => '00cdcd', + 'cyan4' => '008b8b', + 'darkgoldenrod' => 'b8860b', + 'darkgoldenrod1' => 'ffb90f', + 'darkgoldenrod2' => 'eead0e', + 'darkgoldenrod3' => 'cd950c', + 'darkgoldenrod4' => '8b6508', + 'darkgreen' => '006400', + 'darkkhaki' => 'bdb76b', + 'darkolivegreen' => '556b2f', + 'darkolivegreen1' => 'caff70', + 'darkolivegreen2' => 'bcee68', + 'darkolivegreen3' => 'a2cd5a', + 'darkolivegreen4' => '6e8b3d', + 'darkorange' => 'ff8c00', + 'darkorange1' => 'ff7f00', + 'darkorange2' => 'ee7600', + 'darkorange3' => 'cd6600', + 'darkorange4' => '8b4500', + 'darkorchid' => '9932cc', + 'darkorchid1' => 'bf3eff', + 'darkorchid2' => 'b23aee', + 'darkorchid3' => '9a32cd', + 'darkorchid4' => '68228b', + 'darksalmon' => 'e9967a', + 'darkseagreen' => '8fbc8f', + 'darkseagreen1' => 'c1ffc1', + 'darkseagreen2' => 'b4eeb4', + 'darkseagreen3' => '9bcd9b', + 'darkseagreen4' => '698b69', + 'darkslateblue' => '483d8b', + 'darkslategray' => '2f4f4f', + 'darkslategray1' => '97ffff', + 'darkslategray2' => '8deeee', + 'darkslategray3' => '79cdcd', + 'darkslategray4' => '528b8b', + 'darkturquoise' => '00ced1', + 'darkviolet' => '9400d3', + 'deeppink1' => 'ff1493', + 'deeppink2' => 'ee1289', + 'deeppink3' => 'cd1076', + 'deeppink4' => '8b0a50', + 'deepskyblue1' => '00bfff', + 'deepskyblue2' => '00b2ee', + 'deepskyblue3' => '009acd', + 'deepskyblue4' => '00688b', + 'dimgray' => '696969', + 'dodgerblue1' => '1e90ff', + 'dodgerblue2' => '1c86ee', + 'dodgerblue3' => '1874cd', + 'dodgerblue4' => '104e8b', + 'firebrick' => 'b22222', + 'firebrick1' => 'ff3030', + 'firebrick2' => 'ee2c2c', + 'firebrick3' => 'cd2626', + 'firebrick4' => '8b1a1a', + 'floralwhite' => 'fffaf0', + 'forestgreen' => '228b22', + 'fuchsia' => 'ff00ff', + 'gainsboro' => 'dcdcdc', + 'ghostwhite' => 'f8f8ff', + 'gold1' => 'ffd700', + 'gold2' => 'eec900', + 'gold3' => 'cdad00', + 'gold4' => '8b7500', + 'goldenrod' => 'daa520', + 'goldenrod1' => 'ffc125', + 'goldenrod2' => 'eeb422', + 'goldenrod3' => 'cd9b1d', + 'goldenrod4' => '8b6914', + 'gray' => 'bebebe', + 'gray1' => '030303', + 'gray10' => '1a1a1a', + 'gray11' => '1c1c1c', + 'gray12' => '1f1f1f', + 'gray13' => '212121', + 'gray14' => '242424', + 'gray15' => '262626', + 'gray16' => '292929', + 'gray17' => '2b2b2b', + 'gray18' => '2e2e2e', + 'gray19' => '303030', + 'gray2' => '050505', + 'gray20' => '333333', + 'gray21' => '363636', + 'gray22' => '383838', + 'gray23' => '3b3b3b', + 'gray24' => '3d3d3d', + 'gray25' => '404040', + 'gray26' => '424242', + 'gray27' => '454545', + 'gray28' => '474747', + 'gray29' => '4a4a4a', + 'gray3' => '080808', + 'gray30' => '4d4d4d', + 'gray31' => '4f4f4f', + 'gray32' => '525252', + 'gray33' => '545454', + 'gray34' => '575757', + 'gray35' => '595959', + 'gray36' => '5c5c5c', + 'gray37' => '5e5e5e', + 'gray38' => '616161', + 'gray39' => '636363', + 'gray4' => '0a0a0a', + 'gray40' => '666666', + 'gray41' => '696969', + 'gray42' => '6b6b6b', + 'gray43' => '6e6e6e', + 'gray44' => '707070', + 'gray45' => '737373', + 'gray46' => '757575', + 'gray47' => '787878', + 'gray48' => '7a7a7a', + 'gray49' => '7d7d7d', + 'gray5' => '0d0d0d', + 'gray50' => '7f7f7f', + 'gray51' => '828282', + 'gray52' => '858585', + 'gray53' => '878787', + 'gray54' => '8a8a8a', + 'gray55' => '8c8c8c', + 'gray56' => '8f8f8f', + 'gray57' => '919191', + 'gray58' => '949494', + 'gray59' => '969696', + 'gray6' => '0f0f0f', + 'gray60' => '999999', + 'gray61' => '9c9c9c', + 'gray62' => '9e9e9e', + 'gray63' => 'a1a1a1', + 'gray64' => 'a3a3a3', + 'gray65' => 'a6a6a6', + 'gray66' => 'a8a8a8', + 'gray67' => 'ababab', + 'gray68' => 'adadad', + 'gray69' => 'b0b0b0', + 'gray7' => '121212', + 'gray70' => 'b3b3b3', + 'gray71' => 'b5b5b5', + 'gray72' => 'b8b8b8', + 'gray73' => 'bababa', + 'gray74' => 'bdbdbd', + 'gray75' => 'bfbfbf', + 'gray76' => 'c2c2c2', + 'gray77' => 'c4c4c4', + 'gray78' => 'c7c7c7', + 'gray79' => 'c9c9c9', + 'gray8' => '141414', + 'gray80' => 'cccccc', + 'gray81' => 'cfcfcf', + 'gray82' => 'd1d1d1', + 'gray83' => 'd4d4d4', + 'gray84' => 'd6d6d6', + 'gray85' => 'd9d9d9', + 'gray86' => 'dbdbdb', + 'gray87' => 'dedede', + 'gray88' => 'e0e0e0', + 'gray89' => 'e3e3e3', + 'gray9' => '171717', + 'gray90' => 'e5e5e5', + 'gray91' => 'e8e8e8', + 'gray92' => 'ebebeb', + 'gray93' => 'ededed', + 'gray94' => 'f0f0f0', + 'gray95' => 'f2f2f2', + 'gray97' => 'f7f7f7', + 'gray98' => 'fafafa', + 'gray99' => 'fcfcfc', + 'green' => '00ff00', + 'green1' => '00ff00', + 'green2' => '00ee00', + 'green3' => '00cd00', + 'green4' => '008b00', + 'greenyellow' => 'adff2f', + 'honeydew1' => 'f0fff0', + 'honeydew2' => 'e0eee0', + 'honeydew3' => 'c1cdc1', + 'honeydew4' => '838b83', + 'hotpink' => 'ff69b4', + 'hotpink1' => 'ff6eb4', + 'hotpink2' => 'ee6aa7', + 'hotpink3' => 'cd6090', + 'hotpink4' => '8b3a62', + 'indianred' => 'cd5c5c', + 'indianred1' => 'ff6a6a', + 'indianred2' => 'ee6363', + 'indianred3' => 'cd5555', + 'indianred4' => '8b3a3a', + 'ivory1' => 'fffff0', + 'ivory2' => 'eeeee0', + 'ivory3' => 'cdcdc1', + 'ivory4' => '8b8b83', + 'khaki' => 'f0e68c', + 'khaki1' => 'fff68f', + 'khaki2' => 'eee685', + 'khaki3' => 'cdc673', + 'khaki4' => '8b864e', + 'lavender' => 'e6e6fa', + 'lavenderblush1' => 'fff0f5', + 'lavenderblush2' => 'eee0e5', + 'lavenderblush3' => 'cdc1c5', + 'lavenderblush4' => '8b8386', + 'lawngreen' => '7cfc00', + 'lemonchiffon1' => 'fffacd', + 'lemonchiffon2' => 'eee9bf', + 'lemonchiffon3' => 'cdc9a5', + 'lemonchiffon4' => '8b8970', + 'light' => 'eedd82', + 'lightblue' => 'add8e6', + 'lightblue1' => 'bfefff', + 'lightblue2' => 'b2dfee', + 'lightblue3' => '9ac0cd', + 'lightblue4' => '68838b', + 'lightcoral' => 'f08080', + 'lightcyan1' => 'e0ffff', + 'lightcyan2' => 'd1eeee', + 'lightcyan3' => 'b4cdcd', + 'lightcyan4' => '7a8b8b', + 'lightgoldenrod1' => 'ffec8b', + 'lightgoldenrod2' => 'eedc82', + 'lightgoldenrod3' => 'cdbe70', + 'lightgoldenrod4' => '8b814c', + 'lightgoldenrodyellow' => 'fafad2', + 'lightgray' => 'd3d3d3', + 'lightpink' => 'ffb6c1', + 'lightpink1' => 'ffaeb9', + 'lightpink2' => 'eea2ad', + 'lightpink3' => 'cd8c95', + 'lightpink4' => '8b5f65', + 'lightsalmon1' => 'ffa07a', + 'lightsalmon2' => 'ee9572', + 'lightsalmon3' => 'cd8162', + 'lightsalmon4' => '8b5742', + 'lightseagreen' => '20b2aa', + 'lightskyblue' => '87cefa', + 'lightskyblue1' => 'b0e2ff', + 'lightskyblue2' => 'a4d3ee', + 'lightskyblue3' => '8db6cd', + 'lightskyblue4' => '607b8b', + 'lightslateblue' => '8470ff', + 'lightslategray' => '778899', + 'lightsteelblue' => 'b0c4de', + 'lightsteelblue1' => 'cae1ff', + 'lightsteelblue2' => 'bcd2ee', + 'lightsteelblue3' => 'a2b5cd', + 'lightsteelblue4' => '6e7b8b', + 'lightyellow1' => 'ffffe0', + 'lightyellow2' => 'eeeed1', + 'lightyellow3' => 'cdcdb4', + 'lightyellow4' => '8b8b7a', + 'lime' => '00ff00', + 'limegreen' => '32cd32', + 'linen' => 'faf0e6', + 'magenta' => 'ff00ff', + 'magenta2' => 'ee00ee', + 'magenta3' => 'cd00cd', + 'magenta4' => '8b008b', + 'maroon' => 'b03060', + 'maroon1' => 'ff34b3', + 'maroon2' => 'ee30a7', + 'maroon3' => 'cd2990', + 'maroon4' => '8b1c62', + 'medium' => '66cdaa', + 'mediumaquamarine' => '66cdaa', + 'mediumblue' => '0000cd', + 'mediumorchid' => 'ba55d3', + 'mediumorchid1' => 'e066ff', + 'mediumorchid2' => 'd15fee', + 'mediumorchid3' => 'b452cd', + 'mediumorchid4' => '7a378b', + 'mediumpurple' => '9370db', + 'mediumpurple1' => 'ab82ff', + 'mediumpurple2' => '9f79ee', + 'mediumpurple3' => '8968cd', + 'mediumpurple4' => '5d478b', + 'mediumseagreen' => '3cb371', + 'mediumslateblue' => '7b68ee', + 'mediumspringgreen' => '00fa9a', + 'mediumturquoise' => '48d1cc', + 'mediumvioletred' => 'c71585', + 'midnightblue' => '191970', + 'mintcream' => 'f5fffa', + 'mistyrose1' => 'ffe4e1', + 'mistyrose2' => 'eed5d2', + 'mistyrose3' => 'cdb7b5', + 'mistyrose4' => '8b7d7b', + 'moccasin' => 'ffe4b5', + 'navajowhite1' => 'ffdead', + 'navajowhite2' => 'eecfa1', + 'navajowhite3' => 'cdb38b', + 'navajowhite4' => '8b795e', + 'navy' => '000080', + 'navyblue' => '000080', + 'oldlace' => 'fdf5e6', + 'olive' => '808000', + 'olivedrab' => '6b8e23', + 'olivedrab1' => 'c0ff3e', + 'olivedrab2' => 'b3ee3a', + 'olivedrab4' => '698b22', + 'orange' => 'ffa500', + 'orange1' => 'ffa500', + 'orange2' => 'ee9a00', + 'orange3' => 'cd8500', + 'orange4' => '8b5a00', + 'orangered1' => 'ff4500', + 'orangered2' => 'ee4000', + 'orangered3' => 'cd3700', + 'orangered4' => '8b2500', + 'orchid' => 'da70d6', + 'orchid1' => 'ff83fa', + 'orchid2' => 'ee7ae9', + 'orchid3' => 'cd69c9', + 'orchid4' => '8b4789', + 'pale' => 'db7093', + 'palegoldenrod' => 'eee8aa', + 'palegreen' => '98fb98', + 'palegreen1' => '9aff9a', + 'palegreen2' => '90ee90', + 'palegreen3' => '7ccd7c', + 'palegreen4' => '548b54', + 'paleturquoise' => 'afeeee', + 'paleturquoise1' => 'bbffff', + 'paleturquoise2' => 'aeeeee', + 'paleturquoise3' => '96cdcd', + 'paleturquoise4' => '668b8b', + 'palevioletred' => 'db7093', + 'palevioletred1' => 'ff82ab', + 'palevioletred2' => 'ee799f', + 'palevioletred3' => 'cd6889', + 'palevioletred4' => '8b475d', + 'papayawhip' => 'ffefd5', + 'peachpuff1' => 'ffdab9', + 'peachpuff2' => 'eecbad', + 'peachpuff3' => 'cdaf95', + 'peachpuff4' => '8b7765', + 'pink' => 'ffc0cb', + 'pink1' => 'ffb5c5', + 'pink2' => 'eea9b8', + 'pink3' => 'cd919e', + 'pink4' => '8b636c', + 'plum' => 'dda0dd', + 'plum1' => 'ffbbff', + 'plum2' => 'eeaeee', + 'plum3' => 'cd96cd', + 'plum4' => '8b668b', + 'powderblue' => 'b0e0e6', + 'purple' => 'a020f0', + 'rebeccapurple' => '663399', + 'purple1' => '9b30ff', + 'purple2' => '912cee', + 'purple3' => '7d26cd', + 'purple4' => '551a8b', + 'red' => 'ff0000', + 'red1' => 'ff0000', + 'red2' => 'ee0000', + 'red3' => 'cd0000', + 'red4' => '8b0000', + 'rosybrown' => 'bc8f8f', + 'rosybrown1' => 'ffc1c1', + 'rosybrown2' => 'eeb4b4', + 'rosybrown3' => 'cd9b9b', + 'rosybrown4' => '8b6969', + 'royalblue' => '4169e1', + 'royalblue1' => '4876ff', + 'royalblue2' => '436eee', + 'royalblue3' => '3a5fcd', + 'royalblue4' => '27408b', + 'saddlebrown' => '8b4513', + 'salmon' => 'fa8072', + 'salmon1' => 'ff8c69', + 'salmon2' => 'ee8262', + 'salmon3' => 'cd7054', + 'salmon4' => '8b4c39', + 'sandybrown' => 'f4a460', + 'seagreen1' => '54ff9f', + 'seagreen2' => '4eee94', + 'seagreen3' => '43cd80', + 'seagreen4' => '2e8b57', + 'seashell1' => 'fff5ee', + 'seashell2' => 'eee5de', + 'seashell3' => 'cdc5bf', + 'seashell4' => '8b8682', + 'sienna' => 'a0522d', + 'sienna1' => 'ff8247', + 'sienna2' => 'ee7942', + 'sienna3' => 'cd6839', + 'sienna4' => '8b4726', + 'silver' => 'c0c0c0', + 'skyblue' => '87ceeb', + 'skyblue1' => '87ceff', + 'skyblue2' => '7ec0ee', + 'skyblue3' => '6ca6cd', + 'skyblue4' => '4a708b', + 'slateblue' => '6a5acd', + 'slateblue1' => '836fff', + 'slateblue2' => '7a67ee', + 'slateblue3' => '6959cd', + 'slateblue4' => '473c8b', + 'slategray' => '708090', + 'slategray1' => 'c6e2ff', + 'slategray2' => 'b9d3ee', + 'slategray3' => '9fb6cd', + 'slategray4' => '6c7b8b', + 'snow1' => 'fffafa', + 'snow2' => 'eee9e9', + 'snow3' => 'cdc9c9', + 'snow4' => '8b8989', + 'springgreen1' => '00ff7f', + 'springgreen2' => '00ee76', + 'springgreen3' => '00cd66', + 'springgreen4' => '008b45', + 'steelblue' => '4682b4', + 'steelblue1' => '63b8ff', + 'steelblue2' => '5cacee', + 'steelblue3' => '4f94cd', + 'steelblue4' => '36648b', + 'tan' => 'd2b48c', + 'tan1' => 'ffa54f', + 'tan2' => 'ee9a49', + 'tan3' => 'cd853f', + 'tan4' => '8b5a2b', + 'teal' => '008080', + 'thistle' => 'd8bfd8', + 'thistle1' => 'ffe1ff', + 'thistle2' => 'eed2ee', + 'thistle3' => 'cdb5cd', + 'thistle4' => '8b7b8b', + 'tomato1' => 'ff6347', + 'tomato2' => 'ee5c42', + 'tomato3' => 'cd4f39', + 'tomato4' => '8b3626', + 'turquoise' => '40e0d0', + 'turquoise1' => '00f5ff', + 'turquoise2' => '00e5ee', + 'turquoise3' => '00c5cd', + 'turquoise4' => '00868b', + 'violet' => 'ee82ee', + 'violetred' => 'd02090', + 'violetred1' => 'ff3e96', + 'violetred2' => 'ee3a8c', + 'violetred3' => 'cd3278', + 'violetred4' => '8b2252', + 'wheat' => 'f5deb3', + 'wheat1' => 'ffe7ba', + 'wheat2' => 'eed8ae', + 'wheat3' => 'cdba96', + 'wheat4' => '8b7e66', + 'white' => 'ffffff', + 'whitesmoke' => 'f5f5f5', + 'yellow' => 'ffff00', + 'yellow1' => 'ffff00', + 'yellow2' => 'eeee00', + 'yellow3' => 'cdcd00', + 'yellow4' => '8b8b00', + 'yellowgreen' => '9acd32', + ]; + + protected $face; + + protected $size; + + protected $color; + + protected $bold = false; + + protected $italic = false; + + protected $underline = false; + + protected $superscript = false; + + protected $subscript = false; + + protected $strikethrough = false; + + protected $startTagCallbacks = [ + 'font' => 'startFontTag', + 'b' => 'startBoldTag', + 'strong' => 'startBoldTag', + 'i' => 'startItalicTag', + 'em' => 'startItalicTag', + 'u' => 'startUnderlineTag', + 'ins' => 'startUnderlineTag', + 'del' => 'startStrikethruTag', + 'sup' => 'startSuperscriptTag', + 'sub' => 'startSubscriptTag', + ]; + + protected $endTagCallbacks = [ + 'font' => 'endFontTag', + 'b' => 'endBoldTag', + 'strong' => 'endBoldTag', + 'i' => 'endItalicTag', + 'em' => 'endItalicTag', + 'u' => 'endUnderlineTag', + 'ins' => 'endUnderlineTag', + 'del' => 'endStrikethruTag', + 'sup' => 'endSuperscriptTag', + 'sub' => 'endSubscriptTag', + 'br' => 'breakTag', + 'p' => 'breakTag', + 'h1' => 'breakTag', + 'h2' => 'breakTag', + 'h3' => 'breakTag', + 'h4' => 'breakTag', + 'h5' => 'breakTag', + 'h6' => 'breakTag', + ]; + + protected $stack = []; + + protected $stringData = ''; + + /** + * @var RichText + */ + protected $richTextObject; + + protected function initialise() + { + $this->face = $this->size = $this->color = null; + $this->bold = $this->italic = $this->underline = $this->superscript = $this->subscript = $this->strikethrough = false; + + $this->stack = []; + + $this->stringData = ''; + } + + /** + * Parse HTML formatting and return the resulting RichText. + * + * @param string $html + * + * @return RichText + */ + public function toRichTextObject($html) + { + $this->initialise(); + + // Create a new DOM object + $dom = new DOMDocument(); + // Load the HTML file into the DOM object + // Note the use of error suppression, because typically this will be an html fragment, so not fully valid markup + $prefix = ''; + @$dom->loadHTML($prefix . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); + // Discard excess white space + $dom->preserveWhiteSpace = false; + + $this->richTextObject = new RichText(); + $this->parseElements($dom); + + // Clean any further spurious whitespace + $this->cleanWhitespace(); + + return $this->richTextObject; + } + + protected function cleanWhitespace() + { + foreach ($this->richTextObject->getRichTextElements() as $key => $element) { + $text = $element->getText(); + // Trim any leading spaces on the first run + if ($key == 0) { + $text = ltrim($text); + } + // Trim any spaces immediately after a line break + $text = preg_replace('/\n */mu', "\n", $text); + $element->setText($text); + } + } + + protected function buildTextRun() + { + $text = $this->stringData; + if (trim($text) === '') { + return; + } + + $richtextRun = $this->richTextObject->createTextRun($this->stringData); + if ($this->face) { + $richtextRun->getFont()->setName($this->face); + } + if ($this->size) { + $richtextRun->getFont()->setSize($this->size); + } + if ($this->color) { + $richtextRun->getFont()->setColor(new Color('ff' . $this->color)); + } + if ($this->bold) { + $richtextRun->getFont()->setBold(true); + } + if ($this->italic) { + $richtextRun->getFont()->setItalic(true); + } + if ($this->underline) { + $richtextRun->getFont()->setUnderline(Font::UNDERLINE_SINGLE); + } + if ($this->superscript) { + $richtextRun->getFont()->setSuperscript(true); + } + if ($this->subscript) { + $richtextRun->getFont()->setSubscript(true); + } + if ($this->strikethrough) { + $richtextRun->getFont()->setStrikethrough(true); + } + $this->stringData = ''; + } + + protected function rgbToColour($rgb) + { + preg_match_all('/\d+/', $rgb, $values); + foreach ($values[0] as &$value) { + $value = str_pad(dechex($value), 2, '0', STR_PAD_LEFT); + } + + return implode($values[0]); + } + + protected function colourNameLookup($rgb) + { + return self::$colourMap[$rgb]; + } + + protected function startFontTag($tag) + { + foreach ($tag->attributes as $attribute) { + $attributeName = strtolower($attribute->name); + $attributeValue = $attribute->value; + + if ($attributeName == 'color') { + if (preg_match('/rgb\s*\(/', $attributeValue)) { + $this->$attributeName = $this->rgbToColour($attributeValue); + } elseif (strpos(trim($attributeValue), '#') === 0) { + $this->$attributeName = ltrim($attributeValue, '#'); + } else { + $this->$attributeName = $this->colourNameLookup($attributeValue); + } + } else { + $this->$attributeName = $attributeValue; + } + } + } + + protected function endFontTag() + { + $this->face = $this->size = $this->color = null; + } + + protected function startBoldTag() + { + $this->bold = true; + } + + protected function endBoldTag() + { + $this->bold = false; + } + + protected function startItalicTag() + { + $this->italic = true; + } + + protected function endItalicTag() + { + $this->italic = false; + } + + protected function startUnderlineTag() + { + $this->underline = true; + } + + protected function endUnderlineTag() + { + $this->underline = false; + } + + protected function startSubscriptTag() + { + $this->subscript = true; + } + + protected function endSubscriptTag() + { + $this->subscript = false; + } + + protected function startSuperscriptTag() + { + $this->superscript = true; + } + + protected function endSuperscriptTag() + { + $this->superscript = false; + } + + protected function startStrikethruTag() + { + $this->strikethrough = true; + } + + protected function endStrikethruTag() + { + $this->strikethrough = false; + } + + protected function breakTag() + { + $this->stringData .= "\n"; + } + + protected function parseTextNode(DOMText $textNode) + { + $domText = preg_replace( + '/\s+/u', + ' ', + str_replace(["\r", "\n"], ' ', $textNode->nodeValue) + ); + $this->stringData .= $domText; + $this->buildTextRun(); + } + + /** + * @param DOMElement $element + * @param string $callbackTag + * @param array $callbacks + */ + protected function handleCallback(DOMElement $element, $callbackTag, array $callbacks) + { + if (isset($callbacks[$callbackTag])) { + $elementHandler = $callbacks[$callbackTag]; + if (method_exists($this, $elementHandler)) { + call_user_func([$this, $elementHandler], $element); + } + } + } + + protected function parseElementNode(DOMElement $element) + { + $callbackTag = strtolower($element->nodeName); + $this->stack[] = $callbackTag; + + $this->handleCallback($element, $callbackTag, $this->startTagCallbacks); + + $this->parseElements($element); + array_pop($this->stack); + + $this->handleCallback($element, $callbackTag, $this->endTagCallbacks); + } + + protected function parseElements(DOMNode $element) + { + foreach ($element->childNodes as $child) { + if ($child instanceof DOMText) { + $this->parseTextNode($child); + } elseif ($child instanceof DOMElement) { + $this->parseElementNode($child); + } + } + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Helper/Migrator.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Helper/Migrator.php new file mode 100644 index 00000000000..26d5fcead87 --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Helper/Migrator.php @@ -0,0 +1,333 @@ +from = array_keys($this->getMapping()); + $this->to = array_values($this->getMapping()); + } + + /** + * Return the ordered mapping from old PHPExcel class names to new PhpSpreadsheet one. + * + * @return string[] + */ + public function getMapping() + { + // Order matters here, we should have the deepest namespaces first (the most "unique" strings) + $classes = [ + 'PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip' => \PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer\BSE\Blip::class, + 'PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer' => \PhpOffice\PhpSpreadsheet\Shared\Escher\DgContainer\SpgrContainer\SpContainer::class, + 'PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE' => \PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer\BSE::class, + 'PHPExcel_Shared_Escher_DgContainer_SpgrContainer' => \PhpOffice\PhpSpreadsheet\Shared\Escher\DgContainer\SpgrContainer::class, + 'PHPExcel_Shared_Escher_DggContainer_BstoreContainer' => \PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer::class, + 'PHPExcel_Shared_OLE_PPS_File' => \PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\File::class, + 'PHPExcel_Shared_OLE_PPS_Root' => \PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\Root::class, + 'PHPExcel_Worksheet_AutoFilter_Column_Rule' => \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::class, + 'PHPExcel_Writer_OpenDocument_Cell_Comment' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Cell\Comment::class, + 'PHPExcel_Calculation_Token_Stack' => \PhpOffice\PhpSpreadsheet\Calculation\Token\Stack::class, + 'PHPExcel_Chart_Renderer_jpgraph' => \PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class, + 'PHPExcel_Reader_Excel5_Escher' => \PhpOffice\PhpSpreadsheet\Reader\Xls\Escher::class, + 'PHPExcel_Reader_Excel5_MD5' => \PhpOffice\PhpSpreadsheet\Reader\Xls\MD5::class, + 'PHPExcel_Reader_Excel5_RC4' => \PhpOffice\PhpSpreadsheet\Reader\Xls\RC4::class, + 'PHPExcel_Reader_Excel2007_Chart' => \PhpOffice\PhpSpreadsheet\Reader\Xlsx\Chart::class, + 'PHPExcel_Reader_Excel2007_Theme' => \PhpOffice\PhpSpreadsheet\Reader\Xlsx\Theme::class, + 'PHPExcel_Shared_Escher_DgContainer' => \PhpOffice\PhpSpreadsheet\Shared\Escher\DgContainer::class, + 'PHPExcel_Shared_Escher_DggContainer' => \PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer::class, + 'CholeskyDecomposition' => \PhpOffice\PhpSpreadsheet\Shared\JAMA\CholeskyDecomposition::class, + 'EigenvalueDecomposition' => \PhpOffice\PhpSpreadsheet\Shared\JAMA\EigenvalueDecomposition::class, + 'PHPExcel_Shared_JAMA_LUDecomposition' => \PhpOffice\PhpSpreadsheet\Shared\JAMA\LUDecomposition::class, + 'PHPExcel_Shared_JAMA_Matrix' => \PhpOffice\PhpSpreadsheet\Shared\JAMA\Matrix::class, + 'QRDecomposition' => \PhpOffice\PhpSpreadsheet\Shared\JAMA\QRDecomposition::class, + 'PHPExcel_Shared_JAMA_QRDecomposition' => \PhpOffice\PhpSpreadsheet\Shared\JAMA\QRDecomposition::class, + 'SingularValueDecomposition' => \PhpOffice\PhpSpreadsheet\Shared\JAMA\SingularValueDecomposition::class, + 'PHPExcel_Shared_OLE_ChainedBlockStream' => \PhpOffice\PhpSpreadsheet\Shared\OLE\ChainedBlockStream::class, + 'PHPExcel_Shared_OLE_PPS' => \PhpOffice\PhpSpreadsheet\Shared\OLE\PPS::class, + 'PHPExcel_Best_Fit' => \PhpOffice\PhpSpreadsheet\Shared\Trend\BestFit::class, + 'PHPExcel_Exponential_Best_Fit' => \PhpOffice\PhpSpreadsheet\Shared\Trend\ExponentialBestFit::class, + 'PHPExcel_Linear_Best_Fit' => \PhpOffice\PhpSpreadsheet\Shared\Trend\LinearBestFit::class, + 'PHPExcel_Logarithmic_Best_Fit' => \PhpOffice\PhpSpreadsheet\Shared\Trend\LogarithmicBestFit::class, + 'polynomialBestFit' => \PhpOffice\PhpSpreadsheet\Shared\Trend\PolynomialBestFit::class, + 'PHPExcel_Polynomial_Best_Fit' => \PhpOffice\PhpSpreadsheet\Shared\Trend\PolynomialBestFit::class, + 'powerBestFit' => \PhpOffice\PhpSpreadsheet\Shared\Trend\PowerBestFit::class, + 'PHPExcel_Power_Best_Fit' => \PhpOffice\PhpSpreadsheet\Shared\Trend\PowerBestFit::class, + 'trendClass' => \PhpOffice\PhpSpreadsheet\Shared\Trend\Trend::class, + 'PHPExcel_Worksheet_AutoFilter_Column' => \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::class, + 'PHPExcel_Worksheet_Drawing_Shadow' => \PhpOffice\PhpSpreadsheet\Worksheet\Drawing\Shadow::class, + 'PHPExcel_Writer_OpenDocument_Content' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Content::class, + 'PHPExcel_Writer_OpenDocument_Meta' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Meta::class, + 'PHPExcel_Writer_OpenDocument_MetaInf' => \PhpOffice\PhpSpreadsheet\Writer\Ods\MetaInf::class, + 'PHPExcel_Writer_OpenDocument_Mimetype' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Mimetype::class, + 'PHPExcel_Writer_OpenDocument_Settings' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Settings::class, + 'PHPExcel_Writer_OpenDocument_Styles' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Styles::class, + 'PHPExcel_Writer_OpenDocument_Thumbnails' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails::class, + 'PHPExcel_Writer_OpenDocument_WriterPart' => \PhpOffice\PhpSpreadsheet\Writer\Ods\WriterPart::class, + 'PHPExcel_Writer_PDF_Core' => \PhpOffice\PhpSpreadsheet\Writer\Pdf::class, + 'PHPExcel_Writer_PDF_DomPDF' => \PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf::class, + 'PHPExcel_Writer_PDF_mPDF' => \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class, + 'PHPExcel_Writer_PDF_tcPDF' => \PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf::class, + 'PHPExcel_Writer_Excel5_BIFFwriter' => \PhpOffice\PhpSpreadsheet\Writer\Xls\BIFFwriter::class, + 'PHPExcel_Writer_Excel5_Escher' => \PhpOffice\PhpSpreadsheet\Writer\Xls\Escher::class, + 'PHPExcel_Writer_Excel5_Font' => \PhpOffice\PhpSpreadsheet\Writer\Xls\Font::class, + 'PHPExcel_Writer_Excel5_Parser' => \PhpOffice\PhpSpreadsheet\Writer\Xls\Parser::class, + 'PHPExcel_Writer_Excel5_Workbook' => \PhpOffice\PhpSpreadsheet\Writer\Xls\Workbook::class, + 'PHPExcel_Writer_Excel5_Worksheet' => \PhpOffice\PhpSpreadsheet\Writer\Xls\Worksheet::class, + 'PHPExcel_Writer_Excel5_Xf' => \PhpOffice\PhpSpreadsheet\Writer\Xls\Xf::class, + 'PHPExcel_Writer_Excel2007_Chart' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Chart::class, + 'PHPExcel_Writer_Excel2007_Comments' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Comments::class, + 'PHPExcel_Writer_Excel2007_ContentTypes' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\ContentTypes::class, + 'PHPExcel_Writer_Excel2007_DocProps' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\DocProps::class, + 'PHPExcel_Writer_Excel2007_Drawing' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Drawing::class, + 'PHPExcel_Writer_Excel2007_Rels' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Rels::class, + 'PHPExcel_Writer_Excel2007_RelsRibbon' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\RelsRibbon::class, + 'PHPExcel_Writer_Excel2007_RelsVBA' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\RelsVBA::class, + 'PHPExcel_Writer_Excel2007_StringTable' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\StringTable::class, + 'PHPExcel_Writer_Excel2007_Style' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Style::class, + 'PHPExcel_Writer_Excel2007_Theme' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Theme::class, + 'PHPExcel_Writer_Excel2007_Workbook' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Workbook::class, + 'PHPExcel_Writer_Excel2007_Worksheet' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet::class, + 'PHPExcel_Writer_Excel2007_WriterPart' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx\WriterPart::class, + 'PHPExcel_CachedObjectStorage_CacheBase' => \PhpOffice\PhpSpreadsheet\Collection\Cells::class, + 'PHPExcel_CalcEngine_CyclicReferenceStack' => \PhpOffice\PhpSpreadsheet\Calculation\Engine\CyclicReferenceStack::class, + 'PHPExcel_CalcEngine_Logger' => \PhpOffice\PhpSpreadsheet\Calculation\Engine\Logger::class, + 'PHPExcel_Calculation_Functions' => \PhpOffice\PhpSpreadsheet\Calculation\Functions::class, + 'PHPExcel_Calculation_Function' => \PhpOffice\PhpSpreadsheet\Calculation\Category::class, + 'PHPExcel_Calculation_Database' => \PhpOffice\PhpSpreadsheet\Calculation\Database::class, + 'PHPExcel_Calculation_DateTime' => \PhpOffice\PhpSpreadsheet\Calculation\DateTime::class, + 'PHPExcel_Calculation_Engineering' => \PhpOffice\PhpSpreadsheet\Calculation\Engineering::class, + 'PHPExcel_Calculation_Exception' => \PhpOffice\PhpSpreadsheet\Calculation\Exception::class, + 'PHPExcel_Calculation_ExceptionHandler' => \PhpOffice\PhpSpreadsheet\Calculation\ExceptionHandler::class, + 'PHPExcel_Calculation_Financial' => \PhpOffice\PhpSpreadsheet\Calculation\Financial::class, + 'PHPExcel_Calculation_FormulaParser' => \PhpOffice\PhpSpreadsheet\Calculation\FormulaParser::class, + 'PHPExcel_Calculation_FormulaToken' => \PhpOffice\PhpSpreadsheet\Calculation\FormulaToken::class, + 'PHPExcel_Calculation_Logical' => \PhpOffice\PhpSpreadsheet\Calculation\Logical::class, + 'PHPExcel_Calculation_LookupRef' => \PhpOffice\PhpSpreadsheet\Calculation\LookupRef::class, + 'PHPExcel_Calculation_MathTrig' => \PhpOffice\PhpSpreadsheet\Calculation\MathTrig::class, + 'PHPExcel_Calculation_Statistical' => \PhpOffice\PhpSpreadsheet\Calculation\Statistical::class, + 'PHPExcel_Calculation_TextData' => \PhpOffice\PhpSpreadsheet\Calculation\TextData::class, + 'PHPExcel_Cell_AdvancedValueBinder' => \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder::class, + 'PHPExcel_Cell_DataType' => \PhpOffice\PhpSpreadsheet\Cell\DataType::class, + 'PHPExcel_Cell_DataValidation' => \PhpOffice\PhpSpreadsheet\Cell\DataValidation::class, + 'PHPExcel_Cell_DefaultValueBinder' => \PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder::class, + 'PHPExcel_Cell_Hyperlink' => \PhpOffice\PhpSpreadsheet\Cell\Hyperlink::class, + 'PHPExcel_Cell_IValueBinder' => \PhpOffice\PhpSpreadsheet\Cell\IValueBinder::class, + 'PHPExcel_Chart_Axis' => \PhpOffice\PhpSpreadsheet\Chart\Axis::class, + 'PHPExcel_Chart_DataSeries' => \PhpOffice\PhpSpreadsheet\Chart\DataSeries::class, + 'PHPExcel_Chart_DataSeriesValues' => \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues::class, + 'PHPExcel_Chart_Exception' => \PhpOffice\PhpSpreadsheet\Chart\Exception::class, + 'PHPExcel_Chart_GridLines' => \PhpOffice\PhpSpreadsheet\Chart\GridLines::class, + 'PHPExcel_Chart_Layout' => \PhpOffice\PhpSpreadsheet\Chart\Layout::class, + 'PHPExcel_Chart_Legend' => \PhpOffice\PhpSpreadsheet\Chart\Legend::class, + 'PHPExcel_Chart_PlotArea' => \PhpOffice\PhpSpreadsheet\Chart\PlotArea::class, + 'PHPExcel_Properties' => \PhpOffice\PhpSpreadsheet\Chart\Properties::class, + 'PHPExcel_Chart_Title' => \PhpOffice\PhpSpreadsheet\Chart\Title::class, + 'PHPExcel_DocumentProperties' => \PhpOffice\PhpSpreadsheet\Document\Properties::class, + 'PHPExcel_DocumentSecurity' => \PhpOffice\PhpSpreadsheet\Document\Security::class, + 'PHPExcel_Helper_HTML' => \PhpOffice\PhpSpreadsheet\Helper\Html::class, + 'PHPExcel_Reader_Abstract' => \PhpOffice\PhpSpreadsheet\Reader\BaseReader::class, + 'PHPExcel_Reader_CSV' => \PhpOffice\PhpSpreadsheet\Reader\Csv::class, + 'PHPExcel_Reader_DefaultReadFilter' => \PhpOffice\PhpSpreadsheet\Reader\DefaultReadFilter::class, + 'PHPExcel_Reader_Excel2003XML' => \PhpOffice\PhpSpreadsheet\Reader\Xml::class, + 'PHPExcel_Reader_Exception' => \PhpOffice\PhpSpreadsheet\Reader\Exception::class, + 'PHPExcel_Reader_Gnumeric' => \PhpOffice\PhpSpreadsheet\Reader\Gnumeric::class, + 'PHPExcel_Reader_HTML' => \PhpOffice\PhpSpreadsheet\Reader\Html::class, + 'PHPExcel_Reader_IReadFilter' => \PhpOffice\PhpSpreadsheet\Reader\IReadFilter::class, + 'PHPExcel_Reader_IReader' => \PhpOffice\PhpSpreadsheet\Reader\IReader::class, + 'PHPExcel_Reader_OOCalc' => \PhpOffice\PhpSpreadsheet\Reader\Ods::class, + 'PHPExcel_Reader_SYLK' => \PhpOffice\PhpSpreadsheet\Reader\Slk::class, + 'PHPExcel_Reader_Excel5' => \PhpOffice\PhpSpreadsheet\Reader\Xls::class, + 'PHPExcel_Reader_Excel2007' => \PhpOffice\PhpSpreadsheet\Reader\Xlsx::class, + 'PHPExcel_RichText_ITextElement' => \PhpOffice\PhpSpreadsheet\RichText\ITextElement::class, + 'PHPExcel_RichText_Run' => \PhpOffice\PhpSpreadsheet\RichText\Run::class, + 'PHPExcel_RichText_TextElement' => \PhpOffice\PhpSpreadsheet\RichText\TextElement::class, + 'PHPExcel_Shared_CodePage' => \PhpOffice\PhpSpreadsheet\Shared\CodePage::class, + 'PHPExcel_Shared_Date' => \PhpOffice\PhpSpreadsheet\Shared\Date::class, + 'PHPExcel_Shared_Drawing' => \PhpOffice\PhpSpreadsheet\Shared\Drawing::class, + 'PHPExcel_Shared_Escher' => \PhpOffice\PhpSpreadsheet\Shared\Escher::class, + 'PHPExcel_Shared_File' => \PhpOffice\PhpSpreadsheet\Shared\File::class, + 'PHPExcel_Shared_Font' => \PhpOffice\PhpSpreadsheet\Shared\Font::class, + 'PHPExcel_Shared_OLE' => \PhpOffice\PhpSpreadsheet\Shared\OLE::class, + 'PHPExcel_Shared_OLERead' => \PhpOffice\PhpSpreadsheet\Shared\OLERead::class, + 'PHPExcel_Shared_PasswordHasher' => \PhpOffice\PhpSpreadsheet\Shared\PasswordHasher::class, + 'PHPExcel_Shared_String' => \PhpOffice\PhpSpreadsheet\Shared\StringHelper::class, + 'PHPExcel_Shared_TimeZone' => \PhpOffice\PhpSpreadsheet\Shared\TimeZone::class, + 'PHPExcel_Shared_XMLWriter' => \PhpOffice\PhpSpreadsheet\Shared\XMLWriter::class, + 'PHPExcel_Shared_Excel5' => \PhpOffice\PhpSpreadsheet\Shared\Xls::class, + 'PHPExcel_Style_Alignment' => \PhpOffice\PhpSpreadsheet\Style\Alignment::class, + 'PHPExcel_Style_Border' => \PhpOffice\PhpSpreadsheet\Style\Border::class, + 'PHPExcel_Style_Borders' => \PhpOffice\PhpSpreadsheet\Style\Borders::class, + 'PHPExcel_Style_Color' => \PhpOffice\PhpSpreadsheet\Style\Color::class, + 'PHPExcel_Style_Conditional' => \PhpOffice\PhpSpreadsheet\Style\Conditional::class, + 'PHPExcel_Style_Fill' => \PhpOffice\PhpSpreadsheet\Style\Fill::class, + 'PHPExcel_Style_Font' => \PhpOffice\PhpSpreadsheet\Style\Font::class, + 'PHPExcel_Style_NumberFormat' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::class, + 'PHPExcel_Style_Protection' => \PhpOffice\PhpSpreadsheet\Style\Protection::class, + 'PHPExcel_Style_Supervisor' => \PhpOffice\PhpSpreadsheet\Style\Supervisor::class, + 'PHPExcel_Worksheet_AutoFilter' => \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter::class, + 'PHPExcel_Worksheet_BaseDrawing' => \PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing::class, + 'PHPExcel_Worksheet_CellIterator' => \PhpOffice\PhpSpreadsheet\Worksheet\CellIterator::class, + 'PHPExcel_Worksheet_Column' => \PhpOffice\PhpSpreadsheet\Worksheet\Column::class, + 'PHPExcel_Worksheet_ColumnCellIterator' => \PhpOffice\PhpSpreadsheet\Worksheet\ColumnCellIterator::class, + 'PHPExcel_Worksheet_ColumnDimension' => \PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension::class, + 'PHPExcel_Worksheet_ColumnIterator' => \PhpOffice\PhpSpreadsheet\Worksheet\ColumnIterator::class, + 'PHPExcel_Worksheet_Drawing' => \PhpOffice\PhpSpreadsheet\Worksheet\Drawing::class, + 'PHPExcel_Worksheet_HeaderFooter' => \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::class, + 'PHPExcel_Worksheet_HeaderFooterDrawing' => \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing::class, + 'PHPExcel_WorksheetIterator' => \PhpOffice\PhpSpreadsheet\Worksheet\Iterator::class, + 'PHPExcel_Worksheet_MemoryDrawing' => \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::class, + 'PHPExcel_Worksheet_PageMargins' => \PhpOffice\PhpSpreadsheet\Worksheet\PageMargins::class, + 'PHPExcel_Worksheet_PageSetup' => \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::class, + 'PHPExcel_Worksheet_Protection' => \PhpOffice\PhpSpreadsheet\Worksheet\Protection::class, + 'PHPExcel_Worksheet_Row' => \PhpOffice\PhpSpreadsheet\Worksheet\Row::class, + 'PHPExcel_Worksheet_RowCellIterator' => \PhpOffice\PhpSpreadsheet\Worksheet\RowCellIterator::class, + 'PHPExcel_Worksheet_RowDimension' => \PhpOffice\PhpSpreadsheet\Worksheet\RowDimension::class, + 'PHPExcel_Worksheet_RowIterator' => \PhpOffice\PhpSpreadsheet\Worksheet\RowIterator::class, + 'PHPExcel_Worksheet_SheetView' => \PhpOffice\PhpSpreadsheet\Worksheet\SheetView::class, + 'PHPExcel_Writer_Abstract' => \PhpOffice\PhpSpreadsheet\Writer\BaseWriter::class, + 'PHPExcel_Writer_CSV' => \PhpOffice\PhpSpreadsheet\Writer\Csv::class, + 'PHPExcel_Writer_Exception' => \PhpOffice\PhpSpreadsheet\Writer\Exception::class, + 'PHPExcel_Writer_HTML' => \PhpOffice\PhpSpreadsheet\Writer\Html::class, + 'PHPExcel_Writer_IWriter' => \PhpOffice\PhpSpreadsheet\Writer\IWriter::class, + 'PHPExcel_Writer_OpenDocument' => \PhpOffice\PhpSpreadsheet\Writer\Ods::class, + 'PHPExcel_Writer_PDF' => \PhpOffice\PhpSpreadsheet\Writer\Pdf::class, + 'PHPExcel_Writer_Excel5' => \PhpOffice\PhpSpreadsheet\Writer\Xls::class, + 'PHPExcel_Writer_Excel2007' => \PhpOffice\PhpSpreadsheet\Writer\Xlsx::class, + 'PHPExcel_CachedObjectStorageFactory' => \PhpOffice\PhpSpreadsheet\Collection\CellsFactory::class, + 'PHPExcel_Calculation' => \PhpOffice\PhpSpreadsheet\Calculation\Calculation::class, + 'PHPExcel_Cell' => \PhpOffice\PhpSpreadsheet\Cell\Cell::class, + 'PHPExcel_Chart' => \PhpOffice\PhpSpreadsheet\Chart\Chart::class, + 'PHPExcel_Comment' => \PhpOffice\PhpSpreadsheet\Comment::class, + 'PHPExcel_Exception' => \PhpOffice\PhpSpreadsheet\Exception::class, + 'PHPExcel_HashTable' => \PhpOffice\PhpSpreadsheet\HashTable::class, + 'PHPExcel_IComparable' => \PhpOffice\PhpSpreadsheet\IComparable::class, + 'PHPExcel_IOFactory' => \PhpOffice\PhpSpreadsheet\IOFactory::class, + 'PHPExcel_NamedRange' => \PhpOffice\PhpSpreadsheet\NamedRange::class, + 'PHPExcel_ReferenceHelper' => \PhpOffice\PhpSpreadsheet\ReferenceHelper::class, + 'PHPExcel_RichText' => \PhpOffice\PhpSpreadsheet\RichText\RichText::class, + 'PHPExcel_Settings' => \PhpOffice\PhpSpreadsheet\Settings::class, + 'PHPExcel_Style' => \PhpOffice\PhpSpreadsheet\Style\Style::class, + 'PHPExcel_Worksheet' => \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::class, + ]; + + $methods = [ + 'MINUTEOFHOUR' => 'MINUTE', + 'SECONDOFMINUTE' => 'SECOND', + 'DAYOFWEEK' => 'WEEKDAY', + 'WEEKOFYEAR' => 'WEEKNUM', + 'ExcelToPHPObject' => 'excelToDateTimeObject', + 'ExcelToPHP' => 'excelToTimestamp', + 'FormattedPHPToExcel' => 'formattedPHPToExcel', + 'Cell::absoluteCoordinate' => 'Coordinate::absoluteCoordinate', + 'Cell::absoluteReference' => 'Coordinate::absoluteReference', + 'Cell::buildRange' => 'Coordinate::buildRange', + 'Cell::columnIndexFromString' => 'Coordinate::columnIndexFromString', + 'Cell::coordinateFromString' => 'Coordinate::coordinateFromString', + 'Cell::extractAllCellReferencesInRange' => 'Coordinate::extractAllCellReferencesInRange', + 'Cell::getRangeBoundaries' => 'Coordinate::getRangeBoundaries', + 'Cell::mergeRangesInCollection' => 'Coordinate::mergeRangesInCollection', + 'Cell::rangeBoundaries' => 'Coordinate::rangeBoundaries', + 'Cell::rangeDimension' => 'Coordinate::rangeDimension', + 'Cell::splitRange' => 'Coordinate::splitRange', + 'Cell::stringFromColumnIndex' => 'Coordinate::stringFromColumnIndex', + ]; + + // Keep '\' prefix for class names + $prefixedClasses = []; + foreach ($classes as $key => &$value) { + $value = str_replace('PhpOffice\\', '\\PhpOffice\\', $value); + $prefixedClasses['\\' . $key] = $value; + } + $mapping = $prefixedClasses + $classes + $methods; + + return $mapping; + } + + /** + * Search in all files in given directory. + * + * @param string $path + */ + private function recursiveReplace($path) + { + $patterns = [ + '/*.md', + '/*.txt', + '/*.TXT', + '/*.php', + '/*.phpt', + '/*.php3', + '/*.php4', + '/*.php5', + '/*.phtml', + ]; + + foreach ($patterns as $pattern) { + foreach (glob($path . $pattern) as $file) { + if (strpos($path, '/vendor/') !== false) { + echo $file . " skipped\n"; + + continue; + } + $original = file_get_contents($file); + $converted = $this->replace($original); + + if ($original !== $converted) { + echo $file . " converted\n"; + file_put_contents($file, $converted); + } + } + } + + // Do the recursion in subdirectory + foreach (glob($path . '/*', GLOB_ONLYDIR) as $subpath) { + if (strpos($subpath, $path . '/') === 0) { + $this->recursiveReplace($subpath); + } + } + } + + public function migrate() + { + $path = realpath(getcwd()); + echo 'This will search and replace recursively in ' . $path . PHP_EOL; + echo 'You MUST backup your files first, or you risk losing data.' . PHP_EOL; + echo 'Are you sure ? (y/n)'; + + $confirm = fread(STDIN, 1); + if ($confirm === 'y') { + $this->recursiveReplace($path); + } + } + + /** + * Migrate the given code from PHPExcel to PhpSpreadsheet. + * + * @param string $original + * + * @return string + */ + public function replace($original) + { + $converted = str_replace($this->from, $this->to, $original); + + // The string "PHPExcel" gets special treatment because of how common it might be. + // This regex requires a word boundary around the string, and it can't be + // preceded by $ or -> (goal is to filter out cases where a variable is named $PHPExcel or similar) + $converted = preg_replace('~(?)(\b|\\\\)PHPExcel\b~', '\\' . \PhpOffice\PhpSpreadsheet\Spreadsheet::class, $converted); + + return $converted; + } +} diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Helper/Sample.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Helper/Sample.php new file mode 100644 index 00000000000..e199c807c0d --- /dev/null +++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Helper/Sample.php @@ -0,0 +1,230 @@ +getScriptFilename() === 'index'; + } + + /** + * Return the page title. + * + * @return string + */ + public function getPageTitle() + { + return $this->isIndex() ? 'PHPSpreadsheet' : $this->getScriptFilename(); + } + + /** + * Return the page heading. + * + * @return string + */ + public function getPageHeading() + { + return $this->isIndex() ? '' : ' ' . str_replace('_', ' ', $this->getScriptFilename()) . ''; + } + + /** + * Returns an array of all known samples. + * + * @return string[] [$name => $path] + */ + public function getSamples() + { + // Populate samples + $baseDir = realpath(__DIR__ . '/../../../samples'); + $directory = new RecursiveDirectoryIterator($baseDir); + $iterator = new RecursiveIteratorIterator($directory); + $regex = new RegexIterator($iterator, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH); + + $files = []; + foreach ($regex as $file) { + $file = str_replace(str_replace('\\', '/', $baseDir) . '/', '', str_replace('\\', '/', $file[0])); + $info = pathinfo($file); + $category = str_replace('_', ' ', $info['dirname']); + $name = str_replace('_', ' ', preg_replace('/(|\.php)/', '', $info['filename'])); + if (!in_array($category, ['.', 'boostrap', 'templates'])) { + if (!isset($files[$category])) { + $files[$category] = []; + } + $files[$category][$name] = $file; + } + } + + // Sort everything + ksort($files); + foreach ($files as &$f) { + asort($f); + } + + return $files; + } + + /** + * Write documents. + * + * @param Spreadsheet $spreadsheet + * @param string $filename + * @param string[] $writers + */ + public function write(Spreadsheet $spreadsheet, $filename, array $writers = ['Xlsx', 'Xls']) + { + // Set active sheet index to the first sheet, so Excel opens this as the first sheet + $spreadsheet->setActiveSheetIndex(0); + + // Write documents + foreach ($writers as $writerType) { + $path = $this->getFilename($filename, mb_strtolower($writerType)); + $writer = IOFactory::createWriter($spreadsheet, $writerType); + if ($writer instanceof Pdf) { + // PDF writer needs temporary directory + $tempDir = $this->getTemporaryFolder(); + $writer->setTempDir($tempDir); + } + $callStartTime = microtime(true); + $writer->save($path); + $this->logWrite($writer, $path, $callStartTime); + } + + $this->logEndingNotes(); + } + + /** + * Returns the temporary directory and make sure it exists. + * + * @return string + */ + private function getTemporaryFolder() + { + $tempFolder = sys_get_temp_dir() . '/phpspreadsheet'; + if (!is_dir($tempFolder)) { + if (!mkdir($tempFolder) && !is_dir($tempFolder)) { + throw new \RuntimeException(sprintf('Directory "%s" was not created', $tempFolder)); + } + } + + return $tempFolder; + } + + /** + * Returns the filename that should be used for sample output. + * + * @param string $filename + * @param string $extension + * + * @return string + */ + public function getFilename($filename, $extension = 'xlsx') + { + $originalExtension = pathinfo($filename, PATHINFO_EXTENSION); + + return $this->getTemporaryFolder() . '/' . str_replace('.' . $originalExtension, '.' . $extension, basename($filename)); + } + + /** + * Return a random temporary file name. + * + * @param string $extension + * + * @return string + */ + public function getTemporaryFilename($extension = 'xlsx') + { + $temporaryFilename = tempnam($this->getTemporaryFolder(), 'phpspreadsheet-'); + unlink($temporaryFilename); + + return $temporaryFilename . '.' . $extension; + } + + public function log($message) + { + $eol = $this->isCli() ? PHP_EOL : ''; + echo date('H:i:s ') . $message . $eol; + } + + /** + * Log ending notes. + */ + public function logEndingNotes() + { + // Do not show execution time for index + $this->log('Peak memory usage: ' . (memory_get_peak_usage(true) / 1024 / 1024) . 'MB'); + } + + /** + * Log a line about the write operation. + * + * @param IWriter $writer + * @param string $path + * @param float $callStartTime + */ + public function logWrite(IWriter $writer, $path, $callStartTime) + { + $callEndTime = microtime(true); + $callTime = $callEndTime - $callStartTime; + $reflection = new ReflectionClass($writer); + $format = $reflection->getShortName(); + $message = "Write {$format} format to {$path} in " . sprintf('%.4f', $callTime) . ' seconds';
+
+ $this->log($message);
+ }
+
+ /**
+ * Log a line about the read operation.
+ *
+ * @param string $format
+ * @param string $path
+ * @param float $callStartTime
+ */
+ public function logRead($format, $path, $callStartTime)
+ {
+ $callEndTime = microtime(true);
+ $callTime = $callEndTime - $callStartTime;
+ $message = "Read {$format} format from {$path} in " . sprintf('%.4f', $callTime) . ' seconds';
+
+ $this->log($message);
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/IComparable.php b/htdocs/includes/phpoffice/PhpSpreadsheet/IComparable.php
new file mode 100644
index 00000000000..c215847b316
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/IComparable.php
@@ -0,0 +1,13 @@
+ Reader\Xlsx::class,
+ 'Xls' => Reader\Xls::class,
+ 'Xml' => Reader\Xml::class,
+ 'Ods' => Reader\Ods::class,
+ 'Slk' => Reader\Slk::class,
+ 'Gnumeric' => Reader\Gnumeric::class,
+ 'Html' => Reader\Html::class,
+ 'Csv' => Reader\Csv::class,
+ ];
+
+ private static $writers = [
+ 'Xls' => Writer\Xls::class,
+ 'Xlsx' => Writer\Xlsx::class,
+ 'Ods' => Writer\Ods::class,
+ 'Csv' => Writer\Csv::class,
+ 'Html' => Writer\Html::class,
+ 'Tcpdf' => Writer\Pdf\Tcpdf::class,
+ 'Dompdf' => Writer\Pdf\Dompdf::class,
+ 'Mpdf' => Writer\Pdf\Mpdf::class,
+ ];
+
+ /**
+ * Create Writer\IWriter.
+ *
+ * @param Spreadsheet $spreadsheet
+ * @param string $writerType Example: Xlsx
+ *
+ * @throws Writer\Exception
+ *
+ * @return Writer\IWriter
+ */
+ public static function createWriter(Spreadsheet $spreadsheet, $writerType)
+ {
+ if (!isset(self::$writers[$writerType])) {
+ throw new Writer\Exception("No writer found for type $writerType");
+ }
+
+ // Instantiate writer
+ $className = self::$writers[$writerType];
+ $writer = new $className($spreadsheet);
+
+ return $writer;
+ }
+
+ /**
+ * Create Reader\IReader.
+ *
+ * @param string $readerType Example: Xlsx
+ *
+ * @throws Reader\Exception
+ *
+ * @return Reader\IReader
+ */
+ public static function createReader($readerType)
+ {
+ if (!isset(self::$readers[$readerType])) {
+ throw new Reader\Exception("No reader found for type $readerType");
+ }
+
+ // Instantiate reader
+ $className = self::$readers[$readerType];
+ $reader = new $className();
+
+ return $reader;
+ }
+
+ /**
+ * Loads Spreadsheet from file using automatic Reader\IReader resolution.
+ *
+ * @param string $pFilename The name of the spreadsheet file
+ *
+ * @throws Reader\Exception
+ *
+ * @return Spreadsheet
+ */
+ public static function load($pFilename)
+ {
+ $reader = self::createReaderForFile($pFilename);
+
+ return $reader->load($pFilename);
+ }
+
+ /**
+ * Identify file type using automatic Reader\IReader resolution.
+ *
+ * @param string $pFilename The name of the spreadsheet file to identify
+ *
+ * @throws Reader\Exception
+ *
+ * @return string
+ */
+ public static function identify($pFilename)
+ {
+ $reader = self::createReaderForFile($pFilename);
+ $className = get_class($reader);
+ $classType = explode('\\', $className);
+ unset($reader);
+
+ return array_pop($classType);
+ }
+
+ /**
+ * Create Reader\IReader for file using automatic Reader\IReader resolution.
+ *
+ * @param string $filename The name of the spreadsheet file
+ *
+ * @throws Reader\Exception
+ *
+ * @return Reader\IReader
+ */
+ public static function createReaderForFile($filename)
+ {
+ File::assertFile($filename);
+
+ // First, lucky guess by inspecting file extension
+ $guessedReader = self::getReaderTypeFromExtension($filename);
+ if ($guessedReader !== null) {
+ $reader = self::createReader($guessedReader);
+
+ // Let's see if we are lucky
+ if (isset($reader) && $reader->canRead($filename)) {
+ return $reader;
+ }
+ }
+
+ // If we reach here then "lucky guess" didn't give any result
+ // Try walking through all the options in self::$autoResolveClasses
+ foreach (self::$readers as $type => $class) {
+ // Ignore our original guess, we know that won't work
+ if ($type !== $guessedReader) {
+ $reader = self::createReader($type);
+ if ($reader->canRead($filename)) {
+ return $reader;
+ }
+ }
+ }
+
+ throw new Reader\Exception('Unable to identify a reader for this file');
+ }
+
+ /**
+ * Guess a reader type from the file extension, if any.
+ *
+ * @param string $filename
+ *
+ * @return null|string
+ */
+ private static function getReaderTypeFromExtension($filename)
+ {
+ $pathinfo = pathinfo($filename);
+ if (!isset($pathinfo['extension'])) {
+ return null;
+ }
+
+ switch (strtolower($pathinfo['extension'])) {
+ case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet
+ case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)
+ case 'xltx': // Excel (OfficeOpenXML) Template
+ case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded)
+ return 'Xlsx';
+ case 'xls': // Excel (BIFF) Spreadsheet
+ case 'xlt': // Excel (BIFF) Template
+ return 'Xls';
+ case 'ods': // Open/Libre Offic Calc
+ case 'ots': // Open/Libre Offic Calc Template
+ return 'Ods';
+ case 'slk':
+ return 'Slk';
+ case 'xml': // Excel 2003 SpreadSheetML
+ return 'Xml';
+ case 'gnumeric':
+ return 'Gnumeric';
+ case 'htm':
+ case 'html':
+ return 'Html';
+ case 'csv':
+ // Do nothing
+ // We must not try to use CSV reader since it loads
+ // all files including Excel files etc.
+ return null;
+ default:
+ return null;
+ }
+ }
+
+ /**
+ * Register a writer with its type and class name.
+ *
+ * @param string $writerType
+ * @param string $writerClass
+ */
+ public static function registerWriter($writerType, $writerClass)
+ {
+ if (!is_a($writerClass, Writer\IWriter::class, true)) {
+ throw new Writer\Exception('Registered writers must implement ' . Writer\IWriter::class);
+ }
+
+ self::$writers[$writerType] = $writerClass;
+ }
+
+ /**
+ * Register a reader with its type and class name.
+ *
+ * @param string $readerType
+ * @param string $readerClass
+ */
+ public static function registerReader($readerType, $readerClass)
+ {
+ if (!is_a($readerClass, Reader\IReader::class, true)) {
+ throw new Reader\Exception('Registered readers must implement ' . Reader\IReader::class);
+ }
+
+ self::$readers[$readerType] = $readerClass;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/NamedRange.php b/htdocs/includes/phpoffice/PhpSpreadsheet/NamedRange.php
new file mode 100644
index 00000000000..1f94d5a4ed2
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/NamedRange.php
@@ -0,0 +1,240 @@
+worksheet).
+ *
+ * @var bool
+ */
+ private $localOnly;
+
+ /**
+ * Scope.
+ *
+ * @var Worksheet
+ */
+ private $scope;
+
+ /**
+ * Create a new NamedRange.
+ *
+ * @param string $pName
+ * @param Worksheet $pWorksheet
+ * @param string $pRange
+ * @param bool $pLocalOnly
+ * @param null|Worksheet $pScope Scope. Only applies when $pLocalOnly = true. Null for global scope.
+ *
+ * @throws Exception
+ */
+ public function __construct($pName, Worksheet $pWorksheet, $pRange = 'A1', $pLocalOnly = false, $pScope = null)
+ {
+ // Validate data
+ if (($pName === null) || ($pWorksheet === null) || ($pRange === null)) {
+ throw new Exception('Parameters can not be null.');
+ }
+
+ // Set local members
+ $this->name = $pName;
+ $this->worksheet = $pWorksheet;
+ $this->range = $pRange;
+ $this->localOnly = $pLocalOnly;
+ $this->scope = ($pLocalOnly == true) ? (($pScope == null) ? $pWorksheet : $pScope) : null;
+ }
+
+ /**
+ * Get name.
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * Set name.
+ *
+ * @param string $value
+ *
+ * @return NamedRange
+ */
+ public function setName($value)
+ {
+ if ($value !== null) {
+ // Old title
+ $oldTitle = $this->name;
+
+ // Re-attach
+ if ($this->worksheet !== null) {
+ $this->worksheet->getParent()->removeNamedRange($this->name, $this->worksheet);
+ }
+ $this->name = $value;
+
+ if ($this->worksheet !== null) {
+ $this->worksheet->getParent()->addNamedRange($this);
+ }
+
+ // New title
+ $newTitle = $this->name;
+ ReferenceHelper::getInstance()->updateNamedFormulas($this->worksheet->getParent(), $oldTitle, $newTitle);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Get worksheet.
+ *
+ * @return Worksheet
+ */
+ public function getWorksheet()
+ {
+ return $this->worksheet;
+ }
+
+ /**
+ * Set worksheet.
+ *
+ * @param Worksheet $value
+ *
+ * @return NamedRange
+ */
+ public function setWorksheet(Worksheet $value = null)
+ {
+ if ($value !== null) {
+ $this->worksheet = $value;
+ }
+
+ return $this;
+ }
+
+ /**
+ * Get range.
+ *
+ * @return string
+ */
+ public function getRange()
+ {
+ return $this->range;
+ }
+
+ /**
+ * Set range.
+ *
+ * @param string $value
+ *
+ * @return NamedRange
+ */
+ public function setRange($value)
+ {
+ if ($value !== null) {
+ $this->range = $value;
+ }
+
+ return $this;
+ }
+
+ /**
+ * Get localOnly.
+ *
+ * @return bool
+ */
+ public function getLocalOnly()
+ {
+ return $this->localOnly;
+ }
+
+ /**
+ * Set localOnly.
+ *
+ * @param bool $value
+ *
+ * @return NamedRange
+ */
+ public function setLocalOnly($value)
+ {
+ $this->localOnly = $value;
+ $this->scope = $value ? $this->worksheet : null;
+
+ return $this;
+ }
+
+ /**
+ * Get scope.
+ *
+ * @return null|Worksheet
+ */
+ public function getScope()
+ {
+ return $this->scope;
+ }
+
+ /**
+ * Set scope.
+ *
+ * @param null|Worksheet $value
+ *
+ * @return NamedRange
+ */
+ public function setScope(Worksheet $value = null)
+ {
+ $this->scope = $value;
+ $this->localOnly = $value != null;
+
+ return $this;
+ }
+
+ /**
+ * Resolve a named range to a regular cell range.
+ *
+ * @param string $pNamedRange Named range
+ * @param null|Worksheet $pSheet Scope. Use null for global scope
+ *
+ * @return NamedRange
+ */
+ public static function resolveRange($pNamedRange, Worksheet $pSheet)
+ {
+ return $pSheet->getParent()->getNamedRange($pNamedRange, $pSheet);
+ }
+
+ /**
+ * Implement PHP __clone to create a deep clone, not just a shallow copy.
+ */
+ public function __clone()
+ {
+ $vars = get_object_vars($this);
+ foreach ($vars as $key => $value) {
+ if (is_object($value)) {
+ $this->$key = clone $value;
+ } else {
+ $this->$key = $value;
+ }
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/BaseReader.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/BaseReader.php
new file mode 100644
index 00000000000..c191c3f8d8e
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/BaseReader.php
@@ -0,0 +1,239 @@
+readDataOnly;
+ }
+
+ /**
+ * Set read data only
+ * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
+ * Set to false (the default) to advise the Reader to read both data and formatting for cells.
+ *
+ * @param bool $pValue
+ *
+ * @return IReader
+ */
+ public function setReadDataOnly($pValue)
+ {
+ $this->readDataOnly = (bool) $pValue;
+
+ return $this;
+ }
+
+ /**
+ * Read empty cells?
+ * If this is true (the default), then the Reader will read data values for all cells, irrespective of value.
+ * If false it will not read data for cells containing a null value or an empty string.
+ *
+ * @return bool
+ */
+ public function getReadEmptyCells()
+ {
+ return $this->readEmptyCells;
+ }
+
+ /**
+ * Set read empty cells
+ * Set to true (the default) to advise the Reader read data values for all cells, irrespective of value.
+ * Set to false to advise the Reader to ignore cells containing a null value or an empty string.
+ *
+ * @param bool $pValue
+ *
+ * @return IReader
+ */
+ public function setReadEmptyCells($pValue)
+ {
+ $this->readEmptyCells = (bool) $pValue;
+
+ return $this;
+ }
+
+ /**
+ * Read charts in workbook?
+ * If this is true, then the Reader will include any charts that exist in the workbook.
+ * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
+ * If false (the default) it will ignore any charts defined in the workbook file.
+ *
+ * @return bool
+ */
+ public function getIncludeCharts()
+ {
+ return $this->includeCharts;
+ }
+
+ /**
+ * Set read charts in workbook
+ * Set to true, to advise the Reader to include any charts that exist in the workbook.
+ * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
+ * Set to false (the default) to discard charts.
+ *
+ * @param bool $pValue
+ *
+ * @return IReader
+ */
+ public function setIncludeCharts($pValue)
+ {
+ $this->includeCharts = (bool) $pValue;
+
+ return $this;
+ }
+
+ /**
+ * Get which sheets to load
+ * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
+ * indicating that all worksheets in the workbook should be loaded.
+ *
+ * @return mixed
+ */
+ public function getLoadSheetsOnly()
+ {
+ return $this->loadSheetsOnly;
+ }
+
+ /**
+ * Set which sheets to load.
+ *
+ * @param mixed $value
+ * This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
+ * If NULL, then it tells the Reader to read all worksheets in the workbook
+ *
+ * @return IReader
+ */
+ public function setLoadSheetsOnly($value)
+ {
+ if ($value === null) {
+ return $this->setLoadAllSheets();
+ }
+
+ $this->loadSheetsOnly = is_array($value) ? $value : [$value];
+
+ return $this;
+ }
+
+ /**
+ * Set all sheets to load
+ * Tells the Reader to load all worksheets from the workbook.
+ *
+ * @return IReader
+ */
+ public function setLoadAllSheets()
+ {
+ $this->loadSheetsOnly = null;
+
+ return $this;
+ }
+
+ /**
+ * Read filter.
+ *
+ * @return IReadFilter
+ */
+ public function getReadFilter()
+ {
+ return $this->readFilter;
+ }
+
+ /**
+ * Set read filter.
+ *
+ * @param IReadFilter $pValue
+ *
+ * @return IReader
+ */
+ public function setReadFilter(IReadFilter $pValue)
+ {
+ $this->readFilter = $pValue;
+
+ return $this;
+ }
+
+ public function getSecuritySCanner()
+ {
+ if (property_exists($this, 'securityScanner')) {
+ return $this->securityScanner;
+ }
+
+ return null;
+ }
+
+ /**
+ * Open file for reading.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ */
+ protected function openFile($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ // Open file
+ $this->fileHandle = fopen($pFilename, 'r');
+ if ($this->fileHandle === false) {
+ throw new Exception('Could not open file ' . $pFilename . ' for reading.');
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Csv.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Csv.php
new file mode 100644
index 00000000000..25f244c1181
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Csv.php
@@ -0,0 +1,564 @@
+readFilter = new DefaultReadFilter();
+ }
+
+ /**
+ * Set input encoding.
+ *
+ * @param string $pValue Input encoding, eg: 'UTF-8'
+ *
+ * @return Csv
+ */
+ public function setInputEncoding($pValue)
+ {
+ $this->inputEncoding = $pValue;
+
+ return $this;
+ }
+
+ /**
+ * Get input encoding.
+ *
+ * @return string
+ */
+ public function getInputEncoding()
+ {
+ return $this->inputEncoding;
+ }
+
+ /**
+ * Move filepointer past any BOM marker.
+ */
+ protected function skipBOM()
+ {
+ rewind($this->fileHandle);
+
+ switch ($this->inputEncoding) {
+ case 'UTF-8':
+ fgets($this->fileHandle, 4) == "\xEF\xBB\xBF" ?
+ fseek($this->fileHandle, 3) : fseek($this->fileHandle, 0);
+
+ break;
+ case 'UTF-16LE':
+ fgets($this->fileHandle, 3) == "\xFF\xFE" ?
+ fseek($this->fileHandle, 2) : fseek($this->fileHandle, 0);
+
+ break;
+ case 'UTF-16BE':
+ fgets($this->fileHandle, 3) == "\xFE\xFF" ?
+ fseek($this->fileHandle, 2) : fseek($this->fileHandle, 0);
+
+ break;
+ case 'UTF-32LE':
+ fgets($this->fileHandle, 5) == "\xFF\xFE\x00\x00" ?
+ fseek($this->fileHandle, 4) : fseek($this->fileHandle, 0);
+
+ break;
+ case 'UTF-32BE':
+ fgets($this->fileHandle, 5) == "\x00\x00\xFE\xFF" ?
+ fseek($this->fileHandle, 4) : fseek($this->fileHandle, 0);
+
+ break;
+ default:
+ break;
+ }
+ }
+
+ /**
+ * Identify any separator that is explicitly set in the file.
+ */
+ protected function checkSeparator()
+ {
+ $line = fgets($this->fileHandle);
+ if ($line === false) {
+ return;
+ }
+
+ if ((strlen(trim($line, "\r\n")) == 5) && (stripos($line, 'sep=') === 0)) {
+ $this->delimiter = substr($line, 4, 1);
+
+ return;
+ }
+
+ return $this->skipBOM();
+ }
+
+ /**
+ * Infer the separator if it isn't explicitly set in the file or specified by the user.
+ */
+ protected function inferSeparator()
+ {
+ if ($this->delimiter !== null) {
+ return;
+ }
+
+ $potentialDelimiters = [',', ';', "\t", '|', ':', ' '];
+ $counts = [];
+ foreach ($potentialDelimiters as $delimiter) {
+ $counts[$delimiter] = [];
+ }
+
+ // Count how many times each of the potential delimiters appears in each line
+ $numberLines = 0;
+ while (($line = $this->getNextLine()) !== false && (++$numberLines < 1000)) {
+ $countLine = [];
+ for ($i = strlen($line) - 1; $i >= 0; --$i) {
+ $char = $line[$i];
+ if (isset($counts[$char])) {
+ if (!isset($countLine[$char])) {
+ $countLine[$char] = 0;
+ }
+ ++$countLine[$char];
+ }
+ }
+ foreach ($potentialDelimiters as $delimiter) {
+ $counts[$delimiter][] = isset($countLine[$delimiter])
+ ? $countLine[$delimiter]
+ : 0;
+ }
+ }
+
+ // If number of lines is 0, nothing to infer : fall back to the default
+ if ($numberLines === 0) {
+ $this->delimiter = reset($potentialDelimiters);
+
+ return $this->skipBOM();
+ }
+
+ // Calculate the mean square deviations for each delimiter (ignoring delimiters that haven't been found consistently)
+ $meanSquareDeviations = [];
+ $middleIdx = floor(($numberLines - 1) / 2);
+
+ foreach ($potentialDelimiters as $delimiter) {
+ $series = $counts[$delimiter];
+ sort($series);
+
+ $median = ($numberLines % 2)
+ ? $series[$middleIdx]
+ : ($series[$middleIdx] + $series[$middleIdx + 1]) / 2;
+
+ if ($median === 0) {
+ continue;
+ }
+
+ $meanSquareDeviations[$delimiter] = array_reduce(
+ $series,
+ function ($sum, $value) use ($median) {
+ return $sum + pow($value - $median, 2);
+ }
+ ) / count($series);
+ }
+
+ // ... and pick the delimiter with the smallest mean square deviation (in case of ties, the order in potentialDelimiters is respected)
+ $min = INF;
+ foreach ($potentialDelimiters as $delimiter) {
+ if (!isset($meanSquareDeviations[$delimiter])) {
+ continue;
+ }
+
+ if ($meanSquareDeviations[$delimiter] < $min) {
+ $min = $meanSquareDeviations[$delimiter];
+ $this->delimiter = $delimiter;
+ }
+ }
+
+ // If no delimiter could be detected, fall back to the default
+ if ($this->delimiter === null) {
+ $this->delimiter = reset($potentialDelimiters);
+ }
+
+ return $this->skipBOM();
+ }
+
+ /**
+ * Get the next full line from the file.
+ *
+ * @param string $line
+ *
+ * @return bool|string
+ */
+ private function getNextLine($line = '')
+ {
+ // Get the next line in the file
+ $newLine = fgets($this->fileHandle);
+
+ // Return false if there is no next line
+ if ($newLine === false) {
+ return false;
+ }
+
+ // Add the new line to the line passed in
+ $line = $line . $newLine;
+
+ // Drop everything that is enclosed to avoid counting false positives in enclosures
+ $enclosure = preg_quote($this->enclosure, '/');
+ $line = preg_replace('/(' . $enclosure . '.*' . $enclosure . ')/U', '', $line);
+
+ // See if we have any enclosures left in the line
+ $matches = [];
+ preg_match('/(' . $enclosure . ')/', $line, $matches);
+
+ // if we still have an enclosure then we need to read the next line aswell
+ if (count($matches) > 0) {
+ $line = $this->getNextLine($line);
+ }
+
+ return $line;
+ }
+
+ /**
+ * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ public function listWorksheetInfo($pFilename)
+ {
+ // Open file
+ if (!$this->canRead($pFilename)) {
+ throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
+ }
+ $this->openFile($pFilename);
+ $fileHandle = $this->fileHandle;
+
+ // Skip BOM, if any
+ $this->skipBOM();
+ $this->checkSeparator();
+ $this->inferSeparator();
+
+ $worksheetInfo = [];
+ $worksheetInfo[0]['worksheetName'] = 'Worksheet';
+ $worksheetInfo[0]['lastColumnLetter'] = 'A';
+ $worksheetInfo[0]['lastColumnIndex'] = 0;
+ $worksheetInfo[0]['totalRows'] = 0;
+ $worksheetInfo[0]['totalColumns'] = 0;
+
+ // Loop through each line of the file in turn
+ while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure, $this->escapeCharacter)) !== false) {
+ ++$worksheetInfo[0]['totalRows'];
+ $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
+ }
+
+ $worksheetInfo[0]['lastColumnLetter'] = Coordinate::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex'] + 1);
+ $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
+
+ // Close file
+ fclose($fileHandle);
+
+ return $worksheetInfo;
+ }
+
+ /**
+ * Loads Spreadsheet from file.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function load($pFilename)
+ {
+ // Create new Spreadsheet
+ $spreadsheet = new Spreadsheet();
+
+ // Load into this instance
+ return $this->loadIntoExisting($pFilename, $spreadsheet);
+ }
+
+ /**
+ * Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
+ *
+ * @param string $pFilename
+ * @param Spreadsheet $spreadsheet
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
+ {
+ $lineEnding = ini_get('auto_detect_line_endings');
+ ini_set('auto_detect_line_endings', true);
+
+ // Open file
+ if (!$this->canRead($pFilename)) {
+ throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
+ }
+ $this->openFile($pFilename);
+ $fileHandle = $this->fileHandle;
+
+ // Skip BOM, if any
+ $this->skipBOM();
+ $this->checkSeparator();
+ $this->inferSeparator();
+
+ // Create new PhpSpreadsheet object
+ while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
+ $spreadsheet->createSheet();
+ }
+ $sheet = $spreadsheet->setActiveSheetIndex($this->sheetIndex);
+
+ // Set our starting row based on whether we're in contiguous mode or not
+ $currentRow = 1;
+ if ($this->contiguous) {
+ $currentRow = ($this->contiguousRow == -1) ? $sheet->getHighestRow() : $this->contiguousRow;
+ }
+
+ // Loop through each line of the file in turn
+ while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure, $this->escapeCharacter)) !== false) {
+ $columnLetter = 'A';
+ foreach ($rowData as $rowDatum) {
+ if ($rowDatum != '' && $this->readFilter->readCell($columnLetter, $currentRow)) {
+ // Convert encoding if necessary
+ if ($this->inputEncoding !== 'UTF-8') {
+ $rowDatum = StringHelper::convertEncoding($rowDatum, 'UTF-8', $this->inputEncoding);
+ }
+
+ // Set cell value
+ $sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
+ }
+ ++$columnLetter;
+ }
+ ++$currentRow;
+ }
+
+ // Close file
+ fclose($fileHandle);
+
+ if ($this->contiguous) {
+ $this->contiguousRow = $currentRow;
+ }
+
+ ini_set('auto_detect_line_endings', $lineEnding);
+
+ // Return
+ return $spreadsheet;
+ }
+
+ /**
+ * Get delimiter.
+ *
+ * @return string
+ */
+ public function getDelimiter()
+ {
+ return $this->delimiter;
+ }
+
+ /**
+ * Set delimiter.
+ *
+ * @param string $delimiter Delimiter, eg: ','
+ *
+ * @return CSV
+ */
+ public function setDelimiter($delimiter)
+ {
+ $this->delimiter = $delimiter;
+
+ return $this;
+ }
+
+ /**
+ * Get enclosure.
+ *
+ * @return string
+ */
+ public function getEnclosure()
+ {
+ return $this->enclosure;
+ }
+
+ /**
+ * Set enclosure.
+ *
+ * @param string $enclosure Enclosure, defaults to "
+ *
+ * @return CSV
+ */
+ public function setEnclosure($enclosure)
+ {
+ if ($enclosure == '') {
+ $enclosure = '"';
+ }
+ $this->enclosure = $enclosure;
+
+ return $this;
+ }
+
+ /**
+ * Get sheet index.
+ *
+ * @return int
+ */
+ public function getSheetIndex()
+ {
+ return $this->sheetIndex;
+ }
+
+ /**
+ * Set sheet index.
+ *
+ * @param int $pValue Sheet index
+ *
+ * @return CSV
+ */
+ public function setSheetIndex($pValue)
+ {
+ $this->sheetIndex = $pValue;
+
+ return $this;
+ }
+
+ /**
+ * Set Contiguous.
+ *
+ * @param bool $contiguous
+ *
+ * @return Csv
+ */
+ public function setContiguous($contiguous)
+ {
+ $this->contiguous = (bool) $contiguous;
+ if (!$contiguous) {
+ $this->contiguousRow = -1;
+ }
+
+ return $this;
+ }
+
+ /**
+ * Get Contiguous.
+ *
+ * @return bool
+ */
+ public function getContiguous()
+ {
+ return $this->contiguous;
+ }
+
+ /**
+ * Set escape backslashes.
+ *
+ * @param string $escapeCharacter
+ *
+ * @return $this
+ */
+ public function setEscapeCharacter($escapeCharacter)
+ {
+ $this->escapeCharacter = $escapeCharacter;
+
+ return $this;
+ }
+
+ /**
+ * Get escape backslashes.
+ *
+ * @return string
+ */
+ public function getEscapeCharacter()
+ {
+ return $this->escapeCharacter;
+ }
+
+ /**
+ * Can the current IReader read the file?
+ *
+ * @param string $pFilename
+ *
+ * @return bool
+ */
+ public function canRead($pFilename)
+ {
+ // Check if file exists
+ try {
+ $this->openFile($pFilename);
+ } catch (Exception $e) {
+ return false;
+ }
+
+ fclose($this->fileHandle);
+
+ // Trust file extension if any
+ if (strtolower(pathinfo($pFilename, PATHINFO_EXTENSION)) === 'csv') {
+ return true;
+ }
+
+ // Attempt to guess mimetype
+ $type = mime_content_type($pFilename);
+ $supportedTypes = [
+ 'text/csv',
+ 'text/plain',
+ 'inode/x-empty',
+ ];
+
+ return in_array($type, $supportedTypes, true);
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/DefaultReadFilter.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/DefaultReadFilter.php
new file mode 100644
index 00000000000..e104186abbb
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/DefaultReadFilter.php
@@ -0,0 +1,20 @@
+readFilter = new DefaultReadFilter();
+ $this->referenceHelper = ReferenceHelper::getInstance();
+ $this->securityScanner = XmlScanner::getInstance($this);
+ }
+
+ /**
+ * Can the current IReader read the file?
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return bool
+ */
+ public function canRead($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ // Check if gzlib functions are available
+ if (!function_exists('gzread')) {
+ throw new Exception('gzlib library is not enabled');
+ }
+
+ // Read signature data (first 3 bytes)
+ $fh = fopen($pFilename, 'r');
+ $data = fread($fh, 2);
+ fclose($fh);
+
+ return $data == chr(0x1F) . chr(0x8B);
+ }
+
+ /**
+ * Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object.
+ *
+ * @param string $pFilename
+ *
+ * @return array
+ */
+ public function listWorksheetNames($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $xml = new XMLReader();
+ $xml->xml($this->securityScanner->scanFile('compress.zlib://' . realpath($pFilename)), null, Settings::getLibXmlLoaderOptions());
+ $xml->setParserProperty(2, true);
+
+ $worksheetNames = [];
+ while ($xml->read()) {
+ if ($xml->name == 'gnm:SheetName' && $xml->nodeType == XMLReader::ELEMENT) {
+ $xml->read(); // Move onto the value node
+ $worksheetNames[] = (string) $xml->value;
+ } elseif ($xml->name == 'gnm:Sheets') {
+ // break out of the loop once we've got our sheet names rather than parse the entire file
+ break;
+ }
+ }
+
+ return $worksheetNames;
+ }
+
+ /**
+ * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
+ *
+ * @param string $pFilename
+ *
+ * @return array
+ */
+ public function listWorksheetInfo($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $xml = new XMLReader();
+ $xml->xml($this->securityScanner->scanFile('compress.zlib://' . realpath($pFilename)), null, Settings::getLibXmlLoaderOptions());
+ $xml->setParserProperty(2, true);
+
+ $worksheetInfo = [];
+ while ($xml->read()) {
+ if ($xml->name == 'gnm:Sheet' && $xml->nodeType == XMLReader::ELEMENT) {
+ $tmpInfo = [
+ 'worksheetName' => '',
+ 'lastColumnLetter' => 'A',
+ 'lastColumnIndex' => 0,
+ 'totalRows' => 0,
+ 'totalColumns' => 0,
+ ];
+
+ while ($xml->read()) {
+ if ($xml->name == 'gnm:Name' && $xml->nodeType == XMLReader::ELEMENT) {
+ $xml->read(); // Move onto the value node
+ $tmpInfo['worksheetName'] = (string) $xml->value;
+ } elseif ($xml->name == 'gnm:MaxCol' && $xml->nodeType == XMLReader::ELEMENT) {
+ $xml->read(); // Move onto the value node
+ $tmpInfo['lastColumnIndex'] = (int) $xml->value;
+ $tmpInfo['totalColumns'] = (int) $xml->value + 1;
+ } elseif ($xml->name == 'gnm:MaxRow' && $xml->nodeType == XMLReader::ELEMENT) {
+ $xml->read(); // Move onto the value node
+ $tmpInfo['totalRows'] = (int) $xml->value + 1;
+
+ break;
+ }
+ }
+ $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
+ $worksheetInfo[] = $tmpInfo;
+ }
+ }
+
+ return $worksheetInfo;
+ }
+
+ /**
+ * @param string $filename
+ *
+ * @return string
+ */
+ private function gzfileGetContents($filename)
+ {
+ $file = @gzopen($filename, 'rb');
+ $data = '';
+ if ($file !== false) {
+ while (!gzeof($file)) {
+ $data .= gzread($file, 1024);
+ }
+ gzclose($file);
+ }
+
+ return $data;
+ }
+
+ /**
+ * Loads Spreadsheet from file.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function load($pFilename)
+ {
+ // Create new Spreadsheet
+ $spreadsheet = new Spreadsheet();
+
+ // Load into this instance
+ return $this->loadIntoExisting($pFilename, $spreadsheet);
+ }
+
+ /**
+ * Loads from file into Spreadsheet instance.
+ *
+ * @param string $pFilename
+ * @param Spreadsheet $spreadsheet
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
+ {
+ File::assertFile($pFilename);
+
+ $gFileData = $this->gzfileGetContents($pFilename);
+
+ $xml = simplexml_load_string($this->securityScanner->scan($gFileData), 'SimpleXMLElement', Settings::getLibXmlLoaderOptions());
+ $namespacesMeta = $xml->getNamespaces(true);
+
+ $gnmXML = $xml->children($namespacesMeta['gnm']);
+
+ $docProps = $spreadsheet->getProperties();
+ // Document Properties are held differently, depending on the version of Gnumeric
+ if (isset($namespacesMeta['office'])) {
+ $officeXML = $xml->children($namespacesMeta['office']);
+ $officeDocXML = $officeXML->{'document-meta'};
+ $officeDocMetaXML = $officeDocXML->meta;
+
+ foreach ($officeDocMetaXML as $officePropertyData) {
+ $officePropertyDC = [];
+ if (isset($namespacesMeta['dc'])) {
+ $officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
+ }
+ foreach ($officePropertyDC as $propertyName => $propertyValue) {
+ $propertyValue = (string) $propertyValue;
+ switch ($propertyName) {
+ case 'title':
+ $docProps->setTitle(trim($propertyValue));
+
+ break;
+ case 'subject':
+ $docProps->setSubject(trim($propertyValue));
+
+ break;
+ case 'creator':
+ $docProps->setCreator(trim($propertyValue));
+ $docProps->setLastModifiedBy(trim($propertyValue));
+
+ break;
+ case 'date':
+ $creationDate = strtotime(trim($propertyValue));
+ $docProps->setCreated($creationDate);
+ $docProps->setModified($creationDate);
+
+ break;
+ case 'description':
+ $docProps->setDescription(trim($propertyValue));
+
+ break;
+ }
+ }
+ $officePropertyMeta = [];
+ if (isset($namespacesMeta['meta'])) {
+ $officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
+ }
+ foreach ($officePropertyMeta as $propertyName => $propertyValue) {
+ $attributes = $propertyValue->attributes($namespacesMeta['meta']);
+ $propertyValue = (string) $propertyValue;
+ switch ($propertyName) {
+ case 'keyword':
+ $docProps->setKeywords(trim($propertyValue));
+
+ break;
+ case 'initial-creator':
+ $docProps->setCreator(trim($propertyValue));
+ $docProps->setLastModifiedBy(trim($propertyValue));
+
+ break;
+ case 'creation-date':
+ $creationDate = strtotime(trim($propertyValue));
+ $docProps->setCreated($creationDate);
+ $docProps->setModified($creationDate);
+
+ break;
+ case 'user-defined':
+ list(, $attrName) = explode(':', $attributes['name']);
+ switch ($attrName) {
+ case 'publisher':
+ $docProps->setCompany(trim($propertyValue));
+
+ break;
+ case 'category':
+ $docProps->setCategory(trim($propertyValue));
+
+ break;
+ case 'manager':
+ $docProps->setManager(trim($propertyValue));
+
+ break;
+ }
+
+ break;
+ }
+ }
+ }
+ } elseif (isset($gnmXML->Summary)) {
+ foreach ($gnmXML->Summary->Item as $summaryItem) {
+ $propertyName = $summaryItem->name;
+ $propertyValue = $summaryItem->{'val-string'};
+ switch ($propertyName) {
+ case 'title':
+ $docProps->setTitle(trim($propertyValue));
+
+ break;
+ case 'comments':
+ $docProps->setDescription(trim($propertyValue));
+
+ break;
+ case 'keywords':
+ $docProps->setKeywords(trim($propertyValue));
+
+ break;
+ case 'category':
+ $docProps->setCategory(trim($propertyValue));
+
+ break;
+ case 'manager':
+ $docProps->setManager(trim($propertyValue));
+
+ break;
+ case 'author':
+ $docProps->setCreator(trim($propertyValue));
+ $docProps->setLastModifiedBy(trim($propertyValue));
+
+ break;
+ case 'company':
+ $docProps->setCompany(trim($propertyValue));
+
+ break;
+ }
+ }
+ }
+
+ $worksheetID = 0;
+ foreach ($gnmXML->Sheets->Sheet as $sheet) {
+ $worksheetName = (string) $sheet->Name;
+ if ((isset($this->loadSheetsOnly)) && (!in_array($worksheetName, $this->loadSheetsOnly))) {
+ continue;
+ }
+
+ $maxRow = $maxCol = 0;
+
+ // Create new Worksheet
+ $spreadsheet->createSheet();
+ $spreadsheet->setActiveSheetIndex($worksheetID);
+ // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula
+ // cells... during the load, all formulae should be correct, and we're simply bringing the worksheet
+ // name in line with the formula, not the reverse
+ $spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false);
+
+ if ((!$this->readDataOnly) && (isset($sheet->PrintInformation))) {
+ if (isset($sheet->PrintInformation->Margins)) {
+ foreach ($sheet->PrintInformation->Margins->children('gnm', true) as $key => $margin) {
+ $marginAttributes = $margin->attributes();
+ $marginSize = 72 / 100; // Default
+ switch ($marginAttributes['PrefUnit']) {
+ case 'mm':
+ $marginSize = (int) ($marginAttributes['Points']) / 100;
+
+ break;
+ }
+ switch ($key) {
+ case 'top':
+ $spreadsheet->getActiveSheet()->getPageMargins()->setTop($marginSize);
+
+ break;
+ case 'bottom':
+ $spreadsheet->getActiveSheet()->getPageMargins()->setBottom($marginSize);
+
+ break;
+ case 'left':
+ $spreadsheet->getActiveSheet()->getPageMargins()->setLeft($marginSize);
+
+ break;
+ case 'right':
+ $spreadsheet->getActiveSheet()->getPageMargins()->setRight($marginSize);
+
+ break;
+ case 'header':
+ $spreadsheet->getActiveSheet()->getPageMargins()->setHeader($marginSize);
+
+ break;
+ case 'footer':
+ $spreadsheet->getActiveSheet()->getPageMargins()->setFooter($marginSize);
+
+ break;
+ }
+ }
+ }
+ }
+
+ foreach ($sheet->Cells->Cell as $cell) {
+ $cellAttributes = $cell->attributes();
+ $row = (int) $cellAttributes->Row + 1;
+ $column = (int) $cellAttributes->Col;
+
+ if ($row > $maxRow) {
+ $maxRow = $row;
+ }
+ if ($column > $maxCol) {
+ $maxCol = $column;
+ }
+
+ $column = Coordinate::stringFromColumnIndex($column + 1);
+
+ // Read cell?
+ if ($this->getReadFilter() !== null) {
+ if (!$this->getReadFilter()->readCell($column, $row, $worksheetName)) {
+ continue;
+ }
+ }
+
+ $ValueType = $cellAttributes->ValueType;
+ $ExprID = (string) $cellAttributes->ExprID;
+ $type = DataType::TYPE_FORMULA;
+ if ($ExprID > '') {
+ if (((string) $cell) > '') {
+ $this->expressions[$ExprID] = [
+ 'column' => $cellAttributes->Col,
+ 'row' => $cellAttributes->Row,
+ 'formula' => (string) $cell,
+ ];
+ } else {
+ $expression = $this->expressions[$ExprID];
+
+ $cell = $this->referenceHelper->updateFormulaReferences(
+ $expression['formula'],
+ 'A1',
+ $cellAttributes->Col - $expression['column'],
+ $cellAttributes->Row - $expression['row'],
+ $worksheetName
+ );
+ }
+ $type = DataType::TYPE_FORMULA;
+ } else {
+ switch ($ValueType) {
+ case '10': // NULL
+ $type = DataType::TYPE_NULL;
+
+ break;
+ case '20': // Boolean
+ $type = DataType::TYPE_BOOL;
+ $cell = $cell == 'TRUE';
+
+ break;
+ case '30': // Integer
+ $cell = (int) $cell;
+ // Excel 2007+ doesn't differentiate between integer and float, so set the value and dropthru to the next (numeric) case
+ // no break
+ case '40': // Float
+ $type = DataType::TYPE_NUMERIC;
+
+ break;
+ case '50': // Error
+ $type = DataType::TYPE_ERROR;
+
+ break;
+ case '60': // String
+ $type = DataType::TYPE_STRING;
+
+ break;
+ case '70': // Cell Range
+ case '80': // Array
+ }
+ }
+ $spreadsheet->getActiveSheet()->getCell($column . $row)->setValueExplicit($cell, $type);
+ }
+
+ if ((!$this->readDataOnly) && (isset($sheet->Objects))) {
+ foreach ($sheet->Objects->children('gnm', true) as $key => $comment) {
+ $commentAttributes = $comment->attributes();
+ // Only comment objects are handled at the moment
+ if ($commentAttributes->Text) {
+ $spreadsheet->getActiveSheet()->getComment((string) $commentAttributes->ObjectBound)->setAuthor((string) $commentAttributes->Author)->setText($this->parseRichText((string) $commentAttributes->Text));
+ }
+ }
+ }
+ foreach ($sheet->Styles->StyleRegion as $styleRegion) {
+ $styleAttributes = $styleRegion->attributes();
+ if (($styleAttributes['startRow'] <= $maxRow) &&
+ ($styleAttributes['startCol'] <= $maxCol)) {
+ $startColumn = Coordinate::stringFromColumnIndex((int) $styleAttributes['startCol'] + 1);
+ $startRow = $styleAttributes['startRow'] + 1;
+
+ $endColumn = ($styleAttributes['endCol'] > $maxCol) ? $maxCol : (int) $styleAttributes['endCol'];
+ $endColumn = Coordinate::stringFromColumnIndex($endColumn + 1);
+ $endRow = ($styleAttributes['endRow'] > $maxRow) ? $maxRow : $styleAttributes['endRow'];
+ $endRow += 1;
+ $cellRange = $startColumn . $startRow . ':' . $endColumn . $endRow;
+
+ $styleAttributes = $styleRegion->Style->attributes();
+
+ // We still set the number format mask for date/time values, even if readDataOnly is true
+ if ((!$this->readDataOnly) ||
+ (Date::isDateTimeFormatCode((string) $styleAttributes['Format']))) {
+ $styleArray = [];
+ $styleArray['numberFormat']['formatCode'] = (string) $styleAttributes['Format'];
+ // If readDataOnly is false, we set all formatting information
+ if (!$this->readDataOnly) {
+ switch ($styleAttributes['HAlign']) {
+ case '1':
+ $styleArray['alignment']['horizontal'] = Alignment::HORIZONTAL_GENERAL;
+
+ break;
+ case '2':
+ $styleArray['alignment']['horizontal'] = Alignment::HORIZONTAL_LEFT;
+
+ break;
+ case '4':
+ $styleArray['alignment']['horizontal'] = Alignment::HORIZONTAL_RIGHT;
+
+ break;
+ case '8':
+ $styleArray['alignment']['horizontal'] = Alignment::HORIZONTAL_CENTER;
+
+ break;
+ case '16':
+ case '64':
+ $styleArray['alignment']['horizontal'] = Alignment::HORIZONTAL_CENTER_CONTINUOUS;
+
+ break;
+ case '32':
+ $styleArray['alignment']['horizontal'] = Alignment::HORIZONTAL_JUSTIFY;
+
+ break;
+ }
+
+ switch ($styleAttributes['VAlign']) {
+ case '1':
+ $styleArray['alignment']['vertical'] = Alignment::VERTICAL_TOP;
+
+ break;
+ case '2':
+ $styleArray['alignment']['vertical'] = Alignment::VERTICAL_BOTTOM;
+
+ break;
+ case '4':
+ $styleArray['alignment']['vertical'] = Alignment::VERTICAL_CENTER;
+
+ break;
+ case '8':
+ $styleArray['alignment']['vertical'] = Alignment::VERTICAL_JUSTIFY;
+
+ break;
+ }
+
+ $styleArray['alignment']['wrapText'] = $styleAttributes['WrapText'] == '1';
+ $styleArray['alignment']['shrinkToFit'] = $styleAttributes['ShrinkToFit'] == '1';
+ $styleArray['alignment']['indent'] = ((int) ($styleAttributes['Indent']) > 0) ? $styleAttributes['indent'] : 0;
+
+ $RGB = self::parseGnumericColour($styleAttributes['Fore']);
+ $styleArray['font']['color']['rgb'] = $RGB;
+ $RGB = self::parseGnumericColour($styleAttributes['Back']);
+ $shade = $styleAttributes['Shade'];
+ if (($RGB != '000000') || ($shade != '0')) {
+ $styleArray['fill']['color']['rgb'] = $styleArray['fill']['startColor']['rgb'] = $RGB;
+ $RGB2 = self::parseGnumericColour($styleAttributes['PatternColor']);
+ $styleArray['fill']['endColor']['rgb'] = $RGB2;
+ switch ($shade) {
+ case '1':
+ $styleArray['fill']['fillType'] = Fill::FILL_SOLID;
+
+ break;
+ case '2':
+ $styleArray['fill']['fillType'] = Fill::FILL_GRADIENT_LINEAR;
+
+ break;
+ case '3':
+ $styleArray['fill']['fillType'] = Fill::FILL_GRADIENT_PATH;
+
+ break;
+ case '4':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_DARKDOWN;
+
+ break;
+ case '5':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_DARKGRAY;
+
+ break;
+ case '6':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_DARKGRID;
+
+ break;
+ case '7':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_DARKHORIZONTAL;
+
+ break;
+ case '8':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_DARKTRELLIS;
+
+ break;
+ case '9':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_DARKUP;
+
+ break;
+ case '10':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_DARKVERTICAL;
+
+ break;
+ case '11':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_GRAY0625;
+
+ break;
+ case '12':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_GRAY125;
+
+ break;
+ case '13':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_LIGHTDOWN;
+
+ break;
+ case '14':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_LIGHTGRAY;
+
+ break;
+ case '15':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_LIGHTGRID;
+
+ break;
+ case '16':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_LIGHTHORIZONTAL;
+
+ break;
+ case '17':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_LIGHTTRELLIS;
+
+ break;
+ case '18':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_LIGHTUP;
+
+ break;
+ case '19':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_LIGHTVERTICAL;
+
+ break;
+ case '20':
+ $styleArray['fill']['fillType'] = Fill::FILL_PATTERN_MEDIUMGRAY;
+
+ break;
+ }
+ }
+
+ $fontAttributes = $styleRegion->Style->Font->attributes();
+ $styleArray['font']['name'] = (string) $styleRegion->Style->Font;
+ $styleArray['font']['size'] = (int) ($fontAttributes['Unit']);
+ $styleArray['font']['bold'] = $fontAttributes['Bold'] == '1';
+ $styleArray['font']['italic'] = $fontAttributes['Italic'] == '1';
+ $styleArray['font']['strikethrough'] = $fontAttributes['StrikeThrough'] == '1';
+ switch ($fontAttributes['Underline']) {
+ case '1':
+ $styleArray['font']['underline'] = Font::UNDERLINE_SINGLE;
+
+ break;
+ case '2':
+ $styleArray['font']['underline'] = Font::UNDERLINE_DOUBLE;
+
+ break;
+ case '3':
+ $styleArray['font']['underline'] = Font::UNDERLINE_SINGLEACCOUNTING;
+
+ break;
+ case '4':
+ $styleArray['font']['underline'] = Font::UNDERLINE_DOUBLEACCOUNTING;
+
+ break;
+ default:
+ $styleArray['font']['underline'] = Font::UNDERLINE_NONE;
+
+ break;
+ }
+ switch ($fontAttributes['Script']) {
+ case '1':
+ $styleArray['font']['superscript'] = true;
+
+ break;
+ case '-1':
+ $styleArray['font']['subscript'] = true;
+
+ break;
+ }
+
+ if (isset($styleRegion->Style->StyleBorder)) {
+ if (isset($styleRegion->Style->StyleBorder->Top)) {
+ $styleArray['borders']['top'] = self::parseBorderAttributes($styleRegion->Style->StyleBorder->Top->attributes());
+ }
+ if (isset($styleRegion->Style->StyleBorder->Bottom)) {
+ $styleArray['borders']['bottom'] = self::parseBorderAttributes($styleRegion->Style->StyleBorder->Bottom->attributes());
+ }
+ if (isset($styleRegion->Style->StyleBorder->Left)) {
+ $styleArray['borders']['left'] = self::parseBorderAttributes($styleRegion->Style->StyleBorder->Left->attributes());
+ }
+ if (isset($styleRegion->Style->StyleBorder->Right)) {
+ $styleArray['borders']['right'] = self::parseBorderAttributes($styleRegion->Style->StyleBorder->Right->attributes());
+ }
+ if ((isset($styleRegion->Style->StyleBorder->Diagonal)) && (isset($styleRegion->Style->StyleBorder->{'Rev-Diagonal'}))) {
+ $styleArray['borders']['diagonal'] = self::parseBorderAttributes($styleRegion->Style->StyleBorder->Diagonal->attributes());
+ $styleArray['borders']['diagonalDirection'] = Borders::DIAGONAL_BOTH;
+ } elseif (isset($styleRegion->Style->StyleBorder->Diagonal)) {
+ $styleArray['borders']['diagonal'] = self::parseBorderAttributes($styleRegion->Style->StyleBorder->Diagonal->attributes());
+ $styleArray['borders']['diagonalDirection'] = Borders::DIAGONAL_UP;
+ } elseif (isset($styleRegion->Style->StyleBorder->{'Rev-Diagonal'})) {
+ $styleArray['borders']['diagonal'] = self::parseBorderAttributes($styleRegion->Style->StyleBorder->{'Rev-Diagonal'}->attributes());
+ $styleArray['borders']['diagonalDirection'] = Borders::DIAGONAL_DOWN;
+ }
+ }
+ if (isset($styleRegion->Style->HyperLink)) {
+ // TO DO
+ $hyperlink = $styleRegion->Style->HyperLink->attributes();
+ }
+ }
+ $spreadsheet->getActiveSheet()->getStyle($cellRange)->applyFromArray($styleArray);
+ }
+ }
+ }
+
+ if ((!$this->readDataOnly) && (isset($sheet->Cols))) {
+ // Column Widths
+ $columnAttributes = $sheet->Cols->attributes();
+ $defaultWidth = $columnAttributes['DefaultSizePts'] / 5.4;
+ $c = 0;
+ foreach ($sheet->Cols->ColInfo as $columnOverride) {
+ $columnAttributes = $columnOverride->attributes();
+ $column = $columnAttributes['No'];
+ $columnWidth = $columnAttributes['Unit'] / 5.4;
+ $hidden = (isset($columnAttributes['Hidden'])) && ($columnAttributes['Hidden'] == '1');
+ $columnCount = (isset($columnAttributes['Count'])) ? $columnAttributes['Count'] : 1;
+ while ($c < $column) {
+ $spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($c + 1))->setWidth($defaultWidth);
+ ++$c;
+ }
+ while (($c < ($column + $columnCount)) && ($c <= $maxCol)) {
+ $spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($c + 1))->setWidth($columnWidth);
+ if ($hidden) {
+ $spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($c + 1))->setVisible(false);
+ }
+ ++$c;
+ }
+ }
+ while ($c <= $maxCol) {
+ $spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($c + 1))->setWidth($defaultWidth);
+ ++$c;
+ }
+ }
+
+ if ((!$this->readDataOnly) && (isset($sheet->Rows))) {
+ // Row Heights
+ $rowAttributes = $sheet->Rows->attributes();
+ $defaultHeight = $rowAttributes['DefaultSizePts'];
+ $r = 0;
+
+ foreach ($sheet->Rows->RowInfo as $rowOverride) {
+ $rowAttributes = $rowOverride->attributes();
+ $row = $rowAttributes['No'];
+ $rowHeight = $rowAttributes['Unit'];
+ $hidden = (isset($rowAttributes['Hidden'])) && ($rowAttributes['Hidden'] == '1');
+ $rowCount = (isset($rowAttributes['Count'])) ? $rowAttributes['Count'] : 1;
+ while ($r < $row) {
+ ++$r;
+ $spreadsheet->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight);
+ }
+ while (($r < ($row + $rowCount)) && ($r < $maxRow)) {
+ ++$r;
+ $spreadsheet->getActiveSheet()->getRowDimension($r)->setRowHeight($rowHeight);
+ if ($hidden) {
+ $spreadsheet->getActiveSheet()->getRowDimension($r)->setVisible(false);
+ }
+ }
+ }
+ while ($r < $maxRow) {
+ ++$r;
+ $spreadsheet->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight);
+ }
+ }
+
+ // Handle Merged Cells in this worksheet
+ if (isset($sheet->MergedRegions)) {
+ foreach ($sheet->MergedRegions->Merge as $mergeCells) {
+ if (strpos($mergeCells, ':') !== false) {
+ $spreadsheet->getActiveSheet()->mergeCells($mergeCells);
+ }
+ }
+ }
+
+ ++$worksheetID;
+ }
+
+ // Loop through definedNames (global named ranges)
+ if (isset($gnmXML->Names)) {
+ foreach ($gnmXML->Names->Name as $namedRange) {
+ $name = (string) $namedRange->name;
+ $range = (string) $namedRange->value;
+ if (stripos($range, '#REF!') !== false) {
+ continue;
+ }
+
+ $range = Worksheet::extractSheetTitle($range, true);
+ $range[0] = trim($range[0], "'");
+ if ($worksheet = $spreadsheet->getSheetByName($range[0])) {
+ $extractedRange = str_replace('$', '', $range[1]);
+ $spreadsheet->addNamedRange(new NamedRange($name, $worksheet, $extractedRange));
+ }
+ }
+ }
+
+ // Return
+ return $spreadsheet;
+ }
+
+ private static function parseBorderAttributes($borderAttributes)
+ {
+ $styleArray = [];
+ if (isset($borderAttributes['Color'])) {
+ $styleArray['color']['rgb'] = self::parseGnumericColour($borderAttributes['Color']);
+ }
+
+ switch ($borderAttributes['Style']) {
+ case '0':
+ $styleArray['borderStyle'] = Border::BORDER_NONE;
+
+ break;
+ case '1':
+ $styleArray['borderStyle'] = Border::BORDER_THIN;
+
+ break;
+ case '2':
+ $styleArray['borderStyle'] = Border::BORDER_MEDIUM;
+
+ break;
+ case '3':
+ $styleArray['borderStyle'] = Border::BORDER_SLANTDASHDOT;
+
+ break;
+ case '4':
+ $styleArray['borderStyle'] = Border::BORDER_DASHED;
+
+ break;
+ case '5':
+ $styleArray['borderStyle'] = Border::BORDER_THICK;
+
+ break;
+ case '6':
+ $styleArray['borderStyle'] = Border::BORDER_DOUBLE;
+
+ break;
+ case '7':
+ $styleArray['borderStyle'] = Border::BORDER_DOTTED;
+
+ break;
+ case '8':
+ $styleArray['borderStyle'] = Border::BORDER_MEDIUMDASHED;
+
+ break;
+ case '9':
+ $styleArray['borderStyle'] = Border::BORDER_DASHDOT;
+
+ break;
+ case '10':
+ $styleArray['borderStyle'] = Border::BORDER_MEDIUMDASHDOT;
+
+ break;
+ case '11':
+ $styleArray['borderStyle'] = Border::BORDER_DASHDOTDOT;
+
+ break;
+ case '12':
+ $styleArray['borderStyle'] = Border::BORDER_MEDIUMDASHDOTDOT;
+
+ break;
+ case '13':
+ $styleArray['borderStyle'] = Border::BORDER_MEDIUMDASHDOTDOT;
+
+ break;
+ }
+
+ return $styleArray;
+ }
+
+ private function parseRichText($is)
+ {
+ $value = new RichText();
+ $value->createText($is);
+
+ return $value;
+ }
+
+ private static function parseGnumericColour($gnmColour)
+ {
+ list($gnmR, $gnmG, $gnmB) = explode(':', $gnmColour);
+ $gnmR = substr(str_pad($gnmR, 4, '0', STR_PAD_RIGHT), 0, 2);
+ $gnmG = substr(str_pad($gnmG, 4, '0', STR_PAD_RIGHT), 0, 2);
+ $gnmB = substr(str_pad($gnmB, 4, '0', STR_PAD_RIGHT), 0, 2);
+
+ return $gnmR . $gnmG . $gnmB;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Html.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Html.php
new file mode 100644
index 00000000000..d9f25a3166d
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Html.php
@@ -0,0 +1,643 @@
+ [
+ 'font' => [
+ 'bold' => true,
+ 'size' => 24,
+ ],
+ ], // Bold, 24pt
+ 'h2' => [
+ 'font' => [
+ 'bold' => true,
+ 'size' => 18,
+ ],
+ ], // Bold, 18pt
+ 'h3' => [
+ 'font' => [
+ 'bold' => true,
+ 'size' => 13.5,
+ ],
+ ], // Bold, 13.5pt
+ 'h4' => [
+ 'font' => [
+ 'bold' => true,
+ 'size' => 12,
+ ],
+ ], // Bold, 12pt
+ 'h5' => [
+ 'font' => [
+ 'bold' => true,
+ 'size' => 10,
+ ],
+ ], // Bold, 10pt
+ 'h6' => [
+ 'font' => [
+ 'bold' => true,
+ 'size' => 7.5,
+ ],
+ ], // Bold, 7.5pt
+ 'a' => [
+ 'font' => [
+ 'underline' => true,
+ 'color' => [
+ 'argb' => Color::COLOR_BLUE,
+ ],
+ ],
+ ], // Blue underlined
+ 'hr' => [
+ 'borders' => [
+ 'bottom' => [
+ 'borderStyle' => Border::BORDER_THIN,
+ 'color' => [
+ Color::COLOR_BLACK,
+ ],
+ ],
+ ],
+ ], // Bottom border
+ ];
+
+ protected $rowspan = [];
+
+ /**
+ * Create a new HTML Reader instance.
+ */
+ public function __construct()
+ {
+ $this->readFilter = new DefaultReadFilter();
+ $this->securityScanner = XmlScanner::getInstance($this);
+ }
+
+ /**
+ * Validate that the current file is an HTML file.
+ *
+ * @param string $pFilename
+ *
+ * @return bool
+ */
+ public function canRead($pFilename)
+ {
+ // Check if file exists
+ try {
+ $this->openFile($pFilename);
+ } catch (Exception $e) {
+ return false;
+ }
+
+ $beginning = $this->readBeginning();
+ $startWithTag = self::startsWithTag($beginning);
+ $containsTags = self::containsTags($beginning);
+ $endsWithTag = self::endsWithTag($this->readEnding());
+
+ fclose($this->fileHandle);
+
+ return $startWithTag && $containsTags && $endsWithTag;
+ }
+
+ private function readBeginning()
+ {
+ fseek($this->fileHandle, 0);
+
+ return fread($this->fileHandle, self::TEST_SAMPLE_SIZE);
+ }
+
+ private function readEnding()
+ {
+ $meta = stream_get_meta_data($this->fileHandle);
+ $filename = $meta['uri'];
+
+ $size = filesize($filename);
+ if ($size === 0) {
+ return '';
+ }
+
+ $blockSize = self::TEST_SAMPLE_SIZE;
+ if ($size < $blockSize) {
+ $blockSize = $size;
+ }
+
+ fseek($this->fileHandle, $size - $blockSize);
+
+ return fread($this->fileHandle, $blockSize);
+ }
+
+ private static function startsWithTag($data)
+ {
+ return '<' === substr(trim($data), 0, 1);
+ }
+
+ private static function endsWithTag($data)
+ {
+ return '>' === substr(trim($data), -1, 1);
+ }
+
+ private static function containsTags($data)
+ {
+ return strlen($data) !== strlen(strip_tags($data));
+ }
+
+ /**
+ * Loads Spreadsheet from file.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function load($pFilename)
+ {
+ // Create new Spreadsheet
+ $spreadsheet = new Spreadsheet();
+
+ // Load into this instance
+ return $this->loadIntoExisting($pFilename, $spreadsheet);
+ }
+
+ /**
+ * Set input encoding.
+ *
+ * @param string $pValue Input encoding, eg: 'ANSI'
+ *
+ * @return Html
+ */
+ public function setInputEncoding($pValue)
+ {
+ $this->inputEncoding = $pValue;
+
+ return $this;
+ }
+
+ /**
+ * Get input encoding.
+ *
+ * @return string
+ */
+ public function getInputEncoding()
+ {
+ return $this->inputEncoding;
+ }
+
+ // Data Array used for testing only, should write to Spreadsheet object on completion of tests
+ protected $dataArray = [];
+
+ protected $tableLevel = 0;
+
+ protected $nestedColumn = ['A'];
+
+ protected function setTableStartColumn($column)
+ {
+ if ($this->tableLevel == 0) {
+ $column = 'A';
+ }
+ ++$this->tableLevel;
+ $this->nestedColumn[$this->tableLevel] = $column;
+
+ return $this->nestedColumn[$this->tableLevel];
+ }
+
+ protected function getTableStartColumn()
+ {
+ return $this->nestedColumn[$this->tableLevel];
+ }
+
+ protected function releaseTableStartColumn()
+ {
+ --$this->tableLevel;
+
+ return array_pop($this->nestedColumn);
+ }
+
+ protected function flushCell(Worksheet $sheet, $column, $row, &$cellContent)
+ {
+ if (is_string($cellContent)) {
+ // Simple String content
+ if (trim($cellContent) > '') {
+ // Only actually write it if there's content in the string
+ // Write to worksheet to be done here...
+ // ... we return the cell so we can mess about with styles more easily
+ $sheet->setCellValue($column . $row, $cellContent);
+ $this->dataArray[$row][$column] = $cellContent;
+ }
+ } else {
+ // We have a Rich Text run
+ // TODO
+ $this->dataArray[$row][$column] = 'RICH TEXT: ' . $cellContent;
+ }
+ $cellContent = (string) '';
+ }
+
+ /**
+ * @param DOMNode $element
+ * @param Worksheet $sheet
+ * @param int $row
+ * @param string $column
+ * @param string $cellContent
+ */
+ protected function processDomElement(DOMNode $element, Worksheet $sheet, &$row, &$column, &$cellContent)
+ {
+ foreach ($element->childNodes as $child) {
+ if ($child instanceof DOMText) {
+ $domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue));
+ if (is_string($cellContent)) {
+ // simply append the text if the cell content is a plain text string
+ $cellContent .= $domText;
+ }
+ // but if we have a rich text run instead, we need to append it correctly
+ // TODO
+ } elseif ($child instanceof DOMElement) {
+ $attributeArray = [];
+ foreach ($child->attributes as $attribute) {
+ $attributeArray[$attribute->name] = $attribute->value;
+ }
+
+ switch ($child->nodeName) {
+ case 'meta':
+ foreach ($attributeArray as $attributeName => $attributeValue) {
+ switch ($attributeName) {
+ case 'content':
+ // TODO
+ // Extract character set, so we can convert to UTF-8 if required
+ break;
+ }
+ }
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+
+ break;
+ case 'title':
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+ $sheet->setTitle($cellContent, true, false);
+ $cellContent = '';
+
+ break;
+ case 'span':
+ case 'div':
+ case 'font':
+ case 'i':
+ case 'em':
+ case 'strong':
+ case 'b':
+ if (isset($attributeArray['class']) && $attributeArray['class'] === 'comment') {
+ $sheet->getComment($column . $row)
+ ->getText()
+ ->createTextRun($child->textContent);
+
+ break;
+ }
+
+ if ($cellContent > '') {
+ $cellContent .= ' ';
+ }
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+ if ($cellContent > '') {
+ $cellContent .= ' ';
+ }
+
+ break;
+ case 'hr':
+ $this->flushCell($sheet, $column, $row, $cellContent);
+ ++$row;
+ if (isset($this->formats[$child->nodeName])) {
+ $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
+ } else {
+ $cellContent = '----------';
+ $this->flushCell($sheet, $column, $row, $cellContent);
+ }
+ ++$row;
+ // Add a break after a horizontal rule, simply by allowing the code to dropthru
+ // no break
+ case 'br':
+ if ($this->tableLevel > 0) {
+ // If we're inside a table, replace with a \n
+ $cellContent .= "\n";
+ } else {
+ // Otherwise flush our existing content and move the row cursor on
+ $this->flushCell($sheet, $column, $row, $cellContent);
+ ++$row;
+ }
+
+ break;
+ case 'a':
+ foreach ($attributeArray as $attributeName => $attributeValue) {
+ switch ($attributeName) {
+ case 'href':
+ $sheet->getCell($column . $row)->getHyperlink()->setUrl($attributeValue);
+ if (isset($this->formats[$child->nodeName])) {
+ $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
+ }
+
+ break;
+ case 'class':
+ if ($attributeValue === 'comment-indicator') {
+ break; // Ignore - it's just a red square.
+ }
+ }
+ }
+ $cellContent .= ' ';
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+
+ break;
+ case 'h1':
+ case 'h2':
+ case 'h3':
+ case 'h4':
+ case 'h5':
+ case 'h6':
+ case 'ol':
+ case 'ul':
+ case 'p':
+ if ($this->tableLevel > 0) {
+ // If we're inside a table, replace with a \n
+ $cellContent .= "\n";
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+ } else {
+ if ($cellContent > '') {
+ $this->flushCell($sheet, $column, $row, $cellContent);
+ ++$row;
+ }
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+ $this->flushCell($sheet, $column, $row, $cellContent);
+
+ if (isset($this->formats[$child->nodeName])) {
+ $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
+ }
+
+ ++$row;
+ $column = 'A';
+ }
+
+ break;
+ case 'li':
+ if ($this->tableLevel > 0) {
+ // If we're inside a table, replace with a \n
+ $cellContent .= "\n";
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+ } else {
+ if ($cellContent > '') {
+ $this->flushCell($sheet, $column, $row, $cellContent);
+ }
+ ++$row;
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+ $this->flushCell($sheet, $column, $row, $cellContent);
+ $column = 'A';
+ }
+
+ break;
+ case 'table':
+ $this->flushCell($sheet, $column, $row, $cellContent);
+ $column = $this->setTableStartColumn($column);
+ if ($this->tableLevel > 1) {
+ --$row;
+ }
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+ $column = $this->releaseTableStartColumn();
+ if ($this->tableLevel > 1) {
+ ++$column;
+ } else {
+ ++$row;
+ }
+
+ break;
+ case 'thead':
+ case 'tbody':
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+
+ break;
+ case 'tr':
+ $column = $this->getTableStartColumn();
+ $cellContent = '';
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+ ++$row;
+
+ break;
+ case 'th':
+ case 'td':
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+
+ // apply inline style
+ $this->applyInlineStyle($sheet, $row, $column, $attributeArray);
+
+ while (isset($this->rowspan[$column . $row])) {
+ ++$column;
+ }
+
+ $this->flushCell($sheet, $column, $row, $cellContent);
+
+ if (isset($attributeArray['rowspan'], $attributeArray['colspan'])) {
+ //create merging rowspan and colspan
+ $columnTo = $column;
+ for ($i = 0; $i < $attributeArray['colspan'] - 1; ++$i) {
+ ++$columnTo;
+ }
+ $range = $column . $row . ':' . $columnTo . ($row + $attributeArray['rowspan'] - 1);
+ foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) {
+ $this->rowspan[$value] = true;
+ }
+ $sheet->mergeCells($range);
+ $column = $columnTo;
+ } elseif (isset($attributeArray['rowspan'])) {
+ //create merging rowspan
+ $range = $column . $row . ':' . $column . ($row + $attributeArray['rowspan'] - 1);
+ foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) {
+ $this->rowspan[$value] = true;
+ }
+ $sheet->mergeCells($range);
+ } elseif (isset($attributeArray['colspan'])) {
+ //create merging colspan
+ $columnTo = $column;
+ for ($i = 0; $i < $attributeArray['colspan'] - 1; ++$i) {
+ ++$columnTo;
+ }
+ $sheet->mergeCells($column . $row . ':' . $columnTo . $row);
+ $column = $columnTo;
+ } elseif (isset($attributeArray['bgcolor'])) {
+ $sheet->getStyle($column . $row)->applyFromArray(
+ [
+ 'fill' => [
+ 'fillType' => Fill::FILL_SOLID,
+ 'color' => ['rgb' => $attributeArray['bgcolor']],
+ ],
+ ]
+ );
+ }
+ ++$column;
+
+ break;
+ case 'body':
+ $row = 1;
+ $column = 'A';
+ $cellContent = '';
+ $this->tableLevel = 0;
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+
+ break;
+ default:
+ $this->processDomElement($child, $sheet, $row, $column, $cellContent);
+ }
+ }
+ }
+ }
+
+ /**
+ * Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
+ *
+ * @param string $pFilename
+ * @param Spreadsheet $spreadsheet
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
+ {
+ // Validate
+ if (!$this->canRead($pFilename)) {
+ throw new Exception($pFilename . ' is an Invalid HTML file.');
+ }
+
+ // Create new sheet
+ while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
+ $spreadsheet->createSheet();
+ }
+ $spreadsheet->setActiveSheetIndex($this->sheetIndex);
+
+ // Create a new DOM object
+ $dom = new DOMDocument();
+ // Reload the HTML file into the DOM object
+ $loaded = $dom->loadHTML(mb_convert_encoding($this->securityScanner->scanFile($pFilename), 'HTML-ENTITIES', 'UTF-8'));
+ if ($loaded === false) {
+ throw new Exception('Failed to load ' . $pFilename . ' as a DOM Document');
+ }
+
+ // Discard white space
+ $dom->preserveWhiteSpace = false;
+
+ $row = 0;
+ $column = 'A';
+ $content = '';
+ $this->rowspan = [];
+ $this->processDomElement($dom, $spreadsheet->getActiveSheet(), $row, $column, $content);
+
+ // Return
+ return $spreadsheet;
+ }
+
+ /**
+ * Get sheet index.
+ *
+ * @return int
+ */
+ public function getSheetIndex()
+ {
+ return $this->sheetIndex;
+ }
+
+ /**
+ * Set sheet index.
+ *
+ * @param int $pValue Sheet index
+ *
+ * @return HTML
+ */
+ public function setSheetIndex($pValue)
+ {
+ $this->sheetIndex = $pValue;
+
+ return $this;
+ }
+
+ /**
+ * Apply inline css inline style.
+ *
+ * NOTES :
+ * Currently only intended for td & th element,
+ * and only takes 'background-color' and 'color'; property with HEX color
+ *
+ * TODO :
+ * - Implement to other propertie, such as border
+ *
+ * @param Worksheet $sheet
+ * @param int $row
+ * @param string $column
+ * @param array $attributeArray
+ */
+ private function applyInlineStyle(&$sheet, $row, $column, $attributeArray)
+ {
+ if (!isset($attributeArray['style'])) {
+ return;
+ }
+
+ $supported_styles = ['background-color', 'color'];
+
+ // add color styles (background & text) from dom element,currently support : td & th, using ONLY inline css style with RGB color
+ $styles = explode(';', $attributeArray['style']);
+ foreach ($styles as $st) {
+ $value = explode(':', $st);
+
+ if (empty(trim($value[0])) || !in_array(trim($value[0]), $supported_styles)) {
+ continue;
+ }
+
+ //check if has #, so we can get clean hex
+ if (substr(trim($value[1]), 0, 1) == '#') {
+ $style_color = substr(trim($value[1]), 1);
+ }
+
+ if (empty($style_color)) {
+ continue;
+ }
+
+ switch (trim($value[0])) {
+ case 'background-color':
+ $sheet->getStyle($column . $row)->applyFromArray(['fill' => ['fillType' => Fill::FILL_SOLID, 'color' => ['rgb' => "{$style_color}"]]]);
+
+ break;
+ case 'color':
+ $sheet->getStyle($column . $row)->applyFromArray(['font' => ['color' => ['rgb' => "{$style_color}"]]]);
+
+ break;
+ }
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/IReadFilter.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/IReadFilter.php
new file mode 100644
index 00000000000..ccfe05ada6c
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/IReadFilter.php
@@ -0,0 +1,17 @@
+readFilter = new DefaultReadFilter();
+ $this->securityScanner = XmlScanner::getInstance($this);
+ }
+
+ /**
+ * Can the current IReader read the file?
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return bool
+ */
+ public function canRead($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $mimeType = 'UNKNOWN';
+
+ // Load file
+
+ $zip = new ZipArchive();
+ if ($zip->open($pFilename) === true) {
+ // check if it is an OOXML archive
+ $stat = $zip->statName('mimetype');
+ if ($stat && ($stat['size'] <= 255)) {
+ $mimeType = $zip->getFromName($stat['name']);
+ } elseif ($stat = $zip->statName('META-INF/manifest.xml')) {
+ $xml = simplexml_load_string(
+ $this->securityScanner->scan($zip->getFromName('META-INF/manifest.xml')),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $namespacesContent = $xml->getNamespaces(true);
+ if (isset($namespacesContent['manifest'])) {
+ $manifest = $xml->children($namespacesContent['manifest']);
+ foreach ($manifest as $manifestDataSet) {
+ $manifestAttributes = $manifestDataSet->attributes($namespacesContent['manifest']);
+ if ($manifestAttributes->{'full-path'} == '/') {
+ $mimeType = (string) $manifestAttributes->{'media-type'};
+
+ break;
+ }
+ }
+ }
+ }
+
+ $zip->close();
+
+ return $mimeType === 'application/vnd.oasis.opendocument.spreadsheet';
+ }
+
+ return false;
+ }
+
+ /**
+ * Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return string[]
+ */
+ public function listWorksheetNames($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $zip = new ZipArchive();
+ if (!$zip->open($pFilename)) {
+ throw new Exception('Could not open ' . $pFilename . ' for reading! Error opening file.');
+ }
+
+ $worksheetNames = [];
+
+ $xml = new XMLReader();
+ $xml->xml(
+ $this->securityScanner->scanFile('zip://' . realpath($pFilename) . '#content.xml'),
+ null,
+ Settings::getLibXmlLoaderOptions()
+ );
+ $xml->setParserProperty(2, true);
+
+ // Step into the first level of content of the XML
+ $xml->read();
+ while ($xml->read()) {
+ // Quickly jump through to the office:body node
+ while ($xml->name !== 'office:body') {
+ if ($xml->isEmptyElement) {
+ $xml->read();
+ } else {
+ $xml->next();
+ }
+ }
+ // Now read each node until we find our first table:table node
+ while ($xml->read()) {
+ if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
+ // Loop through each table:table node reading the table:name attribute for each worksheet name
+ do {
+ $worksheetNames[] = $xml->getAttribute('table:name');
+ $xml->next();
+ } while ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT);
+ }
+ }
+ }
+
+ return $worksheetNames;
+ }
+
+ /**
+ * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ public function listWorksheetInfo($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $worksheetInfo = [];
+
+ $zip = new ZipArchive();
+ if (!$zip->open($pFilename)) {
+ throw new Exception('Could not open ' . $pFilename . ' for reading! Error opening file.');
+ }
+
+ $xml = new XMLReader();
+ $xml->xml(
+ $this->securityScanner->scanFile('zip://' . realpath($pFilename) . '#content.xml'),
+ null,
+ Settings::getLibXmlLoaderOptions()
+ );
+ $xml->setParserProperty(2, true);
+
+ // Step into the first level of content of the XML
+ $xml->read();
+ while ($xml->read()) {
+ // Quickly jump through to the office:body node
+ while ($xml->name !== 'office:body') {
+ if ($xml->isEmptyElement) {
+ $xml->read();
+ } else {
+ $xml->next();
+ }
+ }
+ // Now read each node until we find our first table:table node
+ while ($xml->read()) {
+ if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
+ $worksheetNames[] = $xml->getAttribute('table:name');
+
+ $tmpInfo = [
+ 'worksheetName' => $xml->getAttribute('table:name'),
+ 'lastColumnLetter' => 'A',
+ 'lastColumnIndex' => 0,
+ 'totalRows' => 0,
+ 'totalColumns' => 0,
+ ];
+
+ // Loop through each child node of the table:table element reading
+ $currCells = 0;
+ do {
+ $xml->read();
+ if ($xml->name == 'table:table-row' && $xml->nodeType == XMLReader::ELEMENT) {
+ $rowspan = $xml->getAttribute('table:number-rows-repeated');
+ $rowspan = empty($rowspan) ? 1 : $rowspan;
+ $tmpInfo['totalRows'] += $rowspan;
+ $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
+ $currCells = 0;
+ // Step into the row
+ $xml->read();
+ do {
+ if ($xml->name == 'table:table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
+ if (!$xml->isEmptyElement) {
+ ++$currCells;
+ $xml->next();
+ } else {
+ $xml->read();
+ }
+ } elseif ($xml->name == 'table:covered-table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
+ $mergeSize = $xml->getAttribute('table:number-columns-repeated');
+ $currCells += (int) $mergeSize;
+ $xml->read();
+ } else {
+ $xml->read();
+ }
+ } while ($xml->name != 'table:table-row');
+ }
+ } while ($xml->name != 'table:table');
+
+ $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
+ $tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
+ $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
+ $worksheetInfo[] = $tmpInfo;
+ }
+ }
+ }
+
+ return $worksheetInfo;
+ }
+
+ /**
+ * Loads PhpSpreadsheet from file.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function load($pFilename)
+ {
+ // Create new Spreadsheet
+ $spreadsheet = new Spreadsheet();
+
+ // Load into this instance
+ return $this->loadIntoExisting($pFilename, $spreadsheet);
+ }
+
+ /**
+ * Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
+ *
+ * @param string $pFilename
+ * @param Spreadsheet $spreadsheet
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
+ {
+ File::assertFile($pFilename);
+
+ $timezoneObj = new DateTimeZone('Europe/London');
+ $GMT = new \DateTimeZone('UTC');
+
+ $zip = new ZipArchive();
+ if (!$zip->open($pFilename)) {
+ throw new Exception('Could not open ' . $pFilename . ' for reading! Error opening file.');
+ }
+
+ // Meta
+
+ $xml = simplexml_load_string(
+ $this->securityScanner->scan($zip->getFromName('meta.xml')),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $namespacesMeta = $xml->getNamespaces(true);
+
+ $docProps = $spreadsheet->getProperties();
+ $officeProperty = $xml->children($namespacesMeta['office']);
+ foreach ($officeProperty as $officePropertyData) {
+ $officePropertyDC = [];
+ if (isset($namespacesMeta['dc'])) {
+ $officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
+ }
+ foreach ($officePropertyDC as $propertyName => $propertyValue) {
+ $propertyValue = (string) $propertyValue;
+ switch ($propertyName) {
+ case 'title':
+ $docProps->setTitle($propertyValue);
+
+ break;
+ case 'subject':
+ $docProps->setSubject($propertyValue);
+
+ break;
+ case 'creator':
+ $docProps->setCreator($propertyValue);
+ $docProps->setLastModifiedBy($propertyValue);
+
+ break;
+ case 'date':
+ $creationDate = strtotime($propertyValue);
+ $docProps->setCreated($creationDate);
+ $docProps->setModified($creationDate);
+
+ break;
+ case 'description':
+ $docProps->setDescription($propertyValue);
+
+ break;
+ }
+ }
+ $officePropertyMeta = [];
+ if (isset($namespacesMeta['dc'])) {
+ $officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
+ }
+ foreach ($officePropertyMeta as $propertyName => $propertyValue) {
+ $propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
+ $propertyValue = (string) $propertyValue;
+ switch ($propertyName) {
+ case 'initial-creator':
+ $docProps->setCreator($propertyValue);
+
+ break;
+ case 'keyword':
+ $docProps->setKeywords($propertyValue);
+
+ break;
+ case 'creation-date':
+ $creationDate = strtotime($propertyValue);
+ $docProps->setCreated($creationDate);
+
+ break;
+ case 'user-defined':
+ $propertyValueType = Properties::PROPERTY_TYPE_STRING;
+ foreach ($propertyValueAttributes as $key => $value) {
+ if ($key == 'name') {
+ $propertyValueName = (string) $value;
+ } elseif ($key == 'value-type') {
+ switch ($value) {
+ case 'date':
+ $propertyValue = Properties::convertProperty($propertyValue, 'date');
+ $propertyValueType = Properties::PROPERTY_TYPE_DATE;
+
+ break;
+ case 'boolean':
+ $propertyValue = Properties::convertProperty($propertyValue, 'bool');
+ $propertyValueType = Properties::PROPERTY_TYPE_BOOLEAN;
+
+ break;
+ case 'float':
+ $propertyValue = Properties::convertProperty($propertyValue, 'r4');
+ $propertyValueType = Properties::PROPERTY_TYPE_FLOAT;
+
+ break;
+ default:
+ $propertyValueType = Properties::PROPERTY_TYPE_STRING;
+ }
+ }
+ }
+ $docProps->setCustomProperty($propertyValueName, $propertyValue, $propertyValueType);
+
+ break;
+ }
+ }
+ }
+
+ // Content
+
+ $dom = new \DOMDocument('1.01', 'UTF-8');
+ $dom->loadXML(
+ $this->securityScanner->scan($zip->getFromName('content.xml')),
+ Settings::getLibXmlLoaderOptions()
+ );
+
+ $officeNs = $dom->lookupNamespaceUri('office');
+ $tableNs = $dom->lookupNamespaceUri('table');
+ $textNs = $dom->lookupNamespaceUri('text');
+ $xlinkNs = $dom->lookupNamespaceUri('xlink');
+
+ $spreadsheets = $dom->getElementsByTagNameNS($officeNs, 'body')
+ ->item(0)
+ ->getElementsByTagNameNS($officeNs, 'spreadsheet');
+
+ foreach ($spreadsheets as $workbookData) {
+ /** @var \DOMElement $workbookData */
+ $tables = $workbookData->getElementsByTagNameNS($tableNs, 'table');
+
+ $worksheetID = 0;
+ foreach ($tables as $worksheetDataSet) {
+ /** @var \DOMElement $worksheetDataSet */
+ $worksheetName = $worksheetDataSet->getAttributeNS($tableNs, 'name');
+
+ // Check loadSheetsOnly
+ if (isset($this->loadSheetsOnly)
+ && $worksheetName
+ && !in_array($worksheetName, $this->loadSheetsOnly)) {
+ continue;
+ }
+
+ // Create sheet
+ if ($worksheetID > 0) {
+ $spreadsheet->createSheet(); // First sheet is added by default
+ }
+ $spreadsheet->setActiveSheetIndex($worksheetID);
+
+ if ($worksheetName) {
+ // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
+ // formula cells... during the load, all formulae should be correct, and we're simply
+ // bringing the worksheet name in line with the formula, not the reverse
+ $spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false);
+ }
+
+ // Go through every child of table element
+ $rowID = 1;
+ foreach ($worksheetDataSet->childNodes as $childNode) {
+ /** @var \DOMElement $childNode */
+
+ // Filter elements which are not under the "table" ns
+ if ($childNode->namespaceURI != $tableNs) {
+ continue;
+ }
+
+ $key = $childNode->nodeName;
+
+ // Remove ns from node name
+ if (strpos($key, ':') !== false) {
+ $keyChunks = explode(':', $key);
+ $key = array_pop($keyChunks);
+ }
+
+ switch ($key) {
+ case 'table-header-rows':
+ /// TODO :: Figure this out. This is only a partial implementation I guess.
+ // ($rowData it's not used at all and I'm not sure that PHPExcel
+ // has an API for this)
+
+// foreach ($rowData as $keyRowData => $cellData) {
+// $rowData = $cellData;
+// break;
+// }
+ break;
+ case 'table-row':
+ if ($childNode->hasAttributeNS($tableNs, 'number-rows-repeated')) {
+ $rowRepeats = $childNode->getAttributeNS($tableNs, 'number-rows-repeated');
+ } else {
+ $rowRepeats = 1;
+ }
+
+ $columnID = 'A';
+ foreach ($childNode->childNodes as $key => $cellData) {
+ // @var \DOMElement $cellData
+
+ if ($this->getReadFilter() !== null) {
+ if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
+ ++$columnID;
+
+ continue;
+ }
+ }
+
+ // Initialize variables
+ $formatting = $hyperlink = null;
+ $hasCalculatedValue = false;
+ $cellDataFormula = '';
+
+ if ($cellData->hasAttributeNS($tableNs, 'formula')) {
+ $cellDataFormula = $cellData->getAttributeNS($tableNs, 'formula');
+ $hasCalculatedValue = true;
+ }
+
+ // Annotations
+ $annotation = $cellData->getElementsByTagNameNS($officeNs, 'annotation');
+
+ if ($annotation->length > 0) {
+ $textNode = $annotation->item(0)->getElementsByTagNameNS($textNs, 'p');
+
+ if ($textNode->length > 0) {
+ $text = $this->scanElementForText($textNode->item(0));
+
+ $spreadsheet->getActiveSheet()
+ ->getComment($columnID . $rowID)
+ ->setText($this->parseRichText($text));
+// ->setAuthor( $author )
+ }
+ }
+
+ // Content
+
+ /** @var \DOMElement[] $paragraphs */
+ $paragraphs = [];
+
+ foreach ($cellData->childNodes as $item) {
+ /** @var \DOMElement $item */
+
+ // Filter text:p elements
+ if ($item->nodeName == 'text:p') {
+ $paragraphs[] = $item;
+ }
+ }
+
+ if (count($paragraphs) > 0) {
+ // Consolidate if there are multiple p records (maybe with spans as well)
+ $dataArray = [];
+
+ // Text can have multiple text:p and within those, multiple text:span.
+ // text:p newlines, but text:span does not.
+ // Also, here we assume there is no text data is span fields are specified, since
+ // we have no way of knowing proper positioning anyway.
+
+ foreach ($paragraphs as $pData) {
+ $dataArray[] = $this->scanElementForText($pData);
+ }
+ $allCellDataText = implode($dataArray, "\n");
+
+ $type = $cellData->getAttributeNS($officeNs, 'value-type');
+
+ switch ($type) {
+ case 'string':
+ $type = DataType::TYPE_STRING;
+ $dataValue = $allCellDataText;
+
+ foreach ($paragraphs as $paragraph) {
+ $link = $paragraph->getElementsByTagNameNS($textNs, 'a');
+ if ($link->length > 0) {
+ $hyperlink = $link->item(0)->getAttributeNS($xlinkNs, 'href');
+ }
+ }
+
+ break;
+ case 'boolean':
+ $type = DataType::TYPE_BOOL;
+ $dataValue = ($allCellDataText == 'TRUE') ? true : false;
+
+ break;
+ case 'percentage':
+ $type = DataType::TYPE_NUMERIC;
+ $dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
+
+ if (floor($dataValue) == $dataValue) {
+ $dataValue = (int) $dataValue;
+ }
+ $formatting = NumberFormat::FORMAT_PERCENTAGE_00;
+
+ break;
+ case 'currency':
+ $type = DataType::TYPE_NUMERIC;
+ $dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
+
+ if (floor($dataValue) == $dataValue) {
+ $dataValue = (int) $dataValue;
+ }
+ $formatting = NumberFormat::FORMAT_CURRENCY_USD_SIMPLE;
+
+ break;
+ case 'float':
+ $type = DataType::TYPE_NUMERIC;
+ $dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
+
+ if (floor($dataValue) == $dataValue) {
+ if ($dataValue == (int) $dataValue) {
+ $dataValue = (int) $dataValue;
+ } else {
+ $dataValue = (float) $dataValue;
+ }
+ }
+
+ break;
+ case 'date':
+ $type = DataType::TYPE_NUMERIC;
+ $value = $cellData->getAttributeNS($officeNs, 'date-value');
+
+ $dateObj = new DateTime($value, $GMT);
+ $dateObj->setTimeZone($timezoneObj);
+ list($year, $month, $day, $hour, $minute, $second) = explode(
+ ' ',
+ $dateObj->format('Y m d H i s')
+ );
+
+ $dataValue = Date::formattedPHPToExcel(
+ $year,
+ $month,
+ $day,
+ $hour,
+ $minute,
+ $second
+ );
+
+ if ($dataValue != floor($dataValue)) {
+ $formatting = NumberFormat::FORMAT_DATE_XLSX15
+ . ' '
+ . NumberFormat::FORMAT_DATE_TIME4;
+ } else {
+ $formatting = NumberFormat::FORMAT_DATE_XLSX15;
+ }
+
+ break;
+ case 'time':
+ $type = DataType::TYPE_NUMERIC;
+
+ $timeValue = $cellData->getAttributeNS($officeNs, 'time-value');
+
+ $dataValue = Date::PHPToExcel(
+ strtotime(
+ '01-01-1970 ' . implode(':', sscanf($timeValue, 'PT%dH%dM%dS'))
+ )
+ );
+ $formatting = NumberFormat::FORMAT_DATE_TIME4;
+
+ break;
+ default:
+ $dataValue = null;
+ }
+ } else {
+ $type = DataType::TYPE_NULL;
+ $dataValue = null;
+ }
+
+ if ($hasCalculatedValue) {
+ $type = DataType::TYPE_FORMULA;
+ $cellDataFormula = substr($cellDataFormula, strpos($cellDataFormula, ':=') + 1);
+ $temp = explode('"', $cellDataFormula);
+ $tKey = false;
+ foreach ($temp as &$value) {
+ // Only replace in alternate array entries (i.e. non-quoted blocks)
+ if ($tKey = !$tKey) {
+ // Cell range reference in another sheet
+ $value = preg_replace('/\[([^\.]+)\.([^\.]+):\.([^\.]+)\]/U', '$1!$2:$3', $value);
+
+ // Cell reference in another sheet
+ $value = preg_replace('/\[([^\.]+)\.([^\.]+)\]/U', '$1!$2', $value);
+
+ // Cell range reference
+ $value = preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/U', '$1:$2', $value);
+
+ // Simple cell reference
+ $value = preg_replace('/\[\.([^\.]+)\]/U', '$1', $value);
+
+ $value = Calculation::translateSeparator(';', ',', $value, $inBraces);
+ }
+ }
+ unset($value);
+
+ // Then rebuild the formula string
+ $cellDataFormula = implode('"', $temp);
+ }
+
+ if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) {
+ $colRepeats = (int) $cellData->getAttributeNS($tableNs, 'number-columns-repeated');
+ } else {
+ $colRepeats = 1;
+ }
+
+ if ($type !== null) {
+ for ($i = 0; $i < $colRepeats; ++$i) {
+ if ($i > 0) {
+ ++$columnID;
+ }
+
+ if ($type !== DataType::TYPE_NULL) {
+ for ($rowAdjust = 0; $rowAdjust < $rowRepeats; ++$rowAdjust) {
+ $rID = $rowID + $rowAdjust;
+
+ $cell = $spreadsheet->getActiveSheet()
+ ->getCell($columnID . $rID);
+
+ // Set value
+ if ($hasCalculatedValue) {
+ $cell->setValueExplicit($cellDataFormula, $type);
+ } else {
+ $cell->setValueExplicit($dataValue, $type);
+ }
+
+ if ($hasCalculatedValue) {
+ $cell->setCalculatedValue($dataValue);
+ }
+
+ // Set other properties
+ if ($formatting !== null) {
+ $spreadsheet->getActiveSheet()
+ ->getStyle($columnID . $rID)
+ ->getNumberFormat()
+ ->setFormatCode($formatting);
+ } else {
+ $spreadsheet->getActiveSheet()
+ ->getStyle($columnID . $rID)
+ ->getNumberFormat()
+ ->setFormatCode(NumberFormat::FORMAT_GENERAL);
+ }
+
+ if ($hyperlink !== null) {
+ $cell->getHyperlink()
+ ->setUrl($hyperlink);
+ }
+ }
+ }
+ }
+ }
+
+ // Merged cells
+ if ($cellData->hasAttributeNS($tableNs, 'number-columns-spanned')
+ || $cellData->hasAttributeNS($tableNs, 'number-rows-spanned')
+ ) {
+ if (($type !== DataType::TYPE_NULL) || (!$this->readDataOnly)) {
+ $columnTo = $columnID;
+
+ if ($cellData->hasAttributeNS($tableNs, 'number-columns-spanned')) {
+ $columnIndex = Coordinate::columnIndexFromString($columnID);
+ $columnIndex += (int) $cellData->getAttributeNS($tableNs, 'number-columns-spanned');
+ $columnIndex -= 2;
+
+ $columnTo = Coordinate::stringFromColumnIndex($columnIndex + 1);
+ }
+
+ $rowTo = $rowID;
+
+ if ($cellData->hasAttributeNS($tableNs, 'number-rows-spanned')) {
+ $rowTo = $rowTo + (int) $cellData->getAttributeNS($tableNs, 'number-rows-spanned') - 1;
+ }
+
+ $cellRange = $columnID . $rowID . ':' . $columnTo . $rowTo;
+ $spreadsheet->getActiveSheet()->mergeCells($cellRange);
+ }
+ }
+
+ ++$columnID;
+ }
+ $rowID += $rowRepeats;
+
+ break;
+ }
+ }
+ ++$worksheetID;
+ }
+ }
+
+ // Return
+ return $spreadsheet;
+ }
+
+ /**
+ * Recursively scan element.
+ *
+ * @param \DOMNode $element
+ *
+ * @return string
+ */
+ protected function scanElementForText(\DOMNode $element)
+ {
+ $str = '';
+ foreach ($element->childNodes as $child) {
+ /** @var \DOMNode $child */
+ if ($child->nodeType == XML_TEXT_NODE) {
+ $str .= $child->nodeValue;
+ } elseif ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName == 'text:s') {
+ // It's a space
+
+ // Multiple spaces?
+ /** @var \DOMAttr $cAttr */
+ $cAttr = $child->attributes->getNamedItem('c');
+ if ($cAttr) {
+ $multiplier = (int) $cAttr->nodeValue;
+ } else {
+ $multiplier = 1;
+ }
+
+ $str .= str_repeat(' ', $multiplier);
+ }
+
+ if ($child->hasChildNodes()) {
+ $str .= $this->scanElementForText($child);
+ }
+ }
+
+ return $str;
+ }
+
+ /**
+ * @param string $is
+ *
+ * @return RichText
+ */
+ private function parseRichText($is)
+ {
+ $value = new RichText();
+ $value->createText($is);
+
+ return $value;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Security/XmlScanner.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Security/XmlScanner.php
new file mode 100644
index 00000000000..b5f7ac60fdc
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Security/XmlScanner.php
@@ -0,0 +1,125 @@
+pattern = $pattern;
+ $this->libxmlDisableEntityLoader = $this->identifyLibxmlDisableEntityLoaderAvailability();
+ }
+
+ public static function getInstance(Reader\IReader $reader)
+ {
+ switch (true) {
+ case $reader instanceof Reader\Html:
+ return new self('= 1;
+ case 1:
+ return PHP_RELEASE_VERSION >= 13;
+ case 0:
+ return PHP_RELEASE_VERSION >= 27;
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public function setAdditionalCallback(callable $callback)
+ {
+ $this->callback = $callback;
+ }
+
+ /**
+ * Scan the XML for use of libxmlDisableEntityLoader) {
+ $previousLibxmlDisableEntityLoaderValue = libxml_disable_entity_loader(true);
+ }
+
+ $pattern = '/encoding="(.*?)"/';
+ $result = preg_match($pattern, $xml, $matches);
+ $charset = $result ? $matches[1] : 'UTF-8';
+
+ if ($charset !== 'UTF-8') {
+ $xml = mb_convert_encoding($xml, 'UTF-8', $charset);
+ }
+
+ // Don't rely purely on libxml_disable_entity_loader()
+ $pattern = '/\\0?' . implode('\\0?', str_split($this->pattern)) . '\\0?/';
+
+ try {
+ if (preg_match($pattern, $xml)) {
+ throw new Reader\Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
+ }
+
+ if ($this->callback !== null && is_callable($this->callback)) {
+ $xml = call_user_func($this->callback, $xml);
+ }
+ } finally {
+ if (isset($previousLibxmlDisableEntityLoaderValue)) {
+ libxml_disable_entity_loader($previousLibxmlDisableEntityLoaderValue);
+ }
+ }
+
+ return $xml;
+ }
+
+ /**
+ * Scan theXML for use of scan(file_get_contents($filestream));
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Slk.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Slk.php
new file mode 100644
index 00000000000..61e52334b5c
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Slk.php
@@ -0,0 +1,496 @@
+readFilter = new DefaultReadFilter();
+ }
+
+ /**
+ * Validate that the current file is a SYLK file.
+ *
+ * @param string $pFilename
+ *
+ * @return bool
+ */
+ public function canRead($pFilename)
+ {
+ // Check if file exists
+ try {
+ $this->openFile($pFilename);
+ } catch (Exception $e) {
+ return false;
+ }
+
+ // Read sample data (first 2 KB will do)
+ $data = fread($this->fileHandle, 2048);
+
+ // Count delimiters in file
+ $delimiterCount = substr_count($data, ';');
+ $hasDelimiter = $delimiterCount > 0;
+
+ // Analyze first line looking for ID; signature
+ $lines = explode("\n", $data);
+ $hasId = substr($lines[0], 0, 4) === 'ID;P';
+
+ fclose($this->fileHandle);
+
+ return $hasDelimiter && $hasId;
+ }
+
+ /**
+ * Set input encoding.
+ *
+ * @param string $pValue Input encoding, eg: 'ANSI'
+ *
+ * @return Slk
+ */
+ public function setInputEncoding($pValue)
+ {
+ $this->inputEncoding = $pValue;
+
+ return $this;
+ }
+
+ /**
+ * Get input encoding.
+ *
+ * @return string
+ */
+ public function getInputEncoding()
+ {
+ return $this->inputEncoding;
+ }
+
+ /**
+ * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ public function listWorksheetInfo($pFilename)
+ {
+ // Open file
+ if (!$this->canRead($pFilename)) {
+ throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
+ }
+ $this->openFile($pFilename);
+ $fileHandle = $this->fileHandle;
+ rewind($fileHandle);
+
+ $worksheetInfo = [];
+ $worksheetInfo[0]['worksheetName'] = 'Worksheet';
+ $worksheetInfo[0]['lastColumnLetter'] = 'A';
+ $worksheetInfo[0]['lastColumnIndex'] = 0;
+ $worksheetInfo[0]['totalRows'] = 0;
+ $worksheetInfo[0]['totalColumns'] = 0;
+
+ // loop through one row (line) at a time in the file
+ $rowIndex = 0;
+ while (($rowData = fgets($fileHandle)) !== false) {
+ $columnIndex = 0;
+
+ // convert SYLK encoded $rowData to UTF-8
+ $rowData = StringHelper::SYLKtoUTF8($rowData);
+
+ // explode each row at semicolons while taking into account that literal semicolon (;)
+ // is escaped like this (;;)
+ $rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowData)))));
+
+ $dataType = array_shift($rowData);
+ if ($dataType == 'C') {
+ // Read cell value data
+ foreach ($rowData as $rowDatum) {
+ switch ($rowDatum[0]) {
+ case 'C':
+ case 'X':
+ $columnIndex = substr($rowDatum, 1) - 1;
+
+ break;
+ case 'R':
+ case 'Y':
+ $rowIndex = substr($rowDatum, 1);
+
+ break;
+ }
+
+ $worksheetInfo[0]['totalRows'] = max($worksheetInfo[0]['totalRows'], $rowIndex);
+ $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], $columnIndex);
+ }
+ }
+ }
+
+ $worksheetInfo[0]['lastColumnLetter'] = Coordinate::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex'] + 1);
+ $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
+
+ // Close file
+ fclose($fileHandle);
+
+ return $worksheetInfo;
+ }
+
+ /**
+ * Loads PhpSpreadsheet from file.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function load($pFilename)
+ {
+ // Create new Spreadsheet
+ $spreadsheet = new Spreadsheet();
+
+ // Load into this instance
+ return $this->loadIntoExisting($pFilename, $spreadsheet);
+ }
+
+ /**
+ * Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
+ *
+ * @param string $pFilename
+ * @param Spreadsheet $spreadsheet
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
+ {
+ // Open file
+ if (!$this->canRead($pFilename)) {
+ throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
+ }
+ $this->openFile($pFilename);
+ $fileHandle = $this->fileHandle;
+ rewind($fileHandle);
+
+ // Create new Worksheets
+ while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
+ $spreadsheet->createSheet();
+ }
+ $spreadsheet->setActiveSheetIndex($this->sheetIndex);
+
+ $fromFormats = ['\-', '\ '];
+ $toFormats = ['-', ' '];
+
+ // Loop through file
+ $column = $row = '';
+
+ // loop through one row (line) at a time in the file
+ while (($rowData = fgets($fileHandle)) !== false) {
+ // convert SYLK encoded $rowData to UTF-8
+ $rowData = StringHelper::SYLKtoUTF8($rowData);
+
+ // explode each row at semicolons while taking into account that literal semicolon (;)
+ // is escaped like this (;;)
+ $rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowData)))));
+
+ $dataType = array_shift($rowData);
+ // Read shared styles
+ if ($dataType == 'P') {
+ $formatArray = [];
+ foreach ($rowData as $rowDatum) {
+ switch ($rowDatum[0]) {
+ case 'P':
+ $formatArray['numberFormat']['formatCode'] = str_replace($fromFormats, $toFormats, substr($rowDatum, 1));
+
+ break;
+ case 'E':
+ case 'F':
+ $formatArray['font']['name'] = substr($rowDatum, 1);
+
+ break;
+ case 'L':
+ $formatArray['font']['size'] = substr($rowDatum, 1);
+
+ break;
+ case 'S':
+ $styleSettings = substr($rowDatum, 1);
+ $iMax = strlen($styleSettings);
+ for ($i = 0; $i < $iMax; ++$i) {
+ switch ($styleSettings[$i]) {
+ case 'I':
+ $formatArray['font']['italic'] = true;
+
+ break;
+ case 'D':
+ $formatArray['font']['bold'] = true;
+
+ break;
+ case 'T':
+ $formatArray['borders']['top']['borderStyle'] = Border::BORDER_THIN;
+
+ break;
+ case 'B':
+ $formatArray['borders']['bottom']['borderStyle'] = Border::BORDER_THIN;
+
+ break;
+ case 'L':
+ $formatArray['borders']['left']['borderStyle'] = Border::BORDER_THIN;
+
+ break;
+ case 'R':
+ $formatArray['borders']['right']['borderStyle'] = Border::BORDER_THIN;
+
+ break;
+ }
+ }
+
+ break;
+ }
+ }
+ $this->formats['P' . $this->format++] = $formatArray;
+ // Read cell value data
+ } elseif ($dataType == 'C') {
+ $hasCalculatedValue = false;
+ $cellData = $cellDataFormula = '';
+ foreach ($rowData as $rowDatum) {
+ switch ($rowDatum[0]) {
+ case 'C':
+ case 'X':
+ $column = substr($rowDatum, 1);
+
+ break;
+ case 'R':
+ case 'Y':
+ $row = substr($rowDatum, 1);
+
+ break;
+ case 'K':
+ $cellData = substr($rowDatum, 1);
+
+ break;
+ case 'E':
+ $cellDataFormula = '=' . substr($rowDatum, 1);
+ // Convert R1C1 style references to A1 style references (but only when not quoted)
+ $temp = explode('"', $cellDataFormula);
+ $key = false;
+ foreach ($temp as &$value) {
+ // Only count/replace in alternate array entries
+ if ($key = !$key) {
+ preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
+ // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
+ // through the formula from left to right. Reversing means that we work right to left.through
+ // the formula
+ $cellReferences = array_reverse($cellReferences);
+ // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
+ // then modify the formula to use that new reference
+ foreach ($cellReferences as $cellReference) {
+ $rowReference = $cellReference[2][0];
+ // Empty R reference is the current row
+ if ($rowReference == '') {
+ $rowReference = $row;
+ }
+ // Bracketed R references are relative to the current row
+ if ($rowReference[0] == '[') {
+ $rowReference = $row + trim($rowReference, '[]');
+ }
+ $columnReference = $cellReference[4][0];
+ // Empty C reference is the current column
+ if ($columnReference == '') {
+ $columnReference = $column;
+ }
+ // Bracketed C references are relative to the current column
+ if ($columnReference[0] == '[') {
+ $columnReference = $column + trim($columnReference, '[]');
+ }
+ $A1CellReference = Coordinate::stringFromColumnIndex($columnReference) . $rowReference;
+
+ $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0]));
+ }
+ }
+ }
+ unset($value);
+ // Then rebuild the formula string
+ $cellDataFormula = implode('"', $temp);
+ $hasCalculatedValue = true;
+
+ break;
+ }
+ }
+ $columnLetter = Coordinate::stringFromColumnIndex($column);
+ $cellData = Calculation::unwrapResult($cellData);
+
+ // Set cell value
+ $spreadsheet->getActiveSheet()->getCell($columnLetter . $row)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData);
+ if ($hasCalculatedValue) {
+ $cellData = Calculation::unwrapResult($cellData);
+ $spreadsheet->getActiveSheet()->getCell($columnLetter . $row)->setCalculatedValue($cellData);
+ }
+ // Read cell formatting
+ } elseif ($dataType == 'F') {
+ $formatStyle = $columnWidth = $styleSettings = '';
+ $styleData = [];
+ foreach ($rowData as $rowDatum) {
+ switch ($rowDatum[0]) {
+ case 'C':
+ case 'X':
+ $column = substr($rowDatum, 1);
+
+ break;
+ case 'R':
+ case 'Y':
+ $row = substr($rowDatum, 1);
+
+ break;
+ case 'P':
+ $formatStyle = $rowDatum;
+
+ break;
+ case 'W':
+ list($startCol, $endCol, $columnWidth) = explode(' ', substr($rowDatum, 1));
+
+ break;
+ case 'S':
+ $styleSettings = substr($rowDatum, 1);
+ $iMax = strlen($styleSettings);
+ for ($i = 0; $i < $iMax; ++$i) {
+ switch ($styleSettings[$i]) {
+ case 'I':
+ $styleData['font']['italic'] = true;
+
+ break;
+ case 'D':
+ $styleData['font']['bold'] = true;
+
+ break;
+ case 'T':
+ $styleData['borders']['top']['borderStyle'] = Border::BORDER_THIN;
+
+ break;
+ case 'B':
+ $styleData['borders']['bottom']['borderStyle'] = Border::BORDER_THIN;
+
+ break;
+ case 'L':
+ $styleData['borders']['left']['borderStyle'] = Border::BORDER_THIN;
+
+ break;
+ case 'R':
+ $styleData['borders']['right']['borderStyle'] = Border::BORDER_THIN;
+
+ break;
+ }
+ }
+
+ break;
+ }
+ }
+ if (($formatStyle > '') && ($column > '') && ($row > '')) {
+ $columnLetter = Coordinate::stringFromColumnIndex($column);
+ if (isset($this->formats[$formatStyle])) {
+ $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->formats[$formatStyle]);
+ }
+ }
+ if ((!empty($styleData)) && ($column > '') && ($row > '')) {
+ $columnLetter = Coordinate::stringFromColumnIndex($column);
+ $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($styleData);
+ }
+ if ($columnWidth > '') {
+ if ($startCol == $endCol) {
+ $startCol = Coordinate::stringFromColumnIndex($startCol);
+ $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
+ } else {
+ $startCol = Coordinate::stringFromColumnIndex($startCol);
+ $endCol = Coordinate::stringFromColumnIndex($endCol);
+ $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
+ do {
+ $spreadsheet->getActiveSheet()->getColumnDimension(++$startCol)->setWidth($columnWidth);
+ } while ($startCol != $endCol);
+ }
+ }
+ } else {
+ foreach ($rowData as $rowDatum) {
+ switch ($rowDatum[0]) {
+ case 'C':
+ case 'X':
+ $column = substr($rowDatum, 1);
+
+ break;
+ case 'R':
+ case 'Y':
+ $row = substr($rowDatum, 1);
+
+ break;
+ }
+ }
+ }
+ }
+
+ // Close file
+ fclose($fileHandle);
+
+ // Return
+ return $spreadsheet;
+ }
+
+ /**
+ * Get sheet index.
+ *
+ * @return int
+ */
+ public function getSheetIndex()
+ {
+ return $this->sheetIndex;
+ }
+
+ /**
+ * Set sheet index.
+ *
+ * @param int $pValue Sheet index
+ *
+ * @return Slk
+ */
+ public function setSheetIndex($pValue)
+ {
+ $this->sheetIndex = $pValue;
+
+ return $this;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls.php
new file mode 100644
index 00000000000..514adae2743
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls.php
@@ -0,0 +1,7949 @@
+data.
+ *
+ * @var int
+ */
+ private $dataSize;
+
+ /**
+ * Current position in stream.
+ *
+ * @var int
+ */
+ private $pos;
+
+ /**
+ * Workbook to be returned by the reader.
+ *
+ * @var Spreadsheet
+ */
+ private $spreadsheet;
+
+ /**
+ * Worksheet that is currently being built by the reader.
+ *
+ * @var Worksheet
+ */
+ private $phpSheet;
+
+ /**
+ * BIFF version.
+ *
+ * @var int
+ */
+ private $version;
+
+ /**
+ * Codepage set in the Excel file being read. Only important for BIFF5 (Excel 5.0 - Excel 95)
+ * For BIFF8 (Excel 97 - Excel 2003) this will always have the value 'UTF-16LE'.
+ *
+ * @var string
+ */
+ private $codepage;
+
+ /**
+ * Shared formats.
+ *
+ * @var array
+ */
+ private $formats;
+
+ /**
+ * Shared fonts.
+ *
+ * @var array
+ */
+ private $objFonts;
+
+ /**
+ * Color palette.
+ *
+ * @var array
+ */
+ private $palette;
+
+ /**
+ * Worksheets.
+ *
+ * @var array
+ */
+ private $sheets;
+
+ /**
+ * External books.
+ *
+ * @var array
+ */
+ private $externalBooks;
+
+ /**
+ * REF structures. Only applies to BIFF8.
+ *
+ * @var array
+ */
+ private $ref;
+
+ /**
+ * External names.
+ *
+ * @var array
+ */
+ private $externalNames;
+
+ /**
+ * Defined names.
+ *
+ * @var array
+ */
+ private $definedname;
+
+ /**
+ * Shared strings. Only applies to BIFF8.
+ *
+ * @var array
+ */
+ private $sst;
+
+ /**
+ * Panes are frozen? (in sheet currently being read). See WINDOW2 record.
+ *
+ * @var bool
+ */
+ private $frozen;
+
+ /**
+ * Fit printout to number of pages? (in sheet currently being read). See SHEETPR record.
+ *
+ * @var bool
+ */
+ private $isFitToPages;
+
+ /**
+ * Objects. One OBJ record contributes with one entry.
+ *
+ * @var array
+ */
+ private $objs;
+
+ /**
+ * Text Objects. One TXO record corresponds with one entry.
+ *
+ * @var array
+ */
+ private $textObjects;
+
+ /**
+ * Cell Annotations (BIFF8).
+ *
+ * @var array
+ */
+ private $cellNotes;
+
+ /**
+ * The combined MSODRAWINGGROUP data.
+ *
+ * @var string
+ */
+ private $drawingGroupData;
+
+ /**
+ * The combined MSODRAWING data (per sheet).
+ *
+ * @var string
+ */
+ private $drawingData;
+
+ /**
+ * Keep track of XF index.
+ *
+ * @var int
+ */
+ private $xfIndex;
+
+ /**
+ * Mapping of XF index (that is a cell XF) to final index in cellXf collection.
+ *
+ * @var array
+ */
+ private $mapCellXfIndex;
+
+ /**
+ * Mapping of XF index (that is a style XF) to final index in cellStyleXf collection.
+ *
+ * @var array
+ */
+ private $mapCellStyleXfIndex;
+
+ /**
+ * The shared formulas in a sheet. One SHAREDFMLA record contributes with one value.
+ *
+ * @var array
+ */
+ private $sharedFormulas;
+
+ /**
+ * The shared formula parts in a sheet. One FORMULA record contributes with one value if it
+ * refers to a shared formula.
+ *
+ * @var array
+ */
+ private $sharedFormulaParts;
+
+ /**
+ * The type of encryption in use.
+ *
+ * @var int
+ */
+ private $encryption = 0;
+
+ /**
+ * The position in the stream after which contents are encrypted.
+ *
+ * @var int
+ */
+ private $encryptionStartPos = false;
+
+ /**
+ * The current RC4 decryption object.
+ *
+ * @var Xls\RC4
+ */
+ private $rc4Key;
+
+ /**
+ * The position in the stream that the RC4 decryption object was left at.
+ *
+ * @var int
+ */
+ private $rc4Pos = 0;
+
+ /**
+ * The current MD5 context state.
+ *
+ * @var string
+ */
+ private $md5Ctxt;
+
+ /**
+ * @var int
+ */
+ private $textObjRef;
+
+ /**
+ * @var string
+ */
+ private $baseCell;
+
+ /**
+ * Create a new Xls Reader instance.
+ */
+ public function __construct()
+ {
+ $this->readFilter = new DefaultReadFilter();
+ }
+
+ /**
+ * Can the current IReader read the file?
+ *
+ * @param string $pFilename
+ *
+ * @return bool
+ */
+ public function canRead($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ try {
+ // Use ParseXL for the hard work.
+ $ole = new OLERead();
+
+ // get excel data
+ $ole->read($pFilename);
+
+ return true;
+ } catch (PhpSpreadsheetException $e) {
+ return false;
+ }
+ }
+
+ /**
+ * Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ public function listWorksheetNames($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $worksheetNames = [];
+
+ // Read the OLE file
+ $this->loadOLE($pFilename);
+
+ // total byte size of Excel data (workbook global substream + sheet substreams)
+ $this->dataSize = strlen($this->data);
+
+ $this->pos = 0;
+ $this->sheets = [];
+
+ // Parse Workbook Global Substream
+ while ($this->pos < $this->dataSize) {
+ $code = self::getUInt2d($this->data, $this->pos);
+
+ switch ($code) {
+ case self::XLS_TYPE_BOF:
+ $this->readBof();
+
+ break;
+ case self::XLS_TYPE_SHEET:
+ $this->readSheet();
+
+ break;
+ case self::XLS_TYPE_EOF:
+ $this->readDefault();
+
+ break 2;
+ default:
+ $this->readDefault();
+
+ break;
+ }
+ }
+
+ foreach ($this->sheets as $sheet) {
+ if ($sheet['sheetType'] != 0x00) {
+ // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
+ continue;
+ }
+
+ $worksheetNames[] = $sheet['name'];
+ }
+
+ return $worksheetNames;
+ }
+
+ /**
+ * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ public function listWorksheetInfo($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $worksheetInfo = [];
+
+ // Read the OLE file
+ $this->loadOLE($pFilename);
+
+ // total byte size of Excel data (workbook global substream + sheet substreams)
+ $this->dataSize = strlen($this->data);
+
+ // initialize
+ $this->pos = 0;
+ $this->sheets = [];
+
+ // Parse Workbook Global Substream
+ while ($this->pos < $this->dataSize) {
+ $code = self::getUInt2d($this->data, $this->pos);
+
+ switch ($code) {
+ case self::XLS_TYPE_BOF:
+ $this->readBof();
+
+ break;
+ case self::XLS_TYPE_SHEET:
+ $this->readSheet();
+
+ break;
+ case self::XLS_TYPE_EOF:
+ $this->readDefault();
+
+ break 2;
+ default:
+ $this->readDefault();
+
+ break;
+ }
+ }
+
+ // Parse the individual sheets
+ foreach ($this->sheets as $sheet) {
+ if ($sheet['sheetType'] != 0x00) {
+ // 0x00: Worksheet
+ // 0x02: Chart
+ // 0x06: Visual Basic module
+ continue;
+ }
+
+ $tmpInfo = [];
+ $tmpInfo['worksheetName'] = $sheet['name'];
+ $tmpInfo['lastColumnLetter'] = 'A';
+ $tmpInfo['lastColumnIndex'] = 0;
+ $tmpInfo['totalRows'] = 0;
+ $tmpInfo['totalColumns'] = 0;
+
+ $this->pos = $sheet['offset'];
+
+ while ($this->pos <= $this->dataSize - 4) {
+ $code = self::getUInt2d($this->data, $this->pos);
+
+ switch ($code) {
+ case self::XLS_TYPE_RK:
+ case self::XLS_TYPE_LABELSST:
+ case self::XLS_TYPE_NUMBER:
+ case self::XLS_TYPE_FORMULA:
+ case self::XLS_TYPE_BOOLERR:
+ case self::XLS_TYPE_LABEL:
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ $rowIndex = self::getUInt2d($recordData, 0) + 1;
+ $columnIndex = self::getUInt2d($recordData, 2);
+
+ $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
+ $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
+
+ break;
+ case self::XLS_TYPE_BOF:
+ $this->readBof();
+
+ break;
+ case self::XLS_TYPE_EOF:
+ $this->readDefault();
+
+ break 2;
+ default:
+ $this->readDefault();
+
+ break;
+ }
+ }
+
+ $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
+ $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
+
+ $worksheetInfo[] = $tmpInfo;
+ }
+
+ return $worksheetInfo;
+ }
+
+ /**
+ * Loads PhpSpreadsheet from file.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function load($pFilename)
+ {
+ // Read the OLE file
+ $this->loadOLE($pFilename);
+
+ // Initialisations
+ $this->spreadsheet = new Spreadsheet();
+ $this->spreadsheet->removeSheetByIndex(0); // remove 1st sheet
+ if (!$this->readDataOnly) {
+ $this->spreadsheet->removeCellStyleXfByIndex(0); // remove the default style
+ $this->spreadsheet->removeCellXfByIndex(0); // remove the default style
+ }
+
+ // Read the summary information stream (containing meta data)
+ $this->readSummaryInformation();
+
+ // Read the Additional document summary information stream (containing application-specific meta data)
+ $this->readDocumentSummaryInformation();
+
+ // total byte size of Excel data (workbook global substream + sheet substreams)
+ $this->dataSize = strlen($this->data);
+
+ // initialize
+ $this->pos = 0;
+ $this->codepage = 'CP1252';
+ $this->formats = [];
+ $this->objFonts = [];
+ $this->palette = [];
+ $this->sheets = [];
+ $this->externalBooks = [];
+ $this->ref = [];
+ $this->definedname = [];
+ $this->sst = [];
+ $this->drawingGroupData = '';
+ $this->xfIndex = '';
+ $this->mapCellXfIndex = [];
+ $this->mapCellStyleXfIndex = [];
+
+ // Parse Workbook Global Substream
+ while ($this->pos < $this->dataSize) {
+ $code = self::getUInt2d($this->data, $this->pos);
+
+ switch ($code) {
+ case self::XLS_TYPE_BOF:
+ $this->readBof();
+
+ break;
+ case self::XLS_TYPE_FILEPASS:
+ $this->readFilepass();
+
+ break;
+ case self::XLS_TYPE_CODEPAGE:
+ $this->readCodepage();
+
+ break;
+ case self::XLS_TYPE_DATEMODE:
+ $this->readDateMode();
+
+ break;
+ case self::XLS_TYPE_FONT:
+ $this->readFont();
+
+ break;
+ case self::XLS_TYPE_FORMAT:
+ $this->readFormat();
+
+ break;
+ case self::XLS_TYPE_XF:
+ $this->readXf();
+
+ break;
+ case self::XLS_TYPE_XFEXT:
+ $this->readXfExt();
+
+ break;
+ case self::XLS_TYPE_STYLE:
+ $this->readStyle();
+
+ break;
+ case self::XLS_TYPE_PALETTE:
+ $this->readPalette();
+
+ break;
+ case self::XLS_TYPE_SHEET:
+ $this->readSheet();
+
+ break;
+ case self::XLS_TYPE_EXTERNALBOOK:
+ $this->readExternalBook();
+
+ break;
+ case self::XLS_TYPE_EXTERNNAME:
+ $this->readExternName();
+
+ break;
+ case self::XLS_TYPE_EXTERNSHEET:
+ $this->readExternSheet();
+
+ break;
+ case self::XLS_TYPE_DEFINEDNAME:
+ $this->readDefinedName();
+
+ break;
+ case self::XLS_TYPE_MSODRAWINGGROUP:
+ $this->readMsoDrawingGroup();
+
+ break;
+ case self::XLS_TYPE_SST:
+ $this->readSst();
+
+ break;
+ case self::XLS_TYPE_EOF:
+ $this->readDefault();
+
+ break 2;
+ default:
+ $this->readDefault();
+
+ break;
+ }
+ }
+
+ // Resolve indexed colors for font, fill, and border colors
+ // Cannot be resolved already in XF record, because PALETTE record comes afterwards
+ if (!$this->readDataOnly) {
+ foreach ($this->objFonts as $objFont) {
+ if (isset($objFont->colorIndex)) {
+ $color = Xls\Color::map($objFont->colorIndex, $this->palette, $this->version);
+ $objFont->getColor()->setRGB($color['rgb']);
+ }
+ }
+
+ foreach ($this->spreadsheet->getCellXfCollection() as $objStyle) {
+ // fill start and end color
+ $fill = $objStyle->getFill();
+
+ if (isset($fill->startcolorIndex)) {
+ $startColor = Xls\Color::map($fill->startcolorIndex, $this->palette, $this->version);
+ $fill->getStartColor()->setRGB($startColor['rgb']);
+ }
+ if (isset($fill->endcolorIndex)) {
+ $endColor = Xls\Color::map($fill->endcolorIndex, $this->palette, $this->version);
+ $fill->getEndColor()->setRGB($endColor['rgb']);
+ }
+
+ // border colors
+ $top = $objStyle->getBorders()->getTop();
+ $right = $objStyle->getBorders()->getRight();
+ $bottom = $objStyle->getBorders()->getBottom();
+ $left = $objStyle->getBorders()->getLeft();
+ $diagonal = $objStyle->getBorders()->getDiagonal();
+
+ if (isset($top->colorIndex)) {
+ $borderTopColor = Xls\Color::map($top->colorIndex, $this->palette, $this->version);
+ $top->getColor()->setRGB($borderTopColor['rgb']);
+ }
+ if (isset($right->colorIndex)) {
+ $borderRightColor = Xls\Color::map($right->colorIndex, $this->palette, $this->version);
+ $right->getColor()->setRGB($borderRightColor['rgb']);
+ }
+ if (isset($bottom->colorIndex)) {
+ $borderBottomColor = Xls\Color::map($bottom->colorIndex, $this->palette, $this->version);
+ $bottom->getColor()->setRGB($borderBottomColor['rgb']);
+ }
+ if (isset($left->colorIndex)) {
+ $borderLeftColor = Xls\Color::map($left->colorIndex, $this->palette, $this->version);
+ $left->getColor()->setRGB($borderLeftColor['rgb']);
+ }
+ if (isset($diagonal->colorIndex)) {
+ $borderDiagonalColor = Xls\Color::map($diagonal->colorIndex, $this->palette, $this->version);
+ $diagonal->getColor()->setRGB($borderDiagonalColor['rgb']);
+ }
+ }
+ }
+
+ // treat MSODRAWINGGROUP records, workbook-level Escher
+ if (!$this->readDataOnly && $this->drawingGroupData) {
+ $escherWorkbook = new Escher();
+ $reader = new Xls\Escher($escherWorkbook);
+ $escherWorkbook = $reader->load($this->drawingGroupData);
+ }
+
+ // Parse the individual sheets
+ foreach ($this->sheets as $sheet) {
+ if ($sheet['sheetType'] != 0x00) {
+ // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
+ continue;
+ }
+
+ // check if sheet should be skipped
+ if (isset($this->loadSheetsOnly) && !in_array($sheet['name'], $this->loadSheetsOnly)) {
+ continue;
+ }
+
+ // add sheet to PhpSpreadsheet object
+ $this->phpSheet = $this->spreadsheet->createSheet();
+ // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula
+ // cells... during the load, all formulae should be correct, and we're simply bringing the worksheet
+ // name in line with the formula, not the reverse
+ $this->phpSheet->setTitle($sheet['name'], false, false);
+ $this->phpSheet->setSheetState($sheet['sheetState']);
+
+ $this->pos = $sheet['offset'];
+
+ // Initialize isFitToPages. May change after reading SHEETPR record.
+ $this->isFitToPages = false;
+
+ // Initialize drawingData
+ $this->drawingData = '';
+
+ // Initialize objs
+ $this->objs = [];
+
+ // Initialize shared formula parts
+ $this->sharedFormulaParts = [];
+
+ // Initialize shared formulas
+ $this->sharedFormulas = [];
+
+ // Initialize text objs
+ $this->textObjects = [];
+
+ // Initialize cell annotations
+ $this->cellNotes = [];
+ $this->textObjRef = -1;
+
+ while ($this->pos <= $this->dataSize - 4) {
+ $code = self::getUInt2d($this->data, $this->pos);
+
+ switch ($code) {
+ case self::XLS_TYPE_BOF:
+ $this->readBof();
+
+ break;
+ case self::XLS_TYPE_PRINTGRIDLINES:
+ $this->readPrintGridlines();
+
+ break;
+ case self::XLS_TYPE_DEFAULTROWHEIGHT:
+ $this->readDefaultRowHeight();
+
+ break;
+ case self::XLS_TYPE_SHEETPR:
+ $this->readSheetPr();
+
+ break;
+ case self::XLS_TYPE_HORIZONTALPAGEBREAKS:
+ $this->readHorizontalPageBreaks();
+
+ break;
+ case self::XLS_TYPE_VERTICALPAGEBREAKS:
+ $this->readVerticalPageBreaks();
+
+ break;
+ case self::XLS_TYPE_HEADER:
+ $this->readHeader();
+
+ break;
+ case self::XLS_TYPE_FOOTER:
+ $this->readFooter();
+
+ break;
+ case self::XLS_TYPE_HCENTER:
+ $this->readHcenter();
+
+ break;
+ case self::XLS_TYPE_VCENTER:
+ $this->readVcenter();
+
+ break;
+ case self::XLS_TYPE_LEFTMARGIN:
+ $this->readLeftMargin();
+
+ break;
+ case self::XLS_TYPE_RIGHTMARGIN:
+ $this->readRightMargin();
+
+ break;
+ case self::XLS_TYPE_TOPMARGIN:
+ $this->readTopMargin();
+
+ break;
+ case self::XLS_TYPE_BOTTOMMARGIN:
+ $this->readBottomMargin();
+
+ break;
+ case self::XLS_TYPE_PAGESETUP:
+ $this->readPageSetup();
+
+ break;
+ case self::XLS_TYPE_PROTECT:
+ $this->readProtect();
+
+ break;
+ case self::XLS_TYPE_SCENPROTECT:
+ $this->readScenProtect();
+
+ break;
+ case self::XLS_TYPE_OBJECTPROTECT:
+ $this->readObjectProtect();
+
+ break;
+ case self::XLS_TYPE_PASSWORD:
+ $this->readPassword();
+
+ break;
+ case self::XLS_TYPE_DEFCOLWIDTH:
+ $this->readDefColWidth();
+
+ break;
+ case self::XLS_TYPE_COLINFO:
+ $this->readColInfo();
+
+ break;
+ case self::XLS_TYPE_DIMENSION:
+ $this->readDefault();
+
+ break;
+ case self::XLS_TYPE_ROW:
+ $this->readRow();
+
+ break;
+ case self::XLS_TYPE_DBCELL:
+ $this->readDefault();
+
+ break;
+ case self::XLS_TYPE_RK:
+ $this->readRk();
+
+ break;
+ case self::XLS_TYPE_LABELSST:
+ $this->readLabelSst();
+
+ break;
+ case self::XLS_TYPE_MULRK:
+ $this->readMulRk();
+
+ break;
+ case self::XLS_TYPE_NUMBER:
+ $this->readNumber();
+
+ break;
+ case self::XLS_TYPE_FORMULA:
+ $this->readFormula();
+
+ break;
+ case self::XLS_TYPE_SHAREDFMLA:
+ $this->readSharedFmla();
+
+ break;
+ case self::XLS_TYPE_BOOLERR:
+ $this->readBoolErr();
+
+ break;
+ case self::XLS_TYPE_MULBLANK:
+ $this->readMulBlank();
+
+ break;
+ case self::XLS_TYPE_LABEL:
+ $this->readLabel();
+
+ break;
+ case self::XLS_TYPE_BLANK:
+ $this->readBlank();
+
+ break;
+ case self::XLS_TYPE_MSODRAWING:
+ $this->readMsoDrawing();
+
+ break;
+ case self::XLS_TYPE_OBJ:
+ $this->readObj();
+
+ break;
+ case self::XLS_TYPE_WINDOW2:
+ $this->readWindow2();
+
+ break;
+ case self::XLS_TYPE_PAGELAYOUTVIEW:
+ $this->readPageLayoutView();
+
+ break;
+ case self::XLS_TYPE_SCL:
+ $this->readScl();
+
+ break;
+ case self::XLS_TYPE_PANE:
+ $this->readPane();
+
+ break;
+ case self::XLS_TYPE_SELECTION:
+ $this->readSelection();
+
+ break;
+ case self::XLS_TYPE_MERGEDCELLS:
+ $this->readMergedCells();
+
+ break;
+ case self::XLS_TYPE_HYPERLINK:
+ $this->readHyperLink();
+
+ break;
+ case self::XLS_TYPE_DATAVALIDATIONS:
+ $this->readDataValidations();
+
+ break;
+ case self::XLS_TYPE_DATAVALIDATION:
+ $this->readDataValidation();
+
+ break;
+ case self::XLS_TYPE_SHEETLAYOUT:
+ $this->readSheetLayout();
+
+ break;
+ case self::XLS_TYPE_SHEETPROTECTION:
+ $this->readSheetProtection();
+
+ break;
+ case self::XLS_TYPE_RANGEPROTECTION:
+ $this->readRangeProtection();
+
+ break;
+ case self::XLS_TYPE_NOTE:
+ $this->readNote();
+
+ break;
+ case self::XLS_TYPE_TXO:
+ $this->readTextObject();
+
+ break;
+ case self::XLS_TYPE_CONTINUE:
+ $this->readContinue();
+
+ break;
+ case self::XLS_TYPE_EOF:
+ $this->readDefault();
+
+ break 2;
+ default:
+ $this->readDefault();
+
+ break;
+ }
+ }
+
+ // treat MSODRAWING records, sheet-level Escher
+ if (!$this->readDataOnly && $this->drawingData) {
+ $escherWorksheet = new Escher();
+ $reader = new Xls\Escher($escherWorksheet);
+ $escherWorksheet = $reader->load($this->drawingData);
+
+ // get all spContainers in one long array, so they can be mapped to OBJ records
+ $allSpContainers = $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers();
+ }
+
+ // treat OBJ records
+ foreach ($this->objs as $n => $obj) {
+ // the first shape container never has a corresponding OBJ record, hence $n + 1
+ if (isset($allSpContainers[$n + 1]) && is_object($allSpContainers[$n + 1])) {
+ $spContainer = $allSpContainers[$n + 1];
+
+ // we skip all spContainers that are a part of a group shape since we cannot yet handle those
+ if ($spContainer->getNestingLevel() > 1) {
+ continue;
+ }
+
+ // calculate the width and height of the shape
+ list($startColumn, $startRow) = Coordinate::coordinateFromString($spContainer->getStartCoordinates());
+ list($endColumn, $endRow) = Coordinate::coordinateFromString($spContainer->getEndCoordinates());
+
+ $startOffsetX = $spContainer->getStartOffsetX();
+ $startOffsetY = $spContainer->getStartOffsetY();
+ $endOffsetX = $spContainer->getEndOffsetX();
+ $endOffsetY = $spContainer->getEndOffsetY();
+
+ $width = \PhpOffice\PhpSpreadsheet\Shared\Xls::getDistanceX($this->phpSheet, $startColumn, $startOffsetX, $endColumn, $endOffsetX);
+ $height = \PhpOffice\PhpSpreadsheet\Shared\Xls::getDistanceY($this->phpSheet, $startRow, $startOffsetY, $endRow, $endOffsetY);
+
+ // calculate offsetX and offsetY of the shape
+ $offsetX = $startOffsetX * \PhpOffice\PhpSpreadsheet\Shared\Xls::sizeCol($this->phpSheet, $startColumn) / 1024;
+ $offsetY = $startOffsetY * \PhpOffice\PhpSpreadsheet\Shared\Xls::sizeRow($this->phpSheet, $startRow) / 256;
+
+ switch ($obj['otObjType']) {
+ case 0x19:
+ // Note
+ if (isset($this->cellNotes[$obj['idObjID']])) {
+ $cellNote = $this->cellNotes[$obj['idObjID']];
+
+ if (isset($this->textObjects[$obj['idObjID']])) {
+ $textObject = $this->textObjects[$obj['idObjID']];
+ $this->cellNotes[$obj['idObjID']]['objTextData'] = $textObject;
+ }
+ }
+
+ break;
+ case 0x08:
+ // picture
+ // get index to BSE entry (1-based)
+ $BSEindex = $spContainer->getOPT(0x0104);
+
+ // If there is no BSE Index, we will fail here and other fields are not read.
+ // Fix by checking here.
+ // TODO: Why is there no BSE Index? Is this a new Office Version? Password protected field?
+ // More likely : a uncompatible picture
+ if (!$BSEindex) {
+ continue 2;
+ }
+
+ $BSECollection = $escherWorkbook->getDggContainer()->getBstoreContainer()->getBSECollection();
+ $BSE = $BSECollection[$BSEindex - 1];
+ $blipType = $BSE->getBlipType();
+
+ // need check because some blip types are not supported by Escher reader such as EMF
+ if ($blip = $BSE->getBlip()) {
+ $ih = imagecreatefromstring($blip->getData());
+ $drawing = new MemoryDrawing();
+ $drawing->setImageResource($ih);
+
+ // width, height, offsetX, offsetY
+ $drawing->setResizeProportional(false);
+ $drawing->setWidth($width);
+ $drawing->setHeight($height);
+ $drawing->setOffsetX($offsetX);
+ $drawing->setOffsetY($offsetY);
+
+ switch ($blipType) {
+ case BSE::BLIPTYPE_JPEG:
+ $drawing->setRenderingFunction(MemoryDrawing::RENDERING_JPEG);
+ $drawing->setMimeType(MemoryDrawing::MIMETYPE_JPEG);
+
+ break;
+ case BSE::BLIPTYPE_PNG:
+ $drawing->setRenderingFunction(MemoryDrawing::RENDERING_PNG);
+ $drawing->setMimeType(MemoryDrawing::MIMETYPE_PNG);
+
+ break;
+ }
+
+ $drawing->setWorksheet($this->phpSheet);
+ $drawing->setCoordinates($spContainer->getStartCoordinates());
+ }
+
+ break;
+ default:
+ // other object type
+ break;
+ }
+ }
+ }
+
+ // treat SHAREDFMLA records
+ if ($this->version == self::XLS_BIFF8) {
+ foreach ($this->sharedFormulaParts as $cell => $baseCell) {
+ list($column, $row) = Coordinate::coordinateFromString($cell);
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($column, $row, $this->phpSheet->getTitle())) {
+ $formula = $this->getFormulaFromStructure($this->sharedFormulas[$baseCell], $cell);
+ $this->phpSheet->getCell($cell)->setValueExplicit('=' . $formula, DataType::TYPE_FORMULA);
+ }
+ }
+ }
+
+ if (!empty($this->cellNotes)) {
+ foreach ($this->cellNotes as $note => $noteDetails) {
+ if (!isset($noteDetails['objTextData'])) {
+ if (isset($this->textObjects[$note])) {
+ $textObject = $this->textObjects[$note];
+ $noteDetails['objTextData'] = $textObject;
+ } else {
+ $noteDetails['objTextData']['text'] = '';
+ }
+ }
+ $cellAddress = str_replace('$', '', $noteDetails['cellRef']);
+ $this->phpSheet->getComment($cellAddress)->setAuthor($noteDetails['author'])->setText($this->parseRichText($noteDetails['objTextData']['text']));
+ }
+ }
+ }
+
+ // add the named ranges (defined names)
+ foreach ($this->definedname as $definedName) {
+ if ($definedName['isBuiltInName']) {
+ switch ($definedName['name']) {
+ case pack('C', 0x06):
+ // print area
+ // in general, formula looks like this: Foo!$C$7:$J$66,Bar!$A$1:$IV$2
+ $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
+
+ $extractedRanges = [];
+ foreach ($ranges as $range) {
+ // $range should look like one of these
+ // Foo!$C$7:$J$66
+ // Bar!$A$1:$IV$2
+ $explodes = Worksheet::extractSheetTitle($range, true);
+ $sheetName = trim($explodes[0], "'");
+ if (count($explodes) == 2) {
+ if (strpos($explodes[1], ':') === false) {
+ $explodes[1] = $explodes[1] . ':' . $explodes[1];
+ }
+ $extractedRanges[] = str_replace('$', '', $explodes[1]); // C7:J66
+ }
+ }
+ if ($docSheet = $this->spreadsheet->getSheetByName($sheetName)) {
+ $docSheet->getPageSetup()->setPrintArea(implode(',', $extractedRanges)); // C7:J66,A1:IV2
+ }
+
+ break;
+ case pack('C', 0x07):
+ // print titles (repeating rows)
+ // Assuming BIFF8, there are 3 cases
+ // 1. repeating rows
+ // formula looks like this: Sheet!$A$1:$IV$2
+ // rows 1-2 repeat
+ // 2. repeating columns
+ // formula looks like this: Sheet!$A$1:$B$65536
+ // columns A-B repeat
+ // 3. both repeating rows and repeating columns
+ // formula looks like this: Sheet!$A$1:$B$65536,Sheet!$A$1:$IV$2
+ $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
+ foreach ($ranges as $range) {
+ // $range should look like this one of these
+ // Sheet!$A$1:$B$65536
+ // Sheet!$A$1:$IV$2
+ if (strpos($range, '!') !== false) {
+ $explodes = Worksheet::extractSheetTitle($range, true);
+ if ($docSheet = $this->spreadsheet->getSheetByName($explodes[0])) {
+ $extractedRange = $explodes[1];
+ $extractedRange = str_replace('$', '', $extractedRange);
+
+ $coordinateStrings = explode(':', $extractedRange);
+ if (count($coordinateStrings) == 2) {
+ list($firstColumn, $firstRow) = Coordinate::coordinateFromString($coordinateStrings[0]);
+ list($lastColumn, $lastRow) = Coordinate::coordinateFromString($coordinateStrings[1]);
+
+ if ($firstColumn == 'A' and $lastColumn == 'IV') {
+ // then we have repeating rows
+ $docSheet->getPageSetup()->setRowsToRepeatAtTop([$firstRow, $lastRow]);
+ } elseif ($firstRow == 1 and $lastRow == 65536) {
+ // then we have repeating columns
+ $docSheet->getPageSetup()->setColumnsToRepeatAtLeft([$firstColumn, $lastColumn]);
+ }
+ }
+ }
+ }
+ }
+
+ break;
+ }
+ } else {
+ // Extract range
+ if (strpos($definedName['formula'], '!') !== false) {
+ $explodes = Worksheet::extractSheetTitle($definedName['formula'], true);
+ if (($docSheet = $this->spreadsheet->getSheetByName($explodes[0])) ||
+ ($docSheet = $this->spreadsheet->getSheetByName(trim($explodes[0], "'")))) {
+ $extractedRange = $explodes[1];
+ $extractedRange = str_replace('$', '', $extractedRange);
+
+ $localOnly = ($definedName['scope'] == 0) ? false : true;
+
+ $scope = ($definedName['scope'] == 0) ? null : $this->spreadsheet->getSheetByName($this->sheets[$definedName['scope'] - 1]['name']);
+
+ $this->spreadsheet->addNamedRange(new NamedRange((string) $definedName['name'], $docSheet, $extractedRange, $localOnly, $scope));
+ }
+ }
+ // Named Value
+ // TODO Provide support for named values
+ }
+ }
+ $this->data = null;
+
+ return $this->spreadsheet;
+ }
+
+ /**
+ * Read record data from stream, decrypting as required.
+ *
+ * @param string $data Data stream to read from
+ * @param int $pos Position to start reading from
+ * @param int $len Record data length
+ *
+ * @return string Record data
+ */
+ private function readRecordData($data, $pos, $len)
+ {
+ $data = substr($data, $pos, $len);
+
+ // File not encrypted, or record before encryption start point
+ if ($this->encryption == self::MS_BIFF_CRYPTO_NONE || $pos < $this->encryptionStartPos) {
+ return $data;
+ }
+
+ $recordData = '';
+ if ($this->encryption == self::MS_BIFF_CRYPTO_RC4) {
+ $oldBlock = floor($this->rc4Pos / self::REKEY_BLOCK);
+ $block = floor($pos / self::REKEY_BLOCK);
+ $endBlock = floor(($pos + $len) / self::REKEY_BLOCK);
+
+ // Spin an RC4 decryptor to the right spot. If we have a decryptor sitting
+ // at a point earlier in the current block, re-use it as we can save some time.
+ if ($block != $oldBlock || $pos < $this->rc4Pos || !$this->rc4Key) {
+ $this->rc4Key = $this->makeKey($block, $this->md5Ctxt);
+ $step = $pos % self::REKEY_BLOCK;
+ } else {
+ $step = $pos - $this->rc4Pos;
+ }
+ $this->rc4Key->RC4(str_repeat("\0", $step));
+
+ // Decrypt record data (re-keying at the end of every block)
+ while ($block != $endBlock) {
+ $step = self::REKEY_BLOCK - ($pos % self::REKEY_BLOCK);
+ $recordData .= $this->rc4Key->RC4(substr($data, 0, $step));
+ $data = substr($data, $step);
+ $pos += $step;
+ $len -= $step;
+ ++$block;
+ $this->rc4Key = $this->makeKey($block, $this->md5Ctxt);
+ }
+ $recordData .= $this->rc4Key->RC4(substr($data, 0, $len));
+
+ // Keep track of the position of this decryptor.
+ // We'll try and re-use it later if we can to speed things up
+ $this->rc4Pos = $pos + $len;
+ } elseif ($this->encryption == self::MS_BIFF_CRYPTO_XOR) {
+ throw new Exception('XOr encryption not supported');
+ }
+
+ return $recordData;
+ }
+
+ /**
+ * Use OLE reader to extract the relevant data streams from the OLE file.
+ *
+ * @param string $pFilename
+ */
+ private function loadOLE($pFilename)
+ {
+ // OLE reader
+ $ole = new OLERead();
+ // get excel data,
+ $ole->read($pFilename);
+ // Get workbook data: workbook stream + sheet streams
+ $this->data = $ole->getStream($ole->wrkbook);
+ // Get summary information data
+ $this->summaryInformation = $ole->getStream($ole->summaryInformation);
+ // Get additional document summary information data
+ $this->documentSummaryInformation = $ole->getStream($ole->documentSummaryInformation);
+ }
+
+ /**
+ * Read summary information.
+ */
+ private function readSummaryInformation()
+ {
+ if (!isset($this->summaryInformation)) {
+ return;
+ }
+
+ // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
+ // offset: 2; size: 2;
+ // offset: 4; size: 2; OS version
+ // offset: 6; size: 2; OS indicator
+ // offset: 8; size: 16
+ // offset: 24; size: 4; section count
+ $secCount = self::getInt4d($this->summaryInformation, 24);
+
+ // offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9
+ // offset: 44; size: 4
+ $secOffset = self::getInt4d($this->summaryInformation, 44);
+
+ // section header
+ // offset: $secOffset; size: 4; section length
+ $secLength = self::getInt4d($this->summaryInformation, $secOffset);
+
+ // offset: $secOffset+4; size: 4; property count
+ $countProperties = self::getInt4d($this->summaryInformation, $secOffset + 4);
+
+ // initialize code page (used to resolve string values)
+ $codePage = 'CP1252';
+
+ // offset: ($secOffset+8); size: var
+ // loop through property decarations and properties
+ for ($i = 0; $i < $countProperties; ++$i) {
+ // offset: ($secOffset+8) + (8 * $i); size: 4; property ID
+ $id = self::getInt4d($this->summaryInformation, ($secOffset + 8) + (8 * $i));
+
+ // Use value of property id as appropriate
+ // offset: ($secOffset+12) + (8 * $i); size: 4; offset from beginning of section (48)
+ $offset = self::getInt4d($this->summaryInformation, ($secOffset + 12) + (8 * $i));
+
+ $type = self::getInt4d($this->summaryInformation, $secOffset + $offset);
+
+ // initialize property value
+ $value = null;
+
+ // extract property value based on property type
+ switch ($type) {
+ case 0x02: // 2 byte signed integer
+ $value = self::getUInt2d($this->summaryInformation, $secOffset + 4 + $offset);
+
+ break;
+ case 0x03: // 4 byte signed integer
+ $value = self::getInt4d($this->summaryInformation, $secOffset + 4 + $offset);
+
+ break;
+ case 0x13: // 4 byte unsigned integer
+ // not needed yet, fix later if necessary
+ break;
+ case 0x1E: // null-terminated string prepended by dword string length
+ $byteLength = self::getInt4d($this->summaryInformation, $secOffset + 4 + $offset);
+ $value = substr($this->summaryInformation, $secOffset + 8 + $offset, $byteLength);
+ $value = StringHelper::convertEncoding($value, 'UTF-8', $codePage);
+ $value = rtrim($value);
+
+ break;
+ case 0x40: // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
+ // PHP-time
+ $value = OLE::OLE2LocalDate(substr($this->summaryInformation, $secOffset + 4 + $offset, 8));
+
+ break;
+ case 0x47: // Clipboard format
+ // not needed yet, fix later if necessary
+ break;
+ }
+
+ switch ($id) {
+ case 0x01: // Code Page
+ $codePage = CodePage::numberToName($value);
+
+ break;
+ case 0x02: // Title
+ $this->spreadsheet->getProperties()->setTitle($value);
+
+ break;
+ case 0x03: // Subject
+ $this->spreadsheet->getProperties()->setSubject($value);
+
+ break;
+ case 0x04: // Author (Creator)
+ $this->spreadsheet->getProperties()->setCreator($value);
+
+ break;
+ case 0x05: // Keywords
+ $this->spreadsheet->getProperties()->setKeywords($value);
+
+ break;
+ case 0x06: // Comments (Description)
+ $this->spreadsheet->getProperties()->setDescription($value);
+
+ break;
+ case 0x07: // Template
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x08: // Last Saved By (LastModifiedBy)
+ $this->spreadsheet->getProperties()->setLastModifiedBy($value);
+
+ break;
+ case 0x09: // Revision
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x0A: // Total Editing Time
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x0B: // Last Printed
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x0C: // Created Date/Time
+ $this->spreadsheet->getProperties()->setCreated($value);
+
+ break;
+ case 0x0D: // Modified Date/Time
+ $this->spreadsheet->getProperties()->setModified($value);
+
+ break;
+ case 0x0E: // Number of Pages
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x0F: // Number of Words
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x10: // Number of Characters
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x11: // Thumbnail
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x12: // Name of creating application
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x13: // Security
+ // Not supported by PhpSpreadsheet
+ break;
+ }
+ }
+ }
+
+ /**
+ * Read additional document summary information.
+ */
+ private function readDocumentSummaryInformation()
+ {
+ if (!isset($this->documentSummaryInformation)) {
+ return;
+ }
+
+ // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
+ // offset: 2; size: 2;
+ // offset: 4; size: 2; OS version
+ // offset: 6; size: 2; OS indicator
+ // offset: 8; size: 16
+ // offset: 24; size: 4; section count
+ $secCount = self::getInt4d($this->documentSummaryInformation, 24);
+
+ // offset: 28; size: 16; first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae
+ // offset: 44; size: 4; first section offset
+ $secOffset = self::getInt4d($this->documentSummaryInformation, 44);
+
+ // section header
+ // offset: $secOffset; size: 4; section length
+ $secLength = self::getInt4d($this->documentSummaryInformation, $secOffset);
+
+ // offset: $secOffset+4; size: 4; property count
+ $countProperties = self::getInt4d($this->documentSummaryInformation, $secOffset + 4);
+
+ // initialize code page (used to resolve string values)
+ $codePage = 'CP1252';
+
+ // offset: ($secOffset+8); size: var
+ // loop through property decarations and properties
+ for ($i = 0; $i < $countProperties; ++$i) {
+ // offset: ($secOffset+8) + (8 * $i); size: 4; property ID
+ $id = self::getInt4d($this->documentSummaryInformation, ($secOffset + 8) + (8 * $i));
+
+ // Use value of property id as appropriate
+ // offset: 60 + 8 * $i; size: 4; offset from beginning of section (48)
+ $offset = self::getInt4d($this->documentSummaryInformation, ($secOffset + 12) + (8 * $i));
+
+ $type = self::getInt4d($this->documentSummaryInformation, $secOffset + $offset);
+
+ // initialize property value
+ $value = null;
+
+ // extract property value based on property type
+ switch ($type) {
+ case 0x02: // 2 byte signed integer
+ $value = self::getUInt2d($this->documentSummaryInformation, $secOffset + 4 + $offset);
+
+ break;
+ case 0x03: // 4 byte signed integer
+ $value = self::getInt4d($this->documentSummaryInformation, $secOffset + 4 + $offset);
+
+ break;
+ case 0x0B: // Boolean
+ $value = self::getUInt2d($this->documentSummaryInformation, $secOffset + 4 + $offset);
+ $value = ($value == 0 ? false : true);
+
+ break;
+ case 0x13: // 4 byte unsigned integer
+ // not needed yet, fix later if necessary
+ break;
+ case 0x1E: // null-terminated string prepended by dword string length
+ $byteLength = self::getInt4d($this->documentSummaryInformation, $secOffset + 4 + $offset);
+ $value = substr($this->documentSummaryInformation, $secOffset + 8 + $offset, $byteLength);
+ $value = StringHelper::convertEncoding($value, 'UTF-8', $codePage);
+ $value = rtrim($value);
+
+ break;
+ case 0x40: // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
+ // PHP-Time
+ $value = OLE::OLE2LocalDate(substr($this->documentSummaryInformation, $secOffset + 4 + $offset, 8));
+
+ break;
+ case 0x47: // Clipboard format
+ // not needed yet, fix later if necessary
+ break;
+ }
+
+ switch ($id) {
+ case 0x01: // Code Page
+ $codePage = CodePage::numberToName($value);
+
+ break;
+ case 0x02: // Category
+ $this->spreadsheet->getProperties()->setCategory($value);
+
+ break;
+ case 0x03: // Presentation Target
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x04: // Bytes
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x05: // Lines
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x06: // Paragraphs
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x07: // Slides
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x08: // Notes
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x09: // Hidden Slides
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x0A: // MM Clips
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x0B: // Scale Crop
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x0C: // Heading Pairs
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x0D: // Titles of Parts
+ // Not supported by PhpSpreadsheet
+ break;
+ case 0x0E: // Manager
+ $this->spreadsheet->getProperties()->setManager($value);
+
+ break;
+ case 0x0F: // Company
+ $this->spreadsheet->getProperties()->setCompany($value);
+
+ break;
+ case 0x10: // Links up-to-date
+ // Not supported by PhpSpreadsheet
+ break;
+ }
+ }
+ }
+
+ /**
+ * Reads a general type of BIFF record. Does nothing except for moving stream pointer forward to next record.
+ */
+ private function readDefault()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+ }
+
+ /**
+ * The NOTE record specifies a comment associated with a particular cell. In Excel 95 (BIFF7) and earlier versions,
+ * this record stores a note (cell note). This feature was significantly enhanced in Excel 97.
+ */
+ private function readNote()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->readDataOnly) {
+ return;
+ }
+
+ $cellAddress = $this->readBIFF8CellAddress(substr($recordData, 0, 4));
+ if ($this->version == self::XLS_BIFF8) {
+ $noteObjID = self::getUInt2d($recordData, 6);
+ $noteAuthor = self::readUnicodeStringLong(substr($recordData, 8));
+ $noteAuthor = $noteAuthor['value'];
+ $this->cellNotes[$noteObjID] = [
+ 'cellRef' => $cellAddress,
+ 'objectID' => $noteObjID,
+ 'author' => $noteAuthor,
+ ];
+ } else {
+ $extension = false;
+ if ($cellAddress == '$B$65536') {
+ // If the address row is -1 and the column is 0, (which translates as $B$65536) then this is a continuation
+ // note from the previous cell annotation. We're not yet handling this, so annotations longer than the
+ // max 2048 bytes will probably throw a wobbly.
+ $row = self::getUInt2d($recordData, 0);
+ $extension = true;
+ $cellAddress = array_pop(array_keys($this->phpSheet->getComments()));
+ }
+
+ $cellAddress = str_replace('$', '', $cellAddress);
+ $noteLength = self::getUInt2d($recordData, 4);
+ $noteText = trim(substr($recordData, 6));
+
+ if ($extension) {
+ // Concatenate this extension with the currently set comment for the cell
+ $comment = $this->phpSheet->getComment($cellAddress);
+ $commentText = $comment->getText()->getPlainText();
+ $comment->setText($this->parseRichText($commentText . $noteText));
+ } else {
+ // Set comment for the cell
+ $this->phpSheet->getComment($cellAddress)->setText($this->parseRichText($noteText));
+// ->setAuthor($author)
+ }
+ }
+ }
+
+ /**
+ * The TEXT Object record contains the text associated with a cell annotation.
+ */
+ private function readTextObject()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->readDataOnly) {
+ return;
+ }
+
+ // recordData consists of an array of subrecords looking like this:
+ // grbit: 2 bytes; Option Flags
+ // rot: 2 bytes; rotation
+ // cchText: 2 bytes; length of the text (in the first continue record)
+ // cbRuns: 2 bytes; length of the formatting (in the second continue record)
+ // followed by the continuation records containing the actual text and formatting
+ $grbitOpts = self::getUInt2d($recordData, 0);
+ $rot = self::getUInt2d($recordData, 2);
+ $cchText = self::getUInt2d($recordData, 10);
+ $cbRuns = self::getUInt2d($recordData, 12);
+ $text = $this->getSplicedRecordData();
+
+ $textByte = $text['spliceOffsets'][1] - $text['spliceOffsets'][0] - 1;
+ $textStr = substr($text['recordData'], $text['spliceOffsets'][0] + 1, $textByte);
+ // get 1 byte
+ $is16Bit = ord($text['recordData'][0]);
+ // it is possible to use a compressed format,
+ // which omits the high bytes of all characters, if they are all zero
+ if (($is16Bit & 0x01) === 0) {
+ $textStr = StringHelper::ConvertEncoding($textStr, 'UTF-8', 'ISO-8859-1');
+ } else {
+ $textStr = $this->decodeCodepage($textStr);
+ }
+
+ $this->textObjects[$this->textObjRef] = [
+ 'text' => $textStr,
+ 'format' => substr($text['recordData'], $text['spliceOffsets'][1], $cbRuns),
+ 'alignment' => $grbitOpts,
+ 'rotation' => $rot,
+ ];
+ }
+
+ /**
+ * Read BOF.
+ */
+ private function readBof()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = substr($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 2; size: 2; type of the following data
+ $substreamType = self::getUInt2d($recordData, 2);
+
+ switch ($substreamType) {
+ case self::XLS_WORKBOOKGLOBALS:
+ $version = self::getUInt2d($recordData, 0);
+ if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
+ throw new Exception('Cannot read this Excel file. Version is too old.');
+ }
+ $this->version = $version;
+
+ break;
+ case self::XLS_WORKSHEET:
+ // do not use this version information for anything
+ // it is unreliable (OpenOffice doc, 5.8), use only version information from the global stream
+ break;
+ default:
+ // substream, e.g. chart
+ // just skip the entire substream
+ do {
+ $code = self::getUInt2d($this->data, $this->pos);
+ $this->readDefault();
+ } while ($code != self::XLS_TYPE_EOF && $this->pos < $this->dataSize);
+
+ break;
+ }
+ }
+
+ /**
+ * FILEPASS.
+ *
+ * This record is part of the File Protection Block. It
+ * contains information about the read/write password of the
+ * file. All record contents following this record will be
+ * encrypted.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ *
+ * The decryption functions and objects used from here on in
+ * are based on the source of Spreadsheet-ParseExcel:
+ * https://metacpan.org/release/Spreadsheet-ParseExcel
+ */
+ private function readFilepass()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+
+ if ($length != 54) {
+ throw new Exception('Unexpected file pass record length');
+ }
+
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->verifyPassword('VelvetSweatshop', substr($recordData, 6, 16), substr($recordData, 22, 16), substr($recordData, 38, 16), $this->md5Ctxt)) {
+ throw new Exception('Decryption password incorrect');
+ }
+
+ $this->encryption = self::MS_BIFF_CRYPTO_RC4;
+
+ // Decryption required from the record after next onwards
+ $this->encryptionStartPos = $this->pos + self::getUInt2d($this->data, $this->pos + 2);
+ }
+
+ /**
+ * Make an RC4 decryptor for the given block.
+ *
+ * @param int $block Block for which to create decrypto
+ * @param string $valContext MD5 context state
+ *
+ * @return Xls\RC4
+ */
+ private function makeKey($block, $valContext)
+ {
+ $pwarray = str_repeat("\0", 64);
+
+ for ($i = 0; $i < 5; ++$i) {
+ $pwarray[$i] = $valContext[$i];
+ }
+
+ $pwarray[5] = chr($block & 0xff);
+ $pwarray[6] = chr(($block >> 8) & 0xff);
+ $pwarray[7] = chr(($block >> 16) & 0xff);
+ $pwarray[8] = chr(($block >> 24) & 0xff);
+
+ $pwarray[9] = "\x80";
+ $pwarray[56] = "\x48";
+
+ $md5 = new Xls\MD5();
+ $md5->add($pwarray);
+
+ $s = $md5->getContext();
+
+ return new Xls\RC4($s);
+ }
+
+ /**
+ * Verify RC4 file password.
+ *
+ * @param string $password Password to check
+ * @param string $docid Document id
+ * @param string $salt_data Salt data
+ * @param string $hashedsalt_data Hashed salt data
+ * @param string $valContext Set to the MD5 context of the value
+ *
+ * @return bool Success
+ */
+ private function verifyPassword($password, $docid, $salt_data, $hashedsalt_data, &$valContext)
+ {
+ $pwarray = str_repeat("\0", 64);
+
+ $iMax = strlen($password);
+ for ($i = 0; $i < $iMax; ++$i) {
+ $o = ord(substr($password, $i, 1));
+ $pwarray[2 * $i] = chr($o & 0xff);
+ $pwarray[2 * $i + 1] = chr(($o >> 8) & 0xff);
+ }
+ $pwarray[2 * $i] = chr(0x80);
+ $pwarray[56] = chr(($i << 4) & 0xff);
+
+ $md5 = new Xls\MD5();
+ $md5->add($pwarray);
+
+ $mdContext1 = $md5->getContext();
+
+ $offset = 0;
+ $keyoffset = 0;
+ $tocopy = 5;
+
+ $md5->reset();
+
+ while ($offset != 16) {
+ if ((64 - $offset) < 5) {
+ $tocopy = 64 - $offset;
+ }
+ for ($i = 0; $i <= $tocopy; ++$i) {
+ $pwarray[$offset + $i] = $mdContext1[$keyoffset + $i];
+ }
+ $offset += $tocopy;
+
+ if ($offset == 64) {
+ $md5->add($pwarray);
+ $keyoffset = $tocopy;
+ $tocopy = 5 - $tocopy;
+ $offset = 0;
+
+ continue;
+ }
+
+ $keyoffset = 0;
+ $tocopy = 5;
+ for ($i = 0; $i < 16; ++$i) {
+ $pwarray[$offset + $i] = $docid[$i];
+ }
+ $offset += 16;
+ }
+
+ $pwarray[16] = "\x80";
+ for ($i = 0; $i < 47; ++$i) {
+ $pwarray[17 + $i] = "\0";
+ }
+ $pwarray[56] = "\x80";
+ $pwarray[57] = "\x0a";
+
+ $md5->add($pwarray);
+ $valContext = $md5->getContext();
+
+ $key = $this->makeKey(0, $valContext);
+
+ $salt = $key->RC4($salt_data);
+ $hashedsalt = $key->RC4($hashedsalt_data);
+
+ $salt .= "\x80" . str_repeat("\0", 47);
+ $salt[56] = "\x80";
+
+ $md5->reset();
+ $md5->add($salt);
+ $mdContext2 = $md5->getContext();
+
+ return $mdContext2 == $hashedsalt;
+ }
+
+ /**
+ * CODEPAGE.
+ *
+ * This record stores the text encoding used to write byte
+ * strings, stored as MS Windows code page identifier.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readCodepage()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; code page identifier
+ $codepage = self::getUInt2d($recordData, 0);
+
+ $this->codepage = CodePage::numberToName($codepage);
+ }
+
+ /**
+ * DATEMODE.
+ *
+ * This record specifies the base date for displaying date
+ * values. All dates are stored as count of days past this
+ * base date. In BIFF2-BIFF4 this record is part of the
+ * Calculation Settings Block. In BIFF5-BIFF8 it is
+ * stored in the Workbook Globals Substream.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readDateMode()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; 0 = base 1900, 1 = base 1904
+ Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
+ if (ord($recordData[0]) == 1) {
+ Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
+ }
+ }
+
+ /**
+ * Read a FONT record.
+ */
+ private function readFont()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ $objFont = new Font();
+
+ // offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
+ $size = self::getUInt2d($recordData, 0);
+ $objFont->setSize($size / 20);
+
+ // offset: 2; size: 2; option flags
+ // bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
+ // bit: 1; mask 0x0002; italic
+ $isItalic = (0x0002 & self::getUInt2d($recordData, 2)) >> 1;
+ if ($isItalic) {
+ $objFont->setItalic(true);
+ }
+
+ // bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
+ // bit: 3; mask 0x0008; strikethrough
+ $isStrike = (0x0008 & self::getUInt2d($recordData, 2)) >> 3;
+ if ($isStrike) {
+ $objFont->setStrikethrough(true);
+ }
+
+ // offset: 4; size: 2; colour index
+ $colorIndex = self::getUInt2d($recordData, 4);
+ $objFont->colorIndex = $colorIndex;
+
+ // offset: 6; size: 2; font weight
+ $weight = self::getUInt2d($recordData, 6);
+ switch ($weight) {
+ case 0x02BC:
+ $objFont->setBold(true);
+
+ break;
+ }
+
+ // offset: 8; size: 2; escapement type
+ $escapement = self::getUInt2d($recordData, 8);
+ switch ($escapement) {
+ case 0x0001:
+ $objFont->setSuperscript(true);
+
+ break;
+ case 0x0002:
+ $objFont->setSubscript(true);
+
+ break;
+ }
+
+ // offset: 10; size: 1; underline type
+ $underlineType = ord($recordData[10]);
+ switch ($underlineType) {
+ case 0x00:
+ break; // no underline
+ case 0x01:
+ $objFont->setUnderline(Font::UNDERLINE_SINGLE);
+
+ break;
+ case 0x02:
+ $objFont->setUnderline(Font::UNDERLINE_DOUBLE);
+
+ break;
+ case 0x21:
+ $objFont->setUnderline(Font::UNDERLINE_SINGLEACCOUNTING);
+
+ break;
+ case 0x22:
+ $objFont->setUnderline(Font::UNDERLINE_DOUBLEACCOUNTING);
+
+ break;
+ }
+
+ // offset: 11; size: 1; font family
+ // offset: 12; size: 1; character set
+ // offset: 13; size: 1; not used
+ // offset: 14; size: var; font name
+ if ($this->version == self::XLS_BIFF8) {
+ $string = self::readUnicodeStringShort(substr($recordData, 14));
+ } else {
+ $string = $this->readByteStringShort(substr($recordData, 14));
+ }
+ $objFont->setName($string['value']);
+
+ $this->objFonts[] = $objFont;
+ }
+ }
+
+ /**
+ * FORMAT.
+ *
+ * This record contains information about a number format.
+ * All FORMAT records occur together in a sequential list.
+ *
+ * In BIFF2-BIFF4 other records referencing a FORMAT record
+ * contain a zero-based index into this list. From BIFF5 on
+ * the FORMAT record contains the index itself that will be
+ * used by other records.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readFormat()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ $indexCode = self::getUInt2d($recordData, 0);
+
+ if ($this->version == self::XLS_BIFF8) {
+ $string = self::readUnicodeStringLong(substr($recordData, 2));
+ } else {
+ // BIFF7
+ $string = $this->readByteStringShort(substr($recordData, 2));
+ }
+
+ $formatString = $string['value'];
+ $this->formats[$indexCode] = $formatString;
+ }
+ }
+
+ /**
+ * XF - Extended Format.
+ *
+ * This record contains formatting information for cells, rows, columns or styles.
+ * According to https://support.microsoft.com/en-us/help/147732 there are always at least 15 cell style XF
+ * and 1 cell XF.
+ * Inspection of Excel files generated by MS Office Excel shows that XF records 0-14 are cell style XF
+ * and XF record 15 is a cell XF
+ * We only read the first cell style XF and skip the remaining cell style XF records
+ * We read all cell XF records.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readXf()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ $objStyle = new Style();
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; Index to FONT record
+ if (self::getUInt2d($recordData, 0) < 4) {
+ $fontIndex = self::getUInt2d($recordData, 0);
+ } else {
+ // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
+ // check the OpenOffice documentation of the FONT record
+ $fontIndex = self::getUInt2d($recordData, 0) - 1;
+ }
+ $objStyle->setFont($this->objFonts[$fontIndex]);
+
+ // offset: 2; size: 2; Index to FORMAT record
+ $numberFormatIndex = self::getUInt2d($recordData, 2);
+ if (isset($this->formats[$numberFormatIndex])) {
+ // then we have user-defined format code
+ $numberFormat = ['formatCode' => $this->formats[$numberFormatIndex]];
+ } elseif (($code = NumberFormat::builtInFormatCode($numberFormatIndex)) !== '') {
+ // then we have built-in format code
+ $numberFormat = ['formatCode' => $code];
+ } else {
+ // we set the general format code
+ $numberFormat = ['formatCode' => 'General'];
+ }
+ $objStyle->getNumberFormat()->setFormatCode($numberFormat['formatCode']);
+
+ // offset: 4; size: 2; XF type, cell protection, and parent style XF
+ // bit 2-0; mask 0x0007; XF_TYPE_PROT
+ $xfTypeProt = self::getUInt2d($recordData, 4);
+ // bit 0; mask 0x01; 1 = cell is locked
+ $isLocked = (0x01 & $xfTypeProt) >> 0;
+ $objStyle->getProtection()->setLocked($isLocked ? Protection::PROTECTION_INHERIT : Protection::PROTECTION_UNPROTECTED);
+
+ // bit 1; mask 0x02; 1 = Formula is hidden
+ $isHidden = (0x02 & $xfTypeProt) >> 1;
+ $objStyle->getProtection()->setHidden($isHidden ? Protection::PROTECTION_PROTECTED : Protection::PROTECTION_UNPROTECTED);
+
+ // bit 2; mask 0x04; 0 = Cell XF, 1 = Cell Style XF
+ $isCellStyleXf = (0x04 & $xfTypeProt) >> 2;
+
+ // offset: 6; size: 1; Alignment and text break
+ // bit 2-0, mask 0x07; horizontal alignment
+ $horAlign = (0x07 & ord($recordData[6])) >> 0;
+ switch ($horAlign) {
+ case 0:
+ $objStyle->getAlignment()->setHorizontal(Alignment::HORIZONTAL_GENERAL);
+
+ break;
+ case 1:
+ $objStyle->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
+
+ break;
+ case 2:
+ $objStyle->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
+
+ break;
+ case 3:
+ $objStyle->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
+
+ break;
+ case 4:
+ $objStyle->getAlignment()->setHorizontal(Alignment::HORIZONTAL_FILL);
+
+ break;
+ case 5:
+ $objStyle->getAlignment()->setHorizontal(Alignment::HORIZONTAL_JUSTIFY);
+
+ break;
+ case 6:
+ $objStyle->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER_CONTINUOUS);
+
+ break;
+ }
+ // bit 3, mask 0x08; wrap text
+ $wrapText = (0x08 & ord($recordData[6])) >> 3;
+ switch ($wrapText) {
+ case 0:
+ $objStyle->getAlignment()->setWrapText(false);
+
+ break;
+ case 1:
+ $objStyle->getAlignment()->setWrapText(true);
+
+ break;
+ }
+ // bit 6-4, mask 0x70; vertical alignment
+ $vertAlign = (0x70 & ord($recordData[6])) >> 4;
+ switch ($vertAlign) {
+ case 0:
+ $objStyle->getAlignment()->setVertical(Alignment::VERTICAL_TOP);
+
+ break;
+ case 1:
+ $objStyle->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
+
+ break;
+ case 2:
+ $objStyle->getAlignment()->setVertical(Alignment::VERTICAL_BOTTOM);
+
+ break;
+ case 3:
+ $objStyle->getAlignment()->setVertical(Alignment::VERTICAL_JUSTIFY);
+
+ break;
+ }
+
+ if ($this->version == self::XLS_BIFF8) {
+ // offset: 7; size: 1; XF_ROTATION: Text rotation angle
+ $angle = ord($recordData[7]);
+ $rotation = 0;
+ if ($angle <= 90) {
+ $rotation = $angle;
+ } elseif ($angle <= 180) {
+ $rotation = 90 - $angle;
+ } elseif ($angle == 255) {
+ $rotation = -165;
+ }
+ $objStyle->getAlignment()->setTextRotation($rotation);
+
+ // offset: 8; size: 1; Indentation, shrink to cell size, and text direction
+ // bit: 3-0; mask: 0x0F; indent level
+ $indent = (0x0F & ord($recordData[8])) >> 0;
+ $objStyle->getAlignment()->setIndent($indent);
+
+ // bit: 4; mask: 0x10; 1 = shrink content to fit into cell
+ $shrinkToFit = (0x10 & ord($recordData[8])) >> 4;
+ switch ($shrinkToFit) {
+ case 0:
+ $objStyle->getAlignment()->setShrinkToFit(false);
+
+ break;
+ case 1:
+ $objStyle->getAlignment()->setShrinkToFit(true);
+
+ break;
+ }
+
+ // offset: 9; size: 1; Flags used for attribute groups
+
+ // offset: 10; size: 4; Cell border lines and background area
+ // bit: 3-0; mask: 0x0000000F; left style
+ if ($bordersLeftStyle = Xls\Style\Border::lookup((0x0000000F & self::getInt4d($recordData, 10)) >> 0)) {
+ $objStyle->getBorders()->getLeft()->setBorderStyle($bordersLeftStyle);
+ }
+ // bit: 7-4; mask: 0x000000F0; right style
+ if ($bordersRightStyle = Xls\Style\Border::lookup((0x000000F0 & self::getInt4d($recordData, 10)) >> 4)) {
+ $objStyle->getBorders()->getRight()->setBorderStyle($bordersRightStyle);
+ }
+ // bit: 11-8; mask: 0x00000F00; top style
+ if ($bordersTopStyle = Xls\Style\Border::lookup((0x00000F00 & self::getInt4d($recordData, 10)) >> 8)) {
+ $objStyle->getBorders()->getTop()->setBorderStyle($bordersTopStyle);
+ }
+ // bit: 15-12; mask: 0x0000F000; bottom style
+ if ($bordersBottomStyle = Xls\Style\Border::lookup((0x0000F000 & self::getInt4d($recordData, 10)) >> 12)) {
+ $objStyle->getBorders()->getBottom()->setBorderStyle($bordersBottomStyle);
+ }
+ // bit: 22-16; mask: 0x007F0000; left color
+ $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & self::getInt4d($recordData, 10)) >> 16;
+
+ // bit: 29-23; mask: 0x3F800000; right color
+ $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & self::getInt4d($recordData, 10)) >> 23;
+
+ // bit: 30; mask: 0x40000000; 1 = diagonal line from top left to right bottom
+ $diagonalDown = (0x40000000 & self::getInt4d($recordData, 10)) >> 30 ? true : false;
+
+ // bit: 31; mask: 0x80000000; 1 = diagonal line from bottom left to top right
+ $diagonalUp = (0x80000000 & self::getInt4d($recordData, 10)) >> 31 ? true : false;
+
+ if ($diagonalUp == false && $diagonalDown == false) {
+ $objStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_NONE);
+ } elseif ($diagonalUp == true && $diagonalDown == false) {
+ $objStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_UP);
+ } elseif ($diagonalUp == false && $diagonalDown == true) {
+ $objStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_DOWN);
+ } elseif ($diagonalUp == true && $diagonalDown == true) {
+ $objStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_BOTH);
+ }
+
+ // offset: 14; size: 4;
+ // bit: 6-0; mask: 0x0000007F; top color
+ $objStyle->getBorders()->getTop()->colorIndex = (0x0000007F & self::getInt4d($recordData, 14)) >> 0;
+
+ // bit: 13-7; mask: 0x00003F80; bottom color
+ $objStyle->getBorders()->getBottom()->colorIndex = (0x00003F80 & self::getInt4d($recordData, 14)) >> 7;
+
+ // bit: 20-14; mask: 0x001FC000; diagonal color
+ $objStyle->getBorders()->getDiagonal()->colorIndex = (0x001FC000 & self::getInt4d($recordData, 14)) >> 14;
+
+ // bit: 24-21; mask: 0x01E00000; diagonal style
+ if ($bordersDiagonalStyle = Xls\Style\Border::lookup((0x01E00000 & self::getInt4d($recordData, 14)) >> 21)) {
+ $objStyle->getBorders()->getDiagonal()->setBorderStyle($bordersDiagonalStyle);
+ }
+
+ // bit: 31-26; mask: 0xFC000000 fill pattern
+ if ($fillType = Xls\Style\FillPattern::lookup((0xFC000000 & self::getInt4d($recordData, 14)) >> 26)) {
+ $objStyle->getFill()->setFillType($fillType);
+ }
+ // offset: 18; size: 2; pattern and background colour
+ // bit: 6-0; mask: 0x007F; color index for pattern color
+ $objStyle->getFill()->startcolorIndex = (0x007F & self::getUInt2d($recordData, 18)) >> 0;
+
+ // bit: 13-7; mask: 0x3F80; color index for pattern background
+ $objStyle->getFill()->endcolorIndex = (0x3F80 & self::getUInt2d($recordData, 18)) >> 7;
+ } else {
+ // BIFF5
+
+ // offset: 7; size: 1; Text orientation and flags
+ $orientationAndFlags = ord($recordData[7]);
+
+ // bit: 1-0; mask: 0x03; XF_ORIENTATION: Text orientation
+ $xfOrientation = (0x03 & $orientationAndFlags) >> 0;
+ switch ($xfOrientation) {
+ case 0:
+ $objStyle->getAlignment()->setTextRotation(0);
+
+ break;
+ case 1:
+ $objStyle->getAlignment()->setTextRotation(-165);
+
+ break;
+ case 2:
+ $objStyle->getAlignment()->setTextRotation(90);
+
+ break;
+ case 3:
+ $objStyle->getAlignment()->setTextRotation(-90);
+
+ break;
+ }
+
+ // offset: 8; size: 4; cell border lines and background area
+ $borderAndBackground = self::getInt4d($recordData, 8);
+
+ // bit: 6-0; mask: 0x0000007F; color index for pattern color
+ $objStyle->getFill()->startcolorIndex = (0x0000007F & $borderAndBackground) >> 0;
+
+ // bit: 13-7; mask: 0x00003F80; color index for pattern background
+ $objStyle->getFill()->endcolorIndex = (0x00003F80 & $borderAndBackground) >> 7;
+
+ // bit: 21-16; mask: 0x003F0000; fill pattern
+ $objStyle->getFill()->setFillType(Xls\Style\FillPattern::lookup((0x003F0000 & $borderAndBackground) >> 16));
+
+ // bit: 24-22; mask: 0x01C00000; bottom line style
+ $objStyle->getBorders()->getBottom()->setBorderStyle(Xls\Style\Border::lookup((0x01C00000 & $borderAndBackground) >> 22));
+
+ // bit: 31-25; mask: 0xFE000000; bottom line color
+ $objStyle->getBorders()->getBottom()->colorIndex = (0xFE000000 & $borderAndBackground) >> 25;
+
+ // offset: 12; size: 4; cell border lines
+ $borderLines = self::getInt4d($recordData, 12);
+
+ // bit: 2-0; mask: 0x00000007; top line style
+ $objStyle->getBorders()->getTop()->setBorderStyle(Xls\Style\Border::lookup((0x00000007 & $borderLines) >> 0));
+
+ // bit: 5-3; mask: 0x00000038; left line style
+ $objStyle->getBorders()->getLeft()->setBorderStyle(Xls\Style\Border::lookup((0x00000038 & $borderLines) >> 3));
+
+ // bit: 8-6; mask: 0x000001C0; right line style
+ $objStyle->getBorders()->getRight()->setBorderStyle(Xls\Style\Border::lookup((0x000001C0 & $borderLines) >> 6));
+
+ // bit: 15-9; mask: 0x0000FE00; top line color index
+ $objStyle->getBorders()->getTop()->colorIndex = (0x0000FE00 & $borderLines) >> 9;
+
+ // bit: 22-16; mask: 0x007F0000; left line color index
+ $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & $borderLines) >> 16;
+
+ // bit: 29-23; mask: 0x3F800000; right line color index
+ $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & $borderLines) >> 23;
+ }
+
+ // add cellStyleXf or cellXf and update mapping
+ if ($isCellStyleXf) {
+ // we only read one style XF record which is always the first
+ if ($this->xfIndex == 0) {
+ $this->spreadsheet->addCellStyleXf($objStyle);
+ $this->mapCellStyleXfIndex[$this->xfIndex] = 0;
+ }
+ } else {
+ // we read all cell XF records
+ $this->spreadsheet->addCellXf($objStyle);
+ $this->mapCellXfIndex[$this->xfIndex] = count($this->spreadsheet->getCellXfCollection()) - 1;
+ }
+
+ // update XF index for when we read next record
+ ++$this->xfIndex;
+ }
+ }
+
+ private function readXfExt()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; 0x087D = repeated header
+
+ // offset: 2; size: 2
+
+ // offset: 4; size: 8; not used
+
+ // offset: 12; size: 2; record version
+
+ // offset: 14; size: 2; index to XF record which this record modifies
+ $ixfe = self::getUInt2d($recordData, 14);
+
+ // offset: 16; size: 2; not used
+
+ // offset: 18; size: 2; number of extension properties that follow
+ $cexts = self::getUInt2d($recordData, 18);
+
+ // start reading the actual extension data
+ $offset = 20;
+ while ($offset < $length) {
+ // extension type
+ $extType = self::getUInt2d($recordData, $offset);
+
+ // extension length
+ $cb = self::getUInt2d($recordData, $offset + 2);
+
+ // extension data
+ $extData = substr($recordData, $offset + 4, $cb);
+
+ switch ($extType) {
+ case 4: // fill start color
+ $xclfType = self::getUInt2d($extData, 0); // color type
+ $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
+
+ if ($xclfType == 2) {
+ $rgb = sprintf('%02X%02X%02X', ord($xclrValue[0]), ord($xclrValue[1]), ord($xclrValue[2]));
+
+ // modify the relevant style property
+ if (isset($this->mapCellXfIndex[$ixfe])) {
+ $fill = $this->spreadsheet->getCellXfByIndex($this->mapCellXfIndex[$ixfe])->getFill();
+ $fill->getStartColor()->setRGB($rgb);
+ unset($fill->startcolorIndex); // normal color index does not apply, discard
+ }
+ }
+
+ break;
+ case 5: // fill end color
+ $xclfType = self::getUInt2d($extData, 0); // color type
+ $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
+
+ if ($xclfType == 2) {
+ $rgb = sprintf('%02X%02X%02X', ord($xclrValue[0]), ord($xclrValue[1]), ord($xclrValue[2]));
+
+ // modify the relevant style property
+ if (isset($this->mapCellXfIndex[$ixfe])) {
+ $fill = $this->spreadsheet->getCellXfByIndex($this->mapCellXfIndex[$ixfe])->getFill();
+ $fill->getEndColor()->setRGB($rgb);
+ unset($fill->endcolorIndex); // normal color index does not apply, discard
+ }
+ }
+
+ break;
+ case 7: // border color top
+ $xclfType = self::getUInt2d($extData, 0); // color type
+ $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
+
+ if ($xclfType == 2) {
+ $rgb = sprintf('%02X%02X%02X', ord($xclrValue[0]), ord($xclrValue[1]), ord($xclrValue[2]));
+
+ // modify the relevant style property
+ if (isset($this->mapCellXfIndex[$ixfe])) {
+ $top = $this->spreadsheet->getCellXfByIndex($this->mapCellXfIndex[$ixfe])->getBorders()->getTop();
+ $top->getColor()->setRGB($rgb);
+ unset($top->colorIndex); // normal color index does not apply, discard
+ }
+ }
+
+ break;
+ case 8: // border color bottom
+ $xclfType = self::getUInt2d($extData, 0); // color type
+ $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
+
+ if ($xclfType == 2) {
+ $rgb = sprintf('%02X%02X%02X', ord($xclrValue[0]), ord($xclrValue[1]), ord($xclrValue[2]));
+
+ // modify the relevant style property
+ if (isset($this->mapCellXfIndex[$ixfe])) {
+ $bottom = $this->spreadsheet->getCellXfByIndex($this->mapCellXfIndex[$ixfe])->getBorders()->getBottom();
+ $bottom->getColor()->setRGB($rgb);
+ unset($bottom->colorIndex); // normal color index does not apply, discard
+ }
+ }
+
+ break;
+ case 9: // border color left
+ $xclfType = self::getUInt2d($extData, 0); // color type
+ $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
+
+ if ($xclfType == 2) {
+ $rgb = sprintf('%02X%02X%02X', ord($xclrValue[0]), ord($xclrValue[1]), ord($xclrValue[2]));
+
+ // modify the relevant style property
+ if (isset($this->mapCellXfIndex[$ixfe])) {
+ $left = $this->spreadsheet->getCellXfByIndex($this->mapCellXfIndex[$ixfe])->getBorders()->getLeft();
+ $left->getColor()->setRGB($rgb);
+ unset($left->colorIndex); // normal color index does not apply, discard
+ }
+ }
+
+ break;
+ case 10: // border color right
+ $xclfType = self::getUInt2d($extData, 0); // color type
+ $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
+
+ if ($xclfType == 2) {
+ $rgb = sprintf('%02X%02X%02X', ord($xclrValue[0]), ord($xclrValue[1]), ord($xclrValue[2]));
+
+ // modify the relevant style property
+ if (isset($this->mapCellXfIndex[$ixfe])) {
+ $right = $this->spreadsheet->getCellXfByIndex($this->mapCellXfIndex[$ixfe])->getBorders()->getRight();
+ $right->getColor()->setRGB($rgb);
+ unset($right->colorIndex); // normal color index does not apply, discard
+ }
+ }
+
+ break;
+ case 11: // border color diagonal
+ $xclfType = self::getUInt2d($extData, 0); // color type
+ $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
+
+ if ($xclfType == 2) {
+ $rgb = sprintf('%02X%02X%02X', ord($xclrValue[0]), ord($xclrValue[1]), ord($xclrValue[2]));
+
+ // modify the relevant style property
+ if (isset($this->mapCellXfIndex[$ixfe])) {
+ $diagonal = $this->spreadsheet->getCellXfByIndex($this->mapCellXfIndex[$ixfe])->getBorders()->getDiagonal();
+ $diagonal->getColor()->setRGB($rgb);
+ unset($diagonal->colorIndex); // normal color index does not apply, discard
+ }
+ }
+
+ break;
+ case 13: // font color
+ $xclfType = self::getUInt2d($extData, 0); // color type
+ $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
+
+ if ($xclfType == 2) {
+ $rgb = sprintf('%02X%02X%02X', ord($xclrValue[0]), ord($xclrValue[1]), ord($xclrValue[2]));
+
+ // modify the relevant style property
+ if (isset($this->mapCellXfIndex[$ixfe])) {
+ $font = $this->spreadsheet->getCellXfByIndex($this->mapCellXfIndex[$ixfe])->getFont();
+ $font->getColor()->setRGB($rgb);
+ unset($font->colorIndex); // normal color index does not apply, discard
+ }
+ }
+
+ break;
+ }
+
+ $offset += $cb;
+ }
+ }
+ }
+
+ /**
+ * Read STYLE record.
+ */
+ private function readStyle()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; index to XF record and flag for built-in style
+ $ixfe = self::getUInt2d($recordData, 0);
+
+ // bit: 11-0; mask 0x0FFF; index to XF record
+ $xfIndex = (0x0FFF & $ixfe) >> 0;
+
+ // bit: 15; mask 0x8000; 0 = user-defined style, 1 = built-in style
+ $isBuiltIn = (bool) ((0x8000 & $ixfe) >> 15);
+
+ if ($isBuiltIn) {
+ // offset: 2; size: 1; identifier for built-in style
+ $builtInId = ord($recordData[2]);
+
+ switch ($builtInId) {
+ case 0x00:
+ // currently, we are not using this for anything
+ break;
+ default:
+ break;
+ }
+ }
+ // user-defined; not supported by PhpSpreadsheet
+ }
+ }
+
+ /**
+ * Read PALETTE record.
+ */
+ private function readPalette()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; number of following colors
+ $nm = self::getUInt2d($recordData, 0);
+
+ // list of RGB colors
+ for ($i = 0; $i < $nm; ++$i) {
+ $rgb = substr($recordData, 2 + 4 * $i, 4);
+ $this->palette[] = self::readRGB($rgb);
+ }
+ }
+ }
+
+ /**
+ * SHEET.
+ *
+ * This record is located in the Workbook Globals
+ * Substream and represents a sheet inside the workbook.
+ * One SHEET record is written for each sheet. It stores the
+ * sheet name and a stream offset to the BOF record of the
+ * respective Sheet Substream within the Workbook Stream.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readSheet()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // offset: 0; size: 4; absolute stream position of the BOF record of the sheet
+ // NOTE: not encrypted
+ $rec_offset = self::getInt4d($this->data, $this->pos + 4);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 4; size: 1; sheet state
+ switch (ord($recordData[4])) {
+ case 0x00:
+ $sheetState = Worksheet::SHEETSTATE_VISIBLE;
+
+ break;
+ case 0x01:
+ $sheetState = Worksheet::SHEETSTATE_HIDDEN;
+
+ break;
+ case 0x02:
+ $sheetState = Worksheet::SHEETSTATE_VERYHIDDEN;
+
+ break;
+ default:
+ $sheetState = Worksheet::SHEETSTATE_VISIBLE;
+
+ break;
+ }
+
+ // offset: 5; size: 1; sheet type
+ $sheetType = ord($recordData[5]);
+
+ // offset: 6; size: var; sheet name
+ if ($this->version == self::XLS_BIFF8) {
+ $string = self::readUnicodeStringShort(substr($recordData, 6));
+ $rec_name = $string['value'];
+ } elseif ($this->version == self::XLS_BIFF7) {
+ $string = $this->readByteStringShort(substr($recordData, 6));
+ $rec_name = $string['value'];
+ }
+
+ $this->sheets[] = [
+ 'name' => $rec_name,
+ 'offset' => $rec_offset,
+ 'sheetState' => $sheetState,
+ 'sheetType' => $sheetType,
+ ];
+ }
+
+ /**
+ * Read EXTERNALBOOK record.
+ */
+ private function readExternalBook()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset within record data
+ $offset = 0;
+
+ // there are 4 types of records
+ if (strlen($recordData) > 4) {
+ // external reference
+ // offset: 0; size: 2; number of sheet names ($nm)
+ $nm = self::getUInt2d($recordData, 0);
+ $offset += 2;
+
+ // offset: 2; size: var; encoded URL without sheet name (Unicode string, 16-bit length)
+ $encodedUrlString = self::readUnicodeStringLong(substr($recordData, 2));
+ $offset += $encodedUrlString['size'];
+
+ // offset: var; size: var; list of $nm sheet names (Unicode strings, 16-bit length)
+ $externalSheetNames = [];
+ for ($i = 0; $i < $nm; ++$i) {
+ $externalSheetNameString = self::readUnicodeStringLong(substr($recordData, $offset));
+ $externalSheetNames[] = $externalSheetNameString['value'];
+ $offset += $externalSheetNameString['size'];
+ }
+
+ // store the record data
+ $this->externalBooks[] = [
+ 'type' => 'external',
+ 'encodedUrl' => $encodedUrlString['value'],
+ 'externalSheetNames' => $externalSheetNames,
+ ];
+ } elseif (substr($recordData, 2, 2) == pack('CC', 0x01, 0x04)) {
+ // internal reference
+ // offset: 0; size: 2; number of sheet in this document
+ // offset: 2; size: 2; 0x01 0x04
+ $this->externalBooks[] = [
+ 'type' => 'internal',
+ ];
+ } elseif (substr($recordData, 0, 4) == pack('vCC', 0x0001, 0x01, 0x3A)) {
+ // add-in function
+ // offset: 0; size: 2; 0x0001
+ $this->externalBooks[] = [
+ 'type' => 'addInFunction',
+ ];
+ } elseif (substr($recordData, 0, 2) == pack('v', 0x0000)) {
+ // DDE links, OLE links
+ // offset: 0; size: 2; 0x0000
+ // offset: 2; size: var; encoded source document name
+ $this->externalBooks[] = [
+ 'type' => 'DDEorOLE',
+ ];
+ }
+ }
+
+ /**
+ * Read EXTERNNAME record.
+ */
+ private function readExternName()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // external sheet references provided for named cells
+ if ($this->version == self::XLS_BIFF8) {
+ // offset: 0; size: 2; options
+ $options = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2;
+
+ // offset: 4; size: 2; not used
+
+ // offset: 6; size: var
+ $nameString = self::readUnicodeStringShort(substr($recordData, 6));
+
+ // offset: var; size: var; formula data
+ $offset = 6 + $nameString['size'];
+ $formula = $this->getFormulaFromStructure(substr($recordData, $offset));
+
+ $this->externalNames[] = [
+ 'name' => $nameString['value'],
+ 'formula' => $formula,
+ ];
+ }
+ }
+
+ /**
+ * Read EXTERNSHEET record.
+ */
+ private function readExternSheet()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // external sheet references provided for named cells
+ if ($this->version == self::XLS_BIFF8) {
+ // offset: 0; size: 2; number of following ref structures
+ $nm = self::getUInt2d($recordData, 0);
+ for ($i = 0; $i < $nm; ++$i) {
+ $this->ref[] = [
+ // offset: 2 + 6 * $i; index to EXTERNALBOOK record
+ 'externalBookIndex' => self::getUInt2d($recordData, 2 + 6 * $i),
+ // offset: 4 + 6 * $i; index to first sheet in EXTERNALBOOK record
+ 'firstSheetIndex' => self::getUInt2d($recordData, 4 + 6 * $i),
+ // offset: 6 + 6 * $i; index to last sheet in EXTERNALBOOK record
+ 'lastSheetIndex' => self::getUInt2d($recordData, 6 + 6 * $i),
+ ];
+ }
+ }
+ }
+
+ /**
+ * DEFINEDNAME.
+ *
+ * This record is part of a Link Table. It contains the name
+ * and the token array of an internal defined name. Token
+ * arrays of defined names contain tokens with aberrant
+ * token classes.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readDefinedName()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->version == self::XLS_BIFF8) {
+ // retrieves named cells
+
+ // offset: 0; size: 2; option flags
+ $opts = self::getUInt2d($recordData, 0);
+
+ // bit: 5; mask: 0x0020; 0 = user-defined name, 1 = built-in-name
+ $isBuiltInName = (0x0020 & $opts) >> 5;
+
+ // offset: 2; size: 1; keyboard shortcut
+
+ // offset: 3; size: 1; length of the name (character count)
+ $nlen = ord($recordData[3]);
+
+ // offset: 4; size: 2; size of the formula data (it can happen that this is zero)
+ // note: there can also be additional data, this is not included in $flen
+ $flen = self::getUInt2d($recordData, 4);
+
+ // offset: 8; size: 2; 0=Global name, otherwise index to sheet (1-based)
+ $scope = self::getUInt2d($recordData, 8);
+
+ // offset: 14; size: var; Name (Unicode string without length field)
+ $string = self::readUnicodeString(substr($recordData, 14), $nlen);
+
+ // offset: var; size: $flen; formula data
+ $offset = 14 + $string['size'];
+ $formulaStructure = pack('v', $flen) . substr($recordData, $offset);
+
+ try {
+ $formula = $this->getFormulaFromStructure($formulaStructure);
+ } catch (PhpSpreadsheetException $e) {
+ $formula = '';
+ }
+
+ $this->definedname[] = [
+ 'isBuiltInName' => $isBuiltInName,
+ 'name' => $string['value'],
+ 'formula' => $formula,
+ 'scope' => $scope,
+ ];
+ }
+ }
+
+ /**
+ * Read MSODRAWINGGROUP record.
+ */
+ private function readMsoDrawingGroup()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+
+ // get spliced record data
+ $splicedRecordData = $this->getSplicedRecordData();
+ $recordData = $splicedRecordData['recordData'];
+
+ $this->drawingGroupData .= $recordData;
+ }
+
+ /**
+ * SST - Shared String Table.
+ *
+ * This record contains a list of all strings used anywhere
+ * in the workbook. Each string occurs only once. The
+ * workbook uses indexes into the list to reference the
+ * strings.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readSst()
+ {
+ // offset within (spliced) record data
+ $pos = 0;
+
+ // get spliced record data
+ $splicedRecordData = $this->getSplicedRecordData();
+
+ $recordData = $splicedRecordData['recordData'];
+ $spliceOffsets = $splicedRecordData['spliceOffsets'];
+
+ // offset: 0; size: 4; total number of strings in the workbook
+ $pos += 4;
+
+ // offset: 4; size: 4; number of following strings ($nm)
+ $nm = self::getInt4d($recordData, 4);
+ $pos += 4;
+
+ // loop through the Unicode strings (16-bit length)
+ for ($i = 0; $i < $nm; ++$i) {
+ // number of characters in the Unicode string
+ $numChars = self::getUInt2d($recordData, $pos);
+ $pos += 2;
+
+ // option flags
+ $optionFlags = ord($recordData[$pos]);
+ ++$pos;
+
+ // bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
+ $isCompressed = (($optionFlags & 0x01) == 0);
+
+ // bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
+ $hasAsian = (($optionFlags & 0x04) != 0);
+
+ // bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
+ $hasRichText = (($optionFlags & 0x08) != 0);
+
+ if ($hasRichText) {
+ // number of Rich-Text formatting runs
+ $formattingRuns = self::getUInt2d($recordData, $pos);
+ $pos += 2;
+ }
+
+ if ($hasAsian) {
+ // size of Asian phonetic setting
+ $extendedRunLength = self::getInt4d($recordData, $pos);
+ $pos += 4;
+ }
+
+ // expected byte length of character array if not split
+ $len = ($isCompressed) ? $numChars : $numChars * 2;
+
+ // look up limit position
+ foreach ($spliceOffsets as $spliceOffset) {
+ // it can happen that the string is empty, therefore we need
+ // <= and not just <
+ if ($pos <= $spliceOffset) {
+ $limitpos = $spliceOffset;
+
+ break;
+ }
+ }
+
+ if ($pos + $len <= $limitpos) {
+ // character array is not split between records
+
+ $retstr = substr($recordData, $pos, $len);
+ $pos += $len;
+ } else {
+ // character array is split between records
+
+ // first part of character array
+ $retstr = substr($recordData, $pos, $limitpos - $pos);
+
+ $bytesRead = $limitpos - $pos;
+
+ // remaining characters in Unicode string
+ $charsLeft = $numChars - (($isCompressed) ? $bytesRead : ($bytesRead / 2));
+
+ $pos = $limitpos;
+
+ // keep reading the characters
+ while ($charsLeft > 0) {
+ // look up next limit position, in case the string span more than one continue record
+ foreach ($spliceOffsets as $spliceOffset) {
+ if ($pos < $spliceOffset) {
+ $limitpos = $spliceOffset;
+
+ break;
+ }
+ }
+
+ // repeated option flags
+ // OpenOffice.org documentation 5.21
+ $option = ord($recordData[$pos]);
+ ++$pos;
+
+ if ($isCompressed && ($option == 0)) {
+ // 1st fragment compressed
+ // this fragment compressed
+ $len = min($charsLeft, $limitpos - $pos);
+ $retstr .= substr($recordData, $pos, $len);
+ $charsLeft -= $len;
+ $isCompressed = true;
+ } elseif (!$isCompressed && ($option != 0)) {
+ // 1st fragment uncompressed
+ // this fragment uncompressed
+ $len = min($charsLeft * 2, $limitpos - $pos);
+ $retstr .= substr($recordData, $pos, $len);
+ $charsLeft -= $len / 2;
+ $isCompressed = false;
+ } elseif (!$isCompressed && ($option == 0)) {
+ // 1st fragment uncompressed
+ // this fragment compressed
+ $len = min($charsLeft, $limitpos - $pos);
+ for ($j = 0; $j < $len; ++$j) {
+ $retstr .= $recordData[$pos + $j]
+ . chr(0);
+ }
+ $charsLeft -= $len;
+ $isCompressed = false;
+ } else {
+ // 1st fragment compressed
+ // this fragment uncompressed
+ $newstr = '';
+ $jMax = strlen($retstr);
+ for ($j = 0; $j < $jMax; ++$j) {
+ $newstr .= $retstr[$j] . chr(0);
+ }
+ $retstr = $newstr;
+ $len = min($charsLeft * 2, $limitpos - $pos);
+ $retstr .= substr($recordData, $pos, $len);
+ $charsLeft -= $len / 2;
+ $isCompressed = false;
+ }
+
+ $pos += $len;
+ }
+ }
+
+ // convert to UTF-8
+ $retstr = self::encodeUTF16($retstr, $isCompressed);
+
+ // read additional Rich-Text information, if any
+ $fmtRuns = [];
+ if ($hasRichText) {
+ // list of formatting runs
+ for ($j = 0; $j < $formattingRuns; ++$j) {
+ // first formatted character; zero-based
+ $charPos = self::getUInt2d($recordData, $pos + $j * 4);
+
+ // index to font record
+ $fontIndex = self::getUInt2d($recordData, $pos + 2 + $j * 4);
+
+ $fmtRuns[] = [
+ 'charPos' => $charPos,
+ 'fontIndex' => $fontIndex,
+ ];
+ }
+ $pos += 4 * $formattingRuns;
+ }
+
+ // read additional Asian phonetics information, if any
+ if ($hasAsian) {
+ // For Asian phonetic settings, we skip the extended string data
+ $pos += $extendedRunLength;
+ }
+
+ // store the shared sting
+ $this->sst[] = [
+ 'value' => $retstr,
+ 'fmtRuns' => $fmtRuns,
+ ];
+ }
+
+ // getSplicedRecordData() takes care of moving current position in data stream
+ }
+
+ /**
+ * Read PRINTGRIDLINES record.
+ */
+ private function readPrintGridlines()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->version == self::XLS_BIFF8 && !$this->readDataOnly) {
+ // offset: 0; size: 2; 0 = do not print sheet grid lines; 1 = print sheet gridlines
+ $printGridlines = (bool) self::getUInt2d($recordData, 0);
+ $this->phpSheet->setPrintGridlines($printGridlines);
+ }
+ }
+
+ /**
+ * Read DEFAULTROWHEIGHT record.
+ */
+ private function readDefaultRowHeight()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; option flags
+ // offset: 2; size: 2; default height for unused rows, (twips 1/20 point)
+ $height = self::getUInt2d($recordData, 2);
+ $this->phpSheet->getDefaultRowDimension()->setRowHeight($height / 20);
+ }
+
+ /**
+ * Read SHEETPR record.
+ */
+ private function readSheetPr()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2
+
+ // bit: 6; mask: 0x0040; 0 = outline buttons above outline group
+ $isSummaryBelow = (0x0040 & self::getUInt2d($recordData, 0)) >> 6;
+ $this->phpSheet->setShowSummaryBelow($isSummaryBelow);
+
+ // bit: 7; mask: 0x0080; 0 = outline buttons left of outline group
+ $isSummaryRight = (0x0080 & self::getUInt2d($recordData, 0)) >> 7;
+ $this->phpSheet->setShowSummaryRight($isSummaryRight);
+
+ // bit: 8; mask: 0x100; 0 = scale printout in percent, 1 = fit printout to number of pages
+ // this corresponds to radio button setting in page setup dialog in Excel
+ $this->isFitToPages = (bool) ((0x0100 & self::getUInt2d($recordData, 0)) >> 8);
+ }
+
+ /**
+ * Read HORIZONTALPAGEBREAKS record.
+ */
+ private function readHorizontalPageBreaks()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->version == self::XLS_BIFF8 && !$this->readDataOnly) {
+ // offset: 0; size: 2; number of the following row index structures
+ $nm = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 6 * $nm; list of $nm row index structures
+ for ($i = 0; $i < $nm; ++$i) {
+ $r = self::getUInt2d($recordData, 2 + 6 * $i);
+ $cf = self::getUInt2d($recordData, 2 + 6 * $i + 2);
+ $cl = self::getUInt2d($recordData, 2 + 6 * $i + 4);
+
+ // not sure why two column indexes are necessary?
+ $this->phpSheet->setBreakByColumnAndRow($cf + 1, $r, Worksheet::BREAK_ROW);
+ }
+ }
+ }
+
+ /**
+ * Read VERTICALPAGEBREAKS record.
+ */
+ private function readVerticalPageBreaks()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->version == self::XLS_BIFF8 && !$this->readDataOnly) {
+ // offset: 0; size: 2; number of the following column index structures
+ $nm = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 6 * $nm; list of $nm row index structures
+ for ($i = 0; $i < $nm; ++$i) {
+ $c = self::getUInt2d($recordData, 2 + 6 * $i);
+ $rf = self::getUInt2d($recordData, 2 + 6 * $i + 2);
+ $rl = self::getUInt2d($recordData, 2 + 6 * $i + 4);
+
+ // not sure why two row indexes are necessary?
+ $this->phpSheet->setBreakByColumnAndRow($c + 1, $rf, Worksheet::BREAK_COLUMN);
+ }
+ }
+ }
+
+ /**
+ * Read HEADER record.
+ */
+ private function readHeader()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: var
+ // realized that $recordData can be empty even when record exists
+ if ($recordData) {
+ if ($this->version == self::XLS_BIFF8) {
+ $string = self::readUnicodeStringLong($recordData);
+ } else {
+ $string = $this->readByteStringShort($recordData);
+ }
+
+ $this->phpSheet->getHeaderFooter()->setOddHeader($string['value']);
+ $this->phpSheet->getHeaderFooter()->setEvenHeader($string['value']);
+ }
+ }
+ }
+
+ /**
+ * Read FOOTER record.
+ */
+ private function readFooter()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: var
+ // realized that $recordData can be empty even when record exists
+ if ($recordData) {
+ if ($this->version == self::XLS_BIFF8) {
+ $string = self::readUnicodeStringLong($recordData);
+ } else {
+ $string = $this->readByteStringShort($recordData);
+ }
+ $this->phpSheet->getHeaderFooter()->setOddFooter($string['value']);
+ $this->phpSheet->getHeaderFooter()->setEvenFooter($string['value']);
+ }
+ }
+ }
+
+ /**
+ * Read HCENTER record.
+ */
+ private function readHcenter()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; 0 = print sheet left aligned, 1 = print sheet centered horizontally
+ $isHorizontalCentered = (bool) self::getUInt2d($recordData, 0);
+
+ $this->phpSheet->getPageSetup()->setHorizontalCentered($isHorizontalCentered);
+ }
+ }
+
+ /**
+ * Read VCENTER record.
+ */
+ private function readVcenter()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; 0 = print sheet aligned at top page border, 1 = print sheet vertically centered
+ $isVerticalCentered = (bool) self::getUInt2d($recordData, 0);
+
+ $this->phpSheet->getPageSetup()->setVerticalCentered($isVerticalCentered);
+ }
+ }
+
+ /**
+ * Read LEFTMARGIN record.
+ */
+ private function readLeftMargin()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 8
+ $this->phpSheet->getPageMargins()->setLeft(self::extractNumber($recordData));
+ }
+ }
+
+ /**
+ * Read RIGHTMARGIN record.
+ */
+ private function readRightMargin()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 8
+ $this->phpSheet->getPageMargins()->setRight(self::extractNumber($recordData));
+ }
+ }
+
+ /**
+ * Read TOPMARGIN record.
+ */
+ private function readTopMargin()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 8
+ $this->phpSheet->getPageMargins()->setTop(self::extractNumber($recordData));
+ }
+ }
+
+ /**
+ * Read BOTTOMMARGIN record.
+ */
+ private function readBottomMargin()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 8
+ $this->phpSheet->getPageMargins()->setBottom(self::extractNumber($recordData));
+ }
+ }
+
+ /**
+ * Read PAGESETUP record.
+ */
+ private function readPageSetup()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; paper size
+ $paperSize = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; scaling factor
+ $scale = self::getUInt2d($recordData, 2);
+
+ // offset: 6; size: 2; fit worksheet width to this number of pages, 0 = use as many as needed
+ $fitToWidth = self::getUInt2d($recordData, 6);
+
+ // offset: 8; size: 2; fit worksheet height to this number of pages, 0 = use as many as needed
+ $fitToHeight = self::getUInt2d($recordData, 8);
+
+ // offset: 10; size: 2; option flags
+
+ // bit: 1; mask: 0x0002; 0=landscape, 1=portrait
+ $isPortrait = (0x0002 & self::getUInt2d($recordData, 10)) >> 1;
+
+ // bit: 2; mask: 0x0004; 1= paper size, scaling factor, paper orient. not init
+ // when this bit is set, do not use flags for those properties
+ $isNotInit = (0x0004 & self::getUInt2d($recordData, 10)) >> 2;
+
+ if (!$isNotInit) {
+ $this->phpSheet->getPageSetup()->setPaperSize($paperSize);
+ switch ($isPortrait) {
+ case 0:
+ $this->phpSheet->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
+
+ break;
+ case 1:
+ $this->phpSheet->getPageSetup()->setOrientation(PageSetup::ORIENTATION_PORTRAIT);
+
+ break;
+ }
+
+ $this->phpSheet->getPageSetup()->setScale($scale, false);
+ $this->phpSheet->getPageSetup()->setFitToPage((bool) $this->isFitToPages);
+ $this->phpSheet->getPageSetup()->setFitToWidth($fitToWidth, false);
+ $this->phpSheet->getPageSetup()->setFitToHeight($fitToHeight, false);
+ }
+
+ // offset: 16; size: 8; header margin (IEEE 754 floating-point value)
+ $marginHeader = self::extractNumber(substr($recordData, 16, 8));
+ $this->phpSheet->getPageMargins()->setHeader($marginHeader);
+
+ // offset: 24; size: 8; footer margin (IEEE 754 floating-point value)
+ $marginFooter = self::extractNumber(substr($recordData, 24, 8));
+ $this->phpSheet->getPageMargins()->setFooter($marginFooter);
+ }
+ }
+
+ /**
+ * PROTECT - Sheet protection (BIFF2 through BIFF8)
+ * if this record is omitted, then it also means no sheet protection.
+ */
+ private function readProtect()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->readDataOnly) {
+ return;
+ }
+
+ // offset: 0; size: 2;
+
+ // bit 0, mask 0x01; 1 = sheet is protected
+ $bool = (0x01 & self::getUInt2d($recordData, 0)) >> 0;
+ $this->phpSheet->getProtection()->setSheet((bool) $bool);
+ }
+
+ /**
+ * SCENPROTECT.
+ */
+ private function readScenProtect()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->readDataOnly) {
+ return;
+ }
+
+ // offset: 0; size: 2;
+
+ // bit: 0, mask 0x01; 1 = scenarios are protected
+ $bool = (0x01 & self::getUInt2d($recordData, 0)) >> 0;
+
+ $this->phpSheet->getProtection()->setScenarios((bool) $bool);
+ }
+
+ /**
+ * OBJECTPROTECT.
+ */
+ private function readObjectProtect()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->readDataOnly) {
+ return;
+ }
+
+ // offset: 0; size: 2;
+
+ // bit: 0, mask 0x01; 1 = objects are protected
+ $bool = (0x01 & self::getUInt2d($recordData, 0)) >> 0;
+
+ $this->phpSheet->getProtection()->setObjects((bool) $bool);
+ }
+
+ /**
+ * PASSWORD - Sheet protection (hashed) password (BIFF2 through BIFF8).
+ */
+ private function readPassword()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; 16-bit hash value of password
+ $password = strtoupper(dechex(self::getUInt2d($recordData, 0))); // the hashed password
+ $this->phpSheet->getProtection()->setPassword($password, true);
+ }
+ }
+
+ /**
+ * Read DEFCOLWIDTH record.
+ */
+ private function readDefColWidth()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; default column width
+ $width = self::getUInt2d($recordData, 0);
+ if ($width != 8) {
+ $this->phpSheet->getDefaultColumnDimension()->setWidth($width);
+ }
+ }
+
+ /**
+ * Read COLINFO record.
+ */
+ private function readColInfo()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; index to first column in range
+ $firstColumnIndex = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; index to last column in range
+ $lastColumnIndex = self::getUInt2d($recordData, 2);
+
+ // offset: 4; size: 2; width of the column in 1/256 of the width of the zero character
+ $width = self::getUInt2d($recordData, 4);
+
+ // offset: 6; size: 2; index to XF record for default column formatting
+ $xfIndex = self::getUInt2d($recordData, 6);
+
+ // offset: 8; size: 2; option flags
+ // bit: 0; mask: 0x0001; 1= columns are hidden
+ $isHidden = (0x0001 & self::getUInt2d($recordData, 8)) >> 0;
+
+ // bit: 10-8; mask: 0x0700; outline level of the columns (0 = no outline)
+ $level = (0x0700 & self::getUInt2d($recordData, 8)) >> 8;
+
+ // bit: 12; mask: 0x1000; 1 = collapsed
+ $isCollapsed = (0x1000 & self::getUInt2d($recordData, 8)) >> 12;
+
+ // offset: 10; size: 2; not used
+
+ for ($i = $firstColumnIndex + 1; $i <= $lastColumnIndex + 1; ++$i) {
+ if ($lastColumnIndex == 255 || $lastColumnIndex == 256) {
+ $this->phpSheet->getDefaultColumnDimension()->setWidth($width / 256);
+
+ break;
+ }
+ $this->phpSheet->getColumnDimensionByColumn($i)->setWidth($width / 256);
+ $this->phpSheet->getColumnDimensionByColumn($i)->setVisible(!$isHidden);
+ $this->phpSheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
+ $this->phpSheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
+ $this->phpSheet->getColumnDimensionByColumn($i)->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+ }
+ }
+
+ /**
+ * ROW.
+ *
+ * This record contains the properties of a single row in a
+ * sheet. Rows and cells in a sheet are divided into blocks
+ * of 32 rows.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readRow()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; index of this row
+ $r = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; index to column of the first cell which is described by a cell record
+
+ // offset: 4; size: 2; index to column of the last cell which is described by a cell record, increased by 1
+
+ // offset: 6; size: 2;
+
+ // bit: 14-0; mask: 0x7FFF; height of the row, in twips = 1/20 of a point
+ $height = (0x7FFF & self::getUInt2d($recordData, 6)) >> 0;
+
+ // bit: 15: mask: 0x8000; 0 = row has custom height; 1= row has default height
+ $useDefaultHeight = (0x8000 & self::getUInt2d($recordData, 6)) >> 15;
+
+ if (!$useDefaultHeight) {
+ $this->phpSheet->getRowDimension($r + 1)->setRowHeight($height / 20);
+ }
+
+ // offset: 8; size: 2; not used
+
+ // offset: 10; size: 2; not used in BIFF5-BIFF8
+
+ // offset: 12; size: 4; option flags and default row formatting
+
+ // bit: 2-0: mask: 0x00000007; outline level of the row
+ $level = (0x00000007 & self::getInt4d($recordData, 12)) >> 0;
+ $this->phpSheet->getRowDimension($r + 1)->setOutlineLevel($level);
+
+ // bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
+ $isCollapsed = (0x00000010 & self::getInt4d($recordData, 12)) >> 4;
+ $this->phpSheet->getRowDimension($r + 1)->setCollapsed($isCollapsed);
+
+ // bit: 5; mask: 0x00000020; 1 = row is hidden
+ $isHidden = (0x00000020 & self::getInt4d($recordData, 12)) >> 5;
+ $this->phpSheet->getRowDimension($r + 1)->setVisible(!$isHidden);
+
+ // bit: 7; mask: 0x00000080; 1 = row has explicit format
+ $hasExplicitFormat = (0x00000080 & self::getInt4d($recordData, 12)) >> 7;
+
+ // bit: 27-16; mask: 0x0FFF0000; only applies when hasExplicitFormat = 1; index to XF record
+ $xfIndex = (0x0FFF0000 & self::getInt4d($recordData, 12)) >> 16;
+
+ if ($hasExplicitFormat && isset($this->mapCellXfIndex[$xfIndex])) {
+ $this->phpSheet->getRowDimension($r + 1)->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+ }
+ }
+
+ /**
+ * Read RK record
+ * This record represents a cell that contains an RK value
+ * (encoded integer or floating-point value). If a
+ * floating-point value cannot be encoded to an RK value,
+ * a NUMBER record will be written. This record replaces the
+ * record INTEGER written in BIFF2.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readRk()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; index to row
+ $row = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; index to column
+ $column = self::getUInt2d($recordData, 2);
+ $columnString = Coordinate::stringFromColumnIndex($column + 1);
+
+ // Read cell?
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
+ // offset: 4; size: 2; index to XF record
+ $xfIndex = self::getUInt2d($recordData, 4);
+
+ // offset: 6; size: 4; RK value
+ $rknum = self::getInt4d($recordData, 6);
+ $numValue = self::getIEEE754($rknum);
+
+ $cell = $this->phpSheet->getCell($columnString . ($row + 1));
+ if (!$this->readDataOnly) {
+ // add style information
+ $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+
+ // add cell
+ $cell->setValueExplicit($numValue, DataType::TYPE_NUMERIC);
+ }
+ }
+
+ /**
+ * Read LABELSST record
+ * This record represents a cell that contains a string. It
+ * replaces the LABEL record and RSTRING record used in
+ * BIFF2-BIFF5.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readLabelSst()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; index to row
+ $row = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; index to column
+ $column = self::getUInt2d($recordData, 2);
+ $columnString = Coordinate::stringFromColumnIndex($column + 1);
+
+ $emptyCell = true;
+ // Read cell?
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
+ // offset: 4; size: 2; index to XF record
+ $xfIndex = self::getUInt2d($recordData, 4);
+
+ // offset: 6; size: 4; index to SST record
+ $index = self::getInt4d($recordData, 6);
+
+ // add cell
+ if (($fmtRuns = $this->sst[$index]['fmtRuns']) && !$this->readDataOnly) {
+ // then we should treat as rich text
+ $richText = new RichText();
+ $charPos = 0;
+ $sstCount = count($this->sst[$index]['fmtRuns']);
+ for ($i = 0; $i <= $sstCount; ++$i) {
+ if (isset($fmtRuns[$i])) {
+ $text = StringHelper::substring($this->sst[$index]['value'], $charPos, $fmtRuns[$i]['charPos'] - $charPos);
+ $charPos = $fmtRuns[$i]['charPos'];
+ } else {
+ $text = StringHelper::substring($this->sst[$index]['value'], $charPos, StringHelper::countCharacters($this->sst[$index]['value']));
+ }
+
+ if (StringHelper::countCharacters($text) > 0) {
+ if ($i == 0) { // first text run, no style
+ $richText->createText($text);
+ } else {
+ $textRun = $richText->createTextRun($text);
+ if (isset($fmtRuns[$i - 1])) {
+ if ($fmtRuns[$i - 1]['fontIndex'] < 4) {
+ $fontIndex = $fmtRuns[$i - 1]['fontIndex'];
+ } else {
+ // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
+ // check the OpenOffice documentation of the FONT record
+ $fontIndex = $fmtRuns[$i - 1]['fontIndex'] - 1;
+ }
+ $textRun->setFont(clone $this->objFonts[$fontIndex]);
+ }
+ }
+ }
+ }
+ if ($this->readEmptyCells || trim($richText->getPlainText()) !== '') {
+ $cell = $this->phpSheet->getCell($columnString . ($row + 1));
+ $cell->setValueExplicit($richText, DataType::TYPE_STRING);
+ $emptyCell = false;
+ }
+ } else {
+ if ($this->readEmptyCells || trim($this->sst[$index]['value']) !== '') {
+ $cell = $this->phpSheet->getCell($columnString . ($row + 1));
+ $cell->setValueExplicit($this->sst[$index]['value'], DataType::TYPE_STRING);
+ $emptyCell = false;
+ }
+ }
+
+ if (!$this->readDataOnly && !$emptyCell) {
+ // add style information
+ $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+ }
+ }
+
+ /**
+ * Read MULRK record
+ * This record represents a cell range containing RK value
+ * cells. All cells are located in the same row.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readMulRk()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; index to row
+ $row = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; index to first column
+ $colFirst = self::getUInt2d($recordData, 2);
+
+ // offset: var; size: 2; index to last column
+ $colLast = self::getUInt2d($recordData, $length - 2);
+ $columns = $colLast - $colFirst + 1;
+
+ // offset within record data
+ $offset = 4;
+
+ for ($i = 1; $i <= $columns; ++$i) {
+ $columnString = Coordinate::stringFromColumnIndex($colFirst + $i);
+
+ // Read cell?
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
+ // offset: var; size: 2; index to XF record
+ $xfIndex = self::getUInt2d($recordData, $offset);
+
+ // offset: var; size: 4; RK value
+ $numValue = self::getIEEE754(self::getInt4d($recordData, $offset + 2));
+ $cell = $this->phpSheet->getCell($columnString . ($row + 1));
+ if (!$this->readDataOnly) {
+ // add style
+ $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+
+ // add cell value
+ $cell->setValueExplicit($numValue, DataType::TYPE_NUMERIC);
+ }
+
+ $offset += 6;
+ }
+ }
+
+ /**
+ * Read NUMBER record
+ * This record represents a cell that contains a
+ * floating-point value.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readNumber()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; index to row
+ $row = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size 2; index to column
+ $column = self::getUInt2d($recordData, 2);
+ $columnString = Coordinate::stringFromColumnIndex($column + 1);
+
+ // Read cell?
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
+ // offset 4; size: 2; index to XF record
+ $xfIndex = self::getUInt2d($recordData, 4);
+
+ $numValue = self::extractNumber(substr($recordData, 6, 8));
+
+ $cell = $this->phpSheet->getCell($columnString . ($row + 1));
+ if (!$this->readDataOnly) {
+ // add cell style
+ $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+
+ // add cell value
+ $cell->setValueExplicit($numValue, DataType::TYPE_NUMERIC);
+ }
+ }
+
+ /**
+ * Read FORMULA record + perhaps a following STRING record if formula result is a string
+ * This record contains the token array and the result of a
+ * formula cell.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readFormula()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; row index
+ $row = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; col index
+ $column = self::getUInt2d($recordData, 2);
+ $columnString = Coordinate::stringFromColumnIndex($column + 1);
+
+ // offset: 20: size: variable; formula structure
+ $formulaStructure = substr($recordData, 20);
+
+ // offset: 14: size: 2; option flags, recalculate always, recalculate on open etc.
+ $options = self::getUInt2d($recordData, 14);
+
+ // bit: 0; mask: 0x0001; 1 = recalculate always
+ // bit: 1; mask: 0x0002; 1 = calculate on open
+ // bit: 2; mask: 0x0008; 1 = part of a shared formula
+ $isPartOfSharedFormula = (bool) (0x0008 & $options);
+
+ // WARNING:
+ // We can apparently not rely on $isPartOfSharedFormula. Even when $isPartOfSharedFormula = true
+ // the formula data may be ordinary formula data, therefore we need to check
+ // explicitly for the tExp token (0x01)
+ $isPartOfSharedFormula = $isPartOfSharedFormula && ord($formulaStructure[2]) == 0x01;
+
+ if ($isPartOfSharedFormula) {
+ // part of shared formula which means there will be a formula with a tExp token and nothing else
+ // get the base cell, grab tExp token
+ $baseRow = self::getUInt2d($formulaStructure, 3);
+ $baseCol = self::getUInt2d($formulaStructure, 5);
+ $this->baseCell = Coordinate::stringFromColumnIndex($baseCol + 1) . ($baseRow + 1);
+ }
+
+ // Read cell?
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
+ if ($isPartOfSharedFormula) {
+ // formula is added to this cell after the sheet has been read
+ $this->sharedFormulaParts[$columnString . ($row + 1)] = $this->baseCell;
+ }
+
+ // offset: 16: size: 4; not used
+
+ // offset: 4; size: 2; XF index
+ $xfIndex = self::getUInt2d($recordData, 4);
+
+ // offset: 6; size: 8; result of the formula
+ if ((ord($recordData[6]) == 0) && (ord($recordData[12]) == 255) && (ord($recordData[13]) == 255)) {
+ // String formula. Result follows in appended STRING record
+ $dataType = DataType::TYPE_STRING;
+
+ // read possible SHAREDFMLA record
+ $code = self::getUInt2d($this->data, $this->pos);
+ if ($code == self::XLS_TYPE_SHAREDFMLA) {
+ $this->readSharedFmla();
+ }
+
+ // read STRING record
+ $value = $this->readString();
+ } elseif ((ord($recordData[6]) == 1)
+ && (ord($recordData[12]) == 255)
+ && (ord($recordData[13]) == 255)) {
+ // Boolean formula. Result is in +2; 0=false, 1=true
+ $dataType = DataType::TYPE_BOOL;
+ $value = (bool) ord($recordData[8]);
+ } elseif ((ord($recordData[6]) == 2)
+ && (ord($recordData[12]) == 255)
+ && (ord($recordData[13]) == 255)) {
+ // Error formula. Error code is in +2
+ $dataType = DataType::TYPE_ERROR;
+ $value = Xls\ErrorCode::lookup(ord($recordData[8]));
+ } elseif ((ord($recordData[6]) == 3)
+ && (ord($recordData[12]) == 255)
+ && (ord($recordData[13]) == 255)) {
+ // Formula result is a null string
+ $dataType = DataType::TYPE_NULL;
+ $value = '';
+ } else {
+ // forumla result is a number, first 14 bytes like _NUMBER record
+ $dataType = DataType::TYPE_NUMERIC;
+ $value = self::extractNumber(substr($recordData, 6, 8));
+ }
+
+ $cell = $this->phpSheet->getCell($columnString . ($row + 1));
+ if (!$this->readDataOnly) {
+ // add cell style
+ $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+
+ // store the formula
+ if (!$isPartOfSharedFormula) {
+ // not part of shared formula
+ // add cell value. If we can read formula, populate with formula, otherwise just used cached value
+ try {
+ if ($this->version != self::XLS_BIFF8) {
+ throw new Exception('Not BIFF8. Can only read BIFF8 formulas');
+ }
+ $formula = $this->getFormulaFromStructure($formulaStructure); // get formula in human language
+ $cell->setValueExplicit('=' . $formula, DataType::TYPE_FORMULA);
+ } catch (PhpSpreadsheetException $e) {
+ $cell->setValueExplicit($value, $dataType);
+ }
+ } else {
+ if ($this->version == self::XLS_BIFF8) {
+ // do nothing at this point, formula id added later in the code
+ } else {
+ $cell->setValueExplicit($value, $dataType);
+ }
+ }
+
+ // store the cached calculated value
+ $cell->setCalculatedValue($value);
+ }
+ }
+
+ /**
+ * Read a SHAREDFMLA record. This function just stores the binary shared formula in the reader,
+ * which usually contains relative references.
+ * These will be used to construct the formula in each shared formula part after the sheet is read.
+ */
+ private function readSharedFmla()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0, size: 6; cell range address of the area used by the shared formula, not used for anything
+ $cellRange = substr($recordData, 0, 6);
+ $cellRange = $this->readBIFF5CellRangeAddressFixed($cellRange); // note: even BIFF8 uses BIFF5 syntax
+
+ // offset: 6, size: 1; not used
+
+ // offset: 7, size: 1; number of existing FORMULA records for this shared formula
+ $no = ord($recordData[7]);
+
+ // offset: 8, size: var; Binary token array of the shared formula
+ $formula = substr($recordData, 8);
+
+ // at this point we only store the shared formula for later use
+ $this->sharedFormulas[$this->baseCell] = $formula;
+ }
+
+ /**
+ * Read a STRING record from current stream position and advance the stream pointer to next record
+ * This record is used for storing result from FORMULA record when it is a string, and
+ * it occurs directly after the FORMULA record.
+ *
+ * @return string The string contents as UTF-8
+ */
+ private function readString()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->version == self::XLS_BIFF8) {
+ $string = self::readUnicodeStringLong($recordData);
+ $value = $string['value'];
+ } else {
+ $string = $this->readByteStringLong($recordData);
+ $value = $string['value'];
+ }
+
+ return $value;
+ }
+
+ /**
+ * Read BOOLERR record
+ * This record represents a Boolean value or error value
+ * cell.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readBoolErr()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; row index
+ $row = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; column index
+ $column = self::getUInt2d($recordData, 2);
+ $columnString = Coordinate::stringFromColumnIndex($column + 1);
+
+ // Read cell?
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
+ // offset: 4; size: 2; index to XF record
+ $xfIndex = self::getUInt2d($recordData, 4);
+
+ // offset: 6; size: 1; the boolean value or error value
+ $boolErr = ord($recordData[6]);
+
+ // offset: 7; size: 1; 0=boolean; 1=error
+ $isError = ord($recordData[7]);
+
+ $cell = $this->phpSheet->getCell($columnString . ($row + 1));
+ switch ($isError) {
+ case 0: // boolean
+ $value = (bool) $boolErr;
+
+ // add cell value
+ $cell->setValueExplicit($value, DataType::TYPE_BOOL);
+
+ break;
+ case 1: // error type
+ $value = Xls\ErrorCode::lookup($boolErr);
+
+ // add cell value
+ $cell->setValueExplicit($value, DataType::TYPE_ERROR);
+
+ break;
+ }
+
+ if (!$this->readDataOnly) {
+ // add cell style
+ $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+ }
+ }
+
+ /**
+ * Read MULBLANK record
+ * This record represents a cell range of empty cells. All
+ * cells are located in the same row.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readMulBlank()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; index to row
+ $row = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; index to first column
+ $fc = self::getUInt2d($recordData, 2);
+
+ // offset: 4; size: 2 x nc; list of indexes to XF records
+ // add style information
+ if (!$this->readDataOnly && $this->readEmptyCells) {
+ for ($i = 0; $i < $length / 2 - 3; ++$i) {
+ $columnString = Coordinate::stringFromColumnIndex($fc + $i + 1);
+
+ // Read cell?
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
+ $xfIndex = self::getUInt2d($recordData, 4 + 2 * $i);
+ $this->phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+ }
+ }
+
+ // offset: 6; size 2; index to last column (not needed)
+ }
+
+ /**
+ * Read LABEL record
+ * This record represents a cell that contains a string. In
+ * BIFF8 it is usually replaced by the LABELSST record.
+ * Excel still uses this record, if it copies unformatted
+ * text cells to the clipboard.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readLabel()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; index to row
+ $row = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; index to column
+ $column = self::getUInt2d($recordData, 2);
+ $columnString = Coordinate::stringFromColumnIndex($column + 1);
+
+ // Read cell?
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
+ // offset: 4; size: 2; XF index
+ $xfIndex = self::getUInt2d($recordData, 4);
+
+ // add cell value
+ // todo: what if string is very long? continue record
+ if ($this->version == self::XLS_BIFF8) {
+ $string = self::readUnicodeStringLong(substr($recordData, 6));
+ $value = $string['value'];
+ } else {
+ $string = $this->readByteStringLong(substr($recordData, 6));
+ $value = $string['value'];
+ }
+ if ($this->readEmptyCells || trim($value) !== '') {
+ $cell = $this->phpSheet->getCell($columnString . ($row + 1));
+ $cell->setValueExplicit($value, DataType::TYPE_STRING);
+
+ if (!$this->readDataOnly) {
+ // add cell style
+ $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+ }
+ }
+ }
+
+ /**
+ * Read BLANK record.
+ */
+ private function readBlank()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; row index
+ $row = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; col index
+ $col = self::getUInt2d($recordData, 2);
+ $columnString = Coordinate::stringFromColumnIndex($col + 1);
+
+ // Read cell?
+ if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
+ // offset: 4; size: 2; XF index
+ $xfIndex = self::getUInt2d($recordData, 4);
+
+ // add style information
+ if (!$this->readDataOnly && $this->readEmptyCells) {
+ $this->phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->mapCellXfIndex[$xfIndex]);
+ }
+ }
+ }
+
+ /**
+ * Read MSODRAWING record.
+ */
+ private function readMsoDrawing()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+
+ // get spliced record data
+ $splicedRecordData = $this->getSplicedRecordData();
+ $recordData = $splicedRecordData['recordData'];
+
+ $this->drawingData .= $recordData;
+ }
+
+ /**
+ * Read OBJ record.
+ */
+ private function readObj()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->readDataOnly || $this->version != self::XLS_BIFF8) {
+ return;
+ }
+
+ // recordData consists of an array of subrecords looking like this:
+ // ft: 2 bytes; ftCmo type (0x15)
+ // cb: 2 bytes; size in bytes of ftCmo data
+ // ot: 2 bytes; Object Type
+ // id: 2 bytes; Object id number
+ // grbit: 2 bytes; Option Flags
+ // data: var; subrecord data
+
+ // for now, we are just interested in the second subrecord containing the object type
+ $ftCmoType = self::getUInt2d($recordData, 0);
+ $cbCmoSize = self::getUInt2d($recordData, 2);
+ $otObjType = self::getUInt2d($recordData, 4);
+ $idObjID = self::getUInt2d($recordData, 6);
+ $grbitOpts = self::getUInt2d($recordData, 6);
+
+ $this->objs[] = [
+ 'ftCmoType' => $ftCmoType,
+ 'cbCmoSize' => $cbCmoSize,
+ 'otObjType' => $otObjType,
+ 'idObjID' => $idObjID,
+ 'grbitOpts' => $grbitOpts,
+ ];
+ $this->textObjRef = $idObjID;
+ }
+
+ /**
+ * Read WINDOW2 record.
+ */
+ private function readWindow2()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; option flags
+ $options = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; index to first visible row
+ $firstVisibleRow = self::getUInt2d($recordData, 2);
+
+ // offset: 4; size: 2; index to first visible colum
+ $firstVisibleColumn = self::getUInt2d($recordData, 4);
+ if ($this->version === self::XLS_BIFF8) {
+ // offset: 8; size: 2; not used
+ // offset: 10; size: 2; cached magnification factor in page break preview (in percent); 0 = Default (60%)
+ // offset: 12; size: 2; cached magnification factor in normal view (in percent); 0 = Default (100%)
+ // offset: 14; size: 4; not used
+ $zoomscaleInPageBreakPreview = self::getUInt2d($recordData, 10);
+ if ($zoomscaleInPageBreakPreview === 0) {
+ $zoomscaleInPageBreakPreview = 60;
+ }
+ $zoomscaleInNormalView = self::getUInt2d($recordData, 12);
+ if ($zoomscaleInNormalView === 0) {
+ $zoomscaleInNormalView = 100;
+ }
+ }
+
+ // bit: 1; mask: 0x0002; 0 = do not show gridlines, 1 = show gridlines
+ $showGridlines = (bool) ((0x0002 & $options) >> 1);
+ $this->phpSheet->setShowGridlines($showGridlines);
+
+ // bit: 2; mask: 0x0004; 0 = do not show headers, 1 = show headers
+ $showRowColHeaders = (bool) ((0x0004 & $options) >> 2);
+ $this->phpSheet->setShowRowColHeaders($showRowColHeaders);
+
+ // bit: 3; mask: 0x0008; 0 = panes are not frozen, 1 = panes are frozen
+ $this->frozen = (bool) ((0x0008 & $options) >> 3);
+
+ // bit: 6; mask: 0x0040; 0 = columns from left to right, 1 = columns from right to left
+ $this->phpSheet->setRightToLeft((bool) ((0x0040 & $options) >> 6));
+
+ // bit: 10; mask: 0x0400; 0 = sheet not active, 1 = sheet active
+ $isActive = (bool) ((0x0400 & $options) >> 10);
+ if ($isActive) {
+ $this->spreadsheet->setActiveSheetIndex($this->spreadsheet->getIndex($this->phpSheet));
+ }
+
+ // bit: 11; mask: 0x0800; 0 = normal view, 1 = page break view
+ $isPageBreakPreview = (bool) ((0x0800 & $options) >> 11);
+
+ //FIXME: set $firstVisibleRow and $firstVisibleColumn
+
+ if ($this->phpSheet->getSheetView()->getView() !== SheetView::SHEETVIEW_PAGE_LAYOUT) {
+ //NOTE: this setting is inferior to page layout view(Excel2007-)
+ $view = $isPageBreakPreview ? SheetView::SHEETVIEW_PAGE_BREAK_PREVIEW : SheetView::SHEETVIEW_NORMAL;
+ $this->phpSheet->getSheetView()->setView($view);
+ if ($this->version === self::XLS_BIFF8) {
+ $zoomScale = $isPageBreakPreview ? $zoomscaleInPageBreakPreview : $zoomscaleInNormalView;
+ $this->phpSheet->getSheetView()->setZoomScale($zoomScale);
+ $this->phpSheet->getSheetView()->setZoomScaleNormal($zoomscaleInNormalView);
+ }
+ }
+ }
+
+ /**
+ * Read PLV Record(Created by Excel2007 or upper).
+ */
+ private function readPageLayoutView()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; rt
+ //->ignore
+ $rt = self::getUInt2d($recordData, 0);
+ // offset: 2; size: 2; grbitfr
+ //->ignore
+ $grbitFrt = self::getUInt2d($recordData, 2);
+ // offset: 4; size: 8; reserved
+ //->ignore
+
+ // offset: 12; size 2; zoom scale
+ $wScalePLV = self::getUInt2d($recordData, 12);
+ // offset: 14; size 2; grbit
+ $grbit = self::getUInt2d($recordData, 14);
+
+ // decomprise grbit
+ $fPageLayoutView = $grbit & 0x01;
+ $fRulerVisible = ($grbit >> 1) & 0x01; //no support
+ $fWhitespaceHidden = ($grbit >> 3) & 0x01; //no support
+
+ if ($fPageLayoutView === 1) {
+ $this->phpSheet->getSheetView()->setView(SheetView::SHEETVIEW_PAGE_LAYOUT);
+ $this->phpSheet->getSheetView()->setZoomScale($wScalePLV); //set by Excel2007 only if SHEETVIEW_PAGE_LAYOUT
+ }
+ //otherwise, we cannot know whether SHEETVIEW_PAGE_LAYOUT or SHEETVIEW_PAGE_BREAK_PREVIEW.
+ }
+
+ /**
+ * Read SCL record.
+ */
+ private function readScl()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // offset: 0; size: 2; numerator of the view magnification
+ $numerator = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; numerator of the view magnification
+ $denumerator = self::getUInt2d($recordData, 2);
+
+ // set the zoom scale (in percent)
+ $this->phpSheet->getSheetView()->setZoomScale($numerator * 100 / $denumerator);
+ }
+
+ /**
+ * Read PANE record.
+ */
+ private function readPane()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; position of vertical split
+ $px = self::getUInt2d($recordData, 0);
+
+ // offset: 2; size: 2; position of horizontal split
+ $py = self::getUInt2d($recordData, 2);
+
+ // offset: 4; size: 2; top most visible row in the bottom pane
+ $rwTop = self::getUInt2d($recordData, 4);
+
+ // offset: 6; size: 2; first visible left column in the right pane
+ $colLeft = self::getUInt2d($recordData, 6);
+
+ if ($this->frozen) {
+ // frozen panes
+ $cell = Coordinate::stringFromColumnIndex($px + 1) . ($py + 1);
+ $topLeftCell = Coordinate::stringFromColumnIndex($colLeft + 1) . ($rwTop + 1);
+ $this->phpSheet->freezePane($cell, $topLeftCell);
+ }
+ // unfrozen panes; split windows; not supported by PhpSpreadsheet core
+ }
+ }
+
+ /**
+ * Read SELECTION record. There is one such record for each pane in the sheet.
+ */
+ private function readSelection()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 1; pane identifier
+ $paneId = ord($recordData[0]);
+
+ // offset: 1; size: 2; index to row of the active cell
+ $r = self::getUInt2d($recordData, 1);
+
+ // offset: 3; size: 2; index to column of the active cell
+ $c = self::getUInt2d($recordData, 3);
+
+ // offset: 5; size: 2; index into the following cell range list to the
+ // entry that contains the active cell
+ $index = self::getUInt2d($recordData, 5);
+
+ // offset: 7; size: var; cell range address list containing all selected cell ranges
+ $data = substr($recordData, 7);
+ $cellRangeAddressList = $this->readBIFF5CellRangeAddressList($data); // note: also BIFF8 uses BIFF5 syntax
+
+ $selectedCells = $cellRangeAddressList['cellRangeAddresses'][0];
+
+ // first row '1' + last row '16384' indicates that full column is selected (apparently also in BIFF8!)
+ if (preg_match('/^([A-Z]+1\:[A-Z]+)16384$/', $selectedCells)) {
+ $selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)16384$/', '${1}1048576', $selectedCells);
+ }
+
+ // first row '1' + last row '65536' indicates that full column is selected
+ if (preg_match('/^([A-Z]+1\:[A-Z]+)65536$/', $selectedCells)) {
+ $selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)65536$/', '${1}1048576', $selectedCells);
+ }
+
+ // first column 'A' + last column 'IV' indicates that full row is selected
+ if (preg_match('/^(A\d+\:)IV(\d+)$/', $selectedCells)) {
+ $selectedCells = preg_replace('/^(A\d+\:)IV(\d+)$/', '${1}XFD${2}', $selectedCells);
+ }
+
+ $this->phpSheet->setSelectedCells($selectedCells);
+ }
+ }
+
+ private function includeCellRangeFiltered($cellRangeAddress)
+ {
+ $includeCellRange = true;
+ if ($this->getReadFilter() !== null) {
+ $includeCellRange = false;
+ $rangeBoundaries = Coordinate::getRangeBoundaries($cellRangeAddress);
+ ++$rangeBoundaries[1][0];
+ for ($row = $rangeBoundaries[0][1]; $row <= $rangeBoundaries[1][1]; ++$row) {
+ for ($column = $rangeBoundaries[0][0]; $column != $rangeBoundaries[1][0]; ++$column) {
+ if ($this->getReadFilter()->readCell($column, $row, $this->phpSheet->getTitle())) {
+ $includeCellRange = true;
+
+ break 2;
+ }
+ }
+ }
+ }
+
+ return $includeCellRange;
+ }
+
+ /**
+ * MERGEDCELLS.
+ *
+ * This record contains the addresses of merged cell ranges
+ * in the current sheet.
+ *
+ * -- "OpenOffice.org's Documentation of the Microsoft
+ * Excel File Format"
+ */
+ private function readMergedCells()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->version == self::XLS_BIFF8 && !$this->readDataOnly) {
+ $cellRangeAddressList = $this->readBIFF8CellRangeAddressList($recordData);
+ foreach ($cellRangeAddressList['cellRangeAddresses'] as $cellRangeAddress) {
+ if ((strpos($cellRangeAddress, ':') !== false) &&
+ ($this->includeCellRangeFiltered($cellRangeAddress))) {
+ $this->phpSheet->mergeCells($cellRangeAddress);
+ }
+ }
+ }
+ }
+
+ /**
+ * Read HYPERLINK record.
+ */
+ private function readHyperLink()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer forward to next record
+ $this->pos += 4 + $length;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 8; cell range address of all cells containing this hyperlink
+ try {
+ $cellRange = $this->readBIFF8CellRangeAddressFixed($recordData);
+ } catch (PhpSpreadsheetException $e) {
+ return;
+ }
+
+ // offset: 8, size: 16; GUID of StdLink
+
+ // offset: 24, size: 4; unknown value
+
+ // offset: 28, size: 4; option flags
+ // bit: 0; mask: 0x00000001; 0 = no link or extant, 1 = file link or URL
+ $isFileLinkOrUrl = (0x00000001 & self::getUInt2d($recordData, 28)) >> 0;
+
+ // bit: 1; mask: 0x00000002; 0 = relative path, 1 = absolute path or URL
+ $isAbsPathOrUrl = (0x00000001 & self::getUInt2d($recordData, 28)) >> 1;
+
+ // bit: 2 (and 4); mask: 0x00000014; 0 = no description
+ $hasDesc = (0x00000014 & self::getUInt2d($recordData, 28)) >> 2;
+
+ // bit: 3; mask: 0x00000008; 0 = no text, 1 = has text
+ $hasText = (0x00000008 & self::getUInt2d($recordData, 28)) >> 3;
+
+ // bit: 7; mask: 0x00000080; 0 = no target frame, 1 = has target frame
+ $hasFrame = (0x00000080 & self::getUInt2d($recordData, 28)) >> 7;
+
+ // bit: 8; mask: 0x00000100; 0 = file link or URL, 1 = UNC path (inc. server name)
+ $isUNC = (0x00000100 & self::getUInt2d($recordData, 28)) >> 8;
+
+ // offset within record data
+ $offset = 32;
+
+ if ($hasDesc) {
+ // offset: 32; size: var; character count of description text
+ $dl = self::getInt4d($recordData, 32);
+ // offset: 36; size: var; character array of description text, no Unicode string header, always 16-bit characters, zero terminated
+ $desc = self::encodeUTF16(substr($recordData, 36, 2 * ($dl - 1)), false);
+ $offset += 4 + 2 * $dl;
+ }
+ if ($hasFrame) {
+ $fl = self::getInt4d($recordData, $offset);
+ $offset += 4 + 2 * $fl;
+ }
+
+ // detect type of hyperlink (there are 4 types)
+ $hyperlinkType = null;
+
+ if ($isUNC) {
+ $hyperlinkType = 'UNC';
+ } elseif (!$isFileLinkOrUrl) {
+ $hyperlinkType = 'workbook';
+ } elseif (ord($recordData[$offset]) == 0x03) {
+ $hyperlinkType = 'local';
+ } elseif (ord($recordData[$offset]) == 0xE0) {
+ $hyperlinkType = 'URL';
+ }
+
+ switch ($hyperlinkType) {
+ case 'URL':
+ // section 5.58.2: Hyperlink containing a URL
+ // e.g. http://example.org/index.php
+
+ // offset: var; size: 16; GUID of URL Moniker
+ $offset += 16;
+ // offset: var; size: 4; size (in bytes) of character array of the URL including trailing zero word
+ $us = self::getInt4d($recordData, $offset);
+ $offset += 4;
+ // offset: var; size: $us; character array of the URL, no Unicode string header, always 16-bit characters, zero-terminated
+ $url = self::encodeUTF16(substr($recordData, $offset, $us - 2), false);
+ $nullOffset = strpos($url, chr(0x00));
+ if ($nullOffset) {
+ $url = substr($url, 0, $nullOffset);
+ }
+ $url .= $hasText ? '#' : '';
+ $offset += $us;
+
+ break;
+ case 'local':
+ // section 5.58.3: Hyperlink to local file
+ // examples:
+ // mydoc.txt
+ // ../../somedoc.xls#Sheet!A1
+
+ // offset: var; size: 16; GUI of File Moniker
+ $offset += 16;
+
+ // offset: var; size: 2; directory up-level count.
+ $upLevelCount = self::getUInt2d($recordData, $offset);
+ $offset += 2;
+
+ // offset: var; size: 4; character count of the shortened file path and name, including trailing zero word
+ $sl = self::getInt4d($recordData, $offset);
+ $offset += 4;
+
+ // offset: var; size: sl; character array of the shortened file path and name in 8.3-DOS-format (compressed Unicode string)
+ $shortenedFilePath = substr($recordData, $offset, $sl);
+ $shortenedFilePath = self::encodeUTF16($shortenedFilePath, true);
+ $shortenedFilePath = substr($shortenedFilePath, 0, -1); // remove trailing zero
+
+ $offset += $sl;
+
+ // offset: var; size: 24; unknown sequence
+ $offset += 24;
+
+ // extended file path
+ // offset: var; size: 4; size of the following file link field including string lenth mark
+ $sz = self::getInt4d($recordData, $offset);
+ $offset += 4;
+
+ // only present if $sz > 0
+ if ($sz > 0) {
+ // offset: var; size: 4; size of the character array of the extended file path and name
+ $xl = self::getInt4d($recordData, $offset);
+ $offset += 4;
+
+ // offset: var; size 2; unknown
+ $offset += 2;
+
+ // offset: var; size $xl; character array of the extended file path and name.
+ $extendedFilePath = substr($recordData, $offset, $xl);
+ $extendedFilePath = self::encodeUTF16($extendedFilePath, false);
+ $offset += $xl;
+ }
+
+ // construct the path
+ $url = str_repeat('..\\', $upLevelCount);
+ $url .= ($sz > 0) ? $extendedFilePath : $shortenedFilePath; // use extended path if available
+ $url .= $hasText ? '#' : '';
+
+ break;
+ case 'UNC':
+ // section 5.58.4: Hyperlink to a File with UNC (Universal Naming Convention) Path
+ // todo: implement
+ return;
+ case 'workbook':
+ // section 5.58.5: Hyperlink to the Current Workbook
+ // e.g. Sheet2!B1:C2, stored in text mark field
+ $url = 'sheet://';
+
+ break;
+ default:
+ return;
+ }
+
+ if ($hasText) {
+ // offset: var; size: 4; character count of text mark including trailing zero word
+ $tl = self::getInt4d($recordData, $offset);
+ $offset += 4;
+ // offset: var; size: var; character array of the text mark without the # sign, no Unicode header, always 16-bit characters, zero-terminated
+ $text = self::encodeUTF16(substr($recordData, $offset, 2 * ($tl - 1)), false);
+ $url .= $text;
+ }
+
+ // apply the hyperlink to all the relevant cells
+ foreach (Coordinate::extractAllCellReferencesInRange($cellRange) as $coordinate) {
+ $this->phpSheet->getCell($coordinate)->getHyperLink()->setUrl($url);
+ }
+ }
+ }
+
+ /**
+ * Read DATAVALIDATIONS record.
+ */
+ private function readDataValidations()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer forward to next record
+ $this->pos += 4 + $length;
+ }
+
+ /**
+ * Read DATAVALIDATION record.
+ */
+ private function readDataValidation()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer forward to next record
+ $this->pos += 4 + $length;
+
+ if ($this->readDataOnly) {
+ return;
+ }
+
+ // offset: 0; size: 4; Options
+ $options = self::getInt4d($recordData, 0);
+
+ // bit: 0-3; mask: 0x0000000F; type
+ $type = (0x0000000F & $options) >> 0;
+ switch ($type) {
+ case 0x00:
+ $type = DataValidation::TYPE_NONE;
+
+ break;
+ case 0x01:
+ $type = DataValidation::TYPE_WHOLE;
+
+ break;
+ case 0x02:
+ $type = DataValidation::TYPE_DECIMAL;
+
+ break;
+ case 0x03:
+ $type = DataValidation::TYPE_LIST;
+
+ break;
+ case 0x04:
+ $type = DataValidation::TYPE_DATE;
+
+ break;
+ case 0x05:
+ $type = DataValidation::TYPE_TIME;
+
+ break;
+ case 0x06:
+ $type = DataValidation::TYPE_TEXTLENGTH;
+
+ break;
+ case 0x07:
+ $type = DataValidation::TYPE_CUSTOM;
+
+ break;
+ }
+
+ // bit: 4-6; mask: 0x00000070; error type
+ $errorStyle = (0x00000070 & $options) >> 4;
+ switch ($errorStyle) {
+ case 0x00:
+ $errorStyle = DataValidation::STYLE_STOP;
+
+ break;
+ case 0x01:
+ $errorStyle = DataValidation::STYLE_WARNING;
+
+ break;
+ case 0x02:
+ $errorStyle = DataValidation::STYLE_INFORMATION;
+
+ break;
+ }
+
+ // bit: 7; mask: 0x00000080; 1= formula is explicit (only applies to list)
+ // I have only seen cases where this is 1
+ $explicitFormula = (0x00000080 & $options) >> 7;
+
+ // bit: 8; mask: 0x00000100; 1= empty cells allowed
+ $allowBlank = (0x00000100 & $options) >> 8;
+
+ // bit: 9; mask: 0x00000200; 1= suppress drop down arrow in list type validity
+ $suppressDropDown = (0x00000200 & $options) >> 9;
+
+ // bit: 18; mask: 0x00040000; 1= show prompt box if cell selected
+ $showInputMessage = (0x00040000 & $options) >> 18;
+
+ // bit: 19; mask: 0x00080000; 1= show error box if invalid values entered
+ $showErrorMessage = (0x00080000 & $options) >> 19;
+
+ // bit: 20-23; mask: 0x00F00000; condition operator
+ $operator = (0x00F00000 & $options) >> 20;
+ switch ($operator) {
+ case 0x00:
+ $operator = DataValidation::OPERATOR_BETWEEN;
+
+ break;
+ case 0x01:
+ $operator = DataValidation::OPERATOR_NOTBETWEEN;
+
+ break;
+ case 0x02:
+ $operator = DataValidation::OPERATOR_EQUAL;
+
+ break;
+ case 0x03:
+ $operator = DataValidation::OPERATOR_NOTEQUAL;
+
+ break;
+ case 0x04:
+ $operator = DataValidation::OPERATOR_GREATERTHAN;
+
+ break;
+ case 0x05:
+ $operator = DataValidation::OPERATOR_LESSTHAN;
+
+ break;
+ case 0x06:
+ $operator = DataValidation::OPERATOR_GREATERTHANOREQUAL;
+
+ break;
+ case 0x07:
+ $operator = DataValidation::OPERATOR_LESSTHANOREQUAL;
+
+ break;
+ }
+
+ // offset: 4; size: var; title of the prompt box
+ $offset = 4;
+ $string = self::readUnicodeStringLong(substr($recordData, $offset));
+ $promptTitle = $string['value'] !== chr(0) ? $string['value'] : '';
+ $offset += $string['size'];
+
+ // offset: var; size: var; title of the error box
+ $string = self::readUnicodeStringLong(substr($recordData, $offset));
+ $errorTitle = $string['value'] !== chr(0) ? $string['value'] : '';
+ $offset += $string['size'];
+
+ // offset: var; size: var; text of the prompt box
+ $string = self::readUnicodeStringLong(substr($recordData, $offset));
+ $prompt = $string['value'] !== chr(0) ? $string['value'] : '';
+ $offset += $string['size'];
+
+ // offset: var; size: var; text of the error box
+ $string = self::readUnicodeStringLong(substr($recordData, $offset));
+ $error = $string['value'] !== chr(0) ? $string['value'] : '';
+ $offset += $string['size'];
+
+ // offset: var; size: 2; size of the formula data for the first condition
+ $sz1 = self::getUInt2d($recordData, $offset);
+ $offset += 2;
+
+ // offset: var; size: 2; not used
+ $offset += 2;
+
+ // offset: var; size: $sz1; formula data for first condition (without size field)
+ $formula1 = substr($recordData, $offset, $sz1);
+ $formula1 = pack('v', $sz1) . $formula1; // prepend the length
+ try {
+ $formula1 = $this->getFormulaFromStructure($formula1);
+
+ // in list type validity, null characters are used as item separators
+ if ($type == DataValidation::TYPE_LIST) {
+ $formula1 = str_replace(chr(0), ',', $formula1);
+ }
+ } catch (PhpSpreadsheetException $e) {
+ return;
+ }
+ $offset += $sz1;
+
+ // offset: var; size: 2; size of the formula data for the first condition
+ $sz2 = self::getUInt2d($recordData, $offset);
+ $offset += 2;
+
+ // offset: var; size: 2; not used
+ $offset += 2;
+
+ // offset: var; size: $sz2; formula data for second condition (without size field)
+ $formula2 = substr($recordData, $offset, $sz2);
+ $formula2 = pack('v', $sz2) . $formula2; // prepend the length
+ try {
+ $formula2 = $this->getFormulaFromStructure($formula2);
+ } catch (PhpSpreadsheetException $e) {
+ return;
+ }
+ $offset += $sz2;
+
+ // offset: var; size: var; cell range address list with
+ $cellRangeAddressList = $this->readBIFF8CellRangeAddressList(substr($recordData, $offset));
+ $cellRangeAddresses = $cellRangeAddressList['cellRangeAddresses'];
+
+ foreach ($cellRangeAddresses as $cellRange) {
+ $stRange = $this->phpSheet->shrinkRangeToFit($cellRange);
+ foreach (Coordinate::extractAllCellReferencesInRange($stRange) as $coordinate) {
+ $objValidation = $this->phpSheet->getCell($coordinate)->getDataValidation();
+ $objValidation->setType($type);
+ $objValidation->setErrorStyle($errorStyle);
+ $objValidation->setAllowBlank((bool) $allowBlank);
+ $objValidation->setShowInputMessage((bool) $showInputMessage);
+ $objValidation->setShowErrorMessage((bool) $showErrorMessage);
+ $objValidation->setShowDropDown(!$suppressDropDown);
+ $objValidation->setOperator($operator);
+ $objValidation->setErrorTitle($errorTitle);
+ $objValidation->setError($error);
+ $objValidation->setPromptTitle($promptTitle);
+ $objValidation->setPrompt($prompt);
+ $objValidation->setFormula1($formula1);
+ $objValidation->setFormula2($formula2);
+ }
+ }
+ }
+
+ /**
+ * Read SHEETLAYOUT record. Stores sheet tab color information.
+ */
+ private function readSheetLayout()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // local pointer in record data
+ $offset = 0;
+
+ if (!$this->readDataOnly) {
+ // offset: 0; size: 2; repeated record identifier 0x0862
+
+ // offset: 2; size: 10; not used
+
+ // offset: 12; size: 4; size of record data
+ // Excel 2003 uses size of 0x14 (documented), Excel 2007 uses size of 0x28 (not documented?)
+ $sz = self::getInt4d($recordData, 12);
+
+ switch ($sz) {
+ case 0x14:
+ // offset: 16; size: 2; color index for sheet tab
+ $colorIndex = self::getUInt2d($recordData, 16);
+ $color = Xls\Color::map($colorIndex, $this->palette, $this->version);
+ $this->phpSheet->getTabColor()->setRGB($color['rgb']);
+
+ break;
+ case 0x28:
+ // TODO: Investigate structure for .xls SHEETLAYOUT record as saved by MS Office Excel 2007
+ return;
+
+ break;
+ }
+ }
+ }
+
+ /**
+ * Read SHEETPROTECTION record (FEATHEADR).
+ */
+ private function readSheetProtection()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ if ($this->readDataOnly) {
+ return;
+ }
+
+ // offset: 0; size: 2; repeated record header
+
+ // offset: 2; size: 2; FRT cell reference flag (=0 currently)
+
+ // offset: 4; size: 8; Currently not used and set to 0
+
+ // offset: 12; size: 2; Shared feature type index (2=Enhanced Protetion, 4=SmartTag)
+ $isf = self::getUInt2d($recordData, 12);
+ if ($isf != 2) {
+ return;
+ }
+
+ // offset: 14; size: 1; =1 since this is a feat header
+
+ // offset: 15; size: 4; size of rgbHdrSData
+
+ // rgbHdrSData, assume "Enhanced Protection"
+ // offset: 19; size: 2; option flags
+ $options = self::getUInt2d($recordData, 19);
+
+ // bit: 0; mask 0x0001; 1 = user may edit objects, 0 = users must not edit objects
+ $bool = (0x0001 & $options) >> 0;
+ $this->phpSheet->getProtection()->setObjects(!$bool);
+
+ // bit: 1; mask 0x0002; edit scenarios
+ $bool = (0x0002 & $options) >> 1;
+ $this->phpSheet->getProtection()->setScenarios(!$bool);
+
+ // bit: 2; mask 0x0004; format cells
+ $bool = (0x0004 & $options) >> 2;
+ $this->phpSheet->getProtection()->setFormatCells(!$bool);
+
+ // bit: 3; mask 0x0008; format columns
+ $bool = (0x0008 & $options) >> 3;
+ $this->phpSheet->getProtection()->setFormatColumns(!$bool);
+
+ // bit: 4; mask 0x0010; format rows
+ $bool = (0x0010 & $options) >> 4;
+ $this->phpSheet->getProtection()->setFormatRows(!$bool);
+
+ // bit: 5; mask 0x0020; insert columns
+ $bool = (0x0020 & $options) >> 5;
+ $this->phpSheet->getProtection()->setInsertColumns(!$bool);
+
+ // bit: 6; mask 0x0040; insert rows
+ $bool = (0x0040 & $options) >> 6;
+ $this->phpSheet->getProtection()->setInsertRows(!$bool);
+
+ // bit: 7; mask 0x0080; insert hyperlinks
+ $bool = (0x0080 & $options) >> 7;
+ $this->phpSheet->getProtection()->setInsertHyperlinks(!$bool);
+
+ // bit: 8; mask 0x0100; delete columns
+ $bool = (0x0100 & $options) >> 8;
+ $this->phpSheet->getProtection()->setDeleteColumns(!$bool);
+
+ // bit: 9; mask 0x0200; delete rows
+ $bool = (0x0200 & $options) >> 9;
+ $this->phpSheet->getProtection()->setDeleteRows(!$bool);
+
+ // bit: 10; mask 0x0400; select locked cells
+ $bool = (0x0400 & $options) >> 10;
+ $this->phpSheet->getProtection()->setSelectLockedCells(!$bool);
+
+ // bit: 11; mask 0x0800; sort cell range
+ $bool = (0x0800 & $options) >> 11;
+ $this->phpSheet->getProtection()->setSort(!$bool);
+
+ // bit: 12; mask 0x1000; auto filter
+ $bool = (0x1000 & $options) >> 12;
+ $this->phpSheet->getProtection()->setAutoFilter(!$bool);
+
+ // bit: 13; mask 0x2000; pivot tables
+ $bool = (0x2000 & $options) >> 13;
+ $this->phpSheet->getProtection()->setPivotTables(!$bool);
+
+ // bit: 14; mask 0x4000; select unlocked cells
+ $bool = (0x4000 & $options) >> 14;
+ $this->phpSheet->getProtection()->setSelectUnlockedCells(!$bool);
+
+ // offset: 21; size: 2; not used
+ }
+
+ /**
+ * Read RANGEPROTECTION record
+ * Reading of this record is based on Microsoft Office Excel 97-2000 Binary File Format Specification,
+ * where it is referred to as FEAT record.
+ */
+ private function readRangeProtection()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ // local pointer in record data
+ $offset = 0;
+
+ if (!$this->readDataOnly) {
+ $offset += 12;
+
+ // offset: 12; size: 2; shared feature type, 2 = enhanced protection, 4 = smart tag
+ $isf = self::getUInt2d($recordData, 12);
+ if ($isf != 2) {
+ // we only read FEAT records of type 2
+ return;
+ }
+ $offset += 2;
+
+ $offset += 5;
+
+ // offset: 19; size: 2; count of ref ranges this feature is on
+ $cref = self::getUInt2d($recordData, 19);
+ $offset += 2;
+
+ $offset += 6;
+
+ // offset: 27; size: 8 * $cref; list of cell ranges (like in hyperlink record)
+ $cellRanges = [];
+ for ($i = 0; $i < $cref; ++$i) {
+ try {
+ $cellRange = $this->readBIFF8CellRangeAddressFixed(substr($recordData, 27 + 8 * $i, 8));
+ } catch (PhpSpreadsheetException $e) {
+ return;
+ }
+ $cellRanges[] = $cellRange;
+ $offset += 8;
+ }
+
+ // offset: var; size: var; variable length of feature specific data
+ $rgbFeat = substr($recordData, $offset);
+ $offset += 4;
+
+ // offset: var; size: 4; the encrypted password (only 16-bit although field is 32-bit)
+ $wPassword = self::getInt4d($recordData, $offset);
+ $offset += 4;
+
+ // Apply range protection to sheet
+ if ($cellRanges) {
+ $this->phpSheet->protectCells(implode(' ', $cellRanges), strtoupper(dechex($wPassword)), true);
+ }
+ }
+ }
+
+ /**
+ * Read a free CONTINUE record. Free CONTINUE record may be a camouflaged MSODRAWING record
+ * When MSODRAWING data on a sheet exceeds 8224 bytes, CONTINUE records are used instead. Undocumented.
+ * In this case, we must treat the CONTINUE record as a MSODRAWING record.
+ */
+ private function readContinue()
+ {
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $recordData = $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ // check if we are reading drawing data
+ // this is in case a free CONTINUE record occurs in other circumstances we are unaware of
+ if ($this->drawingData == '') {
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ return;
+ }
+
+ // check if record data is at least 4 bytes long, otherwise there is no chance this is MSODRAWING data
+ if ($length < 4) {
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+
+ return;
+ }
+
+ // dirty check to see if CONTINUE record could be a camouflaged MSODRAWING record
+ // look inside CONTINUE record to see if it looks like a part of an Escher stream
+ // we know that Escher stream may be split at least at
+ // 0xF003 MsofbtSpgrContainer
+ // 0xF004 MsofbtSpContainer
+ // 0xF00D MsofbtClientTextbox
+ $validSplitPoints = [0xF003, 0xF004, 0xF00D]; // add identifiers if we find more
+
+ $splitPoint = self::getUInt2d($recordData, 2);
+ if (in_array($splitPoint, $validSplitPoints)) {
+ // get spliced record data (and move pointer to next record)
+ $splicedRecordData = $this->getSplicedRecordData();
+ $this->drawingData .= $splicedRecordData['recordData'];
+
+ return;
+ }
+
+ // move stream pointer to next record
+ $this->pos += 4 + $length;
+ }
+
+ /**
+ * Reads a record from current position in data stream and continues reading data as long as CONTINUE
+ * records are found. Splices the record data pieces and returns the combined string as if record data
+ * is in one piece.
+ * Moves to next current position in data stream to start of next record different from a CONtINUE record.
+ *
+ * @return array
+ */
+ private function getSplicedRecordData()
+ {
+ $data = '';
+ $spliceOffsets = [];
+
+ $i = 0;
+ $spliceOffsets[0] = 0;
+
+ do {
+ ++$i;
+
+ // offset: 0; size: 2; identifier
+ $identifier = self::getUInt2d($this->data, $this->pos);
+ // offset: 2; size: 2; length
+ $length = self::getUInt2d($this->data, $this->pos + 2);
+ $data .= $this->readRecordData($this->data, $this->pos + 4, $length);
+
+ $spliceOffsets[$i] = $spliceOffsets[$i - 1] + $length;
+
+ $this->pos += 4 + $length;
+ $nextIdentifier = self::getUInt2d($this->data, $this->pos);
+ } while ($nextIdentifier == self::XLS_TYPE_CONTINUE);
+
+ $splicedData = [
+ 'recordData' => $data,
+ 'spliceOffsets' => $spliceOffsets,
+ ];
+
+ return $splicedData;
+ }
+
+ /**
+ * Convert formula structure into human readable Excel formula like 'A3+A5*5'.
+ *
+ * @param string $formulaStructure The complete binary data for the formula
+ * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
+ *
+ * @return string Human readable formula
+ */
+ private function getFormulaFromStructure($formulaStructure, $baseCell = 'A1')
+ {
+ // offset: 0; size: 2; size of the following formula data
+ $sz = self::getUInt2d($formulaStructure, 0);
+
+ // offset: 2; size: sz
+ $formulaData = substr($formulaStructure, 2, $sz);
+
+ // offset: 2 + sz; size: variable (optional)
+ if (strlen($formulaStructure) > 2 + $sz) {
+ $additionalData = substr($formulaStructure, 2 + $sz);
+ } else {
+ $additionalData = '';
+ }
+
+ return $this->getFormulaFromData($formulaData, $additionalData, $baseCell);
+ }
+
+ /**
+ * Take formula data and additional data for formula and return human readable formula.
+ *
+ * @param string $formulaData The binary data for the formula itself
+ * @param string $additionalData Additional binary data going with the formula
+ * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
+ *
+ * @return string Human readable formula
+ */
+ private function getFormulaFromData($formulaData, $additionalData = '', $baseCell = 'A1')
+ {
+ // start parsing the formula data
+ $tokens = [];
+
+ while (strlen($formulaData) > 0 and $token = $this->getNextToken($formulaData, $baseCell)) {
+ $tokens[] = $token;
+ $formulaData = substr($formulaData, $token['size']);
+ }
+
+ $formulaString = $this->createFormulaFromTokens($tokens, $additionalData);
+
+ return $formulaString;
+ }
+
+ /**
+ * Take array of tokens together with additional data for formula and return human readable formula.
+ *
+ * @param array $tokens
+ * @param string $additionalData Additional binary data going with the formula
+ *
+ * @return string Human readable formula
+ */
+ private function createFormulaFromTokens($tokens, $additionalData)
+ {
+ // empty formula?
+ if (empty($tokens)) {
+ return '';
+ }
+
+ $formulaStrings = [];
+ foreach ($tokens as $token) {
+ // initialize spaces
+ $space0 = isset($space0) ? $space0 : ''; // spaces before next token, not tParen
+ $space1 = isset($space1) ? $space1 : ''; // carriage returns before next token, not tParen
+ $space2 = isset($space2) ? $space2 : ''; // spaces before opening parenthesis
+ $space3 = isset($space3) ? $space3 : ''; // carriage returns before opening parenthesis
+ $space4 = isset($space4) ? $space4 : ''; // spaces before closing parenthesis
+ $space5 = isset($space5) ? $space5 : ''; // carriage returns before closing parenthesis
+
+ switch ($token['name']) {
+ case 'tAdd': // addition
+ case 'tConcat': // addition
+ case 'tDiv': // division
+ case 'tEQ': // equality
+ case 'tGE': // greater than or equal
+ case 'tGT': // greater than
+ case 'tIsect': // intersection
+ case 'tLE': // less than or equal
+ case 'tList': // less than or equal
+ case 'tLT': // less than
+ case 'tMul': // multiplication
+ case 'tNE': // multiplication
+ case 'tPower': // power
+ case 'tRange': // range
+ case 'tSub': // subtraction
+ $op2 = array_pop($formulaStrings);
+ $op1 = array_pop($formulaStrings);
+ $formulaStrings[] = "$op1$space1$space0{$token['data']}$op2";
+ unset($space0, $space1);
+
+ break;
+ case 'tUplus': // unary plus
+ case 'tUminus': // unary minus
+ $op = array_pop($formulaStrings);
+ $formulaStrings[] = "$space1$space0{$token['data']}$op";
+ unset($space0, $space1);
+
+ break;
+ case 'tPercent': // percent sign
+ $op = array_pop($formulaStrings);
+ $formulaStrings[] = "$op$space1$space0{$token['data']}";
+ unset($space0, $space1);
+
+ break;
+ case 'tAttrVolatile': // indicates volatile function
+ case 'tAttrIf':
+ case 'tAttrSkip':
+ case 'tAttrChoose':
+ // token is only important for Excel formula evaluator
+ // do nothing
+ break;
+ case 'tAttrSpace': // space / carriage return
+ // space will be used when next token arrives, do not alter formulaString stack
+ switch ($token['data']['spacetype']) {
+ case 'type0':
+ $space0 = str_repeat(' ', $token['data']['spacecount']);
+
+ break;
+ case 'type1':
+ $space1 = str_repeat("\n", $token['data']['spacecount']);
+
+ break;
+ case 'type2':
+ $space2 = str_repeat(' ', $token['data']['spacecount']);
+
+ break;
+ case 'type3':
+ $space3 = str_repeat("\n", $token['data']['spacecount']);
+
+ break;
+ case 'type4':
+ $space4 = str_repeat(' ', $token['data']['spacecount']);
+
+ break;
+ case 'type5':
+ $space5 = str_repeat("\n", $token['data']['spacecount']);
+
+ break;
+ }
+
+ break;
+ case 'tAttrSum': // SUM function with one parameter
+ $op = array_pop($formulaStrings);
+ $formulaStrings[] = "{$space1}{$space0}SUM($op)";
+ unset($space0, $space1);
+
+ break;
+ case 'tFunc': // function with fixed number of arguments
+ case 'tFuncV': // function with variable number of arguments
+ if ($token['data']['function'] != '') {
+ // normal function
+ $ops = []; // array of operators
+ for ($i = 0; $i < $token['data']['args']; ++$i) {
+ $ops[] = array_pop($formulaStrings);
+ }
+ $ops = array_reverse($ops);
+ $formulaStrings[] = "$space1$space0{$token['data']['function']}(" . implode(',', $ops) . ')';
+ unset($space0, $space1);
+ } else {
+ // add-in function
+ $ops = []; // array of operators
+ for ($i = 0; $i < $token['data']['args'] - 1; ++$i) {
+ $ops[] = array_pop($formulaStrings);
+ }
+ $ops = array_reverse($ops);
+ $function = array_pop($formulaStrings);
+ $formulaStrings[] = "$space1$space0$function(" . implode(',', $ops) . ')';
+ unset($space0, $space1);
+ }
+
+ break;
+ case 'tParen': // parenthesis
+ $expression = array_pop($formulaStrings);
+ $formulaStrings[] = "$space3$space2($expression$space5$space4)";
+ unset($space2, $space3, $space4, $space5);
+
+ break;
+ case 'tArray': // array constant
+ $constantArray = self::readBIFF8ConstantArray($additionalData);
+ $formulaStrings[] = $space1 . $space0 . $constantArray['value'];
+ $additionalData = substr($additionalData, $constantArray['size']); // bite of chunk of additional data
+ unset($space0, $space1);
+
+ break;
+ case 'tMemArea':
+ // bite off chunk of additional data
+ $cellRangeAddressList = $this->readBIFF8CellRangeAddressList($additionalData);
+ $additionalData = substr($additionalData, $cellRangeAddressList['size']);
+ $formulaStrings[] = "$space1$space0{$token['data']}";
+ unset($space0, $space1);
+
+ break;
+ case 'tArea': // cell range address
+ case 'tBool': // boolean
+ case 'tErr': // error code
+ case 'tInt': // integer
+ case 'tMemErr':
+ case 'tMemFunc':
+ case 'tMissArg':
+ case 'tName':
+ case 'tNameX':
+ case 'tNum': // number
+ case 'tRef': // single cell reference
+ case 'tRef3d': // 3d cell reference
+ case 'tArea3d': // 3d cell range reference
+ case 'tRefN':
+ case 'tAreaN':
+ case 'tStr': // string
+ $formulaStrings[] = "$space1$space0{$token['data']}";
+ unset($space0, $space1);
+
+ break;
+ }
+ }
+ $formulaString = $formulaStrings[0];
+
+ return $formulaString;
+ }
+
+ /**
+ * Fetch next token from binary formula data.
+ *
+ * @param string $formulaData Formula data
+ * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ private function getNextToken($formulaData, $baseCell = 'A1')
+ {
+ // offset: 0; size: 1; token id
+ $id = ord($formulaData[0]); // token id
+ $name = false; // initialize token name
+
+ switch ($id) {
+ case 0x03:
+ $name = 'tAdd';
+ $size = 1;
+ $data = '+';
+
+ break;
+ case 0x04:
+ $name = 'tSub';
+ $size = 1;
+ $data = '-';
+
+ break;
+ case 0x05:
+ $name = 'tMul';
+ $size = 1;
+ $data = '*';
+
+ break;
+ case 0x06:
+ $name = 'tDiv';
+ $size = 1;
+ $data = '/';
+
+ break;
+ case 0x07:
+ $name = 'tPower';
+ $size = 1;
+ $data = '^';
+
+ break;
+ case 0x08:
+ $name = 'tConcat';
+ $size = 1;
+ $data = '&';
+
+ break;
+ case 0x09:
+ $name = 'tLT';
+ $size = 1;
+ $data = '<';
+
+ break;
+ case 0x0A:
+ $name = 'tLE';
+ $size = 1;
+ $data = '<=';
+
+ break;
+ case 0x0B:
+ $name = 'tEQ';
+ $size = 1;
+ $data = '=';
+
+ break;
+ case 0x0C:
+ $name = 'tGE';
+ $size = 1;
+ $data = '>=';
+
+ break;
+ case 0x0D:
+ $name = 'tGT';
+ $size = 1;
+ $data = '>';
+
+ break;
+ case 0x0E:
+ $name = 'tNE';
+ $size = 1;
+ $data = '<>';
+
+ break;
+ case 0x0F:
+ $name = 'tIsect';
+ $size = 1;
+ $data = ' ';
+
+ break;
+ case 0x10:
+ $name = 'tList';
+ $size = 1;
+ $data = ',';
+
+ break;
+ case 0x11:
+ $name = 'tRange';
+ $size = 1;
+ $data = ':';
+
+ break;
+ case 0x12:
+ $name = 'tUplus';
+ $size = 1;
+ $data = '+';
+
+ break;
+ case 0x13:
+ $name = 'tUminus';
+ $size = 1;
+ $data = '-';
+
+ break;
+ case 0x14:
+ $name = 'tPercent';
+ $size = 1;
+ $data = '%';
+
+ break;
+ case 0x15: // parenthesis
+ $name = 'tParen';
+ $size = 1;
+ $data = null;
+
+ break;
+ case 0x16: // missing argument
+ $name = 'tMissArg';
+ $size = 1;
+ $data = '';
+
+ break;
+ case 0x17: // string
+ $name = 'tStr';
+ // offset: 1; size: var; Unicode string, 8-bit string length
+ $string = self::readUnicodeStringShort(substr($formulaData, 1));
+ $size = 1 + $string['size'];
+ $data = self::UTF8toExcelDoubleQuoted($string['value']);
+
+ break;
+ case 0x19: // Special attribute
+ // offset: 1; size: 1; attribute type flags:
+ switch (ord($formulaData[1])) {
+ case 0x01:
+ $name = 'tAttrVolatile';
+ $size = 4;
+ $data = null;
+
+ break;
+ case 0x02:
+ $name = 'tAttrIf';
+ $size = 4;
+ $data = null;
+
+ break;
+ case 0x04:
+ $name = 'tAttrChoose';
+ // offset: 2; size: 2; number of choices in the CHOOSE function ($nc, number of parameters decreased by 1)
+ $nc = self::getUInt2d($formulaData, 2);
+ // offset: 4; size: 2 * $nc
+ // offset: 4 + 2 * $nc; size: 2
+ $size = 2 * $nc + 6;
+ $data = null;
+
+ break;
+ case 0x08:
+ $name = 'tAttrSkip';
+ $size = 4;
+ $data = null;
+
+ break;
+ case 0x10:
+ $name = 'tAttrSum';
+ $size = 4;
+ $data = null;
+
+ break;
+ case 0x40:
+ case 0x41:
+ $name = 'tAttrSpace';
+ $size = 4;
+ // offset: 2; size: 2; space type and position
+ switch (ord($formulaData[2])) {
+ case 0x00:
+ $spacetype = 'type0';
+
+ break;
+ case 0x01:
+ $spacetype = 'type1';
+
+ break;
+ case 0x02:
+ $spacetype = 'type2';
+
+ break;
+ case 0x03:
+ $spacetype = 'type3';
+
+ break;
+ case 0x04:
+ $spacetype = 'type4';
+
+ break;
+ case 0x05:
+ $spacetype = 'type5';
+
+ break;
+ default:
+ throw new Exception('Unrecognized space type in tAttrSpace token');
+
+ break;
+ }
+ // offset: 3; size: 1; number of inserted spaces/carriage returns
+ $spacecount = ord($formulaData[3]);
+
+ $data = ['spacetype' => $spacetype, 'spacecount' => $spacecount];
+
+ break;
+ default:
+ throw new Exception('Unrecognized attribute flag in tAttr token');
+
+ break;
+ }
+
+ break;
+ case 0x1C: // error code
+ // offset: 1; size: 1; error code
+ $name = 'tErr';
+ $size = 2;
+ $data = Xls\ErrorCode::lookup(ord($formulaData[1]));
+
+ break;
+ case 0x1D: // boolean
+ // offset: 1; size: 1; 0 = false, 1 = true;
+ $name = 'tBool';
+ $size = 2;
+ $data = ord($formulaData[1]) ? 'TRUE' : 'FALSE';
+
+ break;
+ case 0x1E: // integer
+ // offset: 1; size: 2; unsigned 16-bit integer
+ $name = 'tInt';
+ $size = 3;
+ $data = self::getUInt2d($formulaData, 1);
+
+ break;
+ case 0x1F: // number
+ // offset: 1; size: 8;
+ $name = 'tNum';
+ $size = 9;
+ $data = self::extractNumber(substr($formulaData, 1));
+ $data = str_replace(',', '.', (string) $data); // in case non-English locale
+ break;
+ case 0x20: // array constant
+ case 0x40:
+ case 0x60:
+ // offset: 1; size: 7; not used
+ $name = 'tArray';
+ $size = 8;
+ $data = null;
+
+ break;
+ case 0x21: // function with fixed number of arguments
+ case 0x41:
+ case 0x61:
+ $name = 'tFunc';
+ $size = 3;
+ // offset: 1; size: 2; index to built-in sheet function
+ switch (self::getUInt2d($formulaData, 1)) {
+ case 2:
+ $function = 'ISNA';
+ $args = 1;
+
+ break;
+ case 3:
+ $function = 'ISERROR';
+ $args = 1;
+
+ break;
+ case 10:
+ $function = 'NA';
+ $args = 0;
+
+ break;
+ case 15:
+ $function = 'SIN';
+ $args = 1;
+
+ break;
+ case 16:
+ $function = 'COS';
+ $args = 1;
+
+ break;
+ case 17:
+ $function = 'TAN';
+ $args = 1;
+
+ break;
+ case 18:
+ $function = 'ATAN';
+ $args = 1;
+
+ break;
+ case 19:
+ $function = 'PI';
+ $args = 0;
+
+ break;
+ case 20:
+ $function = 'SQRT';
+ $args = 1;
+
+ break;
+ case 21:
+ $function = 'EXP';
+ $args = 1;
+
+ break;
+ case 22:
+ $function = 'LN';
+ $args = 1;
+
+ break;
+ case 23:
+ $function = 'LOG10';
+ $args = 1;
+
+ break;
+ case 24:
+ $function = 'ABS';
+ $args = 1;
+
+ break;
+ case 25:
+ $function = 'INT';
+ $args = 1;
+
+ break;
+ case 26:
+ $function = 'SIGN';
+ $args = 1;
+
+ break;
+ case 27:
+ $function = 'ROUND';
+ $args = 2;
+
+ break;
+ case 30:
+ $function = 'REPT';
+ $args = 2;
+
+ break;
+ case 31:
+ $function = 'MID';
+ $args = 3;
+
+ break;
+ case 32:
+ $function = 'LEN';
+ $args = 1;
+
+ break;
+ case 33:
+ $function = 'VALUE';
+ $args = 1;
+
+ break;
+ case 34:
+ $function = 'TRUE';
+ $args = 0;
+
+ break;
+ case 35:
+ $function = 'FALSE';
+ $args = 0;
+
+ break;
+ case 38:
+ $function = 'NOT';
+ $args = 1;
+
+ break;
+ case 39:
+ $function = 'MOD';
+ $args = 2;
+
+ break;
+ case 40:
+ $function = 'DCOUNT';
+ $args = 3;
+
+ break;
+ case 41:
+ $function = 'DSUM';
+ $args = 3;
+
+ break;
+ case 42:
+ $function = 'DAVERAGE';
+ $args = 3;
+
+ break;
+ case 43:
+ $function = 'DMIN';
+ $args = 3;
+
+ break;
+ case 44:
+ $function = 'DMAX';
+ $args = 3;
+
+ break;
+ case 45:
+ $function = 'DSTDEV';
+ $args = 3;
+
+ break;
+ case 48:
+ $function = 'TEXT';
+ $args = 2;
+
+ break;
+ case 61:
+ $function = 'MIRR';
+ $args = 3;
+
+ break;
+ case 63:
+ $function = 'RAND';
+ $args = 0;
+
+ break;
+ case 65:
+ $function = 'DATE';
+ $args = 3;
+
+ break;
+ case 66:
+ $function = 'TIME';
+ $args = 3;
+
+ break;
+ case 67:
+ $function = 'DAY';
+ $args = 1;
+
+ break;
+ case 68:
+ $function = 'MONTH';
+ $args = 1;
+
+ break;
+ case 69:
+ $function = 'YEAR';
+ $args = 1;
+
+ break;
+ case 71:
+ $function = 'HOUR';
+ $args = 1;
+
+ break;
+ case 72:
+ $function = 'MINUTE';
+ $args = 1;
+
+ break;
+ case 73:
+ $function = 'SECOND';
+ $args = 1;
+
+ break;
+ case 74:
+ $function = 'NOW';
+ $args = 0;
+
+ break;
+ case 75:
+ $function = 'AREAS';
+ $args = 1;
+
+ break;
+ case 76:
+ $function = 'ROWS';
+ $args = 1;
+
+ break;
+ case 77:
+ $function = 'COLUMNS';
+ $args = 1;
+
+ break;
+ case 83:
+ $function = 'TRANSPOSE';
+ $args = 1;
+
+ break;
+ case 86:
+ $function = 'TYPE';
+ $args = 1;
+
+ break;
+ case 97:
+ $function = 'ATAN2';
+ $args = 2;
+
+ break;
+ case 98:
+ $function = 'ASIN';
+ $args = 1;
+
+ break;
+ case 99:
+ $function = 'ACOS';
+ $args = 1;
+
+ break;
+ case 105:
+ $function = 'ISREF';
+ $args = 1;
+
+ break;
+ case 111:
+ $function = 'CHAR';
+ $args = 1;
+
+ break;
+ case 112:
+ $function = 'LOWER';
+ $args = 1;
+
+ break;
+ case 113:
+ $function = 'UPPER';
+ $args = 1;
+
+ break;
+ case 114:
+ $function = 'PROPER';
+ $args = 1;
+
+ break;
+ case 117:
+ $function = 'EXACT';
+ $args = 2;
+
+ break;
+ case 118:
+ $function = 'TRIM';
+ $args = 1;
+
+ break;
+ case 119:
+ $function = 'REPLACE';
+ $args = 4;
+
+ break;
+ case 121:
+ $function = 'CODE';
+ $args = 1;
+
+ break;
+ case 126:
+ $function = 'ISERR';
+ $args = 1;
+
+ break;
+ case 127:
+ $function = 'ISTEXT';
+ $args = 1;
+
+ break;
+ case 128:
+ $function = 'ISNUMBER';
+ $args = 1;
+
+ break;
+ case 129:
+ $function = 'ISBLANK';
+ $args = 1;
+
+ break;
+ case 130:
+ $function = 'T';
+ $args = 1;
+
+ break;
+ case 131:
+ $function = 'N';
+ $args = 1;
+
+ break;
+ case 140:
+ $function = 'DATEVALUE';
+ $args = 1;
+
+ break;
+ case 141:
+ $function = 'TIMEVALUE';
+ $args = 1;
+
+ break;
+ case 142:
+ $function = 'SLN';
+ $args = 3;
+
+ break;
+ case 143:
+ $function = 'SYD';
+ $args = 4;
+
+ break;
+ case 162:
+ $function = 'CLEAN';
+ $args = 1;
+
+ break;
+ case 163:
+ $function = 'MDETERM';
+ $args = 1;
+
+ break;
+ case 164:
+ $function = 'MINVERSE';
+ $args = 1;
+
+ break;
+ case 165:
+ $function = 'MMULT';
+ $args = 2;
+
+ break;
+ case 184:
+ $function = 'FACT';
+ $args = 1;
+
+ break;
+ case 189:
+ $function = 'DPRODUCT';
+ $args = 3;
+
+ break;
+ case 190:
+ $function = 'ISNONTEXT';
+ $args = 1;
+
+ break;
+ case 195:
+ $function = 'DSTDEVP';
+ $args = 3;
+
+ break;
+ case 196:
+ $function = 'DVARP';
+ $args = 3;
+
+ break;
+ case 198:
+ $function = 'ISLOGICAL';
+ $args = 1;
+
+ break;
+ case 199:
+ $function = 'DCOUNTA';
+ $args = 3;
+
+ break;
+ case 207:
+ $function = 'REPLACEB';
+ $args = 4;
+
+ break;
+ case 210:
+ $function = 'MIDB';
+ $args = 3;
+
+ break;
+ case 211:
+ $function = 'LENB';
+ $args = 1;
+
+ break;
+ case 212:
+ $function = 'ROUNDUP';
+ $args = 2;
+
+ break;
+ case 213:
+ $function = 'ROUNDDOWN';
+ $args = 2;
+
+ break;
+ case 214:
+ $function = 'ASC';
+ $args = 1;
+
+ break;
+ case 215:
+ $function = 'DBCS';
+ $args = 1;
+
+ break;
+ case 221:
+ $function = 'TODAY';
+ $args = 0;
+
+ break;
+ case 229:
+ $function = 'SINH';
+ $args = 1;
+
+ break;
+ case 230:
+ $function = 'COSH';
+ $args = 1;
+
+ break;
+ case 231:
+ $function = 'TANH';
+ $args = 1;
+
+ break;
+ case 232:
+ $function = 'ASINH';
+ $args = 1;
+
+ break;
+ case 233:
+ $function = 'ACOSH';
+ $args = 1;
+
+ break;
+ case 234:
+ $function = 'ATANH';
+ $args = 1;
+
+ break;
+ case 235:
+ $function = 'DGET';
+ $args = 3;
+
+ break;
+ case 244:
+ $function = 'INFO';
+ $args = 1;
+
+ break;
+ case 252:
+ $function = 'FREQUENCY';
+ $args = 2;
+
+ break;
+ case 261:
+ $function = 'ERROR.TYPE';
+ $args = 1;
+
+ break;
+ case 271:
+ $function = 'GAMMALN';
+ $args = 1;
+
+ break;
+ case 273:
+ $function = 'BINOMDIST';
+ $args = 4;
+
+ break;
+ case 274:
+ $function = 'CHIDIST';
+ $args = 2;
+
+ break;
+ case 275:
+ $function = 'CHIINV';
+ $args = 2;
+
+ break;
+ case 276:
+ $function = 'COMBIN';
+ $args = 2;
+
+ break;
+ case 277:
+ $function = 'CONFIDENCE';
+ $args = 3;
+
+ break;
+ case 278:
+ $function = 'CRITBINOM';
+ $args = 3;
+
+ break;
+ case 279:
+ $function = 'EVEN';
+ $args = 1;
+
+ break;
+ case 280:
+ $function = 'EXPONDIST';
+ $args = 3;
+
+ break;
+ case 281:
+ $function = 'FDIST';
+ $args = 3;
+
+ break;
+ case 282:
+ $function = 'FINV';
+ $args = 3;
+
+ break;
+ case 283:
+ $function = 'FISHER';
+ $args = 1;
+
+ break;
+ case 284:
+ $function = 'FISHERINV';
+ $args = 1;
+
+ break;
+ case 285:
+ $function = 'FLOOR';
+ $args = 2;
+
+ break;
+ case 286:
+ $function = 'GAMMADIST';
+ $args = 4;
+
+ break;
+ case 287:
+ $function = 'GAMMAINV';
+ $args = 3;
+
+ break;
+ case 288:
+ $function = 'CEILING';
+ $args = 2;
+
+ break;
+ case 289:
+ $function = 'HYPGEOMDIST';
+ $args = 4;
+
+ break;
+ case 290:
+ $function = 'LOGNORMDIST';
+ $args = 3;
+
+ break;
+ case 291:
+ $function = 'LOGINV';
+ $args = 3;
+
+ break;
+ case 292:
+ $function = 'NEGBINOMDIST';
+ $args = 3;
+
+ break;
+ case 293:
+ $function = 'NORMDIST';
+ $args = 4;
+
+ break;
+ case 294:
+ $function = 'NORMSDIST';
+ $args = 1;
+
+ break;
+ case 295:
+ $function = 'NORMINV';
+ $args = 3;
+
+ break;
+ case 296:
+ $function = 'NORMSINV';
+ $args = 1;
+
+ break;
+ case 297:
+ $function = 'STANDARDIZE';
+ $args = 3;
+
+ break;
+ case 298:
+ $function = 'ODD';
+ $args = 1;
+
+ break;
+ case 299:
+ $function = 'PERMUT';
+ $args = 2;
+
+ break;
+ case 300:
+ $function = 'POISSON';
+ $args = 3;
+
+ break;
+ case 301:
+ $function = 'TDIST';
+ $args = 3;
+
+ break;
+ case 302:
+ $function = 'WEIBULL';
+ $args = 4;
+
+ break;
+ case 303:
+ $function = 'SUMXMY2';
+ $args = 2;
+
+ break;
+ case 304:
+ $function = 'SUMX2MY2';
+ $args = 2;
+
+ break;
+ case 305:
+ $function = 'SUMX2PY2';
+ $args = 2;
+
+ break;
+ case 306:
+ $function = 'CHITEST';
+ $args = 2;
+
+ break;
+ case 307:
+ $function = 'CORREL';
+ $args = 2;
+
+ break;
+ case 308:
+ $function = 'COVAR';
+ $args = 2;
+
+ break;
+ case 309:
+ $function = 'FORECAST';
+ $args = 3;
+
+ break;
+ case 310:
+ $function = 'FTEST';
+ $args = 2;
+
+ break;
+ case 311:
+ $function = 'INTERCEPT';
+ $args = 2;
+
+ break;
+ case 312:
+ $function = 'PEARSON';
+ $args = 2;
+
+ break;
+ case 313:
+ $function = 'RSQ';
+ $args = 2;
+
+ break;
+ case 314:
+ $function = 'STEYX';
+ $args = 2;
+
+ break;
+ case 315:
+ $function = 'SLOPE';
+ $args = 2;
+
+ break;
+ case 316:
+ $function = 'TTEST';
+ $args = 4;
+
+ break;
+ case 325:
+ $function = 'LARGE';
+ $args = 2;
+
+ break;
+ case 326:
+ $function = 'SMALL';
+ $args = 2;
+
+ break;
+ case 327:
+ $function = 'QUARTILE';
+ $args = 2;
+
+ break;
+ case 328:
+ $function = 'PERCENTILE';
+ $args = 2;
+
+ break;
+ case 331:
+ $function = 'TRIMMEAN';
+ $args = 2;
+
+ break;
+ case 332:
+ $function = 'TINV';
+ $args = 2;
+
+ break;
+ case 337:
+ $function = 'POWER';
+ $args = 2;
+
+ break;
+ case 342:
+ $function = 'RADIANS';
+ $args = 1;
+
+ break;
+ case 343:
+ $function = 'DEGREES';
+ $args = 1;
+
+ break;
+ case 346:
+ $function = 'COUNTIF';
+ $args = 2;
+
+ break;
+ case 347:
+ $function = 'COUNTBLANK';
+ $args = 1;
+
+ break;
+ case 350:
+ $function = 'ISPMT';
+ $args = 4;
+
+ break;
+ case 351:
+ $function = 'DATEDIF';
+ $args = 3;
+
+ break;
+ case 352:
+ $function = 'DATESTRING';
+ $args = 1;
+
+ break;
+ case 353:
+ $function = 'NUMBERSTRING';
+ $args = 2;
+
+ break;
+ case 360:
+ $function = 'PHONETIC';
+ $args = 1;
+
+ break;
+ case 368:
+ $function = 'BAHTTEXT';
+ $args = 1;
+
+ break;
+ default:
+ throw new Exception('Unrecognized function in formula');
+
+ break;
+ }
+ $data = ['function' => $function, 'args' => $args];
+
+ break;
+ case 0x22: // function with variable number of arguments
+ case 0x42:
+ case 0x62:
+ $name = 'tFuncV';
+ $size = 4;
+ // offset: 1; size: 1; number of arguments
+ $args = ord($formulaData[1]);
+ // offset: 2: size: 2; index to built-in sheet function
+ $index = self::getUInt2d($formulaData, 2);
+ switch ($index) {
+ case 0:
+ $function = 'COUNT';
+
+ break;
+ case 1:
+ $function = 'IF';
+
+ break;
+ case 4:
+ $function = 'SUM';
+
+ break;
+ case 5:
+ $function = 'AVERAGE';
+
+ break;
+ case 6:
+ $function = 'MIN';
+
+ break;
+ case 7:
+ $function = 'MAX';
+
+ break;
+ case 8:
+ $function = 'ROW';
+
+ break;
+ case 9:
+ $function = 'COLUMN';
+
+ break;
+ case 11:
+ $function = 'NPV';
+
+ break;
+ case 12:
+ $function = 'STDEV';
+
+ break;
+ case 13:
+ $function = 'DOLLAR';
+
+ break;
+ case 14:
+ $function = 'FIXED';
+
+ break;
+ case 28:
+ $function = 'LOOKUP';
+
+ break;
+ case 29:
+ $function = 'INDEX';
+
+ break;
+ case 36:
+ $function = 'AND';
+
+ break;
+ case 37:
+ $function = 'OR';
+
+ break;
+ case 46:
+ $function = 'VAR';
+
+ break;
+ case 49:
+ $function = 'LINEST';
+
+ break;
+ case 50:
+ $function = 'TREND';
+
+ break;
+ case 51:
+ $function = 'LOGEST';
+
+ break;
+ case 52:
+ $function = 'GROWTH';
+
+ break;
+ case 56:
+ $function = 'PV';
+
+ break;
+ case 57:
+ $function = 'FV';
+
+ break;
+ case 58:
+ $function = 'NPER';
+
+ break;
+ case 59:
+ $function = 'PMT';
+
+ break;
+ case 60:
+ $function = 'RATE';
+
+ break;
+ case 62:
+ $function = 'IRR';
+
+ break;
+ case 64:
+ $function = 'MATCH';
+
+ break;
+ case 70:
+ $function = 'WEEKDAY';
+
+ break;
+ case 78:
+ $function = 'OFFSET';
+
+ break;
+ case 82:
+ $function = 'SEARCH';
+
+ break;
+ case 100:
+ $function = 'CHOOSE';
+
+ break;
+ case 101:
+ $function = 'HLOOKUP';
+
+ break;
+ case 102:
+ $function = 'VLOOKUP';
+
+ break;
+ case 109:
+ $function = 'LOG';
+
+ break;
+ case 115:
+ $function = 'LEFT';
+
+ break;
+ case 116:
+ $function = 'RIGHT';
+
+ break;
+ case 120:
+ $function = 'SUBSTITUTE';
+
+ break;
+ case 124:
+ $function = 'FIND';
+
+ break;
+ case 125:
+ $function = 'CELL';
+
+ break;
+ case 144:
+ $function = 'DDB';
+
+ break;
+ case 148:
+ $function = 'INDIRECT';
+
+ break;
+ case 167:
+ $function = 'IPMT';
+
+ break;
+ case 168:
+ $function = 'PPMT';
+
+ break;
+ case 169:
+ $function = 'COUNTA';
+
+ break;
+ case 183:
+ $function = 'PRODUCT';
+
+ break;
+ case 193:
+ $function = 'STDEVP';
+
+ break;
+ case 194:
+ $function = 'VARP';
+
+ break;
+ case 197:
+ $function = 'TRUNC';
+
+ break;
+ case 204:
+ $function = 'USDOLLAR';
+
+ break;
+ case 205:
+ $function = 'FINDB';
+
+ break;
+ case 206:
+ $function = 'SEARCHB';
+
+ break;
+ case 208:
+ $function = 'LEFTB';
+
+ break;
+ case 209:
+ $function = 'RIGHTB';
+
+ break;
+ case 216:
+ $function = 'RANK';
+
+ break;
+ case 219:
+ $function = 'ADDRESS';
+
+ break;
+ case 220:
+ $function = 'DAYS360';
+
+ break;
+ case 222:
+ $function = 'VDB';
+
+ break;
+ case 227:
+ $function = 'MEDIAN';
+
+ break;
+ case 228:
+ $function = 'SUMPRODUCT';
+
+ break;
+ case 247:
+ $function = 'DB';
+
+ break;
+ case 255:
+ $function = '';
+
+ break;
+ case 269:
+ $function = 'AVEDEV';
+
+ break;
+ case 270:
+ $function = 'BETADIST';
+
+ break;
+ case 272:
+ $function = 'BETAINV';
+
+ break;
+ case 317:
+ $function = 'PROB';
+
+ break;
+ case 318:
+ $function = 'DEVSQ';
+
+ break;
+ case 319:
+ $function = 'GEOMEAN';
+
+ break;
+ case 320:
+ $function = 'HARMEAN';
+
+ break;
+ case 321:
+ $function = 'SUMSQ';
+
+ break;
+ case 322:
+ $function = 'KURT';
+
+ break;
+ case 323:
+ $function = 'SKEW';
+
+ break;
+ case 324:
+ $function = 'ZTEST';
+
+ break;
+ case 329:
+ $function = 'PERCENTRANK';
+
+ break;
+ case 330:
+ $function = 'MODE';
+
+ break;
+ case 336:
+ $function = 'CONCATENATE';
+
+ break;
+ case 344:
+ $function = 'SUBTOTAL';
+
+ break;
+ case 345:
+ $function = 'SUMIF';
+
+ break;
+ case 354:
+ $function = 'ROMAN';
+
+ break;
+ case 358:
+ $function = 'GETPIVOTDATA';
+
+ break;
+ case 359:
+ $function = 'HYPERLINK';
+
+ break;
+ case 361:
+ $function = 'AVERAGEA';
+
+ break;
+ case 362:
+ $function = 'MAXA';
+
+ break;
+ case 363:
+ $function = 'MINA';
+
+ break;
+ case 364:
+ $function = 'STDEVPA';
+
+ break;
+ case 365:
+ $function = 'VARPA';
+
+ break;
+ case 366:
+ $function = 'STDEVA';
+
+ break;
+ case 367:
+ $function = 'VARA';
+
+ break;
+ default:
+ throw new Exception('Unrecognized function in formula');
+
+ break;
+ }
+ $data = ['function' => $function, 'args' => $args];
+
+ break;
+ case 0x23: // index to defined name
+ case 0x43:
+ case 0x63:
+ $name = 'tName';
+ $size = 5;
+ // offset: 1; size: 2; one-based index to definedname record
+ $definedNameIndex = self::getUInt2d($formulaData, 1) - 1;
+ // offset: 2; size: 2; not used
+ $data = $this->definedname[$definedNameIndex]['name'];
+
+ break;
+ case 0x24: // single cell reference e.g. A5
+ case 0x44:
+ case 0x64:
+ $name = 'tRef';
+ $size = 5;
+ $data = $this->readBIFF8CellAddress(substr($formulaData, 1, 4));
+
+ break;
+ case 0x25: // cell range reference to cells in the same sheet (2d)
+ case 0x45:
+ case 0x65:
+ $name = 'tArea';
+ $size = 9;
+ $data = $this->readBIFF8CellRangeAddress(substr($formulaData, 1, 8));
+
+ break;
+ case 0x26: // Constant reference sub-expression
+ case 0x46:
+ case 0x66:
+ $name = 'tMemArea';
+ // offset: 1; size: 4; not used
+ // offset: 5; size: 2; size of the following subexpression
+ $subSize = self::getUInt2d($formulaData, 5);
+ $size = 7 + $subSize;
+ $data = $this->getFormulaFromData(substr($formulaData, 7, $subSize));
+
+ break;
+ case 0x27: // Deleted constant reference sub-expression
+ case 0x47:
+ case 0x67:
+ $name = 'tMemErr';
+ // offset: 1; size: 4; not used
+ // offset: 5; size: 2; size of the following subexpression
+ $subSize = self::getUInt2d($formulaData, 5);
+ $size = 7 + $subSize;
+ $data = $this->getFormulaFromData(substr($formulaData, 7, $subSize));
+
+ break;
+ case 0x29: // Variable reference sub-expression
+ case 0x49:
+ case 0x69:
+ $name = 'tMemFunc';
+ // offset: 1; size: 2; size of the following sub-expression
+ $subSize = self::getUInt2d($formulaData, 1);
+ $size = 3 + $subSize;
+ $data = $this->getFormulaFromData(substr($formulaData, 3, $subSize));
+
+ break;
+ case 0x2C: // Relative 2d cell reference reference, used in shared formulas and some other places
+ case 0x4C:
+ case 0x6C:
+ $name = 'tRefN';
+ $size = 5;
+ $data = $this->readBIFF8CellAddressB(substr($formulaData, 1, 4), $baseCell);
+
+ break;
+ case 0x2D: // Relative 2d range reference
+ case 0x4D:
+ case 0x6D:
+ $name = 'tAreaN';
+ $size = 9;
+ $data = $this->readBIFF8CellRangeAddressB(substr($formulaData, 1, 8), $baseCell);
+
+ break;
+ case 0x39: // External name
+ case 0x59:
+ case 0x79:
+ $name = 'tNameX';
+ $size = 7;
+ // offset: 1; size: 2; index to REF entry in EXTERNSHEET record
+ // offset: 3; size: 2; one-based index to DEFINEDNAME or EXTERNNAME record
+ $index = self::getUInt2d($formulaData, 3);
+ // assume index is to EXTERNNAME record
+ $data = $this->externalNames[$index - 1]['name'];
+ // offset: 5; size: 2; not used
+ break;
+ case 0x3A: // 3d reference to cell
+ case 0x5A:
+ case 0x7A:
+ $name = 'tRef3d';
+ $size = 7;
+
+ try {
+ // offset: 1; size: 2; index to REF entry
+ $sheetRange = $this->readSheetRangeByRefIndex(self::getUInt2d($formulaData, 1));
+ // offset: 3; size: 4; cell address
+ $cellAddress = $this->readBIFF8CellAddress(substr($formulaData, 3, 4));
+
+ $data = "$sheetRange!$cellAddress";
+ } catch (PhpSpreadsheetException $e) {
+ // deleted sheet reference
+ $data = '#REF!';
+ }
+
+ break;
+ case 0x3B: // 3d reference to cell range
+ case 0x5B:
+ case 0x7B:
+ $name = 'tArea3d';
+ $size = 11;
+
+ try {
+ // offset: 1; size: 2; index to REF entry
+ $sheetRange = $this->readSheetRangeByRefIndex(self::getUInt2d($formulaData, 1));
+ // offset: 3; size: 8; cell address
+ $cellRangeAddress = $this->readBIFF8CellRangeAddress(substr($formulaData, 3, 8));
+
+ $data = "$sheetRange!$cellRangeAddress";
+ } catch (PhpSpreadsheetException $e) {
+ // deleted sheet reference
+ $data = '#REF!';
+ }
+
+ break;
+ // Unknown cases // don't know how to deal with
+ default:
+ throw new Exception('Unrecognized token ' . sprintf('%02X', $id) . ' in formula');
+
+ break;
+ }
+
+ return [
+ 'id' => $id,
+ 'name' => $name,
+ 'size' => $size,
+ 'data' => $data,
+ ];
+ }
+
+ /**
+ * Reads a cell address in BIFF8 e.g. 'A2' or '$A$2'
+ * section 3.3.4.
+ *
+ * @param string $cellAddressStructure
+ *
+ * @return string
+ */
+ private function readBIFF8CellAddress($cellAddressStructure)
+ {
+ // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
+ $row = self::getUInt2d($cellAddressStructure, 0) + 1;
+
+ // offset: 2; size: 2; index to column or column offset + relative flags
+ // bit: 7-0; mask 0x00FF; column index
+ $column = Coordinate::stringFromColumnIndex((0x00FF & self::getUInt2d($cellAddressStructure, 2)) + 1);
+
+ // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
+ if (!(0x4000 & self::getUInt2d($cellAddressStructure, 2))) {
+ $column = '$' . $column;
+ }
+ // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
+ if (!(0x8000 & self::getUInt2d($cellAddressStructure, 2))) {
+ $row = '$' . $row;
+ }
+
+ return $column . $row;
+ }
+
+ /**
+ * Reads a cell address in BIFF8 for shared formulas. Uses positive and negative values for row and column
+ * to indicate offsets from a base cell
+ * section 3.3.4.
+ *
+ * @param string $cellAddressStructure
+ * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
+ *
+ * @return string
+ */
+ private function readBIFF8CellAddressB($cellAddressStructure, $baseCell = 'A1')
+ {
+ list($baseCol, $baseRow) = Coordinate::coordinateFromString($baseCell);
+ $baseCol = Coordinate::columnIndexFromString($baseCol) - 1;
+
+ // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
+ $rowIndex = self::getUInt2d($cellAddressStructure, 0);
+ $row = self::getUInt2d($cellAddressStructure, 0) + 1;
+
+ // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
+ if (!(0x4000 & self::getUInt2d($cellAddressStructure, 2))) {
+ // offset: 2; size: 2; index to column or column offset + relative flags
+ // bit: 7-0; mask 0x00FF; column index
+ $colIndex = 0x00FF & self::getUInt2d($cellAddressStructure, 2);
+
+ $column = Coordinate::stringFromColumnIndex($colIndex + 1);
+ $column = '$' . $column;
+ } else {
+ // offset: 2; size: 2; index to column or column offset + relative flags
+ // bit: 7-0; mask 0x00FF; column index
+ $relativeColIndex = 0x00FF & self::getInt2d($cellAddressStructure, 2);
+ $colIndex = $baseCol + $relativeColIndex;
+ $colIndex = ($colIndex < 256) ? $colIndex : $colIndex - 256;
+ $colIndex = ($colIndex >= 0) ? $colIndex : $colIndex + 256;
+ $column = Coordinate::stringFromColumnIndex($colIndex + 1);
+ }
+
+ // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
+ if (!(0x8000 & self::getUInt2d($cellAddressStructure, 2))) {
+ $row = '$' . $row;
+ } else {
+ $rowIndex = ($rowIndex <= 32767) ? $rowIndex : $rowIndex - 65536;
+ $row = $baseRow + $rowIndex;
+ }
+
+ return $column . $row;
+ }
+
+ /**
+ * Reads a cell range address in BIFF5 e.g. 'A2:B6' or 'A1'
+ * always fixed range
+ * section 2.5.14.
+ *
+ * @param string $subData
+ *
+ * @throws Exception
+ *
+ * @return string
+ */
+ private function readBIFF5CellRangeAddressFixed($subData)
+ {
+ // offset: 0; size: 2; index to first row
+ $fr = self::getUInt2d($subData, 0) + 1;
+
+ // offset: 2; size: 2; index to last row
+ $lr = self::getUInt2d($subData, 2) + 1;
+
+ // offset: 4; size: 1; index to first column
+ $fc = ord($subData[4]);
+
+ // offset: 5; size: 1; index to last column
+ $lc = ord($subData[5]);
+
+ // check values
+ if ($fr > $lr || $fc > $lc) {
+ throw new Exception('Not a cell range address');
+ }
+
+ // column index to letter
+ $fc = Coordinate::stringFromColumnIndex($fc + 1);
+ $lc = Coordinate::stringFromColumnIndex($lc + 1);
+
+ if ($fr == $lr and $fc == $lc) {
+ return "$fc$fr";
+ }
+
+ return "$fc$fr:$lc$lr";
+ }
+
+ /**
+ * Reads a cell range address in BIFF8 e.g. 'A2:B6' or 'A1'
+ * always fixed range
+ * section 2.5.14.
+ *
+ * @param string $subData
+ *
+ * @throws Exception
+ *
+ * @return string
+ */
+ private function readBIFF8CellRangeAddressFixed($subData)
+ {
+ // offset: 0; size: 2; index to first row
+ $fr = self::getUInt2d($subData, 0) + 1;
+
+ // offset: 2; size: 2; index to last row
+ $lr = self::getUInt2d($subData, 2) + 1;
+
+ // offset: 4; size: 2; index to first column
+ $fc = self::getUInt2d($subData, 4);
+
+ // offset: 6; size: 2; index to last column
+ $lc = self::getUInt2d($subData, 6);
+
+ // check values
+ if ($fr > $lr || $fc > $lc) {
+ throw new Exception('Not a cell range address');
+ }
+
+ // column index to letter
+ $fc = Coordinate::stringFromColumnIndex($fc + 1);
+ $lc = Coordinate::stringFromColumnIndex($lc + 1);
+
+ if ($fr == $lr and $fc == $lc) {
+ return "$fc$fr";
+ }
+
+ return "$fc$fr:$lc$lr";
+ }
+
+ /**
+ * Reads a cell range address in BIFF8 e.g. 'A2:B6' or '$A$2:$B$6'
+ * there are flags indicating whether column/row index is relative
+ * section 3.3.4.
+ *
+ * @param string $subData
+ *
+ * @return string
+ */
+ private function readBIFF8CellRangeAddress($subData)
+ {
+ // todo: if cell range is just a single cell, should this funciton
+ // not just return e.g. 'A1' and not 'A1:A1' ?
+
+ // offset: 0; size: 2; index to first row (0... 65535) (or offset (-32768... 32767))
+ $fr = self::getUInt2d($subData, 0) + 1;
+
+ // offset: 2; size: 2; index to last row (0... 65535) (or offset (-32768... 32767))
+ $lr = self::getUInt2d($subData, 2) + 1;
+
+ // offset: 4; size: 2; index to first column or column offset + relative flags
+
+ // bit: 7-0; mask 0x00FF; column index
+ $fc = Coordinate::stringFromColumnIndex((0x00FF & self::getUInt2d($subData, 4)) + 1);
+
+ // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
+ if (!(0x4000 & self::getUInt2d($subData, 4))) {
+ $fc = '$' . $fc;
+ }
+
+ // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
+ if (!(0x8000 & self::getUInt2d($subData, 4))) {
+ $fr = '$' . $fr;
+ }
+
+ // offset: 6; size: 2; index to last column or column offset + relative flags
+
+ // bit: 7-0; mask 0x00FF; column index
+ $lc = Coordinate::stringFromColumnIndex((0x00FF & self::getUInt2d($subData, 6)) + 1);
+
+ // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
+ if (!(0x4000 & self::getUInt2d($subData, 6))) {
+ $lc = '$' . $lc;
+ }
+
+ // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
+ if (!(0x8000 & self::getUInt2d($subData, 6))) {
+ $lr = '$' . $lr;
+ }
+
+ return "$fc$fr:$lc$lr";
+ }
+
+ /**
+ * Reads a cell range address in BIFF8 for shared formulas. Uses positive and negative values for row and column
+ * to indicate offsets from a base cell
+ * section 3.3.4.
+ *
+ * @param string $subData
+ * @param string $baseCell Base cell
+ *
+ * @return string Cell range address
+ */
+ private function readBIFF8CellRangeAddressB($subData, $baseCell = 'A1')
+ {
+ list($baseCol, $baseRow) = Coordinate::coordinateFromString($baseCell);
+ $baseCol = Coordinate::columnIndexFromString($baseCol) - 1;
+
+ // TODO: if cell range is just a single cell, should this funciton
+ // not just return e.g. 'A1' and not 'A1:A1' ?
+
+ // offset: 0; size: 2; first row
+ $frIndex = self::getUInt2d($subData, 0); // adjust below
+
+ // offset: 2; size: 2; relative index to first row (0... 65535) should be treated as offset (-32768... 32767)
+ $lrIndex = self::getUInt2d($subData, 2); // adjust below
+
+ // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
+ if (!(0x4000 & self::getUInt2d($subData, 4))) {
+ // absolute column index
+ // offset: 4; size: 2; first column with relative/absolute flags
+ // bit: 7-0; mask 0x00FF; column index
+ $fcIndex = 0x00FF & self::getUInt2d($subData, 4);
+ $fc = Coordinate::stringFromColumnIndex($fcIndex + 1);
+ $fc = '$' . $fc;
+ } else {
+ // column offset
+ // offset: 4; size: 2; first column with relative/absolute flags
+ // bit: 7-0; mask 0x00FF; column index
+ $relativeFcIndex = 0x00FF & self::getInt2d($subData, 4);
+ $fcIndex = $baseCol + $relativeFcIndex;
+ $fcIndex = ($fcIndex < 256) ? $fcIndex : $fcIndex - 256;
+ $fcIndex = ($fcIndex >= 0) ? $fcIndex : $fcIndex + 256;
+ $fc = Coordinate::stringFromColumnIndex($fcIndex + 1);
+ }
+
+ // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
+ if (!(0x8000 & self::getUInt2d($subData, 4))) {
+ // absolute row index
+ $fr = $frIndex + 1;
+ $fr = '$' . $fr;
+ } else {
+ // row offset
+ $frIndex = ($frIndex <= 32767) ? $frIndex : $frIndex - 65536;
+ $fr = $baseRow + $frIndex;
+ }
+
+ // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
+ if (!(0x4000 & self::getUInt2d($subData, 6))) {
+ // absolute column index
+ // offset: 6; size: 2; last column with relative/absolute flags
+ // bit: 7-0; mask 0x00FF; column index
+ $lcIndex = 0x00FF & self::getUInt2d($subData, 6);
+ $lc = Coordinate::stringFromColumnIndex($lcIndex + 1);
+ $lc = '$' . $lc;
+ } else {
+ // column offset
+ // offset: 4; size: 2; first column with relative/absolute flags
+ // bit: 7-0; mask 0x00FF; column index
+ $relativeLcIndex = 0x00FF & self::getInt2d($subData, 4);
+ $lcIndex = $baseCol + $relativeLcIndex;
+ $lcIndex = ($lcIndex < 256) ? $lcIndex : $lcIndex - 256;
+ $lcIndex = ($lcIndex >= 0) ? $lcIndex : $lcIndex + 256;
+ $lc = Coordinate::stringFromColumnIndex($lcIndex + 1);
+ }
+
+ // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
+ if (!(0x8000 & self::getUInt2d($subData, 6))) {
+ // absolute row index
+ $lr = $lrIndex + 1;
+ $lr = '$' . $lr;
+ } else {
+ // row offset
+ $lrIndex = ($lrIndex <= 32767) ? $lrIndex : $lrIndex - 65536;
+ $lr = $baseRow + $lrIndex;
+ }
+
+ return "$fc$fr:$lc$lr";
+ }
+
+ /**
+ * Read BIFF8 cell range address list
+ * section 2.5.15.
+ *
+ * @param string $subData
+ *
+ * @return array
+ */
+ private function readBIFF8CellRangeAddressList($subData)
+ {
+ $cellRangeAddresses = [];
+
+ // offset: 0; size: 2; number of the following cell range addresses
+ $nm = self::getUInt2d($subData, 0);
+
+ $offset = 2;
+ // offset: 2; size: 8 * $nm; list of $nm (fixed) cell range addresses
+ for ($i = 0; $i < $nm; ++$i) {
+ $cellRangeAddresses[] = $this->readBIFF8CellRangeAddressFixed(substr($subData, $offset, 8));
+ $offset += 8;
+ }
+
+ return [
+ 'size' => 2 + 8 * $nm,
+ 'cellRangeAddresses' => $cellRangeAddresses,
+ ];
+ }
+
+ /**
+ * Read BIFF5 cell range address list
+ * section 2.5.15.
+ *
+ * @param string $subData
+ *
+ * @return array
+ */
+ private function readBIFF5CellRangeAddressList($subData)
+ {
+ $cellRangeAddresses = [];
+
+ // offset: 0; size: 2; number of the following cell range addresses
+ $nm = self::getUInt2d($subData, 0);
+
+ $offset = 2;
+ // offset: 2; size: 6 * $nm; list of $nm (fixed) cell range addresses
+ for ($i = 0; $i < $nm; ++$i) {
+ $cellRangeAddresses[] = $this->readBIFF5CellRangeAddressFixed(substr($subData, $offset, 6));
+ $offset += 6;
+ }
+
+ return [
+ 'size' => 2 + 6 * $nm,
+ 'cellRangeAddresses' => $cellRangeAddresses,
+ ];
+ }
+
+ /**
+ * Get a sheet range like Sheet1:Sheet3 from REF index
+ * Note: If there is only one sheet in the range, one gets e.g Sheet1
+ * It can also happen that the REF structure uses the -1 (FFFF) code to indicate deleted sheets,
+ * in which case an Exception is thrown.
+ *
+ * @param int $index
+ *
+ * @throws Exception
+ *
+ * @return false|string
+ */
+ private function readSheetRangeByRefIndex($index)
+ {
+ if (isset($this->ref[$index])) {
+ $type = $this->externalBooks[$this->ref[$index]['externalBookIndex']]['type'];
+
+ switch ($type) {
+ case 'internal':
+ // check if we have a deleted 3d reference
+ if ($this->ref[$index]['firstSheetIndex'] == 0xFFFF or $this->ref[$index]['lastSheetIndex'] == 0xFFFF) {
+ throw new Exception('Deleted sheet reference');
+ }
+
+ // we have normal sheet range (collapsed or uncollapsed)
+ $firstSheetName = $this->sheets[$this->ref[$index]['firstSheetIndex']]['name'];
+ $lastSheetName = $this->sheets[$this->ref[$index]['lastSheetIndex']]['name'];
+
+ if ($firstSheetName == $lastSheetName) {
+ // collapsed sheet range
+ $sheetRange = $firstSheetName;
+ } else {
+ $sheetRange = "$firstSheetName:$lastSheetName";
+ }
+
+ // escape the single-quotes
+ $sheetRange = str_replace("'", "''", $sheetRange);
+
+ // if there are special characters, we need to enclose the range in single-quotes
+ // todo: check if we have identified the whole set of special characters
+ // it seems that the following characters are not accepted for sheet names
+ // and we may assume that they are not present: []*/:\?
+ if (preg_match("/[ !\"@#£$%&{()}<>=+'|^,;-]/u", $sheetRange)) {
+ $sheetRange = "'$sheetRange'";
+ }
+
+ return $sheetRange;
+
+ break;
+ default:
+ // TODO: external sheet support
+ throw new Exception('Xls reader only supports internal sheets in formulas');
+
+ break;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * read BIFF8 constant value array from array data
+ * returns e.g. ['value' => '{1,2;3,4}', 'size' => 40]
+ * section 2.5.8.
+ *
+ * @param string $arrayData
+ *
+ * @return array
+ */
+ private static function readBIFF8ConstantArray($arrayData)
+ {
+ // offset: 0; size: 1; number of columns decreased by 1
+ $nc = ord($arrayData[0]);
+
+ // offset: 1; size: 2; number of rows decreased by 1
+ $nr = self::getUInt2d($arrayData, 1);
+ $size = 3; // initialize
+ $arrayData = substr($arrayData, 3);
+
+ // offset: 3; size: var; list of ($nc + 1) * ($nr + 1) constant values
+ $matrixChunks = [];
+ for ($r = 1; $r <= $nr + 1; ++$r) {
+ $items = [];
+ for ($c = 1; $c <= $nc + 1; ++$c) {
+ $constant = self::readBIFF8Constant($arrayData);
+ $items[] = $constant['value'];
+ $arrayData = substr($arrayData, $constant['size']);
+ $size += $constant['size'];
+ }
+ $matrixChunks[] = implode(',', $items); // looks like e.g. '1,"hello"'
+ }
+ $matrix = '{' . implode(';', $matrixChunks) . '}';
+
+ return [
+ 'value' => $matrix,
+ 'size' => $size,
+ ];
+ }
+
+ /**
+ * read BIFF8 constant value which may be 'Empty Value', 'Number', 'String Value', 'Boolean Value', 'Error Value'
+ * section 2.5.7
+ * returns e.g. ['value' => '5', 'size' => 9].
+ *
+ * @param string $valueData
+ *
+ * @return array
+ */
+ private static function readBIFF8Constant($valueData)
+ {
+ // offset: 0; size: 1; identifier for type of constant
+ $identifier = ord($valueData[0]);
+
+ switch ($identifier) {
+ case 0x00: // empty constant (what is this?)
+ $value = '';
+ $size = 9;
+
+ break;
+ case 0x01: // number
+ // offset: 1; size: 8; IEEE 754 floating-point value
+ $value = self::extractNumber(substr($valueData, 1, 8));
+ $size = 9;
+
+ break;
+ case 0x02: // string value
+ // offset: 1; size: var; Unicode string, 16-bit string length
+ $string = self::readUnicodeStringLong(substr($valueData, 1));
+ $value = '"' . $string['value'] . '"';
+ $size = 1 + $string['size'];
+
+ break;
+ case 0x04: // boolean
+ // offset: 1; size: 1; 0 = FALSE, 1 = TRUE
+ if (ord($valueData[1])) {
+ $value = 'TRUE';
+ } else {
+ $value = 'FALSE';
+ }
+ $size = 9;
+
+ break;
+ case 0x10: // error code
+ // offset: 1; size: 1; error code
+ $value = Xls\ErrorCode::lookup(ord($valueData[1]));
+ $size = 9;
+
+ break;
+ }
+
+ return [
+ 'value' => $value,
+ 'size' => $size,
+ ];
+ }
+
+ /**
+ * Extract RGB color
+ * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.4.
+ *
+ * @param string $rgb Encoded RGB value (4 bytes)
+ *
+ * @return array
+ */
+ private static function readRGB($rgb)
+ {
+ // offset: 0; size 1; Red component
+ $r = ord($rgb[0]);
+
+ // offset: 1; size: 1; Green component
+ $g = ord($rgb[1]);
+
+ // offset: 2; size: 1; Blue component
+ $b = ord($rgb[2]);
+
+ // HEX notation, e.g. 'FF00FC'
+ $rgb = sprintf('%02X%02X%02X', $r, $g, $b);
+
+ return ['rgb' => $rgb];
+ }
+
+ /**
+ * Read byte string (8-bit string length)
+ * OpenOffice documentation: 2.5.2.
+ *
+ * @param string $subData
+ *
+ * @return array
+ */
+ private function readByteStringShort($subData)
+ {
+ // offset: 0; size: 1; length of the string (character count)
+ $ln = ord($subData[0]);
+
+ // offset: 1: size: var; character array (8-bit characters)
+ $value = $this->decodeCodepage(substr($subData, 1, $ln));
+
+ return [
+ 'value' => $value,
+ 'size' => 1 + $ln, // size in bytes of data structure
+ ];
+ }
+
+ /**
+ * Read byte string (16-bit string length)
+ * OpenOffice documentation: 2.5.2.
+ *
+ * @param string $subData
+ *
+ * @return array
+ */
+ private function readByteStringLong($subData)
+ {
+ // offset: 0; size: 2; length of the string (character count)
+ $ln = self::getUInt2d($subData, 0);
+
+ // offset: 2: size: var; character array (8-bit characters)
+ $value = $this->decodeCodepage(substr($subData, 2));
+
+ //return $string;
+ return [
+ 'value' => $value,
+ 'size' => 2 + $ln, // size in bytes of data structure
+ ];
+ }
+
+ /**
+ * Extracts an Excel Unicode short string (8-bit string length)
+ * OpenOffice documentation: 2.5.3
+ * function will automatically find out where the Unicode string ends.
+ *
+ * @param string $subData
+ *
+ * @return array
+ */
+ private static function readUnicodeStringShort($subData)
+ {
+ $value = '';
+
+ // offset: 0: size: 1; length of the string (character count)
+ $characterCount = ord($subData[0]);
+
+ $string = self::readUnicodeString(substr($subData, 1), $characterCount);
+
+ // add 1 for the string length
+ $string['size'] += 1;
+
+ return $string;
+ }
+
+ /**
+ * Extracts an Excel Unicode long string (16-bit string length)
+ * OpenOffice documentation: 2.5.3
+ * this function is under construction, needs to support rich text, and Asian phonetic settings.
+ *
+ * @param string $subData
+ *
+ * @return array
+ */
+ private static function readUnicodeStringLong($subData)
+ {
+ $value = '';
+
+ // offset: 0: size: 2; length of the string (character count)
+ $characterCount = self::getUInt2d($subData, 0);
+
+ $string = self::readUnicodeString(substr($subData, 2), $characterCount);
+
+ // add 2 for the string length
+ $string['size'] += 2;
+
+ return $string;
+ }
+
+ /**
+ * Read Unicode string with no string length field, but with known character count
+ * this function is under construction, needs to support rich text, and Asian phonetic settings
+ * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.3.
+ *
+ * @param string $subData
+ * @param int $characterCount
+ *
+ * @return array
+ */
+ private static function readUnicodeString($subData, $characterCount)
+ {
+ $value = '';
+
+ // offset: 0: size: 1; option flags
+ // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
+ $isCompressed = !((0x01 & ord($subData[0])) >> 0);
+
+ // bit: 2; mask: 0x04; Asian phonetic settings
+ $hasAsian = (0x04) & ord($subData[0]) >> 2;
+
+ // bit: 3; mask: 0x08; Rich-Text settings
+ $hasRichText = (0x08) & ord($subData[0]) >> 3;
+
+ // offset: 1: size: var; character array
+ // this offset assumes richtext and Asian phonetic settings are off which is generally wrong
+ // needs to be fixed
+ $value = self::encodeUTF16(substr($subData, 1, $isCompressed ? $characterCount : 2 * $characterCount), $isCompressed);
+
+ return [
+ 'value' => $value,
+ 'size' => $isCompressed ? 1 + $characterCount : 1 + 2 * $characterCount, // the size in bytes including the option flags
+ ];
+ }
+
+ /**
+ * Convert UTF-8 string to string surounded by double quotes. Used for explicit string tokens in formulas.
+ * Example: hello"world --> "hello""world".
+ *
+ * @param string $value UTF-8 encoded string
+ *
+ * @return string
+ */
+ private static function UTF8toExcelDoubleQuoted($value)
+ {
+ return '"' . str_replace('"', '""', $value) . '"';
+ }
+
+ /**
+ * Reads first 8 bytes of a string and return IEEE 754 float.
+ *
+ * @param string $data Binary string that is at least 8 bytes long
+ *
+ * @return float
+ */
+ private static function extractNumber($data)
+ {
+ $rknumhigh = self::getInt4d($data, 4);
+ $rknumlow = self::getInt4d($data, 0);
+ $sign = ($rknumhigh & 0x80000000) >> 31;
+ $exp = (($rknumhigh & 0x7ff00000) >> 20) - 1023;
+ $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
+ $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
+ $mantissalow2 = ($rknumlow & 0x7fffffff);
+ $value = $mantissa / pow(2, (20 - $exp));
+
+ if ($mantissalow1 != 0) {
+ $value += 1 / pow(2, (21 - $exp));
+ }
+
+ $value += $mantissalow2 / pow(2, (52 - $exp));
+ if ($sign) {
+ $value *= -1;
+ }
+
+ return $value;
+ }
+
+ /**
+ * @param int $rknum
+ *
+ * @return float
+ */
+ private static function getIEEE754($rknum)
+ {
+ if (($rknum & 0x02) != 0) {
+ $value = $rknum >> 2;
+ } else {
+ // changes by mmp, info on IEEE754 encoding from
+ // research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
+ // The RK format calls for using only the most significant 30 bits
+ // of the 64 bit floating point value. The other 34 bits are assumed
+ // to be 0 so we use the upper 30 bits of $rknum as follows...
+ $sign = ($rknum & 0x80000000) >> 31;
+ $exp = ($rknum & 0x7ff00000) >> 20;
+ $mantissa = (0x100000 | ($rknum & 0x000ffffc));
+ $value = $mantissa / pow(2, (20 - ($exp - 1023)));
+ if ($sign) {
+ $value = -1 * $value;
+ }
+ //end of changes by mmp
+ }
+ if (($rknum & 0x01) != 0) {
+ $value /= 100;
+ }
+
+ return $value;
+ }
+
+ /**
+ * Get UTF-8 string from (compressed or uncompressed) UTF-16 string.
+ *
+ * @param string $string
+ * @param bool $compressed
+ *
+ * @return string
+ */
+ private static function encodeUTF16($string, $compressed = false)
+ {
+ if ($compressed) {
+ $string = self::uncompressByteString($string);
+ }
+
+ return StringHelper::convertEncoding($string, 'UTF-8', 'UTF-16LE');
+ }
+
+ /**
+ * Convert UTF-16 string in compressed notation to uncompressed form. Only used for BIFF8.
+ *
+ * @param string $string
+ *
+ * @return string
+ */
+ private static function uncompressByteString($string)
+ {
+ $uncompressedString = '';
+ $strLen = strlen($string);
+ for ($i = 0; $i < $strLen; ++$i) {
+ $uncompressedString .= $string[$i] . "\0";
+ }
+
+ return $uncompressedString;
+ }
+
+ /**
+ * Convert string to UTF-8. Only used for BIFF5.
+ *
+ * @param string $string
+ *
+ * @return string
+ */
+ private function decodeCodepage($string)
+ {
+ return StringHelper::convertEncoding($string, 'UTF-8', $this->codepage);
+ }
+
+ /**
+ * Read 16-bit unsigned integer.
+ *
+ * @param string $data
+ * @param int $pos
+ *
+ * @return int
+ */
+ public static function getUInt2d($data, $pos)
+ {
+ return ord($data[$pos]) | (ord($data[$pos + 1]) << 8);
+ }
+
+ /**
+ * Read 16-bit signed integer.
+ *
+ * @param string $data
+ * @param int $pos
+ *
+ * @return int
+ */
+ public static function getInt2d($data, $pos)
+ {
+ return unpack('s', $data[$pos] . $data[$pos + 1])[1];
+ }
+
+ /**
+ * Read 32-bit signed integer.
+ *
+ * @param string $data
+ * @param int $pos
+ *
+ * @return int
+ */
+ public static function getInt4d($data, $pos)
+ {
+ // FIX: represent numbers correctly on 64-bit system
+ // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
+ // Changed by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
+ $_or_24 = ord($data[$pos + 3]);
+ if ($_or_24 >= 128) {
+ // negative number
+ $_ord_24 = -abs((256 - $_or_24) << 24);
+ } else {
+ $_ord_24 = ($_or_24 & 127) << 24;
+ }
+
+ return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $_ord_24;
+ }
+
+ private function parseRichText($is)
+ {
+ $value = new RichText();
+ $value->createText($is);
+
+ return $value;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color.php
new file mode 100644
index 00000000000..c45f88c72b5
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color.php
@@ -0,0 +1,36 @@
+ 'FF0000']
+ */
+ public static function map($color, $palette, $version)
+ {
+ if ($color <= 0x07 || $color >= 0x40) {
+ // special built-in color
+ return Color\BuiltIn::lookup($color);
+ } elseif (isset($palette, $palette[$color - 8])) {
+ // palette color, color index 0x08 maps to pallete index 0
+ return $palette[$color - 8];
+ }
+
+ // default color table
+ if ($version == Xls::XLS_BIFF8) {
+ return Color\BIFF8::lookup($color);
+ }
+
+ // BIFF5
+ return Color\BIFF5::lookup($color);
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color/BIFF5.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color/BIFF5.php
new file mode 100644
index 00000000000..743d938773e
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color/BIFF5.php
@@ -0,0 +1,81 @@
+ '000000',
+ 0x09 => 'FFFFFF',
+ 0x0A => 'FF0000',
+ 0x0B => '00FF00',
+ 0x0C => '0000FF',
+ 0x0D => 'FFFF00',
+ 0x0E => 'FF00FF',
+ 0x0F => '00FFFF',
+ 0x10 => '800000',
+ 0x11 => '008000',
+ 0x12 => '000080',
+ 0x13 => '808000',
+ 0x14 => '800080',
+ 0x15 => '008080',
+ 0x16 => 'C0C0C0',
+ 0x17 => '808080',
+ 0x18 => '8080FF',
+ 0x19 => '802060',
+ 0x1A => 'FFFFC0',
+ 0x1B => 'A0E0F0',
+ 0x1C => '600080',
+ 0x1D => 'FF8080',
+ 0x1E => '0080C0',
+ 0x1F => 'C0C0FF',
+ 0x20 => '000080',
+ 0x21 => 'FF00FF',
+ 0x22 => 'FFFF00',
+ 0x23 => '00FFFF',
+ 0x24 => '800080',
+ 0x25 => '800000',
+ 0x26 => '008080',
+ 0x27 => '0000FF',
+ 0x28 => '00CFFF',
+ 0x29 => '69FFFF',
+ 0x2A => 'E0FFE0',
+ 0x2B => 'FFFF80',
+ 0x2C => 'A6CAF0',
+ 0x2D => 'DD9CB3',
+ 0x2E => 'B38FEE',
+ 0x2F => 'E3E3E3',
+ 0x30 => '2A6FF9',
+ 0x31 => '3FB8CD',
+ 0x32 => '488436',
+ 0x33 => '958C41',
+ 0x34 => '8E5E42',
+ 0x35 => 'A0627A',
+ 0x36 => '624FAC',
+ 0x37 => '969696',
+ 0x38 => '1D2FBE',
+ 0x39 => '286676',
+ 0x3A => '004500',
+ 0x3B => '453E01',
+ 0x3C => '6A2813',
+ 0x3D => '85396A',
+ 0x3E => '4A3285',
+ 0x3F => '424242',
+ ];
+
+ /**
+ * Map color array from BIFF5 built-in color index.
+ *
+ * @param int $color
+ *
+ * @return array
+ */
+ public static function lookup($color)
+ {
+ if (isset(self::$map[$color])) {
+ return ['rgb' => self::$map[$color]];
+ }
+
+ return ['rgb' => '000000'];
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color/BIFF8.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color/BIFF8.php
new file mode 100644
index 00000000000..5c109fb071f
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color/BIFF8.php
@@ -0,0 +1,81 @@
+ '000000',
+ 0x09 => 'FFFFFF',
+ 0x0A => 'FF0000',
+ 0x0B => '00FF00',
+ 0x0C => '0000FF',
+ 0x0D => 'FFFF00',
+ 0x0E => 'FF00FF',
+ 0x0F => '00FFFF',
+ 0x10 => '800000',
+ 0x11 => '008000',
+ 0x12 => '000080',
+ 0x13 => '808000',
+ 0x14 => '800080',
+ 0x15 => '008080',
+ 0x16 => 'C0C0C0',
+ 0x17 => '808080',
+ 0x18 => '9999FF',
+ 0x19 => '993366',
+ 0x1A => 'FFFFCC',
+ 0x1B => 'CCFFFF',
+ 0x1C => '660066',
+ 0x1D => 'FF8080',
+ 0x1E => '0066CC',
+ 0x1F => 'CCCCFF',
+ 0x20 => '000080',
+ 0x21 => 'FF00FF',
+ 0x22 => 'FFFF00',
+ 0x23 => '00FFFF',
+ 0x24 => '800080',
+ 0x25 => '800000',
+ 0x26 => '008080',
+ 0x27 => '0000FF',
+ 0x28 => '00CCFF',
+ 0x29 => 'CCFFFF',
+ 0x2A => 'CCFFCC',
+ 0x2B => 'FFFF99',
+ 0x2C => '99CCFF',
+ 0x2D => 'FF99CC',
+ 0x2E => 'CC99FF',
+ 0x2F => 'FFCC99',
+ 0x30 => '3366FF',
+ 0x31 => '33CCCC',
+ 0x32 => '99CC00',
+ 0x33 => 'FFCC00',
+ 0x34 => 'FF9900',
+ 0x35 => 'FF6600',
+ 0x36 => '666699',
+ 0x37 => '969696',
+ 0x38 => '003366',
+ 0x39 => '339966',
+ 0x3A => '003300',
+ 0x3B => '333300',
+ 0x3C => '993300',
+ 0x3D => '993366',
+ 0x3E => '333399',
+ 0x3F => '333333',
+ ];
+
+ /**
+ * Map color array from BIFF8 built-in color index.
+ *
+ * @param int $color
+ *
+ * @return array
+ */
+ public static function lookup($color)
+ {
+ if (isset(self::$map[$color])) {
+ return ['rgb' => self::$map[$color]];
+ }
+
+ return ['rgb' => '000000'];
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color/BuiltIn.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color/BuiltIn.php
new file mode 100644
index 00000000000..90d50e336bb
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Color/BuiltIn.php
@@ -0,0 +1,35 @@
+ '000000',
+ 0x01 => 'FFFFFF',
+ 0x02 => 'FF0000',
+ 0x03 => '00FF00',
+ 0x04 => '0000FF',
+ 0x05 => 'FFFF00',
+ 0x06 => 'FF00FF',
+ 0x07 => '00FFFF',
+ 0x40 => '000000', // system window text color
+ 0x41 => 'FFFFFF', // system window background color
+ ];
+
+ /**
+ * Map built-in color to RGB value.
+ *
+ * @param int $color Indexed color
+ *
+ * @return array
+ */
+ public static function lookup($color)
+ {
+ if (isset(self::$map[$color])) {
+ return ['rgb' => self::$map[$color]];
+ }
+
+ return ['rgb' => '000000'];
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/ErrorCode.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/ErrorCode.php
new file mode 100644
index 00000000000..7daf7230ff0
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/ErrorCode.php
@@ -0,0 +1,32 @@
+ '#NULL!',
+ 0x07 => '#DIV/0!',
+ 0x0F => '#VALUE!',
+ 0x17 => '#REF!',
+ 0x1D => '#NAME?',
+ 0x24 => '#NUM!',
+ 0x2A => '#N/A',
+ ];
+
+ /**
+ * Map error code, e.g. '#N/A'.
+ *
+ * @param int $code
+ *
+ * @return bool|string
+ */
+ public static function lookup($code)
+ {
+ if (isset(self::$map[$code])) {
+ return self::$map[$code];
+ }
+
+ return false;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Escher.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Escher.php
new file mode 100644
index 00000000000..858d6bbbac4
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Escher.php
@@ -0,0 +1,677 @@
+object = $object;
+ }
+
+ /**
+ * Load Escher stream data. May be a partial Escher stream.
+ *
+ * @param string $data
+ *
+ * @return BSE|BstoreContainer|DgContainer|DggContainer|\PhpOffice\PhpSpreadsheet\Shared\Escher|SpContainer|SpgrContainer
+ */
+ public function load($data)
+ {
+ $this->data = $data;
+
+ // total byte size of Excel data (workbook global substream + sheet substreams)
+ $this->dataSize = strlen($this->data);
+
+ $this->pos = 0;
+
+ // Parse Escher stream
+ while ($this->pos < $this->dataSize) {
+ // offset: 2; size: 2: Record Type
+ $fbt = Xls::getUInt2d($this->data, $this->pos + 2);
+
+ switch ($fbt) {
+ case self::DGGCONTAINER:
+ $this->readDggContainer();
+
+ break;
+ case self::DGG:
+ $this->readDgg();
+
+ break;
+ case self::BSTORECONTAINER:
+ $this->readBstoreContainer();
+
+ break;
+ case self::BSE:
+ $this->readBSE();
+
+ break;
+ case self::BLIPJPEG:
+ $this->readBlipJPEG();
+
+ break;
+ case self::BLIPPNG:
+ $this->readBlipPNG();
+
+ break;
+ case self::OPT:
+ $this->readOPT();
+
+ break;
+ case self::TERTIARYOPT:
+ $this->readTertiaryOPT();
+
+ break;
+ case self::SPLITMENUCOLORS:
+ $this->readSplitMenuColors();
+
+ break;
+ case self::DGCONTAINER:
+ $this->readDgContainer();
+
+ break;
+ case self::DG:
+ $this->readDg();
+
+ break;
+ case self::SPGRCONTAINER:
+ $this->readSpgrContainer();
+
+ break;
+ case self::SPCONTAINER:
+ $this->readSpContainer();
+
+ break;
+ case self::SPGR:
+ $this->readSpgr();
+
+ break;
+ case self::SP:
+ $this->readSp();
+
+ break;
+ case self::CLIENTTEXTBOX:
+ $this->readClientTextbox();
+
+ break;
+ case self::CLIENTANCHOR:
+ $this->readClientAnchor();
+
+ break;
+ case self::CLIENTDATA:
+ $this->readClientData();
+
+ break;
+ default:
+ $this->readDefault();
+
+ break;
+ }
+ }
+
+ return $this->object;
+ }
+
+ /**
+ * Read a generic record.
+ */
+ private function readDefault()
+ {
+ // offset 0; size: 2; recVer and recInstance
+ $verInstance = Xls::getUInt2d($this->data, $this->pos);
+
+ // offset: 2; size: 2: Record Type
+ $fbt = Xls::getUInt2d($this->data, $this->pos + 2);
+
+ // bit: 0-3; mask: 0x000F; recVer
+ $recVer = (0x000F & $verInstance) >> 0;
+
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+ }
+
+ /**
+ * Read DggContainer record (Drawing Group Container).
+ */
+ private function readDggContainer()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ // record is a container, read contents
+ $dggContainer = new DggContainer();
+ $this->object->setDggContainer($dggContainer);
+ $reader = new self($dggContainer);
+ $reader->load($recordData);
+ }
+
+ /**
+ * Read Dgg record (Drawing Group).
+ */
+ private function readDgg()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+ }
+
+ /**
+ * Read BstoreContainer record (Blip Store Container).
+ */
+ private function readBstoreContainer()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ // record is a container, read contents
+ $bstoreContainer = new BstoreContainer();
+ $this->object->setBstoreContainer($bstoreContainer);
+ $reader = new self($bstoreContainer);
+ $reader->load($recordData);
+ }
+
+ /**
+ * Read BSE record.
+ */
+ private function readBSE()
+ {
+ // offset: 0; size: 2; recVer and recInstance
+
+ // bit: 4-15; mask: 0xFFF0; recInstance
+ $recInstance = (0xFFF0 & Xls::getUInt2d($this->data, $this->pos)) >> 4;
+
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ // add BSE to BstoreContainer
+ $BSE = new BSE();
+ $this->object->addBSE($BSE);
+
+ $BSE->setBLIPType($recInstance);
+
+ // offset: 0; size: 1; btWin32 (MSOBLIPTYPE)
+ $btWin32 = ord($recordData[0]);
+
+ // offset: 1; size: 1; btWin32 (MSOBLIPTYPE)
+ $btMacOS = ord($recordData[1]);
+
+ // offset: 2; size: 16; MD4 digest
+ $rgbUid = substr($recordData, 2, 16);
+
+ // offset: 18; size: 2; tag
+ $tag = Xls::getUInt2d($recordData, 18);
+
+ // offset: 20; size: 4; size of BLIP in bytes
+ $size = Xls::getInt4d($recordData, 20);
+
+ // offset: 24; size: 4; number of references to this BLIP
+ $cRef = Xls::getInt4d($recordData, 24);
+
+ // offset: 28; size: 4; MSOFO file offset
+ $foDelay = Xls::getInt4d($recordData, 28);
+
+ // offset: 32; size: 1; unused1
+ $unused1 = ord($recordData[32]);
+
+ // offset: 33; size: 1; size of nameData in bytes (including null terminator)
+ $cbName = ord($recordData[33]);
+
+ // offset: 34; size: 1; unused2
+ $unused2 = ord($recordData[34]);
+
+ // offset: 35; size: 1; unused3
+ $unused3 = ord($recordData[35]);
+
+ // offset: 36; size: $cbName; nameData
+ $nameData = substr($recordData, 36, $cbName);
+
+ // offset: 36 + $cbName, size: var; the BLIP data
+ $blipData = substr($recordData, 36 + $cbName);
+
+ // record is a container, read contents
+ $reader = new self($BSE);
+ $reader->load($blipData);
+ }
+
+ /**
+ * Read BlipJPEG record. Holds raw JPEG image data.
+ */
+ private function readBlipJPEG()
+ {
+ // offset: 0; size: 2; recVer and recInstance
+
+ // bit: 4-15; mask: 0xFFF0; recInstance
+ $recInstance = (0xFFF0 & Xls::getUInt2d($this->data, $this->pos)) >> 4;
+
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ $pos = 0;
+
+ // offset: 0; size: 16; rgbUid1 (MD4 digest of)
+ $rgbUid1 = substr($recordData, 0, 16);
+ $pos += 16;
+
+ // offset: 16; size: 16; rgbUid2 (MD4 digest), only if $recInstance = 0x46B or 0x6E3
+ if (in_array($recInstance, [0x046B, 0x06E3])) {
+ $rgbUid2 = substr($recordData, 16, 16);
+ $pos += 16;
+ }
+
+ // offset: var; size: 1; tag
+ $tag = ord($recordData[$pos]);
+ $pos += 1;
+
+ // offset: var; size: var; the raw image data
+ $data = substr($recordData, $pos);
+
+ $blip = new Blip();
+ $blip->setData($data);
+
+ $this->object->setBlip($blip);
+ }
+
+ /**
+ * Read BlipPNG record. Holds raw PNG image data.
+ */
+ private function readBlipPNG()
+ {
+ // offset: 0; size: 2; recVer and recInstance
+
+ // bit: 4-15; mask: 0xFFF0; recInstance
+ $recInstance = (0xFFF0 & Xls::getUInt2d($this->data, $this->pos)) >> 4;
+
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ $pos = 0;
+
+ // offset: 0; size: 16; rgbUid1 (MD4 digest of)
+ $rgbUid1 = substr($recordData, 0, 16);
+ $pos += 16;
+
+ // offset: 16; size: 16; rgbUid2 (MD4 digest), only if $recInstance = 0x46B or 0x6E3
+ if ($recInstance == 0x06E1) {
+ $rgbUid2 = substr($recordData, 16, 16);
+ $pos += 16;
+ }
+
+ // offset: var; size: 1; tag
+ $tag = ord($recordData[$pos]);
+ $pos += 1;
+
+ // offset: var; size: var; the raw image data
+ $data = substr($recordData, $pos);
+
+ $blip = new Blip();
+ $blip->setData($data);
+
+ $this->object->setBlip($blip);
+ }
+
+ /**
+ * Read OPT record. This record may occur within DggContainer record or SpContainer.
+ */
+ private function readOPT()
+ {
+ // offset: 0; size: 2; recVer and recInstance
+
+ // bit: 4-15; mask: 0xFFF0; recInstance
+ $recInstance = (0xFFF0 & Xls::getUInt2d($this->data, $this->pos)) >> 4;
+
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ $this->readOfficeArtRGFOPTE($recordData, $recInstance);
+ }
+
+ /**
+ * Read TertiaryOPT record.
+ */
+ private function readTertiaryOPT()
+ {
+ // offset: 0; size: 2; recVer and recInstance
+
+ // bit: 4-15; mask: 0xFFF0; recInstance
+ $recInstance = (0xFFF0 & Xls::getUInt2d($this->data, $this->pos)) >> 4;
+
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+ }
+
+ /**
+ * Read SplitMenuColors record.
+ */
+ private function readSplitMenuColors()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+ }
+
+ /**
+ * Read DgContainer record (Drawing Container).
+ */
+ private function readDgContainer()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ // record is a container, read contents
+ $dgContainer = new DgContainer();
+ $this->object->setDgContainer($dgContainer);
+ $reader = new self($dgContainer);
+ $escher = $reader->load($recordData);
+ }
+
+ /**
+ * Read Dg record (Drawing).
+ */
+ private function readDg()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+ }
+
+ /**
+ * Read SpgrContainer record (Shape Group Container).
+ */
+ private function readSpgrContainer()
+ {
+ // context is either context DgContainer or SpgrContainer
+
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ // record is a container, read contents
+ $spgrContainer = new SpgrContainer();
+
+ if ($this->object instanceof DgContainer) {
+ // DgContainer
+ $this->object->setSpgrContainer($spgrContainer);
+ } else {
+ // SpgrContainer
+ $this->object->addChild($spgrContainer);
+ }
+
+ $reader = new self($spgrContainer);
+ $escher = $reader->load($recordData);
+ }
+
+ /**
+ * Read SpContainer record (Shape Container).
+ */
+ private function readSpContainer()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // add spContainer to spgrContainer
+ $spContainer = new SpContainer();
+ $this->object->addChild($spContainer);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ // record is a container, read contents
+ $reader = new self($spContainer);
+ $escher = $reader->load($recordData);
+ }
+
+ /**
+ * Read Spgr record (Shape Group).
+ */
+ private function readSpgr()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+ }
+
+ /**
+ * Read Sp record (Shape).
+ */
+ private function readSp()
+ {
+ // offset: 0; size: 2; recVer and recInstance
+
+ // bit: 4-15; mask: 0xFFF0; recInstance
+ $recInstance = (0xFFF0 & Xls::getUInt2d($this->data, $this->pos)) >> 4;
+
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+ }
+
+ /**
+ * Read ClientTextbox record.
+ */
+ private function readClientTextbox()
+ {
+ // offset: 0; size: 2; recVer and recInstance
+
+ // bit: 4-15; mask: 0xFFF0; recInstance
+ $recInstance = (0xFFF0 & Xls::getUInt2d($this->data, $this->pos)) >> 4;
+
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+ }
+
+ /**
+ * Read ClientAnchor record. This record holds information about where the shape is anchored in worksheet.
+ */
+ private function readClientAnchor()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+
+ // offset: 2; size: 2; upper-left corner column index (0-based)
+ $c1 = Xls::getUInt2d($recordData, 2);
+
+ // offset: 4; size: 2; upper-left corner horizontal offset in 1/1024 of column width
+ $startOffsetX = Xls::getUInt2d($recordData, 4);
+
+ // offset: 6; size: 2; upper-left corner row index (0-based)
+ $r1 = Xls::getUInt2d($recordData, 6);
+
+ // offset: 8; size: 2; upper-left corner vertical offset in 1/256 of row height
+ $startOffsetY = Xls::getUInt2d($recordData, 8);
+
+ // offset: 10; size: 2; bottom-right corner column index (0-based)
+ $c2 = Xls::getUInt2d($recordData, 10);
+
+ // offset: 12; size: 2; bottom-right corner horizontal offset in 1/1024 of column width
+ $endOffsetX = Xls::getUInt2d($recordData, 12);
+
+ // offset: 14; size: 2; bottom-right corner row index (0-based)
+ $r2 = Xls::getUInt2d($recordData, 14);
+
+ // offset: 16; size: 2; bottom-right corner vertical offset in 1/256 of row height
+ $endOffsetY = Xls::getUInt2d($recordData, 16);
+
+ // set the start coordinates
+ $this->object->setStartCoordinates(Coordinate::stringFromColumnIndex($c1 + 1) . ($r1 + 1));
+
+ // set the start offsetX
+ $this->object->setStartOffsetX($startOffsetX);
+
+ // set the start offsetY
+ $this->object->setStartOffsetY($startOffsetY);
+
+ // set the end coordinates
+ $this->object->setEndCoordinates(Coordinate::stringFromColumnIndex($c2 + 1) . ($r2 + 1));
+
+ // set the end offsetX
+ $this->object->setEndOffsetX($endOffsetX);
+
+ // set the end offsetY
+ $this->object->setEndOffsetY($endOffsetY);
+ }
+
+ /**
+ * Read ClientData record.
+ */
+ private function readClientData()
+ {
+ $length = Xls::getInt4d($this->data, $this->pos + 4);
+ $recordData = substr($this->data, $this->pos + 8, $length);
+
+ // move stream pointer to next record
+ $this->pos += 8 + $length;
+ }
+
+ /**
+ * Read OfficeArtRGFOPTE table of property-value pairs.
+ *
+ * @param string $data Binary data
+ * @param int $n Number of properties
+ */
+ private function readOfficeArtRGFOPTE($data, $n)
+ {
+ $splicedComplexData = substr($data, 6 * $n);
+
+ // loop through property-value pairs
+ for ($i = 0; $i < $n; ++$i) {
+ // read 6 bytes at a time
+ $fopte = substr($data, 6 * $i, 6);
+
+ // offset: 0; size: 2; opid
+ $opid = Xls::getUInt2d($fopte, 0);
+
+ // bit: 0-13; mask: 0x3FFF; opid.opid
+ $opidOpid = (0x3FFF & $opid) >> 0;
+
+ // bit: 14; mask 0x4000; 1 = value in op field is BLIP identifier
+ $opidFBid = (0x4000 & $opid) >> 14;
+
+ // bit: 15; mask 0x8000; 1 = this is a complex property, op field specifies size of complex data
+ $opidFComplex = (0x8000 & $opid) >> 15;
+
+ // offset: 2; size: 4; the value for this property
+ $op = Xls::getInt4d($fopte, 2);
+
+ if ($opidFComplex) {
+ $complexData = substr($splicedComplexData, 0, $op);
+ $splicedComplexData = substr($splicedComplexData, $op);
+
+ // we store string value with complex data
+ $value = $complexData;
+ } else {
+ // we store integer value
+ $value = $op;
+ }
+
+ $this->object->setOPT($opidOpid, $value);
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/MD5.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/MD5.php
new file mode 100644
index 00000000000..6a10e591ecb
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/MD5.php
@@ -0,0 +1,184 @@
+reset();
+ }
+
+ /**
+ * Reset the MD5 stream context.
+ */
+ public function reset()
+ {
+ $this->a = 0x67452301;
+ $this->b = 0xEFCDAB89;
+ $this->c = 0x98BADCFE;
+ $this->d = 0x10325476;
+ }
+
+ /**
+ * Get MD5 stream context.
+ *
+ * @return string
+ */
+ public function getContext()
+ {
+ $s = '';
+ foreach (['a', 'b', 'c', 'd'] as $i) {
+ $v = $this->{$i};
+ $s .= chr($v & 0xff);
+ $s .= chr(($v >> 8) & 0xff);
+ $s .= chr(($v >> 16) & 0xff);
+ $s .= chr(($v >> 24) & 0xff);
+ }
+
+ return $s;
+ }
+
+ /**
+ * Add data to context.
+ *
+ * @param string $data Data to add
+ */
+ public function add($data)
+ {
+ $words = array_values(unpack('V16', $data));
+
+ $A = $this->a;
+ $B = $this->b;
+ $C = $this->c;
+ $D = $this->d;
+
+ $F = ['self', 'f'];
+ $G = ['self', 'g'];
+ $H = ['self', 'h'];
+ $I = ['self', 'i'];
+
+ // ROUND 1
+ self::step($F, $A, $B, $C, $D, $words[0], 7, 0xd76aa478);
+ self::step($F, $D, $A, $B, $C, $words[1], 12, 0xe8c7b756);
+ self::step($F, $C, $D, $A, $B, $words[2], 17, 0x242070db);
+ self::step($F, $B, $C, $D, $A, $words[3], 22, 0xc1bdceee);
+ self::step($F, $A, $B, $C, $D, $words[4], 7, 0xf57c0faf);
+ self::step($F, $D, $A, $B, $C, $words[5], 12, 0x4787c62a);
+ self::step($F, $C, $D, $A, $B, $words[6], 17, 0xa8304613);
+ self::step($F, $B, $C, $D, $A, $words[7], 22, 0xfd469501);
+ self::step($F, $A, $B, $C, $D, $words[8], 7, 0x698098d8);
+ self::step($F, $D, $A, $B, $C, $words[9], 12, 0x8b44f7af);
+ self::step($F, $C, $D, $A, $B, $words[10], 17, 0xffff5bb1);
+ self::step($F, $B, $C, $D, $A, $words[11], 22, 0x895cd7be);
+ self::step($F, $A, $B, $C, $D, $words[12], 7, 0x6b901122);
+ self::step($F, $D, $A, $B, $C, $words[13], 12, 0xfd987193);
+ self::step($F, $C, $D, $A, $B, $words[14], 17, 0xa679438e);
+ self::step($F, $B, $C, $D, $A, $words[15], 22, 0x49b40821);
+
+ // ROUND 2
+ self::step($G, $A, $B, $C, $D, $words[1], 5, 0xf61e2562);
+ self::step($G, $D, $A, $B, $C, $words[6], 9, 0xc040b340);
+ self::step($G, $C, $D, $A, $B, $words[11], 14, 0x265e5a51);
+ self::step($G, $B, $C, $D, $A, $words[0], 20, 0xe9b6c7aa);
+ self::step($G, $A, $B, $C, $D, $words[5], 5, 0xd62f105d);
+ self::step($G, $D, $A, $B, $C, $words[10], 9, 0x02441453);
+ self::step($G, $C, $D, $A, $B, $words[15], 14, 0xd8a1e681);
+ self::step($G, $B, $C, $D, $A, $words[4], 20, 0xe7d3fbc8);
+ self::step($G, $A, $B, $C, $D, $words[9], 5, 0x21e1cde6);
+ self::step($G, $D, $A, $B, $C, $words[14], 9, 0xc33707d6);
+ self::step($G, $C, $D, $A, $B, $words[3], 14, 0xf4d50d87);
+ self::step($G, $B, $C, $D, $A, $words[8], 20, 0x455a14ed);
+ self::step($G, $A, $B, $C, $D, $words[13], 5, 0xa9e3e905);
+ self::step($G, $D, $A, $B, $C, $words[2], 9, 0xfcefa3f8);
+ self::step($G, $C, $D, $A, $B, $words[7], 14, 0x676f02d9);
+ self::step($G, $B, $C, $D, $A, $words[12], 20, 0x8d2a4c8a);
+
+ // ROUND 3
+ self::step($H, $A, $B, $C, $D, $words[5], 4, 0xfffa3942);
+ self::step($H, $D, $A, $B, $C, $words[8], 11, 0x8771f681);
+ self::step($H, $C, $D, $A, $B, $words[11], 16, 0x6d9d6122);
+ self::step($H, $B, $C, $D, $A, $words[14], 23, 0xfde5380c);
+ self::step($H, $A, $B, $C, $D, $words[1], 4, 0xa4beea44);
+ self::step($H, $D, $A, $B, $C, $words[4], 11, 0x4bdecfa9);
+ self::step($H, $C, $D, $A, $B, $words[7], 16, 0xf6bb4b60);
+ self::step($H, $B, $C, $D, $A, $words[10], 23, 0xbebfbc70);
+ self::step($H, $A, $B, $C, $D, $words[13], 4, 0x289b7ec6);
+ self::step($H, $D, $A, $B, $C, $words[0], 11, 0xeaa127fa);
+ self::step($H, $C, $D, $A, $B, $words[3], 16, 0xd4ef3085);
+ self::step($H, $B, $C, $D, $A, $words[6], 23, 0x04881d05);
+ self::step($H, $A, $B, $C, $D, $words[9], 4, 0xd9d4d039);
+ self::step($H, $D, $A, $B, $C, $words[12], 11, 0xe6db99e5);
+ self::step($H, $C, $D, $A, $B, $words[15], 16, 0x1fa27cf8);
+ self::step($H, $B, $C, $D, $A, $words[2], 23, 0xc4ac5665);
+
+ // ROUND 4
+ self::step($I, $A, $B, $C, $D, $words[0], 6, 0xf4292244);
+ self::step($I, $D, $A, $B, $C, $words[7], 10, 0x432aff97);
+ self::step($I, $C, $D, $A, $B, $words[14], 15, 0xab9423a7);
+ self::step($I, $B, $C, $D, $A, $words[5], 21, 0xfc93a039);
+ self::step($I, $A, $B, $C, $D, $words[12], 6, 0x655b59c3);
+ self::step($I, $D, $A, $B, $C, $words[3], 10, 0x8f0ccc92);
+ self::step($I, $C, $D, $A, $B, $words[10], 15, 0xffeff47d);
+ self::step($I, $B, $C, $D, $A, $words[1], 21, 0x85845dd1);
+ self::step($I, $A, $B, $C, $D, $words[8], 6, 0x6fa87e4f);
+ self::step($I, $D, $A, $B, $C, $words[15], 10, 0xfe2ce6e0);
+ self::step($I, $C, $D, $A, $B, $words[6], 15, 0xa3014314);
+ self::step($I, $B, $C, $D, $A, $words[13], 21, 0x4e0811a1);
+ self::step($I, $A, $B, $C, $D, $words[4], 6, 0xf7537e82);
+ self::step($I, $D, $A, $B, $C, $words[11], 10, 0xbd3af235);
+ self::step($I, $C, $D, $A, $B, $words[2], 15, 0x2ad7d2bb);
+ self::step($I, $B, $C, $D, $A, $words[9], 21, 0xeb86d391);
+
+ $this->a = ($this->a + $A) & 0xffffffff;
+ $this->b = ($this->b + $B) & 0xffffffff;
+ $this->c = ($this->c + $C) & 0xffffffff;
+ $this->d = ($this->d + $D) & 0xffffffff;
+ }
+
+ private static function f($X, $Y, $Z)
+ {
+ return ($X & $Y) | ((~$X) & $Z); // X AND Y OR NOT X AND Z
+ }
+
+ private static function g($X, $Y, $Z)
+ {
+ return ($X & $Z) | ($Y & (~$Z)); // X AND Z OR Y AND NOT Z
+ }
+
+ private static function h($X, $Y, $Z)
+ {
+ return $X ^ $Y ^ $Z; // X XOR Y XOR Z
+ }
+
+ private static function i($X, $Y, $Z)
+ {
+ return $Y ^ ($X | (~$Z)); // Y XOR (X OR NOT Z)
+ }
+
+ private static function step($func, &$A, $B, $C, $D, $M, $s, $t)
+ {
+ $A = ($A + call_user_func($func, $B, $C, $D) + $M + $t) & 0xffffffff;
+ $A = self::rotate($A, $s);
+ $A = ($B + $A) & 0xffffffff;
+ }
+
+ private static function rotate($decimal, $bits)
+ {
+ $binary = str_pad(decbin($decimal), 32, '0', STR_PAD_LEFT);
+
+ return bindec(substr($binary, $bits) . substr($binary, 0, $bits));
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/RC4.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/RC4.php
new file mode 100644
index 00000000000..691aca7c398
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/RC4.php
@@ -0,0 +1,61 @@
+i = 0; $this->i < 256; ++$this->i) {
+ $this->s[$this->i] = $this->i;
+ }
+
+ $this->j = 0;
+ for ($this->i = 0; $this->i < 256; ++$this->i) {
+ $this->j = ($this->j + $this->s[$this->i] + ord($key[$this->i % $len])) % 256;
+ $t = $this->s[$this->i];
+ $this->s[$this->i] = $this->s[$this->j];
+ $this->s[$this->j] = $t;
+ }
+ $this->i = $this->j = 0;
+ }
+
+ /**
+ * Symmetric decryption/encryption function.
+ *
+ * @param string $data Data to encrypt/decrypt
+ *
+ * @return string
+ */
+ public function RC4($data)
+ {
+ $len = strlen($data);
+ for ($c = 0; $c < $len; ++$c) {
+ $this->i = ($this->i + 1) % 256;
+ $this->j = ($this->j + $this->s[$this->i]) % 256;
+ $t = $this->s[$this->i];
+ $this->s[$this->i] = $this->s[$this->j];
+ $this->s[$this->j] = $t;
+
+ $t = ($this->s[$this->i] + $this->s[$this->j]) % 256;
+
+ $data[$c] = chr(ord($data[$c]) ^ $this->s[$t]);
+ }
+
+ return $data;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Style/Border.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Style/Border.php
new file mode 100644
index 00000000000..91cbe36f136
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Style/Border.php
@@ -0,0 +1,42 @@
+ StyleBorder::BORDER_NONE,
+ 0x01 => StyleBorder::BORDER_THIN,
+ 0x02 => StyleBorder::BORDER_MEDIUM,
+ 0x03 => StyleBorder::BORDER_DASHED,
+ 0x04 => StyleBorder::BORDER_DOTTED,
+ 0x05 => StyleBorder::BORDER_THICK,
+ 0x06 => StyleBorder::BORDER_DOUBLE,
+ 0x07 => StyleBorder::BORDER_HAIR,
+ 0x08 => StyleBorder::BORDER_MEDIUMDASHED,
+ 0x09 => StyleBorder::BORDER_DASHDOT,
+ 0x0A => StyleBorder::BORDER_MEDIUMDASHDOT,
+ 0x0B => StyleBorder::BORDER_DASHDOTDOT,
+ 0x0C => StyleBorder::BORDER_MEDIUMDASHDOTDOT,
+ 0x0D => StyleBorder::BORDER_SLANTDASHDOT,
+ ];
+
+ /**
+ * Map border style
+ * OpenOffice documentation: 2.5.11.
+ *
+ * @param int $index
+ *
+ * @return string
+ */
+ public static function lookup($index)
+ {
+ if (isset(self::$map[$index])) {
+ return self::$map[$index];
+ }
+
+ return StyleBorder::BORDER_NONE;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Style/FillPattern.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Style/FillPattern.php
new file mode 100644
index 00000000000..7b85c088330
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xls/Style/FillPattern.php
@@ -0,0 +1,47 @@
+ Fill::FILL_NONE,
+ 0x01 => Fill::FILL_SOLID,
+ 0x02 => Fill::FILL_PATTERN_MEDIUMGRAY,
+ 0x03 => Fill::FILL_PATTERN_DARKGRAY,
+ 0x04 => Fill::FILL_PATTERN_LIGHTGRAY,
+ 0x05 => Fill::FILL_PATTERN_DARKHORIZONTAL,
+ 0x06 => Fill::FILL_PATTERN_DARKVERTICAL,
+ 0x07 => Fill::FILL_PATTERN_DARKDOWN,
+ 0x08 => Fill::FILL_PATTERN_DARKUP,
+ 0x09 => Fill::FILL_PATTERN_DARKGRID,
+ 0x0A => Fill::FILL_PATTERN_DARKTRELLIS,
+ 0x0B => Fill::FILL_PATTERN_LIGHTHORIZONTAL,
+ 0x0C => Fill::FILL_PATTERN_LIGHTVERTICAL,
+ 0x0D => Fill::FILL_PATTERN_LIGHTDOWN,
+ 0x0E => Fill::FILL_PATTERN_LIGHTUP,
+ 0x0F => Fill::FILL_PATTERN_LIGHTGRID,
+ 0x10 => Fill::FILL_PATTERN_LIGHTTRELLIS,
+ 0x11 => Fill::FILL_PATTERN_GRAY125,
+ 0x12 => Fill::FILL_PATTERN_GRAY0625,
+ ];
+
+ /**
+ * Get fill pattern from index
+ * OpenOffice documentation: 2.5.12.
+ *
+ * @param int $index
+ *
+ * @return string
+ */
+ public static function lookup($index)
+ {
+ if (isset(self::$map[$index])) {
+ return self::$map[$index];
+ }
+
+ return Fill::FILL_NONE;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xlsx.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xlsx.php
new file mode 100644
index 00000000000..335f5d7e99c
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xlsx.php
@@ -0,0 +1,2604 @@
+readFilter = new DefaultReadFilter();
+ $this->referenceHelper = ReferenceHelper::getInstance();
+ $this->securityScanner = XmlScanner::getInstance($this);
+ }
+
+ /**
+ * Can the current IReader read the file?
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return bool
+ */
+ public function canRead($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $xl = false;
+ // Load file
+ $zip = new ZipArchive();
+ if ($zip->open($pFilename) === true) {
+ // check if it is an OOXML archive
+ $rels = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, '_rels/.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ if ($rels !== false) {
+ foreach ($rels->Relationship as $rel) {
+ switch ($rel['Type']) {
+ case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument':
+ if (basename($rel['Target']) == 'workbook.xml') {
+ $xl = true;
+ }
+
+ break;
+ }
+ }
+ }
+ $zip->close();
+ }
+
+ return $xl;
+ }
+
+ /**
+ * Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ public function listWorksheetNames($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $worksheetNames = [];
+
+ $zip = new ZipArchive();
+ $zip->open($pFilename);
+
+ // The files we're looking at here are small enough that simpleXML is more efficient than XMLReader
+ //~ http://schemas.openxmlformats.org/package/2006/relationships");
+ $rels = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, '_rels/.rels'))
+ );
+ foreach ($rels->Relationship as $rel) {
+ switch ($rel['Type']) {
+ case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument':
+ //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main"
+ $xmlWorkbook = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}"))
+ );
+
+ if ($xmlWorkbook->sheets) {
+ foreach ($xmlWorkbook->sheets->sheet as $eleSheet) {
+ // Check if sheet should be skipped
+ $worksheetNames[] = (string) $eleSheet['name'];
+ }
+ }
+ }
+ }
+
+ $zip->close();
+
+ return $worksheetNames;
+ }
+
+ /**
+ * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ public function listWorksheetInfo($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $worksheetInfo = [];
+
+ $zip = new ZipArchive();
+ $zip->open($pFilename);
+
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $rels = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, '_rels/.rels')),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ foreach ($rels->Relationship as $rel) {
+ if ($rel['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument') {
+ $dir = dirname($rel['Target']);
+
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsWorkbook = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, "$dir/_rels/" . basename($rel['Target']) . '.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $relsWorkbook->registerXPathNamespace('rel', 'http://schemas.openxmlformats.org/package/2006/relationships');
+
+ $worksheets = [];
+ foreach ($relsWorkbook->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet') {
+ $worksheets[(string) $ele['Id']] = $ele['Target'];
+ }
+ }
+
+ //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main"
+ $xmlWorkbook = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, "{$rel['Target']}")
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ if ($xmlWorkbook->sheets) {
+ $dir = dirname($rel['Target']);
+ /** @var SimpleXMLElement $eleSheet */
+ foreach ($xmlWorkbook->sheets->sheet as $eleSheet) {
+ $tmpInfo = [
+ 'worksheetName' => (string) $eleSheet['name'],
+ 'lastColumnLetter' => 'A',
+ 'lastColumnIndex' => 0,
+ 'totalRows' => 0,
+ 'totalColumns' => 0,
+ ];
+
+ $fileWorksheet = $worksheets[(string) self::getArrayItem($eleSheet->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), 'id')];
+
+ $xml = new XMLReader();
+ $xml->xml(
+ $this->securityScanner->scanFile(
+ 'zip://' . File::realpath($pFilename) . '#' . "$dir/$fileWorksheet"
+ ),
+ null,
+ Settings::getLibXmlLoaderOptions()
+ );
+ $xml->setParserProperty(2, true);
+
+ $currCells = 0;
+ while ($xml->read()) {
+ if ($xml->name == 'row' && $xml->nodeType == XMLReader::ELEMENT) {
+ $row = $xml->getAttribute('r');
+ $tmpInfo['totalRows'] = $row;
+ $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
+ $currCells = 0;
+ } elseif ($xml->name == 'c' && $xml->nodeType == XMLReader::ELEMENT) {
+ ++$currCells;
+ }
+ }
+ $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
+ $xml->close();
+
+ $tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
+ $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
+
+ $worksheetInfo[] = $tmpInfo;
+ }
+ }
+ }
+ }
+
+ $zip->close();
+
+ return $worksheetInfo;
+ }
+
+ private static function castToBoolean($c)
+ {
+ $value = isset($c->v) ? (string) $c->v : null;
+ if ($value == '0') {
+ return false;
+ } elseif ($value == '1') {
+ return true;
+ }
+
+ return (bool) $c->v;
+ }
+
+ private static function castToError($c)
+ {
+ return isset($c->v) ? (string) $c->v : null;
+ }
+
+ private static function castToString($c)
+ {
+ return isset($c->v) ? (string) $c->v : null;
+ }
+
+ private function castToFormula($c, $r, &$cellDataType, &$value, &$calculatedValue, &$sharedFormulas, $castBaseType)
+ {
+ $cellDataType = 'f';
+ $value = "={$c->f}";
+ $calculatedValue = self::$castBaseType($c);
+
+ // Shared formula?
+ if (isset($c->f['t']) && strtolower((string) $c->f['t']) == 'shared') {
+ $instance = (string) $c->f['si'];
+
+ if (!isset($sharedFormulas[(string) $c->f['si']])) {
+ $sharedFormulas[$instance] = ['master' => $r, 'formula' => $value];
+ } else {
+ $master = Coordinate::coordinateFromString($sharedFormulas[$instance]['master']);
+ $current = Coordinate::coordinateFromString($r);
+
+ $difference = [0, 0];
+ $difference[0] = Coordinate::columnIndexFromString($current[0]) - Coordinate::columnIndexFromString($master[0]);
+ $difference[1] = $current[1] - $master[1];
+
+ $value = $this->referenceHelper->updateFormulaReferences($sharedFormulas[$instance]['formula'], 'A1', $difference[0], $difference[1]);
+ }
+ }
+ }
+
+ /**
+ * @param ZipArchive $archive
+ * @param string $fileName
+ *
+ * @return string
+ */
+ private function getFromZipArchive(ZipArchive $archive, $fileName = '')
+ {
+ // Root-relative paths
+ if (strpos($fileName, '//') !== false) {
+ $fileName = substr($fileName, strpos($fileName, '//') + 1);
+ }
+ $fileName = File::realpath($fileName);
+
+ // Sadly, some 3rd party xlsx generators don't use consistent case for filenaming
+ // so we need to load case-insensitively from the zip file
+
+ // Apache POI fixes
+ $contents = $archive->getFromName($fileName, 0, ZipArchive::FL_NOCASE);
+ if ($contents === false) {
+ $contents = $archive->getFromName(substr($fileName, 1), 0, ZipArchive::FL_NOCASE);
+ }
+
+ return $contents;
+ }
+
+ /**
+ * Set Worksheet column attributes by attributes array passed.
+ *
+ * @param Worksheet $docSheet
+ * @param string $column A, B, ... DX, ...
+ * @param array $columnAttributes array of attributes (indexes are attribute name, values are value)
+ * 'xfIndex', 'visible', 'collapsed', 'outlineLevel', 'width', ... ?
+ */
+ private function setColumnAttributes(Worksheet $docSheet, $column, array $columnAttributes)
+ {
+ if (isset($columnAttributes['xfIndex'])) {
+ $docSheet->getColumnDimension($column)->setXfIndex($columnAttributes['xfIndex']);
+ }
+ if (isset($columnAttributes['visible'])) {
+ $docSheet->getColumnDimension($column)->setVisible($columnAttributes['visible']);
+ }
+ if (isset($columnAttributes['collapsed'])) {
+ $docSheet->getColumnDimension($column)->setCollapsed($columnAttributes['collapsed']);
+ }
+ if (isset($columnAttributes['outlineLevel'])) {
+ $docSheet->getColumnDimension($column)->setOutlineLevel($columnAttributes['outlineLevel']);
+ }
+ if (isset($columnAttributes['width'])) {
+ $docSheet->getColumnDimension($column)->setWidth($columnAttributes['width']);
+ }
+ }
+
+ /**
+ * Set Worksheet row attributes by attributes array passed.
+ *
+ * @param Worksheet $docSheet
+ * @param int $row 1, 2, 3, ... 99, ...
+ * @param array $rowAttributes array of attributes (indexes are attribute name, values are value)
+ * 'xfIndex', 'visible', 'collapsed', 'outlineLevel', 'rowHeight', ... ?
+ */
+ private function setRowAttributes(Worksheet $docSheet, $row, array $rowAttributes)
+ {
+ if (isset($rowAttributes['xfIndex'])) {
+ $docSheet->getRowDimension($row)->setXfIndex($rowAttributes['xfIndex']);
+ }
+ if (isset($rowAttributes['visible'])) {
+ $docSheet->getRowDimension($row)->setVisible($rowAttributes['visible']);
+ }
+ if (isset($rowAttributes['collapsed'])) {
+ $docSheet->getRowDimension($row)->setCollapsed($rowAttributes['collapsed']);
+ }
+ if (isset($rowAttributes['outlineLevel'])) {
+ $docSheet->getRowDimension($row)->setOutlineLevel($rowAttributes['outlineLevel']);
+ }
+ if (isset($rowAttributes['rowHeight'])) {
+ $docSheet->getRowDimension($row)->setRowHeight($rowAttributes['rowHeight']);
+ }
+ }
+
+ /**
+ * Loads Spreadsheet from file.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function load($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ // Initialisations
+ $excel = new Spreadsheet();
+ $excel->removeSheetByIndex(0);
+ if (!$this->readDataOnly) {
+ $excel->removeCellStyleXfByIndex(0); // remove the default style
+ $excel->removeCellXfByIndex(0); // remove the default style
+ }
+ $unparsedLoadedData = [];
+
+ $zip = new ZipArchive();
+ $zip->open($pFilename);
+
+ // Read the theme first, because we need the colour scheme when reading the styles
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $wbRels = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, 'xl/_rels/workbook.xml.rels')),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ foreach ($wbRels->Relationship as $rel) {
+ switch ($rel['Type']) {
+ case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme':
+ $themeOrderArray = ['lt1', 'dk1', 'lt2', 'dk2'];
+ $themeOrderAdditional = count($themeOrderArray);
+
+ $xmlTheme = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "xl/{$rel['Target']}")),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ if (is_object($xmlTheme)) {
+ $xmlThemeName = $xmlTheme->attributes();
+ $xmlTheme = $xmlTheme->children('http://schemas.openxmlformats.org/drawingml/2006/main');
+ $themeName = (string) $xmlThemeName['name'];
+
+ $colourScheme = $xmlTheme->themeElements->clrScheme->attributes();
+ $colourSchemeName = (string) $colourScheme['name'];
+ $colourScheme = $xmlTheme->themeElements->clrScheme->children('http://schemas.openxmlformats.org/drawingml/2006/main');
+
+ $themeColours = [];
+ foreach ($colourScheme as $k => $xmlColour) {
+ $themePos = array_search($k, $themeOrderArray);
+ if ($themePos === false) {
+ $themePos = $themeOrderAdditional++;
+ }
+ if (isset($xmlColour->sysClr)) {
+ $xmlColourData = $xmlColour->sysClr->attributes();
+ $themeColours[$themePos] = $xmlColourData['lastClr'];
+ } elseif (isset($xmlColour->srgbClr)) {
+ $xmlColourData = $xmlColour->srgbClr->attributes();
+ $themeColours[$themePos] = $xmlColourData['val'];
+ }
+ }
+ self::$theme = new Xlsx\Theme($themeName, $colourSchemeName, $themeColours);
+ }
+
+ break;
+ }
+ }
+
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $rels = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, '_rels/.rels')),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ foreach ($rels->Relationship as $rel) {
+ switch ($rel['Type']) {
+ case 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties':
+ $xmlCore = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}")),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ if (is_object($xmlCore)) {
+ $xmlCore->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
+ $xmlCore->registerXPathNamespace('dcterms', 'http://purl.org/dc/terms/');
+ $xmlCore->registerXPathNamespace('cp', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties');
+ $docProps = $excel->getProperties();
+ $docProps->setCreator((string) self::getArrayItem($xmlCore->xpath('dc:creator')));
+ $docProps->setLastModifiedBy((string) self::getArrayItem($xmlCore->xpath('cp:lastModifiedBy')));
+ $docProps->setCreated(strtotime(self::getArrayItem($xmlCore->xpath('dcterms:created')))); //! respect xsi:type
+ $docProps->setModified(strtotime(self::getArrayItem($xmlCore->xpath('dcterms:modified')))); //! respect xsi:type
+ $docProps->setTitle((string) self::getArrayItem($xmlCore->xpath('dc:title')));
+ $docProps->setDescription((string) self::getArrayItem($xmlCore->xpath('dc:description')));
+ $docProps->setSubject((string) self::getArrayItem($xmlCore->xpath('dc:subject')));
+ $docProps->setKeywords((string) self::getArrayItem($xmlCore->xpath('cp:keywords')));
+ $docProps->setCategory((string) self::getArrayItem($xmlCore->xpath('cp:category')));
+ }
+
+ break;
+ case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties':
+ $xmlCore = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}")),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ if (is_object($xmlCore)) {
+ $docProps = $excel->getProperties();
+ if (isset($xmlCore->Company)) {
+ $docProps->setCompany((string) $xmlCore->Company);
+ }
+ if (isset($xmlCore->Manager)) {
+ $docProps->setManager((string) $xmlCore->Manager);
+ }
+ }
+
+ break;
+ case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties':
+ $xmlCore = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}")),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ if (is_object($xmlCore)) {
+ $docProps = $excel->getProperties();
+ /** @var SimpleXMLElement $xmlProperty */
+ foreach ($xmlCore as $xmlProperty) {
+ $cellDataOfficeAttributes = $xmlProperty->attributes();
+ if (isset($cellDataOfficeAttributes['name'])) {
+ $propertyName = (string) $cellDataOfficeAttributes['name'];
+ $cellDataOfficeChildren = $xmlProperty->children('http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
+ $attributeType = $cellDataOfficeChildren->getName();
+ $attributeValue = (string) $cellDataOfficeChildren->{$attributeType};
+ $attributeValue = Properties::convertProperty($attributeValue, $attributeType);
+ $attributeType = Properties::convertPropertyType($attributeType);
+ $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
+ }
+ }
+ }
+
+ break;
+ //Ribbon
+ case 'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility':
+ $customUI = $rel['Target'];
+ if ($customUI !== null) {
+ $this->readRibbon($excel, $customUI, $zip);
+ }
+
+ break;
+ case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument':
+ $dir = dirname($rel['Target']);
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsWorkbook = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "$dir/_rels/" . basename($rel['Target']) . '.rels')),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $relsWorkbook->registerXPathNamespace('rel', 'http://schemas.openxmlformats.org/package/2006/relationships');
+
+ $sharedStrings = [];
+ $xpath = self::getArrayItem($relsWorkbook->xpath("rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings']"));
+ //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main"
+ $xmlStrings = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "$dir/$xpath[Target]")),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ if (isset($xmlStrings, $xmlStrings->si)) {
+ foreach ($xmlStrings->si as $val) {
+ if (isset($val->t)) {
+ $sharedStrings[] = StringHelper::controlCharacterOOXML2PHP((string) $val->t);
+ } elseif (isset($val->r)) {
+ $sharedStrings[] = $this->parseRichText($val);
+ }
+ }
+ }
+
+ $worksheets = [];
+ $macros = $customUI = null;
+ foreach ($relsWorkbook->Relationship as $ele) {
+ switch ($ele['Type']) {
+ case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet':
+ $worksheets[(string) $ele['Id']] = $ele['Target'];
+
+ break;
+ // a vbaProject ? (: some macros)
+ case 'http://schemas.microsoft.com/office/2006/relationships/vbaProject':
+ $macros = $ele['Target'];
+
+ break;
+ }
+ }
+
+ if ($macros !== null) {
+ $macrosCode = $this->getFromZipArchive($zip, 'xl/vbaProject.bin'); //vbaProject.bin always in 'xl' dir and always named vbaProject.bin
+ if ($macrosCode !== false) {
+ $excel->setMacrosCode($macrosCode);
+ $excel->setHasMacros(true);
+ //short-circuit : not reading vbaProject.bin.rel to get Signature =>allways vbaProjectSignature.bin in 'xl' dir
+ $Certificate = $this->getFromZipArchive($zip, 'xl/vbaProjectSignature.bin');
+ if ($Certificate !== false) {
+ $excel->setMacrosCertificate($Certificate);
+ }
+ }
+ }
+ $styles = [];
+ $cellStyles = [];
+ $xpath = self::getArrayItem($relsWorkbook->xpath("rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles']"));
+ //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main"
+ $xmlStyles = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "$dir/$xpath[Target]")),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $numFmts = null;
+ if ($xmlStyles && $xmlStyles->numFmts[0]) {
+ $numFmts = $xmlStyles->numFmts[0];
+ }
+ if (isset($numFmts) && ($numFmts !== null)) {
+ $numFmts->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
+ }
+ if (!$this->readDataOnly && $xmlStyles) {
+ foreach ($xmlStyles->cellXfs->xf as $xf) {
+ $numFmt = NumberFormat::FORMAT_GENERAL;
+
+ if ($xf['numFmtId']) {
+ if (isset($numFmts)) {
+ $tmpNumFmt = self::getArrayItem($numFmts->xpath("sml:numFmt[@numFmtId=$xf[numFmtId]]"));
+
+ if (isset($tmpNumFmt['formatCode'])) {
+ $numFmt = (string) $tmpNumFmt['formatCode'];
+ }
+ }
+
+ // We shouldn't override any of the built-in MS Excel values (values below id 164)
+ // But there's a lot of naughty homebrew xlsx writers that do use "reserved" id values that aren't actually used
+ // So we make allowance for them rather than lose formatting masks
+ if ((int) $xf['numFmtId'] < 164 &&
+ NumberFormat::builtInFormatCode((int) $xf['numFmtId']) !== '') {
+ $numFmt = NumberFormat::builtInFormatCode((int) $xf['numFmtId']);
+ }
+ }
+ $quotePrefix = false;
+ if (isset($xf['quotePrefix'])) {
+ $quotePrefix = (bool) $xf['quotePrefix'];
+ }
+
+ $style = (object) [
+ 'numFmt' => $numFmt,
+ 'font' => $xmlStyles->fonts->font[(int) ($xf['fontId'])],
+ 'fill' => $xmlStyles->fills->fill[(int) ($xf['fillId'])],
+ 'border' => $xmlStyles->borders->border[(int) ($xf['borderId'])],
+ 'alignment' => $xf->alignment,
+ 'protection' => $xf->protection,
+ 'quotePrefix' => $quotePrefix,
+ ];
+ $styles[] = $style;
+
+ // add style to cellXf collection
+ $objStyle = new Style();
+ self::readStyle($objStyle, $style);
+ $excel->addCellXf($objStyle);
+ }
+
+ foreach (isset($xmlStyles->cellStyleXfs->xf) ? $xmlStyles->cellStyleXfs->xf : [] as $xf) {
+ $numFmt = NumberFormat::FORMAT_GENERAL;
+ if ($numFmts && $xf['numFmtId']) {
+ $tmpNumFmt = self::getArrayItem($numFmts->xpath("sml:numFmt[@numFmtId=$xf[numFmtId]]"));
+ if (isset($tmpNumFmt['formatCode'])) {
+ $numFmt = (string) $tmpNumFmt['formatCode'];
+ } elseif ((int) $xf['numFmtId'] < 165) {
+ $numFmt = NumberFormat::builtInFormatCode((int) $xf['numFmtId']);
+ }
+ }
+
+ $cellStyle = (object) [
+ 'numFmt' => $numFmt,
+ 'font' => $xmlStyles->fonts->font[(int) ($xf['fontId'])],
+ 'fill' => $xmlStyles->fills->fill[(int) ($xf['fillId'])],
+ 'border' => $xmlStyles->borders->border[(int) ($xf['borderId'])],
+ 'alignment' => $xf->alignment,
+ 'protection' => $xf->protection,
+ 'quotePrefix' => $quotePrefix,
+ ];
+ $cellStyles[] = $cellStyle;
+
+ // add style to cellStyleXf collection
+ $objStyle = new Style();
+ self::readStyle($objStyle, $cellStyle);
+ $excel->addCellStyleXf($objStyle);
+ }
+ }
+
+ $dxfs = [];
+ if (!$this->readDataOnly && $xmlStyles) {
+ // Conditional Styles
+ if ($xmlStyles->dxfs) {
+ foreach ($xmlStyles->dxfs->dxf as $dxf) {
+ $style = new Style(false, true);
+ self::readStyle($style, $dxf);
+ $dxfs[] = $style;
+ }
+ }
+ // Cell Styles
+ if ($xmlStyles->cellStyles) {
+ foreach ($xmlStyles->cellStyles->cellStyle as $cellStyle) {
+ if ((int) ($cellStyle['builtinId']) == 0) {
+ if (isset($cellStyles[(int) ($cellStyle['xfId'])])) {
+ // Set default style
+ $style = new Style();
+ self::readStyle($style, $cellStyles[(int) ($cellStyle['xfId'])]);
+
+ // normal style, currently not using it for anything
+ }
+ }
+ }
+ }
+ }
+
+ //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main"
+ $xmlWorkbook = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}")),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+
+ // Set base date
+ if ($xmlWorkbook->workbookPr) {
+ Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
+ if (isset($xmlWorkbook->workbookPr['date1904'])) {
+ if (self::boolean((string) $xmlWorkbook->workbookPr['date1904'])) {
+ Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
+ }
+ }
+ }
+
+ // Set protection
+ $this->readProtection($excel, $xmlWorkbook);
+
+ $sheetId = 0; // keep track of new sheet id in final workbook
+ $oldSheetId = -1; // keep track of old sheet id in final workbook
+ $countSkippedSheets = 0; // keep track of number of skipped sheets
+ $mapSheetId = []; // mapping of sheet ids from old to new
+
+ $charts = $chartDetails = [];
+
+ if ($xmlWorkbook->sheets) {
+ /** @var SimpleXMLElement $eleSheet */
+ foreach ($xmlWorkbook->sheets->sheet as $eleSheet) {
+ ++$oldSheetId;
+
+ // Check if sheet should be skipped
+ if (isset($this->loadSheetsOnly) && !in_array((string) $eleSheet['name'], $this->loadSheetsOnly)) {
+ ++$countSkippedSheets;
+ $mapSheetId[$oldSheetId] = null;
+
+ continue;
+ }
+
+ // Map old sheet id in original workbook to new sheet id.
+ // They will differ if loadSheetsOnly() is being used
+ $mapSheetId[$oldSheetId] = $oldSheetId - $countSkippedSheets;
+
+ // Load sheet
+ $docSheet = $excel->createSheet();
+ // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet
+ // references in formula cells... during the load, all formulae should be correct,
+ // and we're simply bringing the worksheet name in line with the formula, not the
+ // reverse
+ $docSheet->setTitle((string) $eleSheet['name'], false, false);
+ $fileWorksheet = $worksheets[(string) self::getArrayItem($eleSheet->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), 'id')];
+ //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main"
+ $xmlSheet = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, "$dir/$fileWorksheet")),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+
+ $sharedFormulas = [];
+
+ if (isset($eleSheet['state']) && (string) $eleSheet['state'] != '') {
+ $docSheet->setSheetState((string) $eleSheet['state']);
+ }
+
+ if (isset($xmlSheet->sheetViews, $xmlSheet->sheetViews->sheetView)) {
+ if (isset($xmlSheet->sheetViews->sheetView['zoomScale'])) {
+ $zoomScale = (int) ($xmlSheet->sheetViews->sheetView['zoomScale']);
+ if ($zoomScale <= 0) {
+ // setZoomScale will throw an Exception if the scale is less than or equals 0
+ // that is OK when manually creating documents, but we should be able to read all documents
+ $zoomScale = 100;
+ }
+
+ $docSheet->getSheetView()->setZoomScale($zoomScale);
+ }
+ if (isset($xmlSheet->sheetViews->sheetView['zoomScaleNormal'])) {
+ $zoomScaleNormal = (int) ($xmlSheet->sheetViews->sheetView['zoomScaleNormal']);
+ if ($zoomScaleNormal <= 0) {
+ // setZoomScaleNormal will throw an Exception if the scale is less than or equals 0
+ // that is OK when manually creating documents, but we should be able to read all documents
+ $zoomScaleNormal = 100;
+ }
+
+ $docSheet->getSheetView()->setZoomScaleNormal($zoomScaleNormal);
+ }
+ if (isset($xmlSheet->sheetViews->sheetView['view'])) {
+ $docSheet->getSheetView()->setView((string) $xmlSheet->sheetViews->sheetView['view']);
+ }
+ if (isset($xmlSheet->sheetViews->sheetView['showGridLines'])) {
+ $docSheet->setShowGridLines(self::boolean((string) $xmlSheet->sheetViews->sheetView['showGridLines']));
+ }
+ if (isset($xmlSheet->sheetViews->sheetView['showRowColHeaders'])) {
+ $docSheet->setShowRowColHeaders(self::boolean((string) $xmlSheet->sheetViews->sheetView['showRowColHeaders']));
+ }
+ if (isset($xmlSheet->sheetViews->sheetView['rightToLeft'])) {
+ $docSheet->setRightToLeft(self::boolean((string) $xmlSheet->sheetViews->sheetView['rightToLeft']));
+ }
+ if (isset($xmlSheet->sheetViews->sheetView->pane)) {
+ $xSplit = 0;
+ $ySplit = 0;
+ $topLeftCell = null;
+
+ if (isset($xmlSheet->sheetViews->sheetView->pane['xSplit'])) {
+ $xSplit = (int) ($xmlSheet->sheetViews->sheetView->pane['xSplit']);
+ }
+
+ if (isset($xmlSheet->sheetViews->sheetView->pane['ySplit'])) {
+ $ySplit = (int) ($xmlSheet->sheetViews->sheetView->pane['ySplit']);
+ }
+
+ if (isset($xmlSheet->sheetViews->sheetView->pane['topLeftCell'])) {
+ $topLeftCell = (string) $xmlSheet->sheetViews->sheetView->pane['topLeftCell'];
+ }
+
+ $docSheet->freezePane(Coordinate::stringFromColumnIndex($xSplit + 1) . ($ySplit + 1), $topLeftCell);
+ }
+
+ if (isset($xmlSheet->sheetViews->sheetView->selection)) {
+ if (isset($xmlSheet->sheetViews->sheetView->selection['sqref'])) {
+ $sqref = (string) $xmlSheet->sheetViews->sheetView->selection['sqref'];
+ $sqref = explode(' ', $sqref);
+ $sqref = $sqref[0];
+ $docSheet->setSelectedCells($sqref);
+ }
+ }
+ }
+
+ if (isset($xmlSheet->sheetPr, $xmlSheet->sheetPr->tabColor)) {
+ if (isset($xmlSheet->sheetPr->tabColor['rgb'])) {
+ $docSheet->getTabColor()->setARGB((string) $xmlSheet->sheetPr->tabColor['rgb']);
+ }
+ }
+ if (isset($xmlSheet->sheetPr, $xmlSheet->sheetPr['codeName'])) {
+ $docSheet->setCodeName((string) $xmlSheet->sheetPr['codeName'], false);
+ }
+ if (isset($xmlSheet->sheetPr, $xmlSheet->sheetPr->outlinePr)) {
+ if (isset($xmlSheet->sheetPr->outlinePr['summaryRight']) &&
+ !self::boolean((string) $xmlSheet->sheetPr->outlinePr['summaryRight'])) {
+ $docSheet->setShowSummaryRight(false);
+ } else {
+ $docSheet->setShowSummaryRight(true);
+ }
+
+ if (isset($xmlSheet->sheetPr->outlinePr['summaryBelow']) &&
+ !self::boolean((string) $xmlSheet->sheetPr->outlinePr['summaryBelow'])) {
+ $docSheet->setShowSummaryBelow(false);
+ } else {
+ $docSheet->setShowSummaryBelow(true);
+ }
+ }
+
+ if (isset($xmlSheet->sheetPr, $xmlSheet->sheetPr->pageSetUpPr)) {
+ if (isset($xmlSheet->sheetPr->pageSetUpPr['fitToPage']) &&
+ !self::boolean((string) $xmlSheet->sheetPr->pageSetUpPr['fitToPage'])) {
+ $docSheet->getPageSetup()->setFitToPage(false);
+ } else {
+ $docSheet->getPageSetup()->setFitToPage(true);
+ }
+ }
+
+ if (isset($xmlSheet->sheetFormatPr)) {
+ if (isset($xmlSheet->sheetFormatPr['customHeight']) &&
+ self::boolean((string) $xmlSheet->sheetFormatPr['customHeight']) &&
+ isset($xmlSheet->sheetFormatPr['defaultRowHeight'])) {
+ $docSheet->getDefaultRowDimension()->setRowHeight((float) $xmlSheet->sheetFormatPr['defaultRowHeight']);
+ }
+ if (isset($xmlSheet->sheetFormatPr['defaultColWidth'])) {
+ $docSheet->getDefaultColumnDimension()->setWidth((float) $xmlSheet->sheetFormatPr['defaultColWidth']);
+ }
+ if (isset($xmlSheet->sheetFormatPr['zeroHeight']) &&
+ ((string) $xmlSheet->sheetFormatPr['zeroHeight'] == '1')) {
+ $docSheet->getDefaultRowDimension()->setZeroHeight(true);
+ }
+ }
+
+ if (isset($xmlSheet->printOptions) && !$this->readDataOnly) {
+ if (self::boolean((string) $xmlSheet->printOptions['gridLinesSet'])) {
+ $docSheet->setShowGridlines(true);
+ }
+ if (self::boolean((string) $xmlSheet->printOptions['gridLines'])) {
+ $docSheet->setPrintGridlines(true);
+ }
+ if (self::boolean((string) $xmlSheet->printOptions['horizontalCentered'])) {
+ $docSheet->getPageSetup()->setHorizontalCentered(true);
+ }
+ if (self::boolean((string) $xmlSheet->printOptions['verticalCentered'])) {
+ $docSheet->getPageSetup()->setVerticalCentered(true);
+ }
+ }
+
+ $this->readColumnsAndRowsAttributes($xmlSheet, $docSheet);
+
+ if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) {
+ $cIndex = 1; // Cell Start from 1
+ foreach ($xmlSheet->sheetData->row as $row) {
+ $rowIndex = 1;
+ foreach ($row->c as $c) {
+ $r = (string) $c['r'];
+ if ($r == '') {
+ $r = Coordinate::stringFromColumnIndex($rowIndex) . $cIndex;
+ }
+ $cellDataType = (string) $c['t'];
+ $value = null;
+ $calculatedValue = null;
+
+ // Read cell?
+ if ($this->getReadFilter() !== null) {
+ $coordinates = Coordinate::coordinateFromString($r);
+
+ if (!$this->getReadFilter()->readCell($coordinates[0], (int) $coordinates[1], $docSheet->getTitle())) {
+ $rowIndex += 1;
+
+ continue;
+ }
+ }
+
+ // Read cell!
+ switch ($cellDataType) {
+ case 's':
+ if ((string) $c->v != '') {
+ $value = $sharedStrings[(int) ($c->v)];
+
+ if ($value instanceof RichText) {
+ $value = clone $value;
+ }
+ } else {
+ $value = '';
+ }
+
+ break;
+ case 'b':
+ if (!isset($c->f)) {
+ $value = self::castToBoolean($c);
+ } else {
+ // Formula
+ $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToBoolean');
+ if (isset($c->f['t'])) {
+ $att = $c->f;
+ $docSheet->getCell($r)->setFormulaAttributes($att);
+ }
+ }
+
+ break;
+ case 'inlineStr':
+ if (isset($c->f)) {
+ $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
+ } else {
+ $value = $this->parseRichText($c->is);
+ }
+
+ break;
+ case 'e':
+ if (!isset($c->f)) {
+ $value = self::castToError($c);
+ } else {
+ // Formula
+ $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
+ }
+
+ break;
+ default:
+ if (!isset($c->f)) {
+ $value = self::castToString($c);
+ } else {
+ // Formula
+ $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString');
+ }
+
+ break;
+ }
+
+ // Check for numeric values
+ if (is_numeric($value) && $cellDataType != 's') {
+ if ($value == (int) $value) {
+ $value = (int) $value;
+ } elseif ($value == (float) $value) {
+ $value = (float) $value;
+ } elseif ($value == (float) $value) {
+ $value = (float) $value;
+ }
+ }
+
+ // Rich text?
+ if ($value instanceof RichText && $this->readDataOnly) {
+ $value = $value->getPlainText();
+ }
+
+ $cell = $docSheet->getCell($r);
+ // Assign value
+ if ($cellDataType != '') {
+ $cell->setValueExplicit($value, $cellDataType);
+ } else {
+ $cell->setValue($value);
+ }
+ if ($calculatedValue !== null) {
+ $cell->setCalculatedValue($calculatedValue);
+ }
+
+ // Style information?
+ if ($c['s'] && !$this->readDataOnly) {
+ // no style index means 0, it seems
+ $cell->setXfIndex(isset($styles[(int) ($c['s'])]) ?
+ (int) ($c['s']) : 0);
+ }
+ $rowIndex += 1;
+ }
+ $cIndex += 1;
+ }
+ }
+
+ $conditionals = [];
+ if (!$this->readDataOnly && $xmlSheet && $xmlSheet->conditionalFormatting) {
+ foreach ($xmlSheet->conditionalFormatting as $conditional) {
+ foreach ($conditional->cfRule as $cfRule) {
+ if (((string) $cfRule['type'] == Conditional::CONDITION_NONE || (string) $cfRule['type'] == Conditional::CONDITION_CELLIS || (string) $cfRule['type'] == Conditional::CONDITION_CONTAINSTEXT || (string) $cfRule['type'] == Conditional::CONDITION_EXPRESSION) && isset($dxfs[(int) ($cfRule['dxfId'])])) {
+ $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule;
+ }
+ }
+ }
+
+ foreach ($conditionals as $ref => $cfRules) {
+ ksort($cfRules);
+ $conditionalStyles = [];
+ foreach ($cfRules as $cfRule) {
+ $objConditional = new Conditional();
+ $objConditional->setConditionType((string) $cfRule['type']);
+ $objConditional->setOperatorType((string) $cfRule['operator']);
+
+ if ((string) $cfRule['text'] != '') {
+ $objConditional->setText((string) $cfRule['text']);
+ }
+
+ if (isset($cfRule['stopIfTrue']) && (int) $cfRule['stopIfTrue'] === 1) {
+ $objConditional->setStopIfTrue(true);
+ }
+
+ if (count($cfRule->formula) > 1) {
+ foreach ($cfRule->formula as $formula) {
+ $objConditional->addCondition((string) $formula);
+ }
+ } else {
+ $objConditional->addCondition((string) $cfRule->formula);
+ }
+ $objConditional->setStyle(clone $dxfs[(int) ($cfRule['dxfId'])]);
+ $conditionalStyles[] = $objConditional;
+ }
+
+ // Extract all cell references in $ref
+ $cellBlocks = explode(' ', str_replace('$', '', strtoupper($ref)));
+ foreach ($cellBlocks as $cellBlock) {
+ $docSheet->getStyle($cellBlock)->setConditionalStyles($conditionalStyles);
+ }
+ }
+ }
+
+ $aKeys = ['sheet', 'objects', 'scenarios', 'formatCells', 'formatColumns', 'formatRows', 'insertColumns', 'insertRows', 'insertHyperlinks', 'deleteColumns', 'deleteRows', 'selectLockedCells', 'sort', 'autoFilter', 'pivotTables', 'selectUnlockedCells'];
+ if (!$this->readDataOnly && $xmlSheet && $xmlSheet->sheetProtection) {
+ foreach ($aKeys as $key) {
+ $method = 'set' . ucfirst($key);
+ $docSheet->getProtection()->$method(self::boolean((string) $xmlSheet->sheetProtection[$key]));
+ }
+ }
+
+ if (!$this->readDataOnly && $xmlSheet && $xmlSheet->sheetProtection) {
+ $docSheet->getProtection()->setPassword((string) $xmlSheet->sheetProtection['password'], true);
+ if ($xmlSheet->protectedRanges->protectedRange) {
+ foreach ($xmlSheet->protectedRanges->protectedRange as $protectedRange) {
+ $docSheet->protectCells((string) $protectedRange['sqref'], (string) $protectedRange['password'], true);
+ }
+ }
+ }
+
+ if ($xmlSheet && $xmlSheet->autoFilter && !$this->readDataOnly) {
+ $autoFilterRange = (string) $xmlSheet->autoFilter['ref'];
+ if (strpos($autoFilterRange, ':') !== false) {
+ $autoFilter = $docSheet->getAutoFilter();
+ $autoFilter->setRange($autoFilterRange);
+
+ foreach ($xmlSheet->autoFilter->filterColumn as $filterColumn) {
+ $column = $autoFilter->getColumnByOffset((int) $filterColumn['colId']);
+ // Check for standard filters
+ if ($filterColumn->filters) {
+ $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER);
+ $filters = $filterColumn->filters;
+ if ((isset($filters['blank'])) && ($filters['blank'] == 1)) {
+ // Operator is undefined, but always treated as EQUAL
+ $column->createRule()->setRule(null, '')->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_FILTER);
+ }
+ // Standard filters are always an OR join, so no join rule needs to be set
+ // Entries can be either filter elements
+ foreach ($filters->filter as $filterRule) {
+ // Operator is undefined, but always treated as EQUAL
+ $column->createRule()->setRule(null, (string) $filterRule['val'])->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_FILTER);
+ }
+ // Or Date Group elements
+ foreach ($filters->dateGroupItem as $dateGroupItem) {
+ // Operator is undefined, but always treated as EQUAL
+ $column->createRule()->setRule(
+ null,
+ [
+ 'year' => (string) $dateGroupItem['year'],
+ 'month' => (string) $dateGroupItem['month'],
+ 'day' => (string) $dateGroupItem['day'],
+ 'hour' => (string) $dateGroupItem['hour'],
+ 'minute' => (string) $dateGroupItem['minute'],
+ 'second' => (string) $dateGroupItem['second'],
+ ],
+ (string) $dateGroupItem['dateTimeGrouping']
+ )
+ ->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_DATEGROUP);
+ }
+ }
+ // Check for custom filters
+ if ($filterColumn->customFilters) {
+ $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER);
+ $customFilters = $filterColumn->customFilters;
+ // Custom filters can an AND or an OR join;
+ // and there should only ever be one or two entries
+ if ((isset($customFilters['and'])) && ($customFilters['and'] == 1)) {
+ $column->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND);
+ }
+ foreach ($customFilters->customFilter as $filterRule) {
+ $column->createRule()->setRule(
+ (string) $filterRule['operator'],
+ (string) $filterRule['val']
+ )
+ ->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER);
+ }
+ }
+ // Check for dynamic filters
+ if ($filterColumn->dynamicFilter) {
+ $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER);
+ // We should only ever have one dynamic filter
+ foreach ($filterColumn->dynamicFilter as $filterRule) {
+ // Operator is undefined, but always treated as EQUAL
+ $column->createRule()->setRule(
+ null,
+ (string) $filterRule['val'],
+ (string) $filterRule['type']
+ )
+ ->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER);
+ if (isset($filterRule['val'])) {
+ $column->setAttribute('val', (string) $filterRule['val']);
+ }
+ if (isset($filterRule['maxVal'])) {
+ $column->setAttribute('maxVal', (string) $filterRule['maxVal']);
+ }
+ }
+ }
+ // Check for dynamic filters
+ if ($filterColumn->top10) {
+ $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_TOPTENFILTER);
+ // We should only ever have one top10 filter
+ foreach ($filterColumn->top10 as $filterRule) {
+ $column->createRule()->setRule(
+ (((isset($filterRule['percent'])) && ($filterRule['percent'] == 1))
+ ? Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT
+ : Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE
+ ),
+ (string) $filterRule['val'],
+ (((isset($filterRule['top'])) && ($filterRule['top'] == 1))
+ ? Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP
+ : Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM
+ )
+ )
+ ->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_TOPTENFILTER);
+ }
+ }
+ }
+ }
+ }
+
+ if ($xmlSheet && $xmlSheet->mergeCells && $xmlSheet->mergeCells->mergeCell && !$this->readDataOnly) {
+ foreach ($xmlSheet->mergeCells->mergeCell as $mergeCell) {
+ $mergeRef = (string) $mergeCell['ref'];
+ if (strpos($mergeRef, ':') !== false) {
+ $docSheet->mergeCells((string) $mergeCell['ref']);
+ }
+ }
+ }
+
+ if ($xmlSheet && $xmlSheet->pageMargins && !$this->readDataOnly) {
+ $docPageMargins = $docSheet->getPageMargins();
+ $docPageMargins->setLeft((float) ($xmlSheet->pageMargins['left']));
+ $docPageMargins->setRight((float) ($xmlSheet->pageMargins['right']));
+ $docPageMargins->setTop((float) ($xmlSheet->pageMargins['top']));
+ $docPageMargins->setBottom((float) ($xmlSheet->pageMargins['bottom']));
+ $docPageMargins->setHeader((float) ($xmlSheet->pageMargins['header']));
+ $docPageMargins->setFooter((float) ($xmlSheet->pageMargins['footer']));
+ }
+
+ if ($xmlSheet && $xmlSheet->pageSetup && !$this->readDataOnly) {
+ $docPageSetup = $docSheet->getPageSetup();
+
+ if (isset($xmlSheet->pageSetup['orientation'])) {
+ $docPageSetup->setOrientation((string) $xmlSheet->pageSetup['orientation']);
+ }
+ if (isset($xmlSheet->pageSetup['paperSize'])) {
+ $docPageSetup->setPaperSize((int) ($xmlSheet->pageSetup['paperSize']));
+ }
+ if (isset($xmlSheet->pageSetup['scale'])) {
+ $docPageSetup->setScale((int) ($xmlSheet->pageSetup['scale']), false);
+ }
+ if (isset($xmlSheet->pageSetup['fitToHeight']) && (int) ($xmlSheet->pageSetup['fitToHeight']) >= 0) {
+ $docPageSetup->setFitToHeight((int) ($xmlSheet->pageSetup['fitToHeight']), false);
+ }
+ if (isset($xmlSheet->pageSetup['fitToWidth']) && (int) ($xmlSheet->pageSetup['fitToWidth']) >= 0) {
+ $docPageSetup->setFitToWidth((int) ($xmlSheet->pageSetup['fitToWidth']), false);
+ }
+ if (isset($xmlSheet->pageSetup['firstPageNumber'], $xmlSheet->pageSetup['useFirstPageNumber']) &&
+ self::boolean((string) $xmlSheet->pageSetup['useFirstPageNumber'])) {
+ $docPageSetup->setFirstPageNumber((int) ($xmlSheet->pageSetup['firstPageNumber']));
+ }
+
+ $relAttributes = $xmlSheet->pageSetup->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships');
+ if (isset($relAttributes['id'])) {
+ $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['pageSetupRelId'] = (string) $relAttributes['id'];
+ }
+ }
+
+ if ($xmlSheet && $xmlSheet->headerFooter && !$this->readDataOnly) {
+ $docHeaderFooter = $docSheet->getHeaderFooter();
+
+ if (isset($xmlSheet->headerFooter['differentOddEven']) &&
+ self::boolean((string) $xmlSheet->headerFooter['differentOddEven'])) {
+ $docHeaderFooter->setDifferentOddEven(true);
+ } else {
+ $docHeaderFooter->setDifferentOddEven(false);
+ }
+ if (isset($xmlSheet->headerFooter['differentFirst']) &&
+ self::boolean((string) $xmlSheet->headerFooter['differentFirst'])) {
+ $docHeaderFooter->setDifferentFirst(true);
+ } else {
+ $docHeaderFooter->setDifferentFirst(false);
+ }
+ if (isset($xmlSheet->headerFooter['scaleWithDoc']) &&
+ !self::boolean((string) $xmlSheet->headerFooter['scaleWithDoc'])) {
+ $docHeaderFooter->setScaleWithDocument(false);
+ } else {
+ $docHeaderFooter->setScaleWithDocument(true);
+ }
+ if (isset($xmlSheet->headerFooter['alignWithMargins']) &&
+ !self::boolean((string) $xmlSheet->headerFooter['alignWithMargins'])) {
+ $docHeaderFooter->setAlignWithMargins(false);
+ } else {
+ $docHeaderFooter->setAlignWithMargins(true);
+ }
+
+ $docHeaderFooter->setOddHeader((string) $xmlSheet->headerFooter->oddHeader);
+ $docHeaderFooter->setOddFooter((string) $xmlSheet->headerFooter->oddFooter);
+ $docHeaderFooter->setEvenHeader((string) $xmlSheet->headerFooter->evenHeader);
+ $docHeaderFooter->setEvenFooter((string) $xmlSheet->headerFooter->evenFooter);
+ $docHeaderFooter->setFirstHeader((string) $xmlSheet->headerFooter->firstHeader);
+ $docHeaderFooter->setFirstFooter((string) $xmlSheet->headerFooter->firstFooter);
+ }
+
+ if ($xmlSheet && $xmlSheet->rowBreaks && $xmlSheet->rowBreaks->brk && !$this->readDataOnly) {
+ foreach ($xmlSheet->rowBreaks->brk as $brk) {
+ if ($brk['man']) {
+ $docSheet->setBreak("A$brk[id]", Worksheet::BREAK_ROW);
+ }
+ }
+ }
+ if ($xmlSheet && $xmlSheet->colBreaks && $xmlSheet->colBreaks->brk && !$this->readDataOnly) {
+ foreach ($xmlSheet->colBreaks->brk as $brk) {
+ if ($brk['man']) {
+ $docSheet->setBreak(Coordinate::stringFromColumnIndex((string) $brk['id'] + 1) . '1', Worksheet::BREAK_COLUMN);
+ }
+ }
+ }
+
+ if ($xmlSheet && $xmlSheet->dataValidations && !$this->readDataOnly) {
+ foreach ($xmlSheet->dataValidations->dataValidation as $dataValidation) {
+ // Uppercase coordinate
+ $range = strtoupper($dataValidation['sqref']);
+ $rangeSet = explode(' ', $range);
+ foreach ($rangeSet as $range) {
+ $stRange = $docSheet->shrinkRangeToFit($range);
+
+ // Extract all cell references in $range
+ foreach (Coordinate::extractAllCellReferencesInRange($stRange) as $reference) {
+ // Create validation
+ $docValidation = $docSheet->getCell($reference)->getDataValidation();
+ $docValidation->setType((string) $dataValidation['type']);
+ $docValidation->setErrorStyle((string) $dataValidation['errorStyle']);
+ $docValidation->setOperator((string) $dataValidation['operator']);
+ $docValidation->setAllowBlank($dataValidation['allowBlank'] != 0);
+ $docValidation->setShowDropDown($dataValidation['showDropDown'] == 0);
+ $docValidation->setShowInputMessage($dataValidation['showInputMessage'] != 0);
+ $docValidation->setShowErrorMessage($dataValidation['showErrorMessage'] != 0);
+ $docValidation->setErrorTitle((string) $dataValidation['errorTitle']);
+ $docValidation->setError((string) $dataValidation['error']);
+ $docValidation->setPromptTitle((string) $dataValidation['promptTitle']);
+ $docValidation->setPrompt((string) $dataValidation['prompt']);
+ $docValidation->setFormula1((string) $dataValidation->formula1);
+ $docValidation->setFormula2((string) $dataValidation->formula2);
+ }
+ }
+ }
+ }
+
+ // unparsed sheet AlternateContent
+ if ($xmlSheet && !$this->readDataOnly) {
+ $mc = $xmlSheet->children('http://schemas.openxmlformats.org/markup-compatibility/2006');
+ if ($mc->AlternateContent) {
+ foreach ($mc->AlternateContent as $alternateContent) {
+ $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['AlternateContents'][] = $alternateContent->asXML();
+ }
+ }
+ }
+
+ // Add hyperlinks
+ $hyperlinks = [];
+ if (!$this->readDataOnly) {
+ // Locate hyperlink relations
+ if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) {
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsWorksheet = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ foreach ($relsWorksheet->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink') {
+ $hyperlinks[(string) $ele['Id']] = (string) $ele['Target'];
+ }
+ }
+ }
+
+ // Loop through hyperlinks
+ if ($xmlSheet && $xmlSheet->hyperlinks) {
+ /** @var SimpleXMLElement $hyperlink */
+ foreach ($xmlSheet->hyperlinks->hyperlink as $hyperlink) {
+ // Link url
+ $linkRel = $hyperlink->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships');
+
+ foreach (Coordinate::extractAllCellReferencesInRange($hyperlink['ref']) as $cellReference) {
+ $cell = $docSheet->getCell($cellReference);
+ if (isset($linkRel['id'])) {
+ $hyperlinkUrl = $hyperlinks[(string) $linkRel['id']];
+ if (isset($hyperlink['location'])) {
+ $hyperlinkUrl .= '#' . (string) $hyperlink['location'];
+ }
+ $cell->getHyperlink()->setUrl($hyperlinkUrl);
+ } elseif (isset($hyperlink['location'])) {
+ $cell->getHyperlink()->setUrl('sheet://' . (string) $hyperlink['location']);
+ }
+
+ // Tooltip
+ if (isset($hyperlink['tooltip'])) {
+ $cell->getHyperlink()->setTooltip((string) $hyperlink['tooltip']);
+ }
+ }
+ }
+ }
+ }
+
+ // Add comments
+ $comments = [];
+ $vmlComments = [];
+ if (!$this->readDataOnly) {
+ // Locate comment relations
+ if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) {
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsWorksheet = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ foreach ($relsWorksheet->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments') {
+ $comments[(string) $ele['Id']] = (string) $ele['Target'];
+ }
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing') {
+ $vmlComments[(string) $ele['Id']] = (string) $ele['Target'];
+ }
+ }
+ }
+
+ // Loop through comments
+ foreach ($comments as $relName => $relPath) {
+ // Load comments file
+ $relPath = File::realpath(dirname("$dir/$fileWorksheet") . '/' . $relPath);
+ $commentsFile = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, $relPath)),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+
+ // Utility variables
+ $authors = [];
+
+ // Loop through authors
+ foreach ($commentsFile->authors->author as $author) {
+ $authors[] = (string) $author;
+ }
+
+ // Loop through contents
+ foreach ($commentsFile->commentList->comment as $comment) {
+ if (!empty($comment['authorId'])) {
+ $docSheet->getComment((string) $comment['ref'])->setAuthor($authors[(string) $comment['authorId']]);
+ }
+ $docSheet->getComment((string) $comment['ref'])->setText($this->parseRichText($comment->text));
+ }
+ }
+
+ // later we will remove from it real vmlComments
+ $unparsedVmlDrawings = $vmlComments;
+
+ // Loop through VML comments
+ foreach ($vmlComments as $relName => $relPath) {
+ // Load VML comments file
+ $relPath = File::realpath(dirname("$dir/$fileWorksheet") . '/' . $relPath);
+ $vmlCommentsFile = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, $relPath)),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $vmlCommentsFile->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml');
+
+ $shapes = $vmlCommentsFile->xpath('//v:shape');
+ foreach ($shapes as $shape) {
+ $shape->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml');
+
+ if (isset($shape['style'])) {
+ $style = (string) $shape['style'];
+ $fillColor = strtoupper(substr((string) $shape['fillcolor'], 1));
+ $column = null;
+ $row = null;
+
+ $clientData = $shape->xpath('.//x:ClientData');
+ if (is_array($clientData) && !empty($clientData)) {
+ $clientData = $clientData[0];
+
+ if (isset($clientData['ObjectType']) && (string) $clientData['ObjectType'] == 'Note') {
+ $temp = $clientData->xpath('.//x:Row');
+ if (is_array($temp)) {
+ $row = $temp[0];
+ }
+
+ $temp = $clientData->xpath('.//x:Column');
+ if (is_array($temp)) {
+ $column = $temp[0];
+ }
+ }
+ }
+
+ if (($column !== null) && ($row !== null)) {
+ // Set comment properties
+ $comment = $docSheet->getCommentByColumnAndRow($column + 1, $row + 1);
+ $comment->getFillColor()->setRGB($fillColor);
+
+ // Parse style
+ $styleArray = explode(';', str_replace(' ', '', $style));
+ foreach ($styleArray as $stylePair) {
+ $stylePair = explode(':', $stylePair);
+
+ if ($stylePair[0] == 'margin-left') {
+ $comment->setMarginLeft($stylePair[1]);
+ }
+ if ($stylePair[0] == 'margin-top') {
+ $comment->setMarginTop($stylePair[1]);
+ }
+ if ($stylePair[0] == 'width') {
+ $comment->setWidth($stylePair[1]);
+ }
+ if ($stylePair[0] == 'height') {
+ $comment->setHeight($stylePair[1]);
+ }
+ if ($stylePair[0] == 'visibility') {
+ $comment->setVisible($stylePair[1] == 'visible');
+ }
+ }
+
+ unset($unparsedVmlDrawings[$relName]);
+ }
+ }
+ }
+ }
+
+ // unparsed vmlDrawing
+ if ($unparsedVmlDrawings) {
+ foreach ($unparsedVmlDrawings as $rId => $relPath) {
+ $rId = substr($rId, 3); // rIdXXX
+ $unparsedVmlDrawing = &$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['vmlDrawings'];
+ $unparsedVmlDrawing[$rId] = [];
+ $unparsedVmlDrawing[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $relPath);
+ $unparsedVmlDrawing[$rId]['relFilePath'] = $relPath;
+ $unparsedVmlDrawing[$rId]['content'] = $this->securityScanner->scan($this->getFromZipArchive($zip, $unparsedVmlDrawing[$rId]['filePath']));
+ unset($unparsedVmlDrawing);
+ }
+ }
+
+ // Header/footer images
+ if ($xmlSheet && $xmlSheet->legacyDrawingHF && !$this->readDataOnly) {
+ if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) {
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsWorksheet = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $vmlRelationship = '';
+
+ foreach ($relsWorksheet->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing') {
+ $vmlRelationship = self::dirAdd("$dir/$fileWorksheet", $ele['Target']);
+ }
+ }
+
+ if ($vmlRelationship != '') {
+ // Fetch linked images
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsVML = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, dirname($vmlRelationship) . '/_rels/' . basename($vmlRelationship) . '.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $drawings = [];
+ foreach ($relsVML->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image') {
+ $drawings[(string) $ele['Id']] = self::dirAdd($vmlRelationship, $ele['Target']);
+ }
+ }
+
+ // Fetch VML document
+ $vmlDrawing = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, $vmlRelationship)),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $vmlDrawing->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml');
+
+ $hfImages = [];
+
+ $shapes = $vmlDrawing->xpath('//v:shape');
+ foreach ($shapes as $idx => $shape) {
+ $shape->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml');
+ $imageData = $shape->xpath('//v:imagedata');
+
+ if (!$imageData) {
+ continue;
+ }
+
+ $imageData = $imageData[$idx];
+
+ $imageData = $imageData->attributes('urn:schemas-microsoft-com:office:office');
+ $style = self::toCSSArray((string) $shape['style']);
+
+ $hfImages[(string) $shape['id']] = new HeaderFooterDrawing();
+ if (isset($imageData['title'])) {
+ $hfImages[(string) $shape['id']]->setName((string) $imageData['title']);
+ }
+
+ $hfImages[(string) $shape['id']]->setPath('zip://' . File::realpath($pFilename) . '#' . $drawings[(string) $imageData['relid']], false);
+ $hfImages[(string) $shape['id']]->setResizeProportional(false);
+ $hfImages[(string) $shape['id']]->setWidth($style['width']);
+ $hfImages[(string) $shape['id']]->setHeight($style['height']);
+ if (isset($style['margin-left'])) {
+ $hfImages[(string) $shape['id']]->setOffsetX($style['margin-left']);
+ }
+ $hfImages[(string) $shape['id']]->setOffsetY($style['margin-top']);
+ $hfImages[(string) $shape['id']]->setResizeProportional(true);
+ }
+
+ $docSheet->getHeaderFooter()->setImages($hfImages);
+ }
+ }
+ }
+ }
+
+ // TODO: Autoshapes from twoCellAnchors!
+ if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) {
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsWorksheet = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $drawings = [];
+ foreach ($relsWorksheet->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing') {
+ $drawings[(string) $ele['Id']] = self::dirAdd("$dir/$fileWorksheet", $ele['Target']);
+ }
+ }
+ if ($xmlSheet->drawing && !$this->readDataOnly) {
+ foreach ($xmlSheet->drawing as $drawing) {
+ $fileDrawing = $drawings[(string) self::getArrayItem($drawing->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), 'id')];
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsDrawing = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, dirname($fileDrawing) . '/_rels/' . basename($fileDrawing) . '.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $images = [];
+ $hyperlinks = [];
+ if ($relsDrawing && $relsDrawing->Relationship) {
+ foreach ($relsDrawing->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink') {
+ $hyperlinks[(string) $ele['Id']] = (string) $ele['Target'];
+ }
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image') {
+ $images[(string) $ele['Id']] = self::dirAdd($fileDrawing, $ele['Target']);
+ } elseif ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart') {
+ if ($this->includeCharts) {
+ $charts[self::dirAdd($fileDrawing, $ele['Target'])] = [
+ 'id' => (string) $ele['Id'],
+ 'sheet' => $docSheet->getTitle(),
+ ];
+ }
+ }
+ }
+ }
+ $xmlDrawing = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, $fileDrawing)),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ )->children('http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing');
+
+ if ($xmlDrawing->oneCellAnchor) {
+ foreach ($xmlDrawing->oneCellAnchor as $oneCellAnchor) {
+ if ($oneCellAnchor->pic->blipFill) {
+ /** @var SimpleXMLElement $blip */
+ $blip = $oneCellAnchor->pic->blipFill->children('http://schemas.openxmlformats.org/drawingml/2006/main')->blip;
+ /** @var SimpleXMLElement $xfrm */
+ $xfrm = $oneCellAnchor->pic->spPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->xfrm;
+ /** @var SimpleXMLElement $outerShdw */
+ $outerShdw = $oneCellAnchor->pic->spPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->effectLst->outerShdw;
+ /** @var \SimpleXMLElement $hlinkClick */
+ $hlinkClick = $oneCellAnchor->pic->nvPicPr->cNvPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->hlinkClick;
+
+ $objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
+ $objDrawing->setName((string) self::getArrayItem($oneCellAnchor->pic->nvPicPr->cNvPr->attributes(), 'name'));
+ $objDrawing->setDescription((string) self::getArrayItem($oneCellAnchor->pic->nvPicPr->cNvPr->attributes(), 'descr'));
+ $objDrawing->setPath(
+ 'zip://' . File::realpath($pFilename) . '#' .
+ $images[(string) self::getArrayItem(
+ $blip->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'),
+ 'embed'
+ )],
+ false
+ );
+ $objDrawing->setCoordinates(Coordinate::stringFromColumnIndex(((string) $oneCellAnchor->from->col) + 1) . ($oneCellAnchor->from->row + 1));
+ $objDrawing->setOffsetX(Drawing::EMUToPixels($oneCellAnchor->from->colOff));
+ $objDrawing->setOffsetY(Drawing::EMUToPixels($oneCellAnchor->from->rowOff));
+ $objDrawing->setResizeProportional(false);
+ $objDrawing->setWidth(Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cx')));
+ $objDrawing->setHeight(Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cy')));
+ if ($xfrm) {
+ $objDrawing->setRotation(Drawing::angleToDegrees(self::getArrayItem($xfrm->attributes(), 'rot')));
+ }
+ if ($outerShdw) {
+ $shadow = $objDrawing->getShadow();
+ $shadow->setVisible(true);
+ $shadow->setBlurRadius(Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), 'blurRad')));
+ $shadow->setDistance(Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), 'dist')));
+ $shadow->setDirection(Drawing::angleToDegrees(self::getArrayItem($outerShdw->attributes(), 'dir')));
+ $shadow->setAlignment((string) self::getArrayItem($outerShdw->attributes(), 'algn'));
+ $shadow->getColor()->setRGB(self::getArrayItem($outerShdw->srgbClr->attributes(), 'val'));
+ $shadow->setAlpha(self::getArrayItem($outerShdw->srgbClr->alpha->attributes(), 'val') / 1000);
+ }
+
+ $this->readHyperLinkDrawing($objDrawing, $oneCellAnchor, $hyperlinks);
+
+ $objDrawing->setWorksheet($docSheet);
+ } else {
+ // ? Can charts be positioned with a oneCellAnchor ?
+ $coordinates = Coordinate::stringFromColumnIndex(((string) $oneCellAnchor->from->col) + 1) . ($oneCellAnchor->from->row + 1);
+ $offsetX = Drawing::EMUToPixels($oneCellAnchor->from->colOff);
+ $offsetY = Drawing::EMUToPixels($oneCellAnchor->from->rowOff);
+ $width = Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cx'));
+ $height = Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cy'));
+ }
+ }
+ }
+ if ($xmlDrawing->twoCellAnchor) {
+ foreach ($xmlDrawing->twoCellAnchor as $twoCellAnchor) {
+ if ($twoCellAnchor->pic->blipFill) {
+ $blip = $twoCellAnchor->pic->blipFill->children('http://schemas.openxmlformats.org/drawingml/2006/main')->blip;
+ $xfrm = $twoCellAnchor->pic->spPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->xfrm;
+ $outerShdw = $twoCellAnchor->pic->spPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->effectLst->outerShdw;
+ $hlinkClick = $twoCellAnchor->pic->nvPicPr->cNvPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->hlinkClick;
+ $objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
+ $objDrawing->setName((string) self::getArrayItem($twoCellAnchor->pic->nvPicPr->cNvPr->attributes(), 'name'));
+ $objDrawing->setDescription((string) self::getArrayItem($twoCellAnchor->pic->nvPicPr->cNvPr->attributes(), 'descr'));
+ $objDrawing->setPath(
+ 'zip://' . File::realpath($pFilename) . '#' .
+ $images[(string) self::getArrayItem(
+ $blip->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'),
+ 'embed'
+ )],
+ false
+ );
+ $objDrawing->setCoordinates(Coordinate::stringFromColumnIndex(((string) $twoCellAnchor->from->col) + 1) . ($twoCellAnchor->from->row + 1));
+ $objDrawing->setOffsetX(Drawing::EMUToPixels($twoCellAnchor->from->colOff));
+ $objDrawing->setOffsetY(Drawing::EMUToPixels($twoCellAnchor->from->rowOff));
+ $objDrawing->setResizeProportional(false);
+
+ if ($xfrm) {
+ $objDrawing->setWidth(Drawing::EMUToPixels(self::getArrayItem($xfrm->ext->attributes(), 'cx')));
+ $objDrawing->setHeight(Drawing::EMUToPixels(self::getArrayItem($xfrm->ext->attributes(), 'cy')));
+ $objDrawing->setRotation(Drawing::angleToDegrees(self::getArrayItem($xfrm->attributes(), 'rot')));
+ }
+ if ($outerShdw) {
+ $shadow = $objDrawing->getShadow();
+ $shadow->setVisible(true);
+ $shadow->setBlurRadius(Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), 'blurRad')));
+ $shadow->setDistance(Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), 'dist')));
+ $shadow->setDirection(Drawing::angleToDegrees(self::getArrayItem($outerShdw->attributes(), 'dir')));
+ $shadow->setAlignment((string) self::getArrayItem($outerShdw->attributes(), 'algn'));
+ $shadow->getColor()->setRGB(self::getArrayItem($outerShdw->srgbClr->attributes(), 'val'));
+ $shadow->setAlpha(self::getArrayItem($outerShdw->srgbClr->alpha->attributes(), 'val') / 1000);
+ }
+
+ $this->readHyperLinkDrawing($objDrawing, $twoCellAnchor, $hyperlinks);
+
+ $objDrawing->setWorksheet($docSheet);
+ } elseif (($this->includeCharts) && ($twoCellAnchor->graphicFrame)) {
+ $fromCoordinate = Coordinate::stringFromColumnIndex(((string) $twoCellAnchor->from->col) + 1) . ($twoCellAnchor->from->row + 1);
+ $fromOffsetX = Drawing::EMUToPixels($twoCellAnchor->from->colOff);
+ $fromOffsetY = Drawing::EMUToPixels($twoCellAnchor->from->rowOff);
+ $toCoordinate = Coordinate::stringFromColumnIndex(((string) $twoCellAnchor->to->col) + 1) . ($twoCellAnchor->to->row + 1);
+ $toOffsetX = Drawing::EMUToPixels($twoCellAnchor->to->colOff);
+ $toOffsetY = Drawing::EMUToPixels($twoCellAnchor->to->rowOff);
+ $graphic = $twoCellAnchor->graphicFrame->children('http://schemas.openxmlformats.org/drawingml/2006/main')->graphic;
+ /** @var SimpleXMLElement $chartRef */
+ $chartRef = $graphic->graphicData->children('http://schemas.openxmlformats.org/drawingml/2006/chart')->chart;
+ $thisChart = (string) $chartRef->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships');
+
+ $chartDetails[$docSheet->getTitle() . '!' . $thisChart] = [
+ 'fromCoordinate' => $fromCoordinate,
+ 'fromOffsetX' => $fromOffsetX,
+ 'fromOffsetY' => $fromOffsetY,
+ 'toCoordinate' => $toCoordinate,
+ 'toOffsetX' => $toOffsetX,
+ 'toOffsetY' => $toOffsetY,
+ 'worksheetTitle' => $docSheet->getTitle(),
+ ];
+ }
+ }
+ }
+ }
+
+ // store original rId of drawing files
+ $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['drawingOriginalIds'] = [];
+ foreach ($relsWorksheet->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing') {
+ $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['drawingOriginalIds'][(string) $ele['Target']] = (string) $ele['Id'];
+ }
+ }
+
+ // unparsed drawing AlternateContent
+ $xmlAltDrawing = simplexml_load_string(
+ $this->securityScanner->scan($this->getFromZipArchive($zip, $fileDrawing)),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ )->children('http://schemas.openxmlformats.org/markup-compatibility/2006');
+
+ if ($xmlAltDrawing->AlternateContent) {
+ foreach ($xmlAltDrawing->AlternateContent as $alternateContent) {
+ $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['drawingAlternateContents'][] = $alternateContent->asXML();
+ }
+ }
+ }
+ }
+
+ $this->readFormControlProperties($excel, $zip, $dir, $fileWorksheet, $docSheet, $unparsedLoadedData);
+ $this->readPrinterSettings($excel, $zip, $dir, $fileWorksheet, $docSheet, $unparsedLoadedData);
+
+ // Loop through definedNames
+ if ($xmlWorkbook->definedNames) {
+ foreach ($xmlWorkbook->definedNames->definedName as $definedName) {
+ // Extract range
+ $extractedRange = (string) $definedName;
+ if (($spos = strpos($extractedRange, '!')) !== false) {
+ $extractedRange = substr($extractedRange, 0, $spos) . str_replace('$', '', substr($extractedRange, $spos));
+ } else {
+ $extractedRange = str_replace('$', '', $extractedRange);
+ }
+
+ // Valid range?
+ if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') {
+ continue;
+ }
+
+ // Some definedNames are only applicable if we are on the same sheet...
+ if ((string) $definedName['localSheetId'] != '' && (string) $definedName['localSheetId'] == $oldSheetId) {
+ // Switch on type
+ switch ((string) $definedName['name']) {
+ case '_xlnm._FilterDatabase':
+ if ((string) $definedName['hidden'] !== '1') {
+ $extractedRange = explode(',', $extractedRange);
+ foreach ($extractedRange as $range) {
+ $autoFilterRange = $range;
+ if (strpos($autoFilterRange, ':') !== false) {
+ $docSheet->getAutoFilter()->setRange($autoFilterRange);
+ }
+ }
+ }
+
+ break;
+ case '_xlnm.Print_Titles':
+ // Split $extractedRange
+ $extractedRange = explode(',', $extractedRange);
+
+ // Set print titles
+ foreach ($extractedRange as $range) {
+ $matches = [];
+ $range = str_replace('$', '', $range);
+
+ // check for repeating columns, e g. 'A:A' or 'A:D'
+ if (preg_match('/!?([A-Z]+)\:([A-Z]+)$/', $range, $matches)) {
+ $docSheet->getPageSetup()->setColumnsToRepeatAtLeft([$matches[1], $matches[2]]);
+ } elseif (preg_match('/!?(\d+)\:(\d+)$/', $range, $matches)) {
+ // check for repeating rows, e.g. '1:1' or '1:5'
+ $docSheet->getPageSetup()->setRowsToRepeatAtTop([$matches[1], $matches[2]]);
+ }
+ }
+
+ break;
+ case '_xlnm.Print_Area':
+ $rangeSets = preg_split("/('?(?:.*?)'?(?:![A-Z0-9]+:[A-Z0-9]+)),?/", $extractedRange, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
+ $newRangeSets = [];
+ foreach ($rangeSets as $rangeSet) {
+ list($sheetName, $rangeSet) = Worksheet::extractSheetTitle($rangeSet, true);
+ if (strpos($rangeSet, ':') === false) {
+ $rangeSet = $rangeSet . ':' . $rangeSet;
+ }
+ $newRangeSets[] = str_replace('$', '', $rangeSet);
+ }
+ $docSheet->getPageSetup()->setPrintArea(implode(',', $newRangeSets));
+
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ }
+
+ // Next sheet id
+ ++$sheetId;
+ }
+
+ // Loop through definedNames
+ if ($xmlWorkbook->definedNames) {
+ foreach ($xmlWorkbook->definedNames->definedName as $definedName) {
+ // Extract range
+ $extractedRange = (string) $definedName;
+ if (($spos = strpos($extractedRange, '!')) !== false) {
+ $extractedRange = substr($extractedRange, 0, $spos) . str_replace('$', '', substr($extractedRange, $spos));
+ } else {
+ $extractedRange = str_replace('$', '', $extractedRange);
+ }
+
+ // Valid range?
+ if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') {
+ continue;
+ }
+
+ // Some definedNames are only applicable if we are on the same sheet...
+ if ((string) $definedName['localSheetId'] != '') {
+ // Local defined name
+ // Switch on type
+ switch ((string) $definedName['name']) {
+ case '_xlnm._FilterDatabase':
+ case '_xlnm.Print_Titles':
+ case '_xlnm.Print_Area':
+ break;
+ default:
+ if ($mapSheetId[(int) $definedName['localSheetId']] !== null) {
+ if (strpos((string) $definedName, '!') !== false) {
+ $range = Worksheet::extractSheetTitle((string) $definedName, true);
+ $range[0] = str_replace("''", "'", $range[0]);
+ $range[0] = str_replace("'", '', $range[0]);
+ if ($worksheet = $docSheet->getParent()->getSheetByName($range[0])) {
+ $extractedRange = str_replace('$', '', $range[1]);
+ $scope = $docSheet->getParent()->getSheet($mapSheetId[(int) $definedName['localSheetId']]);
+ $excel->addNamedRange(new NamedRange((string) $definedName['name'], $worksheet, $extractedRange, true, $scope));
+ }
+ }
+ }
+
+ break;
+ }
+ } elseif (!isset($definedName['localSheetId'])) {
+ // "Global" definedNames
+ $locatedSheet = null;
+ $extractedSheetName = '';
+ if (strpos((string) $definedName, '!') !== false) {
+ // Extract sheet name
+ $extractedSheetName = Worksheet::extractSheetTitle((string) $definedName, true);
+ $extractedSheetName = $extractedSheetName[0];
+
+ // Locate sheet
+ $locatedSheet = $excel->getSheetByName($extractedSheetName);
+
+ // Modify range
+ list($worksheetName, $extractedRange) = Worksheet::extractSheetTitle($extractedRange, true);
+ }
+
+ if ($locatedSheet !== null) {
+ $excel->addNamedRange(new NamedRange((string) $definedName['name'], $locatedSheet, $extractedRange, false));
+ }
+ }
+ }
+ }
+ }
+
+ if ((!$this->readDataOnly) || (!empty($this->loadSheetsOnly))) {
+ $workbookView = $xmlWorkbook->bookViews->workbookView;
+
+ // active sheet index
+ $activeTab = (int) ($workbookView['activeTab']); // refers to old sheet index
+
+ // keep active sheet index if sheet is still loaded, else first sheet is set as the active
+ if (isset($mapSheetId[$activeTab]) && $mapSheetId[$activeTab] !== null) {
+ $excel->setActiveSheetIndex($mapSheetId[$activeTab]);
+ } else {
+ if ($excel->getSheetCount() == 0) {
+ $excel->createSheet();
+ }
+ $excel->setActiveSheetIndex(0);
+ }
+
+ if (isset($workbookView['showHorizontalScroll'])) {
+ $showHorizontalScroll = (string) $workbookView['showHorizontalScroll'];
+ $excel->setShowHorizontalScroll($this->castXsdBooleanToBool($showHorizontalScroll));
+ }
+
+ if (isset($workbookView['showVerticalScroll'])) {
+ $showVerticalScroll = (string) $workbookView['showVerticalScroll'];
+ $excel->setShowVerticalScroll($this->castXsdBooleanToBool($showVerticalScroll));
+ }
+
+ if (isset($workbookView['showSheetTabs'])) {
+ $showSheetTabs = (string) $workbookView['showSheetTabs'];
+ $excel->setShowSheetTabs($this->castXsdBooleanToBool($showSheetTabs));
+ }
+
+ if (isset($workbookView['minimized'])) {
+ $minimized = (string) $workbookView['minimized'];
+ $excel->setMinimized($this->castXsdBooleanToBool($minimized));
+ }
+
+ if (isset($workbookView['autoFilterDateGrouping'])) {
+ $autoFilterDateGrouping = (string) $workbookView['autoFilterDateGrouping'];
+ $excel->setAutoFilterDateGrouping($this->castXsdBooleanToBool($autoFilterDateGrouping));
+ }
+
+ if (isset($workbookView['firstSheet'])) {
+ $firstSheet = (string) $workbookView['firstSheet'];
+ $excel->setFirstSheetIndex((int) $firstSheet);
+ }
+
+ if (isset($workbookView['visibility'])) {
+ $visibility = (string) $workbookView['visibility'];
+ $excel->setVisibility($visibility);
+ }
+
+ if (isset($workbookView['tabRatio'])) {
+ $tabRatio = (string) $workbookView['tabRatio'];
+ $excel->setTabRatio((int) $tabRatio);
+ }
+ }
+
+ break;
+ }
+ }
+
+ if (!$this->readDataOnly) {
+ $contentTypes = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, '[Content_Types].xml')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+
+ // Default content types
+ foreach ($contentTypes->Default as $contentType) {
+ switch ($contentType['ContentType']) {
+ case 'application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings':
+ $unparsedLoadedData['default_content_types'][(string) $contentType['Extension']] = (string) $contentType['ContentType'];
+
+ break;
+ }
+ }
+
+ // Override content types
+ foreach ($contentTypes->Override as $contentType) {
+ switch ($contentType['ContentType']) {
+ case 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml':
+ if ($this->includeCharts) {
+ $chartEntryRef = ltrim($contentType['PartName'], '/');
+ $chartElements = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, $chartEntryRef)
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $objChart = Chart::readChart($chartElements, basename($chartEntryRef, '.xml'));
+
+ if (isset($charts[$chartEntryRef])) {
+ $chartPositionRef = $charts[$chartEntryRef]['sheet'] . '!' . $charts[$chartEntryRef]['id'];
+ if (isset($chartDetails[$chartPositionRef])) {
+ $excel->getSheetByName($charts[$chartEntryRef]['sheet'])->addChart($objChart);
+ $objChart->setWorksheet($excel->getSheetByName($charts[$chartEntryRef]['sheet']));
+ $objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']);
+ $objChart->setBottomRightPosition($chartDetails[$chartPositionRef]['toCoordinate'], $chartDetails[$chartPositionRef]['toOffsetX'], $chartDetails[$chartPositionRef]['toOffsetY']);
+ }
+ }
+ }
+
+ break;
+
+ // unparsed
+ case 'application/vnd.ms-excel.controlproperties+xml':
+ $unparsedLoadedData['override_content_types'][(string) $contentType['PartName']] = (string) $contentType['ContentType'];
+
+ break;
+ }
+ }
+ }
+
+ $excel->setUnparsedLoadedData($unparsedLoadedData);
+
+ $zip->close();
+
+ return $excel;
+ }
+
+ private static function readColor($color, $background = false)
+ {
+ if (isset($color['rgb'])) {
+ return (string) $color['rgb'];
+ } elseif (isset($color['indexed'])) {
+ return Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
+ } elseif (isset($color['theme'])) {
+ if (self::$theme !== null) {
+ $returnColour = self::$theme->getColourByIndex((int) $color['theme']);
+ if (isset($color['tint'])) {
+ $tintAdjust = (float) $color['tint'];
+ $returnColour = Color::changeBrightness($returnColour, $tintAdjust);
+ }
+
+ return 'FF' . $returnColour;
+ }
+ }
+
+ if ($background) {
+ return 'FFFFFFFF';
+ }
+
+ return 'FF000000';
+ }
+
+ /**
+ * @param Style $docStyle
+ * @param SimpleXMLElement|\stdClass $style
+ */
+ private static function readStyle(Style $docStyle, $style)
+ {
+ $docStyle->getNumberFormat()->setFormatCode($style->numFmt);
+
+ // font
+ if (isset($style->font)) {
+ $docStyle->getFont()->setName((string) $style->font->name['val']);
+ $docStyle->getFont()->setSize((string) $style->font->sz['val']);
+ if (isset($style->font->b)) {
+ $docStyle->getFont()->setBold(!isset($style->font->b['val']) || self::boolean((string) $style->font->b['val']));
+ }
+ if (isset($style->font->i)) {
+ $docStyle->getFont()->setItalic(!isset($style->font->i['val']) || self::boolean((string) $style->font->i['val']));
+ }
+ if (isset($style->font->strike)) {
+ $docStyle->getFont()->setStrikethrough(!isset($style->font->strike['val']) || self::boolean((string) $style->font->strike['val']));
+ }
+ $docStyle->getFont()->getColor()->setARGB(self::readColor($style->font->color));
+
+ if (isset($style->font->u) && !isset($style->font->u['val'])) {
+ $docStyle->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
+ } elseif (isset($style->font->u, $style->font->u['val'])) {
+ $docStyle->getFont()->setUnderline((string) $style->font->u['val']);
+ }
+
+ if (isset($style->font->vertAlign, $style->font->vertAlign['val'])) {
+ $vertAlign = strtolower((string) $style->font->vertAlign['val']);
+ if ($vertAlign == 'superscript') {
+ $docStyle->getFont()->setSuperscript(true);
+ }
+ if ($vertAlign == 'subscript') {
+ $docStyle->getFont()->setSubscript(true);
+ }
+ }
+ }
+
+ // fill
+ if (isset($style->fill)) {
+ if ($style->fill->gradientFill) {
+ /** @var SimpleXMLElement $gradientFill */
+ $gradientFill = $style->fill->gradientFill[0];
+ if (!empty($gradientFill['type'])) {
+ $docStyle->getFill()->setFillType((string) $gradientFill['type']);
+ }
+ $docStyle->getFill()->setRotation((float) ($gradientFill['degree']));
+ $gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
+ $docStyle->getFill()->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
+ $docStyle->getFill()->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
+ } elseif ($style->fill->patternFill) {
+ $patternType = (string) $style->fill->patternFill['patternType'] != '' ? (string) $style->fill->patternFill['patternType'] : 'solid';
+ $docStyle->getFill()->setFillType($patternType);
+ if ($style->fill->patternFill->fgColor) {
+ $docStyle->getFill()->getStartColor()->setARGB(self::readColor($style->fill->patternFill->fgColor, true));
+ } else {
+ $docStyle->getFill()->getStartColor()->setARGB('FF000000');
+ }
+ if ($style->fill->patternFill->bgColor) {
+ $docStyle->getFill()->getEndColor()->setARGB(self::readColor($style->fill->patternFill->bgColor, true));
+ }
+ }
+ }
+
+ // border
+ if (isset($style->border)) {
+ $diagonalUp = self::boolean((string) $style->border['diagonalUp']);
+ $diagonalDown = self::boolean((string) $style->border['diagonalDown']);
+ if (!$diagonalUp && !$diagonalDown) {
+ $docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_NONE);
+ } elseif ($diagonalUp && !$diagonalDown) {
+ $docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_UP);
+ } elseif (!$diagonalUp && $diagonalDown) {
+ $docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_DOWN);
+ } else {
+ $docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_BOTH);
+ }
+ self::readBorder($docStyle->getBorders()->getLeft(), $style->border->left);
+ self::readBorder($docStyle->getBorders()->getRight(), $style->border->right);
+ self::readBorder($docStyle->getBorders()->getTop(), $style->border->top);
+ self::readBorder($docStyle->getBorders()->getBottom(), $style->border->bottom);
+ self::readBorder($docStyle->getBorders()->getDiagonal(), $style->border->diagonal);
+ }
+
+ // alignment
+ if (isset($style->alignment)) {
+ $docStyle->getAlignment()->setHorizontal((string) $style->alignment['horizontal']);
+ $docStyle->getAlignment()->setVertical((string) $style->alignment['vertical']);
+
+ $textRotation = 0;
+ if ((int) $style->alignment['textRotation'] <= 90) {
+ $textRotation = (int) $style->alignment['textRotation'];
+ } elseif ((int) $style->alignment['textRotation'] > 90) {
+ $textRotation = 90 - (int) $style->alignment['textRotation'];
+ }
+
+ $docStyle->getAlignment()->setTextRotation((int) $textRotation);
+ $docStyle->getAlignment()->setWrapText(self::boolean((string) $style->alignment['wrapText']));
+ $docStyle->getAlignment()->setShrinkToFit(self::boolean((string) $style->alignment['shrinkToFit']));
+ $docStyle->getAlignment()->setIndent((int) ((string) $style->alignment['indent']) > 0 ? (int) ((string) $style->alignment['indent']) : 0);
+ $docStyle->getAlignment()->setReadOrder((int) ((string) $style->alignment['readingOrder']) > 0 ? (int) ((string) $style->alignment['readingOrder']) : 0);
+ }
+
+ // protection
+ if (isset($style->protection)) {
+ if (isset($style->protection['locked'])) {
+ if (self::boolean((string) $style->protection['locked'])) {
+ $docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
+ } else {
+ $docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
+ }
+ }
+
+ if (isset($style->protection['hidden'])) {
+ if (self::boolean((string) $style->protection['hidden'])) {
+ $docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
+ } else {
+ $docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
+ }
+ }
+ }
+
+ // top-level style settings
+ if (isset($style->quotePrefix)) {
+ $docStyle->setQuotePrefix($style->quotePrefix);
+ }
+ }
+
+ /**
+ * @param Border $docBorder
+ * @param SimpleXMLElement $eleBorder
+ */
+ private static function readBorder(Border $docBorder, $eleBorder)
+ {
+ if (isset($eleBorder['style'])) {
+ $docBorder->setBorderStyle((string) $eleBorder['style']);
+ }
+ if (isset($eleBorder->color)) {
+ $docBorder->getColor()->setARGB(self::readColor($eleBorder->color));
+ }
+ }
+
+ /**
+ * @param SimpleXMLElement | null $is
+ *
+ * @return RichText
+ */
+ private function parseRichText($is)
+ {
+ $value = new RichText();
+
+ if (isset($is->t)) {
+ $value->createText(StringHelper::controlCharacterOOXML2PHP((string) $is->t));
+ } else {
+ if (is_object($is->r)) {
+ foreach ($is->r as $run) {
+ if (!isset($run->rPr)) {
+ $value->createText(StringHelper::controlCharacterOOXML2PHP((string) $run->t));
+ } else {
+ $objText = $value->createTextRun(StringHelper::controlCharacterOOXML2PHP((string) $run->t));
+
+ if (isset($run->rPr->rFont['val'])) {
+ $objText->getFont()->setName((string) $run->rPr->rFont['val']);
+ }
+ if (isset($run->rPr->sz['val'])) {
+ $objText->getFont()->setSize((float) $run->rPr->sz['val']);
+ }
+ if (isset($run->rPr->color)) {
+ $objText->getFont()->setColor(new Color(self::readColor($run->rPr->color)));
+ }
+ if ((isset($run->rPr->b['val']) && self::boolean((string) $run->rPr->b['val'])) ||
+ (isset($run->rPr->b) && !isset($run->rPr->b['val']))) {
+ $objText->getFont()->setBold(true);
+ }
+ if ((isset($run->rPr->i['val']) && self::boolean((string) $run->rPr->i['val'])) ||
+ (isset($run->rPr->i) && !isset($run->rPr->i['val']))) {
+ $objText->getFont()->setItalic(true);
+ }
+ if (isset($run->rPr->vertAlign, $run->rPr->vertAlign['val'])) {
+ $vertAlign = strtolower((string) $run->rPr->vertAlign['val']);
+ if ($vertAlign == 'superscript') {
+ $objText->getFont()->setSuperscript(true);
+ }
+ if ($vertAlign == 'subscript') {
+ $objText->getFont()->setSubscript(true);
+ }
+ }
+ if (isset($run->rPr->u) && !isset($run->rPr->u['val'])) {
+ $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
+ } elseif (isset($run->rPr->u, $run->rPr->u['val'])) {
+ $objText->getFont()->setUnderline((string) $run->rPr->u['val']);
+ }
+ if ((isset($run->rPr->strike['val']) && self::boolean((string) $run->rPr->strike['val'])) ||
+ (isset($run->rPr->strike) && !isset($run->rPr->strike['val']))) {
+ $objText->getFont()->setStrikethrough(true);
+ }
+ }
+ }
+ }
+ }
+
+ return $value;
+ }
+
+ /**
+ * @param Spreadsheet $excel
+ * @param mixed $customUITarget
+ * @param mixed $zip
+ */
+ private function readRibbon(Spreadsheet $excel, $customUITarget, $zip)
+ {
+ $baseDir = dirname($customUITarget);
+ $nameCustomUI = basename($customUITarget);
+ // get the xml file (ribbon)
+ $localRibbon = $this->getFromZipArchive($zip, $customUITarget);
+ $customUIImagesNames = [];
+ $customUIImagesBinaries = [];
+ // something like customUI/_rels/customUI.xml.rels
+ $pathRels = $baseDir . '/_rels/' . $nameCustomUI . '.rels';
+ $dataRels = $this->getFromZipArchive($zip, $pathRels);
+ if ($dataRels) {
+ // exists and not empty if the ribbon have some pictures (other than internal MSO)
+ $UIRels = simplexml_load_string(
+ $this->securityScanner->scan($dataRels),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ if (false !== $UIRels) {
+ // we need to save id and target to avoid parsing customUI.xml and "guess" if it's a pseudo callback who load the image
+ foreach ($UIRels->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image') {
+ // an image ?
+ $customUIImagesNames[(string) $ele['Id']] = (string) $ele['Target'];
+ $customUIImagesBinaries[(string) $ele['Target']] = $this->getFromZipArchive($zip, $baseDir . '/' . (string) $ele['Target']);
+ }
+ }
+ }
+ }
+ if ($localRibbon) {
+ $excel->setRibbonXMLData($customUITarget, $localRibbon);
+ if (count($customUIImagesNames) > 0 && count($customUIImagesBinaries) > 0) {
+ $excel->setRibbonBinObjects($customUIImagesNames, $customUIImagesBinaries);
+ } else {
+ $excel->setRibbonBinObjects(null, null);
+ }
+ } else {
+ $excel->setRibbonXMLData(null, null);
+ $excel->setRibbonBinObjects(null, null);
+ }
+ }
+
+ private static function getArrayItem($array, $key = 0)
+ {
+ return isset($array[$key]) ? $array[$key] : null;
+ }
+
+ private static function dirAdd($base, $add)
+ {
+ return preg_replace('~[^/]+/\.\./~', '', dirname($base) . "/$add");
+ }
+
+ private static function toCSSArray($style)
+ {
+ $style = trim(str_replace(["\r", "\n"], '', $style), ';');
+
+ $temp = explode(';', $style);
+ $style = [];
+ foreach ($temp as $item) {
+ $item = explode(':', $item);
+
+ if (strpos($item[1], 'px') !== false) {
+ $item[1] = str_replace('px', '', $item[1]);
+ }
+ if (strpos($item[1], 'pt') !== false) {
+ $item[1] = str_replace('pt', '', $item[1]);
+ $item[1] = Font::fontSizeToPixels($item[1]);
+ }
+ if (strpos($item[1], 'in') !== false) {
+ $item[1] = str_replace('in', '', $item[1]);
+ $item[1] = Font::inchSizeToPixels($item[1]);
+ }
+ if (strpos($item[1], 'cm') !== false) {
+ $item[1] = str_replace('cm', '', $item[1]);
+ $item[1] = Font::centimeterSizeToPixels($item[1]);
+ }
+
+ $style[$item[0]] = $item[1];
+ }
+
+ return $style;
+ }
+
+ private static function boolean($value)
+ {
+ if (is_object($value)) {
+ $value = (string) $value;
+ }
+ if (is_numeric($value)) {
+ return (bool) $value;
+ }
+
+ return $value === 'true' || $value === 'TRUE';
+ }
+
+ /**
+ * @param \PhpOffice\PhpSpreadsheet\Worksheet\Drawing $objDrawing
+ * @param \SimpleXMLElement $cellAnchor
+ * @param array $hyperlinks
+ */
+ private function readHyperLinkDrawing($objDrawing, $cellAnchor, $hyperlinks)
+ {
+ $hlinkClick = $cellAnchor->pic->nvPicPr->cNvPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->hlinkClick;
+
+ if ($hlinkClick->count() === 0) {
+ return;
+ }
+
+ $hlinkId = (string) $hlinkClick->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships')['id'];
+ $hyperlink = new Hyperlink(
+ $hyperlinks[$hlinkId],
+ (string) self::getArrayItem($cellAnchor->pic->nvPicPr->cNvPr->attributes(), 'name')
+ );
+ $objDrawing->setHyperlink($hyperlink);
+ }
+
+ private function readProtection(Spreadsheet $excel, SimpleXMLElement $xmlWorkbook)
+ {
+ if (!$xmlWorkbook->workbookProtection) {
+ return;
+ }
+
+ if ($xmlWorkbook->workbookProtection['lockRevision']) {
+ $excel->getSecurity()->setLockRevision((bool) $xmlWorkbook->workbookProtection['lockRevision']);
+ }
+
+ if ($xmlWorkbook->workbookProtection['lockStructure']) {
+ $excel->getSecurity()->setLockStructure((bool) $xmlWorkbook->workbookProtection['lockStructure']);
+ }
+
+ if ($xmlWorkbook->workbookProtection['lockWindows']) {
+ $excel->getSecurity()->setLockWindows((bool) $xmlWorkbook->workbookProtection['lockWindows']);
+ }
+
+ if ($xmlWorkbook->workbookProtection['revisionsPassword']) {
+ $excel->getSecurity()->setRevisionsPassword((string) $xmlWorkbook->workbookProtection['revisionsPassword'], true);
+ }
+
+ if ($xmlWorkbook->workbookProtection['workbookPassword']) {
+ $excel->getSecurity()->setWorkbookPassword((string) $xmlWorkbook->workbookProtection['workbookPassword'], true);
+ }
+ }
+
+ private function readFormControlProperties(Spreadsheet $excel, ZipArchive $zip, $dir, $fileWorksheet, $docSheet, array &$unparsedLoadedData)
+ {
+ if (!$zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) {
+ return;
+ }
+
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsWorksheet = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $ctrlProps = [];
+ foreach ($relsWorksheet->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/ctrlProp') {
+ $ctrlProps[(string) $ele['Id']] = $ele;
+ }
+ }
+
+ $unparsedCtrlProps = &$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['ctrlProps'];
+ foreach ($ctrlProps as $rId => $ctrlProp) {
+ $rId = substr($rId, 3); // rIdXXX
+ $unparsedCtrlProps[$rId] = [];
+ $unparsedCtrlProps[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $ctrlProp['Target']);
+ $unparsedCtrlProps[$rId]['relFilePath'] = (string) $ctrlProp['Target'];
+ $unparsedCtrlProps[$rId]['content'] = $this->securityScanner->scan($this->getFromZipArchive($zip, $unparsedCtrlProps[$rId]['filePath']));
+ }
+ unset($unparsedCtrlProps);
+ }
+
+ private function readPrinterSettings(Spreadsheet $excel, ZipArchive $zip, $dir, $fileWorksheet, $docSheet, array &$unparsedLoadedData)
+ {
+ if (!$zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) {
+ return;
+ }
+
+ //~ http://schemas.openxmlformats.org/package/2006/relationships"
+ $relsWorksheet = simplexml_load_string(
+ $this->securityScanner->scan(
+ $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')
+ ),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ $sheetPrinterSettings = [];
+ foreach ($relsWorksheet->Relationship as $ele) {
+ if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings') {
+ $sheetPrinterSettings[(string) $ele['Id']] = $ele;
+ }
+ }
+
+ $unparsedPrinterSettings = &$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['printerSettings'];
+ foreach ($sheetPrinterSettings as $rId => $printerSettings) {
+ $rId = substr($rId, 3); // rIdXXX
+ $unparsedPrinterSettings[$rId] = [];
+ $unparsedPrinterSettings[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $printerSettings['Target']);
+ $unparsedPrinterSettings[$rId]['relFilePath'] = (string) $printerSettings['Target'];
+ $unparsedPrinterSettings[$rId]['content'] = $this->securityScanner->scan($this->getFromZipArchive($zip, $unparsedPrinterSettings[$rId]['filePath']));
+ }
+ unset($unparsedPrinterSettings);
+ }
+
+ /**
+ * Convert an 'xsd:boolean' XML value to a PHP boolean value.
+ * A valid 'xsd:boolean' XML value can be one of the following
+ * four values: 'true', 'false', '1', '0'. It is case sensitive.
+ *
+ * Note that just doing '(bool) $xsdBoolean' is not safe,
+ * since '(bool) "false"' returns true.
+ *
+ * @see https://www.w3.org/TR/xmlschema11-2/#boolean
+ *
+ * @param string $xsdBoolean An XML string value of type 'xsd:boolean'
+ *
+ * @return bool Boolean value
+ */
+ private function castXsdBooleanToBool($xsdBoolean)
+ {
+ if ($xsdBoolean === 'false') {
+ return false;
+ }
+
+ return (bool) $xsdBoolean;
+ }
+
+ /**
+ * Read columns and rows attributes from XML and set them on the worksheet.
+ *
+ * @param SimpleXMLElement $xmlSheet
+ * @param Worksheet $docSheet
+ */
+ private function readColumnsAndRowsAttributes(SimpleXMLElement $xmlSheet, Worksheet $docSheet)
+ {
+ $columnsAttributes = [];
+ $rowsAttributes = [];
+ if (isset($xmlSheet->cols) && !$this->readDataOnly) {
+ foreach ($xmlSheet->cols->col as $col) {
+ for ($i = (int) ($col['min']); $i <= (int) ($col['max']); ++$i) {
+ if ($col['style'] && !$this->readDataOnly) {
+ $columnsAttributes[Coordinate::stringFromColumnIndex($i)]['xfIndex'] = (int) $col['style'];
+ }
+ if (self::boolean($col['hidden'])) {
+ $columnsAttributes[Coordinate::stringFromColumnIndex($i)]['visible'] = false;
+ }
+ if (self::boolean($col['collapsed'])) {
+ $columnsAttributes[Coordinate::stringFromColumnIndex($i)]['collapsed'] = true;
+ }
+ if ($col['outlineLevel'] > 0) {
+ $columnsAttributes[Coordinate::stringFromColumnIndex($i)]['outlineLevel'] = (int) $col['outlineLevel'];
+ }
+ $columnsAttributes[Coordinate::stringFromColumnIndex($i)]['width'] = (float) $col['width'];
+
+ if ((int) ($col['max']) == 16384) {
+ break;
+ }
+ }
+ }
+ }
+
+ if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) {
+ foreach ($xmlSheet->sheetData->row as $row) {
+ if ($row['ht'] && !$this->readDataOnly) {
+ $rowsAttributes[(int) $row['r']]['rowHeight'] = (float) $row['ht'];
+ }
+ if (self::boolean($row['hidden']) && !$this->readDataOnly) {
+ $rowsAttributes[(int) $row['r']]['visible'] = false;
+ }
+ if (self::boolean($row['collapsed'])) {
+ $rowsAttributes[(int) $row['r']]['collapsed'] = true;
+ }
+ if ($row['outlineLevel'] > 0) {
+ $rowsAttributes[(int) $row['r']]['outlineLevel'] = (int) $row['outlineLevel'];
+ }
+ if ($row['s'] && !$this->readDataOnly) {
+ $rowsAttributes[(int) $row['r']]['xfIndex'] = (int) $row['s'];
+ }
+ }
+ }
+
+ $readFilter = (\get_class($this->getReadFilter()) !== DefaultReadFilter::class ? $this->getReadFilter() : null);
+
+ // set columns/rows attributes
+ $columnsAttributesSet = [];
+ $rowsAttributesSet = [];
+ foreach ($columnsAttributes as $coordColumn => $columnAttributes) {
+ if ($readFilter !== null) {
+ foreach ($rowsAttributes as $coordRow => $rowAttributes) {
+ if (!$readFilter->readCell($coordColumn, $coordRow, $docSheet->getTitle())) {
+ continue 2;
+ }
+ }
+ }
+
+ if (!isset($columnsAttributesSet[$coordColumn])) {
+ $this->setColumnAttributes($docSheet, $coordColumn, $columnAttributes);
+ $columnsAttributesSet[$coordColumn] = true;
+ }
+ }
+
+ foreach ($rowsAttributes as $coordRow => $rowAttributes) {
+ if ($readFilter !== null) {
+ foreach ($columnsAttributes as $coordColumn => $columnAttributes) {
+ if (!$readFilter->readCell($coordColumn, $coordRow, $docSheet->getTitle())) {
+ continue 2;
+ }
+ }
+ }
+
+ if (!isset($rowsAttributesSet[$coordRow])) {
+ $this->setRowAttributes($docSheet, $coordRow, $rowAttributes);
+ $rowsAttributesSet[$coordRow] = true;
+ }
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xlsx/Chart.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xlsx/Chart.php
new file mode 100644
index 00000000000..2b920d705c3
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xlsx/Chart.php
@@ -0,0 +1,570 @@
+attributes();
+ if (isset($attributes[$name])) {
+ if ($format == 'string') {
+ return (string) $attributes[$name];
+ } elseif ($format == 'integer') {
+ return (int) $attributes[$name];
+ } elseif ($format == 'boolean') {
+ return (bool) ($attributes[$name] === '0' || $attributes[$name] !== 'true') ? false : true;
+ }
+
+ return (float) $attributes[$name];
+ }
+
+ return null;
+ }
+
+ private static function readColor($color, $background = false)
+ {
+ if (isset($color['rgb'])) {
+ return (string) $color['rgb'];
+ } elseif (isset($color['indexed'])) {
+ return Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
+ }
+ }
+
+ /**
+ * @param SimpleXMLElement $chartElements
+ * @param string $chartName
+ *
+ * @return \PhpOffice\PhpSpreadsheet\Chart\Chart
+ */
+ public static function readChart(SimpleXMLElement $chartElements, $chartName)
+ {
+ $namespacesChartMeta = $chartElements->getNamespaces(true);
+ $chartElementsC = $chartElements->children($namespacesChartMeta['c']);
+
+ $XaxisLabel = $YaxisLabel = $legend = $title = null;
+ $dispBlanksAs = $plotVisOnly = null;
+
+ foreach ($chartElementsC as $chartElementKey => $chartElement) {
+ switch ($chartElementKey) {
+ case 'chart':
+ foreach ($chartElement as $chartDetailsKey => $chartDetails) {
+ $chartDetailsC = $chartDetails->children($namespacesChartMeta['c']);
+ switch ($chartDetailsKey) {
+ case 'plotArea':
+ $plotAreaLayout = $XaxisLable = $YaxisLable = null;
+ $plotSeries = $plotAttributes = [];
+ foreach ($chartDetails as $chartDetailKey => $chartDetail) {
+ switch ($chartDetailKey) {
+ case 'layout':
+ $plotAreaLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta);
+
+ break;
+ case 'catAx':
+ if (isset($chartDetail->title)) {
+ $XaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta);
+ }
+
+ break;
+ case 'dateAx':
+ if (isset($chartDetail->title)) {
+ $XaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta);
+ }
+
+ break;
+ case 'valAx':
+ if (isset($chartDetail->title)) {
+ $YaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta);
+ }
+
+ break;
+ case 'barChart':
+ case 'bar3DChart':
+ $barDirection = self::getAttribute($chartDetail->barDir, 'val', 'string');
+ $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
+ $plotSer->setPlotDirection($barDirection);
+ $plotSeries[] = $plotSer;
+ $plotAttributes = self::readChartAttributes($chartDetail);
+
+ break;
+ case 'lineChart':
+ case 'line3DChart':
+ $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
+ $plotAttributes = self::readChartAttributes($chartDetail);
+
+ break;
+ case 'areaChart':
+ case 'area3DChart':
+ $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
+ $plotAttributes = self::readChartAttributes($chartDetail);
+
+ break;
+ case 'doughnutChart':
+ case 'pieChart':
+ case 'pie3DChart':
+ $explosion = isset($chartDetail->ser->explosion);
+ $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
+ $plotSer->setPlotStyle($explosion);
+ $plotSeries[] = $plotSer;
+ $plotAttributes = self::readChartAttributes($chartDetail);
+
+ break;
+ case 'scatterChart':
+ $scatterStyle = self::getAttribute($chartDetail->scatterStyle, 'val', 'string');
+ $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
+ $plotSer->setPlotStyle($scatterStyle);
+ $plotSeries[] = $plotSer;
+ $plotAttributes = self::readChartAttributes($chartDetail);
+
+ break;
+ case 'bubbleChart':
+ $bubbleScale = self::getAttribute($chartDetail->bubbleScale, 'val', 'integer');
+ $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
+ $plotSer->setPlotStyle($bubbleScale);
+ $plotSeries[] = $plotSer;
+ $plotAttributes = self::readChartAttributes($chartDetail);
+
+ break;
+ case 'radarChart':
+ $radarStyle = self::getAttribute($chartDetail->radarStyle, 'val', 'string');
+ $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
+ $plotSer->setPlotStyle($radarStyle);
+ $plotSeries[] = $plotSer;
+ $plotAttributes = self::readChartAttributes($chartDetail);
+
+ break;
+ case 'surfaceChart':
+ case 'surface3DChart':
+ $wireFrame = self::getAttribute($chartDetail->wireframe, 'val', 'boolean');
+ $plotSer = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
+ $plotSer->setPlotStyle($wireFrame);
+ $plotSeries[] = $plotSer;
+ $plotAttributes = self::readChartAttributes($chartDetail);
+
+ break;
+ case 'stockChart':
+ $plotSeries[] = self::chartDataSeries($chartDetail, $namespacesChartMeta, $chartDetailKey);
+ $plotAttributes = self::readChartAttributes($plotAreaLayout);
+
+ break;
+ }
+ }
+ if ($plotAreaLayout == null) {
+ $plotAreaLayout = new Layout();
+ }
+ $plotArea = new PlotArea($plotAreaLayout, $plotSeries);
+ self::setChartAttributes($plotAreaLayout, $plotAttributes);
+
+ break;
+ case 'plotVisOnly':
+ $plotVisOnly = self::getAttribute($chartDetails, 'val', 'string');
+
+ break;
+ case 'dispBlanksAs':
+ $dispBlanksAs = self::getAttribute($chartDetails, 'val', 'string');
+
+ break;
+ case 'title':
+ $title = self::chartTitle($chartDetails, $namespacesChartMeta);
+
+ break;
+ case 'legend':
+ $legendPos = 'r';
+ $legendLayout = null;
+ $legendOverlay = false;
+ foreach ($chartDetails as $chartDetailKey => $chartDetail) {
+ switch ($chartDetailKey) {
+ case 'legendPos':
+ $legendPos = self::getAttribute($chartDetail, 'val', 'string');
+
+ break;
+ case 'overlay':
+ $legendOverlay = self::getAttribute($chartDetail, 'val', 'boolean');
+
+ break;
+ case 'layout':
+ $legendLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta);
+
+ break;
+ }
+ }
+ $legend = new Legend($legendPos, $legendLayout, $legendOverlay);
+
+ break;
+ }
+ }
+ }
+ }
+ $chart = new \PhpOffice\PhpSpreadsheet\Chart\Chart($chartName, $title, $legend, $plotArea, $plotVisOnly, $dispBlanksAs, $XaxisLabel, $YaxisLabel);
+
+ return $chart;
+ }
+
+ private static function chartTitle(SimpleXMLElement $titleDetails, array $namespacesChartMeta)
+ {
+ $caption = [];
+ $titleLayout = null;
+ foreach ($titleDetails as $titleDetailKey => $chartDetail) {
+ switch ($titleDetailKey) {
+ case 'tx':
+ $titleDetails = $chartDetail->rich->children($namespacesChartMeta['a']);
+ foreach ($titleDetails as $titleKey => $titleDetail) {
+ switch ($titleKey) {
+ case 'p':
+ $titleDetailPart = $titleDetail->children($namespacesChartMeta['a']);
+ $caption[] = self::parseRichText($titleDetailPart);
+ }
+ }
+
+ break;
+ case 'layout':
+ $titleLayout = self::chartLayoutDetails($chartDetail, $namespacesChartMeta);
+
+ break;
+ }
+ }
+
+ return new Title($caption, $titleLayout);
+ }
+
+ private static function chartLayoutDetails($chartDetail, $namespacesChartMeta)
+ {
+ if (!isset($chartDetail->manualLayout)) {
+ return null;
+ }
+ $details = $chartDetail->manualLayout->children($namespacesChartMeta['c']);
+ if ($details === null) {
+ return null;
+ }
+ $layout = [];
+ foreach ($details as $detailKey => $detail) {
+ $layout[$detailKey] = self::getAttribute($detail, 'val', 'string');
+ }
+
+ return new Layout($layout);
+ }
+
+ private static function chartDataSeries($chartDetail, $namespacesChartMeta, $plotType)
+ {
+ $multiSeriesType = null;
+ $smoothLine = false;
+ $seriesLabel = $seriesCategory = $seriesValues = $plotOrder = [];
+
+ $seriesDetailSet = $chartDetail->children($namespacesChartMeta['c']);
+ foreach ($seriesDetailSet as $seriesDetailKey => $seriesDetails) {
+ switch ($seriesDetailKey) {
+ case 'grouping':
+ $multiSeriesType = self::getAttribute($chartDetail->grouping, 'val', 'string');
+
+ break;
+ case 'ser':
+ $marker = null;
+ $seriesIndex = '';
+ foreach ($seriesDetails as $seriesKey => $seriesDetail) {
+ switch ($seriesKey) {
+ case 'idx':
+ $seriesIndex = self::getAttribute($seriesDetail, 'val', 'integer');
+
+ break;
+ case 'order':
+ $seriesOrder = self::getAttribute($seriesDetail, 'val', 'integer');
+ $plotOrder[$seriesIndex] = $seriesOrder;
+
+ break;
+ case 'tx':
+ $seriesLabel[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta);
+
+ break;
+ case 'marker':
+ $marker = self::getAttribute($seriesDetail->symbol, 'val', 'string');
+
+ break;
+ case 'smooth':
+ $smoothLine = self::getAttribute($seriesDetail, 'val', 'boolean');
+
+ break;
+ case 'cat':
+ $seriesCategory[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta);
+
+ break;
+ case 'val':
+ $seriesValues[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
+
+ break;
+ case 'xVal':
+ $seriesCategory[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
+
+ break;
+ case 'yVal':
+ $seriesValues[$seriesIndex] = self::chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker);
+
+ break;
+ }
+ }
+ }
+ }
+
+ return new DataSeries($plotType, $multiSeriesType, $plotOrder, $seriesLabel, $seriesCategory, $seriesValues, $smoothLine);
+ }
+
+ private static function chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker = null)
+ {
+ if (isset($seriesDetail->strRef)) {
+ $seriesSource = (string) $seriesDetail->strRef->f;
+ $seriesData = self::chartDataSeriesValues($seriesDetail->strRef->strCache->children($namespacesChartMeta['c']), 's');
+
+ return new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker);
+ } elseif (isset($seriesDetail->numRef)) {
+ $seriesSource = (string) $seriesDetail->numRef->f;
+ $seriesData = self::chartDataSeriesValues($seriesDetail->numRef->numCache->children($namespacesChartMeta['c']));
+
+ return new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker);
+ } elseif (isset($seriesDetail->multiLvlStrRef)) {
+ $seriesSource = (string) $seriesDetail->multiLvlStrRef->f;
+ $seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlStrRef->multiLvlStrCache->children($namespacesChartMeta['c']), 's');
+ $seriesData['pointCount'] = count($seriesData['dataValues']);
+
+ return new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker);
+ } elseif (isset($seriesDetail->multiLvlNumRef)) {
+ $seriesSource = (string) $seriesDetail->multiLvlNumRef->f;
+ $seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlNumRef->multiLvlNumCache->children($namespacesChartMeta['c']), 's');
+ $seriesData['pointCount'] = count($seriesData['dataValues']);
+
+ return new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker);
+ }
+
+ return null;
+ }
+
+ private static function chartDataSeriesValues($seriesValueSet, $dataType = 'n')
+ {
+ $seriesVal = [];
+ $formatCode = '';
+ $pointCount = 0;
+
+ foreach ($seriesValueSet as $seriesValueIdx => $seriesValue) {
+ switch ($seriesValueIdx) {
+ case 'ptCount':
+ $pointCount = self::getAttribute($seriesValue, 'val', 'integer');
+
+ break;
+ case 'formatCode':
+ $formatCode = (string) $seriesValue;
+
+ break;
+ case 'pt':
+ $pointVal = self::getAttribute($seriesValue, 'idx', 'integer');
+ if ($dataType == 's') {
+ $seriesVal[$pointVal] = (string) $seriesValue->v;
+ } elseif ($seriesValue->v === Functions::NA()) {
+ $seriesVal[$pointVal] = null;
+ } else {
+ $seriesVal[$pointVal] = (float) $seriesValue->v;
+ }
+
+ break;
+ }
+ }
+
+ return [
+ 'formatCode' => $formatCode,
+ 'pointCount' => $pointCount,
+ 'dataValues' => $seriesVal,
+ ];
+ }
+
+ private static function chartDataSeriesValuesMultiLevel($seriesValueSet, $dataType = 'n')
+ {
+ $seriesVal = [];
+ $formatCode = '';
+ $pointCount = 0;
+
+ foreach ($seriesValueSet->lvl as $seriesLevelIdx => $seriesLevel) {
+ foreach ($seriesLevel as $seriesValueIdx => $seriesValue) {
+ switch ($seriesValueIdx) {
+ case 'ptCount':
+ $pointCount = self::getAttribute($seriesValue, 'val', 'integer');
+
+ break;
+ case 'formatCode':
+ $formatCode = (string) $seriesValue;
+
+ break;
+ case 'pt':
+ $pointVal = self::getAttribute($seriesValue, 'idx', 'integer');
+ if ($dataType == 's') {
+ $seriesVal[$pointVal][] = (string) $seriesValue->v;
+ } elseif ($seriesValue->v === Functions::NA()) {
+ $seriesVal[$pointVal] = null;
+ } else {
+ $seriesVal[$pointVal][] = (float) $seriesValue->v;
+ }
+
+ break;
+ }
+ }
+ }
+
+ return [
+ 'formatCode' => $formatCode,
+ 'pointCount' => $pointCount,
+ 'dataValues' => $seriesVal,
+ ];
+ }
+
+ private static function parseRichText(SimpleXMLElement $titleDetailPart)
+ {
+ $value = new RichText();
+ $objText = null;
+ foreach ($titleDetailPart as $titleDetailElementKey => $titleDetailElement) {
+ if (isset($titleDetailElement->t)) {
+ $objText = $value->createTextRun((string) $titleDetailElement->t);
+ }
+ if (isset($titleDetailElement->rPr)) {
+ if (isset($titleDetailElement->rPr->rFont['val'])) {
+ $objText->getFont()->setName((string) $titleDetailElement->rPr->rFont['val']);
+ }
+
+ $fontSize = (self::getAttribute($titleDetailElement->rPr, 'sz', 'integer'));
+ if ($fontSize !== null) {
+ $objText->getFont()->setSize(floor($fontSize / 100));
+ }
+
+ $fontColor = (self::getAttribute($titleDetailElement->rPr, 'color', 'string'));
+ if ($fontColor !== null) {
+ $objText->getFont()->setColor(new Color(self::readColor($fontColor)));
+ }
+
+ $bold = self::getAttribute($titleDetailElement->rPr, 'b', 'boolean');
+ if ($bold !== null) {
+ $objText->getFont()->setBold($bold);
+ }
+
+ $italic = self::getAttribute($titleDetailElement->rPr, 'i', 'boolean');
+ if ($italic !== null) {
+ $objText->getFont()->setItalic($italic);
+ }
+
+ $baseline = self::getAttribute($titleDetailElement->rPr, 'baseline', 'integer');
+ if ($baseline !== null) {
+ if ($baseline > 0) {
+ $objText->getFont()->setSuperscript(true);
+ } elseif ($baseline < 0) {
+ $objText->getFont()->setSubscript(true);
+ }
+ }
+
+ $underscore = (self::getAttribute($titleDetailElement->rPr, 'u', 'string'));
+ if ($underscore !== null) {
+ if ($underscore == 'sng') {
+ $objText->getFont()->setUnderline(Font::UNDERLINE_SINGLE);
+ } elseif ($underscore == 'dbl') {
+ $objText->getFont()->setUnderline(Font::UNDERLINE_DOUBLE);
+ } else {
+ $objText->getFont()->setUnderline(Font::UNDERLINE_NONE);
+ }
+ }
+
+ $strikethrough = (self::getAttribute($titleDetailElement->rPr, 's', 'string'));
+ if ($strikethrough !== null) {
+ if ($strikethrough == 'noStrike') {
+ $objText->getFont()->setStrikethrough(false);
+ } else {
+ $objText->getFont()->setStrikethrough(true);
+ }
+ }
+ }
+ }
+
+ return $value;
+ }
+
+ private static function readChartAttributes($chartDetail)
+ {
+ $plotAttributes = [];
+ if (isset($chartDetail->dLbls)) {
+ if (isset($chartDetail->dLbls->howLegendKey)) {
+ $plotAttributes['showLegendKey'] = self::getAttribute($chartDetail->dLbls->showLegendKey, 'val', 'string');
+ }
+ if (isset($chartDetail->dLbls->showVal)) {
+ $plotAttributes['showVal'] = self::getAttribute($chartDetail->dLbls->showVal, 'val', 'string');
+ }
+ if (isset($chartDetail->dLbls->showCatName)) {
+ $plotAttributes['showCatName'] = self::getAttribute($chartDetail->dLbls->showCatName, 'val', 'string');
+ }
+ if (isset($chartDetail->dLbls->showSerName)) {
+ $plotAttributes['showSerName'] = self::getAttribute($chartDetail->dLbls->showSerName, 'val', 'string');
+ }
+ if (isset($chartDetail->dLbls->showPercent)) {
+ $plotAttributes['showPercent'] = self::getAttribute($chartDetail->dLbls->showPercent, 'val', 'string');
+ }
+ if (isset($chartDetail->dLbls->showBubbleSize)) {
+ $plotAttributes['showBubbleSize'] = self::getAttribute($chartDetail->dLbls->showBubbleSize, 'val', 'string');
+ }
+ if (isset($chartDetail->dLbls->showLeaderLines)) {
+ $plotAttributes['showLeaderLines'] = self::getAttribute($chartDetail->dLbls->showLeaderLines, 'val', 'string');
+ }
+ }
+
+ return $plotAttributes;
+ }
+
+ /**
+ * @param Layout $plotArea
+ * @param mixed $plotAttributes
+ */
+ private static function setChartAttributes(Layout $plotArea, $plotAttributes)
+ {
+ foreach ($plotAttributes as $plotAttributeKey => $plotAttributeValue) {
+ switch ($plotAttributeKey) {
+ case 'showLegendKey':
+ $plotArea->setShowLegendKey($plotAttributeValue);
+
+ break;
+ case 'showVal':
+ $plotArea->setShowVal($plotAttributeValue);
+
+ break;
+ case 'showCatName':
+ $plotArea->setShowCatName($plotAttributeValue);
+
+ break;
+ case 'showSerName':
+ $plotArea->setShowSerName($plotAttributeValue);
+
+ break;
+ case 'showPercent':
+ $plotArea->setShowPercent($plotAttributeValue);
+
+ break;
+ case 'showBubbleSize':
+ $plotArea->setShowBubbleSize($plotAttributeValue);
+
+ break;
+ case 'showLeaderLines':
+ $plotArea->setShowLeaderLines($plotAttributeValue);
+
+ break;
+ }
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xlsx/Theme.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xlsx/Theme.php
new file mode 100644
index 00000000000..c105f3c1a66
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xlsx/Theme.php
@@ -0,0 +1,93 @@
+themeName = $themeName;
+ $this->colourSchemeName = $colourSchemeName;
+ $this->colourMap = $colourMap;
+ }
+
+ /**
+ * Get Theme Name.
+ *
+ * @return string
+ */
+ public function getThemeName()
+ {
+ return $this->themeName;
+ }
+
+ /**
+ * Get colour Scheme Name.
+ *
+ * @return string
+ */
+ public function getColourSchemeName()
+ {
+ return $this->colourSchemeName;
+ }
+
+ /**
+ * Get colour Map Value by Position.
+ *
+ * @param mixed $index
+ *
+ * @return string
+ */
+ public function getColourByIndex($index)
+ {
+ if (isset($this->colourMap[$index])) {
+ return $this->colourMap[$index];
+ }
+
+ return null;
+ }
+
+ /**
+ * Implement PHP __clone to create a deep clone, not just a shallow copy.
+ */
+ public function __clone()
+ {
+ $vars = get_object_vars($this);
+ foreach ($vars as $key => $value) {
+ if ((is_object($value)) && ($key != '_parent')) {
+ $this->$key = clone $value;
+ } else {
+ $this->$key = $value;
+ }
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xml.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xml.php
new file mode 100644
index 00000000000..2b7959f47c1
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Reader/Xml.php
@@ -0,0 +1,882 @@
+readFilter = new DefaultReadFilter();
+ $this->securityScanner = XmlScanner::getInstance($this);
+ }
+
+ /**
+ * Can the current IReader read the file?
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return bool
+ */
+ public function canRead($pFilename)
+ {
+ // Office xmlns:o="urn:schemas-microsoft-com:office:office"
+ // Excel xmlns:x="urn:schemas-microsoft-com:office:excel"
+ // XML Spreadsheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
+ // Spreadsheet component xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet"
+ // XML schema xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
+ // XML data type xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
+ // MS-persist recordset xmlns:rs="urn:schemas-microsoft-com:rowset"
+ // Rowset xmlns:z="#RowsetSchema"
+ //
+
+ $signature = [
+ '',
+ ];
+
+ // Open file
+ $this->openFile($pFilename);
+ $fileHandle = $this->fileHandle;
+
+ // Read sample data (first 2 KB will do)
+ $data = fread($fileHandle, 2048);
+ fclose($fileHandle);
+ $data = str_replace("'", '"', $data); // fix headers with single quote
+
+ $valid = true;
+ foreach ($signature as $match) {
+ // every part of the signature must be present
+ if (strpos($data, $match) === false) {
+ $valid = false;
+
+ break;
+ }
+ }
+
+ // Retrieve charset encoding
+ if (preg_match('//um', $data, $matches)) {
+ $this->charSet = strtoupper($matches[1]);
+ }
+
+ return $valid;
+ }
+
+ /**
+ * Check if the file is a valid SimpleXML.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return false|\SimpleXMLElement
+ */
+ public function trySimpleXMLLoadString($pFilename)
+ {
+ try {
+ $xml = simplexml_load_string(
+ $this->securityScanner->scan(file_get_contents($pFilename)),
+ 'SimpleXMLElement',
+ Settings::getLibXmlLoaderOptions()
+ );
+ } catch (\Exception $e) {
+ throw new Exception('Cannot load invalid XML file: ' . $pFilename, 0, $e);
+ }
+
+ return $xml;
+ }
+
+ /**
+ * Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ public function listWorksheetNames($pFilename)
+ {
+ File::assertFile($pFilename);
+ if (!$this->canRead($pFilename)) {
+ throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
+ }
+
+ $worksheetNames = [];
+
+ $xml = $this->trySimpleXMLLoadString($pFilename);
+
+ $namespaces = $xml->getNamespaces(true);
+
+ $xml_ss = $xml->children($namespaces['ss']);
+ foreach ($xml_ss->Worksheet as $worksheet) {
+ $worksheet_ss = $worksheet->attributes($namespaces['ss']);
+ $worksheetNames[] = self::convertStringEncoding((string) $worksheet_ss['Name'], $this->charSet);
+ }
+
+ return $worksheetNames;
+ }
+
+ /**
+ * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return array
+ */
+ public function listWorksheetInfo($pFilename)
+ {
+ File::assertFile($pFilename);
+
+ $worksheetInfo = [];
+
+ $xml = $this->trySimpleXMLLoadString($pFilename);
+
+ $namespaces = $xml->getNamespaces(true);
+
+ $worksheetID = 1;
+ $xml_ss = $xml->children($namespaces['ss']);
+ foreach ($xml_ss->Worksheet as $worksheet) {
+ $worksheet_ss = $worksheet->attributes($namespaces['ss']);
+
+ $tmpInfo = [];
+ $tmpInfo['worksheetName'] = '';
+ $tmpInfo['lastColumnLetter'] = 'A';
+ $tmpInfo['lastColumnIndex'] = 0;
+ $tmpInfo['totalRows'] = 0;
+ $tmpInfo['totalColumns'] = 0;
+
+ if (isset($worksheet_ss['Name'])) {
+ $tmpInfo['worksheetName'] = (string) $worksheet_ss['Name'];
+ } else {
+ $tmpInfo['worksheetName'] = "Worksheet_{$worksheetID}";
+ }
+
+ if (isset($worksheet->Table->Row)) {
+ $rowIndex = 0;
+
+ foreach ($worksheet->Table->Row as $rowData) {
+ $columnIndex = 0;
+ $rowHasData = false;
+
+ foreach ($rowData->Cell as $cell) {
+ if (isset($cell->Data)) {
+ $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
+ $rowHasData = true;
+ }
+
+ ++$columnIndex;
+ }
+
+ ++$rowIndex;
+
+ if ($rowHasData) {
+ $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
+ }
+ }
+ }
+
+ $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
+ $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
+
+ $worksheetInfo[] = $tmpInfo;
+ ++$worksheetID;
+ }
+
+ return $worksheetInfo;
+ }
+
+ /**
+ * Loads Spreadsheet from file.
+ *
+ * @param string $pFilename
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function load($pFilename)
+ {
+ // Create new Spreadsheet
+ $spreadsheet = new Spreadsheet();
+ $spreadsheet->removeSheetByIndex(0);
+
+ // Load into this instance
+ return $this->loadIntoExisting($pFilename, $spreadsheet);
+ }
+
+ private static function identifyFixedStyleValue($styleList, &$styleAttributeValue)
+ {
+ $styleAttributeValue = strtolower($styleAttributeValue);
+ foreach ($styleList as $style) {
+ if ($styleAttributeValue == strtolower($style)) {
+ $styleAttributeValue = $style;
+
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * pixel units to excel width units(units of 1/256th of a character width).
+ *
+ * @param float $pxs
+ *
+ * @return float
+ */
+ protected static function pixel2WidthUnits($pxs)
+ {
+ $UNIT_OFFSET_MAP = [0, 36, 73, 109, 146, 182, 219];
+
+ $widthUnits = 256 * ($pxs / 7);
+ $widthUnits += $UNIT_OFFSET_MAP[($pxs % 7)];
+
+ return $widthUnits;
+ }
+
+ /**
+ * excel width units(units of 1/256th of a character width) to pixel units.
+ *
+ * @param float $widthUnits
+ *
+ * @return float
+ */
+ protected static function widthUnits2Pixel($widthUnits)
+ {
+ $pixels = ($widthUnits / 256) * 7;
+ $offsetWidthUnits = $widthUnits % 256;
+ $pixels += round($offsetWidthUnits / (256 / 7));
+
+ return $pixels;
+ }
+
+ protected static function hex2str($hex)
+ {
+ return chr(hexdec($hex[1]));
+ }
+
+ /**
+ * Loads from file into Spreadsheet instance.
+ *
+ * @param string $pFilename
+ * @param Spreadsheet $spreadsheet
+ *
+ * @throws Exception
+ *
+ * @return Spreadsheet
+ */
+ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
+ {
+ File::assertFile($pFilename);
+ if (!$this->canRead($pFilename)) {
+ throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
+ }
+
+ $xml = $this->trySimpleXMLLoadString($pFilename);
+
+ $namespaces = $xml->getNamespaces(true);
+
+ $docProps = $spreadsheet->getProperties();
+ if (isset($xml->DocumentProperties[0])) {
+ foreach ($xml->DocumentProperties[0] as $propertyName => $propertyValue) {
+ switch ($propertyName) {
+ case 'Title':
+ $docProps->setTitle(self::convertStringEncoding($propertyValue, $this->charSet));
+
+ break;
+ case 'Subject':
+ $docProps->setSubject(self::convertStringEncoding($propertyValue, $this->charSet));
+
+ break;
+ case 'Author':
+ $docProps->setCreator(self::convertStringEncoding($propertyValue, $this->charSet));
+
+ break;
+ case 'Created':
+ $creationDate = strtotime($propertyValue);
+ $docProps->setCreated($creationDate);
+
+ break;
+ case 'LastAuthor':
+ $docProps->setLastModifiedBy(self::convertStringEncoding($propertyValue, $this->charSet));
+
+ break;
+ case 'LastSaved':
+ $lastSaveDate = strtotime($propertyValue);
+ $docProps->setModified($lastSaveDate);
+
+ break;
+ case 'Company':
+ $docProps->setCompany(self::convertStringEncoding($propertyValue, $this->charSet));
+
+ break;
+ case 'Category':
+ $docProps->setCategory(self::convertStringEncoding($propertyValue, $this->charSet));
+
+ break;
+ case 'Manager':
+ $docProps->setManager(self::convertStringEncoding($propertyValue, $this->charSet));
+
+ break;
+ case 'Keywords':
+ $docProps->setKeywords(self::convertStringEncoding($propertyValue, $this->charSet));
+
+ break;
+ case 'Description':
+ $docProps->setDescription(self::convertStringEncoding($propertyValue, $this->charSet));
+
+ break;
+ }
+ }
+ }
+ if (isset($xml->CustomDocumentProperties)) {
+ foreach ($xml->CustomDocumentProperties[0] as $propertyName => $propertyValue) {
+ $propertyAttributes = $propertyValue->attributes($namespaces['dt']);
+ $propertyName = preg_replace_callback('/_x([0-9a-z]{4})_/', ['self', 'hex2str'], $propertyName);
+ $propertyType = Properties::PROPERTY_TYPE_UNKNOWN;
+ switch ((string) $propertyAttributes) {
+ case 'string':
+ $propertyType = Properties::PROPERTY_TYPE_STRING;
+ $propertyValue = trim($propertyValue);
+
+ break;
+ case 'boolean':
+ $propertyType = Properties::PROPERTY_TYPE_BOOLEAN;
+ $propertyValue = (bool) $propertyValue;
+
+ break;
+ case 'integer':
+ $propertyType = Properties::PROPERTY_TYPE_INTEGER;
+ $propertyValue = (int) $propertyValue;
+
+ break;
+ case 'float':
+ $propertyType = Properties::PROPERTY_TYPE_FLOAT;
+ $propertyValue = (float) $propertyValue;
+
+ break;
+ case 'dateTime.tz':
+ $propertyType = Properties::PROPERTY_TYPE_DATE;
+ $propertyValue = strtotime(trim($propertyValue));
+
+ break;
+ }
+ $docProps->setCustomProperty($propertyName, $propertyValue, $propertyType);
+ }
+ }
+
+ $this->parseStyles($xml, $namespaces);
+
+ $worksheetID = 0;
+ $xml_ss = $xml->children($namespaces['ss']);
+
+ foreach ($xml_ss->Worksheet as $worksheet) {
+ $worksheet_ss = $worksheet->attributes($namespaces['ss']);
+
+ if ((isset($this->loadSheetsOnly)) && (isset($worksheet_ss['Name'])) &&
+ (!in_array($worksheet_ss['Name'], $this->loadSheetsOnly))) {
+ continue;
+ }
+
+ // Create new Worksheet
+ $spreadsheet->createSheet();
+ $spreadsheet->setActiveSheetIndex($worksheetID);
+ if (isset($worksheet_ss['Name'])) {
+ $worksheetName = self::convertStringEncoding((string) $worksheet_ss['Name'], $this->charSet);
+ // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
+ // formula cells... during the load, all formulae should be correct, and we're simply bringing
+ // the worksheet name in line with the formula, not the reverse
+ $spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false);
+ }
+
+ $columnID = 'A';
+ if (isset($worksheet->Table->Column)) {
+ foreach ($worksheet->Table->Column as $columnData) {
+ $columnData_ss = $columnData->attributes($namespaces['ss']);
+ if (isset($columnData_ss['Index'])) {
+ $columnID = Coordinate::stringFromColumnIndex((int) $columnData_ss['Index']);
+ }
+ if (isset($columnData_ss['Width'])) {
+ $columnWidth = $columnData_ss['Width'];
+ $spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setWidth($columnWidth / 5.4);
+ }
+ ++$columnID;
+ }
+ }
+
+ $rowID = 1;
+ if (isset($worksheet->Table->Row)) {
+ $additionalMergedCells = 0;
+ foreach ($worksheet->Table->Row as $rowData) {
+ $rowHasData = false;
+ $row_ss = $rowData->attributes($namespaces['ss']);
+ if (isset($row_ss['Index'])) {
+ $rowID = (int) $row_ss['Index'];
+ }
+
+ $columnID = 'A';
+ foreach ($rowData->Cell as $cell) {
+ $cell_ss = $cell->attributes($namespaces['ss']);
+ if (isset($cell_ss['Index'])) {
+ $columnID = Coordinate::stringFromColumnIndex((int) $cell_ss['Index']);
+ }
+ $cellRange = $columnID . $rowID;
+
+ if ($this->getReadFilter() !== null) {
+ if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
+ ++$columnID;
+
+ continue;
+ }
+ }
+
+ if (isset($cell_ss['HRef'])) {
+ $spreadsheet->getActiveSheet()->getCell($cellRange)->getHyperlink()->setUrl($cell_ss['HRef']);
+ }
+
+ if ((isset($cell_ss['MergeAcross'])) || (isset($cell_ss['MergeDown']))) {
+ $columnTo = $columnID;
+ if (isset($cell_ss['MergeAcross'])) {
+ $additionalMergedCells += (int) $cell_ss['MergeAcross'];
+ $columnTo = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($columnID) + $cell_ss['MergeAcross']);
+ }
+ $rowTo = $rowID;
+ if (isset($cell_ss['MergeDown'])) {
+ $rowTo = $rowTo + $cell_ss['MergeDown'];
+ }
+ $cellRange .= ':' . $columnTo . $rowTo;
+ $spreadsheet->getActiveSheet()->mergeCells($cellRange);
+ }
+
+ $cellIsSet = $hasCalculatedValue = false;
+ $cellDataFormula = '';
+ if (isset($cell_ss['Formula'])) {
+ $cellDataFormula = $cell_ss['Formula'];
+ $hasCalculatedValue = true;
+ }
+ if (isset($cell->Data)) {
+ $cellValue = $cellData = $cell->Data;
+ $type = DataType::TYPE_NULL;
+ $cellData_ss = $cellData->attributes($namespaces['ss']);
+ if (isset($cellData_ss['Type'])) {
+ $cellDataType = $cellData_ss['Type'];
+ switch ($cellDataType) {
+ /*
+ const TYPE_STRING = 's';
+ const TYPE_FORMULA = 'f';
+ const TYPE_NUMERIC = 'n';
+ const TYPE_BOOL = 'b';
+ const TYPE_NULL = 'null';
+ const TYPE_INLINE = 'inlineStr';
+ const TYPE_ERROR = 'e';
+ */
+ case 'String':
+ $cellValue = self::convertStringEncoding($cellValue, $this->charSet);
+ $type = DataType::TYPE_STRING;
+
+ break;
+ case 'Number':
+ $type = DataType::TYPE_NUMERIC;
+ $cellValue = (float) $cellValue;
+ if (floor($cellValue) == $cellValue) {
+ $cellValue = (int) $cellValue;
+ }
+
+ break;
+ case 'Boolean':
+ $type = DataType::TYPE_BOOL;
+ $cellValue = ($cellValue != 0);
+
+ break;
+ case 'DateTime':
+ $type = DataType::TYPE_NUMERIC;
+ $cellValue = Date::PHPToExcel(strtotime($cellValue));
+
+ break;
+ case 'Error':
+ $type = DataType::TYPE_ERROR;
+
+ break;
+ }
+ }
+
+ if ($hasCalculatedValue) {
+ $type = DataType::TYPE_FORMULA;
+ $columnNumber = Coordinate::columnIndexFromString($columnID);
+ if (substr($cellDataFormula, 0, 3) == 'of:') {
+ $cellDataFormula = substr($cellDataFormula, 3);
+ $temp = explode('"', $cellDataFormula);
+ $key = false;
+ foreach ($temp as &$value) {
+ // Only replace in alternate array entries (i.e. non-quoted blocks)
+ if ($key = !$key) {
+ $value = str_replace(['[.', '.', ']'], '', $value);
+ }
+ }
+ } else {
+ // Convert R1C1 style references to A1 style references (but only when not quoted)
+ $temp = explode('"', $cellDataFormula);
+ $key = false;
+ foreach ($temp as &$value) {
+ // Only replace in alternate array entries (i.e. non-quoted blocks)
+ if ($key = !$key) {
+ preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
+ // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
+ // through the formula from left to right. Reversing means that we work right to left.through
+ // the formula
+ $cellReferences = array_reverse($cellReferences);
+ // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
+ // then modify the formula to use that new reference
+ foreach ($cellReferences as $cellReference) {
+ $rowReference = $cellReference[2][0];
+ // Empty R reference is the current row
+ if ($rowReference == '') {
+ $rowReference = $rowID;
+ }
+ // Bracketed R references are relative to the current row
+ if ($rowReference[0] == '[') {
+ $rowReference = $rowID + trim($rowReference, '[]');
+ }
+ $columnReference = $cellReference[4][0];
+ // Empty C reference is the current column
+ if ($columnReference == '') {
+ $columnReference = $columnNumber;
+ }
+ // Bracketed C references are relative to the current column
+ if ($columnReference[0] == '[') {
+ $columnReference = $columnNumber + trim($columnReference, '[]');
+ }
+ $A1CellReference = Coordinate::stringFromColumnIndex($columnReference) . $rowReference;
+ $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0]));
+ }
+ }
+ }
+ }
+ unset($value);
+ // Then rebuild the formula string
+ $cellDataFormula = implode('"', $temp);
+ }
+
+ $spreadsheet->getActiveSheet()->getCell($columnID . $rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $cellValue), $type);
+ if ($hasCalculatedValue) {
+ $spreadsheet->getActiveSheet()->getCell($columnID . $rowID)->setCalculatedValue($cellValue);
+ }
+ $cellIsSet = $rowHasData = true;
+ }
+
+ if (isset($cell->Comment)) {
+ $commentAttributes = $cell->Comment->attributes($namespaces['ss']);
+ $author = 'unknown';
+ if (isset($commentAttributes->Author)) {
+ $author = (string) $commentAttributes->Author;
+ }
+ $node = $cell->Comment->Data->asXML();
+ $annotation = strip_tags($node);
+ $spreadsheet->getActiveSheet()->getComment($columnID . $rowID)->setAuthor(self::convertStringEncoding($author, $this->charSet))->setText($this->parseRichText($annotation));
+ }
+
+ if (($cellIsSet) && (isset($cell_ss['StyleID']))) {
+ $style = (string) $cell_ss['StyleID'];
+ if ((isset($this->styles[$style])) && (!empty($this->styles[$style]))) {
+ if (!$spreadsheet->getActiveSheet()->cellExists($columnID . $rowID)) {
+ $spreadsheet->getActiveSheet()->getCell($columnID . $rowID)->setValue(null);
+ }
+ $spreadsheet->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->styles[$style]);
+ }
+ }
+ ++$columnID;
+ while ($additionalMergedCells > 0) {
+ ++$columnID;
+ --$additionalMergedCells;
+ }
+ }
+
+ if ($rowHasData) {
+ if (isset($row_ss['Height'])) {
+ $rowHeight = $row_ss['Height'];
+ $spreadsheet->getActiveSheet()->getRowDimension($rowID)->setRowHeight($rowHeight);
+ }
+ }
+
+ ++$rowID;
+ }
+ }
+ ++$worksheetID;
+ }
+
+ // Return
+ return $spreadsheet;
+ }
+
+ protected static function convertStringEncoding($string, $charset)
+ {
+ if ($charset != 'UTF-8') {
+ return StringHelper::convertEncoding($string, 'UTF-8', $charset);
+ }
+
+ return $string;
+ }
+
+ protected function parseRichText($is)
+ {
+ $value = new RichText();
+
+ $value->createText(self::convertStringEncoding($is, $this->charSet));
+
+ return $value;
+ }
+
+ /**
+ * @param SimpleXMLElement $xml
+ * @param array $namespaces
+ */
+ private function parseStyles(SimpleXMLElement $xml, array $namespaces)
+ {
+ if (!isset($xml->Styles)) {
+ return;
+ }
+
+ foreach ($xml->Styles[0] as $style) {
+ $style_ss = $style->attributes($namespaces['ss']);
+ $styleID = (string) $style_ss['ID'];
+ $this->styles[$styleID] = (isset($this->styles['Default'])) ? $this->styles['Default'] : [];
+ foreach ($style as $styleType => $styleData) {
+ $styleAttributes = $styleData->attributes($namespaces['ss']);
+ switch ($styleType) {
+ case 'Alignment':
+ $this->parseStyleAlignment($styleID, $styleAttributes);
+
+ break;
+ case 'Borders':
+ $this->parseStyleBorders($styleID, $styleData, $namespaces);
+
+ break;
+ case 'Font':
+ $this->parseStyleFont($styleID, $styleAttributes);
+
+ break;
+ case 'Interior':
+ $this->parseStyleInterior($styleID, $styleAttributes);
+
+ break;
+ case 'NumberFormat':
+ $this->parseStyleNumberFormat($styleID, $styleAttributes);
+
+ break;
+ }
+ }
+ }
+ }
+
+ /**
+ * @param string $styleID
+ * @param SimpleXMLElement $styleAttributes
+ */
+ private function parseStyleAlignment($styleID, SimpleXMLElement $styleAttributes)
+ {
+ $verticalAlignmentStyles = [
+ Alignment::VERTICAL_BOTTOM,
+ Alignment::VERTICAL_TOP,
+ Alignment::VERTICAL_CENTER,
+ Alignment::VERTICAL_JUSTIFY,
+ ];
+ $horizontalAlignmentStyles = [
+ Alignment::HORIZONTAL_GENERAL,
+ Alignment::HORIZONTAL_LEFT,
+ Alignment::HORIZONTAL_RIGHT,
+ Alignment::HORIZONTAL_CENTER,
+ Alignment::HORIZONTAL_CENTER_CONTINUOUS,
+ Alignment::HORIZONTAL_JUSTIFY,
+ ];
+
+ foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
+ $styleAttributeValue = (string) $styleAttributeValue;
+ switch ($styleAttributeKey) {
+ case 'Vertical':
+ if (self::identifyFixedStyleValue($verticalAlignmentStyles, $styleAttributeValue)) {
+ $this->styles[$styleID]['alignment']['vertical'] = $styleAttributeValue;
+ }
+
+ break;
+ case 'Horizontal':
+ if (self::identifyFixedStyleValue($horizontalAlignmentStyles, $styleAttributeValue)) {
+ $this->styles[$styleID]['alignment']['horizontal'] = $styleAttributeValue;
+ }
+
+ break;
+ case 'WrapText':
+ $this->styles[$styleID]['alignment']['wrapText'] = true;
+
+ break;
+ }
+ }
+ }
+
+ /**
+ * @param $styleID
+ * @param SimpleXMLElement $styleData
+ * @param array $namespaces
+ */
+ private function parseStyleBorders($styleID, SimpleXMLElement $styleData, array $namespaces)
+ {
+ foreach ($styleData->Border as $borderStyle) {
+ $borderAttributes = $borderStyle->attributes($namespaces['ss']);
+ $thisBorder = [];
+ foreach ($borderAttributes as $borderStyleKey => $borderStyleValue) {
+ switch ($borderStyleKey) {
+ case 'LineStyle':
+ $thisBorder['borderStyle'] = Border::BORDER_MEDIUM;
+
+ break;
+ case 'Weight':
+ break;
+ case 'Position':
+ $borderPosition = strtolower($borderStyleValue);
+
+ break;
+ case 'Color':
+ $borderColour = substr($borderStyleValue, 1);
+ $thisBorder['color']['rgb'] = $borderColour;
+
+ break;
+ }
+ }
+ if (!empty($thisBorder)) {
+ if (($borderPosition == 'left') || ($borderPosition == 'right') || ($borderPosition == 'top') || ($borderPosition == 'bottom')) {
+ $this->styles[$styleID]['borders'][$borderPosition] = $thisBorder;
+ }
+ }
+ }
+ }
+
+ /**
+ * @param $styleID
+ * @param SimpleXMLElement $styleAttributes
+ */
+ private function parseStyleFont($styleID, SimpleXMLElement $styleAttributes)
+ {
+ $underlineStyles = [
+ Font::UNDERLINE_NONE,
+ Font::UNDERLINE_DOUBLE,
+ Font::UNDERLINE_DOUBLEACCOUNTING,
+ Font::UNDERLINE_SINGLE,
+ Font::UNDERLINE_SINGLEACCOUNTING,
+ ];
+
+ foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
+ $styleAttributeValue = (string) $styleAttributeValue;
+ switch ($styleAttributeKey) {
+ case 'FontName':
+ $this->styles[$styleID]['font']['name'] = $styleAttributeValue;
+
+ break;
+ case 'Size':
+ $this->styles[$styleID]['font']['size'] = $styleAttributeValue;
+
+ break;
+ case 'Color':
+ $this->styles[$styleID]['font']['color']['rgb'] = substr($styleAttributeValue, 1);
+
+ break;
+ case 'Bold':
+ $this->styles[$styleID]['font']['bold'] = true;
+
+ break;
+ case 'Italic':
+ $this->styles[$styleID]['font']['italic'] = true;
+
+ break;
+ case 'Underline':
+ if (self::identifyFixedStyleValue($underlineStyles, $styleAttributeValue)) {
+ $this->styles[$styleID]['font']['underline'] = $styleAttributeValue;
+ }
+
+ break;
+ }
+ }
+ }
+
+ /**
+ * @param $styleID
+ * @param SimpleXMLElement $styleAttributes
+ */
+ private function parseStyleInterior($styleID, SimpleXMLElement $styleAttributes)
+ {
+ foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
+ switch ($styleAttributeKey) {
+ case 'Color':
+ $this->styles[$styleID]['fill']['color']['rgb'] = substr($styleAttributeValue, 1);
+
+ break;
+ case 'Pattern':
+ $this->styles[$styleID]['fill']['fillType'] = strtolower($styleAttributeValue);
+
+ break;
+ }
+ }
+ }
+
+ /**
+ * @param $styleID
+ * @param SimpleXMLElement $styleAttributes
+ */
+ private function parseStyleNumberFormat($styleID, SimpleXMLElement $styleAttributes)
+ {
+ $fromFormats = ['\-', '\ '];
+ $toFormats = ['-', ' '];
+
+ foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
+ $styleAttributeValue = str_replace($fromFormats, $toFormats, $styleAttributeValue);
+ switch ($styleAttributeValue) {
+ case 'Short Date':
+ $styleAttributeValue = 'dd/mm/yyyy';
+
+ break;
+ }
+
+ if ($styleAttributeValue > '') {
+ $this->styles[$styleID]['numberFormat']['formatCode'] = $styleAttributeValue;
+ }
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/ReferenceHelper.php b/htdocs/includes/phpoffice/PhpSpreadsheet/ReferenceHelper.php
new file mode 100644
index 00000000000..54bc182a8eb
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/ReferenceHelper.php
@@ -0,0 +1,916 @@
+= ($beforeRow + $pNumRows)) &&
+ ($cellRow < $beforeRow)) {
+ return true;
+ } elseif ($pNumCols < 0 &&
+ ($cellColumnIndex >= ($beforeColumnIndex + $pNumCols)) &&
+ ($cellColumnIndex < $beforeColumnIndex)) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Update page breaks when inserting/deleting rows/columns.
+ *
+ * @param Worksheet $pSheet The worksheet that we're editing
+ * @param string $pBefore Insert/Delete before this cell address (e.g. 'A1')
+ * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
+ * @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion)
+ * @param int $beforeRow Number of the row we're inserting/deleting before
+ * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
+ */
+ protected function adjustPageBreaks(Worksheet $pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
+ {
+ $aBreaks = $pSheet->getBreaks();
+ ($pNumCols > 0 || $pNumRows > 0) ?
+ uksort($aBreaks, ['self', 'cellReverseSort']) : uksort($aBreaks, ['self', 'cellSort']);
+
+ foreach ($aBreaks as $key => $value) {
+ if (self::cellAddressInDeleteRange($key, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols)) {
+ // If we're deleting, then clear any defined breaks that are within the range
+ // of rows/columns that we're deleting
+ $pSheet->setBreak($key, Worksheet::BREAK_NONE);
+ } else {
+ // Otherwise update any affected breaks by inserting a new break at the appropriate point
+ // and removing the old affected break
+ $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
+ if ($key != $newReference) {
+ $pSheet->setBreak($newReference, $value)
+ ->setBreak($key, Worksheet::BREAK_NONE);
+ }
+ }
+ }
+ }
+
+ /**
+ * Update cell comments when inserting/deleting rows/columns.
+ *
+ * @param Worksheet $pSheet The worksheet that we're editing
+ * @param string $pBefore Insert/Delete before this cell address (e.g. 'A1')
+ * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
+ * @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion)
+ * @param int $beforeRow Number of the row we're inserting/deleting before
+ * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
+ */
+ protected function adjustComments($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
+ {
+ $aComments = $pSheet->getComments();
+ $aNewComments = []; // the new array of all comments
+
+ foreach ($aComments as $key => &$value) {
+ // Any comments inside a deleted range will be ignored
+ if (!self::cellAddressInDeleteRange($key, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols)) {
+ // Otherwise build a new array of comments indexed by the adjusted cell reference
+ $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
+ $aNewComments[$newReference] = $value;
+ }
+ }
+ // Replace the comments array with the new set of comments
+ $pSheet->setComments($aNewComments);
+ }
+
+ /**
+ * Update hyperlinks when inserting/deleting rows/columns.
+ *
+ * @param Worksheet $pSheet The worksheet that we're editing
+ * @param string $pBefore Insert/Delete before this cell address (e.g. 'A1')
+ * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
+ * @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion)
+ * @param int $beforeRow Number of the row we're inserting/deleting before
+ * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
+ */
+ protected function adjustHyperlinks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
+ {
+ $aHyperlinkCollection = $pSheet->getHyperlinkCollection();
+ ($pNumCols > 0 || $pNumRows > 0) ?
+ uksort($aHyperlinkCollection, ['self', 'cellReverseSort']) : uksort($aHyperlinkCollection, ['self', 'cellSort']);
+
+ foreach ($aHyperlinkCollection as $key => $value) {
+ $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
+ if ($key != $newReference) {
+ $pSheet->setHyperlink($newReference, $value);
+ $pSheet->setHyperlink($key, null);
+ }
+ }
+ }
+
+ /**
+ * Update data validations when inserting/deleting rows/columns.
+ *
+ * @param Worksheet $pSheet The worksheet that we're editing
+ * @param string $pBefore Insert/Delete before this cell address (e.g. 'A1')
+ * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
+ * @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion)
+ * @param int $beforeRow Number of the row we're inserting/deleting before
+ * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
+ */
+ protected function adjustDataValidations($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
+ {
+ $aDataValidationCollection = $pSheet->getDataValidationCollection();
+ ($pNumCols > 0 || $pNumRows > 0) ?
+ uksort($aDataValidationCollection, ['self', 'cellReverseSort']) : uksort($aDataValidationCollection, ['self', 'cellSort']);
+
+ foreach ($aDataValidationCollection as $key => $value) {
+ $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
+ if ($key != $newReference) {
+ $pSheet->setDataValidation($newReference, $value);
+ $pSheet->setDataValidation($key, null);
+ }
+ }
+ }
+
+ /**
+ * Update merged cells when inserting/deleting rows/columns.
+ *
+ * @param Worksheet $pSheet The worksheet that we're editing
+ * @param string $pBefore Insert/Delete before this cell address (e.g. 'A1')
+ * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
+ * @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion)
+ * @param int $beforeRow Number of the row we're inserting/deleting before
+ * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
+ */
+ protected function adjustMergeCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
+ {
+ $aMergeCells = $pSheet->getMergeCells();
+ $aNewMergeCells = []; // the new array of all merge cells
+ foreach ($aMergeCells as $key => &$value) {
+ $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
+ $aNewMergeCells[$newReference] = $newReference;
+ }
+ $pSheet->setMergeCells($aNewMergeCells); // replace the merge cells array
+ }
+
+ /**
+ * Update protected cells when inserting/deleting rows/columns.
+ *
+ * @param Worksheet $pSheet The worksheet that we're editing
+ * @param string $pBefore Insert/Delete before this cell address (e.g. 'A1')
+ * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
+ * @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion)
+ * @param int $beforeRow Number of the row we're inserting/deleting before
+ * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
+ */
+ protected function adjustProtectedCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
+ {
+ $aProtectedCells = $pSheet->getProtectedCells();
+ ($pNumCols > 0 || $pNumRows > 0) ?
+ uksort($aProtectedCells, ['self', 'cellReverseSort']) : uksort($aProtectedCells, ['self', 'cellSort']);
+ foreach ($aProtectedCells as $key => $value) {
+ $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
+ if ($key != $newReference) {
+ $pSheet->protectCells($newReference, $value, true);
+ $pSheet->unprotectCells($key);
+ }
+ }
+ }
+
+ /**
+ * Update column dimensions when inserting/deleting rows/columns.
+ *
+ * @param Worksheet $pSheet The worksheet that we're editing
+ * @param string $pBefore Insert/Delete before this cell address (e.g. 'A1')
+ * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
+ * @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion)
+ * @param int $beforeRow Number of the row we're inserting/deleting before
+ * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
+ */
+ protected function adjustColumnDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
+ {
+ $aColumnDimensions = array_reverse($pSheet->getColumnDimensions(), true);
+ if (!empty($aColumnDimensions)) {
+ foreach ($aColumnDimensions as $objColumnDimension) {
+ $newReference = $this->updateCellReference($objColumnDimension->getColumnIndex() . '1', $pBefore, $pNumCols, $pNumRows);
+ list($newReference) = Coordinate::coordinateFromString($newReference);
+ if ($objColumnDimension->getColumnIndex() != $newReference) {
+ $objColumnDimension->setColumnIndex($newReference);
+ }
+ }
+ $pSheet->refreshColumnDimensions();
+ }
+ }
+
+ /**
+ * Update row dimensions when inserting/deleting rows/columns.
+ *
+ * @param Worksheet $pSheet The worksheet that we're editing
+ * @param string $pBefore Insert/Delete before this cell address (e.g. 'A1')
+ * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
+ * @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion)
+ * @param int $beforeRow Number of the row we're inserting/deleting before
+ * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
+ */
+ protected function adjustRowDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
+ {
+ $aRowDimensions = array_reverse($pSheet->getRowDimensions(), true);
+ if (!empty($aRowDimensions)) {
+ foreach ($aRowDimensions as $objRowDimension) {
+ $newReference = $this->updateCellReference('A' . $objRowDimension->getRowIndex(), $pBefore, $pNumCols, $pNumRows);
+ list(, $newReference) = Coordinate::coordinateFromString($newReference);
+ if ($objRowDimension->getRowIndex() != $newReference) {
+ $objRowDimension->setRowIndex($newReference);
+ }
+ }
+ $pSheet->refreshRowDimensions();
+
+ $copyDimension = $pSheet->getRowDimension($beforeRow - 1);
+ for ($i = $beforeRow; $i <= $beforeRow - 1 + $pNumRows; ++$i) {
+ $newDimension = $pSheet->getRowDimension($i);
+ $newDimension->setRowHeight($copyDimension->getRowHeight());
+ $newDimension->setVisible($copyDimension->getVisible());
+ $newDimension->setOutlineLevel($copyDimension->getOutlineLevel());
+ $newDimension->setCollapsed($copyDimension->getCollapsed());
+ }
+ }
+ }
+
+ /**
+ * Insert a new column or row, updating all possible related data.
+ *
+ * @param string $pBefore Insert before this cell address (e.g. 'A1')
+ * @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion)
+ * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
+ * @param Worksheet $pSheet The worksheet that we're editing
+ *
+ * @throws Exception
+ */
+ public function insertNewBefore($pBefore, $pNumCols, $pNumRows, Worksheet $pSheet)
+ {
+ $remove = ($pNumCols < 0 || $pNumRows < 0);
+ $allCoordinates = $pSheet->getCoordinates();
+
+ // Get coordinate of $pBefore
+ list($beforeColumn, $beforeRow) = Coordinate::coordinateFromString($pBefore);
+ $beforeColumnIndex = Coordinate::columnIndexFromString($beforeColumn);
+
+ // Clear cells if we are removing columns or rows
+ $highestColumn = $pSheet->getHighestColumn();
+ $highestRow = $pSheet->getHighestRow();
+
+ // 1. Clear column strips if we are removing columns
+ if ($pNumCols < 0 && $beforeColumnIndex - 2 + $pNumCols > 0) {
+ for ($i = 1; $i <= $highestRow - 1; ++$i) {
+ for ($j = $beforeColumnIndex - 1 + $pNumCols; $j <= $beforeColumnIndex - 2; ++$j) {
+ $coordinate = Coordinate::stringFromColumnIndex($j + 1) . $i;
+ $pSheet->removeConditionalStyles($coordinate);
+ if ($pSheet->cellExists($coordinate)) {
+ $pSheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL);
+ $pSheet->getCell($coordinate)->setXfIndex(0);
+ }
+ }
+ }
+ }
+
+ // 2. Clear row strips if we are removing rows
+ if ($pNumRows < 0 && $beforeRow - 1 + $pNumRows > 0) {
+ for ($i = $beforeColumnIndex - 1; $i <= Coordinate::columnIndexFromString($highestColumn) - 1; ++$i) {
+ for ($j = $beforeRow + $pNumRows; $j <= $beforeRow - 1; ++$j) {
+ $coordinate = Coordinate::stringFromColumnIndex($i + 1) . $j;
+ $pSheet->removeConditionalStyles($coordinate);
+ if ($pSheet->cellExists($coordinate)) {
+ $pSheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL);
+ $pSheet->getCell($coordinate)->setXfIndex(0);
+ }
+ }
+ }
+ }
+
+ // Loop through cells, bottom-up, and change cell coordinate
+ if ($remove) {
+ // It's faster to reverse and pop than to use unshift, especially with large cell collections
+ $allCoordinates = array_reverse($allCoordinates);
+ }
+ while ($coordinate = array_pop($allCoordinates)) {
+ $cell = $pSheet->getCell($coordinate);
+ $cellIndex = Coordinate::columnIndexFromString($cell->getColumn());
+
+ if ($cellIndex - 1 + $pNumCols < 0) {
+ continue;
+ }
+
+ // New coordinate
+ $newCoordinate = Coordinate::stringFromColumnIndex($cellIndex + $pNumCols) . ($cell->getRow() + $pNumRows);
+
+ // Should the cell be updated? Move value and cellXf index from one cell to another.
+ if (($cellIndex >= $beforeColumnIndex) && ($cell->getRow() >= $beforeRow)) {
+ // Update cell styles
+ $pSheet->getCell($newCoordinate)->setXfIndex($cell->getXfIndex());
+
+ // Insert this cell at its new location
+ if ($cell->getDataType() == DataType::TYPE_FORMULA) {
+ // Formula should be adjusted
+ $pSheet->getCell($newCoordinate)
+ ->setValue($this->updateFormulaReferences($cell->getValue(), $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle()));
+ } else {
+ // Formula should not be adjusted
+ $pSheet->getCell($newCoordinate)->setValue($cell->getValue());
+ }
+
+ // Clear the original cell
+ $pSheet->getCellCollection()->delete($coordinate);
+ } else {
+ /* We don't need to update styles for rows/columns before our insertion position,
+ but we do still need to adjust any formulae in those cells */
+ if ($cell->getDataType() == DataType::TYPE_FORMULA) {
+ // Formula should be adjusted
+ $cell->setValue($this->updateFormulaReferences($cell->getValue(), $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle()));
+ }
+ }
+ }
+
+ // Duplicate styles for the newly inserted cells
+ $highestColumn = $pSheet->getHighestColumn();
+ $highestRow = $pSheet->getHighestRow();
+
+ if ($pNumCols > 0 && $beforeColumnIndex - 2 > 0) {
+ for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {
+ // Style
+ $coordinate = Coordinate::stringFromColumnIndex($beforeColumnIndex - 1) . $i;
+ if ($pSheet->cellExists($coordinate)) {
+ $xfIndex = $pSheet->getCell($coordinate)->getXfIndex();
+ $conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ?
+ $pSheet->getConditionalStyles($coordinate) : false;
+ for ($j = $beforeColumnIndex; $j <= $beforeColumnIndex - 1 + $pNumCols; ++$j) {
+ $pSheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex);
+ if ($conditionalStyles) {
+ $cloned = [];
+ foreach ($conditionalStyles as $conditionalStyle) {
+ $cloned[] = clone $conditionalStyle;
+ }
+ $pSheet->setConditionalStyles(Coordinate::stringFromColumnIndex($j) . $i, $cloned);
+ }
+ }
+ }
+ }
+ }
+
+ if ($pNumRows > 0 && $beforeRow - 1 > 0) {
+ for ($i = $beforeColumnIndex; $i <= Coordinate::columnIndexFromString($highestColumn); ++$i) {
+ // Style
+ $coordinate = Coordinate::stringFromColumnIndex($i) . ($beforeRow - 1);
+ if ($pSheet->cellExists($coordinate)) {
+ $xfIndex = $pSheet->getCell($coordinate)->getXfIndex();
+ $conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ?
+ $pSheet->getConditionalStyles($coordinate) : false;
+ for ($j = $beforeRow; $j <= $beforeRow - 1 + $pNumRows; ++$j) {
+ $pSheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex);
+ if ($conditionalStyles) {
+ $cloned = [];
+ foreach ($conditionalStyles as $conditionalStyle) {
+ $cloned[] = clone $conditionalStyle;
+ }
+ $pSheet->setConditionalStyles(Coordinate::stringFromColumnIndex($i) . $j, $cloned);
+ }
+ }
+ }
+ }
+ }
+
+ // Update worksheet: column dimensions
+ $this->adjustColumnDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
+
+ // Update worksheet: row dimensions
+ $this->adjustRowDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
+
+ // Update worksheet: page breaks
+ $this->adjustPageBreaks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
+
+ // Update worksheet: comments
+ $this->adjustComments($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
+
+ // Update worksheet: hyperlinks
+ $this->adjustHyperlinks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
+
+ // Update worksheet: data validations
+ $this->adjustDataValidations($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
+
+ // Update worksheet: merge cells
+ $this->adjustMergeCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
+
+ // Update worksheet: protected cells
+ $this->adjustProtectedCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
+
+ // Update worksheet: autofilter
+ $autoFilter = $pSheet->getAutoFilter();
+ $autoFilterRange = $autoFilter->getRange();
+ if (!empty($autoFilterRange)) {
+ if ($pNumCols != 0) {
+ $autoFilterColumns = $autoFilter->getColumns();
+ if (count($autoFilterColumns) > 0) {
+ $column = '';
+ $row = 0;
+ sscanf($pBefore, '%[A-Z]%d', $column, $row);
+ $columnIndex = Coordinate::columnIndexFromString($column);
+ list($rangeStart, $rangeEnd) = Coordinate::rangeBoundaries($autoFilterRange);
+ if ($columnIndex <= $rangeEnd[0]) {
+ if ($pNumCols < 0) {
+ // If we're actually deleting any columns that fall within the autofilter range,
+ // then we delete any rules for those columns
+ $deleteColumn = $columnIndex + $pNumCols - 1;
+ $deleteCount = abs($pNumCols);
+ for ($i = 1; $i <= $deleteCount; ++$i) {
+ if (isset($autoFilterColumns[Coordinate::stringFromColumnIndex($deleteColumn + 1)])) {
+ $autoFilter->clearColumn(Coordinate::stringFromColumnIndex($deleteColumn + 1));
+ }
+ ++$deleteColumn;
+ }
+ }
+ $startCol = ($columnIndex > $rangeStart[0]) ? $columnIndex : $rangeStart[0];
+
+ // Shuffle columns in autofilter range
+ if ($pNumCols > 0) {
+ $startColRef = $startCol;
+ $endColRef = $rangeEnd[0];
+ $toColRef = $rangeEnd[0] + $pNumCols;
+
+ do {
+ $autoFilter->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef));
+ --$endColRef;
+ --$toColRef;
+ } while ($startColRef <= $endColRef);
+ } else {
+ // For delete, we shuffle from beginning to end to avoid overwriting
+ $startColID = Coordinate::stringFromColumnIndex($startCol);
+ $toColID = Coordinate::stringFromColumnIndex($startCol + $pNumCols);
+ $endColID = Coordinate::stringFromColumnIndex($rangeEnd[0] + 1);
+ do {
+ $autoFilter->shiftColumn($startColID, $toColID);
+ ++$startColID;
+ ++$toColID;
+ } while ($startColID != $endColID);
+ }
+ }
+ }
+ }
+ $pSheet->setAutoFilter($this->updateCellReference($autoFilterRange, $pBefore, $pNumCols, $pNumRows));
+ }
+
+ // Update worksheet: freeze pane
+ if ($pSheet->getFreezePane()) {
+ $splitCell = $pSheet->getFreezePane();
+ $topLeftCell = $pSheet->getTopLeftCell();
+
+ $splitCell = $this->updateCellReference($splitCell, $pBefore, $pNumCols, $pNumRows);
+ $topLeftCell = $this->updateCellReference($topLeftCell, $pBefore, $pNumCols, $pNumRows);
+
+ $pSheet->freezePane($splitCell, $topLeftCell);
+ }
+
+ // Page setup
+ if ($pSheet->getPageSetup()->isPrintAreaSet()) {
+ $pSheet->getPageSetup()->setPrintArea($this->updateCellReference($pSheet->getPageSetup()->getPrintArea(), $pBefore, $pNumCols, $pNumRows));
+ }
+
+ // Update worksheet: drawings
+ $aDrawings = $pSheet->getDrawingCollection();
+ foreach ($aDrawings as $objDrawing) {
+ $newReference = $this->updateCellReference($objDrawing->getCoordinates(), $pBefore, $pNumCols, $pNumRows);
+ if ($objDrawing->getCoordinates() != $newReference) {
+ $objDrawing->setCoordinates($newReference);
+ }
+ }
+
+ // Update workbook: named ranges
+ if (count($pSheet->getParent()->getNamedRanges()) > 0) {
+ foreach ($pSheet->getParent()->getNamedRanges() as $namedRange) {
+ if ($namedRange->getWorksheet()->getHashCode() == $pSheet->getHashCode()) {
+ $namedRange->setRange($this->updateCellReference($namedRange->getRange(), $pBefore, $pNumCols, $pNumRows));
+ }
+ }
+ }
+
+ // Garbage collect
+ $pSheet->garbageCollect();
+ }
+
+ /**
+ * Update references within formulas.
+ *
+ * @param string $pFormula Formula to update
+ * @param int $pBefore Insert before this one
+ * @param int $pNumCols Number of columns to insert
+ * @param int $pNumRows Number of rows to insert
+ * @param string $sheetName Worksheet name/title
+ *
+ * @throws Exception
+ *
+ * @return string Updated formula
+ */
+ public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, $sheetName = '')
+ {
+ // Update cell references in the formula
+ $formulaBlocks = explode('"', $pFormula);
+ $i = false;
+ foreach ($formulaBlocks as &$formulaBlock) {
+ // Ignore blocks that were enclosed in quotes (alternating entries in the $formulaBlocks array after the explode)
+ if ($i = !$i) {
+ $adjustCount = 0;
+ $newCellTokens = $cellTokens = [];
+ // Search for row ranges (e.g. 'Sheet1'!3:5 or 3:5) with or without $ absolutes (e.g. $3:5)
+ $matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_ROWRANGE . '/i', ' ' . $formulaBlock . ' ', $matches, PREG_SET_ORDER);
+ if ($matchCount > 0) {
+ foreach ($matches as $match) {
+ $fromString = ($match[2] > '') ? $match[2] . '!' : '';
+ $fromString .= $match[3] . ':' . $match[4];
+ $modified3 = substr($this->updateCellReference('$A' . $match[3], $pBefore, $pNumCols, $pNumRows), 2);
+ $modified4 = substr($this->updateCellReference('$A' . $match[4], $pBefore, $pNumCols, $pNumRows), 2);
+
+ if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) {
+ if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) {
+ $toString = ($match[2] > '') ? $match[2] . '!' : '';
+ $toString .= $modified3 . ':' . $modified4;
+ // Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more
+ $column = 100000;
+ $row = 10000000 + trim($match[3], '$');
+ $cellIndex = $column . $row;
+
+ $newCellTokens[$cellIndex] = preg_quote($toString, '/');
+ $cellTokens[$cellIndex] = '/(? 0) {
+ foreach ($matches as $match) {
+ $fromString = ($match[2] > '') ? $match[2] . '!' : '';
+ $fromString .= $match[3] . ':' . $match[4];
+ $modified3 = substr($this->updateCellReference($match[3] . '$1', $pBefore, $pNumCols, $pNumRows), 0, -2);
+ $modified4 = substr($this->updateCellReference($match[4] . '$1', $pBefore, $pNumCols, $pNumRows), 0, -2);
+
+ if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) {
+ if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) {
+ $toString = ($match[2] > '') ? $match[2] . '!' : '';
+ $toString .= $modified3 . ':' . $modified4;
+ // Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more
+ $column = Coordinate::columnIndexFromString(trim($match[3], '$')) + 100000;
+ $row = 10000000;
+ $cellIndex = $column . $row;
+
+ $newCellTokens[$cellIndex] = preg_quote($toString, '/');
+ $cellTokens[$cellIndex] = '/(? 0) {
+ foreach ($matches as $match) {
+ $fromString = ($match[2] > '') ? $match[2] . '!' : '';
+ $fromString .= $match[3] . ':' . $match[4];
+ $modified3 = $this->updateCellReference($match[3], $pBefore, $pNumCols, $pNumRows);
+ $modified4 = $this->updateCellReference($match[4], $pBefore, $pNumCols, $pNumRows);
+
+ if ($match[3] . $match[4] !== $modified3 . $modified4) {
+ if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) {
+ $toString = ($match[2] > '') ? $match[2] . '!' : '';
+ $toString .= $modified3 . ':' . $modified4;
+ list($column, $row) = Coordinate::coordinateFromString($match[3]);
+ // Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more
+ $column = Coordinate::columnIndexFromString(trim($column, '$')) + 100000;
+ $row = trim($row, '$') + 10000000;
+ $cellIndex = $column . $row;
+
+ $newCellTokens[$cellIndex] = preg_quote($toString, '/');
+ $cellTokens[$cellIndex] = '/(? 0) {
+ foreach ($matches as $match) {
+ $fromString = ($match[2] > '') ? $match[2] . '!' : '';
+ $fromString .= $match[3];
+
+ $modified3 = $this->updateCellReference($match[3], $pBefore, $pNumCols, $pNumRows);
+ if ($match[3] !== $modified3) {
+ if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) {
+ $toString = ($match[2] > '') ? $match[2] . '!' : '';
+ $toString .= $modified3;
+ list($column, $row) = Coordinate::coordinateFromString($match[3]);
+ // Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more
+ $column = Coordinate::columnIndexFromString(trim($column, '$')) + 100000;
+ $row = trim($row, '$') + 10000000;
+ $cellIndex = $row . $column;
+
+ $newCellTokens[$cellIndex] = preg_quote($toString, '/');
+ $cellTokens[$cellIndex] = '/(? 0) {
+ if ($pNumCols > 0 || $pNumRows > 0) {
+ krsort($cellTokens);
+ krsort($newCellTokens);
+ } else {
+ ksort($cellTokens);
+ ksort($newCellTokens);
+ } // Update cell references in the formula
+ $formulaBlock = str_replace('\\', '', preg_replace($cellTokens, $newCellTokens, $formulaBlock));
+ }
+ }
+ }
+ unset($formulaBlock);
+
+ // Then rebuild the formula string
+ return implode('"', $formulaBlocks);
+ }
+
+ /**
+ * Update cell reference.
+ *
+ * @param string $pCellRange Cell range
+ * @param string $pBefore Insert before this one
+ * @param int $pNumCols Number of columns to increment
+ * @param int $pNumRows Number of rows to increment
+ *
+ * @throws Exception
+ *
+ * @return string Updated cell range
+ */
+ public function updateCellReference($pCellRange = 'A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0)
+ {
+ // Is it in another worksheet? Will not have to update anything.
+ if (strpos($pCellRange, '!') !== false) {
+ return $pCellRange;
+ // Is it a range or a single cell?
+ } elseif (!Coordinate::coordinateIsRange($pCellRange)) {
+ // Single cell
+ return $this->updateSingleCellReference($pCellRange, $pBefore, $pNumCols, $pNumRows);
+ } elseif (Coordinate::coordinateIsRange($pCellRange)) {
+ // Range
+ return $this->updateCellRange($pCellRange, $pBefore, $pNumCols, $pNumRows);
+ }
+
+ // Return original
+ return $pCellRange;
+ }
+
+ /**
+ * Update named formulas (i.e. containing worksheet references / named ranges).
+ *
+ * @param Spreadsheet $spreadsheet Object to update
+ * @param string $oldName Old name (name to replace)
+ * @param string $newName New name
+ */
+ public function updateNamedFormulas(Spreadsheet $spreadsheet, $oldName = '', $newName = '')
+ {
+ if ($oldName == '') {
+ return;
+ }
+
+ foreach ($spreadsheet->getWorksheetIterator() as $sheet) {
+ foreach ($sheet->getCoordinates(false) as $coordinate) {
+ $cell = $sheet->getCell($coordinate);
+ if (($cell !== null) && ($cell->getDataType() == DataType::TYPE_FORMULA)) {
+ $formula = $cell->getValue();
+ if (strpos($formula, $oldName) !== false) {
+ $formula = str_replace("'" . $oldName . "'!", "'" . $newName . "'!", $formula);
+ $formula = str_replace($oldName . '!', $newName . '!', $formula);
+ $cell->setValueExplicit($formula, DataType::TYPE_FORMULA);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Update cell range.
+ *
+ * @param string $pCellRange Cell range (e.g. 'B2:D4', 'B:C' or '2:3')
+ * @param string $pBefore Insert before this one
+ * @param int $pNumCols Number of columns to increment
+ * @param int $pNumRows Number of rows to increment
+ *
+ * @throws Exception
+ *
+ * @return string Updated cell range
+ */
+ private function updateCellRange($pCellRange = 'A1:A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0)
+ {
+ if (!Coordinate::coordinateIsRange($pCellRange)) {
+ throw new Exception('Only cell ranges may be passed to this method.');
+ }
+
+ // Update range
+ $range = Coordinate::splitRange($pCellRange);
+ $ic = count($range);
+ for ($i = 0; $i < $ic; ++$i) {
+ $jc = count($range[$i]);
+ for ($j = 0; $j < $jc; ++$j) {
+ if (ctype_alpha($range[$i][$j])) {
+ $r = Coordinate::coordinateFromString($this->updateSingleCellReference($range[$i][$j] . '1', $pBefore, $pNumCols, $pNumRows));
+ $range[$i][$j] = $r[0];
+ } elseif (ctype_digit($range[$i][$j])) {
+ $r = Coordinate::coordinateFromString($this->updateSingleCellReference('A' . $range[$i][$j], $pBefore, $pNumCols, $pNumRows));
+ $range[$i][$j] = $r[1];
+ } else {
+ $range[$i][$j] = $this->updateSingleCellReference($range[$i][$j], $pBefore, $pNumCols, $pNumRows);
+ }
+ }
+ }
+
+ // Recreate range string
+ return Coordinate::buildRange($range);
+ }
+
+ /**
+ * Update single cell reference.
+ *
+ * @param string $pCellReference Single cell reference
+ * @param string $pBefore Insert before this one
+ * @param int $pNumCols Number of columns to increment
+ * @param int $pNumRows Number of rows to increment
+ *
+ * @throws Exception
+ *
+ * @return string Updated cell reference
+ */
+ private function updateSingleCellReference($pCellReference = 'A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0)
+ {
+ if (Coordinate::coordinateIsRange($pCellReference)) {
+ throw new Exception('Only single cell references may be passed to this method.');
+ }
+
+ // Get coordinate of $pBefore
+ list($beforeColumn, $beforeRow) = Coordinate::coordinateFromString($pBefore);
+
+ // Get coordinate of $pCellReference
+ list($newColumn, $newRow) = Coordinate::coordinateFromString($pCellReference);
+
+ // Verify which parts should be updated
+ $updateColumn = (($newColumn[0] != '$') && ($beforeColumn[0] != '$') && (Coordinate::columnIndexFromString($newColumn) >= Coordinate::columnIndexFromString($beforeColumn)));
+ $updateRow = (($newRow[0] != '$') && ($beforeRow[0] != '$') && $newRow >= $beforeRow);
+
+ // Create new column reference
+ if ($updateColumn) {
+ $newColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($newColumn) + $pNumCols);
+ }
+
+ // Create new row reference
+ if ($updateRow) {
+ $newRow = $newRow + $pNumRows;
+ }
+
+ // Return new reference
+ return $newColumn . $newRow;
+ }
+
+ /**
+ * __clone implementation. Cloning should not be allowed in a Singleton!
+ *
+ * @throws Exception
+ */
+ final public function __clone()
+ {
+ throw new Exception('Cloning a Singleton is not allowed!');
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/RichText/ITextElement.php b/htdocs/includes/phpoffice/PhpSpreadsheet/RichText/ITextElement.php
new file mode 100644
index 00000000000..69954676028
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/RichText/ITextElement.php
@@ -0,0 +1,36 @@
+richTextElements = [];
+
+ // Rich-Text string attached to cell?
+ if ($pCell !== null) {
+ // Add cell text and style
+ if ($pCell->getValue() != '') {
+ $objRun = new Run($pCell->getValue());
+ $objRun->setFont(clone $pCell->getWorksheet()->getStyle($pCell->getCoordinate())->getFont());
+ $this->addText($objRun);
+ }
+
+ // Set parent value
+ $pCell->setValueExplicit($this, DataType::TYPE_STRING);
+ }
+ }
+
+ /**
+ * Add text.
+ *
+ * @param ITextElement $pText Rich text element
+ *
+ * @return RichText
+ */
+ public function addText(ITextElement $pText)
+ {
+ $this->richTextElements[] = $pText;
+
+ return $this;
+ }
+
+ /**
+ * Create text.
+ *
+ * @param string $pText Text
+ *
+ * @throws Exception
+ *
+ * @return TextElement
+ */
+ public function createText($pText)
+ {
+ $objText = new TextElement($pText);
+ $this->addText($objText);
+
+ return $objText;
+ }
+
+ /**
+ * Create text run.
+ *
+ * @param string $pText Text
+ *
+ * @throws Exception
+ *
+ * @return Run
+ */
+ public function createTextRun($pText)
+ {
+ $objText = new Run($pText);
+ $this->addText($objText);
+
+ return $objText;
+ }
+
+ /**
+ * Get plain text.
+ *
+ * @return string
+ */
+ public function getPlainText()
+ {
+ // Return value
+ $returnValue = '';
+
+ // Loop through all ITextElements
+ foreach ($this->richTextElements as $text) {
+ $returnValue .= $text->getText();
+ }
+
+ return $returnValue;
+ }
+
+ /**
+ * Convert to string.
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return $this->getPlainText();
+ }
+
+ /**
+ * Get Rich Text elements.
+ *
+ * @return ITextElement[]
+ */
+ public function getRichTextElements()
+ {
+ return $this->richTextElements;
+ }
+
+ /**
+ * Set Rich Text elements.
+ *
+ * @param ITextElement[] $textElements Array of elements
+ *
+ * @return RichText
+ */
+ public function setRichTextElements(array $textElements)
+ {
+ $this->richTextElements = $textElements;
+
+ return $this;
+ }
+
+ /**
+ * Get hash code.
+ *
+ * @return string Hash code
+ */
+ public function getHashCode()
+ {
+ $hashElements = '';
+ foreach ($this->richTextElements as $element) {
+ $hashElements .= $element->getHashCode();
+ }
+
+ return md5(
+ $hashElements .
+ __CLASS__
+ );
+ }
+
+ /**
+ * Implement PHP __clone to create a deep clone, not just a shallow copy.
+ */
+ public function __clone()
+ {
+ $vars = get_object_vars($this);
+ foreach ($vars as $key => $value) {
+ if (is_object($value)) {
+ $this->$key = clone $value;
+ } else {
+ $this->$key = $value;
+ }
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/RichText/Run.php b/htdocs/includes/phpoffice/PhpSpreadsheet/RichText/Run.php
new file mode 100644
index 00000000000..b4996235f33
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/RichText/Run.php
@@ -0,0 +1,65 @@
+font = new Font();
+ }
+
+ /**
+ * Get font.
+ *
+ * @return null|\PhpOffice\PhpSpreadsheet\Style\Font
+ */
+ public function getFont()
+ {
+ return $this->font;
+ }
+
+ /**
+ * Set font.
+ *
+ * @param Font $pFont Font
+ *
+ * @return ITextElement
+ */
+ public function setFont(Font $pFont = null)
+ {
+ $this->font = $pFont;
+
+ return $this;
+ }
+
+ /**
+ * Get hash code.
+ *
+ * @return string Hash code
+ */
+ public function getHashCode()
+ {
+ return md5(
+ $this->getText() .
+ $this->font->getHashCode() .
+ __CLASS__
+ );
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/RichText/TextElement.php b/htdocs/includes/phpoffice/PhpSpreadsheet/RichText/TextElement.php
new file mode 100644
index 00000000000..d9ad0d7f5ed
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/RichText/TextElement.php
@@ -0,0 +1,86 @@
+text = $pText;
+ }
+
+ /**
+ * Get text.
+ *
+ * @return string Text
+ */
+ public function getText()
+ {
+ return $this->text;
+ }
+
+ /**
+ * Set text.
+ *
+ * @param $text string Text
+ *
+ * @return ITextElement
+ */
+ public function setText($text)
+ {
+ $this->text = $text;
+
+ return $this;
+ }
+
+ /**
+ * Get font.
+ *
+ * @return null|\PhpOffice\PhpSpreadsheet\Style\Font
+ */
+ public function getFont()
+ {
+ return null;
+ }
+
+ /**
+ * Get hash code.
+ *
+ * @return string Hash code
+ */
+ public function getHashCode()
+ {
+ return md5(
+ $this->text .
+ __CLASS__
+ );
+ }
+
+ /**
+ * Implement PHP __clone to create a deep clone, not just a shallow copy.
+ */
+ public function __clone()
+ {
+ $vars = get_object_vars($this);
+ foreach ($vars as $key => $value) {
+ if (is_object($value)) {
+ $this->$key = clone $value;
+ } else {
+ $this->$key = $value;
+ }
+ }
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Settings.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Settings.php
new file mode 100644
index 00000000000..22196b7e0e2
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Settings.php
@@ -0,0 +1,127 @@
+setLocale($locale);
+ }
+
+ /**
+ * Identify to PhpSpreadsheet the external library to use for rendering charts.
+ *
+ * @param string $rendererClass Class name of the chart renderer
+ * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
+ *
+ * @throws Exception
+ */
+ public static function setChartRenderer($rendererClass)
+ {
+ if (!is_a($rendererClass, IRenderer::class, true)) {
+ throw new Exception('Chart renderer must implement ' . IRenderer::class);
+ }
+
+ self::$chartRenderer = $rendererClass;
+ }
+
+ /**
+ * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
+ *
+ * @return null|string Class name of the chart renderer
+ * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
+ */
+ public static function getChartRenderer()
+ {
+ return self::$chartRenderer;
+ }
+
+ /**
+ * Set default options for libxml loader.
+ *
+ * @param int $options Default options for libxml loader
+ */
+ public static function setLibXmlLoaderOptions($options)
+ {
+ if ($options === null && defined('LIBXML_DTDLOAD')) {
+ $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
+ }
+ self::$libXmlLoaderOptions = $options;
+ }
+
+ /**
+ * Get default options for libxml loader.
+ * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
+ *
+ * @return int Default options for libxml loader
+ */
+ public static function getLibXmlLoaderOptions()
+ {
+ if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) {
+ self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
+ } elseif (self::$libXmlLoaderOptions === null) {
+ self::$libXmlLoaderOptions = true;
+ }
+
+ return self::$libXmlLoaderOptions;
+ }
+
+ /**
+ * Sets the implementation of cache that should be used for cell collection.
+ *
+ * @param CacheInterface $cache
+ */
+ public static function setCache(CacheInterface $cache)
+ {
+ self::$cache = $cache;
+ }
+
+ /**
+ * Gets the implementation of cache that should be used for cell collection.
+ *
+ * @return CacheInterface
+ */
+ public static function getCache()
+ {
+ if (!self::$cache) {
+ self::$cache = new Memory();
+ }
+
+ return self::$cache;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/CodePage.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/CodePage.php
new file mode 100644
index 00000000000..4b57824203e
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/CodePage.php
@@ -0,0 +1,138 @@
+ 'January',
+ 'Feb' => 'February',
+ 'Mar' => 'March',
+ 'Apr' => 'April',
+ 'May' => 'May',
+ 'Jun' => 'June',
+ 'Jul' => 'July',
+ 'Aug' => 'August',
+ 'Sep' => 'September',
+ 'Oct' => 'October',
+ 'Nov' => 'November',
+ 'Dec' => 'December',
+ ];
+
+ /**
+ * @var string[]
+ */
+ public static $numberSuffixes = [
+ 'st',
+ 'nd',
+ 'rd',
+ 'th',
+ ];
+
+ /**
+ * Base calendar year to use for calculations
+ * Value is either CALENDAR_WINDOWS_1900 (1900) or CALENDAR_MAC_1904 (1904).
+ *
+ * @var int
+ */
+ protected static $excelCalendar = self::CALENDAR_WINDOWS_1900;
+
+ /**
+ * Default timezone to use for DateTime objects.
+ *
+ * @var null|\DateTimeZone
+ */
+ protected static $defaultTimeZone;
+
+ /**
+ * Set the Excel calendar (Windows 1900 or Mac 1904).
+ *
+ * @param int $baseDate Excel base date (1900 or 1904)
+ *
+ * @return bool Success or failure
+ */
+ public static function setExcelCalendar($baseDate)
+ {
+ if (($baseDate == self::CALENDAR_WINDOWS_1900) ||
+ ($baseDate == self::CALENDAR_MAC_1904)) {
+ self::$excelCalendar = $baseDate;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return the Excel calendar (Windows 1900 or Mac 1904).
+ *
+ * @return int Excel base date (1900 or 1904)
+ */
+ public static function getExcelCalendar()
+ {
+ return self::$excelCalendar;
+ }
+
+ /**
+ * Set the Default timezone to use for dates.
+ *
+ * @param DateTimeZone|string $timeZone The timezone to set for all Excel datetimestamp to PHP DateTime Object conversions
+ *
+ * @throws \Exception
+ *
+ * @return bool Success or failure
+ * @return bool Success or failure
+ */
+ public static function setDefaultTimezone($timeZone)
+ {
+ if ($timeZone = self::validateTimeZone($timeZone)) {
+ self::$defaultTimeZone = $timeZone;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return the Default timezone being used for dates.
+ *
+ * @return DateTimeZone The timezone being used as default for Excel timestamp to PHP DateTime object
+ */
+ public static function getDefaultTimezone()
+ {
+ if (self::$defaultTimeZone === null) {
+ self::$defaultTimeZone = new DateTimeZone('UTC');
+ }
+
+ return self::$defaultTimeZone;
+ }
+
+ /**
+ * Validate a timezone.
+ *
+ * @param DateTimeZone|string $timeZone The timezone to validate, either as a timezone string or object
+ *
+ * @throws \Exception
+ *
+ * @return DateTimeZone The timezone as a timezone object
+ * @return DateTimeZone The timezone as a timezone object
+ */
+ protected static function validateTimeZone($timeZone)
+ {
+ if (is_object($timeZone) && $timeZone instanceof DateTimeZone) {
+ return $timeZone;
+ } elseif (is_string($timeZone)) {
+ return new DateTimeZone($timeZone);
+ }
+
+ throw new \Exception('Invalid timezone');
+ }
+
+ /**
+ * Convert a MS serialized datetime value from Excel to a PHP Date/Time object.
+ *
+ * @param float|int $excelTimestamp MS Excel serialized date/time value
+ * @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp,
+ * if you don't want to treat it as a UTC value
+ * Use the default (UST) unless you absolutely need a conversion
+ *
+ * @throws \Exception
+ *
+ * @return \DateTime PHP date/time object
+ */
+ public static function excelToDateTimeObject($excelTimestamp, $timeZone = null)
+ {
+ $timeZone = ($timeZone === null) ? self::getDefaultTimezone() : self::validateTimeZone($timeZone);
+ if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
+ if ($excelTimestamp < 1.0) {
+ // Unix timestamp base date
+ $baseDate = new \DateTime('1970-01-01', $timeZone);
+ } else {
+ // MS Excel calendar base dates
+ if (self::$excelCalendar == self::CALENDAR_WINDOWS_1900) {
+ // Allow adjustment for 1900 Leap Year in MS Excel
+ $baseDate = ($excelTimestamp < 60) ? new \DateTime('1899-12-31', $timeZone) : new \DateTime('1899-12-30', $timeZone);
+ } else {
+ $baseDate = new \DateTime('1904-01-01', $timeZone);
+ }
+ }
+ } else {
+ $baseDate = new \DateTime('1899-12-30', $timeZone);
+ }
+
+ $days = floor($excelTimestamp);
+ $partDay = $excelTimestamp - $days;
+ $hours = floor($partDay * 24);
+ $partDay = $partDay * 24 - $hours;
+ $minutes = floor($partDay * 60);
+ $partDay = $partDay * 60 - $minutes;
+ $seconds = round($partDay * 60);
+
+ if ($days >= 0) {
+ $days = '+' . $days;
+ }
+ $interval = $days . ' days';
+
+ return $baseDate->modify($interval)
+ ->setTime($hours, $minutes, $seconds);
+ }
+
+ /**
+ * Convert a MS serialized datetime value from Excel to a unix timestamp.
+ *
+ * @param float|int $excelTimestamp MS Excel serialized date/time value
+ * @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp,
+ * if you don't want to treat it as a UTC value
+ * Use the default (UST) unless you absolutely need a conversion
+ *
+ * @throws \Exception
+ *
+ * @return int Unix timetamp for this date/time
+ */
+ public static function excelToTimestamp($excelTimestamp, $timeZone = null)
+ {
+ return (int) self::excelToDateTimeObject($excelTimestamp, $timeZone)
+ ->format('U');
+ }
+
+ /**
+ * Convert a date from PHP to an MS Excel serialized date/time value.
+ *
+ * @param mixed $dateValue Unix Timestamp or PHP DateTime object or a string
+ *
+ * @return bool|float Excel date/time value
+ * or boolean FALSE on failure
+ */
+ public static function PHPToExcel($dateValue)
+ {
+ if ((is_object($dateValue)) && ($dateValue instanceof DateTimeInterface)) {
+ return self::dateTimeToExcel($dateValue);
+ } elseif (is_numeric($dateValue)) {
+ return self::timestampToExcel($dateValue);
+ } elseif (is_string($dateValue)) {
+ return self::stringToExcel($dateValue);
+ }
+
+ return false;
+ }
+
+ /**
+ * Convert a PHP DateTime object to an MS Excel serialized date/time value.
+ *
+ * @param DateTimeInterface $dateValue PHP DateTime object
+ *
+ * @return float MS Excel serialized date/time value
+ */
+ public static function dateTimeToExcel(DateTimeInterface $dateValue)
+ {
+ return self::formattedPHPToExcel(
+ $dateValue->format('Y'),
+ $dateValue->format('m'),
+ $dateValue->format('d'),
+ $dateValue->format('H'),
+ $dateValue->format('i'),
+ $dateValue->format('s')
+ );
+ }
+
+ /**
+ * Convert a Unix timestamp to an MS Excel serialized date/time value.
+ *
+ * @param int $dateValue Unix Timestamp
+ *
+ * @return float MS Excel serialized date/time value
+ */
+ public static function timestampToExcel($dateValue)
+ {
+ if (!is_numeric($dateValue)) {
+ return false;
+ }
+
+ return self::dateTimeToExcel(new \DateTime('@' . $dateValue));
+ }
+
+ /**
+ * formattedPHPToExcel.
+ *
+ * @param int $year
+ * @param int $month
+ * @param int $day
+ * @param int $hours
+ * @param int $minutes
+ * @param int $seconds
+ *
+ * @return float Excel date/time value
+ */
+ public static function formattedPHPToExcel($year, $month, $day, $hours = 0, $minutes = 0, $seconds = 0)
+ {
+ if (self::$excelCalendar == self::CALENDAR_WINDOWS_1900) {
+ //
+ // Fudge factor for the erroneous fact that the year 1900 is treated as a Leap Year in MS Excel
+ // This affects every date following 28th February 1900
+ //
+ $excel1900isLeapYear = true;
+ if (($year == 1900) && ($month <= 2)) {
+ $excel1900isLeapYear = false;
+ }
+ $myexcelBaseDate = 2415020;
+ } else {
+ $myexcelBaseDate = 2416481;
+ $excel1900isLeapYear = false;
+ }
+
+ // Julian base date Adjustment
+ if ($month > 2) {
+ $month -= 3;
+ } else {
+ $month += 9;
+ --$year;
+ }
+
+ // Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
+ $century = substr($year, 0, 2);
+ $decade = substr($year, 2, 2);
+ $excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myexcelBaseDate + $excel1900isLeapYear;
+
+ $excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
+
+ return (float) $excelDate + $excelTime;
+ }
+
+ /**
+ * Is a given cell a date/time?
+ *
+ * @param Cell $pCell
+ *
+ * @return bool
+ */
+ public static function isDateTime(Cell $pCell)
+ {
+ return self::isDateTimeFormat(
+ $pCell->getWorksheet()->getStyle(
+ $pCell->getCoordinate()
+ )->getNumberFormat()
+ );
+ }
+
+ /**
+ * Is a given number format a date/time?
+ *
+ * @param NumberFormat $pFormat
+ *
+ * @return bool
+ */
+ public static function isDateTimeFormat(NumberFormat $pFormat)
+ {
+ return self::isDateTimeFormatCode($pFormat->getFormatCode());
+ }
+
+ private static $possibleDateFormatCharacters = 'eymdHs';
+
+ /**
+ * Is a given number format code a date/time?
+ *
+ * @param string $pFormatCode
+ *
+ * @return bool
+ */
+ public static function isDateTimeFormatCode($pFormatCode)
+ {
+ if (strtolower($pFormatCode) === strtolower(NumberFormat::FORMAT_GENERAL)) {
+ // "General" contains an epoch letter 'e', so we trap for it explicitly here (case-insensitive check)
+ return false;
+ }
+ if (preg_match('/[0#]E[+-]0/i', $pFormatCode)) {
+ // Scientific format
+ return false;
+ }
+
+ // Switch on formatcode
+ switch ($pFormatCode) {
+ // Explicitly defined date formats
+ case NumberFormat::FORMAT_DATE_YYYYMMDD:
+ case NumberFormat::FORMAT_DATE_YYYYMMDD2:
+ case NumberFormat::FORMAT_DATE_DDMMYYYY:
+ case NumberFormat::FORMAT_DATE_DMYSLASH:
+ case NumberFormat::FORMAT_DATE_DMYMINUS:
+ case NumberFormat::FORMAT_DATE_DMMINUS:
+ case NumberFormat::FORMAT_DATE_MYMINUS:
+ case NumberFormat::FORMAT_DATE_DATETIME:
+ case NumberFormat::FORMAT_DATE_TIME1:
+ case NumberFormat::FORMAT_DATE_TIME2:
+ case NumberFormat::FORMAT_DATE_TIME3:
+ case NumberFormat::FORMAT_DATE_TIME4:
+ case NumberFormat::FORMAT_DATE_TIME5:
+ case NumberFormat::FORMAT_DATE_TIME6:
+ case NumberFormat::FORMAT_DATE_TIME7:
+ case NumberFormat::FORMAT_DATE_TIME8:
+ case NumberFormat::FORMAT_DATE_YYYYMMDDSLASH:
+ case NumberFormat::FORMAT_DATE_XLSX14:
+ case NumberFormat::FORMAT_DATE_XLSX15:
+ case NumberFormat::FORMAT_DATE_XLSX16:
+ case NumberFormat::FORMAT_DATE_XLSX17:
+ case NumberFormat::FORMAT_DATE_XLSX22:
+ return true;
+ }
+
+ // Typically number, currency or accounting (or occasionally fraction) formats
+ if ((substr($pFormatCode, 0, 1) == '_') || (substr($pFormatCode, 0, 2) == '0 ')) {
+ return false;
+ }
+ // Try checking for any of the date formatting characters that don't appear within square braces
+ if (preg_match('/(^|\])[^\[]*[' . self::$possibleDateFormatCharacters . ']/i', $pFormatCode)) {
+ // We might also have a format mask containing quoted strings...
+ // we don't want to test for any of our characters within the quoted blocks
+ if (strpos($pFormatCode, '"') !== false) {
+ $segMatcher = false;
+ foreach (explode('"', $pFormatCode) as $subVal) {
+ // Only test in alternate array entries (the non-quoted blocks)
+ if (($segMatcher = !$segMatcher) &&
+ (preg_match('/(^|\])[^\[]*[' . self::$possibleDateFormatCharacters . ']/i', $subVal))) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ return true;
+ }
+
+ // No date...
+ return false;
+ }
+
+ /**
+ * Convert a date/time string to Excel time.
+ *
+ * @param string $dateValue Examples: '2009-12-31', '2009-12-31 15:59', '2009-12-31 15:59:10'
+ *
+ * @return false|float Excel date/time serial value
+ */
+ public static function stringToExcel($dateValue)
+ {
+ if (strlen($dateValue) < 2) {
+ return false;
+ }
+ if (!preg_match('/^(\d{1,4}[ \.\/\-][A-Z]{3,9}([ \.\/\-]\d{1,4})?|[A-Z]{3,9}[ \.\/\-]\d{1,4}([ \.\/\-]\d{1,4})?|\d{1,4}[ \.\/\-]\d{1,4}([ \.\/\-]\d{1,4})?)( \d{1,2}:\d{1,2}(:\d{1,2})?)?$/iu', $dateValue)) {
+ return false;
+ }
+
+ $dateValueNew = DateTime::DATEVALUE($dateValue);
+
+ if ($dateValueNew === Functions::VALUE()) {
+ return false;
+ }
+
+ if (strpos($dateValue, ':') !== false) {
+ $timeValue = DateTime::TIMEVALUE($dateValue);
+ if ($timeValue === Functions::VALUE()) {
+ return false;
+ }
+ $dateValueNew += $timeValue;
+ }
+
+ return $dateValueNew;
+ }
+
+ /**
+ * Converts a month name (either a long or a short name) to a month number.
+ *
+ * @param string $month Month name or abbreviation
+ *
+ * @return int|string Month number (1 - 12), or the original string argument if it isn't a valid month name
+ */
+ public static function monthStringToNumber($month)
+ {
+ $monthIndex = 1;
+ foreach (self::$monthNames as $shortMonthName => $longMonthName) {
+ if (($month === $longMonthName) || ($month === $shortMonthName)) {
+ return $monthIndex;
+ }
+ ++$monthIndex;
+ }
+
+ return $month;
+ }
+
+ /**
+ * Strips an ordinal from a numeric value.
+ *
+ * @param string $day Day number with an ordinal
+ *
+ * @return int|string The integer value with any ordinal stripped, or the original string argument if it isn't a valid numeric
+ */
+ public static function dayStringToNumber($day)
+ {
+ $strippedDayValue = (str_replace(self::$numberSuffixes, '', $day));
+ if (is_numeric($strippedDayValue)) {
+ return (int) $strippedDayValue;
+ }
+
+ return $day;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Drawing.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Drawing.php
new file mode 100644
index 00000000000..25d6910d34e
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Drawing.php
@@ -0,0 +1,249 @@
+getName();
+ $size = $pDefaultFont->getSize();
+
+ if (isset(Font::$defaultColumnWidths[$name][$size])) {
+ // Exact width can be determined
+ $colWidth = $pValue * Font::$defaultColumnWidths[$name][$size]['width'] / Font::$defaultColumnWidths[$name][$size]['px'];
+ } else {
+ // We don't have data for this particular font and size, use approximation by
+ // extrapolating from Calibri 11
+ $colWidth = $pValue * 11 * Font::$defaultColumnWidths['Calibri'][11]['width'] / Font::$defaultColumnWidths['Calibri'][11]['px'] / $size;
+ }
+
+ return $colWidth;
+ }
+
+ /**
+ * Convert column width from (intrinsic) Excel units to pixels.
+ *
+ * @param float $pValue Value in cell dimension
+ * @param \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont Default font of the workbook
+ *
+ * @return int Value in pixels
+ */
+ public static function cellDimensionToPixels($pValue, \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont)
+ {
+ // Font name and size
+ $name = $pDefaultFont->getName();
+ $size = $pDefaultFont->getSize();
+
+ if (isset(Font::$defaultColumnWidths[$name][$size])) {
+ // Exact width can be determined
+ $colWidth = $pValue * Font::$defaultColumnWidths[$name][$size]['px'] / Font::$defaultColumnWidths[$name][$size]['width'];
+ } else {
+ // We don't have data for this particular font and size, use approximation by
+ // extrapolating from Calibri 11
+ $colWidth = $pValue * $size * Font::$defaultColumnWidths['Calibri'][11]['px'] / Font::$defaultColumnWidths['Calibri'][11]['width'] / 11;
+ }
+
+ // Round pixels to closest integer
+ $colWidth = (int) round($colWidth);
+
+ return $colWidth;
+ }
+
+ /**
+ * Convert pixels to points.
+ *
+ * @param int $pValue Value in pixels
+ *
+ * @return float Value in points
+ */
+ public static function pixelsToPoints($pValue)
+ {
+ return $pValue * 0.67777777;
+ }
+
+ /**
+ * Convert points to pixels.
+ *
+ * @param int $pValue Value in points
+ *
+ * @return int Value in pixels
+ */
+ public static function pointsToPixels($pValue)
+ {
+ if ($pValue != 0) {
+ return (int) ceil($pValue * 1.333333333);
+ }
+
+ return 0;
+ }
+
+ /**
+ * Convert degrees to angle.
+ *
+ * @param int $pValue Degrees
+ *
+ * @return int Angle
+ */
+ public static function degreesToAngle($pValue)
+ {
+ return (int) round($pValue * 60000);
+ }
+
+ /**
+ * Convert angle to degrees.
+ *
+ * @param int $pValue Angle
+ *
+ * @return int Degrees
+ */
+ public static function angleToDegrees($pValue)
+ {
+ if ($pValue != 0) {
+ return round($pValue / 60000);
+ }
+
+ return 0;
+ }
+
+ /**
+ * Create a new image from file. By alexander at alexauto dot nl.
+ *
+ * @see http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214
+ *
+ * @param string $p_sFile Path to Windows DIB (BMP) image
+ *
+ * @return resource
+ */
+ public static function imagecreatefrombmp($p_sFile)
+ {
+ // Load the image into a string
+ $file = fopen($p_sFile, 'rb');
+ $read = fread($file, 10);
+ while (!feof($file) && ($read != '')) {
+ $read .= fread($file, 1024);
+ }
+
+ $temp = unpack('H*', $read);
+ $hex = $temp[1];
+ $header = substr($hex, 0, 108);
+
+ // Process the header
+ // Structure: http://www.fastgraph.com/help/bmp_header_format.html
+ if (substr($header, 0, 4) == '424d') {
+ // Cut it in parts of 2 bytes
+ $header_parts = str_split($header, 2);
+
+ // Get the width 4 bytes
+ $width = hexdec($header_parts[19] . $header_parts[18]);
+
+ // Get the height 4 bytes
+ $height = hexdec($header_parts[23] . $header_parts[22]);
+
+ // Unset the header params
+ unset($header_parts);
+ }
+
+ // Define starting X and Y
+ $x = 0;
+ $y = 1;
+
+ // Create newimage
+ $image = imagecreatetruecolor($width, $height);
+
+ // Grab the body from the image
+ $body = substr($hex, 108);
+
+ // Calculate if padding at the end-line is needed
+ // Divided by two to keep overview.
+ // 1 byte = 2 HEX-chars
+ $body_size = (strlen($body) / 2);
+ $header_size = ($width * $height);
+
+ // Use end-line padding? Only when needed
+ $usePadding = ($body_size > ($header_size * 3) + 4);
+
+ // Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
+ // Calculate the next DWORD-position in the body
+ for ($i = 0; $i < $body_size; $i += 3) {
+ // Calculate line-ending and padding
+ if ($x >= $width) {
+ // If padding needed, ignore image-padding
+ // Shift i to the ending of the current 32-bit-block
+ if ($usePadding) {
+ $i += $width % 4;
+ }
+
+ // Reset horizontal position
+ $x = 0;
+
+ // Raise the height-position (bottom-up)
+ ++$y;
+
+ // Reached the image-height? Break the for-loop
+ if ($y > $height) {
+ break;
+ }
+ }
+
+ // Calculation of the RGB-pixel (defined as BGR in image-data)
+ // Define $i_pos as absolute position in the body
+ $i_pos = $i * 2;
+ $r = hexdec($body[$i_pos + 4] . $body[$i_pos + 5]);
+ $g = hexdec($body[$i_pos + 2] . $body[$i_pos + 3]);
+ $b = hexdec($body[$i_pos] . $body[$i_pos + 1]);
+
+ // Calculate and draw the pixel
+ $color = imagecolorallocate($image, $r, $g, $b);
+ imagesetpixel($image, $x, $height - $y, $color);
+
+ // Raise the horizontal position
+ ++$x;
+ }
+
+ // Unset the body / free the memory
+ unset($body);
+
+ // Return image-object
+ return $image;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher.php
new file mode 100644
index 00000000000..c6d0a6f8118
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher.php
@@ -0,0 +1,64 @@
+dggContainer;
+ }
+
+ /**
+ * Set Drawing Group Container.
+ *
+ * @param Escher\DggContainer $dggContainer
+ *
+ * @return Escher\DggContainer
+ */
+ public function setDggContainer($dggContainer)
+ {
+ return $this->dggContainer = $dggContainer;
+ }
+
+ /**
+ * Get Drawing Container.
+ *
+ * @return Escher\DgContainer
+ */
+ public function getDgContainer()
+ {
+ return $this->dgContainer;
+ }
+
+ /**
+ * Set Drawing Container.
+ *
+ * @param Escher\DgContainer $dgContainer
+ *
+ * @return Escher\DgContainer
+ */
+ public function setDgContainer($dgContainer)
+ {
+ return $this->dgContainer = $dgContainer;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DgContainer.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DgContainer.php
new file mode 100644
index 00000000000..e9d387daa4b
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DgContainer.php
@@ -0,0 +1,52 @@
+dgId;
+ }
+
+ public function setDgId($value)
+ {
+ $this->dgId = $value;
+ }
+
+ public function getLastSpId()
+ {
+ return $this->lastSpId;
+ }
+
+ public function setLastSpId($value)
+ {
+ $this->lastSpId = $value;
+ }
+
+ public function getSpgrContainer()
+ {
+ return $this->spgrContainer;
+ }
+
+ public function setSpgrContainer($spgrContainer)
+ {
+ return $this->spgrContainer = $spgrContainer;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DgContainer/SpgrContainer.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DgContainer/SpgrContainer.php
new file mode 100644
index 00000000000..7e2c34608a2
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DgContainer/SpgrContainer.php
@@ -0,0 +1,79 @@
+parent = $parent;
+ }
+
+ /**
+ * Get the parent Shape Group Container if any.
+ *
+ * @return null|\PhpOffice\PhpSpreadsheet\Shared\Escher\DgContainer\SpgrContainer
+ */
+ public function getParent()
+ {
+ return $this->parent;
+ }
+
+ /**
+ * Add a child. This will be either spgrContainer or spContainer.
+ *
+ * @param mixed $child
+ */
+ public function addChild($child)
+ {
+ $this->children[] = $child;
+ $child->setParent($this);
+ }
+
+ /**
+ * Get collection of Shape Containers.
+ */
+ public function getChildren()
+ {
+ return $this->children;
+ }
+
+ /**
+ * Recursively get all spContainers within this spgrContainer.
+ *
+ * @return SpgrContainer\SpContainer[]
+ */
+ public function getAllSpContainers()
+ {
+ $allSpContainers = [];
+
+ foreach ($this->children as $child) {
+ if ($child instanceof self) {
+ $allSpContainers = array_merge($allSpContainers, $child->getAllSpContainers());
+ } else {
+ $allSpContainers[] = $child;
+ }
+ }
+
+ return $allSpContainers;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DgContainer/SpgrContainer/SpContainer.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DgContainer/SpgrContainer/SpContainer.php
new file mode 100644
index 00000000000..bbf51df186e
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DgContainer/SpgrContainer/SpContainer.php
@@ -0,0 +1,369 @@
+parent = $parent;
+ }
+
+ /**
+ * Get the parent Shape Group Container.
+ *
+ * @return SpgrContainer
+ */
+ public function getParent()
+ {
+ return $this->parent;
+ }
+
+ /**
+ * Set whether this is a group shape.
+ *
+ * @param bool $value
+ */
+ public function setSpgr($value)
+ {
+ $this->spgr = $value;
+ }
+
+ /**
+ * Get whether this is a group shape.
+ *
+ * @return bool
+ */
+ public function getSpgr()
+ {
+ return $this->spgr;
+ }
+
+ /**
+ * Set the shape type.
+ *
+ * @param int $value
+ */
+ public function setSpType($value)
+ {
+ $this->spType = $value;
+ }
+
+ /**
+ * Get the shape type.
+ *
+ * @return int
+ */
+ public function getSpType()
+ {
+ return $this->spType;
+ }
+
+ /**
+ * Set the shape flag.
+ *
+ * @param int $value
+ */
+ public function setSpFlag($value)
+ {
+ $this->spFlag = $value;
+ }
+
+ /**
+ * Get the shape flag.
+ *
+ * @return int
+ */
+ public function getSpFlag()
+ {
+ return $this->spFlag;
+ }
+
+ /**
+ * Set the shape index.
+ *
+ * @param int $value
+ */
+ public function setSpId($value)
+ {
+ $this->spId = $value;
+ }
+
+ /**
+ * Get the shape index.
+ *
+ * @return int
+ */
+ public function getSpId()
+ {
+ return $this->spId;
+ }
+
+ /**
+ * Set an option for the Shape Group Container.
+ *
+ * @param int $property The number specifies the option
+ * @param mixed $value
+ */
+ public function setOPT($property, $value)
+ {
+ $this->OPT[$property] = $value;
+ }
+
+ /**
+ * Get an option for the Shape Group Container.
+ *
+ * @param int $property The number specifies the option
+ *
+ * @return mixed
+ */
+ public function getOPT($property)
+ {
+ if (isset($this->OPT[$property])) {
+ return $this->OPT[$property];
+ }
+
+ return null;
+ }
+
+ /**
+ * Get the collection of options.
+ *
+ * @return array
+ */
+ public function getOPTCollection()
+ {
+ return $this->OPT;
+ }
+
+ /**
+ * Set cell coordinates of upper-left corner of shape.
+ *
+ * @param string $value eg: 'A1'
+ */
+ public function setStartCoordinates($value)
+ {
+ $this->startCoordinates = $value;
+ }
+
+ /**
+ * Get cell coordinates of upper-left corner of shape.
+ *
+ * @return string
+ */
+ public function getStartCoordinates()
+ {
+ return $this->startCoordinates;
+ }
+
+ /**
+ * Set offset in x-direction of upper-left corner of shape measured in 1/1024 of column width.
+ *
+ * @param int $startOffsetX
+ */
+ public function setStartOffsetX($startOffsetX)
+ {
+ $this->startOffsetX = $startOffsetX;
+ }
+
+ /**
+ * Get offset in x-direction of upper-left corner of shape measured in 1/1024 of column width.
+ *
+ * @return int
+ */
+ public function getStartOffsetX()
+ {
+ return $this->startOffsetX;
+ }
+
+ /**
+ * Set offset in y-direction of upper-left corner of shape measured in 1/256 of row height.
+ *
+ * @param int $startOffsetY
+ */
+ public function setStartOffsetY($startOffsetY)
+ {
+ $this->startOffsetY = $startOffsetY;
+ }
+
+ /**
+ * Get offset in y-direction of upper-left corner of shape measured in 1/256 of row height.
+ *
+ * @return int
+ */
+ public function getStartOffsetY()
+ {
+ return $this->startOffsetY;
+ }
+
+ /**
+ * Set cell coordinates of bottom-right corner of shape.
+ *
+ * @param string $value eg: 'A1'
+ */
+ public function setEndCoordinates($value)
+ {
+ $this->endCoordinates = $value;
+ }
+
+ /**
+ * Get cell coordinates of bottom-right corner of shape.
+ *
+ * @return string
+ */
+ public function getEndCoordinates()
+ {
+ return $this->endCoordinates;
+ }
+
+ /**
+ * Set offset in x-direction of bottom-right corner of shape measured in 1/1024 of column width.
+ *
+ * @param int $endOffsetX
+ */
+ public function setEndOffsetX($endOffsetX)
+ {
+ $this->endOffsetX = $endOffsetX;
+ }
+
+ /**
+ * Get offset in x-direction of bottom-right corner of shape measured in 1/1024 of column width.
+ *
+ * @return int
+ */
+ public function getEndOffsetX()
+ {
+ return $this->endOffsetX;
+ }
+
+ /**
+ * Set offset in y-direction of bottom-right corner of shape measured in 1/256 of row height.
+ *
+ * @param int $endOffsetY
+ */
+ public function setEndOffsetY($endOffsetY)
+ {
+ $this->endOffsetY = $endOffsetY;
+ }
+
+ /**
+ * Get offset in y-direction of bottom-right corner of shape measured in 1/256 of row height.
+ *
+ * @return int
+ */
+ public function getEndOffsetY()
+ {
+ return $this->endOffsetY;
+ }
+
+ /**
+ * Get the nesting level of this spContainer. This is the number of spgrContainers between this spContainer and
+ * the dgContainer. A value of 1 = immediately within first spgrContainer
+ * Higher nesting level occurs if and only if spContainer is part of a shape group.
+ *
+ * @return int Nesting level
+ */
+ public function getNestingLevel()
+ {
+ $nestingLevel = 0;
+
+ $parent = $this->getParent();
+ while ($parent instanceof SpgrContainer) {
+ ++$nestingLevel;
+ $parent = $parent->getParent();
+ }
+
+ return $nestingLevel;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DggContainer.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DggContainer.php
new file mode 100644
index 00000000000..96da32131e3
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/Escher/DggContainer.php
@@ -0,0 +1,175 @@
+spIdMax;
+ }
+
+ /**
+ * Set maximum shape index of all shapes in all drawings (plus one).
+ *
+ * @param int $value
+ */
+ public function setSpIdMax($value)
+ {
+ $this->spIdMax = $value;
+ }
+
+ /**
+ * Get total number of drawings saved.
+ *
+ * @return int
+ */
+ public function getCDgSaved()
+ {
+ return $this->cDgSaved;
+ }
+
+ /**
+ * Set total number of drawings saved.
+ *
+ * @param int $value
+ */
+ public function setCDgSaved($value)
+ {
+ $this->cDgSaved = $value;
+ }
+
+ /**
+ * Get total number of shapes saved (including group shapes).
+ *
+ * @return int
+ */
+ public function getCSpSaved()
+ {
+ return $this->cSpSaved;
+ }
+
+ /**
+ * Set total number of shapes saved (including group shapes).
+ *
+ * @param int $value
+ */
+ public function setCSpSaved($value)
+ {
+ $this->cSpSaved = $value;
+ }
+
+ /**
+ * Get BLIP Store Container.
+ *
+ * @return DggContainer\BstoreContainer
+ */
+ public function getBstoreContainer()
+ {
+ return $this->bstoreContainer;
+ }
+
+ /**
+ * Set BLIP Store Container.
+ *
+ * @param DggContainer\BstoreContainer $bstoreContainer
+ */
+ public function setBstoreContainer($bstoreContainer)
+ {
+ $this->bstoreContainer = $bstoreContainer;
+ }
+
+ /**
+ * Set an option for the drawing group.
+ *
+ * @param int $property The number specifies the option
+ * @param mixed $value
+ */
+ public function setOPT($property, $value)
+ {
+ $this->OPT[$property] = $value;
+ }
+
+ /**
+ * Get an option for the drawing group.
+ *
+ * @param int $property The number specifies the option
+ *
+ * @return mixed
+ */
+ public function getOPT($property)
+ {
+ if (isset($this->OPT[$property])) {
+ return $this->OPT[$property];
+ }
+
+ return null;
+ }
+
+ /**
+ * Get identifier clusters.
+ *
+ * @return array
+ */
+ public function getIDCLs()
+ {
+ return $this->IDCLs;
+ }
+
+ /**
+ * Set identifier clusters. [
= -1; --$k) {
+ if ($k == -1) {
+ break;
+ }
+ if (abs($e[$k]) <= $eps * (abs($this->s[$k]) + abs($this->s[$k + 1]))) {
+ $e[$k] = 0.0;
+
+ break;
+ }
+ }
+ if ($k == $p - 2) {
+ $kase = 4;
+ } else {
+ for ($ks = $p - 1; $ks >= $k; --$ks) {
+ if ($ks == $k) {
+ break;
+ }
+ $t = ($ks != $p ? abs($e[$ks]) : 0.) + ($ks != $k + 1 ? abs($e[$ks - 1]) : 0.);
+ if (abs($this->s[$ks]) <= $eps * $t) {
+ $this->s[$ks] = 0.0;
+
+ break;
+ }
+ }
+ if ($ks == $k) {
+ $kase = 3;
+ } elseif ($ks == $p - 1) {
+ $kase = 1;
+ } else {
+ $kase = 2;
+ $k = $ks;
+ }
+ }
+ ++$k;
+
+ // Perform the task indicated by kase.
+ switch ($kase) {
+ // Deflate negligible s(p).
+ case 1:
+ $f = $e[$p - 2];
+ $e[$p - 2] = 0.0;
+ for ($j = $p - 2; $j >= $k; --$j) {
+ $t = hypo($this->s[$j], $f);
+ $cs = $this->s[$j] / $t;
+ $sn = $f / $t;
+ $this->s[$j] = $t;
+ if ($j != $k) {
+ $f = -$sn * $e[$j - 1];
+ $e[$j - 1] = $cs * $e[$j - 1];
+ }
+ if ($wantv) {
+ for ($i = 0; $i < $this->n; ++$i) {
+ $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$p - 1];
+ $this->V[$i][$p - 1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$p - 1];
+ $this->V[$i][$j] = $t;
+ }
+ }
+ }
+
+ break;
+ // Split at negligible s(k).
+ case 2:
+ $f = $e[$k - 1];
+ $e[$k - 1] = 0.0;
+ for ($j = $k; $j < $p; ++$j) {
+ $t = hypo($this->s[$j], $f);
+ $cs = $this->s[$j] / $t;
+ $sn = $f / $t;
+ $this->s[$j] = $t;
+ $f = -$sn * $e[$j];
+ $e[$j] = $cs * $e[$j];
+ if ($wantu) {
+ for ($i = 0; $i < $this->m; ++$i) {
+ $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$k - 1];
+ $this->U[$i][$k - 1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$k - 1];
+ $this->U[$i][$j] = $t;
+ }
+ }
+ }
+
+ break;
+ // Perform one qr step.
+ case 3:
+ // Calculate the shift.
+ $scale = max(max(max(max(abs($this->s[$p - 1]), abs($this->s[$p - 2])), abs($e[$p - 2])), abs($this->s[$k])), abs($e[$k]));
+ $sp = $this->s[$p - 1] / $scale;
+ $spm1 = $this->s[$p - 2] / $scale;
+ $epm1 = $e[$p - 2] / $scale;
+ $sk = $this->s[$k] / $scale;
+ $ek = $e[$k] / $scale;
+ $b = (($spm1 + $sp) * ($spm1 - $sp) + $epm1 * $epm1) / 2.0;
+ $c = ($sp * $epm1) * ($sp * $epm1);
+ $shift = 0.0;
+ if (($b != 0.0) || ($c != 0.0)) {
+ $shift = sqrt($b * $b + $c);
+ if ($b < 0.0) {
+ $shift = -$shift;
+ }
+ $shift = $c / ($b + $shift);
+ }
+ $f = ($sk + $sp) * ($sk - $sp) + $shift;
+ $g = $sk * $ek;
+ // Chase zeros.
+ for ($j = $k; $j < $p - 1; ++$j) {
+ $t = hypo($f, $g);
+ $cs = $f / $t;
+ $sn = $g / $t;
+ if ($j != $k) {
+ $e[$j - 1] = $t;
+ }
+ $f = $cs * $this->s[$j] + $sn * $e[$j];
+ $e[$j] = $cs * $e[$j] - $sn * $this->s[$j];
+ $g = $sn * $this->s[$j + 1];
+ $this->s[$j + 1] = $cs * $this->s[$j + 1];
+ if ($wantv) {
+ for ($i = 0; $i < $this->n; ++$i) {
+ $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$j + 1];
+ $this->V[$i][$j + 1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$j + 1];
+ $this->V[$i][$j] = $t;
+ }
+ }
+ $t = hypo($f, $g);
+ $cs = $f / $t;
+ $sn = $g / $t;
+ $this->s[$j] = $t;
+ $f = $cs * $e[$j] + $sn * $this->s[$j + 1];
+ $this->s[$j + 1] = -$sn * $e[$j] + $cs * $this->s[$j + 1];
+ $g = $sn * $e[$j + 1];
+ $e[$j + 1] = $cs * $e[$j + 1];
+ if ($wantu && ($j < $this->m - 1)) {
+ for ($i = 0; $i < $this->m; ++$i) {
+ $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$j + 1];
+ $this->U[$i][$j + 1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$j + 1];
+ $this->U[$i][$j] = $t;
+ }
+ }
+ }
+ $e[$p - 2] = $f;
+ $iter = $iter + 1;
+
+ break;
+ // Convergence.
+ case 4:
+ // Make the singular values positive.
+ if ($this->s[$k] <= 0.0) {
+ $this->s[$k] = ($this->s[$k] < 0.0 ? -$this->s[$k] : 0.0);
+ if ($wantv) {
+ for ($i = 0; $i <= $pp; ++$i) {
+ $this->V[$i][$k] = -$this->V[$i][$k];
+ }
+ }
+ }
+ // Order the singular values.
+ while ($k < $pp) {
+ if ($this->s[$k] >= $this->s[$k + 1]) {
+ break;
+ }
+ $t = $this->s[$k];
+ $this->s[$k] = $this->s[$k + 1];
+ $this->s[$k + 1] = $t;
+ if ($wantv and ($k < $this->n - 1)) {
+ for ($i = 0; $i < $this->n; ++$i) {
+ $t = $this->V[$i][$k + 1];
+ $this->V[$i][$k + 1] = $this->V[$i][$k];
+ $this->V[$i][$k] = $t;
+ }
+ }
+ if ($wantu and ($k < $this->m - 1)) {
+ for ($i = 0; $i < $this->m; ++$i) {
+ $t = $this->U[$i][$k + 1];
+ $this->U[$i][$k + 1] = $this->U[$i][$k];
+ $this->U[$i][$k] = $t;
+ }
+ }
+ ++$k;
+ }
+ $iter = 0;
+ --$p;
+
+ break;
+ } // end switch
+ } // end while
+ }
+
+ /**
+ * Return the left singular vectors.
+ *
+ * @return Matrix U
+ */
+ public function getU()
+ {
+ return new Matrix($this->U, $this->m, min($this->m + 1, $this->n));
+ }
+
+ /**
+ * Return the right singular vectors.
+ *
+ * @return Matrix V
+ */
+ public function getV()
+ {
+ return new Matrix($this->V);
+ }
+
+ /**
+ * Return the one-dimensional array of singular values.
+ *
+ * @return array diagonal of S
+ */
+ public function getSingularValues()
+ {
+ return $this->s;
+ }
+
+ /**
+ * Return the diagonal matrix of singular values.
+ *
+ * @return Matrix S
+ */
+ public function getS()
+ {
+ for ($i = 0; $i < $this->n; ++$i) {
+ for ($j = 0; $j < $this->n; ++$j) {
+ $S[$i][$j] = 0.0;
+ }
+ $S[$i][$i] = $this->s[$i];
+ }
+
+ return new Matrix($S);
+ }
+
+ /**
+ * Two norm.
+ *
+ * @return float max(S)
+ */
+ public function norm2()
+ {
+ return $this->s[0];
+ }
+
+ /**
+ * Two norm condition number.
+ *
+ * @return float max(S)/min(S)
+ */
+ public function cond()
+ {
+ return $this->s[0] / $this->s[min($this->m, $this->n) - 1];
+ }
+
+ /**
+ * Effective numerical matrix rank.
+ *
+ * @return int Number of nonnegligible singular values
+ */
+ public function rank()
+ {
+ $eps = pow(2.0, -52.0);
+ $tol = max($this->m, $this->n) * $this->s[0] * $eps;
+ $r = 0;
+ $iMax = count($this->s);
+ for ($i = 0; $i < $iMax; ++$i) {
+ if ($this->s[$i] > $tol) {
+ ++$r;
+ }
+ }
+
+ return $r;
+ }
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/JAMA/utils/Maths.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/JAMA/utils/Maths.php
new file mode 100644
index 00000000000..68c3864932c
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/JAMA/utils/Maths.php
@@ -0,0 +1,30 @@
+ abs($b)) {
+ $r = $b / $a;
+ $r = abs($a) * sqrt(1 + $r * $r);
+ } elseif ($b != 0) {
+ $r = $a / $b;
+ $r = abs($b) * sqrt(1 + $r * $r);
+ } else {
+ $r = 0.0;
+ }
+
+ return $r;
+}
diff --git a/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/OLE.php b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/OLE.php
new file mode 100644
index 00000000000..2e9ec256734
--- /dev/null
+++ b/htdocs/includes/phpoffice/PhpSpreadsheet/Shared/OLE.php
@@ -0,0 +1,567 @@
+ |
+// | Based on OLE::Storage_Lite by Kawai, Takanori |
+// +----------------------------------------------------------------------+
+//
+
+use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
+use PhpOffice\PhpSpreadsheet\Shared\OLE\ChainedBlockStream;
+use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\Root;
+
+/*
+ * Array for storing OLE instances that are accessed from
+ * OLE_ChainedBlockStream::stream_open().
+ *
+ * @var array
+ */
+$GLOBALS['_OLE_INSTANCES'] = [];
+
+/**
+ * OLE package base class.
+ *
+ * @author Xavier Noguer | ||||