Merge branch 'master' into issues/axis-options
This commit is contained in:
commit
0a56a9cfc6
|
|
@ -43,7 +43,7 @@ jobs:
|
|||
|
||||
- name: Delete composer lock file
|
||||
id: composer-lock
|
||||
if: ${{ matrix.php-version == '8.0' || matrix.php-version == '8.1' }}
|
||||
if: ${{ matrix.php-version == '8.1' }}
|
||||
run: |
|
||||
rm composer.lock
|
||||
echo "::set-output name=flags::--ignore-platform-reqs"
|
||||
|
|
@ -124,6 +124,37 @@ jobs:
|
|||
- name: Code style with PHP_CodeSniffer
|
||||
run: ./vendor/bin/phpcs -q --report=checkstyle | cs2pr
|
||||
|
||||
phpstan:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Setup PHP, with composer and extensions
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: 7.4
|
||||
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
|
||||
coverage: none
|
||||
tools: cs2pr
|
||||
|
||||
- name: Get composer cache directory
|
||||
id: composer-cache
|
||||
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
|
||||
|
||||
- name: Cache composer dependencies
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ${{ steps.composer-cache.outputs.dir }}
|
||||
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
|
||||
restore-keys: ${{ runner.os }}-composer-
|
||||
|
||||
- name: Install dependencies
|
||||
run: composer install --no-progress --prefer-dist --optimize-autoloader
|
||||
|
||||
- name: Static analysis with PHPStan
|
||||
run: ./vendor/bin/phpstan analyse
|
||||
|
||||
coverage:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
|
|
|||
|
|
@ -160,7 +160,7 @@ return PhpCsFixer\Config::create()
|
|||
'php_unit_test_annotation' => true,
|
||||
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
|
||||
'php_unit_test_class_requires_covers' => false, // We don't care as much as we should about coverage
|
||||
'phpdoc_add_missing_param_annotation' => true,
|
||||
'phpdoc_add_missing_param_annotation' => false, // Don't add things that bring no value
|
||||
'phpdoc_align' => false, // Waste of time
|
||||
'phpdoc_annotation_without_dot' => true,
|
||||
'phpdoc_indent' => true,
|
||||
|
|
|
|||
10
CHANGELOG.md
10
CHANGELOG.md
|
|
@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
|||
|
||||
### Added
|
||||
|
||||
- Improved support for Row and Column ranges in formulae [Issue #1755](https://github.com/PHPOffice/PhpSpreadsheet/issues/1755) [PR #2028](https://github.com/PHPOffice/PhpSpreadsheet/pull/2028)
|
||||
- Implemented the CHITEST(), CHISQ.DIST() and CHISQ.INV() and equivalent Statistical functions, for both left- and right-tailed distributions.
|
||||
- Support for ActiveSheet and SelectedCells in the ODS Reader and Writer. [PR #1908](https://github.com/PHPOffice/PhpSpreadsheet/pull/1908)
|
||||
|
||||
### Changed
|
||||
|
|
@ -24,8 +26,11 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
|||
- Nothing.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed issue with _ spacing character in number format mask corrumpting output from toFormattedString() [Issue 1924#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1924) [PR #1927](https://github.com/PHPOffice/PhpSpreadsheet/pull/1927)
|
||||
- Correct default fill style for conditional without a pattern defined [Issue #2035](https://github.com/PHPOffice/PhpSpreadsheet/issues/2035) [PR #2050](https://github.com/PHPOffice/PhpSpreadsheet/pull/2050)
|
||||
- Fixed issue where array key check for existince before accessing arrays in Xlsx.php. [PR #1970](https://github.com/PHPOffice/PhpSpreadsheet/pull/1970)
|
||||
- Fixed issue with quoted strings in number format mask rendered with toFormattedString() [Issue 1972#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1972) [PR #1978](https://github.com/PHPOffice/PhpSpreadsheet/pull/1978)
|
||||
- Fixed issue with percentage formats in number format mask rendered with toFormattedString() [Issue 1929#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1929) [PR #1928](https://github.com/PHPOffice/PhpSpreadsheet/pull/1928)
|
||||
- Fixed issue with _ spacing character in number format mask corrupting output from toFormattedString() [Issue 1924#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1924) [PR #1927](https://github.com/PHPOffice/PhpSpreadsheet/pull/1927)
|
||||
- Fix for [Issue #1887](https://github.com/PHPOffice/PhpSpreadsheet/issues/1887) - Lose Track of Selected Cells After Save
|
||||
- Fixed issue with Xlsx@listWorksheetInfo not returning any data
|
||||
- Fixed invalid arguments triggering mb_substr() error in LEFT(), MID() and RIGHT() text functions. [Issue #640](https://github.com/PHPOffice/PhpSpreadsheet/issues/640)
|
||||
|
|
@ -106,6 +111,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
|||
|
||||
### Fixed
|
||||
|
||||
- Fixed issue with absolute path in worksheets' Target. [PR #1769](https://github.com/PHPOffice/PhpSpreadsheet/pull/1769)
|
||||
- Fix for Xls Reader when SST has a bad length [#1592](https://github.com/PHPOffice/PhpSpreadsheet/issues/1592)
|
||||
- Resolve Xlsx loader issue whe hyperlinks don't have a destination
|
||||
- Resolve issues when printer settings resources IDs clash with drawing IDs
|
||||
|
|
|
|||
|
|
@ -9,3 +9,12 @@ If you would like to contribute, here are some notes and guidelines:
|
|||
- All code changes must be validated by `composer check`
|
||||
- [Helpful article about forking](https://help.github.com/articles/fork-a-repo/ "Forking a GitHub repository")
|
||||
- [Helpful article about pull requests](https://help.github.com/articles/using-pull-requests/ "Pull Requests")
|
||||
|
||||
## How to release
|
||||
|
||||
1. Complete CHANGELOG.md and commit
|
||||
2. Create an annotated tag
|
||||
1. `git tag -a 1.2.3`
|
||||
2. Tag subject must be the version number, eg: `1.2.3`
|
||||
3. Tag body must be a copy-paste of the changelog entries
|
||||
3. Push tag with `git push --tags`, GitHub Actions will create a GitHub release automatically
|
||||
|
|
|
|||
|
|
@ -1,7 +1,19 @@
|
|||
{
|
||||
"name": "phpoffice/phpspreadsheet",
|
||||
"description": "PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine",
|
||||
"keywords": ["PHP", "OpenXML", "Excel", "xlsx", "xls", "ods", "gnumeric", "spreadsheet"],
|
||||
"keywords": [
|
||||
"PHP",
|
||||
"OpenXML",
|
||||
"Excel",
|
||||
"xlsx",
|
||||
"xls",
|
||||
"ods",
|
||||
"gnumeric",
|
||||
"spreadsheet"
|
||||
],
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"homepage": "https://github.com/PHPOffice/PhpSpreadsheet",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
|
|
@ -29,7 +41,8 @@
|
|||
"check": [
|
||||
"php-cs-fixer fix --ansi --dry-run --diff",
|
||||
"phpcs",
|
||||
"phpunit --color=always"
|
||||
"phpunit --color=always",
|
||||
"phpstan analyse --ansi"
|
||||
],
|
||||
"fix": [
|
||||
"php-cs-fixer fix --ansi"
|
||||
|
|
@ -40,34 +53,36 @@
|
|||
},
|
||||
"require": {
|
||||
"php": "^7.2 || ^8.0",
|
||||
"ext-simplexml": "*",
|
||||
"ext-ctype": "*",
|
||||
"ext-dom": "*",
|
||||
"ext-fileinfo": "*",
|
||||
"ext-gd": "*",
|
||||
"ext-iconv": "*",
|
||||
"ext-fileinfo": "*",
|
||||
"ext-libxml": "*",
|
||||
"ext-mbstring": "*",
|
||||
"ext-SimpleXML": "*",
|
||||
"ext-xml": "*",
|
||||
"ext-xmlreader": "*",
|
||||
"ext-xmlwriter": "*",
|
||||
"ext-zip": "*",
|
||||
"ext-zlib": "*",
|
||||
"ezyang/htmlpurifier": "^4.13",
|
||||
"maennchen/zipstream-php": "^2.1",
|
||||
"markbaker/complex": "^1.5||^2.0",
|
||||
"markbaker/matrix": "^1.2||^2.0",
|
||||
"psr/simple-cache": "^1.0",
|
||||
"markbaker/complex": "^2.0",
|
||||
"markbaker/matrix": "^2.0",
|
||||
"psr/http-client": "^1.0",
|
||||
"psr/http-factory": "^1.0",
|
||||
"ezyang/htmlpurifier": "^4.13"
|
||||
"psr/simple-cache": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"dompdf/dompdf": "^0.8.5",
|
||||
"dompdf/dompdf": "^1.0",
|
||||
"friendsofphp/php-cs-fixer": "^2.18",
|
||||
"jpgraph/jpgraph": "^4.0",
|
||||
"mpdf/mpdf": "^8.0",
|
||||
"phpcompatibility/php-compatibility": "^9.3",
|
||||
"phpunit/phpunit": "^8.5||^9.3",
|
||||
"phpstan/phpstan": "^0.12.82",
|
||||
"phpstan/phpstan-phpunit": "^0.12.18",
|
||||
"phpunit/phpunit": "^8.5",
|
||||
"squizlabs/php_codesniffer": "^3.5",
|
||||
"tecnickcom/tcpdf": "^6.3"
|
||||
},
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -580,6 +580,18 @@ $writer->setUseBOM(true);
|
|||
$writer->save("05featuredemo.csv");
|
||||
```
|
||||
|
||||
#### Writing CSV files with desired encoding
|
||||
|
||||
It can be set to output with the encoding that can be specified by PHP's mb_convert_encoding.
|
||||
This looks like the following code:
|
||||
|
||||
```php
|
||||
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
|
||||
$writer->setUseBOM(false);
|
||||
$writer->setOutputEncoding('SJIS-WIN');
|
||||
$writer->save("05featuredemo.csv");
|
||||
```
|
||||
|
||||
#### Decimal and thousands separators
|
||||
|
||||
If the worksheet you are exporting contains numbers with decimal or
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,27 @@
|
|||
includes:
|
||||
- phpstan-baseline.neon
|
||||
- vendor/phpstan/phpstan-phpunit/extension.neon
|
||||
- vendor/phpstan/phpstan-phpunit/rules.neon
|
||||
|
||||
parameters:
|
||||
level: max
|
||||
paths:
|
||||
- src/
|
||||
- tests/
|
||||
parallel:
|
||||
processTimeout: 300.0
|
||||
checkMissingIterableValueType: false
|
||||
ignoreErrors:
|
||||
- '~^Class GdImage not found\.$~'
|
||||
- '~^Return typehint of method .* has invalid type GdImage\.$~'
|
||||
- '~^Property .* has unknown class GdImage as its type\.$~'
|
||||
- '~^Parameter .* of method .* has invalid typehint type GdImage\.$~'
|
||||
# Accept a bit anything for assert methods
|
||||
- '~^Parameter \#2 .* of static method PHPUnit\\Framework\\Assert\:\:assert\w+\(\) expects .*, .* given\.$~'
|
||||
- '~^Method PhpOffice\\PhpSpreadsheetTests\\.*\:\:test.*\(\) has parameter \$args with no typehint specified\.$~'
|
||||
|
||||
# Ignore all JpGraph issues
|
||||
- '~^Constant (MARK_CIRCLE|MARK_CROSS|MARK_DIAMOND|MARK_DTRIANGLE|MARK_FILLEDCIRCLE|MARK_SQUARE|MARK_STAR|MARK_UTRIANGLE|MARK_X|SIDE_RIGHT) not found\.$~'
|
||||
- '~^Instantiated class (AccBarPlot|AccLinePlot|BarPlot|ContourPlot|Graph|GroupBarPlot|GroupBarPlot|LinePlot|LinePlot|PieGraph|PiePlot|PiePlot3D|PiePlotC|RadarGraph|RadarPlot|ScatterPlot|Spline|StockPlot) not found\.$~'
|
||||
- '~^Call to method .*\(\) on an unknown class (AccBarPlot|AccLinePlot|BarPlot|ContourPlot|Graph|GroupBarPlot|GroupBarPlot|LinePlot|LinePlot|PieGraph|PiePlot|PiePlot3D|PiePlotC|RadarGraph|RadarPlot|ScatterPlot|Spline|StockPlot)\.$~'
|
||||
- '~^Access to property .* on an unknown class (AccBarPlot|AccLinePlot|BarPlot|ContourPlot|Graph|GroupBarPlot|GroupBarPlot|LinePlot|LinePlot|PieGraph|PiePlot|PiePlot3D|PiePlotC|RadarGraph|RadarPlot|ScatterPlot|Spline|StockPlot)\.$~'
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the accrued interest for a security that pays periodic interest.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Issue Date', DateHelper::getDateValue('01-Jan-2012')],
|
||||
['First Interest Date', DateHelper::getDateValue('01-Apr-2012')],
|
||||
['Settlement Date', DateHelper::getDateValue('31-Dec-2013')],
|
||||
['Annual Coupon Rate', 0.08],
|
||||
['Par Value', 10000],
|
||||
['Frequency', 4],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B3')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('0.00%');
|
||||
$worksheet->getStyle('B5')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B10', '=ACCRINT(B1, B2, B3, B4, B5, B6)');
|
||||
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell('B10')->getValue());
|
||||
$helper->log('ACCRINT() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the accrued interest for a security that pays interest at maturity.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Issue Date', DateHelper::getDateValue('01-Jan-2012')],
|
||||
['Settlement Date', DateHelper::getDateValue('31-Dec-2012')],
|
||||
['Annual Coupon Rate', 0.08],
|
||||
['Par Value', 10000],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('0.00%');
|
||||
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B6', '=ACCRINTM(B1, B2, B3, B4)');
|
||||
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell('B6')->getValue());
|
||||
$helper->log('ACCRINTM() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the prorated linear depreciation of an asset for a specified accounting period.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Cost', 150.00],
|
||||
['Date Purchased', DateHelper::getDateValue('01-Jan-2015')],
|
||||
['First Period Date', DateHelper::getDateValue('30-Sep-2015')],
|
||||
['Salvage Value', 20.00],
|
||||
['Number of Periods', 1],
|
||||
['Depreciation Rate', 0.20],
|
||||
['Basis', FinancialConstants::BASIS_DAYS_PER_YEAR_360_EUROPEAN],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
$worksheet->getStyle('B2:B3')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B10', '=AMORDEGRC(B1, B2, B3, B4, B5, B6, B7)');
|
||||
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell('B10')->getValue());
|
||||
$helper->log('AMORDEGRC() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the prorated linear depreciation of an asset for a specified accounting period.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Cost', 150.00],
|
||||
['Date Purchased', DateHelper::getDateValue('01-Jan-2015')],
|
||||
['First Period Date', DateHelper::getDateValue('30-Sep-2015')],
|
||||
['Salvage Value', 20.00],
|
||||
['Period', 1],
|
||||
['Depreciation Rate', 0.20],
|
||||
['Basis', FinancialConstants::BASIS_DAYS_PER_YEAR_360_EUROPEAN],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
$worksheet->getStyle('B2:B3')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B10', '=AMORLINC(B1, B2, B3, B4, B5, B6, B7)');
|
||||
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell('B10')->getValue());
|
||||
$helper->log('AMORLINC() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the number of days from the beginning of a coupon\'s period to the settlement date.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
|
||||
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
|
||||
['Frequency', 4],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B6', '=COUPDAYBS(B1, B2, B3)');
|
||||
|
||||
$helper->log($worksheet->getCell('B6')->getValue());
|
||||
$helper->log('COUPDAYBS() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the number of days in the coupon period that contains the settlement date.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
|
||||
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
|
||||
['Frequency', 4],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B6', '=COUPDAYS(B1, B2, B3)');
|
||||
|
||||
$helper->log($worksheet->getCell('B6')->getValue());
|
||||
$helper->log('COUPDAYS() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the number of days from the settlement date to the next coupon date.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
|
||||
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
|
||||
['Frequency', 4],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B6', '=COUPDAYSNC(B1, B2, B3)');
|
||||
|
||||
$helper->log($worksheet->getCell('B6')->getValue());
|
||||
$helper->log('COUPDAYSNC() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the next coupon date, after the settlement date.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
|
||||
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
|
||||
['Frequency', 4],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B6', '=COUPNCD(B1, B2, B3)');
|
||||
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
|
||||
$helper->log($worksheet->getCell('B6')->getValue());
|
||||
$helper->log('COUPNCD() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the number of coupons payable, between a security\'s settlement date and maturity date,');
|
||||
$helper->log('rounded up to the nearest whole coupon.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
|
||||
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
|
||||
['Frequency', 4],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B6', '=COUPNUM(B1, B2, B3)');
|
||||
|
||||
$helper->log($worksheet->getCell('B6')->getValue());
|
||||
$helper->log('COUPNUM() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the previous coupon date, before the settlement date for a security.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
|
||||
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
|
||||
['Frequency', 4],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B6', '=COUPPCD(B1, B2, B3)');
|
||||
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
|
||||
$helper->log($worksheet->getCell('B6')->getValue());
|
||||
$helper->log('COUPPCD() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the cumulative interest paid on a loan or investment, between two specified periods.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Interest Rate (per period)', 0.05 / 12],
|
||||
['Number of Periods', 5 * 12],
|
||||
['Present Value', 50000],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('0.00%');
|
||||
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$baseRow = 5;
|
||||
for ($year = 1; $year <= 5; ++$year) {
|
||||
$row = (string) ($baseRow + $year);
|
||||
$yearStartPeriod = (int) $year * 12 - 11;
|
||||
$yearEndPeriod = (int) $year * 12;
|
||||
|
||||
$worksheet->setCellValue("A{$row}", "Yr {$year}");
|
||||
$worksheet->setCellValue("B{$row}", "=CUMIPMT(\$B\$1, \$B\$2, \$B\$3, {$yearStartPeriod}, {$yearEndPeriod}, 0)");
|
||||
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell("B{$row}")->getValue());
|
||||
$helper->log("CUMIPMT() Year {$year} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the cumulative payment on the principal of a loan or investment, between two specified periods.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Interest Rate (per period)', 0.05 / 12],
|
||||
['Number of Periods', 5 * 12],
|
||||
['Present Value', 50000],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('0.00%');
|
||||
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$baseRow = 5;
|
||||
for ($year = 1; $year <= 5; ++$year) {
|
||||
$row = (string) ($baseRow + $year);
|
||||
$yearStartPeriod = (int) $year * 12 - 11;
|
||||
$yearEndPeriod = (int) $year * 12;
|
||||
|
||||
$worksheet->setCellValue("A{$row}", "Yr {$year}");
|
||||
$worksheet->setCellValue("B{$row}", "=CUMPRINC(\$B\$1, \$B\$2, \$B\$3, {$yearStartPeriod}, {$yearEndPeriod}, 0)");
|
||||
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell("B{$row}")->getValue());
|
||||
$helper->log("CUMPRINC() Year {$year} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the depreciation of an asset, using the Fixed Declining Balance Method,');
|
||||
$helper->log('for each period of the asset\'s lifetime.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Cost Value', 10000],
|
||||
['Salvage', 1000],
|
||||
['Life', 5, 'Years'],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$baseRow = 5;
|
||||
for ($year = 1; $year <= 5; ++$year) {
|
||||
$row = (string) ($baseRow + $year);
|
||||
|
||||
$worksheet->setCellValue("A{$row}", "Depreciation after Yr {$year}");
|
||||
$worksheet->setCellValue("B{$row}", "=DB(\$B\$1, \$B\$2, \$B\$3, {$year})");
|
||||
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell("B{$row}")->getValue());
|
||||
$helper->log("DB() Year {$year} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
|
||||
}
|
||||
|
||||
$helper->log('And with depreciation only starting after 6 months.');
|
||||
|
||||
$baseRow = 12;
|
||||
for ($year = 1; $year <= 6; ++$year) {
|
||||
$row = (string) ($baseRow + $year);
|
||||
|
||||
$worksheet->setCellValue("A{$row}", "Depreciation after Yr {$year}");
|
||||
$worksheet->setCellValue("B{$row}", "=DB(\$B\$1, \$B\$2, \$B\$3, {$year}, 6)");
|
||||
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell("B{$row}")->getValue());
|
||||
$helper->log("DB() Year {$year} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the depreciation of an asset, using the Double Declining Balance Method,');
|
||||
$helper->log('for each period of the asset\'s lifetime.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Cost Value', 10000],
|
||||
['Salvage', 1000],
|
||||
['Life', 5, 'Years'],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$baseRow = 5;
|
||||
for ($year = 1; $year <= 5; ++$year) {
|
||||
$row = (string) ($baseRow + $year);
|
||||
|
||||
$worksheet->setCellValue("A{$row}", "Depreciation after Yr {$year}");
|
||||
$worksheet->setCellValue("B{$row}", "=DDB(\$B\$1, \$B\$2, \$B\$3, {$year})");
|
||||
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell("B{$row}")->getValue());
|
||||
$helper->log("DDB() Year {$year} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the the Discount Rate for a security.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Settlement Date', DateHelper::getDateValue('01-Apr-2016')],
|
||||
['Maturity Date', DateHelper::getDateValue('31-Mar-2021')],
|
||||
['Par Value', 95.00],
|
||||
['Redemption Value', 100.00],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
$worksheet->getStyle('B3:B4')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B7', '=DISC(B1, B2, B3, B4)');
|
||||
$worksheet->getStyle('B7')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
$helper->log($worksheet->getCell('B7')->getValue());
|
||||
$helper->log('DISC() Result is ' . $worksheet->getCell('B7')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the dollar value in fractional notation, into a dollar value expressed as a decimal.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
[1.01, 16],
|
||||
[1.1, 16],
|
||||
[1.03, 32],
|
||||
[1.3, 32],
|
||||
[1.12, 32],
|
||||
];
|
||||
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
|
||||
// Now the formula
|
||||
for ($row = 1; $row <= 5; ++$row) {
|
||||
$worksheet->setCellValue("C{$row}", "=DOLLARDE(A{$row}, B{$row})");
|
||||
|
||||
$helper->log($worksheet->getCell("C{$row}")->getValue());
|
||||
$helper->log('DOLLARDE() Result is ' . $worksheet->getCell("C{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the dollar value expressed as a decimal number, into a dollar price, expressed as a fraction.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
[1.0625, 16],
|
||||
[1.625, 16],
|
||||
[1.09375, 32],
|
||||
[1.9375, 32],
|
||||
[1.375, 32],
|
||||
];
|
||||
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
|
||||
// Now the formula
|
||||
for ($row = 1; $row <= 5; ++$row) {
|
||||
$worksheet->setCellValue("C{$row}", "=DOLLARFR(A{$row}, B{$row})");
|
||||
|
||||
$helper->log($worksheet->getCell("C{$row}")->getValue());
|
||||
$helper->log('DOLLARFR() Result is ' . $worksheet->getCell("C{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the effective annual interest rate for a given nominal interest rate and number of');
|
||||
$helper->log('compounding periods per year.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
[0.10, 4],
|
||||
[0.10, 2],
|
||||
[0.025, 2],
|
||||
];
|
||||
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B3')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
// Now the formula
|
||||
for ($row = 1; $row <= 3; ++$row) {
|
||||
$worksheet->setCellValue("C{$row}", "=EFFECT(A{$row}, B{$row})");
|
||||
$worksheet->getStyle("C{$row}")->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
$helper->log($worksheet->getCell("C{$row}")->getValue());
|
||||
$helper->log('EFFECT() Result is ' . $worksheet->getCell("C{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the Future Value of an investment with periodic constant payments and a constant interest rate.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Interest Rate', 0.05, 0.10],
|
||||
['Pament Frequency', 12, 4],
|
||||
['Duration (Years)', 5, 4],
|
||||
['Investment', -1000.00, -2000.00],
|
||||
['Payment Type', 0, 1],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:C1')->getNumberFormat()->setFormatCode('0.00%');
|
||||
$worksheet->getStyle('B4:C4')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B8', '=FV(B1/B2, B3*B2, B4)');
|
||||
$worksheet->setCellValue('C8', '=FV(C1/C2, C3*C2, C4, null, C5)');
|
||||
$worksheet->getStyle('B8:C8')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell('B8')->getValue());
|
||||
$helper->log('FV() Result is ' . $worksheet->getCell('B8')->getFormattedValue());
|
||||
|
||||
$helper->log($worksheet->getCell('C6')->getValue());
|
||||
$helper->log('FV() Result is ' . $worksheet->getCell('C8')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the Future Value of an initial principal, after applying a series of compound interest rates.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Principal'],
|
||||
[10000.00],
|
||||
[null],
|
||||
['Schedule'],
|
||||
[0.05],
|
||||
[0.05],
|
||||
[0.035],
|
||||
[0.035],
|
||||
[0.035],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('A2')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
$worksheet->getStyle('A5:A9')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B1', '=FVSCHEDULE(A2, A5:A9)');
|
||||
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell('B1')->getValue());
|
||||
$helper->log('FVSCHEDULE() Result is ' . $worksheet->getCell('B1')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the interest rate for a fully invested security.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Settlement Date', DateHelper::getDateValue('01-Apr-2005')],
|
||||
['Maturity Date', DateHelper::getDateValue('31-Mar-2010')],
|
||||
['Investment', 1000.00],
|
||||
['Investment', 2125.00],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
|
||||
$worksheet->getStyle('B3:B4')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B7', '=INTRATE(B1, B2, B3, B4)');
|
||||
$worksheet->getStyle('B7')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
$helper->log($worksheet->getCell('B7')->getValue());
|
||||
$helper->log('INTRATE() Result is ' . $worksheet->getCell('B7')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the interest payment, during a specific period of a loan or investment that is paid in,');
|
||||
$helper->log('constant periodic payments, with a constant interest rate.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Interest Rate', 0.05],
|
||||
['Number of Years', 5],
|
||||
['Present Value', 50000.00],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('0.00%');
|
||||
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$baseRow = 6;
|
||||
for ($month = 1; $month <= 12; ++$month) {
|
||||
$row = (string) ($baseRow + $month);
|
||||
|
||||
$worksheet->setCellValue("A{$row}", "Payment for Mth {$month}");
|
||||
$worksheet->setCellValue("B{$row}", "=IPMT(\$B\$1/12, {$month}, \$B\$2*12, \$B\$3)");
|
||||
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell("B{$row}")->getValue());
|
||||
$helper->log("IPMT() Month {$month} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the Internal Rate of Return for a supplied series of periodic cash flows.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Initial Investment', -100.00],
|
||||
['Year 1 Income', 20.00],
|
||||
['Year 2 Income', 24.00, 'IRR after 3 Years'],
|
||||
['Year 3 Income', 28.80],
|
||||
['Year 4 Income', 34.56, 'IRR after 5 Years'],
|
||||
['Year 5 Income', 41.47],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B6')->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('C4', '=IRR(B1:B4)');
|
||||
$worksheet->getStyle('C4')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
$helper->log($worksheet->getCell('C4')->getValue());
|
||||
$helper->log('IRR() Result is ' . $worksheet->getCell('C4')->getFormattedValue());
|
||||
|
||||
$worksheet->setCellValue('C6', '=IRR(B1:B6)');
|
||||
$worksheet->getStyle('C6')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
$helper->log($worksheet->getCell('C6')->getValue());
|
||||
$helper->log('IRR() Result is ' . $worksheet->getCell('C6')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the interest paid during a specific period of a loan or investment.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Interest Rate', 0.05],
|
||||
['Number of Years', 5],
|
||||
['Present Value', 50000.00],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('0.00%');
|
||||
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$baseRow = 6;
|
||||
for ($month = 1; $month <= 12; ++$month) {
|
||||
$row = (string) ($baseRow + $month);
|
||||
|
||||
$worksheet->setCellValue("A{$row}", "Payment for Mth {$month}");
|
||||
$worksheet->setCellValue("B{$row}", "=ISPMT(\$B\$1/12, {$month}, \$B\$2*12, \$B\$3)");
|
||||
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell("B{$row}")->getValue());
|
||||
$helper->log("ISPMT() Month {$month} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the Modified Internal Rate of Return for a supplied series of periodic cash flows.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Initial Investment', -100.00],
|
||||
['Year 1 Income', 18.00],
|
||||
['Year 2 Income', 22.50, 'MIRR after 3 Years'],
|
||||
['Year 3 Income', 28.00],
|
||||
['Year 4 Income', 35.50, 'MIRR after 5 Years'],
|
||||
['Year 5 Income', 45.00],
|
||||
[null],
|
||||
['Finance Rate', 0.055],
|
||||
['Re-invest Rate', 0.05],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B6')->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
|
||||
$worksheet->getStyle('B8:B9')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('C4', '=MIRR(B1:B4, B8, B9)');
|
||||
$worksheet->getStyle('C4')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
$helper->log($worksheet->getCell('C4')->getValue());
|
||||
$helper->log('MIRR() Result is ' . $worksheet->getCell('C4')->getFormattedValue());
|
||||
|
||||
$worksheet->setCellValue('C6', '=MIRR(B1:B6, B8, B9)');
|
||||
$worksheet->getStyle('C6')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
$helper->log($worksheet->getCell('C6')->getValue());
|
||||
$helper->log('MIRR() Result is ' . $worksheet->getCell('C6')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the nominal interest rate for a given effective interest rate and number of');
|
||||
$helper->log('compounding periods per year.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
[0.10, 4],
|
||||
[0.10, 2],
|
||||
[0.025, 12],
|
||||
];
|
||||
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:B3')->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
// Now the formula
|
||||
for ($row = 1; $row <= 3; ++$row) {
|
||||
$worksheet->setCellValue("C{$row}", "=NOMINAL(A{$row}, B{$row})");
|
||||
$worksheet->getStyle("C{$row}")->getNumberFormat()->setFormatCode('0.00%');
|
||||
|
||||
$helper->log($worksheet->getCell("C{$row}")->getValue());
|
||||
$helper->log('NOMINAL() Result is ' . $worksheet->getCell("C{$row}")->getFormattedValue());
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the number of periods required to pay off a loan, for a constant periodic payment');
|
||||
$helper->log('and a constant interest rate.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Interest Rate', 0.04, 0.06],
|
||||
['Payments per Year', 1, 4],
|
||||
['Payment Amount', -6000.00, -2000],
|
||||
['Present Value', 50000, 60000],
|
||||
['Future Value', null, 30000],
|
||||
['Payment Type', null, FinancialConstants::PAYMENT_BEGINNING_OF_PERIOD],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:C1')->getNumberFormat()->setFormatCode('0.00%');
|
||||
$worksheet->getStyle('B3:C5')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
$worksheet->setCellValue('B8', '=NPER(B1/B2, B3, B4)');
|
||||
|
||||
$helper->log($worksheet->getCell('B8')->getValue());
|
||||
$helper->log('NPER() Result is ' . $worksheet->getCell('B8')->getFormattedValue());
|
||||
|
||||
$worksheet->setCellValue('C8', '=NPER(C1/C2, C3, C4, C5, C6)');
|
||||
|
||||
$helper->log($worksheet->getCell('C8')->getValue());
|
||||
$helper->log('NPER() Result is ' . $worksheet->getCell('C8')->getFormattedValue());
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../../Header.php';
|
||||
|
||||
$helper->log('Returns the Net Present Value of an investment, based on a supplied discount rate,');
|
||||
$helper->log('and a series of future payments and income.');
|
||||
|
||||
// Create new PhpSpreadsheet object
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// Add some data
|
||||
$arguments = [
|
||||
['Annual Discount Rate', 0.02, 0.05],
|
||||
['Initial Investment Cost', -5000.00, -10000],
|
||||
['Return from Year 1', 800.00, 2000.00],
|
||||
['Return from Year 2', 950.00, 2400.00],
|
||||
['Return from Year 3', 1080.00, 2900.00],
|
||||
['Return from Year 4', 1220.00, 3500.00],
|
||||
['Return from Year 5', 1500.00, 4100.00],
|
||||
];
|
||||
|
||||
// Some basic formatting for the data
|
||||
$worksheet->fromArray($arguments, null, 'A1');
|
||||
$worksheet->getStyle('B1:C1')->getNumberFormat()->setFormatCode('0.00%');
|
||||
$worksheet->getStyle('B2:C7')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
// Now the formula
|
||||
// When initial investment is made at the end of the first period
|
||||
$worksheet->setCellValue('B10', '=NPV(B1, B2:B7)');
|
||||
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell('B10')->getValue());
|
||||
$helper->log('NPV() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
|
||||
|
||||
// When initial investment is made at the start of the first period
|
||||
$worksheet->setCellValue('C10', '=NPV(C1, C3:C7) + C2');
|
||||
$worksheet->getStyle('C10')->getNumberFormat()->setFormatCode('$#,##0.00');
|
||||
|
||||
$helper->log($worksheet->getCell('C10')->getValue());
|
||||
$helper->log('NPV() Result is ' . $worksheet->getCell('C10')->getFormattedValue());
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Shared\File;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
require __DIR__ . '/../Header.php';
|
||||
|
|
@ -35,7 +36,7 @@ $helper->log('Write link: ' . $baseUrl);
|
|||
|
||||
$drawing->setWorksheet($aSheet);
|
||||
|
||||
$filename = tempnam(\PhpOffice\PhpSpreadsheet\Shared\File::sysGetTempDir(), 'phpspreadsheet-test');
|
||||
$filename = File::temporaryFilename();
|
||||
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, $inputFileType);
|
||||
$writer->save($filename);
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -17,7 +17,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DAverage class instead
|
||||
* @see Database\DAverage::evaluate()
|
||||
* Use the evaluate() method in the Database\DAverage class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -52,7 +53,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DCount class instead
|
||||
* @see Database\DCount::evaluate()
|
||||
* Use the evaluate() method in the Database\DCount class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -89,7 +91,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DCountA class instead
|
||||
* @see Database\DCountA::evaluate()
|
||||
* Use the evaluate() method in the Database\DCountA class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -124,7 +127,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DGet class instead
|
||||
* @see Database\DGet::evaluate()
|
||||
* Use the evaluate() method in the Database\DGet class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -159,7 +163,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DMax class instead
|
||||
* @see Database\DMax::evaluate()
|
||||
* Use the evaluate() method in the Database\DMax class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -194,7 +199,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DMin class instead
|
||||
* @see Database\DMin::evaluate()
|
||||
* Use the evaluate() method in the Database\DMin class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -228,7 +234,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DProduct class instead
|
||||
* @see Database\DProduct::evaluate()
|
||||
* Use the evaluate() method in the Database\DProduct class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -245,7 +252,7 @@ class Database
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float
|
||||
* @return float|string
|
||||
*/
|
||||
public static function DPRODUCT($database, $field, $criteria)
|
||||
{
|
||||
|
|
@ -263,7 +270,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DStDev class instead
|
||||
* @see Database\DStDev::evaluate()
|
||||
* Use the evaluate() method in the Database\DStDev class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -298,7 +306,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DStDevP class instead
|
||||
* @see Database\DStDevP::evaluate()
|
||||
* Use the evaluate() method in the Database\DStDevP class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -332,7 +341,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DSum class instead
|
||||
* @see Database\DSum::evaluate()
|
||||
* Use the evaluate() method in the Database\DSum class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -349,7 +359,7 @@ class Database
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float
|
||||
* @return float|string
|
||||
*/
|
||||
public static function DSUM($database, $field, $criteria)
|
||||
{
|
||||
|
|
@ -367,7 +377,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DVar class instead
|
||||
* @see Database\DVar::evaluate()
|
||||
* Use the evaluate() method in the Database\DVar class instead
|
||||
*
|
||||
* @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
|
||||
|
|
@ -402,7 +413,8 @@ class Database
|
|||
*
|
||||
* @Deprecated 1.17.0
|
||||
*
|
||||
* @see Use the evaluate() method in the Database\DVarP class instead
|
||||
* @see Database\DVarP::evaluate()
|
||||
* Use the evaluate() method in the Database\DVarP class instead
|
||||
*
|
||||
* @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
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class DAverage extends DatabaseAbstract
|
|||
return null;
|
||||
}
|
||||
|
||||
return Averages::AVERAGE(
|
||||
return Averages::average(
|
||||
self::getFilteredColumn($database, $field, $criteria)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class DMax extends DatabaseAbstract
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float
|
||||
* @return null|float|string
|
||||
*/
|
||||
public static function evaluate($database, $field, $criteria)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class DMin extends DatabaseAbstract
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float
|
||||
* @return null|float|string
|
||||
*/
|
||||
public static function evaluate($database, $field, $criteria)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ class DProduct extends DatabaseAbstract
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float
|
||||
* @return null|float|string
|
||||
*/
|
||||
public static function evaluate($database, $field, $criteria)
|
||||
{
|
||||
|
|
@ -38,7 +38,7 @@ class DProduct extends DatabaseAbstract
|
|||
return null;
|
||||
}
|
||||
|
||||
return MathTrig::PRODUCT(
|
||||
return MathTrig\Product::evaluate(
|
||||
self::getFilteredColumn($database, $field, $criteria)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class DStDev extends DatabaseAbstract
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float|string
|
||||
* @return null|float|string
|
||||
*/
|
||||
public static function evaluate($database, $field, $criteria)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class DStDevP extends DatabaseAbstract
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float|string
|
||||
* @return null|float|string
|
||||
*/
|
||||
public static function evaluate($database, $field, $criteria)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ class DSum extends DatabaseAbstract
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float
|
||||
* @return null|float|string
|
||||
*/
|
||||
public static function evaluate($database, $field, $criteria)
|
||||
{
|
||||
|
|
@ -38,7 +38,7 @@ class DSum extends DatabaseAbstract
|
|||
return null;
|
||||
}
|
||||
|
||||
return MathTrig::SUM(
|
||||
return MathTrig\Sum::funcSum(
|
||||
self::getFilteredColumn($database, $field, $criteria)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class DVar extends DatabaseAbstract
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float|string (string if result is an error)
|
||||
* @return null|float|string (string if result is an error)
|
||||
*/
|
||||
public static function evaluate($database, $field, $criteria)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class DVarP extends DatabaseAbstract
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return float|string (string if result is an error)
|
||||
* @return null|float|string (string if result is an error)
|
||||
*/
|
||||
public static function evaluate($database, $field, $criteria)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ abstract class DatabaseAbstract
|
|||
* the column label in which you specify a condition for the
|
||||
* column.
|
||||
*
|
||||
* @return array of mixed
|
||||
* @return mixed[]
|
||||
*/
|
||||
protected static function filter(array $database, array $criteria): array
|
||||
{
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
class Constants
|
||||
{
|
||||
// Constants currently used by WeekNum; will eventually be used by WEEKDAY
|
||||
const STARTWEEK_SUNDAY = 1;
|
||||
const STARTWEEK_MONDAY = 2;
|
||||
const STARTWEEK_MONDAY_ALT = 11;
|
||||
const STARTWEEK_TUESDAY = 12;
|
||||
const STARTWEEK_WEDNESDAY = 13;
|
||||
const STARTWEEK_THURSDAY = 14;
|
||||
const STARTWEEK_FRIDAY = 15;
|
||||
const STARTWEEK_SATURDAY = 16;
|
||||
const STARTWEEK_SUNDAY_ALT = 17;
|
||||
const DOW_SUNDAY = 1;
|
||||
const DOW_MONDAY = 2;
|
||||
const DOW_TUESDAY = 3;
|
||||
const DOW_WEDNESDAY = 4;
|
||||
const DOW_THURSDAY = 5;
|
||||
const DOW_FRIDAY = 6;
|
||||
const DOW_SATURDAY = 7;
|
||||
const STARTWEEK_MONDAY_ISO = 21;
|
||||
const METHODARR = [
|
||||
self::STARTWEEK_SUNDAY => self::DOW_SUNDAY,
|
||||
self::DOW_MONDAY,
|
||||
self::STARTWEEK_MONDAY_ALT => self::DOW_MONDAY,
|
||||
self::DOW_TUESDAY,
|
||||
self::DOW_WEDNESDAY,
|
||||
self::DOW_THURSDAY,
|
||||
self::DOW_FRIDAY,
|
||||
self::DOW_SATURDAY,
|
||||
self::DOW_SUNDAY,
|
||||
self::STARTWEEK_MONDAY_ISO => self::STARTWEEK_MONDAY_ISO,
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class DateDif
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($startDate, $endDate, $unit = 'D')
|
||||
{
|
||||
try {
|
||||
$startDate = Helpers::getDateValue($startDate);
|
||||
$endDate = Helpers::getDateValue($endDate);
|
||||
$difference = self::initialDiff($startDate, $endDate);
|
||||
$unit = strtoupper(Functions::flattenSingleValue($unit));
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$PHPStartDateObject = Date::excelToDateTimeObject($startDate);
|
||||
$startDays = (int) $PHPStartDateObject->format('j');
|
||||
$startMonths = (int) $PHPStartDateObject->format('n');
|
||||
$startYears = (int) $PHPStartDateObject->format('Y');
|
||||
|
||||
$PHPEndDateObject = Date::excelToDateTimeObject($endDate);
|
||||
$endDays = (int) $PHPEndDateObject->format('j');
|
||||
$endMonths = (int) $PHPEndDateObject->format('n');
|
||||
$endYears = (int) $PHPEndDateObject->format('Y');
|
||||
|
||||
$PHPDiffDateObject = $PHPEndDateObject->diff($PHPStartDateObject);
|
||||
|
||||
$retVal = false;
|
||||
$retVal = self::replaceRetValue($retVal, $unit, 'D') ?? self::datedifD($difference);
|
||||
$retVal = self::replaceRetValue($retVal, $unit, 'M') ?? self::datedifM($PHPDiffDateObject);
|
||||
$retVal = self::replaceRetValue($retVal, $unit, 'MD') ?? self::datedifMD($startDays, $endDays, $PHPEndDateObject, $PHPDiffDateObject);
|
||||
$retVal = self::replaceRetValue($retVal, $unit, 'Y') ?? self::datedifY($PHPDiffDateObject);
|
||||
$retVal = self::replaceRetValue($retVal, $unit, 'YD') ?? self::datedifYD($difference, $startYears, $endYears, $PHPStartDateObject, $PHPEndDateObject);
|
||||
$retVal = self::replaceRetValue($retVal, $unit, 'YM') ?? self::datedifYM($PHPDiffDateObject);
|
||||
|
||||
return is_bool($retVal) ? Functions::VALUE() : $retVal;
|
||||
}
|
||||
|
||||
private static function initialDiff(float $startDate, float $endDate): float
|
||||
{
|
||||
// Validate parameters
|
||||
if ($startDate > $endDate) {
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
|
||||
return $endDate - $startDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide whether it's time to set retVal.
|
||||
*
|
||||
* @param bool|int $retVal
|
||||
*
|
||||
* @return null|bool|int
|
||||
*/
|
||||
private static function replaceRetValue($retVal, string $unit, string $compare)
|
||||
{
|
||||
if ($retVal !== false || $unit !== $compare) {
|
||||
return $retVal;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function datedifD(float $difference): int
|
||||
{
|
||||
return (int) $difference;
|
||||
}
|
||||
|
||||
private static function datedifM(DateInterval $PHPDiffDateObject): int
|
||||
{
|
||||
return 12 * (int) $PHPDiffDateObject->format('%y') + (int) $PHPDiffDateObject->format('%m');
|
||||
}
|
||||
|
||||
private static function datedifMD(int $startDays, int $endDays, DateTime $PHPEndDateObject, DateInterval $PHPDiffDateObject): int
|
||||
{
|
||||
if ($endDays < $startDays) {
|
||||
$retVal = $endDays;
|
||||
$PHPEndDateObject->modify('-' . $endDays . ' days');
|
||||
$adjustDays = (int) $PHPEndDateObject->format('j');
|
||||
$retVal += ($adjustDays - $startDays);
|
||||
} else {
|
||||
$retVal = (int) $PHPDiffDateObject->format('%d');
|
||||
}
|
||||
|
||||
return $retVal;
|
||||
}
|
||||
|
||||
private static function datedifY(DateInterval $PHPDiffDateObject): int
|
||||
{
|
||||
return (int) $PHPDiffDateObject->format('%y');
|
||||
}
|
||||
|
||||
private static function datedifYD(float $difference, int $startYears, int $endYears, DateTime $PHPStartDateObject, DateTime $PHPEndDateObject): int
|
||||
{
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
return (int) $retVal;
|
||||
}
|
||||
|
||||
private static function datedifYM(DateInterval $PHPDiffDateObject): int
|
||||
{
|
||||
return (int) $PHPDiffDateObject->format('%m');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,151 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class DateValue
|
||||
{
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @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 evaluate($dateValue)
|
||||
{
|
||||
$dti = new DateTimeImmutable();
|
||||
$baseYear = Date::getExcelCalendar();
|
||||
$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);
|
||||
$t = '';
|
||||
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) {
|
||||
// We've been fed a time value without any date
|
||||
return ((strpos((string) $t, ':') === false)) ? Functions::Value() : 0.0;
|
||||
}
|
||||
unset($t);
|
||||
|
||||
$dateValue = self::t1ToString($t1, $dti, $yearFound);
|
||||
|
||||
$PHPDateArray = self::setUpArray($dateValue, $dti);
|
||||
|
||||
return self::finalResults($PHPDateArray, $dti, $baseYear);
|
||||
}
|
||||
|
||||
private static function t1ToString(array $t1, DateTimeImmutable $dti, bool $yearFound): string
|
||||
{
|
||||
if (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 (is_numeric($t1[1]) && $t1[1] > 29) {
|
||||
$t1[1] += 1900;
|
||||
array_unshift($t1, 1);
|
||||
} else {
|
||||
$t1[] = $dti->format('Y');
|
||||
}
|
||||
}
|
||||
}
|
||||
$dateValue = implode(' ', $t1);
|
||||
|
||||
return $dateValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse date.
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
private static function setUpArray(string $dateValue, DateTimeImmutable $dti)
|
||||
{
|
||||
$PHPDateArray = date_parse($dateValue);
|
||||
if (($PHPDateArray === false) || ($PHPDateArray['error_count'] > 0)) {
|
||||
// If original count was 1, we've already returned.
|
||||
// If it was 2, we added another.
|
||||
// Therefore, neither of the first 2 stroks below can fail.
|
||||
$testVal1 = strtok($dateValue, '- ');
|
||||
$testVal2 = strtok('- ');
|
||||
$testVal3 = strtok('- ') ?: $dti->format('Y');
|
||||
Helpers::adjustYear((string) $testVal1, (string) $testVal2, $testVal3);
|
||||
$PHPDateArray = date_parse($testVal1 . '-' . $testVal2 . '-' . $testVal3);
|
||||
if (($PHPDateArray === false) || ($PHPDateArray['error_count'] > 0)) {
|
||||
$PHPDateArray = date_parse($testVal2 . '-' . $testVal1 . '-' . $testVal3);
|
||||
}
|
||||
}
|
||||
|
||||
return $PHPDateArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Final results.
|
||||
*
|
||||
* @param array|bool $PHPDateArray
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
private static function finalResults($PHPDateArray, DateTimeImmutable $dti, int $baseYear)
|
||||
{
|
||||
$retValue = Functions::Value();
|
||||
if (is_array($PHPDateArray) && $PHPDateArray['error_count'] == 0) {
|
||||
// Execute function
|
||||
Helpers::replaceIfEmpty($PHPDateArray['year'], $dti->format('Y'));
|
||||
if ($PHPDateArray['year'] < $baseYear) {
|
||||
return Functions::VALUE();
|
||||
}
|
||||
Helpers::replaceIfEmpty($PHPDateArray['month'], $dti->format('m'));
|
||||
Helpers::replaceIfEmpty($PHPDateArray['day'], $dti->format('d'));
|
||||
$PHPDateArray['hour'] = 0;
|
||||
$PHPDateArray['minute'] = 0;
|
||||
$PHPDateArray['second'] = 0;
|
||||
$month = (int) $PHPDateArray['month'];
|
||||
$day = (int) $PHPDateArray['day'];
|
||||
$year = (int) $PHPDateArray['year'];
|
||||
if (!checkdate($month, $day, $year)) {
|
||||
return ($year === 1900 && $month === 2 && $day === 29) ? Helpers::returnIn3FormatsFloat(60.0) : Functions::VALUE();
|
||||
}
|
||||
$retValue = Helpers::returnIn3FormatsArray($PHPDateArray, true);
|
||||
}
|
||||
|
||||
return $retValue;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,168 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
||||
|
||||
class Datefunc
|
||||
{
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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 evaluate($year, $month, $day)
|
||||
{
|
||||
$baseYear = Date::getExcelCalendar();
|
||||
|
||||
try {
|
||||
$year = self::getYear($year, $baseYear);
|
||||
$month = self::getMonth($month);
|
||||
$day = self::getDay($day);
|
||||
self::adjustYearMonth($year, $month, $baseYear);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$excelDateValue = Date::formattedPHPToExcel($year, $month, $day);
|
||||
|
||||
return Helpers::returnIn3FormatsFloat($excelDateValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert year from multiple formats to int.
|
||||
*
|
||||
* @param mixed $year
|
||||
*/
|
||||
private static function getYear($year, int $baseYear): int
|
||||
{
|
||||
$year = Functions::flattenSingleValue($year);
|
||||
$year = ($year !== null) ? StringHelper::testStringAsNumeric((string) $year) : 0;
|
||||
if (!is_numeric($year)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
$year = (int) $year;
|
||||
|
||||
if ($year < ($baseYear - 1900)) {
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
if ((($baseYear - 1900) !== 0) && ($year < $baseYear) && ($year >= 1900)) {
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
|
||||
if (($year < $baseYear) && ($year >= ($baseYear - 1900))) {
|
||||
$year += 1900;
|
||||
}
|
||||
|
||||
return $year;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert month from multiple formats to int.
|
||||
*
|
||||
* @param mixed $month
|
||||
*/
|
||||
private static function getMonth($month): int
|
||||
{
|
||||
$month = Functions::flattenSingleValue($month);
|
||||
|
||||
if (($month !== null) && (!is_numeric($month))) {
|
||||
$month = Date::monthStringToNumber($month);
|
||||
}
|
||||
|
||||
$month = ($month !== null) ? StringHelper::testStringAsNumeric((string) $month) : 0;
|
||||
if (!is_numeric($month)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
|
||||
return (int) $month;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert day from multiple formats to int.
|
||||
*
|
||||
* @param mixed $day
|
||||
*/
|
||||
private static function getDay($day): int
|
||||
{
|
||||
$day = Functions::flattenSingleValue($day);
|
||||
|
||||
if (($day !== null) && (!is_numeric($day))) {
|
||||
$day = Date::dayStringToNumber($day);
|
||||
}
|
||||
|
||||
$day = ($day !== null) ? StringHelper::testStringAsNumeric((string) $day) : 0;
|
||||
if (!is_numeric($day)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
|
||||
return (int) $day;
|
||||
}
|
||||
|
||||
private static function adjustYearMonth(int &$year, int &$month, int $baseYear): void
|
||||
{
|
||||
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)) {
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Day
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($dateValue)
|
||||
{
|
||||
$weirdResult = self::weirdCondition($dateValue);
|
||||
if ($weirdResult >= 0) {
|
||||
return $weirdResult;
|
||||
}
|
||||
|
||||
try {
|
||||
$dateValue = Helpers::getDateValue($dateValue);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
|
||||
|
||||
return (int) $PHPDateObject->format('j');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
|
||||
* PHP DateTime object, or a standard date string
|
||||
*/
|
||||
private static function weirdCondition($dateValue): int
|
||||
{
|
||||
// Excel does not treat 0 consistently for DAY vs. (MONTH or YEAR)
|
||||
if (Date::getExcelCalendar() === DATE::CALENDAR_WINDOWS_1900 && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
|
||||
if (is_bool($dateValue)) {
|
||||
return (int) $dateValue;
|
||||
}
|
||||
if ($dateValue === null) {
|
||||
return 0;
|
||||
}
|
||||
if (is_numeric($dateValue) && $dateValue < 1 && $dateValue >= 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Days
|
||||
{
|
||||
/**
|
||||
* DAYS.
|
||||
*
|
||||
* Returns the number of days between two dates
|
||||
*
|
||||
* Excel Function:
|
||||
* DAYS(endDate, startDate)
|
||||
*
|
||||
* @param DateTimeInterface|float|int|string $endDate Excel date serial value (float),
|
||||
* PHP date timestamp (integer), PHP DateTime object, or a standard date string
|
||||
* @param DateTimeInterface|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 evaluate($endDate, $startDate)
|
||||
{
|
||||
try {
|
||||
$startDate = Helpers::getDateValue($startDate);
|
||||
$endDate = Helpers::getDateValue($endDate);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$PHPStartDateObject = Date::excelToDateTimeObject($startDate);
|
||||
$PHPEndDateObject = Date::excelToDateTimeObject($endDate);
|
||||
|
||||
$days = Functions::VALUE();
|
||||
$diff = $PHPStartDateObject->diff($PHPEndDateObject);
|
||||
if ($diff !== false && !is_bool($diff->days)) {
|
||||
$days = $diff->days;
|
||||
if ($diff->invert) {
|
||||
$days = -$days;
|
||||
}
|
||||
}
|
||||
|
||||
return $days;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Days360
|
||||
{
|
||||
/**
|
||||
* 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])
|
||||
*
|
||||
* @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 mixed $method US or European Method as a bool
|
||||
* 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 evaluate($startDate = 0, $endDate = 0, $method = false)
|
||||
{
|
||||
try {
|
||||
$startDate = Helpers::getDateValue($startDate);
|
||||
$endDate = Helpers::getDateValue($endDate);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
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((int) $startDay, (int) $startMonth, (int) $startYear, (int) $endDay, (int) $endMonth, (int) $endYear, !$method);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of days between two dates based on a 360 day calendar.
|
||||
*/
|
||||
private static function dateDiff360(int $startDay, int $startMonth, int $startYear, int $endDay, int $endMonth, int $endYear, bool $methodUS): int
|
||||
{
|
||||
$startDay = self::getStartDay($startDay, $startMonth, $startYear, $methodUS);
|
||||
$endDay = self::getEndDay($endDay, $endMonth, $endYear, $startDay, $methodUS);
|
||||
|
||||
return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
|
||||
}
|
||||
|
||||
private static function getStartDay(int $startDay, int $startMonth, int $startYear, bool $methodUS): int
|
||||
{
|
||||
if ($startDay == 31) {
|
||||
--$startDay;
|
||||
} elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !Helpers::isLeapYear($startYear))))) {
|
||||
$startDay = 30;
|
||||
}
|
||||
|
||||
return $startDay;
|
||||
}
|
||||
|
||||
private static function getEndDay(int $endDay, int &$endMonth, int &$endYear, int $startDay, bool $methodUS): int
|
||||
{
|
||||
if ($endDay == 31) {
|
||||
if ($methodUS && $startDay != 30) {
|
||||
$endDay = 1;
|
||||
if ($endMonth == 12) {
|
||||
++$endYear;
|
||||
$endMonth = 1;
|
||||
} else {
|
||||
++$endMonth;
|
||||
}
|
||||
} else {
|
||||
$endDay = 30;
|
||||
}
|
||||
}
|
||||
|
||||
return $endDay;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class EDate
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($dateValue, $adjustmentMonths)
|
||||
{
|
||||
try {
|
||||
$dateValue = Helpers::getDateValue($dateValue, false);
|
||||
$adjustmentMonths = Helpers::validateNumericNull($adjustmentMonths);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
$adjustmentMonths = floor($adjustmentMonths);
|
||||
|
||||
// Execute function
|
||||
$PHPDateObject = Helpers::adjustDateByMonths($dateValue, $adjustmentMonths);
|
||||
|
||||
return Helpers::returnIn3FormatsObject($PHPDateObject);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class EoMonth
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($dateValue, $adjustmentMonths)
|
||||
{
|
||||
try {
|
||||
$dateValue = Helpers::getDateValue($dateValue, false);
|
||||
$adjustmentMonths = Helpers::validateNumericNull($adjustmentMonths);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
$adjustmentMonths = floor($adjustmentMonths);
|
||||
|
||||
// Execute function
|
||||
$PHPDateObject = Helpers::adjustDateByMonths($dateValue, $adjustmentMonths + 1);
|
||||
$adjustDays = (int) $PHPDateObject->format('d');
|
||||
$adjustDaysString = '-' . $adjustDays . ' days';
|
||||
$PHPDateObject->modify($adjustDaysString);
|
||||
|
||||
return Helpers::returnIn3FormatsObject($PHPDateObject);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,285 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use DateTime;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Helpers
|
||||
{
|
||||
/**
|
||||
* Identify if a year is a leap year or not.
|
||||
*
|
||||
* @param int|string $year The year to test
|
||||
*
|
||||
* @return bool TRUE if the year is a leap year, otherwise FALSE
|
||||
*/
|
||||
public static function isLeapYear($year): bool
|
||||
{
|
||||
return (($year % 4) === 0) && (($year % 100) !== 0) || (($year % 400) === 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* getDateValue.
|
||||
*
|
||||
* @param mixed $dateValue
|
||||
*
|
||||
* @return float Excel date/time serial value
|
||||
*/
|
||||
public static function getDateValue($dateValue, bool $allowBool = true): float
|
||||
{
|
||||
if (is_object($dateValue)) {
|
||||
$retval = Date::PHPToExcel($dateValue);
|
||||
if (is_bool($retval)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
self::nullFalseTrueToNumber($dateValue, $allowBool);
|
||||
if (!is_numeric($dateValue)) {
|
||||
$saveReturnDateType = Functions::getReturnDateType();
|
||||
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
|
||||
$dateValue = DateValue::evaluate($dateValue);
|
||||
Functions::setReturnDateType($saveReturnDateType);
|
||||
if (!is_numeric($dateValue)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
}
|
||||
if ($dateValue < 0 && Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE) {
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
|
||||
return (float) $dateValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* getTimeValue.
|
||||
*
|
||||
* @param string $timeValue
|
||||
*
|
||||
* @return mixed Excel date/time serial value, or string if error
|
||||
*/
|
||||
public static function getTimeValue($timeValue)
|
||||
{
|
||||
$saveReturnDateType = Functions::getReturnDateType();
|
||||
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
|
||||
$timeValue = TimeValue::evaluate($timeValue);
|
||||
Functions::setReturnDateType($saveReturnDateType);
|
||||
|
||||
return $timeValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust date by given months.
|
||||
*
|
||||
* @param mixed $dateValue
|
||||
*/
|
||||
public static function adjustDateByMonths($dateValue = 0, float $adjustmentMonths = 0): DateTime
|
||||
{
|
||||
// Execute function
|
||||
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
|
||||
$oMonth = (int) $PHPDateObject->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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Help reduce perceived complexity of some tests.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param mixed $altValue
|
||||
*/
|
||||
public static function replaceIfEmpty(&$value, $altValue): void
|
||||
{
|
||||
$value = $value ?: $altValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust year in ambiguous situations.
|
||||
*/
|
||||
public static function adjustYear(string $testVal1, string $testVal2, string &$testVal3): void
|
||||
{
|
||||
if (!is_numeric($testVal1) || $testVal1 < 31) {
|
||||
if (!is_numeric($testVal2) || $testVal2 < 12) {
|
||||
if (is_numeric($testVal3) && $testVal3 < 12) {
|
||||
$testVal3 += 2000;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return result in one of three formats.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function returnIn3FormatsArray(array $dateArray, bool $noFrac = false)
|
||||
{
|
||||
$retType = Functions::getReturnDateType();
|
||||
if ($retType === Functions::RETURNDATE_PHP_DATETIME_OBJECT) {
|
||||
return new DateTime(
|
||||
$dateArray['year']
|
||||
. '-' . $dateArray['month']
|
||||
. '-' . $dateArray['day']
|
||||
. ' ' . $dateArray['hour']
|
||||
. ':' . $dateArray['minute']
|
||||
. ':' . $dateArray['second']
|
||||
);
|
||||
}
|
||||
$excelDateValue =
|
||||
Date::formattedPHPToExcel(
|
||||
$dateArray['year'],
|
||||
$dateArray['month'],
|
||||
$dateArray['day'],
|
||||
$dateArray['hour'],
|
||||
$dateArray['minute'],
|
||||
$dateArray['second']
|
||||
);
|
||||
if ($retType === Functions::RETURNDATE_EXCEL) {
|
||||
return $noFrac ? floor($excelDateValue) : (float) $excelDateValue;
|
||||
}
|
||||
// RETURNDATE_UNIX_TIMESTAMP)
|
||||
|
||||
return (int) Date::excelToTimestamp($excelDateValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return result in one of three formats.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function returnIn3FormatsFloat(float $excelDateValue)
|
||||
{
|
||||
$retType = Functions::getReturnDateType();
|
||||
if ($retType === Functions::RETURNDATE_EXCEL) {
|
||||
return $excelDateValue;
|
||||
}
|
||||
if ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
|
||||
return (int) Date::excelToTimestamp($excelDateValue);
|
||||
}
|
||||
// RETURNDATE_PHP_DATETIME_OBJECT
|
||||
|
||||
return Date::excelToDateTimeObject($excelDateValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return result in one of three formats.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function returnIn3FormatsObject(DateTime $PHPDateObject)
|
||||
{
|
||||
$retType = Functions::getReturnDateType();
|
||||
if ($retType === Functions::RETURNDATE_PHP_DATETIME_OBJECT) {
|
||||
return $PHPDateObject;
|
||||
}
|
||||
if ($retType === Functions::RETURNDATE_EXCEL) {
|
||||
return (float) Date::PHPToExcel($PHPDateObject);
|
||||
}
|
||||
// RETURNDATE_UNIX_TIMESTAMP
|
||||
$stamp = Date::PHPToExcel($PHPDateObject);
|
||||
$stamp = is_bool($stamp) ? ((int) $stamp) : $stamp;
|
||||
|
||||
return (int) Date::excelToTimestamp($stamp);
|
||||
}
|
||||
|
||||
private static function baseDate(): int
|
||||
{
|
||||
if (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE) {
|
||||
return 0;
|
||||
}
|
||||
if (Date::getExcelCalendar() === Date::CALENDAR_MAC_1904) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Many functions accept null/false/true argument treated as 0/0/1.
|
||||
*
|
||||
* @param mixed $number
|
||||
*/
|
||||
public static function nullFalseTrueToNumber(&$number, bool $allowBool = true): void
|
||||
{
|
||||
$number = Functions::flattenSingleValue($number);
|
||||
$nullVal = self::baseDate();
|
||||
if ($number === null) {
|
||||
$number = $nullVal;
|
||||
} elseif ($allowBool && is_bool($number)) {
|
||||
$number = $nullVal + (int) $number;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Many functions accept null argument treated as 0.
|
||||
*
|
||||
* @param mixed $number
|
||||
*
|
||||
* @return float|int
|
||||
*/
|
||||
public static function validateNumericNull($number)
|
||||
{
|
||||
$number = Functions::flattenSingleValue($number);
|
||||
if ($number === null) {
|
||||
return 0;
|
||||
}
|
||||
if (is_int($number)) {
|
||||
return $number;
|
||||
}
|
||||
if (is_numeric($number)) {
|
||||
return (float) $number;
|
||||
}
|
||||
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
|
||||
/**
|
||||
* Many functions accept null/false/true argument treated as 0/0/1.
|
||||
*
|
||||
* @param mixed $number
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public static function validateNotNegative($number)
|
||||
{
|
||||
if (!is_numeric($number)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
if ($number >= 0) {
|
||||
return (float) $number;
|
||||
}
|
||||
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
|
||||
public static function silly1900(DateTime $PHPDateObject, string $mod = '-1 day'): void
|
||||
{
|
||||
$isoDate = $PHPDateObject->format('c');
|
||||
if ($isoDate < '1900-03-01') {
|
||||
$PHPDateObject->modify($mod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Hour
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($timeValue)
|
||||
{
|
||||
try {
|
||||
$timeValue = Functions::flattenSingleValue($timeValue);
|
||||
Helpers::nullFalseTrueToNumber($timeValue);
|
||||
if (!is_numeric($timeValue)) {
|
||||
$timeValue = Helpers::getTimeValue($timeValue);
|
||||
}
|
||||
Helpers::validateNotNegative($timeValue);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$timeValue = fmod($timeValue, 1);
|
||||
$timeValue = Date::excelToDateTimeObject($timeValue);
|
||||
|
||||
return (int) $timeValue->format('H');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class IsoWeekNum
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($dateValue)
|
||||
{
|
||||
if (self::apparentBug($dateValue)) {
|
||||
return 52;
|
||||
}
|
||||
|
||||
try {
|
||||
$dateValue = Helpers::getDateValue($dateValue);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
|
||||
Helpers::silly1900($PHPDateObject);
|
||||
|
||||
return (int) $PHPDateObject->format('W');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
|
||||
* PHP DateTime object, or a standard date string
|
||||
*/
|
||||
private static function apparentBug($dateValue): bool
|
||||
{
|
||||
if (Date::getExcelCalendar() !== DATE::CALENDAR_MAC_1904) {
|
||||
if (is_bool($dateValue)) {
|
||||
return true;
|
||||
}
|
||||
if (is_numeric($dateValue) && !((int) $dateValue)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Minute
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($timeValue)
|
||||
{
|
||||
try {
|
||||
$timeValue = Functions::flattenSingleValue($timeValue);
|
||||
Helpers::nullFalseTrueToNumber($timeValue);
|
||||
if (!is_numeric($timeValue)) {
|
||||
$timeValue = Helpers::getTimeValue($timeValue);
|
||||
}
|
||||
Helpers::validateNotNegative($timeValue);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$timeValue = fmod($timeValue, 1);
|
||||
$timeValue = Date::excelToDateTimeObject($timeValue);
|
||||
|
||||
return (int) $timeValue->format('i');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Month
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($dateValue)
|
||||
{
|
||||
try {
|
||||
$dateValue = Helpers::getDateValue($dateValue);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
if ($dateValue < 1 && Date::getExcelCalendar() === DATE::CALENDAR_WINDOWS_1900) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
|
||||
|
||||
return (int) $PHPDateObject->format('n');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class NetworkDays
|
||||
{
|
||||
/**
|
||||
* 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[,...]]])
|
||||
*
|
||||
* @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 mixed $dateArgs
|
||||
*
|
||||
* @return int|string Interval between the dates
|
||||
*/
|
||||
public static function evaluate($startDate, $endDate, ...$dateArgs)
|
||||
{
|
||||
try {
|
||||
// Retrieve the mandatory start and end date that are referenced in the function definition
|
||||
$sDate = Helpers::getDateValue($startDate);
|
||||
$eDate = Helpers::getDateValue($endDate);
|
||||
$startDate = min($sDate, $eDate);
|
||||
$endDate = max($sDate, $eDate);
|
||||
// Get the optional days
|
||||
$dateArgs = Functions::flattenArray($dateArgs);
|
||||
// Test any extra holiday parameters
|
||||
$holidayArray = [];
|
||||
foreach ($dateArgs as $holidayDate) {
|
||||
$holidayArray[] = Helpers::getDateValue($holidayDate);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$startDow = self::calcStartDow($startDate);
|
||||
$endDow = self::calcEndDow($endDate);
|
||||
$wholeWeekDays = (int) floor(($endDate - $startDate) / 7) * 5;
|
||||
$partWeekDays = self::calcPartWeekDays($startDow, $endDow);
|
||||
|
||||
// Test any extra holiday parameters
|
||||
$holidayCountedArray = [];
|
||||
foreach ($holidayArray as $holidayDate) {
|
||||
if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
|
||||
if ((WeekDay::evaluate($holidayDate, 2) < 6) && (!in_array($holidayDate, $holidayCountedArray))) {
|
||||
--$partWeekDays;
|
||||
$holidayCountedArray[] = $holidayDate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return self::applySign($wholeWeekDays + $partWeekDays, $sDate, $eDate);
|
||||
}
|
||||
|
||||
private static function calcStartDow(float $startDate): int
|
||||
{
|
||||
$startDow = 6 - (int) WeekDay::evaluate($startDate, 2);
|
||||
if ($startDow < 0) {
|
||||
$startDow = 5;
|
||||
}
|
||||
|
||||
return $startDow;
|
||||
}
|
||||
|
||||
private static function calcEndDow(float $endDate): int
|
||||
{
|
||||
$endDow = (int) WeekDay::evaluate($endDate, 2);
|
||||
if ($endDow >= 6) {
|
||||
$endDow = 0;
|
||||
}
|
||||
|
||||
return $endDow;
|
||||
}
|
||||
|
||||
private static function calcPartWeekDays(int $startDow, int $endDow): int
|
||||
{
|
||||
$partWeekDays = $endDow + $startDow;
|
||||
if ($partWeekDays > 5) {
|
||||
$partWeekDays -= 5;
|
||||
}
|
||||
|
||||
return $partWeekDays;
|
||||
}
|
||||
|
||||
private static function applySign(int $result, float $sDate, float $eDate): int
|
||||
{
|
||||
return ($sDate > $eDate) ? -$result : $result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class Now
|
||||
{
|
||||
/**
|
||||
* 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()
|
||||
*
|
||||
* @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 evaluate()
|
||||
{
|
||||
$dti = new DateTimeImmutable();
|
||||
$dateArray = date_parse($dti->format('c'));
|
||||
|
||||
return is_array($dateArray) ? Helpers::returnIn3FormatsArray($dateArray) : Functions::VALUE();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Second
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($timeValue)
|
||||
{
|
||||
try {
|
||||
$timeValue = Functions::flattenSingleValue($timeValue);
|
||||
Helpers::nullFalseTrueToNumber($timeValue);
|
||||
if (!is_numeric($timeValue)) {
|
||||
$timeValue = Helpers::getTimeValue($timeValue);
|
||||
}
|
||||
Helpers::validateNotNegative($timeValue);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$timeValue = fmod($timeValue, 1);
|
||||
$timeValue = Date::excelToDateTimeObject($timeValue);
|
||||
|
||||
return (int) $timeValue->format('s');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use DateTime;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Time
|
||||
{
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param mixed $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 mixed $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 mixed $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 evaluate($hour, $minute, $second)
|
||||
{
|
||||
try {
|
||||
$hour = self::toIntWithNullBool($hour);
|
||||
$minute = self::toIntWithNullBool($minute);
|
||||
$second = self::toIntWithNullBool($second);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
self::adjustSecond($second, $minute);
|
||||
self::adjustMinute($minute, $hour);
|
||||
|
||||
if ($hour > 23) {
|
||||
$hour = $hour % 24;
|
||||
} elseif ($hour < 0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$retType = Functions::getReturnDateType();
|
||||
if ($retType === Functions::RETURNDATE_EXCEL) {
|
||||
$calendar = Date::getExcelCalendar();
|
||||
$date = (int) ($calendar !== Date::CALENDAR_WINDOWS_1900);
|
||||
|
||||
return (float) Date::formattedPHPToExcel($calendar, 1, $date, $hour, $minute, $second);
|
||||
}
|
||||
if ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
|
||||
return (int) Date::excelToTimestamp(Date::formattedPHPToExcel(1970, 1, 1, $hour, $minute, $second)); // -2147468400; // -2147472000 + 3600
|
||||
}
|
||||
// RETURNDATE_PHP_DATETIME_OBJECT
|
||||
// Hour has already been normalized (0-23) above
|
||||
$phpDateObject = new DateTime('1900-01-01 ' . $hour . ':' . $minute . ':' . $second);
|
||||
|
||||
return $phpDateObject;
|
||||
}
|
||||
|
||||
private static function adjustSecond(int &$second, int &$minute): void
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private static function adjustMinute(int &$minute, int &$hour): void
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value expect int
|
||||
*/
|
||||
private static function toIntWithNullBool($value): int
|
||||
{
|
||||
$value = Functions::flattenSingleValue($value);
|
||||
$value = $value ?? 0;
|
||||
if (is_bool($value)) {
|
||||
$value = (int) $value;
|
||||
}
|
||||
if (!is_numeric($value)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
|
||||
return (int) $value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Datetime;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class TimeValue
|
||||
{
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @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 evaluate($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);
|
||||
$retValue = Functions::VALUE();
|
||||
if (($PHPDateArray !== false) && ($PHPDateArray['error_count'] == 0)) {
|
||||
// OpenOffice-specific code removed - it works just like Excel
|
||||
$excelDateValue = Date::formattedPHPToExcel(1900, 1, 1, $PHPDateArray['hour'], $PHPDateArray['minute'], $PHPDateArray['second']) - 1;
|
||||
|
||||
$retType = Functions::getReturnDateType();
|
||||
if ($retType === Functions::RETURNDATE_EXCEL) {
|
||||
$retValue = (float) $excelDateValue;
|
||||
} elseif ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
|
||||
$retValue = (int) $phpDateValue = Date::excelToTimestamp($excelDateValue + 25569) - 3600;
|
||||
} else {
|
||||
$retValue = new DateTime('1900-01-01 ' . $PHPDateArray['hour'] . ':' . $PHPDateArray['minute'] . ':' . $PHPDateArray['second']);
|
||||
}
|
||||
}
|
||||
|
||||
return $retValue;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class Today
|
||||
{
|
||||
/**
|
||||
* 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()
|
||||
*
|
||||
* @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 evaluate()
|
||||
{
|
||||
$dti = new DateTimeImmutable();
|
||||
$dateArray = date_parse($dti->format('c'));
|
||||
|
||||
return is_array($dateArray) ? Helpers::returnIn3FormatsArray($dateArray, true) : Functions::VALUE();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class WeekDay
|
||||
{
|
||||
/**
|
||||
* 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 null|float|int|string $dateValue Excel date serial value (float), PHP date timestamp (integer),
|
||||
* PHP DateTime object, or a standard date string
|
||||
* @param mixed $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 evaluate($dateValue, $style = 1)
|
||||
{
|
||||
try {
|
||||
$dateValue = Helpers::getDateValue($dateValue);
|
||||
$style = self::validateStyle($style);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
|
||||
Helpers::silly1900($PHPDateObject);
|
||||
$DoW = (int) $PHPDateObject->format('w');
|
||||
|
||||
switch ($style) {
|
||||
case 1:
|
||||
++$DoW;
|
||||
|
||||
break;
|
||||
case 2:
|
||||
$DoW = self::dow0Becomes7($DoW);
|
||||
|
||||
break;
|
||||
case 3:
|
||||
$DoW = self::dow0Becomes7($DoW) - 1;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return $DoW;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $style expect int
|
||||
*/
|
||||
private static function validateStyle($style): int
|
||||
{
|
||||
$style = Functions::flattenSingleValue($style);
|
||||
|
||||
if (!is_numeric($style)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
$style = (int) $style;
|
||||
if (($style < 1) || ($style > 3)) {
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
|
||||
return $style;
|
||||
}
|
||||
|
||||
private static function dow0Becomes7(int $DoW): int
|
||||
{
|
||||
return ($DoW === 0) ? 7 : $DoW;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class WeekNum
|
||||
{
|
||||
/**
|
||||
* 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.
|
||||
* 11 Week begins on Monday.
|
||||
* 12 Week begins on Tuesday.
|
||||
* 13 Week begins on Wednesday.
|
||||
* 14 Week begins on Thursday.
|
||||
* 15 Week begins on Friday.
|
||||
* 16 Week begins on Saturday.
|
||||
* 17 Week begins on Sunday.
|
||||
* 21 ISO (Jan. 4 is week 1, begins on Monday).
|
||||
*
|
||||
* @return int|string Week Number
|
||||
*/
|
||||
public static function evaluate($dateValue, $method = Constants::STARTWEEK_SUNDAY)
|
||||
{
|
||||
$origDateValueNull = empty($dateValue);
|
||||
|
||||
try {
|
||||
$method = self::validateMethod($method);
|
||||
if ($dateValue === null) { // boolean not allowed
|
||||
$dateValue = (Date::getExcelCalendar() === DATE::CALENDAR_MAC_1904 || $method === Constants::DOW_SUNDAY) ? 0 : 1;
|
||||
}
|
||||
$dateValue = self::validateDateValue($dateValue);
|
||||
if (!$dateValue && self::buggyWeekNum1900($method)) {
|
||||
// This seems to be an additional Excel bug.
|
||||
return 0;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Execute function
|
||||
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
|
||||
if ($method == Constants::STARTWEEK_MONDAY_ISO) {
|
||||
Helpers::silly1900($PHPDateObject);
|
||||
|
||||
return (int) $PHPDateObject->format('W');
|
||||
}
|
||||
if (self::buggyWeekNum1904($method, $origDateValueNull, $PHPDateObject)) {
|
||||
return 0;
|
||||
}
|
||||
Helpers::silly1900($PHPDateObject, '+ 5 years'); // 1905 calendar matches
|
||||
$dayOfYear = (int) $PHPDateObject->format('z');
|
||||
$PHPDateObject->modify('-' . $dayOfYear . ' days');
|
||||
$firstDayOfFirstWeek = (int) $PHPDateObject->format('w');
|
||||
$daysInFirstWeek = (6 - $firstDayOfFirstWeek + $method) % 7;
|
||||
$daysInFirstWeek += 7 * !$daysInFirstWeek;
|
||||
$endFirstWeek = $daysInFirstWeek - 1;
|
||||
$weekOfYear = floor(($dayOfYear - $endFirstWeek + 13) / 7);
|
||||
|
||||
return (int) $weekOfYear;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate dateValue parameter.
|
||||
*
|
||||
* @param mixed $dateValue
|
||||
*/
|
||||
private static function validateDateValue($dateValue): float
|
||||
{
|
||||
if (is_bool($dateValue)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
|
||||
return Helpers::getDateValue($dateValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate method parameter.
|
||||
*
|
||||
* @param mixed $method
|
||||
*/
|
||||
private static function validateMethod($method): int
|
||||
{
|
||||
if ($method === null) {
|
||||
$method = Constants::STARTWEEK_SUNDAY;
|
||||
}
|
||||
$method = Functions::flattenSingleValue($method);
|
||||
if (!is_numeric($method)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
|
||||
$method = (int) $method;
|
||||
if (!array_key_exists($method, Constants::METHODARR)) {
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
$method = Constants::METHODARR[$method];
|
||||
|
||||
return $method;
|
||||
}
|
||||
|
||||
private static function buggyWeekNum1900(int $method): bool
|
||||
{
|
||||
return $method === Constants::DOW_SUNDAY && Date::getExcelCalendar() === Date::CALENDAR_WINDOWS_1900;
|
||||
}
|
||||
|
||||
private static function buggyWeekNum1904(int $method, bool $origNull, DateTime $dateObject): bool
|
||||
{
|
||||
// This appears to be another Excel bug.
|
||||
|
||||
return $method === Constants::DOW_SUNDAY && Date::getExcelCalendar() === Date::CALENDAR_MAC_1904 && !$origNull && $dateObject->format('Y-m-d') === '1904-01-01';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,190 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class WorkDay
|
||||
{
|
||||
/**
|
||||
* 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[,...]]])
|
||||
*
|
||||
* @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.
|
||||
* @param mixed $dateArgs
|
||||
*
|
||||
* @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 evaluate($startDate, $endDays, ...$dateArgs)
|
||||
{
|
||||
// Retrieve the mandatory start date and days that are referenced in the function definition
|
||||
try {
|
||||
$startDate = Helpers::getDateValue($startDate);
|
||||
$endDays = Helpers::validateNumericNull($endDays);
|
||||
$dateArgs = Functions::flattenArray($dateArgs);
|
||||
$holidayArray = [];
|
||||
foreach ($dateArgs as $holidayDate) {
|
||||
$holidayArray[] = Helpers::getDateValue($holidayDate);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
$startDate = (float) floor($startDate);
|
||||
$endDays = (int) floor($endDays);
|
||||
// If endDays is 0, we always return startDate
|
||||
if ($endDays == 0) {
|
||||
return $startDate;
|
||||
}
|
||||
if ($endDays < 0) {
|
||||
return self::decrementing($startDate, $endDays, $holidayArray);
|
||||
}
|
||||
|
||||
return self::incrementing($startDate, $endDays, $holidayArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use incrementing logic to determine Workday.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private static function incrementing(float $startDate, int $endDays, array $holidayArray)
|
||||
{
|
||||
// Adjust the start date if it falls over a weekend
|
||||
|
||||
$startDoW = self::getWeekDay($startDate, 3);
|
||||
if (self::getWeekDay($startDate, 3) >= 5) {
|
||||
$startDate += 7 - $startDoW;
|
||||
--$endDays;
|
||||
}
|
||||
|
||||
// Add endDays
|
||||
$endDate = (float) $startDate + ((int) ($endDays / 5) * 7);
|
||||
$endDays = $endDays % 5;
|
||||
while ($endDays > 0) {
|
||||
++$endDate;
|
||||
// Adjust the calculated end date if it falls over a weekend
|
||||
$endDow = self::getWeekDay($endDate, 3);
|
||||
if ($endDow >= 5) {
|
||||
$endDate += 7 - $endDow;
|
||||
}
|
||||
--$endDays;
|
||||
}
|
||||
|
||||
// Test any extra holiday parameters
|
||||
if (!empty($holidayArray)) {
|
||||
$endDate = self::incrementingArray($startDate, $endDate, $holidayArray);
|
||||
}
|
||||
|
||||
return Helpers::returnIn3FormatsFloat($endDate);
|
||||
}
|
||||
|
||||
private static function incrementingArray(float $startDate, float $endDate, array $holidayArray): float
|
||||
{
|
||||
$holidayCountedArray = $holidayDates = [];
|
||||
foreach ($holidayArray as $holidayDate) {
|
||||
if (self::getWeekDay($holidayDate, 3) < 5) {
|
||||
$holidayDates[] = $holidayDate;
|
||||
}
|
||||
}
|
||||
sort($holidayDates, SORT_NUMERIC);
|
||||
foreach ($holidayDates as $holidayDate) {
|
||||
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::getWeekDay($endDate, 3);
|
||||
if ($endDoW >= 5) {
|
||||
$endDate += 7 - $endDoW;
|
||||
}
|
||||
}
|
||||
|
||||
return $endDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use decrementing logic to determine Workday.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private static function decrementing(float $startDate, int $endDays, array $holidayArray)
|
||||
{
|
||||
// Adjust the start date if it falls over a weekend
|
||||
|
||||
$startDoW = self::getWeekDay($startDate, 3);
|
||||
if (self::getWeekDay($startDate, 3) >= 5) {
|
||||
$startDate += -$startDoW + 4;
|
||||
++$endDays;
|
||||
}
|
||||
|
||||
// Add endDays
|
||||
$endDate = (float) $startDate + ((int) ($endDays / 5) * 7);
|
||||
$endDays = $endDays % 5;
|
||||
while ($endDays < 0) {
|
||||
--$endDate;
|
||||
// Adjust the calculated end date if it falls over a weekend
|
||||
$endDow = self::getWeekDay($endDate, 3);
|
||||
if ($endDow >= 5) {
|
||||
$endDate += 4 - $endDow;
|
||||
}
|
||||
++$endDays;
|
||||
}
|
||||
|
||||
// Test any extra holiday parameters
|
||||
if (!empty($holidayArray)) {
|
||||
$endDate = self::decrementingArray($startDate, $endDate, $holidayArray);
|
||||
}
|
||||
|
||||
return Helpers::returnIn3FormatsFloat($endDate);
|
||||
}
|
||||
|
||||
private static function decrementingArray(float $startDate, float $endDate, array $holidayArray): float
|
||||
{
|
||||
$holidayCountedArray = $holidayDates = [];
|
||||
foreach ($holidayArray as $holidayDate) {
|
||||
if (self::getWeekDay($holidayDate, 3) < 5) {
|
||||
$holidayDates[] = $holidayDate;
|
||||
}
|
||||
}
|
||||
rsort($holidayDates, SORT_NUMERIC);
|
||||
foreach ($holidayDates as $holidayDate) {
|
||||
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::getWeekDay($endDate, 3);
|
||||
if ($endDoW >= 5) {
|
||||
$endDate += -$endDoW + 4;
|
||||
}
|
||||
}
|
||||
|
||||
return $endDate;
|
||||
}
|
||||
|
||||
private static function getWeekDay(float $date, int $wd): int
|
||||
{
|
||||
$result = WeekDay::evaluate($date, $wd);
|
||||
|
||||
return is_string($result) ? -1 : $result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class Year
|
||||
{
|
||||
/**
|
||||
* 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 evaluate($dateValue)
|
||||
{
|
||||
try {
|
||||
$dateValue = Helpers::getDateValue($dateValue);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
if ($dateValue < 1 && Date::getExcelCalendar() === DATE::CALENDAR_WINDOWS_1900) {
|
||||
return 1900;
|
||||
}
|
||||
// Execute function
|
||||
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
|
||||
|
||||
return (int) $PHPDateObject->format('Y');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
class YearFrac
|
||||
{
|
||||
/**
|
||||
* 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])
|
||||
* See https://lists.oasis-open.org/archives/office-formula/200806/msg00039.html
|
||||
* for description of algorithm used in Excel
|
||||
*
|
||||
* @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|string fraction of the year, or a string containing an error
|
||||
*/
|
||||
public static function evaluate($startDate, $endDate, $method = 0)
|
||||
{
|
||||
try {
|
||||
$method = (int) Helpers::validateNumericNull($method);
|
||||
$sDate = Helpers::getDateValue($startDate);
|
||||
$eDate = Helpers::getDateValue($endDate);
|
||||
$sDate = self::excelBug($sDate, $startDate, $endDate, $method);
|
||||
$eDate = self::excelBug($eDate, $endDate, $startDate, $method);
|
||||
$startDate = min($sDate, $eDate);
|
||||
$endDate = max($sDate, $eDate);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
switch ($method) {
|
||||
case 0:
|
||||
return Days360::evaluate($startDate, $endDate) / 360;
|
||||
case 1:
|
||||
return self::method1($startDate, $endDate);
|
||||
case 2:
|
||||
return DateDif::evaluate($startDate, $endDate) / 360;
|
||||
case 3:
|
||||
return DateDif::evaluate($startDate, $endDate) / 365;
|
||||
case 4:
|
||||
return Days360::evaluate($startDate, $endDate, true) / 360;
|
||||
}
|
||||
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
/**
|
||||
* Excel 1900 calendar treats date argument of null as 1900-01-00. Really.
|
||||
*
|
||||
* @param mixed $startDate
|
||||
* @param mixed $endDate
|
||||
*/
|
||||
private static function excelBug(float $sDate, $startDate, $endDate, int $method): float
|
||||
{
|
||||
if (Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE && Date::getExcelCalendar() !== Date::CALENDAR_MAC_1904) {
|
||||
if ($endDate === null && $startDate !== null) {
|
||||
if (Month::evaluate($sDate) == 12 && Day::evaluate($sDate) === 31 && $method === 0) {
|
||||
$sDate += 2;
|
||||
} else {
|
||||
++$sDate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $sDate;
|
||||
}
|
||||
|
||||
private static function method1(float $startDate, float $endDate): float
|
||||
{
|
||||
$days = DateDif::evaluate($startDate, $endDate);
|
||||
$startYear = (int) Year::evaluate($startDate);
|
||||
$endYear = (int) Year::evaluate($endDate);
|
||||
$years = $endYear - $startYear + 1;
|
||||
$startMonth = (int) Month::evaluate($startDate);
|
||||
$startDay = (int) Day::evaluate($startDate);
|
||||
$endMonth = (int) Month::evaluate($endDate);
|
||||
$endDay = (int) Day::evaluate($endDate);
|
||||
$startMonthDay = 100 * $startMonth + $startDay;
|
||||
$endMonthDay = 100 * $endMonth + $endDay;
|
||||
if ($years == 1) {
|
||||
$tmpCalcAnnualBasis = 365 + (int) Helpers::isLeapYear($endYear);
|
||||
} elseif ($years == 2 && $startMonthDay >= $endMonthDay) {
|
||||
if (Helpers::isLeapYear($startYear)) {
|
||||
$tmpCalcAnnualBasis = 365 + (int) ($startMonthDay <= 229);
|
||||
} elseif (Helpers::isLeapYear($endYear)) {
|
||||
$tmpCalcAnnualBasis = 365 + (int) ($endMonthDay >= 229);
|
||||
} else {
|
||||
$tmpCalcAnnualBasis = 365;
|
||||
}
|
||||
} else {
|
||||
$tmpCalcAnnualBasis = 0;
|
||||
for ($year = $startYear; $year <= $endYear; ++$year) {
|
||||
$tmpCalcAnnualBasis += 365 + (int) Helpers::isLeapYear($year);
|
||||
}
|
||||
$tmpCalcAnnualBasis /= $years;
|
||||
}
|
||||
|
||||
return $days / $tmpCalcAnnualBasis;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,14 +3,21 @@
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation;
|
||||
|
||||
use Complex\Complex;
|
||||
use Complex\Exception as ComplexException;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ComplexFunctions;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ComplexOperations;
|
||||
|
||||
/**
|
||||
* @deprecated 1.18.0
|
||||
*/
|
||||
class Engineering
|
||||
{
|
||||
/**
|
||||
* EULER.
|
||||
*
|
||||
* @deprecated 1.18.0
|
||||
* @see Use Engineering\Constants\EULER instead
|
||||
*/
|
||||
const EULER = 2.71828182845904523536;
|
||||
public const EULER = 2.71828182845904523536;
|
||||
|
||||
/**
|
||||
* parseComplex.
|
||||
|
|
@ -149,7 +156,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toDecimal() method in the Engineering\ConvertBinary class instead
|
||||
*
|
||||
* @param string $x The binary number (as a string) that you want to convert. The number
|
||||
* @param mixed $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.
|
||||
|
|
@ -175,13 +182,13 @@ class Engineering
|
|||
*
|
||||
* @see Use the toHex() method in the Engineering\ConvertBinary class instead
|
||||
*
|
||||
* @param string $x The binary number (as a string) that you want to convert. The number
|
||||
* @param mixed $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
|
||||
* @param mixed $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.
|
||||
|
|
@ -207,13 +214,13 @@ class Engineering
|
|||
*
|
||||
* @see Use the toOctal() method in the Engineering\ConvertBinary class instead
|
||||
*
|
||||
* @param string $x The binary number (as a string) that you want to convert. The number
|
||||
* @param mixed $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
|
||||
* @param mixed $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.
|
||||
|
|
@ -239,7 +246,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toBinary() method in the Engineering\ConvertDecimal class instead
|
||||
*
|
||||
* @param string $x The decimal integer you want to convert. If number is negative,
|
||||
* @param mixed $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
|
||||
|
|
@ -249,7 +256,7 @@ class Engineering
|
|||
* 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
|
||||
* @param mixed $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.
|
||||
|
|
@ -275,7 +282,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toHex() method in the Engineering\ConvertDecimal class instead
|
||||
*
|
||||
* @param string $x The decimal integer you want to convert. If number is negative,
|
||||
* @param mixed $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
|
||||
|
|
@ -285,7 +292,7 @@ class Engineering
|
|||
* 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
|
||||
* @param mixed $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.
|
||||
|
|
@ -311,7 +318,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toOctal() method in the Engineering\ConvertDecimal class instead
|
||||
*
|
||||
* @param string $x The decimal integer you want to convert. If number is negative,
|
||||
* @param mixed $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
|
||||
|
|
@ -321,7 +328,7 @@ class Engineering
|
|||
* 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
|
||||
* @param mixed $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.
|
||||
|
|
@ -347,7 +354,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toBinary() method in the Engineering\ConvertHex class instead
|
||||
*
|
||||
* @param string $x the hexadecimal number you want to convert.
|
||||
* @param mixed $x the hexadecimal number (as a string) that 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.
|
||||
|
|
@ -357,7 +364,7 @@ class Engineering
|
|||
* 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,
|
||||
* @param mixed $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.
|
||||
|
|
@ -383,7 +390,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toDecimal() method in the Engineering\ConvertHex class instead
|
||||
*
|
||||
* @param string $x The hexadecimal number you want to convert. This number cannot
|
||||
* @param mixed $x The hexadecimal number (as a string) that 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
|
||||
|
|
@ -410,7 +417,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toOctal() method in the Engineering\ConvertHex class instead
|
||||
*
|
||||
* @param string $x The hexadecimal number you want to convert. Number cannot
|
||||
* @param mixed $x The hexadecimal number (as a string) that 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
|
||||
|
|
@ -423,7 +430,7 @@ class Engineering
|
|||
* 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
|
||||
* @param mixed $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.
|
||||
|
|
@ -450,7 +457,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toBinary() method in the Engineering\ConvertOctal class instead
|
||||
*
|
||||
* @param string $x The octal number you want to convert. Number may not
|
||||
* @param mixed $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
|
||||
|
|
@ -463,7 +470,7 @@ class Engineering
|
|||
* 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,
|
||||
* @param mixed $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).
|
||||
|
|
@ -492,7 +499,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toDecimal() method in the Engineering\ConvertOctal class instead
|
||||
*
|
||||
* @param string $x The octal number you want to convert. Number may not contain
|
||||
* @param mixed $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
|
||||
|
|
@ -519,7 +526,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the toHex() method in the Engineering\ConvertOctal class instead
|
||||
*
|
||||
* @param string $x The octal number you want to convert. Number may not contain
|
||||
* @param mixed $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
|
||||
|
|
@ -530,7 +537,7 @@ class Engineering
|
|||
* #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
|
||||
* @param mixed $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.
|
||||
|
|
@ -552,6 +559,10 @@ class Engineering
|
|||
* Excel Function:
|
||||
* COMPLEX(realNumber,imaginary[,suffix])
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the COMPLEX() method in the Engineering\Complex class instead
|
||||
*
|
||||
* @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.
|
||||
|
|
@ -561,20 +572,7 @@ class Engineering
|
|||
*/
|
||||
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();
|
||||
return Engineering\Complex::COMPLEX($realNumber, $imaginary, $suffix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -585,16 +583,18 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMAGINARY(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMAGINARY() method in the Engineering\Complex class instead
|
||||
*
|
||||
* @param string $complexNumber the complex number for which you want the imaginary
|
||||
* coefficient
|
||||
*
|
||||
* @return float
|
||||
* @return float|string
|
||||
*/
|
||||
public static function IMAGINARY($complexNumber)
|
||||
{
|
||||
$complexNumber = Functions::flattenSingleValue($complexNumber);
|
||||
|
||||
return (new Complex($complexNumber))->getImaginary();
|
||||
return Engineering\Complex::IMAGINARY($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -605,15 +605,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMREAL(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMREAL() method in the Engineering\Complex class instead
|
||||
*
|
||||
* @param string $complexNumber the complex number for which you want the real coefficient
|
||||
*
|
||||
* @return float
|
||||
* @return float|string
|
||||
*/
|
||||
public static function IMREAL($complexNumber)
|
||||
{
|
||||
$complexNumber = Functions::flattenSingleValue($complexNumber);
|
||||
|
||||
return (new Complex($complexNumber))->getReal();
|
||||
return Engineering\Complex::IMREAL($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -624,15 +626,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMABS(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMABS() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @param string $complexNumber the complex number for which you want the absolute value
|
||||
*
|
||||
* @return float
|
||||
* @return float|string
|
||||
*/
|
||||
public static function IMABS($complexNumber)
|
||||
{
|
||||
$complexNumber = Functions::flattenSingleValue($complexNumber);
|
||||
|
||||
return (new Complex($complexNumber))->abs();
|
||||
return ComplexFunctions::IMABS($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -644,20 +648,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMARGUMENT(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMARGUMENT() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMARGUMENT($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -668,15 +669,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMCONJUGATE(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMARGUMENT() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMCONJUGATE($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -687,15 +690,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMCOS(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMCOS() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMCOS($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -706,15 +711,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMCOSH(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMCOSH() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMCOSH($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -725,15 +732,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMCOT(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMCOT() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMCOT($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -744,15 +753,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMCSC(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMCSC() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMCSC($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -763,15 +774,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMCSCH(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMCSCH() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMCSCH($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -782,15 +795,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMSIN(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMSIN() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMSIN($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -801,15 +816,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMSINH(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMSINH() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMSINH($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -820,15 +837,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMSEC(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMSEC() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMSEC($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -839,15 +858,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMSECH(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMSECH() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMSECH($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -858,15 +879,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMTAN(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMTAN() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMTAN($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -877,20 +900,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMSQRT(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMSQRT() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMSQRT($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -901,20 +921,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMLN(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMLN() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMLN($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -925,20 +942,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMLOG10(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMLOG10() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMLOG10($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -949,20 +963,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMLOG2(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMLOG2() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMLOG2($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -973,15 +984,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMEXP(complexNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMEXP() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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();
|
||||
return ComplexFunctions::IMEXP($complexNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -992,6 +1005,10 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMPOWER(complexNumber,realNumber)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMPOWER() method in the Engineering\ComplexFunctions class instead
|
||||
*
|
||||
* @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
|
||||
*
|
||||
|
|
@ -999,14 +1016,7 @@ class Engineering
|
|||
*/
|
||||
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);
|
||||
return ComplexFunctions::IMPOWER($complexNumber, $realNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1017,6 +1027,10 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMDIV(complexDividend,complexDivisor)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMDIV() method in the Engineering\ComplexOperations class instead
|
||||
*
|
||||
* @param string $complexDividend the complex numerator or dividend
|
||||
* @param string $complexDivisor the complex denominator or divisor
|
||||
*
|
||||
|
|
@ -1024,14 +1038,7 @@ class Engineering
|
|||
*/
|
||||
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();
|
||||
}
|
||||
return ComplexOperations::IMDIV($complexDividend, $complexDivisor);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1042,6 +1049,10 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMSUB(complexNumber1,complexNumber2)
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMSUB() method in the Engineering\ComplexOperations class instead
|
||||
*
|
||||
* @param string $complexNumber1 the complex number from which to subtract complexNumber2
|
||||
* @param string $complexNumber2 the complex number to subtract from complexNumber1
|
||||
*
|
||||
|
|
@ -1049,14 +1060,7 @@ class Engineering
|
|||
*/
|
||||
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();
|
||||
}
|
||||
return ComplexOperations::IMSUB($complexNumber1, $complexNumber2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1067,26 +1071,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMSUM(complexNumber[,complexNumber[,...]])
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMSUM() method in the Engineering\ComplexOperations class instead
|
||||
*
|
||||
* @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;
|
||||
return ComplexOperations::IMSUM(...$complexNumbers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1097,26 +1092,17 @@ class Engineering
|
|||
* Excel Function:
|
||||
* IMPRODUCT(complexNumber[,complexNumber[,...]])
|
||||
*
|
||||
* @Deprecated 1.18.0
|
||||
*
|
||||
* @see Use the IMPRODUCT() method in the Engineering\ComplexOperations class instead
|
||||
*
|
||||
* @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;
|
||||
return ComplexOperations::IMPRODUCT(...$complexNumbers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1409,7 +1395,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the getConversionMultipliers() method in the ConvertUOM class instead
|
||||
*
|
||||
* @return array of mixed
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function getConversionMultipliers()
|
||||
{
|
||||
|
|
@ -1426,7 +1412,7 @@ class Engineering
|
|||
*
|
||||
* @see Use the getBinaryConversionMultipliers() method in the ConvertUOM class instead
|
||||
*
|
||||
* @return array of mixed
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function getBinaryConversionMultipliers()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
|
||||
|
||||
class BesselI
|
||||
{
|
||||
|
|
@ -16,9 +16,12 @@ class BesselI
|
|||
* Excel Function:
|
||||
* BESSELI(x,ord)
|
||||
*
|
||||
* @param float $x The value at which to evaluate the function.
|
||||
* NOTE: The MS Excel implementation of the BESSELI function is still not accurate.
|
||||
* This code provides a more accurate calculation
|
||||
*
|
||||
* @param mixed $x A float 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.
|
||||
* @param mixed $ord The integer 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.
|
||||
|
|
@ -27,11 +30,16 @@ class BesselI
|
|||
*/
|
||||
public static function BESSELI($x, $ord)
|
||||
{
|
||||
$x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
|
||||
$ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
|
||||
$x = Functions::flattenSingleValue($x);
|
||||
$ord = Functions::flattenSingleValue($ord);
|
||||
|
||||
try {
|
||||
$x = EngineeringValidations::validateFloat($x);
|
||||
$ord = EngineeringValidations::validateInt($ord);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
if ((is_numeric($x)) && (is_numeric($ord))) {
|
||||
$ord = (int) floor($ord);
|
||||
if ($ord < 0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
|
@ -41,32 +49,89 @@ class BesselI
|
|||
return (is_nan($fResult)) ? Functions::NAN() : $fResult;
|
||||
}
|
||||
|
||||
return Functions::VALUE();
|
||||
}
|
||||
|
||||
private static function calculate(float $x, int $ord): float
|
||||
{
|
||||
if (abs($x) <= 30) {
|
||||
$fResult = $fTerm = ($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));
|
||||
|
||||
return $fResult;
|
||||
// special cases
|
||||
switch ($ord) {
|
||||
case 0:
|
||||
return self::besselI0($x);
|
||||
case 1:
|
||||
return self::besselI1($x);
|
||||
}
|
||||
|
||||
$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 self::besselI2($x, $ord);
|
||||
}
|
||||
|
||||
return $fResult;
|
||||
private static function besselI0(float $x): float
|
||||
{
|
||||
$ax = abs($x);
|
||||
|
||||
if ($ax < 3.75) {
|
||||
$y = $x / 3.75;
|
||||
$y = $y * $y;
|
||||
|
||||
return 1.0 + $y * (3.5156229 + $y * (3.0899424 + $y * (1.2067492
|
||||
+ $y * (0.2659732 + $y * (0.360768e-1 + $y * 0.45813e-2)))));
|
||||
}
|
||||
|
||||
$y = 3.75 / $ax;
|
||||
|
||||
return (exp($ax) / sqrt($ax)) * (0.39894228 + $y * (0.1328592e-1 + $y * (0.225319e-2 + $y * (-0.157565e-2
|
||||
+ $y * (0.916281e-2 + $y * (-0.2057706e-1 + $y * (0.2635537e-1 +
|
||||
$y * (-0.1647633e-1 + $y * 0.392377e-2))))))));
|
||||
}
|
||||
|
||||
private static function besselI1(float $x): float
|
||||
{
|
||||
$ax = abs($x);
|
||||
|
||||
if ($ax < 3.75) {
|
||||
$y = $x / 3.75;
|
||||
$y = $y * $y;
|
||||
$ans = $ax * (0.5 + $y * (0.87890594 + $y * (0.51498869 + $y * (0.15084934 + $y * (0.2658733e-1 +
|
||||
$y * (0.301532e-2 + $y * 0.32411e-3))))));
|
||||
|
||||
return ($x < 0.0) ? -$ans : $ans;
|
||||
}
|
||||
|
||||
$y = 3.75 / $ax;
|
||||
$ans = 0.2282967e-1 + $y * (-0.2895312e-1 + $y * (0.1787654e-1 - $y * 0.420059e-2));
|
||||
$ans = 0.39894228 + $y * (-0.3988024e-1 + $y * (-0.362018e-2 + $y * (0.163801e-2 +
|
||||
$y * (-0.1031555e-1 + $y * $ans))));
|
||||
$ans *= exp($ax) / sqrt($ax);
|
||||
|
||||
return ($x < 0.0) ? -$ans : $ans;
|
||||
}
|
||||
|
||||
private static function besselI2(float $x, int $ord): float
|
||||
{
|
||||
if ($x === 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
$tox = 2.0 / abs($x);
|
||||
$bip = 0;
|
||||
$ans = 0.0;
|
||||
$bi = 1.0;
|
||||
|
||||
for ($j = 2 * ($ord + (int) sqrt(40.0 * $ord)); $j > 0; --$j) {
|
||||
$bim = $bip + $j * $tox * $bi;
|
||||
$bip = $bi;
|
||||
$bi = $bim;
|
||||
|
||||
if (abs($bi) > 1.0e+12) {
|
||||
$ans *= 1.0e-12;
|
||||
$bi *= 1.0e-12;
|
||||
$bip *= 1.0e-12;
|
||||
}
|
||||
|
||||
if ($j === $ord) {
|
||||
$ans = $bip;
|
||||
}
|
||||
}
|
||||
|
||||
$ans *= self::besselI0($x) / $bi;
|
||||
|
||||
return ($x < 0.0 && (($ord % 2) === 1)) ? -$ans : $ans;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
|
||||
|
||||
class BesselJ
|
||||
{
|
||||
|
|
@ -15,9 +15,13 @@ class BesselJ
|
|||
* Excel Function:
|
||||
* BESSELJ(x,ord)
|
||||
*
|
||||
* @param float $x The value at which to evaluate the function.
|
||||
* NOTE: The MS Excel implementation of the BESSELJ function is still not accurate, particularly for higher order
|
||||
* values with x < -8 and x > 8. This code provides a more accurate calculation
|
||||
*
|
||||
* @param mixed $x A float 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.
|
||||
* @param mixed $ord The integer order of the Bessel function.
|
||||
* If ord 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.
|
||||
*
|
||||
|
|
@ -25,11 +29,16 @@ class BesselJ
|
|||
*/
|
||||
public static function BESSELJ($x, $ord)
|
||||
{
|
||||
$x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
|
||||
$ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
|
||||
$x = Functions::flattenSingleValue($x);
|
||||
$ord = Functions::flattenSingleValue($ord);
|
||||
|
||||
try {
|
||||
$x = EngineeringValidations::validateFloat($x);
|
||||
$ord = EngineeringValidations::validateInt($ord);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
if ((is_numeric($x)) && (is_numeric($ord))) {
|
||||
$ord = (int) floor($ord);
|
||||
if ($ord < 0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
|
@ -39,33 +48,125 @@ class BesselJ
|
|||
return (is_nan($fResult)) ? Functions::NAN() : $fResult;
|
||||
}
|
||||
|
||||
return Functions::VALUE();
|
||||
}
|
||||
|
||||
private static function calculate(float $x, int $ord): float
|
||||
{
|
||||
if (abs($x) <= 30) {
|
||||
$fResult = $fTerm = ($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));
|
||||
|
||||
return $fResult;
|
||||
// special cases
|
||||
switch ($ord) {
|
||||
case 0:
|
||||
return self::besselJ0($x);
|
||||
case 1:
|
||||
return self::besselJ1($x);
|
||||
}
|
||||
|
||||
$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 self::besselJ2($x, $ord);
|
||||
}
|
||||
|
||||
return $fResult;
|
||||
private static function besselJ0(float $x): float
|
||||
{
|
||||
$ax = abs($x);
|
||||
|
||||
if ($ax < 8.0) {
|
||||
$y = $x * $x;
|
||||
$ans1 = 57568490574.0 + $y * (-13362590354.0 + $y * (651619640.7 + $y * (-11214424.18 + $y *
|
||||
(77392.33017 + $y * (-184.9052456)))));
|
||||
$ans2 = 57568490411.0 + $y * (1029532985.0 + $y * (9494680.718 + $y * (59272.64853 + $y *
|
||||
(267.8532712 + $y * 1.0))));
|
||||
|
||||
return $ans1 / $ans2;
|
||||
}
|
||||
|
||||
$z = 8.0 / $ax;
|
||||
$y = $z * $z;
|
||||
$xx = $ax - 0.785398164;
|
||||
$ans1 = 1.0 + $y * (-0.1098628627e-2 + $y * (0.2734510407e-4 + $y * (-0.2073370639e-5 + $y * 0.2093887211e-6)));
|
||||
$ans2 = -0.1562499995e-1 + $y * (0.1430488765e-3 + $y * (-0.6911147651e-5 + $y *
|
||||
(0.7621095161e-6 - $y * 0.934935152e-7)));
|
||||
|
||||
return sqrt(0.636619772 / $ax) * (cos($xx) * $ans1 - $z * sin($xx) * $ans2);
|
||||
}
|
||||
|
||||
private static function besselJ1(float $x): float
|
||||
{
|
||||
$ax = abs($x);
|
||||
|
||||
if ($ax < 8.0) {
|
||||
$y = $x * $x;
|
||||
$ans1 = $x * (72362614232.0 + $y * (-7895059235.0 + $y * (242396853.1 + $y *
|
||||
(-2972611.439 + $y * (15704.48260 + $y * (-30.16036606))))));
|
||||
$ans2 = 144725228442.0 + $y * (2300535178.0 + $y * (18583304.74 + $y * (99447.43394 + $y *
|
||||
(376.9991397 + $y * 1.0))));
|
||||
|
||||
return $ans1 / $ans2;
|
||||
}
|
||||
|
||||
$z = 8.0 / $ax;
|
||||
$y = $z * $z;
|
||||
$xx = $ax - 2.356194491;
|
||||
|
||||
$ans1 = 1.0 + $y * (0.183105e-2 + $y * (-0.3516396496e-4 + $y * (0.2457520174e-5 + $y * (-0.240337019e-6))));
|
||||
$ans2 = 0.04687499995 + $y * (-0.2002690873e-3 + $y * (0.8449199096e-5 + $y *
|
||||
(-0.88228987e-6 + $y * 0.105787412e-6)));
|
||||
$ans = sqrt(0.636619772 / $ax) * (cos($xx) * $ans1 - $z * sin($xx) * $ans2);
|
||||
|
||||
return ($x < 0.0) ? -$ans : $ans;
|
||||
}
|
||||
|
||||
private static function besselJ2(float $x, int $ord): float
|
||||
{
|
||||
$ax = abs($x);
|
||||
if ($ax === 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
if ($ax > $ord) {
|
||||
return self::besselj2a($ax, $ord, $x);
|
||||
}
|
||||
|
||||
return self::besselj2b($ax, $ord, $x);
|
||||
}
|
||||
|
||||
private static function besselj2a(float $ax, int $ord, float $x)
|
||||
{
|
||||
$tox = 2.0 / $ax;
|
||||
$bjm = self::besselJ0($ax);
|
||||
$bj = self::besselJ1($ax);
|
||||
for ($j = 1; $j < $ord; ++$j) {
|
||||
$bjp = $j * $tox * $bj - $bjm;
|
||||
$bjm = $bj;
|
||||
$bj = $bjp;
|
||||
}
|
||||
$ans = $bj;
|
||||
|
||||
return ($x < 0.0 && ($ord % 2) == 1) ? -$ans : $ans;
|
||||
}
|
||||
|
||||
private static function besselj2b(float $ax, int $ord, float $x)
|
||||
{
|
||||
$tox = 2.0 / $ax;
|
||||
$jsum = false;
|
||||
$bjp = $ans = $sum = 0.0;
|
||||
$bj = 1.0;
|
||||
for ($j = 2 * ($ord + (int) sqrt(40.0 * $ord)); $j > 0; --$j) {
|
||||
$bjm = $j * $tox * $bj - $bjp;
|
||||
$bjp = $bj;
|
||||
$bj = $bjm;
|
||||
if (abs($bj) > 1.0e+10) {
|
||||
$bj *= 1.0e-10;
|
||||
$bjp *= 1.0e-10;
|
||||
$ans *= 1.0e-10;
|
||||
$sum *= 1.0e-10;
|
||||
}
|
||||
if ($jsum === true) {
|
||||
$sum += $bj;
|
||||
}
|
||||
$jsum = !$jsum;
|
||||
if ($j === $ord) {
|
||||
$ans = $bjp;
|
||||
}
|
||||
}
|
||||
$sum = 2.0 * $sum - $bj;
|
||||
$ans /= $sum;
|
||||
|
||||
return ($x < 0.0 && ($ord % 2) === 1) ? -$ans : $ans;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class BesselK
|
||||
|
|
@ -15,75 +16,81 @@ class BesselK
|
|||
* Excel Function:
|
||||
* BESSELK(x,ord)
|
||||
*
|
||||
* @param float $x The value at which to evaluate the function.
|
||||
* @param mixed $x A float 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.
|
||||
* @param mixed $ord The integer order of the Bessel function.
|
||||
* If ord 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.
|
||||
* If $ord < 0, BESSELKI returns the #NUM! error value.
|
||||
*
|
||||
* @return float|string Result, or a string containing an error
|
||||
*/
|
||||
public static function BESSELK($x, $ord)
|
||||
{
|
||||
$x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
|
||||
$ord = ($ord === null) ? 0 : Functions::flattenSingleValue($ord);
|
||||
$x = Functions::flattenSingleValue($x);
|
||||
$ord = Functions::flattenSingleValue($ord);
|
||||
|
||||
if ((is_numeric($x)) && (is_numeric($ord))) {
|
||||
if (($ord < 0) || ($x == 0.0)) {
|
||||
try {
|
||||
$x = EngineeringValidations::validateFloat($x);
|
||||
$ord = EngineeringValidations::validateInt($ord);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
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:
|
||||
$fBk = self::besselK2($x, $ord);
|
||||
}
|
||||
$fBk = self::calculate($x, $ord);
|
||||
|
||||
return (is_nan($fBk)) ? Functions::NAN() : $fBk;
|
||||
}
|
||||
|
||||
return Functions::VALUE();
|
||||
private static function calculate(float $x, int $ord): float
|
||||
{
|
||||
// special cases
|
||||
switch ($ord) {
|
||||
case 0:
|
||||
return self::besselK0($x);
|
||||
case 1:
|
||||
return self::besselK1($x);
|
||||
}
|
||||
|
||||
private static function besselK0(float $fNum): float
|
||||
return self::besselK2($x, $ord);
|
||||
}
|
||||
|
||||
private static function besselK0(float $x): float
|
||||
{
|
||||
if ($fNum <= 2) {
|
||||
$fNum2 = $fNum * 0.5;
|
||||
if ($x <= 2) {
|
||||
$fNum2 = $x * 0.5;
|
||||
$y = ($fNum2 * $fNum2);
|
||||
|
||||
return -log($fNum2) * BesselI::BESSELI($fNum, 0) +
|
||||
return -log($fNum2) * BesselI::BESSELI($x, 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))))));
|
||||
}
|
||||
|
||||
$y = 2 / $fNum;
|
||||
$y = 2 / $x;
|
||||
|
||||
return exp(-$fNum) / sqrt($fNum) *
|
||||
return exp(-$x) / sqrt($x) *
|
||||
(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))))));
|
||||
}
|
||||
|
||||
private static function besselK1(float $fNum): float
|
||||
private static function besselK1(float $x): float
|
||||
{
|
||||
if ($fNum <= 2) {
|
||||
$fNum2 = $fNum * 0.5;
|
||||
if ($x <= 2) {
|
||||
$fNum2 = $x * 0.5;
|
||||
$y = ($fNum2 * $fNum2);
|
||||
|
||||
return log($fNum2) * BesselI::BESSELI($fNum, 1) +
|
||||
return log($fNum2) * BesselI::BESSELI($x, 1) +
|
||||
(1 + $y * (0.15443144 + $y * (-0.67278579 + $y * (-0.18156897 + $y * (-0.1919402e-1 + $y *
|
||||
(-0.110404e-2 + $y * (-0.4686e-4))))))) / $fNum;
|
||||
(-0.110404e-2 + $y * (-0.4686e-4))))))) / $x;
|
||||
}
|
||||
|
||||
$y = 2 / $fNum;
|
||||
$y = 2 / $x;
|
||||
|
||||
return exp(-$fNum) / sqrt($fNum) *
|
||||
return exp(-$x) / sqrt($x) *
|
||||
(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)))))));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class BesselY
|
||||
|
|
@ -14,9 +15,10 @@ class BesselY
|
|||
* Excel Function:
|
||||
* BESSELY(x,ord)
|
||||
*
|
||||
* @param float $x The value at which to evaluate the function.
|
||||
* @param mixed $x A float value at which to evaluate the function.
|
||||
* If x is nonnumeric, BESSELY returns the #VALUE! error value.
|
||||
* @param int $ord The order of the Bessel function. If n is not an integer, it is truncated.
|
||||
* @param mixed $ord The integer order of the Bessel function.
|
||||
* If ord is not an integer, it is truncated.
|
||||
* If $ord is nonnumeric, BESSELY returns the #VALUE! error value.
|
||||
* If $ord < 0, BESSELY returns the #NUM! error value.
|
||||
*
|
||||
|
|
@ -24,73 +26,85 @@ class BesselY
|
|||
*/
|
||||
public static function BESSELY($x, $ord)
|
||||
{
|
||||
$x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
|
||||
$ord = ($ord === null) ? 0 : Functions::flattenSingleValue($ord);
|
||||
$x = Functions::flattenSingleValue($x);
|
||||
$ord = Functions::flattenSingleValue($ord);
|
||||
|
||||
if ((is_numeric($x)) && (is_numeric($ord))) {
|
||||
if (($ord < 0) || ($x == 0.0)) {
|
||||
try {
|
||||
$x = EngineeringValidations::validateFloat($x);
|
||||
$ord = EngineeringValidations::validateInt($ord);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
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:
|
||||
$fBy = self::besselY2($x, $ord);
|
||||
}
|
||||
$fBy = self::calculate($x, $ord);
|
||||
|
||||
return (is_nan($fBy)) ? Functions::NAN() : $fBy;
|
||||
}
|
||||
|
||||
return Functions::VALUE();
|
||||
private static function calculate(float $x, int $ord): float
|
||||
{
|
||||
// special cases
|
||||
switch ($ord) {
|
||||
case 0:
|
||||
return self::besselY0($x);
|
||||
case 1:
|
||||
return self::besselY1($x);
|
||||
}
|
||||
|
||||
private static function besselY0(float $fNum): float
|
||||
return self::besselY2($x, $ord);
|
||||
}
|
||||
|
||||
private static function besselY0(float $x): float
|
||||
{
|
||||
if ($fNum < 8.0) {
|
||||
$y = ($fNum * $fNum);
|
||||
$f1 = -2957821389.0 + $y * (7062834065.0 + $y * (-512359803.6 + $y * (10879881.29 + $y *
|
||||
if ($x < 8.0) {
|
||||
$y = ($x * $x);
|
||||
$ans1 = -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 *
|
||||
$ans2 = 40076544269.0 + $y * (745249964.8 + $y * (7189466.438 + $y *
|
||||
(47447.26470 + $y * (226.1030244 + $y))));
|
||||
|
||||
return $f1 / $f2 + 0.636619772 * BesselJ::BESSELJ($fNum, 0) * log($fNum);
|
||||
return $ans1 / $ans2 + 0.636619772 * BesselJ::BESSELJ($x, 0) * log($x);
|
||||
}
|
||||
|
||||
$z = 8.0 / $fNum;
|
||||
$z = 8.0 / $x;
|
||||
$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 *
|
||||
$xx = $x - 0.785398164;
|
||||
$ans1 = 1 + $y * (-0.1098628627e-2 + $y * (0.2734510407e-4 + $y * (-0.2073370639e-5 + $y * 0.2093887211e-6)));
|
||||
$ans2 = -0.1562499995e-1 + $y * (0.1430488765e-3 + $y * (-0.6911147651e-5 + $y * (0.7621095161e-6 + $y *
|
||||
(-0.934945152e-7))));
|
||||
|
||||
return sqrt(0.636619772 / $fNum) * (sin($xx) * $f1 + $z * cos($xx) * $f2);
|
||||
return sqrt(0.636619772 / $x) * (sin($xx) * $ans1 + $z * cos($xx) * $ans2);
|
||||
}
|
||||
|
||||
private static function besselY1(float $fNum): float
|
||||
private static function besselY1(float $x): float
|
||||
{
|
||||
if ($fNum < 8.0) {
|
||||
$y = ($fNum * $fNum);
|
||||
$f1 = $fNum * (-0.4900604943e13 + $y * (0.1275274390e13 + $y * (-0.5153438139e11 + $y *
|
||||
if ($x < 8.0) {
|
||||
$y = ($x * $x);
|
||||
$ans1 = $x * (-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 *
|
||||
$ans2 = 0.2499580570e14 + $y * (0.4244419664e12 + $y * (0.3733650367e10 + $y * (0.2245904002e8 + $y *
|
||||
(0.1020426050e6 + $y * (0.3549632885e3 + $y)))));
|
||||
|
||||
return $f1 / $f2 + 0.636619772 * (BesselJ::BESSELJ($fNum, 1) * log($fNum) - 1 / $fNum);
|
||||
return ($ans1 / $ans2) + 0.636619772 * (BesselJ::BESSELJ($x, 1) * log($x) - 1 / $x);
|
||||
}
|
||||
|
||||
return sqrt(0.636619772 / $fNum) * sin($fNum - 2.356194491);
|
||||
$z = 8.0 / $x;
|
||||
$y = $z * $z;
|
||||
$xx = $x - 2.356194491;
|
||||
$ans1 = 1.0 + $y * (0.183105e-2 + $y * (-0.3516396496e-4 + $y * (0.2457520174e-5 + $y * (-0.240337019e-6))));
|
||||
$ans2 = 0.04687499995 + $y * (-0.2002690873e-3 + $y * (0.8449199096e-5 + $y *
|
||||
(-0.88228987e-6 + $y * 0.105787412e-6)));
|
||||
|
||||
return sqrt(0.636619772 / $x) * (sin($xx) * $ans1 + $z * cos($xx) * $ans2);
|
||||
}
|
||||
|
||||
private static function besselY2(float $x, int $ord)
|
||||
private static function besselY2(float $x, int $ord): float
|
||||
{
|
||||
$fTox = 2 / $x;
|
||||
$fTox = 2.0 / $x;
|
||||
$fBym = self::besselY0($x);
|
||||
$fBy = self::besselY1($x);
|
||||
for ($n = 1; $n < $ord; ++$n) {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class Compare
|
||||
|
|
@ -27,8 +28,11 @@ class Compare
|
|||
$a = Functions::flattenSingleValue($a);
|
||||
$b = Functions::flattenSingleValue($b);
|
||||
|
||||
if (!is_numeric($a) || !is_numeric($b)) {
|
||||
return Functions::VALUE();
|
||||
try {
|
||||
$a = EngineeringValidations::validateFloat($a);
|
||||
$b = EngineeringValidations::validateFloat($b);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
return (int) ($a == $b);
|
||||
|
|
@ -54,8 +58,11 @@ class Compare
|
|||
$number = Functions::flattenSingleValue($number);
|
||||
$step = Functions::flattenSingleValue($step);
|
||||
|
||||
if (!is_numeric($number) || !is_numeric($step)) {
|
||||
return Functions::VALUE();
|
||||
try {
|
||||
$number = EngineeringValidations::validateFloat($number);
|
||||
$step = EngineeringValidations::validateFloat($step);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
return (int) ($number >= $step);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
use Complex\Complex as ComplexObject;
|
||||
use Complex\Exception as ComplexException;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class Complex
|
||||
{
|
||||
/**
|
||||
* COMPLEX.
|
||||
*
|
||||
* Converts real and imaginary coefficients into a complex number of the form x +/- yi or x +/- yj.
|
||||
*
|
||||
* Excel Function:
|
||||
* COMPLEX(realNumber,imaginary[,suffix])
|
||||
*
|
||||
* @param mixed $realNumber the real float coefficient of the complex number
|
||||
* @param mixed $imaginary the imaginary float coefficient of the complex number
|
||||
* @param mixed $suffix The character 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);
|
||||
|
||||
try {
|
||||
$realNumber = EngineeringValidations::validateFloat($realNumber);
|
||||
$imaginary = EngineeringValidations::validateFloat($imaginary);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
if (($suffix == 'i') || ($suffix == 'j') || ($suffix == '')) {
|
||||
$complex = new ComplexObject($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)
|
||||
*
|
||||
* @param string $complexNumber the complex number for which you want the imaginary
|
||||
* coefficient
|
||||
*
|
||||
* @return float|string
|
||||
*/
|
||||
public static function IMAGINARY($complexNumber)
|
||||
{
|
||||
$complexNumber = Functions::flattenSingleValue($complexNumber);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return $complex->getImaginary();
|
||||
}
|
||||
|
||||
/**
|
||||
* IMREAL.
|
||||
*
|
||||
* Returns the real coefficient of a complex number in x + yi or x + yj text format.
|
||||
*
|
||||
* Excel Function:
|
||||
* IMREAL(complexNumber)
|
||||
*
|
||||
* @param string $complexNumber the complex number for which you want the real coefficient
|
||||
*
|
||||
* @return float|string
|
||||
*/
|
||||
public static function IMREAL($complexNumber)
|
||||
{
|
||||
$complexNumber = Functions::flattenSingleValue($complexNumber);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return $complex->getReal();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,513 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
use Complex\Complex as ComplexObject;
|
||||
use Complex\Exception as ComplexException;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class ComplexFunctions
|
||||
{
|
||||
/**
|
||||
* 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|string
|
||||
*/
|
||||
public static function IMABS($complexNumber)
|
||||
{
|
||||
$complexNumber = Functions::flattenSingleValue($complexNumber);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
$theta = self::IMARGUMENT($complexNumber);
|
||||
if ($theta === Functions::DIV0()) {
|
||||
return '0';
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $complex->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);
|
||||
|
||||
try {
|
||||
$complex = new ComplexObject($complexNumber);
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
if (!is_numeric($realNumber)) {
|
||||
return Functions::VALUE();
|
||||
}
|
||||
|
||||
return (string) $complex->pow($realNumber);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
use Complex\Complex as ComplexObject;
|
||||
use Complex\Exception as ComplexException;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class ComplexOperations
|
||||
{
|
||||
/**
|
||||
* 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 ComplexObject($complexDividend))->divideby(new ComplexObject($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 ComplexObject($complexNumber1))->subtract(new ComplexObject($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 ComplexObject(0.0);
|
||||
$aArgs = Functions::flattenArray($complexNumbers);
|
||||
|
||||
try {
|
||||
// Loop through the arguments
|
||||
foreach ($aArgs as $complex) {
|
||||
$returnValue = $returnValue->add(new ComplexObject($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 ComplexObject(1.0);
|
||||
$aArgs = Functions::flattenArray($complexNumbers);
|
||||
|
||||
try {
|
||||
// Loop through the arguments
|
||||
foreach ($aArgs as $complex) {
|
||||
$returnValue = $returnValue->multiply(new ComplexObject($complex));
|
||||
}
|
||||
} catch (ComplexException $e) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return (string) $returnValue;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
class Constants
|
||||
{
|
||||
/**
|
||||
* EULER.
|
||||
*/
|
||||
public const EULER = 2.71828182845904523536;
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ class ConvertBinary extends ConvertBase
|
|||
return '-' . (512 - bindec($value));
|
||||
}
|
||||
|
||||
return bindec($value);
|
||||
return (string) bindec($value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -85,10 +85,10 @@ class ConvertHex extends ConvertBase
|
|||
$binX[$i] = ($binX[$i] == '1' ? '0' : '1');
|
||||
}
|
||||
|
||||
return (bindec($binX) + 1) * -1;
|
||||
return (string) ((bindec($binX) + 1) * -1);
|
||||
}
|
||||
|
||||
return bindec($binX);
|
||||
return (string) bindec($binX);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -85,10 +85,10 @@ class ConvertOctal extends ConvertBase
|
|||
$binX[$i] = ($binX[$i] == '1' ? '0' : '1');
|
||||
}
|
||||
|
||||
return (bindec($binX) + 1) * -1;
|
||||
return (string) ((bindec($binX) + 1) * -1);
|
||||
}
|
||||
|
||||
return bindec($binX);
|
||||
return (string) bindec($binX);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -490,7 +490,7 @@ class ConvertUOM
|
|||
* getConversionMultipliers
|
||||
* Returns an array of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM().
|
||||
*
|
||||
* @return array of mixed
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function getConversionMultipliers()
|
||||
{
|
||||
|
|
@ -501,7 +501,7 @@ class ConvertUOM
|
|||
* getBinaryConversionMultipliers
|
||||
* Returns an array of the additional Multiplier prefixes that can be used with Information Units of Measure in CONVERTUOM().
|
||||
*
|
||||
* @return array of mixed
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function getBinaryConversionMultipliers()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class EngineeringValidations
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public static function validateFloat($value): float
|
||||
{
|
||||
if (!is_numeric($value)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
|
||||
return (float) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public static function validateInt($value): int
|
||||
{
|
||||
if (!is_numeric($value)) {
|
||||
throw new Exception(Functions::VALUE());
|
||||
}
|
||||
|
||||
return (int) floor((float) $value);
|
||||
}
|
||||
}
|
||||
|
|
@ -21,8 +21,8 @@ class Erf
|
|||
* Excel Function:
|
||||
* ERF(lower[,upper])
|
||||
*
|
||||
* @param float $lower lower bound for integrating ERF
|
||||
* @param float $upper upper bound for integrating ERF.
|
||||
* @param mixed $lower Lower bound float for integrating ERF
|
||||
* @param mixed $upper Upper bound float for integrating ERF.
|
||||
* If omitted, ERF integrates between zero and lower_limit
|
||||
*
|
||||
* @return float|string
|
||||
|
|
@ -52,7 +52,7 @@ class Erf
|
|||
* Excel Function:
|
||||
* ERF.PRECISE(limit)
|
||||
*
|
||||
* @param float $limit bound for integrating ERF
|
||||
* @param mixed $limit Float bound for integrating ERF, other bound is zero
|
||||
*
|
||||
* @return float|string
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ class ErfC
|
|||
* Excel Function:
|
||||
* ERFC(x)
|
||||
*
|
||||
* @param float $value The lower bound for integrating ERFC
|
||||
* @param mixed $value The float lower bound for integrating ERFC
|
||||
*
|
||||
* @return float|string
|
||||
*/
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,210 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class Amortization
|
||||
{
|
||||
/**
|
||||
* 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])
|
||||
*
|
||||
* @param mixed $cost The float 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 mixed $period the period (float)
|
||||
* @param mixed $rate rate of depreciation (float)
|
||||
* @param mixed $basis The type of day count to use (int).
|
||||
* 0 or omitted US (NASD) 30/360
|
||||
* 1 Actual/actual
|
||||
* 2 Actual/360
|
||||
* 3 Actual/365
|
||||
* 4 European 30/360
|
||||
*
|
||||
* @return float|string (string containing the error type if there is an error)
|
||||
*/
|
||||
public static function AMORDEGRC(
|
||||
$cost,
|
||||
$purchased,
|
||||
$firstPeriod,
|
||||
$salvage,
|
||||
$period,
|
||||
$rate,
|
||||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
|
||||
) {
|
||||
$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)
|
||||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
|
||||
: Functions::flattenSingleValue($basis);
|
||||
|
||||
try {
|
||||
$cost = FinancialValidations::validateFloat($cost);
|
||||
$purchased = FinancialValidations::validateDate($purchased);
|
||||
$firstPeriod = FinancialValidations::validateDate($firstPeriod);
|
||||
$salvage = FinancialValidations::validateFloat($salvage);
|
||||
$period = FinancialValidations::validateInt($period);
|
||||
$rate = FinancialValidations::validateFloat($rate);
|
||||
$basis = FinancialValidations::validateBasis($basis);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
$yearFrac = DateTimeExcel\YearFrac::evaluate($purchased, $firstPeriod, $basis);
|
||||
if (is_string($yearFrac)) {
|
||||
return $yearFrac;
|
||||
}
|
||||
|
||||
$amortiseCoeff = self::getAmortizationCoefficient($rate);
|
||||
|
||||
$rate *= $amortiseCoeff;
|
||||
$fNRate = round($yearFrac * $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])
|
||||
*
|
||||
* @param mixed $cost The cost of the asset as a float
|
||||
* @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 mixed $period The period as a float
|
||||
* @param mixed $rate Rate of depreciation as float
|
||||
* @param mixed $basis Integer indicating 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 (string containing the error type if there is an error)
|
||||
*/
|
||||
public static function AMORLINC(
|
||||
$cost,
|
||||
$purchased,
|
||||
$firstPeriod,
|
||||
$salvage,
|
||||
$period,
|
||||
$rate,
|
||||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
|
||||
) {
|
||||
$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)
|
||||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
|
||||
: Functions::flattenSingleValue($basis);
|
||||
|
||||
try {
|
||||
$cost = FinancialValidations::validateFloat($cost);
|
||||
$purchased = FinancialValidations::validateDate($purchased);
|
||||
$firstPeriod = FinancialValidations::validateDate($firstPeriod);
|
||||
$salvage = FinancialValidations::validateFloat($salvage);
|
||||
$period = FinancialValidations::validateFloat($period);
|
||||
$rate = FinancialValidations::validateFloat($rate);
|
||||
$basis = FinancialValidations::validateBasis($basis);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
$fOneRate = $cost * $rate;
|
||||
$fCostDelta = $cost - $salvage;
|
||||
// Note, quirky variation for leap years on the YEARFRAC for this function
|
||||
$purchasedYear = DateTimeExcel\Year::evaluate($purchased);
|
||||
$yearFrac = DateTimeExcel\YearFrac::evaluate($purchased, $firstPeriod, $basis);
|
||||
if (is_string($yearFrac)) {
|
||||
return $yearFrac;
|
||||
}
|
||||
|
||||
if (
|
||||
($basis == FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL) &&
|
||||
($yearFrac < 1) && (DateTimeExcel\Helpers::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;
|
||||
}
|
||||
|
||||
private static function getAmortizationCoefficient(float $rate): float
|
||||
{
|
||||
// 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) {
|
||||
return 1.0;
|
||||
} elseif ($fUsePer < 4.0) {
|
||||
return 1.5;
|
||||
} elseif ($fUsePer <= 6.0) {
|
||||
return 2.0;
|
||||
}
|
||||
|
||||
return 2.5;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\FinancialValidations;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class CashFlowValidations extends FinancialValidations
|
||||
{
|
||||
/**
|
||||
* @param mixed $rate
|
||||
*/
|
||||
public static function validateRate($rate): float
|
||||
{
|
||||
$rate = self::validateFloat($rate);
|
||||
if ($rate < 0.0) {
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
|
||||
return $rate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $type
|
||||
*/
|
||||
public static function validatePeriodType($type): int
|
||||
{
|
||||
$rate = self::validateInt($type);
|
||||
if (
|
||||
$type !== FinancialConstants::PAYMENT_END_OF_PERIOD &&
|
||||
$type !== FinancialConstants::PAYMENT_BEGINNING_OF_PERIOD
|
||||
) {
|
||||
throw new Exception(Functions::NAN());
|
||||
}
|
||||
|
||||
return $rate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $presentValue
|
||||
*/
|
||||
public static function validatePresentValue($presentValue): float
|
||||
{
|
||||
return self::validateFloat($presentValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $futureValue
|
||||
*/
|
||||
public static function validateFutureValue($futureValue): float
|
||||
{
|
||||
return self::validateFloat($futureValue);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,199 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\Constant;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\CashFlowValidations;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class Periodic
|
||||
{
|
||||
/**
|
||||
* FV.
|
||||
*
|
||||
* Returns the Future Value of a cash flow with constant payments and interest rate (annuities).
|
||||
*
|
||||
* Excel Function:
|
||||
* FV(rate,nper,pmt[,pv[,type]])
|
||||
*
|
||||
* @param mixed $rate The interest rate per period
|
||||
* @param mixed $numberOfPeriods Total number of payment periods in an annuity as an integer
|
||||
* @param mixed $payment 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 mixed $presentValue present Value, or the lump-sum amount that a series of
|
||||
* future payments is worth right now
|
||||
* @param mixed $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 futureValue(
|
||||
$rate,
|
||||
$numberOfPeriods,
|
||||
$payment = 0.0,
|
||||
$presentValue = 0.0,
|
||||
$type = FinancialConstants::PAYMENT_END_OF_PERIOD
|
||||
) {
|
||||
$rate = Functions::flattenSingleValue($rate);
|
||||
$numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
|
||||
$payment = ($payment === null) ? 0.0 : Functions::flattenSingleValue($payment);
|
||||
$presentValue = ($presentValue === null) ? 0.0 : Functions::flattenSingleValue($presentValue);
|
||||
$type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
|
||||
|
||||
try {
|
||||
$rate = CashFlowValidations::validateFloat($rate);
|
||||
$numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
|
||||
$payment = CashFlowValidations::validateFloat($payment);
|
||||
$presentValue = CashFlowValidations::validatePresentValue($presentValue);
|
||||
$type = CashFlowValidations::validatePeriodType($type);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
return self::calculateFutureValue($rate, $numberOfPeriods, $payment, $presentValue, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* PV.
|
||||
*
|
||||
* Returns the Present Value of a cash flow with constant payments and interest rate (annuities).
|
||||
*
|
||||
* @param mixed $rate Interest rate per period
|
||||
* @param mixed $numberOfPeriods Number of periods as an integer
|
||||
* @param mixed $payment Periodic payment (annuity)
|
||||
* @param mixed $futureValue Future Value
|
||||
* @param mixed $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
|
||||
*
|
||||
* @return float|string Result, or a string containing an error
|
||||
*/
|
||||
public static function presentValue(
|
||||
$rate,
|
||||
$numberOfPeriods,
|
||||
$payment = 0.0,
|
||||
$futureValue = 0.0,
|
||||
$type = FinancialConstants::PAYMENT_END_OF_PERIOD
|
||||
) {
|
||||
$rate = Functions::flattenSingleValue($rate);
|
||||
$numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
|
||||
$payment = ($payment === null) ? 0.0 : Functions::flattenSingleValue($payment);
|
||||
$futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
|
||||
$type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
|
||||
|
||||
try {
|
||||
$rate = CashFlowValidations::validateRate($rate);
|
||||
$numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
|
||||
$payment = CashFlowValidations::validateFloat($payment);
|
||||
$futureValue = CashFlowValidations::validateFutureValue($futureValue);
|
||||
$type = CashFlowValidations::validatePeriodType($type);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Validate parameters
|
||||
if ($numberOfPeriods < 0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return self::calculatePresentValue($rate, $numberOfPeriods, $payment, $futureValue, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* NPER.
|
||||
*
|
||||
* Returns the number of periods for a cash flow with constant periodic payments (annuities), and interest rate.
|
||||
*
|
||||
* @param mixed $rate Interest rate per period
|
||||
* @param mixed $payment Periodic payment (annuity)
|
||||
* @param mixed $presentValue Present Value
|
||||
* @param mixed $futureValue Future Value
|
||||
* @param mixed $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
|
||||
*
|
||||
* @return float|string Result, or a string containing an error
|
||||
*/
|
||||
public static function periods(
|
||||
$rate,
|
||||
$payment,
|
||||
$presentValue,
|
||||
$futureValue = 0.0,
|
||||
$type = FinancialConstants::PAYMENT_END_OF_PERIOD
|
||||
) {
|
||||
$rate = Functions::flattenSingleValue($rate);
|
||||
$payment = Functions::flattenSingleValue($payment);
|
||||
$presentValue = Functions::flattenSingleValue($presentValue);
|
||||
$futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
|
||||
$type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
|
||||
|
||||
try {
|
||||
$rate = CashFlowValidations::validateRate($rate);
|
||||
$payment = CashFlowValidations::validateFloat($payment);
|
||||
$presentValue = CashFlowValidations::validatePresentValue($presentValue);
|
||||
$futureValue = CashFlowValidations::validateFutureValue($futureValue);
|
||||
$type = CashFlowValidations::validatePeriodType($type);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Validate parameters
|
||||
if ($payment == 0.0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return self::calculatePeriods($rate, $payment, $presentValue, $futureValue, $type);
|
||||
}
|
||||
|
||||
private static function calculateFutureValue(
|
||||
float $rate,
|
||||
int $numberOfPeriods,
|
||||
float $payment,
|
||||
float $presentValue,
|
||||
int $type
|
||||
): float {
|
||||
if ($rate !== null && $rate != 0) {
|
||||
return -$presentValue *
|
||||
(1 + $rate) ** $numberOfPeriods - $payment * (1 + $rate * $type) * ((1 + $rate) ** $numberOfPeriods - 1)
|
||||
/ $rate;
|
||||
}
|
||||
|
||||
return -$presentValue - $payment * $numberOfPeriods;
|
||||
}
|
||||
|
||||
private static function calculatePresentValue(
|
||||
float $rate,
|
||||
int $numberOfPeriods,
|
||||
float $payment,
|
||||
float $futureValue,
|
||||
int $type
|
||||
): float {
|
||||
if ($rate != 0.0) {
|
||||
return (-$payment * (1 + $rate * $type)
|
||||
* (((1 + $rate) ** $numberOfPeriods - 1) / $rate) - $futureValue) / (1 + $rate) ** $numberOfPeriods;
|
||||
}
|
||||
|
||||
return -$futureValue - $payment * $numberOfPeriods;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float|string
|
||||
*/
|
||||
private static function calculatePeriods(
|
||||
float $rate,
|
||||
float $payment,
|
||||
float $presentValue,
|
||||
float $futureValue,
|
||||
int $type
|
||||
) {
|
||||
if ($rate != 0.0) {
|
||||
if ($presentValue == 0.0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return log(($payment * (1 + $rate * $type) / $rate - $futureValue) /
|
||||
($presentValue + $payment * (1 + $rate * $type) / $rate)) / log(1 + $rate);
|
||||
}
|
||||
|
||||
return (-$presentValue - $futureValue) / $payment;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\Constant\Periodic;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\CashFlowValidations;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class Cumulative
|
||||
{
|
||||
/**
|
||||
* CUMIPMT.
|
||||
*
|
||||
* Returns the cumulative interest paid on a loan between the start and end periods.
|
||||
*
|
||||
* Excel Function:
|
||||
* CUMIPMT(rate,nper,pv,start,end[,type])
|
||||
*
|
||||
* @param mixed $rate The Interest rate
|
||||
* @param mixed $periods The total number of payment periods
|
||||
* @param mixed $presentValue Present Value
|
||||
* @param mixed $start The first period in the calculation.
|
||||
* Payment periods are numbered beginning with 1.
|
||||
* @param mixed $end the last period in the calculation
|
||||
* @param mixed $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 interest(
|
||||
$rate,
|
||||
$periods,
|
||||
$presentValue,
|
||||
$start,
|
||||
$end,
|
||||
$type = FinancialConstants::PAYMENT_END_OF_PERIOD
|
||||
) {
|
||||
$rate = Functions::flattenSingleValue($rate);
|
||||
$periods = Functions::flattenSingleValue($periods);
|
||||
$presentValue = Functions::flattenSingleValue($presentValue);
|
||||
$start = Functions::flattenSingleValue($start);
|
||||
$end = Functions::flattenSingleValue($end);
|
||||
$type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
|
||||
|
||||
try {
|
||||
$rate = CashFlowValidations::validateRate($rate);
|
||||
$periods = CashFlowValidations::validateInt($periods);
|
||||
$presentValue = CashFlowValidations::validatePresentValue($presentValue);
|
||||
$start = CashFlowValidations::validateInt($start);
|
||||
$end = CashFlowValidations::validateInt($end);
|
||||
$type = CashFlowValidations::validatePeriodType($type);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Validate parameters
|
||||
if ($start < 1 || $start > $end) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
// Calculate
|
||||
$interest = 0;
|
||||
for ($per = $start; $per <= $end; ++$per) {
|
||||
$ipmt = Interest::payment($rate, $per, $periods, $presentValue, 0, $type);
|
||||
if (is_string($ipmt)) {
|
||||
return $ipmt;
|
||||
}
|
||||
|
||||
$interest += $ipmt;
|
||||
}
|
||||
|
||||
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])
|
||||
*
|
||||
* @param mixed $rate The Interest rate
|
||||
* @param mixed $periods The total number of payment periods as an integer
|
||||
* @param mixed $presentValue Present Value
|
||||
* @param mixed $start The first period in the calculation.
|
||||
* Payment periods are numbered beginning with 1.
|
||||
* @param mixed $end the last period in the calculation
|
||||
* @param mixed $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 principal(
|
||||
$rate,
|
||||
$periods,
|
||||
$presentValue,
|
||||
$start,
|
||||
$end,
|
||||
$type = FinancialConstants::PAYMENT_END_OF_PERIOD
|
||||
) {
|
||||
$rate = Functions::flattenSingleValue($rate);
|
||||
$periods = Functions::flattenSingleValue($periods);
|
||||
$presentValue = Functions::flattenSingleValue($presentValue);
|
||||
$start = Functions::flattenSingleValue($start);
|
||||
$end = Functions::flattenSingleValue($end);
|
||||
$type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
|
||||
|
||||
try {
|
||||
$rate = CashFlowValidations::validateRate($rate);
|
||||
$periods = CashFlowValidations::validateInt($periods);
|
||||
$presentValue = CashFlowValidations::validatePresentValue($presentValue);
|
||||
$start = CashFlowValidations::validateInt($start);
|
||||
$end = CashFlowValidations::validateInt($end);
|
||||
$type = CashFlowValidations::validatePeriodType($type);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Validate parameters
|
||||
if ($start < 1 || $start > $end) {
|
||||
return Functions::VALUE();
|
||||
}
|
||||
|
||||
// Calculate
|
||||
$principal = 0;
|
||||
for ($per = $start; $per <= $end; ++$per) {
|
||||
$ppmt = Payments::interestPayment($rate, $per, $periods, $presentValue, 0, $type);
|
||||
if (is_string($ppmt)) {
|
||||
return $ppmt;
|
||||
}
|
||||
|
||||
$principal += $ppmt;
|
||||
}
|
||||
|
||||
return $principal;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,216 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\Constant\Periodic;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\CashFlowValidations;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class Interest
|
||||
{
|
||||
private const FINANCIAL_MAX_ITERATIONS = 128;
|
||||
|
||||
private const FINANCIAL_PRECISION = 1.0e-08;
|
||||
|
||||
/**
|
||||
* 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 mixed $interestRate Interest rate per period
|
||||
* @param mixed $period Period for which we want to find the interest
|
||||
* @param mixed $numberOfPeriods Number of periods
|
||||
* @param mixed $presentValue Present Value
|
||||
* @param mixed $futureValue Future Value
|
||||
* @param mixed $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
|
||||
*
|
||||
* @return float|string
|
||||
*/
|
||||
public static function payment(
|
||||
$interestRate,
|
||||
$period,
|
||||
$numberOfPeriods,
|
||||
$presentValue,
|
||||
$futureValue = 0,
|
||||
$type = FinancialConstants::PAYMENT_END_OF_PERIOD
|
||||
) {
|
||||
$interestRate = Functions::flattenSingleValue($interestRate);
|
||||
$period = Functions::flattenSingleValue($period);
|
||||
$numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
|
||||
$presentValue = Functions::flattenSingleValue($presentValue);
|
||||
$futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
|
||||
$type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
|
||||
|
||||
try {
|
||||
$interestRate = CashFlowValidations::validateRate($interestRate);
|
||||
$period = CashFlowValidations::validateInt($period);
|
||||
$numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
|
||||
$presentValue = CashFlowValidations::validatePresentValue($presentValue);
|
||||
$futureValue = CashFlowValidations::validateFutureValue($futureValue);
|
||||
$type = CashFlowValidations::validatePeriodType($type);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Validate parameters
|
||||
if ($period <= 0 || $period > $numberOfPeriods) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
// Calculate
|
||||
$interestAndPrincipal = new InterestAndPrincipal(
|
||||
$interestRate,
|
||||
$period,
|
||||
$numberOfPeriods,
|
||||
$presentValue,
|
||||
$futureValue,
|
||||
$type
|
||||
);
|
||||
|
||||
return $interestAndPrincipal->interest();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param mixed $interestRate is the interest rate for the investment
|
||||
* @param mixed $period is the period to calculate the interest rate. It must be betweeen 1 and number_payments.
|
||||
* @param mixed $numberOfPeriods is the number of payments for the annuity
|
||||
* @param mixed $principleRemaining is the loan amount or present value of the payments
|
||||
*/
|
||||
public static function schedulePayment($interestRate, $period, $numberOfPeriods, $principleRemaining)
|
||||
{
|
||||
$interestRate = Functions::flattenSingleValue($interestRate);
|
||||
$period = Functions::flattenSingleValue($period);
|
||||
$numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
|
||||
$principleRemaining = Functions::flattenSingleValue($principleRemaining);
|
||||
|
||||
try {
|
||||
$interestRate = CashFlowValidations::validateRate($interestRate);
|
||||
$period = CashFlowValidations::validateInt($period);
|
||||
$numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
|
||||
$principleRemaining = CashFlowValidations::validateFloat($principleRemaining);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
// Validate parameters
|
||||
if ($period <= 0 || $period > $numberOfPeriods) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
// Return value
|
||||
$returnValue = 0;
|
||||
|
||||
// Calculate
|
||||
$principlePayment = ($principleRemaining * 1.0) / ($numberOfPeriods * 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 == $numberOfPeriods) {
|
||||
$returnValue = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
return $returnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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]]])
|
||||
*
|
||||
* @param mixed $numberOfPeriods The total number of payment periods in an annuity
|
||||
* @param mixed $payment 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 mixed $presentValue The present value - the total amount that a series of future payments is worth now
|
||||
* @param mixed $futureValue 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 mixed $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 mixed $guess Your guess for what the rate will be.
|
||||
* If you omit guess, it is assumed to be 10 percent.
|
||||
*
|
||||
* @return float|string
|
||||
*/
|
||||
public static function rate(
|
||||
$numberOfPeriods,
|
||||
$payment,
|
||||
$presentValue,
|
||||
$futureValue = 0.0,
|
||||
$type = FinancialConstants::PAYMENT_END_OF_PERIOD,
|
||||
$guess = 0.1
|
||||
) {
|
||||
$numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
|
||||
$payment = Functions::flattenSingleValue($payment);
|
||||
$presentValue = Functions::flattenSingleValue($presentValue);
|
||||
$futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
|
||||
$type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
|
||||
$guess = ($guess === null) ? 0.1 : Functions::flattenSingleValue($guess);
|
||||
|
||||
try {
|
||||
$numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
|
||||
$payment = CashFlowValidations::validateFloat($payment);
|
||||
$presentValue = CashFlowValidations::validatePresentValue($presentValue);
|
||||
$futureValue = CashFlowValidations::validateFutureValue($futureValue);
|
||||
$type = CashFlowValidations::validatePeriodType($type);
|
||||
$guess = CashFlowValidations::validateFloat($guess);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
$rate = $guess;
|
||||
// rest of code adapted from python/numpy
|
||||
$close = false;
|
||||
$iter = 0;
|
||||
while (!$close && $iter < self::FINANCIAL_MAX_ITERATIONS) {
|
||||
$nextdiff = self::rateNextGuess($rate, $numberOfPeriods, $payment, $presentValue, $futureValue, $type);
|
||||
if (!is_numeric($nextdiff)) {
|
||||
break;
|
||||
}
|
||||
$rate1 = $rate - $nextdiff;
|
||||
$close = abs($rate1 - $rate) < self::FINANCIAL_PRECISION;
|
||||
++$iter;
|
||||
$rate = $rate1;
|
||||
}
|
||||
|
||||
return $close ? $rate : Functions::NAN();
|
||||
}
|
||||
|
||||
private static function rateNextGuess($rate, $numberOfPeriods, $payment, $presentValue, $futureValue, $type)
|
||||
{
|
||||
if ($rate == 0.0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
$tt1 = ($rate + 1) ** $numberOfPeriods;
|
||||
$tt2 = ($rate + 1) ** ($numberOfPeriods - 1);
|
||||
$numerator = $futureValue + $tt1 * $presentValue + $payment * ($tt1 - 1) * ($rate * $type + 1) / $rate;
|
||||
$denominator = $numberOfPeriods * $tt2 * $presentValue - $payment * ($tt1 - 1)
|
||||
* ($rate * $type + 1) / ($rate * $rate) + $numberOfPeriods
|
||||
* $payment * $tt2 * ($rate * $type + 1) / $rate + $payment * ($tt1 - 1) * $type / $rate;
|
||||
if ($denominator == 0) {
|
||||
return Functions::NAN();
|
||||
}
|
||||
|
||||
return $numerator / $denominator;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\Constant\Periodic;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
|
||||
|
||||
class InterestAndPrincipal
|
||||
{
|
||||
protected $interest;
|
||||
|
||||
protected $principal;
|
||||
|
||||
public function __construct(
|
||||
float $rate = 0.0,
|
||||
int $period = 0,
|
||||
int $numberOfPeriods = 0,
|
||||
float $presentValue = 0,
|
||||
float $futureValue = 0,
|
||||
int $type = FinancialConstants::PAYMENT_END_OF_PERIOD
|
||||
) {
|
||||
$payment = Payments::annuity($rate, $numberOfPeriods, $presentValue, $futureValue, $type);
|
||||
$capital = $presentValue;
|
||||
$interest = 0.0;
|
||||
$principal = 0.0;
|
||||
for ($i = 1; $i <= $period; ++$i) {
|
||||
$interest = ($type === FinancialConstants::PAYMENT_BEGINNING_OF_PERIOD && $i == 1) ? 0 : -$capital * $rate;
|
||||
$principal = $payment - $interest;
|
||||
$capital += $principal;
|
||||
}
|
||||
|
||||
$this->interest = $interest;
|
||||
$this->principal = $principal;
|
||||
}
|
||||
|
||||
public function interest(): float
|
||||
{
|
||||
return $this->interest;
|
||||
}
|
||||
|
||||
public function principal(): float
|
||||
{
|
||||
return $this->principal;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue