281 changed files with 56351 additions and 0 deletions
@ -0,0 +1,226 @@ |
|||
<?php |
|||
|
|||
$finder = PhpCsFixer\Finder::create() |
|||
->exclude('vendor') |
|||
->in(__DIR__); |
|||
|
|||
$config = new PhpCsFixer\Config(); |
|||
$config |
|||
->setRiskyAllowed(true) |
|||
->setFinder($finder) |
|||
->setCacheFile(sys_get_temp_dir() . '/php-cs-fixer' . preg_replace('~\W~', '-', __DIR__)) |
|||
->setRules([ |
|||
'align_multiline_comment' => true, |
|||
'array_indentation' => true, |
|||
'array_syntax' => ['syntax' => 'short'], |
|||
'backtick_to_shell_exec' => true, |
|||
'binary_operator_spaces' => true, |
|||
'blank_line_after_namespace' => true, |
|||
'blank_line_after_opening_tag' => true, |
|||
'blank_line_before_statement' => true, |
|||
'braces' => true, |
|||
'cast_spaces' => true, |
|||
'class_attributes_separation' => ['elements' => ['method' => 'one', 'property' => 'one']], // const are often grouped with other related const |
|||
'class_definition' => true, |
|||
'class_keyword_remove' => false, // ::class keyword gives us better support in IDE |
|||
'combine_consecutive_issets' => true, |
|||
'combine_consecutive_unsets' => true, |
|||
'combine_nested_dirname' => true, |
|||
'comment_to_phpdoc' => true, |
|||
'compact_nullable_typehint' => true, |
|||
'concat_space' => ['spacing' => 'one'], |
|||
'constant_case' => true, |
|||
'date_time_immutable' => false, // Break our unit tests |
|||
'declare_equal_normalize' => true, |
|||
'declare_strict_types' => false, // Too early to adopt strict types |
|||
'dir_constant' => true, |
|||
'doctrine_annotation_array_assignment' => true, |
|||
'doctrine_annotation_braces' => true, |
|||
'doctrine_annotation_indentation' => true, |
|||
'doctrine_annotation_spaces' => true, |
|||
'elseif' => true, |
|||
'encoding' => true, |
|||
'ereg_to_preg' => true, |
|||
'escape_implicit_backslashes' => true, |
|||
'explicit_indirect_variable' => false, // I feel it makes the code actually harder to read |
|||
'explicit_string_variable' => false, // I feel it makes the code actually harder to read |
|||
'final_class' => false, // We need non-final classes |
|||
'final_internal_class' => true, |
|||
'final_public_method_for_abstract_class' => false, // We need non-final methods |
|||
'self_static_accessor' => true, |
|||
'fopen_flag_order' => true, |
|||
'fopen_flags' => true, |
|||
'full_opening_tag' => true, |
|||
'fully_qualified_strict_types' => true, |
|||
'function_declaration' => true, |
|||
'function_to_constant' => true, |
|||
'function_typehint_space' => true, |
|||
'general_phpdoc_annotation_remove' => ['annotations' => ['access', 'category', 'copyright', 'throws']], |
|||
'global_namespace_import' => true, |
|||
'header_comment' => false, // We don't use common header in all our files |
|||
'heredoc_indentation' => false, // Requires PHP >= 7.3 |
|||
'heredoc_to_nowdoc' => false, // Not sure about this one |
|||
'implode_call' => true, |
|||
'include' => true, |
|||
'increment_style' => true, |
|||
'indentation_type' => true, |
|||
'is_null' => true, |
|||
'line_ending' => true, |
|||
'linebreak_after_opening_tag' => true, |
|||
'list_syntax' => ['syntax' => 'short'], |
|||
'logical_operators' => true, |
|||
'lowercase_cast' => true, |
|||
'lowercase_keywords' => true, |
|||
'lowercase_static_reference' => true, |
|||
'magic_constant_casing' => true, |
|||
'magic_method_casing' => true, |
|||
'mb_str_functions' => false, // No, too dangerous to change that |
|||
'method_argument_space' => true, |
|||
'method_chaining_indentation' => true, |
|||
'modernize_types_casting' => true, |
|||
'multiline_comment_opening_closing' => true, |
|||
'multiline_whitespace_before_semicolons' => true, |
|||
'native_constant_invocation' => false, // Micro optimization that look messy |
|||
'native_function_casing' => true, |
|||
'native_function_invocation' => false, // I suppose this would be best, but I am still unconvinced about the visual aspect of it |
|||
'native_function_type_declaration_casing' => true, |
|||
'new_with_braces' => true, |
|||
'no_alias_functions' => true, |
|||
'no_alternative_syntax' => true, |
|||
'no_binary_string' => true, |
|||
'no_blank_lines_after_class_opening' => true, |
|||
'no_blank_lines_after_phpdoc' => true, |
|||
'no_blank_lines_before_namespace' => false, // we want 1 blank line before namespace |
|||
'no_break_comment' => true, |
|||
'no_closing_tag' => true, |
|||
'no_empty_comment' => true, |
|||
'no_empty_phpdoc' => true, |
|||
'no_empty_statement' => true, |
|||
'no_extra_blank_lines' => true, |
|||
'no_homoglyph_names' => true, |
|||
'no_leading_import_slash' => true, |
|||
'no_leading_namespace_whitespace' => true, |
|||
'no_mixed_echo_print' => true, |
|||
'no_multiline_whitespace_around_double_arrow' => true, |
|||
'no_null_property_initialization' => true, |
|||
'no_php4_constructor' => true, |
|||
'no_short_bool_cast' => true, |
|||
'echo_tag_syntax' => ['format' => 'long'], |
|||
'no_singleline_whitespace_before_semicolons' => true, |
|||
'no_spaces_after_function_name' => true, |
|||
'no_spaces_around_offset' => true, |
|||
'no_spaces_inside_parenthesis' => true, |
|||
'no_superfluous_elseif' => false, // Might be risky on a huge code base |
|||
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true], |
|||
'no_trailing_comma_in_list_call' => true, |
|||
'no_trailing_comma_in_singleline_array' => true, |
|||
'no_trailing_whitespace' => true, |
|||
'no_trailing_whitespace_in_comment' => true, |
|||
'no_unneeded_control_parentheses' => true, |
|||
'no_unneeded_curly_braces' => true, |
|||
'no_unneeded_final_method' => true, |
|||
'no_unreachable_default_argument_value' => true, |
|||
'no_unset_cast' => true, |
|||
'no_unset_on_property' => true, |
|||
'no_unused_imports' => true, |
|||
'no_useless_else' => true, |
|||
'no_useless_return' => true, |
|||
'no_whitespace_before_comma_in_array' => true, |
|||
'no_whitespace_in_blank_line' => true, |
|||
'non_printable_character' => true, |
|||
'normalize_index_brace' => true, |
|||
'not_operator_with_space' => false, // No we prefer to keep '!' without spaces |
|||
'not_operator_with_successor_space' => false, // idem |
|||
'nullable_type_declaration_for_default_null_value' => true, |
|||
'object_operator_without_whitespace' => true, |
|||
'ordered_class_elements' => false, // We prefer to keep some freedom |
|||
'ordered_imports' => true, |
|||
'ordered_interfaces' => true, |
|||
'php_unit_construct' => true, |
|||
'php_unit_dedicate_assert' => true, |
|||
'php_unit_dedicate_assert_internal_type' => true, |
|||
'php_unit_expectation' => true, |
|||
'php_unit_fqcn_annotation' => true, |
|||
'php_unit_internal_class' => false, // Because tests are excluded from package |
|||
'php_unit_method_casing' => true, |
|||
'php_unit_mock' => true, |
|||
'php_unit_mock_short_will_return' => true, |
|||
'php_unit_namespaced' => true, |
|||
'php_unit_no_expectation_annotation' => true, |
|||
'phpdoc_order_by_value' => ['annotations' => ['covers']], |
|||
'php_unit_set_up_tear_down_visibility' => true, |
|||
'php_unit_size_class' => false, // That seems extra work to maintain for little benefits |
|||
'php_unit_strict' => false, // We sometime actually need assertEquals |
|||
'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' => false, // Don't add things that bring no value |
|||
'phpdoc_align' => false, // Waste of time |
|||
'phpdoc_annotation_without_dot' => true, |
|||
'phpdoc_indent' => true, |
|||
//'phpdoc_inline_tag' => true, |
|||
'phpdoc_line_span' => false, // Unfortunately our old comments turn even uglier with this |
|||
'phpdoc_no_access' => true, |
|||
'phpdoc_no_alias_tag' => true, |
|||
'phpdoc_no_empty_return' => true, |
|||
'phpdoc_no_package' => true, |
|||
'phpdoc_no_useless_inheritdoc' => true, |
|||
'phpdoc_order' => true, |
|||
'phpdoc_return_self_reference' => true, |
|||
'phpdoc_scalar' => true, |
|||
'phpdoc_separation' => true, |
|||
'phpdoc_single_line_var_spacing' => true, |
|||
'phpdoc_summary' => true, |
|||
'phpdoc_to_comment' => true, |
|||
'phpdoc_to_param_type' => false, // Because experimental, but interesting for one shot use |
|||
'phpdoc_to_return_type' => false, // idem |
|||
'phpdoc_trim' => true, |
|||
'phpdoc_trim_consecutive_blank_line_separation' => true, |
|||
'phpdoc_types' => true, |
|||
'phpdoc_types_order' => true, |
|||
'phpdoc_var_annotation_correct_order' => true, |
|||
'phpdoc_var_without_name' => true, |
|||
'pow_to_exponentiation' => true, |
|||
'protected_to_private' => true, |
|||
//'psr0' => true, |
|||
//'psr4' => true, |
|||
'random_api_migration' => true, |
|||
'return_assignment' => false, // Sometimes useful for clarity or debug |
|||
'return_type_declaration' => true, |
|||
'self_accessor' => true, |
|||
'self_static_accessor' => true, |
|||
'semicolon_after_instruction' => false, // Buggy in `samples/index.php` |
|||
'set_type_to_cast' => true, |
|||
'short_scalar_cast' => true, |
|||
'simple_to_complex_string_variable' => false, // Would differ from TypeScript without obvious advantages |
|||
'simplified_null_return' => false, // Even if technically correct we prefer to be explicit |
|||
'single_blank_line_at_eof' => true, |
|||
'single_blank_line_before_namespace' => true, |
|||
'single_class_element_per_statement' => true, |
|||
'single_import_per_statement' => true, |
|||
'single_line_after_imports' => true, |
|||
'single_line_comment_style' => true, |
|||
'single_line_throw' => false, // I don't see any reason for having a special case for Exception |
|||
'single_quote' => true, |
|||
'single_trait_insert_per_statement' => true, |
|||
'space_after_semicolon' => true, |
|||
'standardize_increment' => true, |
|||
'standardize_not_equals' => true, |
|||
'static_lambda' => false, // Risky if we can't guarantee nobody use `bindTo()` |
|||
'strict_comparison' => false, // No, too dangerous to change that |
|||
'strict_param' => false, // No, too dangerous to change that |
|||
'string_line_ending' => true, |
|||
'switch_case_semicolon_to_colon' => true, |
|||
'switch_case_space' => true, |
|||
'ternary_operator_spaces' => true, |
|||
'ternary_to_null_coalescing' => true, |
|||
'trailing_comma_in_multiline' => true, |
|||
'trim_array_spaces' => true, |
|||
'unary_operator_spaces' => true, |
|||
'visibility_required' => ['elements' => ['property', 'method']], // not const |
|||
'void_return' => true, |
|||
'whitespace_after_comma_in_array' => true, |
|||
'yoda_style' => false, |
|||
]); |
|||
|
|||
return $config; |
|||
@ -0,0 +1,22 @@ |
|||
<?xml version="1.0"?> |
|||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" |
|||
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> |
|||
|
|||
<file>samples</file> |
|||
<file>src</file> |
|||
<file>tests</file> |
|||
|
|||
<exclude-pattern>samples/Header.php</exclude-pattern> |
|||
<exclude-pattern>*/tests/Core/*/*Test\.(inc|css|js)$</exclude-pattern> |
|||
|
|||
<arg name="report-width" value="200"/> |
|||
<arg name="parallel" value="80"/> |
|||
<arg name="cache" value="/tmp/.phpspreadsheet.phpcs-cache"/> |
|||
<arg name="colors"/> |
|||
<arg value="np"/> |
|||
|
|||
<!-- Include the whole PSR12 standard --> |
|||
<rule ref="PSR12"> |
|||
<exclude name="PSR2.Methods.MethodDeclaration.Underscore"/> |
|||
</rule> |
|||
</ruleset> |
|||
@ -0,0 +1,958 @@ |
|||
# Changelog |
|||
|
|||
All notable changes to this project will be documented in this file. |
|||
|
|||
The format is based on [Keep a Changelog](https://keepachangelog.com) |
|||
and this project adheres to [Semantic Versioning](https://semver.org). |
|||
|
|||
## 1.22.0 - 2022-02-18 |
|||
|
|||
### Added |
|||
|
|||
- Namespacing phase 2 - styles. |
|||
[PR #2471](https://github.com/PHPOffice/PhpSpreadsheet/pull/2471) |
|||
|
|||
- Improved support for passing of array arguments to Excel function implementations to return array results (where appropriate). [Issue #2551](https://github.com/PHPOffice/PhpSpreadsheet/issues/2551) |
|||
|
|||
This is the first stage in an ongoing process of adding array support to all appropriate function implementations, |
|||
- Support for the Excel365 Math/Trig SEQUENCE() function [PR #2536](https://github.com/PHPOffice/PhpSpreadsheet/pull/2536) |
|||
- Support for the Excel365 Math/Trig RANDARRAY() function [PR #2540](https://github.com/PHPOffice/PhpSpreadsheet/pull/2540) |
|||
|
|||
Note that the Spill Operator is not yet supported in the Calculation Engine; but this can still be useful for defining array constants. |
|||
- Improved support for Conditional Formatting Rules [PR #2491](https://github.com/PHPOffice/PhpSpreadsheet/pull/2491) |
|||
- Provide support for a wider range of Conditional Formatting Rules for Xlsx Reader/Writer: |
|||
- Cells Containing (cellIs) |
|||
- Specific Text (containing, notContaining, beginsWith, endsWith) |
|||
- Dates Occurring (all supported timePeriods) |
|||
- Blanks/NoBlanks |
|||
- Errors/NoErrors |
|||
- Duplicates/Unique |
|||
- Expression |
|||
- Provision of CF Wizards (for all the above listed rule types) to help create/modify CF Rules without having to manage all the combinations of types/operators, and the complexities of formula expressions, or the text/timePeriod attributes. |
|||
|
|||
See [documentation](https://phpspreadsheet.readthedocs.io/en/latest/topics/conditional-formatting/) for details |
|||
|
|||
- Full support of the above CF Rules for the Xlsx Reader and Writer; even when the file being loaded has CF rules listed in the `<extLst><ext><ConditionalFormattings>` element for the worksheet rather than the `<ConditionalFormatting>` element. |
|||
- Provision of a CellMatcher to identify if rules are matched for a cell, and which matching style will be applied. |
|||
- Improved documentation and examples, covering all supported CF rule types. |
|||
- Add support for one digit decimals (FORMAT_NUMBER_0, FORMAT_PERCENTAGE_0). [PR #2525](https://github.com/PHPOffice/PhpSpreadsheet/pull/2525) |
|||
- Initial work enabling Excel function implementations for handling arrays as arguments when used in "array formulae" [#2562](https://github.com/PHPOffice/PhpSpreadsheet/issues/2562) |
|||
- Enable most of the Date/Time functions to accept array arguments [#2573](https://github.com/PHPOffice/PhpSpreadsheet/issues/2573) |
|||
- Array ready functions - Text, Math/Trig, Statistical, Engineering and Logical [#2580](https://github.com/PHPOffice/PhpSpreadsheet/issues/2580) |
|||
|
|||
### Changed |
|||
|
|||
- Additional Russian translations for Excel Functions (courtesy of aleks-samurai). |
|||
- Improved code coverage for NumberFormat. [PR #2556](https://github.com/PHPOffice/PhpSpreadsheet/pull/2556) |
|||
- Extract some methods from the Calculation Engine into dedicated classes [#2537](https://github.com/PHPOffice/PhpSpreadsheet/issues/2537) |
|||
- Eliminate calls to `flattenSingleValue()` that are no longer required when we're checking for array values as arguments [#2590](https://github.com/PHPOffice/PhpSpreadsheet/issues/2590) |
|||
|
|||
### Deprecated |
|||
|
|||
- Nothing |
|||
|
|||
### Removed |
|||
|
|||
- Nothing |
|||
|
|||
### Fixed |
|||
|
|||
- Fixed `ReferenceHelper@insertNewBefore` behavior when removing column before last column with null value |
|||
[PR #2541](https://github.com/PHPOffice/PhpSpreadsheet/pull/2541) |
|||
- Fix bug with `DOLLARDE()` and `DOLLARFR()` functions when the dollar value is negative [Issue #2578](https://github.com/PHPOffice/PhpSpreadsheet/issues/2578) [PR #2579](https://github.com/PHPOffice/PhpSpreadsheet/pull/2579) |
|||
- Fix partial function name matching when translating formulae from Russian to English [Issue #2533](https://github.com/PHPOffice/PhpSpreadsheet/issues/2533) [PR #2534](https://github.com/PHPOffice/PhpSpreadsheet/pull/2534) |
|||
- Various bugs related to Conditional Formatting Rules, and errors in the Xlsx Writer for Conditional Formatting [PR #2491](https://github.com/PHPOffice/PhpSpreadsheet/pull/2491) |
|||
- Xlsx Reader merge range fixes. |
|||
[Issue #2501](https://github.com/PHPOffice/PhpSpreadsheet/issues/2501) |
|||
[PR #2504](https://github.com/PHPOffice/PhpSpreadsheet/pull/2504) |
|||
- Handle explicit "date" type for Cell in Xlsx Reader. |
|||
[Issue #2373](https://github.com/PHPOffice/PhpSpreadsheet/issues/2373) |
|||
[PR #2485](https://github.com/PHPOffice/PhpSpreadsheet/pull/2485) |
|||
- Recalibrate Row/Column Dimensions after removeRow/Column. |
|||
[Issue #2442](https://github.com/PHPOffice/PhpSpreadsheet/issues/2442) |
|||
[PR #2486](https://github.com/PHPOffice/PhpSpreadsheet/pull/2486) |
|||
- Refinement for XIRR. |
|||
[Issue #2469](https://github.com/PHPOffice/PhpSpreadsheet/issues/2469) |
|||
[PR #2487](https://github.com/PHPOffice/PhpSpreadsheet/pull/2487) |
|||
- Xlsx Reader handle cell with non-null explicit type but null value. |
|||
[Issue #2488](https://github.com/PHPOffice/PhpSpreadsheet/issues/2488) |
|||
[PR #2489](https://github.com/PHPOffice/PhpSpreadsheet/pull/2489) |
|||
- Xlsx Reader fix height and width for oneCellAnchorDrawings. |
|||
[PR #2492](https://github.com/PHPOffice/PhpSpreadsheet/pull/2492) |
|||
- Fix rounding error in NumberFormat::NUMBER_PERCENTAGE, NumberFormat::NUMBER_PERCENTAGE_00. [PR #2555](https://github.com/PHPOffice/PhpSpreadsheet/pull/2555) |
|||
- Don't treat thumbnail file as xml. |
|||
[Issue #2516](https://github.com/PHPOffice/PhpSpreadsheet/issues/2516) |
|||
[PR #2517](https://github.com/PHPOffice/PhpSpreadsheet/pull/2517) |
|||
- Eliminating Xlsx Reader warning when no sz tag for RichText. |
|||
[Issue #2542](https://github.com/PHPOffice/PhpSpreadsheet/issues/2542) |
|||
[PR #2550](https://github.com/PHPOffice/PhpSpreadsheet/pull/2550) |
|||
- Fix Xlsx/Xls Writer handling of inline strings. |
|||
[Issue #353](https://github.com/PHPOffice/PhpSpreadsheet/issues/353) |
|||
[PR #2569](https://github.com/PHPOffice/PhpSpreadsheet/pull/2569) |
|||
- Richtext colors were not being read correctly after namespace change [#2458](https://github.com/PHPOffice/PhpSpreadsheet/issues/2458) |
|||
- Fix discrepancy between the way markdown tables are rendered in ReadTheDocs and in PHPStorm [#2520](https://github.com/PHPOffice/PhpSpreadsheet/issues/2520) |
|||
- Update Russian Functions Text File [#2557](https://github.com/PHPOffice/PhpSpreadsheet/issues/2557) |
|||
- Fix documentation, instantiation example [#2564](https://github.com/PHPOffice/PhpSpreadsheet/issues/2564) |
|||
|
|||
|
|||
## 1.21.0 - 2022-01-06 |
|||
|
|||
### Added |
|||
|
|||
- Ability to add a picture to the background of the comment. Supports four image formats: png, jpeg, gif, bmp. New `Comment::setSizeAsBackgroundImage()` to change the size of a comment to the size of a background image. [Issue #1547](https://github.com/PHPOffice/PhpSpreadsheet/issues/1547) [PR #2422](https://github.com/PHPOffice/PhpSpreadsheet/pull/2422) |
|||
- Ability to set default paper size and orientation [PR #2410](https://github.com/PHPOffice/PhpSpreadsheet/pull/2410) |
|||
- Ability to extend AutoFilter to Maximum Row [PR #2414](https://github.com/PHPOffice/PhpSpreadsheet/pull/2414) |
|||
|
|||
### Changed |
|||
|
|||
- Xlsx Writer will evaluate AutoFilter only if it is as yet unevaluated, or has changed since it was last evaluated [PR #2414](https://github.com/PHPOffice/PhpSpreadsheet/pull/2414) |
|||
|
|||
### Deprecated |
|||
|
|||
- Nothing |
|||
|
|||
### Removed |
|||
|
|||
- Nothing |
|||
|
|||
### Fixed |
|||
|
|||
- Rounding in `NumberFormatter` [Issue #2385](https://github.com/PHPOffice/PhpSpreadsheet/issues/2385) [PR #2399](https://github.com/PHPOffice/PhpSpreadsheet/pull/2399) |
|||
- Support for themes [Issue #2075](https://github.com/PHPOffice/PhpSpreadsheet/issues/2075) [Issue #2387](https://github.com/PHPOffice/PhpSpreadsheet/issues/2387) [PR #2403](https://github.com/PHPOffice/PhpSpreadsheet/pull/2403) |
|||
- Read spreadsheet with `#` in name [Issue #2405](https://github.com/PHPOffice/PhpSpreadsheet/issues/2405) [PR #2409](https://github.com/PHPOffice/PhpSpreadsheet/pull/2409) |
|||
- Improve PDF support for page size and orientation [Issue #1691](https://github.com/PHPOffice/PhpSpreadsheet/issues/1691) [PR #2410](https://github.com/PHPOffice/PhpSpreadsheet/pull/2410) |
|||
- Wildcard handling issues in text match [Issue #2430](https://github.com/PHPOffice/PhpSpreadsheet/issues/2430) [PR #2431](https://github.com/PHPOffice/PhpSpreadsheet/pull/2431) |
|||
- Respect DataType in `insertNewBefore` [PR #2433](https://github.com/PHPOffice/PhpSpreadsheet/pull/2433) |
|||
- Handle rows explicitly hidden after AutoFilter [Issue #1641](https://github.com/PHPOffice/PhpSpreadsheet/issues/1641) [PR #2414](https://github.com/PHPOffice/PhpSpreadsheet/pull/2414) |
|||
- Special characters in image file name [Issue #1470](https://github.com/PHPOffice/PhpSpreadsheet/issues/1470) [Issue #2415](https://github.com/PHPOffice/PhpSpreadsheet/issues/2415) [PR #2416](https://github.com/PHPOffice/PhpSpreadsheet/pull/2416) |
|||
- Mpdf with very many styles [Issue #2432](https://github.com/PHPOffice/PhpSpreadsheet/issues/2432) [PR #2434](https://github.com/PHPOffice/PhpSpreadsheet/pull/2434) |
|||
- Name clashes between parsed and unparsed drawings [Issue #1767](https://github.com/PHPOffice/PhpSpreadsheet/issues/1767) [Issue #2396](https://github.com/PHPOffice/PhpSpreadsheet/issues/2396) [PR #2423](https://github.com/PHPOffice/PhpSpreadsheet/pull/2423) |
|||
- Fill pattern start and end colors [Issue #2441](https://github.com/PHPOffice/PhpSpreadsheet/issues/2441) [PR #2444](https://github.com/PHPOffice/PhpSpreadsheet/pull/2444) |
|||
- General style specified in wrong case [Issue #2450](https://github.com/PHPOffice/PhpSpreadsheet/issues/2450) [PR #2451](https://github.com/PHPOffice/PhpSpreadsheet/pull/2451) |
|||
- Null passed to `AutoFilter::setRange()` [Issue #2281](https://github.com/PHPOffice/PhpSpreadsheet/issues/2281) [PR #2454](https://github.com/PHPOffice/PhpSpreadsheet/pull/2454) |
|||
- Another undefined index in Xls reader (#2470) [Issue #2463](https://github.com/PHPOffice/PhpSpreadsheet/issues/2463) [PR #2470](https://github.com/PHPOffice/PhpSpreadsheet/pull/2470) |
|||
- Allow single-cell checks on conditional styles, even when the style is configured for a range of cells (#) [PR #2483](https://github.com/PHPOffice/PhpSpreadsheet/pull/2483) |
|||
|
|||
## 1.20.0 - 2021-11-23 |
|||
|
|||
### Added |
|||
|
|||
- Xlsx Writer Support for WMF Files [#2339](https://github.com/PHPOffice/PhpSpreadsheet/issues/2339) |
|||
- Use standard temporary file for internal use of HTMLPurifier [#2383](https://github.com/PHPOffice/PhpSpreadsheet/issues/2383) |
|||
|
|||
### Changed |
|||
|
|||
- Drop support for PHP 7.2, according to https://phpspreadsheet.readthedocs.io/en/latest/#php-version-support |
|||
- Use native typing for objects that were already documented as such |
|||
|
|||
### Deprecated |
|||
|
|||
- Nothing |
|||
|
|||
### Removed |
|||
|
|||
- Nothing |
|||
|
|||
### Fixed |
|||
|
|||
- Fixed null conversation for strToUpper [#2292](https://github.com/PHPOffice/PhpSpreadsheet/issues/2292) |
|||
- Fixed Trying to access array offset on value of type null (Xls Reader) [#2315](https://github.com/PHPOffice/PhpSpreadsheet/issues/2315) |
|||
- Don't corrupt XLSX files containing data validation [#2377](https://github.com/PHPOffice/PhpSpreadsheet/issues/2377) |
|||
- Non-fixed cells were not updated if shared formula has a fixed cell [#2354](https://github.com/PHPOffice/PhpSpreadsheet/issues/2354) |
|||
- Declare key of generic ArrayObject |
|||
- CSV reader better support for boolean values [#2374](https://github.com/PHPOffice/PhpSpreadsheet/pull/2374) |
|||
- Some ZIP file could not be read [#2376](https://github.com/PHPOffice/PhpSpreadsheet/pull/2376) |
|||
- Fix regression were hyperlinks could not be read [#2391](https://github.com/PHPOffice/PhpSpreadsheet/pull/2391) |
|||
- AutoFilter Improvements [#2393](https://github.com/PHPOffice/PhpSpreadsheet/pull/2393) |
|||
- Don't corrupt file when using chart with fill color [#589](https://github.com/PHPOffice/PhpSpreadsheet/pull/589) |
|||
- Restore imperfect array formula values in xlsx writer [#2343](https://github.com/PHPOffice/PhpSpreadsheet/pull/2343) |
|||
- Restore explicit list of changes to PHPExcel migration document [#1546](https://github.com/PHPOffice/PhpSpreadsheet/issues/1546) |
|||
|
|||
## 1.19.0 - 2021-10-31 |
|||
|
|||
### Added |
|||
|
|||
- Ability to set style on named range, and validate input to setSelectedCells [Issue #2279](https://github.com/PHPOffice/PhpSpreadsheet/issues/2279) [PR #2280](https://github.com/PHPOffice/PhpSpreadsheet/pull/2280) |
|||
- Process comments in Sylk file [Issue #2276](https://github.com/PHPOffice/PhpSpreadsheet/issues/2276) [PR #2277](https://github.com/PHPOffice/PhpSpreadsheet/pull/2277) |
|||
- Addition of Custom Properties to Ods Writer, and 32-bit-safe timestamps for Document Properties [PR #2113](https://github.com/PHPOffice/PhpSpreadsheet/pull/2113) |
|||
- Added callback to CSV reader to set user-specified defaults for various properties (especially for escape which has a poor PHP-inherited default of backslash which does not correspond with Excel) [PR #2103](https://github.com/PHPOffice/PhpSpreadsheet/pull/2103) |
|||
- Phase 1 of better namespace handling for Xlsx, resolving many open issues [PR #2173](https://github.com/PHPOffice/PhpSpreadsheet/pull/2173) [PR #2204](https://github.com/PHPOffice/PhpSpreadsheet/pull/2204) [PR #2303](https://github.com/PHPOffice/PhpSpreadsheet/pull/2303) |
|||
- Add ability to extract images if source is a URL [Issue #1997](https://github.com/PHPOffice/PhpSpreadsheet/issues/1997) [PR #2072](https://github.com/PHPOffice/PhpSpreadsheet/pull/2072) |
|||
- Support for passing flags in the Reader `load()` and Writer `save()`methods, and through the IOFactory, to set behaviours [PR #2136](https://github.com/PHPOffice/PhpSpreadsheet/pull/2136) |
|||
- See [documentation](https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/#readerwriter-flags) for details |
|||
- More flexibility in the StringValueBinder to determine what datatypes should be treated as strings [PR #2138](https://github.com/PHPOffice/PhpSpreadsheet/pull/2138) |
|||
- Helper class for conversion between css size Units of measure (`px`, `pt`, `pc`, `in`, `cm`, `mm`) [PR #2152](https://github.com/PHPOffice/PhpSpreadsheet/issues/2145) |
|||
- Allow Row height and Column Width to be set using different units of measure (`px`, `pt`, `pc`, `in`, `cm`, `mm`), rather than only in points or MS Excel column width units [PR #2152](https://github.com/PHPOffice/PhpSpreadsheet/issues/2145) |
|||
- Ability to stream to an Amazon S3 bucket [Issue #2249](https://github.com/PHPOffice/PhpSpreadsheet/issues/2249) |
|||
- Provided a Size Helper class to validate size values (pt, px, em) [PR #1694](https://github.com/PHPOffice/PhpSpreadsheet/pull/1694) |
|||
|
|||
### Changed |
|||
|
|||
- Nothing. |
|||
|
|||
### Deprecated |
|||
|
|||
- PHP 8.1 will deprecate auto_detect_line_endings. As a result of this change, Csv Reader using some release after PHP8.1 will no longer be able to handle a Csv with Mac line endings. |
|||
|
|||
### Removed |
|||
|
|||
- Nothing. |
|||
|
|||
### Fixed |
|||
|
|||
- Unexpected format in Xlsx Timestamp [Issue #2331](https://github.com/PHPOffice/PhpSpreadsheet/issues/2331) [PR #2332](https://github.com/PHPOffice/PhpSpreadsheet/pull/2332) |
|||
- Corrections for HLOOKUP [Issue #2123](https://github.com/PHPOffice/PhpSpreadsheet/issues/2123) [PR #2330](https://github.com/PHPOffice/PhpSpreadsheet/pull/2330) |
|||
- Corrections for Xlsx Read Comments [Issue #2316](https://github.com/PHPOffice/PhpSpreadsheet/issues/2316) [PR #2329](https://github.com/PHPOffice/PhpSpreadsheet/pull/2329) |
|||
- Lowercase Calibri font names [Issue #2273](https://github.com/PHPOffice/PhpSpreadsheet/issues/2273) [PR #2325](https://github.com/PHPOffice/PhpSpreadsheet/pull/2325) |
|||
- isFormula Referencing Sheet with Space in Title [Issue #2304](https://github.com/PHPOffice/PhpSpreadsheet/issues/2304) [PR #2306](https://github.com/PHPOffice/PhpSpreadsheet/pull/2306) |
|||
- Xls Reader Fatal Error due to Undefined Offset [Issue #1114](https://github.com/PHPOffice/PhpSpreadsheet/issues/1114) [PR #2308](https://github.com/PHPOffice/PhpSpreadsheet/pull/2308) |
|||
- Permit Csv Reader delimiter to be set to null [Issue #2287](https://github.com/PHPOffice/PhpSpreadsheet/issues/2287) [PR #2288](https://github.com/PHPOffice/PhpSpreadsheet/pull/2288) |
|||
- Csv Reader did not handle booleans correctly [PR #2232](https://github.com/PHPOffice/PhpSpreadsheet/pull/2232) |
|||
- Problems when deleting sheet with local defined name [Issue #2266](https://github.com/PHPOffice/PhpSpreadsheet/issues/2266) [PR #2284](https://github.com/PHPOffice/PhpSpreadsheet/pull/2284) |
|||
- Worksheet passwords were not always handled correctly [Issue #1897](https://github.com/PHPOffice/PhpSpreadsheet/issues/1897) [PR #2197](https://github.com/PHPOffice/PhpSpreadsheet/pull/2197) |
|||
- Gnumeric Reader will now distinguish between Created and Modified timestamp [PR #2133](https://github.com/PHPOffice/PhpSpreadsheet/pull/2133) |
|||
- Xls Reader will now handle MACCENTRALEUROPE with or without hyphen [Issue #549](https://github.com/PHPOffice/PhpSpreadsheet/issues/549) [PR #2213](https://github.com/PHPOffice/PhpSpreadsheet/pull/2213) |
|||
- Tweaks to input file validation [Issue #1718](https://github.com/PHPOffice/PhpSpreadsheet/issues/1718) [PR #2217](https://github.com/PHPOffice/PhpSpreadsheet/pull/2217) |
|||
- Html Reader did not handle comments correctly [Issue #2234](https://github.com/PHPOffice/PhpSpreadsheet/issues/2234) [PR #2235](https://github.com/PHPOffice/PhpSpreadsheet/pull/2235) |
|||
- Apache OpenOffice Uses Unexpected Case for General format [Issue #2239](https://github.com/PHPOffice/PhpSpreadsheet/issues/2239) [PR #2242](https://github.com/PHPOffice/PhpSpreadsheet/pull/2242) |
|||
- Problems with fraction formatting [Issue #2253](https://github.com/PHPOffice/PhpSpreadsheet/issues/2253) [PR #2254](https://github.com/PHPOffice/PhpSpreadsheet/pull/2254) |
|||
- Xlsx Reader had problems reading file with no styles.xml or empty styles.xml [Issue #2246](https://github.com/PHPOffice/PhpSpreadsheet/issues/2246) [PR #2247](https://github.com/PHPOffice/PhpSpreadsheet/pull/2247) |
|||
- Xlsx Reader did not read Data Validation flags correctly [Issue #2224](https://github.com/PHPOffice/PhpSpreadsheet/issues/2224) [PR #2225](https://github.com/PHPOffice/PhpSpreadsheet/pull/2225) |
|||
- Better handling of empty arguments in Calculation engine [PR #2143](https://github.com/PHPOffice/PhpSpreadsheet/pull/2143) |
|||
- Many fixes for Autofilter [Issue #2216](https://github.com/PHPOffice/PhpSpreadsheet/issues/2216) [PR #2141](https://github.com/PHPOffice/PhpSpreadsheet/pull/2141) [PR #2162](https://github.com/PHPOffice/PhpSpreadsheet/pull/2162) [PR #2218](https://github.com/PHPOffice/PhpSpreadsheet/pull/2218) |
|||
- Locale generator will now use Unix line endings even on Windows [Issue #2172](https://github.com/PHPOffice/PhpSpreadsheet/issues/2172) [PR #2174](https://github.com/PHPOffice/PhpSpreadsheet/pull/2174) |
|||
- Support differences in implementation of Text functions between Excel/Ods/Gnumeric [PR #2151](https://github.com/PHPOffice/PhpSpreadsheet/pull/2151) |
|||
- Fixes to places where PHP8.1 enforces new or previously unenforced restrictions [PR #2137](https://github.com/PHPOffice/PhpSpreadsheet/pull/2137) [PR #2191](https://github.com/PHPOffice/PhpSpreadsheet/pull/2191) [PR #2231](https://github.com/PHPOffice/PhpSpreadsheet/pull/2231) |
|||
- Clone for HashTable was incorrect [PR #2130](https://github.com/PHPOffice/PhpSpreadsheet/pull/2130) |
|||
- Xlsx Reader was not evaluating Document Security Lock correctly [PR #2128](https://github.com/PHPOffice/PhpSpreadsheet/pull/2128) |
|||
- Error in COUPNCD handling end of month [Issue #2116](https://github.com/PHPOffice/PhpSpreadsheet/issues/2116) [PR #2119](https://github.com/PHPOffice/PhpSpreadsheet/pull/2119) |
|||
- Xls Writer Parser did not handle concatenation operator correctly [PR #2080](https://github.com/PHPOffice/PhpSpreadsheet/pull/2080) |
|||
- Xlsx Writer did not handle boolean false correctly [Issue #2082](https://github.com/PHPOffice/PhpSpreadsheet/issues/2082) [PR #2087](https://github.com/PHPOffice/PhpSpreadsheet/pull/2087) |
|||
- SUM needs to treat invalid strings differently depending on whether they come from a cell or are used as literals [Issue #2042](https://github.com/PHPOffice/PhpSpreadsheet/issues/2042) [PR #2045](https://github.com/PHPOffice/PhpSpreadsheet/pull/2045) |
|||
- Html reader could have set illegal coordinates when dealing with embedded tables [Issue #2029](https://github.com/PHPOffice/PhpSpreadsheet/issues/2029) [PR #2032](https://github.com/PHPOffice/PhpSpreadsheet/pull/2032) |
|||
- Documentation for printing gridlines was wrong [PR #2188](https://github.com/PHPOffice/PhpSpreadsheet/pull/2188) |
|||
- Return Value Error - DatabaseAbstruct::buildQuery() return null but must be string [Issue #2158](https://github.com/PHPOffice/PhpSpreadsheet/issues/2158) [PR #2160](https://github.com/PHPOffice/PhpSpreadsheet/pull/2160) |
|||
- Xlsx reader not recognize data validations that references another sheet [Issue #1432](https://github.com/PHPOffice/PhpSpreadsheet/issues/1432) [Issue #2149](https://github.com/PHPOffice/PhpSpreadsheet/issues/2149) [PR #2150](https://github.com/PHPOffice/PhpSpreadsheet/pull/2150) [PR #2265](https://github.com/PHPOffice/PhpSpreadsheet/pull/2265) |
|||
- Don't calculate cell width for autosize columns if a cell contains a null or empty string value [Issue #2165](https://github.com/PHPOffice/PhpSpreadsheet/issues/2165) [PR #2167](https://github.com/PHPOffice/PhpSpreadsheet/pull/2167) |
|||
- Allow negative interest rate values in a number of the Financial functions (`PPMT()`, `PMT()`, `FV()`, `PV()`, `NPER()`, etc) [Issue #2163](https://github.com/PHPOffice/PhpSpreadsheet/issues/2163) [PR #2164](https://github.com/PHPOffice/PhpSpreadsheet/pull/2164) |
|||
- Xls Reader changing grey background to black in Excel template [Issue #2147](https://github.com/PHPOffice/PhpSpreadsheet/issues/2147) [PR #2156](https://github.com/PHPOffice/PhpSpreadsheet/pull/2156) |
|||
- Column width and Row height styles in the Html Reader when the value includes a unit of measure [Issue #2145](https://github.com/PHPOffice/PhpSpreadsheet/issues/2145). |
|||
- Data Validation flags not set correctly when reading XLSX files [Issue #2224](https://github.com/PHPOffice/PhpSpreadsheet/issues/2224) [PR #2225](https://github.com/PHPOffice/PhpSpreadsheet/pull/2225) |
|||
- Reading XLSX files without styles.xml throws an exception [Issue #2246](https://github.com/PHPOffice/PhpSpreadsheet/issues/2246) |
|||
- Improved performance of `Style::applyFromArray()` when applied to several cells [PR #1785](https://github.com/PHPOffice/PhpSpreadsheet/issues/1785). |
|||
- Improve XLSX parsing speed if no readFilter is applied (again) - [#772](https://github.com/PHPOffice/PhpSpreadsheet/issues/772) |
|||
|
|||
## 1.18.0 - 2021-05-31 |
|||
|
|||
### Added |
|||
|
|||
- Enhancements to CSV Reader, allowing options to be set when using `IOFactory::load()` with a callback to set delimiter, enclosure, charset etc [PR #2103](https://github.com/PHPOffice/PhpSpreadsheet/pull/2103) - See [documentation](https://github.com/PHPOffice/PhpSpreadsheet/blob/master/docs/topics/reading-and-writing-to-file.md#csv-comma-separated-values) for details. |
|||
- Implemented basic AutoFiltering for Ods Reader and Writer [PR #2053](https://github.com/PHPOffice/PhpSpreadsheet/pull/2053) |
|||
- Implemented basic AutoFiltering for Gnumeric Reader [PR #2055](https://github.com/PHPOffice/PhpSpreadsheet/pull/2055) |
|||
- 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 URLENCODE() Web Function |
|||
- 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) |
|||
- Support for notContainsText Conditional Style in xlsx [Issue #984](https://github.com/PHPOffice/PhpSpreadsheet/issues/984) |
|||
|
|||
### Changed |
|||
|
|||
- Use of `nb` rather than `no` as the locale code for Norsk Bokmål. |
|||
|
|||
### Deprecated |
|||
|
|||
- All Excel Function implementations in `Calculation\Database`, `Calculation\DateTime`, `Calculation\Engineering`, `Calculation\Financial`, `Calculation\Logical`, `Calculation\LookupRef`, `Calculation\MathTrig`, `Calculation\Statistical`, `Calculation\TextData` and `Calculation\Web` have been moved to dedicated classes for individual functions or groups of related functions. See the docblocks against all the deprecated methods for details of the new methods to call instead. At some point, these old classes will be deleted. |
|||
|
|||
### Removed |
|||
|
|||
- Use of `nb` rather than `no` as the locale language code for Norsk Bokmål. |
|||
|
|||
### Fixed |
|||
|
|||
- Fixed error in COUPNCD() calculation for end of month [Issue #2116](https://github.com/PHPOffice/PhpSpreadsheet/issues/2116) - [PR #2119](https://github.com/PHPOffice/PhpSpreadsheet/pull/2119) |
|||
- Resolve default values when a null argument is passed for HLOOKUP(), VLOOKUP() and ADDRESS() functions [Issue #2120](https://github.com/PHPOffice/PhpSpreadsheet/issues/2120) - [PR #2121](https://github.com/PHPOffice/PhpSpreadsheet/pull/2121) |
|||
- Fixed incorrect R1C1 to A1 subtraction formula conversion (`R[-2]C-R[2]C`) [Issue #2076](https://github.com/PHPOffice/PhpSpreadsheet/pull/2076) [PR #2086](https://github.com/PHPOffice/PhpSpreadsheet/pull/2086) |
|||
- Correctly handle absolute A1 references when converting to R1C1 format [PR #2060](https://github.com/PHPOffice/PhpSpreadsheet/pull/2060) |
|||
- 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) |
|||
- Fix for [Issue #1916](https://github.com/PHPOffice/PhpSpreadsheet/issues/1916) - Invalid signature check for XML files |
|||
- Fix change in `Font::setSize()` behavior for PHP8 [PR #2100](https://github.com/PHPOffice/PhpSpreadsheet/pull/2100) |
|||
|
|||
## 1.17.1 - 2021-03-01 |
|||
|
|||
### Added |
|||
|
|||
- Implementation of the Excel `AVERAGEIFS()` functions as part of a restructuring of Database functions and Conditional Statistical functions. |
|||
- Support for date values and percentages in query parameters for Database functions, and the IF expressions in functions like COUNTIF() and AVERAGEIF(). [#1875](https://github.com/PHPOffice/PhpSpreadsheet/pull/1875) |
|||
- Support for booleans, and for wildcard text search in query parameters for Database functions, and the IF expressions in functions like COUNTIF() and AVERAGEIF(). [#1876](https://github.com/PHPOffice/PhpSpreadsheet/pull/1876) |
|||
- Implemented DataBar for conditional formatting in Xlsx, providing read/write and creation of (type, value, direction, fills, border, axis position, color settings) as DataBar options in Excel. [#1754](https://github.com/PHPOffice/PhpSpreadsheet/pull/1754) |
|||
- Alignment for ODS Writer [#1796](https://github.com/PHPOffice/PhpSpreadsheet/issues/1796) |
|||
- Basic implementation of the PERMUTATIONA() Statistical Function |
|||
|
|||
### Changed |
|||
|
|||
- Formula functions that previously called PHP functions directly are now processed through the Excel Functions classes; resolving issues with PHP8 stricter typing. [#1789](https://github.com/PHPOffice/PhpSpreadsheet/issues/1789) |
|||
|
|||
The following MathTrig functions are affected: |
|||
`ABS()`, `ACOS()`, `ACOSH()`, `ASIN()`, `ASINH()`, `ATAN()`, `ATANH()`, |
|||
`COS()`, `COSH()`, `DEGREES()` (rad2deg), `EXP()`, `LN()` (log), `LOG10()`, |
|||
`RADIANS()` (deg2rad), `SIN()`, `SINH()`, `SQRT()`, `TAN()`, `TANH()`. |
|||
|
|||
One TextData function is also affected: `REPT()` (str_repeat). |
|||
- `formatAsDate` correctly matches language metadata, reverting c55272e |
|||
- Formulae that previously crashed on sub function call returning excel error value now return said value. |
|||
The following functions are affected `CUMPRINC()`, `CUMIPMT()`, `AMORLINC()`, |
|||
`AMORDEGRC()`. |
|||
- Adapt some function error return value to match excel's error. |
|||
The following functions are affected `PPMT()`, `IPMT()`. |
|||
|
|||
### Deprecated |
|||
|
|||
- Calling many of the Excel formula functions directly rather than through the Calculation Engine. |
|||
|
|||
The logic for these Functions is now being moved out of the categorised `Database`, `DateTime`, `Engineering`, `Financial`, `Logical`, `LookupRef`, `MathTrig`, `Statistical`, `TextData` and `Web` classes into small, dedicated classes for individual functions or related groups of functions. |
|||
|
|||
This makes the logic in these classes easier to maintain; and will reduce the memory footprint required to execute formulae when calling these functions. |
|||
|
|||
### Removed |
|||
|
|||
- Nothing. |
|||
|
|||
### Fixed |
|||
|
|||
- Avoid Duplicate Titles When Reading Multiple HTML Files.[Issue #1823](https://github.com/PHPOffice/PhpSpreadsheet/issues/1823) [PR #1829](https://github.com/PHPOffice/PhpSpreadsheet/pull/1829) |
|||
- Fixed issue with Worksheet's `getCell()` method when trying to get a cell by defined name. [#1858](https://github.com/PHPOffice/PhpSpreadsheet/issues/1858) |
|||
- Fix possible endless loop in NumberFormat Masks [#1792](https://github.com/PHPOffice/PhpSpreadsheet/issues/1792) |
|||
- Fix problem resulting from literal dot inside quotes in number format masks [PR #1830](https://github.com/PHPOffice/PhpSpreadsheet/pull/1830) |
|||
- Resolve Google Sheets Xlsx charts issue. Google Sheets uses oneCellAnchor positioning and does not include *Cache values in the exported Xlsx [PR #1761](https://github.com/PHPOffice/PhpSpreadsheet/pull/1761) |
|||
- Fix for Xlsx Chart axis titles mapping to correct X or Y axis label when only one is present [PR #1760](https://github.com/PHPOffice/PhpSpreadsheet/pull/1760) |
|||
- Fix For Null Exception on ODS Read of Page Settings. [#1772](https://github.com/PHPOffice/PhpSpreadsheet/issues/1772) |
|||
- Fix Xlsx reader overriding manually set number format with builtin number format [PR #1805](https://github.com/PHPOffice/PhpSpreadsheet/pull/1805) |
|||
- Fix Xlsx reader cell alignment [PR #1710](https://github.com/PHPOffice/PhpSpreadsheet/pull/1710) |
|||
- Fix for not yet implemented data-types in Open Document writer [Issue #1674](https://github.com/PHPOffice/PhpSpreadsheet/issues/1674) |
|||
- Fix XLSX reader when having a corrupt numeric cell data type [PR #1664](https://github.com/phpoffice/phpspreadsheet/pull/1664) |
|||
- Fix on `CUMPRINC()`, `CUMIPMT()`, `AMORLINC()`, `AMORDEGRC()` usage. When those functions called one of `YEARFRAC()`, `PPMT()`, `IPMT()` and they would get back an error value (represented as a string), trying to use numeral operands (`+`, `/`, `-`, `*`) on said return value and a number (`float or `int`) would fail. |
|||
|
|||
## 1.16.0 - 2020-12-31 |
|||
|
|||
### Added |
|||
|
|||
- CSV Reader - Best Guess for Encoding, and Handle Null-string Escape [#1647](https://github.com/PHPOffice/PhpSpreadsheet/issues/1647) |
|||
|
|||
### Changed |
|||
|
|||
- Updated the CONVERT() function to support all current MS Excel categories and Units of Measure. |
|||
|
|||
### Deprecated |
|||
|
|||
- All Excel Function implementations in `Calculation\Database`, `Calculation\DateTime`, `Calculation\Engineering`, `Calculation\Financial`, `Calculation\Logical`, `Calculation\LookupRef`, `Calculation\MathTrig`, `Calculation\Statistical`, `Calculation\TextData` and `Calculation\Web` have been moved to dedicated classes for individual functions or groups of related functions. See the docblocks against all the deprecated methods for details of the new methods to call instead. At some point, these old classes will be deleted. |
|||
|
|||
### Removed |
|||
|
|||
- Nothing. |
|||
|
|||
### 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 |
|||
- Resolve issue with SLK long filenames [#1612](https://github.com/PHPOffice/PhpSpreadsheet/issues/1612) |
|||
- ROUNDUP and ROUNDDOWN return incorrect results for values of 0 [#1627](https://github.com/phpoffice/phpspreadsheet/pull/1627) |
|||
- Apply Column and Row Styles to Existing Cells [#1712](https://github.com/PHPOffice/PhpSpreadsheet/issues/1712) [PR #1721](https://github.com/PHPOffice/PhpSpreadsheet/pull/1721) |
|||
- Resolve issues with defined names where worksheet doesn't exist (#1686)[https://github.com/PHPOffice/PhpSpreadsheet/issues/1686] and [#1723](https://github.com/PHPOffice/PhpSpreadsheet/issues/1723) - [PR #1742](https://github.com/PHPOffice/PhpSpreadsheet/pull/1742) |
|||
- Fix for issue [#1735](https://github.com/PHPOffice/PhpSpreadsheet/issues/1735) Incorrect activeSheetIndex after RemoveSheetByIndex - [PR #1743](https://github.com/PHPOffice/PhpSpreadsheet/pull/1743) |
|||
- Ensure that the list of shared formulae is maintained when an xlsx file is chunked with readFilter[Issue #169](https://github.com/PHPOffice/PhpSpreadsheet/issues/1669). |
|||
- Fix for notice during accessing "cached magnification factor" offset [#1354](https://github.com/PHPOffice/PhpSpreadsheet/pull/1354) |
|||
- Fix compatibility with ext-gd on php 8 |
|||
|
|||
### Security Fix (CVE-2020-7776) |
|||
|
|||
- Prevent XSS through cell comments in the HTML Writer. |
|||
|
|||
## 1.15.0 - 2020-10-11 |
|||
|
|||
### Added |
|||
|
|||
- Implemented Page Order for Xlsx and Xls Readers, and provided Page Settings (Orientation, Scale, Horizontal/Vertical Centering, Page Order, Margins) support for Ods, Gnumeric and Xls Readers [#1559](https://github.com/PHPOffice/PhpSpreadsheet/pull/1559) |
|||
- Implementation of the Excel `LOGNORM.DIST()`, `NORM.S.DIST()`, `GAMMA()` and `GAUSS()` functions. [#1588](https://github.com/PHPOffice/PhpSpreadsheet/pull/1588) |
|||
- Named formula implementation, and improved handling of Defined Names generally [#1535](https://github.com/PHPOffice/PhpSpreadsheet/pull/1535) |
|||
- Defined Names are now case-insensitive |
|||
- Distinction between named ranges and named formulae |
|||
- Correct handling of union and intersection operators in named ranges |
|||
- Correct evaluation of named range operators in calculations |
|||
- fix resolution of relative named range values in the calculation engine; previously all named range values had been treated as absolute. |
|||
- Calculation support for named formulae |
|||
- Support for nested ranges and formulae (named ranges and formulae that reference other named ranges/formulae) in calculations |
|||
- Introduction of a helper to convert address formats between R1C1 and A1 (and the reverse) |
|||
- Proper support for both named ranges and named formulae in all appropriate Readers |
|||
- **Xlsx** (Previously only simple named ranges were supported) |
|||
- **Xls** (Previously only simple named ranges were supported) |
|||
- **Gnumeric** (Previously neither named ranges nor formulae were supported) |
|||
- **Ods** (Previously neither named ranges nor formulae were supported) |
|||
- **Xml** (Previously neither named ranges nor formulae were supported) |
|||
- Proper support for named ranges and named formulae in all appropriate Writers |
|||
- **Xlsx** (Previously only simple named ranges were supported) |
|||
- **Xls** (Previously neither named ranges nor formulae were supported) - Still not supported, but some parser issues resolved that previously failed to differentiate between a defined name and a function name |
|||
- **Ods** (Previously neither named ranges nor formulae were supported) |
|||
- Support for PHP 8.0 |
|||
|
|||
### Changed |
|||
|
|||
- Improve Coverage for ODS Reader [#1545](https://github.com/phpoffice/phpspreadsheet/pull/1545) |
|||
- Named formula implementation, and improved handling of Defined Names generally [#1535](https://github.com/PHPOffice/PhpSpreadsheet/pull/1535) |
|||
- fix resolution of relative named range values in the calculation engine; previously all named range values had been treated as absolute. |
|||
- Drop $this->spreadSheet null check from Xlsx Writer [#1646](https://github.com/phpoffice/phpspreadsheet/pull/1646) |
|||
- Improving Coverage for Excel2003 XML Reader [#1557](https://github.com/phpoffice/phpspreadsheet/pull/1557) |
|||
|
|||
### Deprecated |
|||
|
|||
- **IMPORTANT NOTE:** This Introduces a **BC break** in the handling of named ranges. Previously, a named range cell reference of `B2` would be treated identically to a named range cell reference of `$B2` or `B$2` or `$B$2` because the calculation engine treated then all as absolute references. These changes "fix" that, so the calculation engine now handles relative references in named ranges correctly. |
|||
This change that resolves previously incorrect behaviour in the calculation may affect users who have dynamically defined named ranges using relative references when they should have used absolute references. |
|||
|
|||
### Removed |
|||
|
|||
- Nothing. |
|||
|
|||
### Fixed |
|||
|
|||
- PrintArea causes exception [#1544](https://github.com/phpoffice/phpspreadsheet/pull/1544) |
|||
- Calculation/DateTime Failure With PHP8 [#1661](https://github.com/phpoffice/phpspreadsheet/pull/1661) |
|||
- Reader/Gnumeric Failure with PHP8 [#1662](https://github.com/phpoffice/phpspreadsheet/pull/1662) |
|||
- ReverseSort bug, exposed but not caused by PHP8 [#1660](https://github.com/phpoffice/phpspreadsheet/pull/1660) |
|||
- Bug setting Superscript/Subscript to false [#1567](https://github.com/phpoffice/phpspreadsheet/pull/1567) |
|||
|
|||
## 1.14.1 - 2020-07-19 |
|||
|
|||
### Added |
|||
|
|||
- nothing |
|||
|
|||
### Fixed |
|||
|
|||
- WEBSERVICE is HTTP client agnostic and must be configured via `Settings::setHttpClient()` [#1562](https://github.com/PHPOffice/PhpSpreadsheet/issues/1562) |
|||
- Borders were not complete on rowspanned columns using HTML reader [#1473](https://github.com/PHPOffice/PhpSpreadsheet/pull/1473) |
|||
|
|||
### Changed |
|||
|
|||
## 1.14.0 - 2020-06-29 |
|||
|
|||
### Added |
|||
|
|||
- Add support for IFS() logical function [#1442](https://github.com/PHPOffice/PhpSpreadsheet/pull/1442) |
|||
- Add Cell Address Helper to provide conversions between the R1C1 and A1 address formats [#1558](https://github.com/PHPOffice/PhpSpreadsheet/pull/1558) |
|||
- Add ability to edit Html/Pdf before saving [#1499](https://github.com/PHPOffice/PhpSpreadsheet/pull/1499) |
|||
- Add ability to set codepage explicitly for BIFF5 [#1018](https://github.com/PHPOffice/PhpSpreadsheet/issues/1018) |
|||
- Added support for the WEBSERVICE function [#1409](https://github.com/PHPOffice/PhpSpreadsheet/pull/1409) |
|||
|
|||
### Fixed |
|||
|
|||
- Resolve evaluation of utf-8 named ranges in calculation engine [#1522](https://github.com/PHPOffice/PhpSpreadsheet/pull/1522) |
|||
- Fix HLOOKUP on single row [#1512](https://github.com/PHPOffice/PhpSpreadsheet/pull/1512) |
|||
- Fix MATCH when comparing different numeric types [#1521](https://github.com/PHPOffice/PhpSpreadsheet/pull/1521) |
|||
- Fix exact MATCH on ranges with empty cells [#1520](https://github.com/PHPOffice/PhpSpreadsheet/pull/1520) |
|||
- Fix for Issue [#1516](https://github.com/PHPOffice/PhpSpreadsheet/issues/1516) (Cloning worksheet makes corrupted Xlsx) [#1530](https://github.com/PHPOffice/PhpSpreadsheet/pull/1530) |
|||
- Fix For Issue [#1509](https://github.com/PHPOffice/PhpSpreadsheet/issues/1509) (Can not set empty enclosure for CSV) [#1518](https://github.com/PHPOffice/PhpSpreadsheet/pull/1518) |
|||
- Fix for Issue [#1505](https://github.com/PHPOffice/PhpSpreadsheet/issues/1505) (TypeError : Argument 4 passed to PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet::writeAttributeIf() must be of the type string) [#1525](https://github.com/PHPOffice/PhpSpreadsheet/pull/1525) |
|||
- Fix for Issue [#1495](https://github.com/PHPOffice/PhpSpreadsheet/issues/1495) (Sheet index being changed when multiple sheets are used in formula) [#1500]((https://github.com/PHPOffice/PhpSpreadsheet/pull/1500)) |
|||
- Fix for Issue [#1533](https://github.com/PHPOffice/PhpSpreadsheet/issues/1533) (A reference to a cell containing a string starting with "#" leads to errors in the generated xlsx.) [#1534](https://github.com/PHPOffice/PhpSpreadsheet/pull/1534) |
|||
- Xls Writer - Correct Timestamp Bug [#1493](https://github.com/PHPOffice/PhpSpreadsheet/pull/1493) |
|||
- Don't ouput row and columns without any cells in HTML writer [#1235](https://github.com/PHPOffice/PhpSpreadsheet/issues/1235) |
|||
|
|||
## 1.13.0 - 2020-05-31 |
|||
|
|||
### Added |
|||
|
|||
- Support writing to streams in all writers [#1292](https://github.com/PHPOffice/PhpSpreadsheet/issues/1292) |
|||
- Support CSV files with data wrapping a lot of lines [#1468](https://github.com/PHPOffice/PhpSpreadsheet/pull/1468) |
|||
- Support protection of worksheet by a specific hash algorithm [#1485](https://github.com/PHPOffice/PhpSpreadsheet/pull/1485) |
|||
|
|||
### Fixed |
|||
|
|||
- Fix Chart samples by updating chart parameter from 0 to DataSeries::EMPTY_AS_GAP [#1448](https://github.com/PHPOffice/PhpSpreadsheet/pull/1448) |
|||
- Fix return type in docblock for the Cells::get() [#1398](https://github.com/PHPOffice/PhpSpreadsheet/pull/1398) |
|||
- Fix RATE, PRICE, XIRR, and XNPV Functions [#1456](https://github.com/PHPOffice/PhpSpreadsheet/pull/1456) |
|||
- Save Excel 2010+ functions properly in XLSX [#1461](https://github.com/PHPOffice/PhpSpreadsheet/pull/1461) |
|||
- Several improvements in HTML writer [#1464](https://github.com/PHPOffice/PhpSpreadsheet/pull/1464) |
|||
- Fix incorrect behaviour when saving XLSX file with drawings [#1462](https://github.com/PHPOffice/PhpSpreadsheet/pull/1462), |
|||
- Fix Crash while trying setting a cell the value "123456\n" [#1476](https://github.com/PHPOffice/PhpSpreadsheet/pull/1481) |
|||
- Improved DATEDIF() function and reduced errors for Y and YM units [#1466](https://github.com/PHPOffice/PhpSpreadsheet/pull/1466) |
|||
- Stricter typing for mergeCells [#1494](https://github.com/PHPOffice/PhpSpreadsheet/pull/1494) |
|||
|
|||
### Changed |
|||
|
|||
- Drop support for PHP 7.1, according to https://phpspreadsheet.readthedocs.io/en/latest/#php-version-support |
|||
- Drop partial migration tool in favor of complete migration via RectorPHP [#1445](https://github.com/PHPOffice/PhpSpreadsheet/issues/1445) |
|||
- Limit composer package to `src/` [#1424](https://github.com/PHPOffice/PhpSpreadsheet/pull/1424) |
|||
|
|||
## 1.12.0 - 2020-04-27 |
|||
|
|||
### Added |
|||
|
|||
- Improved the ARABIC function to also handle short-hand roman numerals |
|||
- Added support for the FLOOR.MATH and FLOOR.PRECISE functions [#1351](https://github.com/PHPOffice/PhpSpreadsheet/pull/1351) |
|||
|
|||
### Fixed |
|||
|
|||
- Fix ROUNDUP and ROUNDDOWN for floating-point rounding error [#1404](https://github.com/PHPOffice/PhpSpreadsheet/pull/1404) |
|||
- Fix ROUNDUP and ROUNDDOWN for negative number [#1417](https://github.com/PHPOffice/PhpSpreadsheet/pull/1417) |
|||
- Fix loading styles from vmlDrawings when containing whitespace [#1347](https://github.com/PHPOffice/PhpSpreadsheet/issues/1347) |
|||
- Fix incorrect behavior when removing last row [#1365](https://github.com/PHPOffice/PhpSpreadsheet/pull/1365) |
|||
- MATCH with a static array should return the position of the found value based on the values submitted [#1332](https://github.com/PHPOffice/PhpSpreadsheet/pull/1332) |
|||
- Fix Xlsx Reader's handling of undefined fill color [#1353](https://github.com/PHPOffice/PhpSpreadsheet/pull/1353) |
|||
|
|||
## 1.11.0 - 2020-03-02 |
|||
|
|||
### Added |
|||
|
|||
- Added support for the BASE function |
|||
- Added support for the ARABIC function |
|||
- Conditionals - Extend Support for (NOT)CONTAINSBLANKS [#1278](https://github.com/PHPOffice/PhpSpreadsheet/pull/1278) |
|||
|
|||
### Fixed |
|||
|
|||
- Handle Error in Formula Processing Better for Xls [#1267](https://github.com/PHPOffice/PhpSpreadsheet/pull/1267) |
|||
- Handle ConditionalStyle NumberFormat When Reading Xlsx File [#1296](https://github.com/PHPOffice/PhpSpreadsheet/pull/1296) |
|||
- Fix Xlsx Writer's handling of decimal commas [#1282](https://github.com/PHPOffice/PhpSpreadsheet/pull/1282) |
|||
- Fix for issue by removing test code mistakenly left in [#1328](https://github.com/PHPOffice/PhpSpreadsheet/pull/1328) |
|||
- Fix for Xls writer wrong selected cells and active sheet [#1256](https://github.com/PHPOffice/PhpSpreadsheet/pull/1256) |
|||
- Fix active cell when freeze pane is used [#1323](https://github.com/PHPOffice/PhpSpreadsheet/pull/1323) |
|||
- Fix XLSX file loading with autofilter containing '$' [#1326](https://github.com/PHPOffice/PhpSpreadsheet/pull/1326) |
|||
- PHPDoc - Use `@return $this` for fluent methods [#1362](https://github.com/PHPOffice/PhpSpreadsheet/pull/1362) |
|||
|
|||
## 1.10.1 - 2019-12-02 |
|||
|
|||
### Changed |
|||
|
|||
- PHP 7.4 compatibility |
|||
|
|||
### Fixed |
|||
|
|||
- FLOOR() function accept negative number and negative significance [#1245](https://github.com/PHPOffice/PhpSpreadsheet/pull/1245) |
|||
- Correct column style even when using rowspan [#1249](https://github.com/PHPOffice/PhpSpreadsheet/pull/1249) |
|||
- Do not confuse defined names and cell refs [#1263](https://github.com/PHPOffice/PhpSpreadsheet/pull/1263) |
|||
- XLSX reader/writer keep decimal for floats with a zero decimal part [#1262](https://github.com/PHPOffice/PhpSpreadsheet/pull/1262) |
|||
- ODS writer prevent invalid numeric value if locale decimal separator is comma [#1268](https://github.com/PHPOffice/PhpSpreadsheet/pull/1268) |
|||
- Xlsx writer actually writes plotVisOnly and dispBlanksAs from chart properties [#1266](https://github.com/PHPOffice/PhpSpreadsheet/pull/1266) |
|||
|
|||
## 1.10.0 - 2019-11-18 |
|||
|
|||
### Changed |
|||
|
|||
- Change license from LGPL 2.1 to MIT [#140](https://github.com/PHPOffice/PhpSpreadsheet/issues/140) |
|||
|
|||
### Added |
|||
|
|||
- Implementation of IFNA() logical function |
|||
- Support "showZeros" worksheet option to change how Excel shows and handles "null" values returned from a calculation |
|||
- Allow HTML Reader to accept HTML as a string into an existing spreadsheet [#1212](https://github.com/PHPOffice/PhpSpreadsheet/pull/1212) |
|||
|
|||
### Fixed |
|||
|
|||
- IF implementation properly handles the value `#N/A` [#1165](https://github.com/PHPOffice/PhpSpreadsheet/pull/1165) |
|||
- Formula Parser: Wrong line count for stuff like "MyOtherSheet!A:D" [#1215](https://github.com/PHPOffice/PhpSpreadsheet/issues/1215) |
|||
- Call garbage collector after removing a column to prevent stale cached values |
|||
- Trying to remove a column that doesn't exist deletes the latest column |
|||
- Keep big integer as integer instead of lossely casting to float [#874](https://github.com/PHPOffice/PhpSpreadsheet/pull/874) |
|||
- Fix branch pruning handling of non boolean conditions [#1167](https://github.com/PHPOffice/PhpSpreadsheet/pull/1167) |
|||
- Fix ODS Reader when no DC namespace are defined [#1182](https://github.com/PHPOffice/PhpSpreadsheet/pull/1182) |
|||
- Fixed Functions->ifCondition for allowing <> and empty condition [#1206](https://github.com/PHPOffice/PhpSpreadsheet/pull/1206) |
|||
- Validate XIRR inputs and return correct error values [#1120](https://github.com/PHPOffice/PhpSpreadsheet/issues/1120) |
|||
- Allow to read xlsx files with exotic workbook names like "workbook2.xml" [#1183](https://github.com/PHPOffice/PhpSpreadsheet/pull/1183) |
|||
|
|||
## 1.9.0 - 2019-08-17 |
|||
|
|||
### Changed |
|||
|
|||
- Drop support for PHP 5.6 and 7.0, according to https://phpspreadsheet.readthedocs.io/en/latest/#php-version-support |
|||
|
|||
### Added |
|||
|
|||
- When <br> appears in a table cell, set the cell to wrap [#1071](https://github.com/PHPOffice/PhpSpreadsheet/issues/1071) and [#1070](https://github.com/PHPOffice/PhpSpreadsheet/pull/1070) |
|||
- Add MAXIFS, MINIFS, COUNTIFS and Remove MINIF, MAXIF [#1056](https://github.com/PHPOffice/PhpSpreadsheet/issues/1056) |
|||
- HLookup needs an ordered list even if range_lookup is set to false [#1055](https://github.com/PHPOffice/PhpSpreadsheet/issues/1055) and [#1076](https://github.com/PHPOffice/PhpSpreadsheet/pull/1076) |
|||
- Improve performance of IF function calls via ranch pruning to avoid resolution of every branches [#844](https://github.com/PHPOffice/PhpSpreadsheet/pull/844) |
|||
- MATCH function supports `*?~` Excel functionality, when match_type=0 [#1116](https://github.com/PHPOffice/PhpSpreadsheet/issues/1116) |
|||
- Allow HTML Reader to accept HTML as a string [#1136](https://github.com/PHPOffice/PhpSpreadsheet/pull/1136) |
|||
|
|||
### Fixed |
|||
|
|||
- Fix to AVERAGEIF() function when called with a third argument |
|||
- Eliminate duplicate fill none style entries [#1066](https://github.com/PHPOffice/PhpSpreadsheet/issues/1066) |
|||
- Fix number format masks containing literal (non-decimal point) dots [#1079](https://github.com/PHPOffice/PhpSpreadsheet/issues/1079) |
|||
- Fix number format masks containing named colours that were being misinterpreted as date formats; and add support for masks that fully replace the value with a full text string [#1009](https://github.com/PHPOffice/PhpSpreadsheet/issues/1009) |
|||
- Stricter-typed comparison testing in COUNTIF() and COUNTIFS() evaluation [#1046](https://github.com/PHPOffice/PhpSpreadsheet/issues/1046) |
|||
- COUPNUM should not return zero when settlement is in the last period [#1020](https://github.com/PHPOffice/PhpSpreadsheet/issues/1020) and [#1021](https://github.com/PHPOffice/PhpSpreadsheet/pull/1021) |
|||
- Fix handling of named ranges referencing sheets with spaces or "!" in their title |
|||
- Cover `getSheetByName()` with tests for name with quote and spaces [#739](https://github.com/PHPOffice/PhpSpreadsheet/issues/739) |
|||
- Best effort to support invalid colspan values in HTML reader - [#878](https://github.com/PHPOffice/PhpSpreadsheet/pull/878) |
|||
- Fixes incorrect rows deletion [#868](https://github.com/PHPOffice/PhpSpreadsheet/issues/868) |
|||
- MATCH function fix (value search by type, stop search when match_type=-1 and unordered element encountered) [#1116](https://github.com/PHPOffice/PhpSpreadsheet/issues/1116) |
|||
- Fix `getCalculatedValue()` error with more than two INDIRECT [#1115](https://github.com/PHPOffice/PhpSpreadsheet/pull/1115) |
|||
- Writer\Html did not hide columns [#985](https://github.com/PHPOffice/PhpSpreadsheet/pull/985) |
|||
|
|||
## 1.8.2 - 2019-07-08 |
|||
|
|||
### Fixed |
|||
|
|||
- Uncaught error when opening ods file and properties aren't defined [#1047](https://github.com/PHPOffice/PhpSpreadsheet/issues/1047) |
|||
- Xlsx Reader Cell datavalidations bug [#1052](https://github.com/PHPOffice/PhpSpreadsheet/pull/1052) |
|||
|
|||
## 1.8.1 - 2019-07-02 |
|||
|
|||
### Fixed |
|||
|
|||
- Allow nullable theme for Xlsx Style Reader class [#1043](https://github.com/PHPOffice/PhpSpreadsheet/issues/1043) |
|||
|
|||
## 1.8.0 - 2019-07-01 |
|||
|
|||
### Security Fix (CVE-2019-12331) |
|||
|
|||
- Detect double-encoded xml in the Security scanner, and reject as suspicious. |
|||
- This change also broadens the scope of the `libxml_disable_entity_loader` setting when reading XML-based formats, so that it is enabled while the xml is being parsed and not simply while it is loaded. |
|||
On some versions of PHP, this can cause problems because it is not thread-safe, and can affect other PHP scripts running on the same server. This flag is set to true when instantiating a loader, and back to its original setting when the Reader is no longer in scope, or manually unset. |
|||
- Provide a check to identify whether libxml_disable_entity_loader is thread-safe or not. |
|||
|
|||
`XmlScanner::threadSafeLibxmlDisableEntityLoaderAvailability()` |
|||
- Provide an option to disable the libxml_disable_entity_loader call through settings. This is not recommended as it reduces the security of the XML-based readers, and should only be used if you understand the consequences and have no other choice. |
|||
|
|||
### Added |
|||
|
|||
- Added support for the SWITCH function [#963](https://github.com/PHPOffice/PhpSpreadsheet/issues/963) and [#983](https://github.com/PHPOffice/PhpSpreadsheet/pull/983) |
|||
- Add accounting number format style [#974](https://github.com/PHPOffice/PhpSpreadsheet/pull/974) |
|||
|
|||
### Fixed |
|||
|
|||
- Whitelist `tsv` extension when opening CSV files [#429](https://github.com/PHPOffice/PhpSpreadsheet/issues/429) |
|||
- Fix a SUMIF warning with some versions of PHP when having different length of arrays provided as input [#873](https://github.com/PHPOffice/PhpSpreadsheet/pull/873) |
|||
- Fix incorrectly handled backslash-escaped space characters in number format |
|||
|
|||
## 1.7.0 - 2019-05-26 |
|||
|
|||
- Added support for inline styles in Html reader (borders, alignment, width, height) |
|||
- QuotedText cells no longer treated as formulae if the content begins with a `=` |
|||
- Clean handling for DDE in formulae |
|||
|
|||
### Fixed |
|||
|
|||
- Fix handling for escaped enclosures and new lines in CSV Separator Inference |
|||
- Fix MATCH an error was appearing when comparing strings against 0 (always true) |
|||
- Fix wrong calculation of highest column with specified row [#700](https://github.com/PHPOffice/PhpSpreadsheet/issues/700) |
|||
- Fix VLOOKUP |
|||
- Fix return type hint |
|||
|
|||
## 1.6.0 - 2019-01-02 |
|||
|
|||
### Added |
|||
|
|||
- Refactored Matrix Functions to use external Matrix library |
|||
- Possibility to specify custom colors of values for pie and donut charts [#768](https://github.com/PHPOffice/PhpSpreadsheet/pull/768) |
|||
|
|||
### Fixed |
|||
|
|||
- Improve XLSX parsing speed if no readFilter is applied [#772](https://github.com/PHPOffice/PhpSpreadsheet/issues/772) |
|||
- Fix column names if read filter calls in XLSX reader skip columns [#777](https://github.com/PHPOffice/PhpSpreadsheet/pull/777) |
|||
- XLSX reader can now ignore blank cells, using the setReadEmptyCells(false) method. [#810](https://github.com/PHPOffice/PhpSpreadsheet/issues/810) |
|||
- Fix LOOKUP function which was breaking on edge cases [#796](https://github.com/PHPOffice/PhpSpreadsheet/issues/796) |
|||
- Fix VLOOKUP with exact matches [#809](https://github.com/PHPOffice/PhpSpreadsheet/pull/809) |
|||
- Support COUNTIFS multiple arguments [#830](https://github.com/PHPOffice/PhpSpreadsheet/pull/830) |
|||
- Change `libxml_disable_entity_loader()` as shortly as possible [#819](https://github.com/PHPOffice/PhpSpreadsheet/pull/819) |
|||
- Improved memory usage and performance when loading large spreadsheets [#822](https://github.com/PHPOffice/PhpSpreadsheet/pull/822) |
|||
- Improved performance when loading large spreadsheets [#825](https://github.com/PHPOffice/PhpSpreadsheet/pull/825) |
|||
- Improved performance when loading large spreadsheets [#824](https://github.com/PHPOffice/PhpSpreadsheet/pull/824) |
|||
- Fix color from CSS when reading from HTML [#831](https://github.com/PHPOffice/PhpSpreadsheet/pull/831) |
|||
- Fix infinite loop when reading invalid ODS files [#832](https://github.com/PHPOffice/PhpSpreadsheet/pull/832) |
|||
- Fix time format for duration is incorrect [#666](https://github.com/PHPOffice/PhpSpreadsheet/pull/666) |
|||
- Fix iconv unsupported `//IGNORE//TRANSLIT` on IBM i [#791](https://github.com/PHPOffice/PhpSpreadsheet/issues/791) |
|||
|
|||
### Changed |
|||
|
|||
- `master` is the new default branch, `develop` does not exist anymore |
|||
|
|||
## 1.5.2 - 2018-11-25 |
|||
|
|||
### Security |
|||
|
|||
- Improvements to the design of the XML Security Scanner [#771](https://github.com/PHPOffice/PhpSpreadsheet/issues/771) |
|||
|
|||
## 1.5.1 - 2018-11-20 |
|||
|
|||
### Security |
|||
|
|||
- Fix and improve XXE security scanning for XML-based and HTML Readers [#771](https://github.com/PHPOffice/PhpSpreadsheet/issues/771) |
|||
|
|||
### Added |
|||
|
|||
- Support page margin in mPDF [#750](https://github.com/PHPOffice/PhpSpreadsheet/issues/750) |
|||
|
|||
### Fixed |
|||
|
|||
- Support numeric condition in SUMIF, SUMIFS, AVERAGEIF, COUNTIF, MAXIF and MINIF [#683](https://github.com/PHPOffice/PhpSpreadsheet/issues/683) |
|||
- SUMIFS containing multiple conditions [#704](https://github.com/PHPOffice/PhpSpreadsheet/issues/704) |
|||
- Csv reader avoid notice when the file is empty [#743](https://github.com/PHPOffice/PhpSpreadsheet/pull/743) |
|||
- Fix print area parser for XLSX reader [#734](https://github.com/PHPOffice/PhpSpreadsheet/pull/734) |
|||
- Support overriding `DefaultValueBinder::dataTypeForValue()` without overriding `DefaultValueBinder::bindValue()` [#735](https://github.com/PHPOffice/PhpSpreadsheet/pull/735) |
|||
- Mpdf export can exceed pcre.backtrack_limit [#637](https://github.com/PHPOffice/PhpSpreadsheet/issues/637) |
|||
- Fix index overflow on data values array [#748](https://github.com/PHPOffice/PhpSpreadsheet/pull/748) |
|||
|
|||
## 1.5.0 - 2018-10-21 |
|||
|
|||
### Added |
|||
|
|||
- PHP 7.3 support |
|||
- Add the DAYS() function [#594](https://github.com/PHPOffice/PhpSpreadsheet/pull/594) |
|||
|
|||
### Fixed |
|||
|
|||
- Sheet title can contain exclamation mark [#325](https://github.com/PHPOffice/PhpSpreadsheet/issues/325) |
|||
- Xls file cause the exception during open by Xls reader [#402](https://github.com/PHPOffice/PhpSpreadsheet/issues/402) |
|||
- Skip non numeric value in SUMIF [#618](https://github.com/PHPOffice/PhpSpreadsheet/pull/618) |
|||
- OFFSET should allow omitted height and width [#561](https://github.com/PHPOffice/PhpSpreadsheet/issues/561) |
|||
- Correctly determine delimiter when CSV contains line breaks inside enclosures [#716](https://github.com/PHPOffice/PhpSpreadsheet/issues/716) |
|||
|
|||
## 1.4.1 - 2018-09-30 |
|||
|
|||
### Fixed |
|||
|
|||
- Remove locale from formatting string [#644](https://github.com/PHPOffice/PhpSpreadsheet/pull/644) |
|||
- Allow iterators to go out of bounds with prev [#587](https://github.com/PHPOffice/PhpSpreadsheet/issues/587) |
|||
- Fix warning when reading xlsx without styles [#631](https://github.com/PHPOffice/PhpSpreadsheet/pull/631) |
|||
- Fix broken sample links on windows due to $baseDir having backslash [#653](https://github.com/PHPOffice/PhpSpreadsheet/pull/653) |
|||
|
|||
## 1.4.0 - 2018-08-06 |
|||
|
|||
### Added |
|||
|
|||
- Add excel function EXACT(value1, value2) support [#595](https://github.com/PHPOffice/PhpSpreadsheet/pull/595) |
|||
- Support workbook view attributes for Xlsx format [#523](https://github.com/PHPOffice/PhpSpreadsheet/issues/523) |
|||
- Read and write hyperlink for drawing image [#490](https://github.com/PHPOffice/PhpSpreadsheet/pull/490) |
|||
- Added calculation engine support for the new bitwise functions that were added in MS Excel 2013 |
|||
- BITAND() Returns a Bitwise 'And' of two numbers |
|||
- BITOR() Returns a Bitwise 'Or' of two number |
|||
- BITXOR() Returns a Bitwise 'Exclusive Or' of two numbers |
|||
- BITLSHIFT() Returns a number shifted left by a specified number of bits |
|||
- BITRSHIFT() Returns a number shifted right by a specified number of bits |
|||
- Added calculation engine support for other new functions that were added in MS Excel 2013 and MS Excel 2016 |
|||
- Text Functions |
|||
- CONCAT() Synonym for CONCATENATE() |
|||
- NUMBERVALUE() Converts text to a number, in a locale-independent way |
|||
- UNICHAR() Synonym for CHAR() in PHPSpreadsheet, which has always used UTF-8 internally |
|||
- UNIORD() Synonym for ORD() in PHPSpreadsheet, which has always used UTF-8 internally |
|||
- TEXTJOIN() Joins together two or more text strings, separated by a delimiter |
|||
- Logical Functions |
|||
- XOR() Returns a logical Exclusive Or of all arguments |
|||
- Date/Time Functions |
|||
- ISOWEEKNUM() Returns the ISO 8601 week number of the year for a given date |
|||
- Lookup and Reference Functions |
|||
- FORMULATEXT() Returns a formula as a string |
|||
- Financial Functions |
|||
- PDURATION() Calculates the number of periods required for an investment to reach a specified value |
|||
- RRI() Calculates the interest rate required for an investment to grow to a specified future value |
|||
- Engineering Functions |
|||
- ERF.PRECISE() Returns the error function integrated between 0 and a supplied limit |
|||
- ERFC.PRECISE() Synonym for ERFC |
|||
- Math and Trig Functions |
|||
- SEC() Returns the secant of an angle |
|||
- SECH() Returns the hyperbolic secant of an angle |
|||
- CSC() Returns the cosecant of an angle |
|||
- CSCH() Returns the hyperbolic cosecant of an angle |
|||
- COT() Returns the cotangent of an angle |
|||
- COTH() Returns the hyperbolic cotangent of an angle |
|||
- ACOT() Returns the cotangent of an angle |
|||
- ACOTH() Returns the hyperbolic cotangent of an angle |
|||
- Refactored Complex Engineering Functions to use external complex number library |
|||
- Added calculation engine support for the new complex number functions that were added in MS Excel 2013 |
|||
- IMCOSH() Returns the hyperbolic cosine of a complex number |
|||
- IMCOT() Returns the cotangent of a complex number |
|||
- IMCSC() Returns the cosecant of a complex number |
|||
- IMCSCH() Returns the hyperbolic cosecant of a complex number |
|||
- IMSEC() Returns the secant of a complex number |
|||
- IMSECH() Returns the hyperbolic secant of a complex number |
|||
- IMSINH() Returns the hyperbolic sine of a complex number |
|||
- IMTAN() Returns the tangent of a complex number |
|||
|
|||
### Fixed |
|||
|
|||
- Fix ISFORMULA() function to work with a cell reference to another worksheet |
|||
- Xlsx reader crashed when reading a file with workbook protection [#553](https://github.com/PHPOffice/PhpSpreadsheet/pull/553) |
|||
- Cell formats with escaped spaces were causing incorrect date formatting [#557](https://github.com/PHPOffice/PhpSpreadsheet/issues/557) |
|||
- Could not open CSV file containing HTML fragment [#564](https://github.com/PHPOffice/PhpSpreadsheet/issues/564) |
|||
- Exclude the vendor folder in migration [#481](https://github.com/PHPOffice/PhpSpreadsheet/issues/481) |
|||
- Chained operations on cell ranges involving borders operated on last cell only [#428](https://github.com/PHPOffice/PhpSpreadsheet/issues/428) |
|||
- Avoid memory exhaustion when cloning worksheet with a drawing [#437](https://github.com/PHPOffice/PhpSpreadsheet/issues/437) |
|||
- Migration tool keep variables containing $PHPExcel untouched [#598](https://github.com/PHPOffice/PhpSpreadsheet/issues/598) |
|||
- Rowspans/colspans were incorrect when adding worksheet using loadIntoExisting [#619](https://github.com/PHPOffice/PhpSpreadsheet/issues/619) |
|||
|
|||
## 1.3.1 - 2018-06-12 |
|||
|
|||
### Fixed |
|||
|
|||
- Ranges across Z and AA columns incorrectly threw an exception [#545](https://github.com/PHPOffice/PhpSpreadsheet/issues/545) |
|||
|
|||
## 1.3.0 - 2018-06-10 |
|||
|
|||
### Added |
|||
|
|||
- Support to read Xlsm templates with form elements, macros, printer settings, protected elements and back compatibility drawing, and save result without losing important elements of document [#435](https://github.com/PHPOffice/PhpSpreadsheet/issues/435) |
|||
- Expose sheet title maximum length as `Worksheet::SHEET_TITLE_MAXIMUM_LENGTH` [#482](https://github.com/PHPOffice/PhpSpreadsheet/issues/482) |
|||
- Allow escape character to be set in CSV reader [#492](https://github.com/PHPOffice/PhpSpreadsheet/issues/492) |
|||
|
|||
### Fixed |
|||
|
|||
- Subtotal 9 in a group that has other subtotals 9 exclude the totals of the other subtotals in the range [#332](https://github.com/PHPOffice/PhpSpreadsheet/issues/332) |
|||
- `Helper\Html` support UTF-8 HTML input [#444](https://github.com/PHPOffice/PhpSpreadsheet/issues/444) |
|||
- Xlsx loaded an extra empty comment for each real comment [#375](https://github.com/PHPOffice/PhpSpreadsheet/issues/375) |
|||
- Xlsx reader do not read rows and columns filtered out in readFilter at all [#370](https://github.com/PHPOffice/PhpSpreadsheet/issues/370) |
|||
- Make newer Excel versions properly recalculate formulas on document open [#456](https://github.com/PHPOffice/PhpSpreadsheet/issues/456) |
|||
- `Coordinate::extractAllCellReferencesInRange()` throws an exception for an invalid range [#519](https://github.com/PHPOffice/PhpSpreadsheet/issues/519) |
|||
- Fixed parsing of conditionals in COUNTIF functions [#526](https://github.com/PHPOffice/PhpSpreadsheet/issues/526) |
|||
- Corruption errors for saved Xlsx docs with frozen panes [#532](https://github.com/PHPOffice/PhpSpreadsheet/issues/532) |
|||
|
|||
## 1.2.1 - 2018-04-10 |
|||
|
|||
### Fixed |
|||
|
|||
- Plain text and richtext mixed in same cell can be read [#442](https://github.com/PHPOffice/PhpSpreadsheet/issues/442) |
|||
|
|||
## 1.2.0 - 2018-03-04 |
|||
|
|||
### Added |
|||
|
|||
- HTML writer creates a generator meta tag [#312](https://github.com/PHPOffice/PhpSpreadsheet/issues/312) |
|||
- Support invalid zoom value in XLSX format [#350](https://github.com/PHPOffice/PhpSpreadsheet/pull/350) |
|||
- Support for `_xlfn.` prefixed functions and `ISFORMULA`, `MODE.SNGL`, `STDEV.S`, `STDEV.P` [#390](https://github.com/PHPOffice/PhpSpreadsheet/pull/390) |
|||
|
|||
### Fixed |
|||
|
|||
- Avoid potentially unsupported PSR-16 cache keys [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354) |
|||
- Check for MIME type to know if CSV reader can read a file [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167) |
|||
- Use proper € symbol for currency format [#379](https://github.com/PHPOffice/PhpSpreadsheet/pull/379) |
|||
- Read printing area correctly when skipping some sheets [#371](https://github.com/PHPOffice/PhpSpreadsheet/issues/371) |
|||
- Avoid incorrectly overwriting calculated value type [#394](https://github.com/PHPOffice/PhpSpreadsheet/issues/394) |
|||
- Select correct cell when calling freezePane [#389](https://github.com/PHPOffice/PhpSpreadsheet/issues/389) |
|||
- `setStrikethrough()` did not set the font [#403](https://github.com/PHPOffice/PhpSpreadsheet/issues/403) |
|||
|
|||
## 1.1.0 - 2018-01-28 |
|||
|
|||
### Added |
|||
|
|||
- Support for PHP 7.2 |
|||
- Support cell comments in HTML writer and reader [#308](https://github.com/PHPOffice/PhpSpreadsheet/issues/308) |
|||
- Option to stop at a conditional styling, if it matches (only XLSX format) [#292](https://github.com/PHPOffice/PhpSpreadsheet/pull/292) |
|||
- Support for line width for data series when rendering Xlsx [#329](https://github.com/PHPOffice/PhpSpreadsheet/pull/329) |
|||
|
|||
### Fixed |
|||
|
|||
- Better auto-detection of CSV separators [#305](https://github.com/PHPOffice/PhpSpreadsheet/issues/305) |
|||
- Support for shape style ending with `;` [#304](https://github.com/PHPOffice/PhpSpreadsheet/issues/304) |
|||
- Freeze Panes takes wrong coordinates for XLSX [#322](https://github.com/PHPOffice/PhpSpreadsheet/issues/322) |
|||
- `COLUMNS` and `ROWS` functions crashed in some cases [#336](https://github.com/PHPOffice/PhpSpreadsheet/issues/336) |
|||
- Support XML file without styles [#331](https://github.com/PHPOffice/PhpSpreadsheet/pull/331) |
|||
- Cell coordinates which are already a range cause an exception [#319](https://github.com/PHPOffice/PhpSpreadsheet/issues/319) |
|||
|
|||
## 1.0.0 - 2017-12-25 |
|||
|
|||
### Added |
|||
|
|||
- Support to write merged cells in ODS format [#287](https://github.com/PHPOffice/PhpSpreadsheet/issues/287) |
|||
- Able to set the `topLeftCell` in freeze panes [#261](https://github.com/PHPOffice/PhpSpreadsheet/pull/261) |
|||
- Support `DateTimeImmutable` as cell value |
|||
- Support migration of prefixed classes |
|||
|
|||
### Fixed |
|||
|
|||
- Can read very small HTML files [#194](https://github.com/PHPOffice/PhpSpreadsheet/issues/194) |
|||
- Written DataValidation was corrupted [#290](https://github.com/PHPOffice/PhpSpreadsheet/issues/290) |
|||
- Date format compatible with both LibreOffice and Excel [#298](https://github.com/PHPOffice/PhpSpreadsheet/issues/298) |
|||
|
|||
### BREAKING CHANGE |
|||
|
|||
- Constant `TYPE_DOUGHTNUTCHART` is now `TYPE_DOUGHNUTCHART`. |
|||
|
|||
## 1.0.0-beta2 - 2017-11-26 |
|||
|
|||
### Added |
|||
|
|||
- Support for chart fill color - @CrazyBite [#158](https://github.com/PHPOffice/PhpSpreadsheet/pull/158) |
|||
- Support for read Hyperlink for xml - @GreatHumorist [#223](https://github.com/PHPOffice/PhpSpreadsheet/pull/223) |
|||
- Support for cell value validation according to data validation rules - @SailorMax [#257](https://github.com/PHPOffice/PhpSpreadsheet/pull/257) |
|||
- Support for custom implementation, or configuration, of PDF libraries - @SailorMax [#266](https://github.com/PHPOffice/PhpSpreadsheet/pull/266) |
|||
|
|||
### Changed |
|||
|
|||
- Merge data-validations to reduce written worksheet size - @billblume [#131](https://github.com/PHPOffice/PhpSpreadSheet/issues/131) |
|||
- Throws exception if a XML file is invalid - @GreatHumorist [#222](https://github.com/PHPOffice/PhpSpreadsheet/pull/222) |
|||
- Upgrade to mPDF 7.0+ [#144](https://github.com/PHPOffice/PhpSpreadsheet/issues/144) |
|||
|
|||
### Fixed |
|||
|
|||
- Control characters in cell values are automatically escaped [#212](https://github.com/PHPOffice/PhpSpreadsheet/issues/212) |
|||
- Prevent color changing when copy/pasting xls files written by PhpSpreadsheet to another file - @al-lala [#218](https://github.com/PHPOffice/PhpSpreadsheet/issues/218) |
|||
- Add cell reference automatic when there is no cell reference('r' attribute) in Xlsx file. - @GreatHumorist [#225](https://github.com/PHPOffice/PhpSpreadsheet/pull/225) Refer to [#201](https://github.com/PHPOffice/PhpSpreadsheet/issues/201) |
|||
- `Reader\Xlsx::getFromZipArchive()` function return false if the zip entry could not be located. - @anton-harvey [#268](https://github.com/PHPOffice/PhpSpreadsheet/pull/268) |
|||
|
|||
### BREAKING CHANGE |
|||
|
|||
- Extracted coordinate method to dedicate class [migration guide](./docs/topics/migration-from-PHPExcel.md). |
|||
- Column indexes are based on 1, see the [migration guide](./docs/topics/migration-from-PHPExcel.md). |
|||
- Standardization of array keys used for style, see the [migration guide](./docs/topics/migration-from-PHPExcel.md). |
|||
- Easier usage of PDF writers, and other custom readers and writers, see the [migration guide](./docs/topics/migration-from-PHPExcel.md). |
|||
- Easier usage of chart renderers, see the [migration guide](./docs/topics/migration-from-PHPExcel.md). |
|||
- Rename a few more classes to keep them in their related namespaces: |
|||
- `CalcEngine` => `Calculation\Engine` |
|||
- `PhpSpreadsheet\Calculation` => `PhpSpreadsheet\Calculation\Calculation` |
|||
- `PhpSpreadsheet\Cell` => `PhpSpreadsheet\Cell\Cell` |
|||
- `PhpSpreadsheet\Chart` => `PhpSpreadsheet\Chart\Chart` |
|||
- `PhpSpreadsheet\RichText` => `PhpSpreadsheet\RichText\RichText` |
|||
- `PhpSpreadsheet\Style` => `PhpSpreadsheet\Style\Style` |
|||
- `PhpSpreadsheet\Worksheet` => `PhpSpreadsheet\Worksheet\Worksheet` |
|||
|
|||
## 1.0.0-beta - 2017-08-17 |
|||
|
|||
### Added |
|||
|
|||
- Initial implementation of SUMIFS() function |
|||
- Additional codepages |
|||
- MemoryDrawing not working in HTML writer [#808](https://github.com/PHPOffice/PHPExcel/issues/808) |
|||
- CSV Reader can auto-detect the separator used in file [#141](https://github.com/PHPOffice/PhpSpreadsheet/pull/141) |
|||
- HTML Reader supports some basic inline styles [#180](https://github.com/PHPOffice/PhpSpreadsheet/pull/180) |
|||
|
|||
### Changed |
|||
|
|||
- Start following [SemVer](https://semver.org) properly. |
|||
|
|||
### Fixed |
|||
|
|||
- Fix to getCell() method when cell reference includes a worksheet reference - @MarkBaker |
|||
- Ignore inlineStr type if formula element exists - @ncrypthic [#570](https://github.com/PHPOffice/PHPExcel/issues/570) |
|||
- Excel 2007 Reader freezes because of conditional formatting - @rentalhost [#575](https://github.com/PHPOffice/PHPExcel/issues/575) |
|||
- Readers will now parse files containing worksheet titles over 31 characters [#176](https://github.com/PHPOffice/PhpSpreadsheet/pull/176) |
|||
- Fixed PHP8 deprecation warning for libxml_disable_entity_loader() [#1625](https://github.com/phpoffice/phpspreadsheet/pull/1625) |
|||
|
|||
### General |
|||
|
|||
- Whitespace after toRichTextObject() - @MarkBaker [#554](https://github.com/PHPOffice/PHPExcel/issues/554) |
|||
- Optimize vlookup() sort - @umpirsky [#548](https://github.com/PHPOffice/PHPExcel/issues/548) |
|||
- c:max and c:min elements shall NOT be inside c:orientation elements - @vitalyrepin [#869](https://github.com/PHPOffice/PHPExcel/pull/869) |
|||
- Implement actual timezone adjustment into PHPExcel_Shared_Date::PHPToExcel - @sim642 [#489](https://github.com/PHPOffice/PHPExcel/pull/489) |
|||
|
|||
### BREAKING CHANGE |
|||
|
|||
- Introduction of namespaces for all classes, eg: `PHPExcel_Calculation_Functions` becomes `PhpOffice\PhpSpreadsheet\Calculation\Functions` |
|||
- Some classes were renamed for clarity and/or consistency: |
|||
|
|||
For a comprehensive list of all class changes, and a semi-automated migration path, read the [migration guide](./docs/topics/migration-from-PHPExcel.md). |
|||
|
|||
- Dropped `PHPExcel_Calculation_Functions::VERSION()`. Composer or git should be used to know the version. |
|||
- Dropped `PHPExcel_Settings::setPdfRenderer()` and `PHPExcel_Settings::setPdfRenderer()`. Composer should be used to autoload PDF libs. |
|||
- Dropped support for HHVM |
|||
|
|||
## Previous versions of PHPExcel |
|||
|
|||
The changelog for the project when it was called PHPExcel is [still available](./CHANGELOG.PHPExcel.md). |
|||
@ -0,0 +1,20 @@ |
|||
# Want to contribute? |
|||
|
|||
If you would like to contribute, here are some notes and guidelines: |
|||
|
|||
- All new development happens on feature/fix branches, and are then merged to the `master` branch once stable; so the `master` branch is always the most up-to-date, working code |
|||
- Tagged releases are made from the `master` branch |
|||
- If you are going to be submitting a pull request, please fork from `master`, and submit your pull request back as a fix/feature branch referencing the GitHub issue number |
|||
- Code style might be automatically fixed by `composer fix` |
|||
- 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 |
|||
@ -0,0 +1,110 @@ |
|||
{ |
|||
"name": "phpoffice/phpspreadsheet", |
|||
"description": "PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine", |
|||
"keywords": [ |
|||
"PHP", |
|||
"OpenXML", |
|||
"Excel", |
|||
"xlsx", |
|||
"xls", |
|||
"ods", |
|||
"gnumeric", |
|||
"spreadsheet" |
|||
], |
|||
"config": { |
|||
"sort-packages": true, |
|||
"allow-plugins": { |
|||
"dealerdirect/phpcodesniffer-composer-installer": true |
|||
} |
|||
}, |
|||
"homepage": "https://github.com/PHPOffice/PhpSpreadsheet", |
|||
"type": "library", |
|||
"license": "MIT", |
|||
"authors": [ |
|||
{ |
|||
"name": "Maarten Balliauw", |
|||
"homepage": "https://blog.maartenballiauw.be" |
|||
}, |
|||
{ |
|||
"name": "Mark Baker", |
|||
"homepage": "https://markbakeruk.net" |
|||
}, |
|||
{ |
|||
"name": "Franck Lefevre", |
|||
"homepage": "https://rootslabs.net" |
|||
}, |
|||
{ |
|||
"name": "Erik Tilt" |
|||
}, |
|||
{ |
|||
"name": "Adrien Crivelli" |
|||
} |
|||
], |
|||
"scripts": { |
|||
"check": [ |
|||
"php-cs-fixer fix --ansi --dry-run --diff", |
|||
"phpcs", |
|||
"phpunit --color=always", |
|||
"phpstan analyse --ansi" |
|||
], |
|||
"fix": [ |
|||
"php-cs-fixer fix --ansi" |
|||
], |
|||
"versions": [ |
|||
"phpcs --report-width=200 samples/ src/ tests/ --ignore=samples/Header.php --standard=PHPCompatibility --runtime-set testVersion 7.3- -n" |
|||
] |
|||
}, |
|||
"require": { |
|||
"php": "^7.3 || ^8.0", |
|||
"ext-ctype": "*", |
|||
"ext-dom": "*", |
|||
"ext-fileinfo": "*", |
|||
"ext-gd": "*", |
|||
"ext-iconv": "*", |
|||
"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": "^3.0", |
|||
"markbaker/matrix": "^3.0", |
|||
"psr/http-client": "^1.0", |
|||
"psr/http-factory": "^1.0", |
|||
"psr/simple-cache": "^1.0" |
|||
}, |
|||
"require-dev": { |
|||
"dealerdirect/phpcodesniffer-composer-installer": "dev-master", |
|||
"dompdf/dompdf": "^1.0", |
|||
"friendsofphp/php-cs-fixer": "^3.2", |
|||
"jpgraph/jpgraph": "^4.0", |
|||
"mpdf/mpdf": "8.0.17", |
|||
"phpcompatibility/php-compatibility": "^9.3", |
|||
"phpstan/phpstan": "^1.1", |
|||
"phpstan/phpstan-phpunit": "^1.0", |
|||
"phpunit/phpunit": "^8.5 || ^9.0", |
|||
"squizlabs/php_codesniffer": "^3.6", |
|||
"tecnickcom/tcpdf": "^6.4" |
|||
}, |
|||
"suggest": { |
|||
"mpdf/mpdf": "Option for rendering PDF with PDF Writer", |
|||
"dompdf/dompdf": "Option for rendering PDF with PDF Writer (doesn't yet support PHP8)", |
|||
"tecnickcom/tcpdf": "Option for rendering PDF with PDF Writer (doesn't yet support PHP8)", |
|||
"jpgraph/jpgraph": "Option for rendering charts, or including charts with PDF or HTML Writers" |
|||
}, |
|||
"autoload": { |
|||
"psr-4": { |
|||
"PhpOffice\\PhpSpreadsheet\\": "src/PhpSpreadsheet" |
|||
} |
|||
}, |
|||
"autoload-dev": { |
|||
"psr-4": { |
|||
"PhpOffice\\PhpSpreadsheetTests\\": "tests/PhpSpreadsheetTests", |
|||
"PhpOffice\\PhpSpreadsheetInfra\\": "infra" |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,56 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Engine\ArrayArgumentHelper; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Engine\ArrayArgumentProcessor; |
|||
|
|||
trait ArrayEnabled |
|||
{ |
|||
/** |
|||
* @var ArrayArgumentHelper |
|||
*/ |
|||
private static $arrayArgumentHelper; |
|||
|
|||
private static function initialiseHelper(array $arguments): void |
|||
{ |
|||
if (self::$arrayArgumentHelper === null) { |
|||
self::$arrayArgumentHelper = new ArrayArgumentHelper(); |
|||
} |
|||
self::$arrayArgumentHelper->initialise($arguments); |
|||
} |
|||
|
|||
protected static function evaluateSingleArgumentArray(callable $method, array $values): array |
|||
{ |
|||
$result = []; |
|||
foreach ($values as $value) { |
|||
$result[] = $method($value); |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed ...$arguments |
|||
*/ |
|||
protected static function evaluateArrayArguments(callable $method, ...$arguments): array |
|||
{ |
|||
self::initialiseHelper($arguments); |
|||
$arguments = self::$arrayArgumentHelper->arguments(); |
|||
|
|||
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments); |
|||
} |
|||
|
|||
/** |
|||
* @param mixed ...$arguments |
|||
*/ |
|||
protected static function evaluateArrayArgumentsSubset(callable $method, int $limit, ...$arguments): array |
|||
{ |
|||
self::initialiseHelper(array_slice($arguments, 0, $limit)); |
|||
$trailingArguments = array_slice($arguments, $limit); |
|||
$arguments = self::$arrayArgumentHelper->arguments(); |
|||
$arguments = array_merge($arguments, $trailingArguments); |
|||
|
|||
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments); |
|||
} |
|||
} |
|||
@ -0,0 +1,181 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
|||
|
|||
class BinaryComparison |
|||
{ |
|||
/** |
|||
* Epsilon Precision used for comparisons in calculations. |
|||
*/ |
|||
private const DELTA = 0.1e-12; |
|||
|
|||
/** |
|||
* Compare two strings in the same way as strcmp() except that lowercase come before uppercase letters. |
|||
* |
|||
* @param null|string $str1 First string value for the comparison |
|||
* @param null|string $str2 Second string value for the comparison |
|||
*/ |
|||
private static function strcmpLowercaseFirst($str1, $str2): int |
|||
{ |
|||
$inversedStr1 = StringHelper::strCaseReverse($str1 ?? ''); |
|||
$inversedStr2 = StringHelper::strCaseReverse($str2 ?? ''); |
|||
|
|||
return strcmp($inversedStr1, $inversedStr2); |
|||
} |
|||
|
|||
/** |
|||
* PHP8.1 deprecates passing null to strcmp. |
|||
* |
|||
* @param null|string $str1 First string value for the comparison |
|||
* @param null|string $str2 Second string value for the comparison |
|||
*/ |
|||
private static function strcmpAllowNull($str1, $str2): int |
|||
{ |
|||
return strcmp($str1 ?? '', $str2 ?? ''); |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $operand1 |
|||
* @param mixed $operand2 |
|||
*/ |
|||
public static function compare($operand1, $operand2, string $operator): bool |
|||
{ |
|||
// Simple validate the two operands if they are string values |
|||
if (is_string($operand1) && $operand1 > '' && $operand1[0] == Calculation::FORMULA_STRING_QUOTE) { |
|||
$operand1 = Calculation::unwrapResult($operand1); |
|||
} |
|||
if (is_string($operand2) && $operand2 > '' && $operand2[0] == Calculation::FORMULA_STRING_QUOTE) { |
|||
$operand2 = Calculation::unwrapResult($operand2); |
|||
} |
|||
|
|||
// Use case insensitive comparaison if not OpenOffice mode |
|||
if (Functions::getCompatibilityMode() != Functions::COMPATIBILITY_OPENOFFICE) { |
|||
if (is_string($operand1)) { |
|||
$operand1 = StringHelper::strToUpper($operand1); |
|||
} |
|||
if (is_string($operand2)) { |
|||
$operand2 = StringHelper::strToUpper($operand2); |
|||
} |
|||
} |
|||
|
|||
$useLowercaseFirstComparison = is_string($operand1) && |
|||
is_string($operand2) && |
|||
Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE; |
|||
|
|||
return self::evaluateComparison($operand1, $operand2, $operator, $useLowercaseFirstComparison); |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $operand1 |
|||
* @param mixed $operand2 |
|||
*/ |
|||
private static function evaluateComparison($operand1, $operand2, string $operator, bool $useLowercaseFirstComparison): bool |
|||
{ |
|||
switch ($operator) { |
|||
// Equality |
|||
case '=': |
|||
return self::equal($operand1, $operand2); |
|||
// Greater than |
|||
case '>': |
|||
return self::greaterThan($operand1, $operand2, $useLowercaseFirstComparison); |
|||
// Less than |
|||
case '<': |
|||
return self::lessThan($operand1, $operand2, $useLowercaseFirstComparison); |
|||
// Greater than or equal |
|||
case '>=': |
|||
return self::greaterThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison); |
|||
// Less than or equal |
|||
case '<=': |
|||
return self::lessThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison); |
|||
// Inequality |
|||
case '<>': |
|||
return self::notEqual($operand1, $operand2); |
|||
default: |
|||
throw new Exception('Unsupported binary comparison operator'); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $operand1 |
|||
* @param mixed $operand2 |
|||
*/ |
|||
private static function equal($operand1, $operand2): bool |
|||
{ |
|||
if (is_numeric($operand1) && is_numeric($operand2)) { |
|||
$result = (abs($operand1 - $operand2) < self::DELTA); |
|||
} elseif (($operand1 === null && is_numeric($operand2)) || ($operand2 === null && is_numeric($operand1))) { |
|||
$result = $operand1 == $operand2; |
|||
} else { |
|||
$result = self::strcmpAllowNull($operand1, $operand2) == 0; |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $operand1 |
|||
* @param mixed $operand2 |
|||
*/ |
|||
private static function greaterThanOrEqual($operand1, $operand2, bool $useLowercaseFirstComparison): bool |
|||
{ |
|||
if (is_numeric($operand1) && is_numeric($operand2)) { |
|||
$result = ((abs($operand1 - $operand2) < self::DELTA) || ($operand1 > $operand2)); |
|||
} elseif (($operand1 === null && is_numeric($operand2)) || ($operand2 === null && is_numeric($operand1))) { |
|||
$result = $operand1 >= $operand2; |
|||
} elseif ($useLowercaseFirstComparison) { |
|||
$result = self::strcmpLowercaseFirst($operand1, $operand2) >= 0; |
|||
} else { |
|||
$result = self::strcmpAllowNull($operand1, $operand2) >= 0; |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $operand1 |
|||
* @param mixed $operand2 |
|||
*/ |
|||
private static function lessThanOrEqual($operand1, $operand2, bool $useLowercaseFirstComparison): bool |
|||
{ |
|||
if (is_numeric($operand1) && is_numeric($operand2)) { |
|||
$result = ((abs($operand1 - $operand2) < self::DELTA) || ($operand1 < $operand2)); |
|||
} elseif (($operand1 === null && is_numeric($operand2)) || ($operand2 === null && is_numeric($operand1))) { |
|||
$result = $operand1 <= $operand2; |
|||
} elseif ($useLowercaseFirstComparison) { |
|||
$result = self::strcmpLowercaseFirst($operand1, $operand2) <= 0; |
|||
} else { |
|||
$result = self::strcmpAllowNull($operand1, $operand2) <= 0; |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $operand1 |
|||
* @param mixed $operand2 |
|||
*/ |
|||
private static function greaterThan($operand1, $operand2, bool $useLowercaseFirstComparison): bool |
|||
{ |
|||
return self::lessThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison) !== true; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $operand1 |
|||
* @param mixed $operand2 |
|||
*/ |
|||
private static function lessThan($operand1, $operand2, bool $useLowercaseFirstComparison): bool |
|||
{ |
|||
return self::greaterThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison) !== true; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $operand1 |
|||
* @param mixed $operand2 |
|||
*/ |
|||
private static function notEqual($operand1, $operand2): bool |
|||
{ |
|||
return self::equal($operand1, $operand2) !== true; |
|||
} |
|||
} |
|||
File diff suppressed because it is too large
@ -0,0 +1,20 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation; |
|||
|
|||
abstract class Category |
|||
{ |
|||
// Function categories |
|||
const CATEGORY_CUBE = 'Cube'; |
|||
const CATEGORY_DATABASE = 'Database'; |
|||
const CATEGORY_DATE_AND_TIME = 'Date and Time'; |
|||
const CATEGORY_ENGINEERING = 'Engineering'; |
|||
const CATEGORY_FINANCIAL = 'Financial'; |
|||
const CATEGORY_INFORMATION = 'Information'; |
|||
const CATEGORY_LOGICAL = 'Logical'; |
|||
const CATEGORY_LOOKUP_AND_REFERENCE = 'Lookup and Reference'; |
|||
const CATEGORY_MATH_AND_TRIG = 'Math and Trig'; |
|||
const CATEGORY_STATISTICAL = 'Statistical'; |
|||
const CATEGORY_TEXT_AND_DATA = 'Text and Data'; |
|||
const CATEGORY_WEB = 'Web'; |
|||
} |
|||
@ -0,0 +1,440 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation; |
|||
|
|||
/** |
|||
* @deprecated 1.17.0 |
|||
*/ |
|||
class Database |
|||
{ |
|||
/** |
|||
* DAVERAGE. |
|||
* |
|||
* Averages the values in a column of a list or database that match conditions you specify. |
|||
* |
|||
* Excel Function: |
|||
* DAVERAGE(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return null|float|string |
|||
*/ |
|||
public static function DAVERAGE($database, $field, $criteria) |
|||
{ |
|||
return Database\DAverage::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DCOUNT. |
|||
* |
|||
* Counts the cells that contain numbers in a column of a list or database that match conditions |
|||
* that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DCOUNT(database,[field],criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param null|int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return int |
|||
* |
|||
* @TODO The field argument is optional. If field is omitted, DCOUNT counts all records in the |
|||
* database that match the criteria. |
|||
*/ |
|||
public static function DCOUNT($database, $field, $criteria) |
|||
{ |
|||
return Database\DCount::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DCOUNTA. |
|||
* |
|||
* Counts the nonblank cells in a column of a list or database that match conditions that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DCOUNTA(database,[field],criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param null|int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return int |
|||
*/ |
|||
public static function DCOUNTA($database, $field, $criteria) |
|||
{ |
|||
return Database\DCountA::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DGET. |
|||
* |
|||
* Extracts a single value from a column of a list or database that matches conditions that you |
|||
* specify. |
|||
* |
|||
* Excel Function: |
|||
* DGET(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return mixed |
|||
*/ |
|||
public static function DGET($database, $field, $criteria) |
|||
{ |
|||
return Database\DGet::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DMAX. |
|||
* |
|||
* Returns the largest number in a column of a list or database that matches conditions you that |
|||
* specify. |
|||
* |
|||
* Excel Function: |
|||
* DMAX(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return float |
|||
*/ |
|||
public static function DMAX($database, $field, $criteria) |
|||
{ |
|||
return Database\DMax::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DMIN. |
|||
* |
|||
* Returns the smallest number in a column of a list or database that matches conditions you that |
|||
* specify. |
|||
* |
|||
* Excel Function: |
|||
* DMIN(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return float |
|||
*/ |
|||
public static function DMIN($database, $field, $criteria) |
|||
{ |
|||
return Database\DMin::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DPRODUCT. |
|||
* |
|||
* Multiplies the values in a column of a list or database that match conditions that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DPRODUCT(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function DPRODUCT($database, $field, $criteria) |
|||
{ |
|||
return Database\DProduct::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DSTDEV. |
|||
* |
|||
* Estimates the standard deviation of a population based on a sample by using the numbers in a |
|||
* column of a list or database that match conditions that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DSTDEV(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function DSTDEV($database, $field, $criteria) |
|||
{ |
|||
return Database\DStDev::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DSTDEVP. |
|||
* |
|||
* Calculates the standard deviation of a population based on the entire population by using the |
|||
* numbers in a column of a list or database that match conditions that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DSTDEVP(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function DSTDEVP($database, $field, $criteria) |
|||
{ |
|||
return Database\DStDevP::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DSUM. |
|||
* |
|||
* Adds the numbers in a column of a list or database that match conditions that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DSUM(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function DSUM($database, $field, $criteria) |
|||
{ |
|||
return Database\DSum::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DVAR. |
|||
* |
|||
* Estimates the variance of a population based on a sample by using the numbers in a column |
|||
* of a list or database that match conditions that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DVAR(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return float|string (string if result is an error) |
|||
*/ |
|||
public static function DVAR($database, $field, $criteria) |
|||
{ |
|||
return Database\DVar::evaluate($database, $field, $criteria); |
|||
} |
|||
|
|||
/** |
|||
* DVARP. |
|||
* |
|||
* Calculates the variance of a population based on the entire population by using the numbers |
|||
* in a column of a list or database that match conditions that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DVARP(database,field,criteria) |
|||
* |
|||
* @Deprecated 1.17.0 |
|||
* |
|||
* @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 |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return float|string (string if result is an error) |
|||
*/ |
|||
public static function DVARP($database, $field, $criteria) |
|||
{ |
|||
return Database\DVarP::evaluate($database, $field, $criteria); |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Database; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Averages; |
|||
|
|||
class DAverage extends DatabaseAbstract |
|||
{ |
|||
/** |
|||
* DAVERAGE. |
|||
* |
|||
* Averages the values in a column of a list or database that match conditions you specify. |
|||
* |
|||
* Excel Function: |
|||
* DAVERAGE(database,field,criteria) |
|||
* |
|||
* @param mixed[] $database The range of cells that makes up the list or database. |
|||
* A database is a list of related data in which rows of related |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return null|float|string |
|||
*/ |
|||
public static function evaluate($database, $field, $criteria) |
|||
{ |
|||
$field = self::fieldExtract($database, $field); |
|||
if ($field === null) { |
|||
return null; |
|||
} |
|||
|
|||
return Averages::average( |
|||
self::getFilteredColumn($database, $field, $criteria) |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Database; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Counts; |
|||
|
|||
class DCount extends DatabaseAbstract |
|||
{ |
|||
/** |
|||
* DCOUNT. |
|||
* |
|||
* Counts the cells that contain numbers in a column of a list or database that match conditions |
|||
* that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DCOUNT(database,[field],criteria) |
|||
* |
|||
* @param mixed[] $database The range of cells that makes up the list or database. |
|||
* A database is a list of related data in which rows of related |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param null|int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return int |
|||
*/ |
|||
public static function evaluate($database, $field, $criteria) |
|||
{ |
|||
$field = self::fieldExtract($database, $field); |
|||
|
|||
return Counts::COUNT( |
|||
self::getFilteredColumn($database, $field, $criteria) |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Database; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Counts; |
|||
|
|||
class DCountA extends DatabaseAbstract |
|||
{ |
|||
/** |
|||
* DCOUNTA. |
|||
* |
|||
* Counts the nonblank cells in a column of a list or database that match conditions that you specify. |
|||
* |
|||
* Excel Function: |
|||
* DCOUNTA(database,[field],criteria) |
|||
* |
|||
* @param mixed[] $database The range of cells that makes up the list or database. |
|||
* A database is a list of related data in which rows of related |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return int |
|||
*/ |
|||
public static function evaluate($database, $field, $criteria) |
|||
{ |
|||
$field = self::fieldExtract($database, $field); |
|||
|
|||
return Counts::COUNTA( |
|||
self::getFilteredColumn($database, $field ?? 0, $criteria) |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Database; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class DGet extends DatabaseAbstract |
|||
{ |
|||
/** |
|||
* DGET. |
|||
* |
|||
* Extracts a single value from a column of a list or database that matches conditions that you |
|||
* specify. |
|||
* |
|||
* Excel Function: |
|||
* DGET(database,field,criteria) |
|||
* |
|||
* @param mixed[] $database The range of cells that makes up the list or database. |
|||
* A database is a list of related data in which rows of related |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param int|string $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return mixed |
|||
*/ |
|||
public static function evaluate($database, $field, $criteria) |
|||
{ |
|||
$field = self::fieldExtract($database, $field); |
|||
if ($field === null) { |
|||
return null; |
|||
} |
|||
|
|||
$columnData = self::getFilteredColumn($database, $field, $criteria); |
|||
if (count($columnData) > 1) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$row = array_pop($columnData); |
|||
|
|||
return array_pop($row); |
|||
} |
|||
} |
|||
@ -0,0 +1,174 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Database; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Internal\WildcardMatch; |
|||
|
|||
abstract class DatabaseAbstract |
|||
{ |
|||
abstract public static function evaluate($database, $field, $criteria); |
|||
|
|||
/** |
|||
* fieldExtract. |
|||
* |
|||
* Extracts the column ID to use for the data field. |
|||
* |
|||
* @param mixed[] $database The range of cells that makes up the list or database. |
|||
* A database is a list of related data in which rows of related |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param mixed $field Indicates which column is used in the function. Enter the |
|||
* column label enclosed between double quotation marks, such as |
|||
* "Age" or "Yield," or a number (without quotation marks) that |
|||
* represents the position of the column within the list: 1 for |
|||
* the first column, 2 for the second column, and so on. |
|||
*/ |
|||
protected static function fieldExtract(array $database, $field): ?int |
|||
{ |
|||
$field = strtoupper(Functions::flattenSingleValue($field ?? '')); |
|||
if ($field === '') { |
|||
return null; |
|||
} |
|||
|
|||
$fieldNames = array_map('strtoupper', array_shift($database)); |
|||
if (is_numeric($field)) { |
|||
return ((int) $field) - 1; |
|||
} |
|||
$key = array_search($field, array_values($fieldNames), true); |
|||
|
|||
return ($key !== false) ? (int) $key : null; |
|||
} |
|||
|
|||
/** |
|||
* filter. |
|||
* |
|||
* Parses the selection criteria, extracts the database rows that match those criteria, and |
|||
* returns that subset of rows. |
|||
* |
|||
* @param mixed[] $database The range of cells that makes up the list or database. |
|||
* A database is a list of related data in which rows of related |
|||
* information are records, and columns of data are fields. The |
|||
* first row of the list contains labels for each column. |
|||
* @param mixed[] $criteria The range of cells that contains the conditions you specify. |
|||
* You can use any range for the criteria argument, as long as it |
|||
* includes at least one column label and at least one cell below |
|||
* the column label in which you specify a condition for the |
|||
* column. |
|||
* |
|||
* @return mixed[] |
|||
*/ |
|||
protected static function filter(array $database, array $criteria): array |
|||
{ |
|||
$fieldNames = array_shift($database); |
|||
$criteriaNames = array_shift($criteria); |
|||
|
|||
// Convert the criteria into a set of AND/OR conditions with [:placeholders] |
|||
$query = self::buildQuery($criteriaNames, $criteria); |
|||
|
|||
// Loop through each row of the database |
|||
return self::executeQuery($database, $query, $criteriaNames, $fieldNames); |
|||
} |
|||
|
|||
protected static function getFilteredColumn(array $database, ?int $field, array $criteria): array |
|||
{ |
|||
// reduce the database to a set of rows that match all the criteria |
|||
$database = self::filter($database, $criteria); |
|||
$defaultReturnColumnValue = ($field === null) ? 1 : null; |
|||
|
|||
// extract an array of values for the requested column |
|||
$columnData = []; |
|||
foreach ($database as $rowKey => $row) { |
|||
$keys = array_keys($row); |
|||
$key = $keys[$field] ?? null; |
|||
$columnKey = $key ?? 'A'; |
|||
$columnData[$rowKey][$columnKey] = $row[$key] ?? $defaultReturnColumnValue; |
|||
} |
|||
|
|||
return $columnData; |
|||
} |
|||
|
|||
private static function buildQuery(array $criteriaNames, array $criteria): string |
|||
{ |
|||
$baseQuery = []; |
|||
foreach ($criteria as $key => $criterion) { |
|||
foreach ($criterion as $field => $value) { |
|||
$criterionName = $criteriaNames[$field]; |
|||
if ($value !== null) { |
|||
$condition = self::buildCondition($value, $criterionName); |
|||
$baseQuery[$key][] = $condition; |
|||
} |
|||
} |
|||
} |
|||
|
|||
$rowQuery = array_map( |
|||
function ($rowValue) { |
|||
return (count($rowValue) > 1) ? 'AND(' . implode(',', $rowValue) . ')' : ($rowValue[0] ?? ''); |
|||
}, |
|||
$baseQuery |
|||
); |
|||
|
|||
return (count($rowQuery) > 1) ? 'OR(' . implode(',', $rowQuery) . ')' : ($rowQuery[0] ?? ''); |
|||
} |
|||
|
|||
private static function buildCondition($criterion, string $criterionName): string |
|||
{ |
|||
$ifCondition = Functions::ifCondition($criterion); |
|||
|
|||
// Check for wildcard characters used in the condition |
|||
$result = preg_match('/(?<operator>[^"]*)(?<operand>".*[*?].*")/ui', $ifCondition, $matches); |
|||
if ($result !== 1) { |
|||
return "[:{$criterionName}]{$ifCondition}"; |
|||
} |
|||
|
|||
$trueFalse = ($matches['operator'] !== '<>'); |
|||
$wildcard = WildcardMatch::wildcard($matches['operand']); |
|||
$condition = "WILDCARDMATCH([:{$criterionName}],{$wildcard})"; |
|||
if ($trueFalse === false) { |
|||
$condition = "NOT({$condition})"; |
|||
} |
|||
|
|||
return $condition; |
|||
} |
|||
|
|||
private static function executeQuery(array $database, string $query, array $criteria, array $fields): array |
|||
{ |
|||
foreach ($database as $dataRow => $dataValues) { |
|||
// Substitute actual values from the database row for our [:placeholders] |
|||
$conditions = $query; |
|||
foreach ($criteria as $criterion) { |
|||
$conditions = self::processCondition($criterion, $fields, $dataValues, $conditions); |
|||
} |
|||
|
|||
// evaluate the criteria against the row data |
|||
$result = Calculation::getInstance()->_calculateFormulaValue('=' . $conditions); |
|||
|
|||
// If the row failed to meet the criteria, remove it from the database |
|||
if ($result !== true) { |
|||
unset($database[$dataRow]); |
|||
} |
|||
} |
|||
|
|||
return $database; |
|||
} |
|||
|
|||
private static function processCondition(string $criterion, array $fields, array $dataValues, string $conditions) |
|||
{ |
|||
$key = array_search($criterion, $fields, true); |
|||
|
|||
$dataValue = 'NULL'; |
|||
if (is_bool($dataValues[$key])) { |
|||
$dataValue = ($dataValues[$key]) ? 'TRUE' : 'FALSE'; |
|||
} elseif ($dataValues[$key] !== null) { |
|||
$dataValue = $dataValues[$key]; |
|||
// escape quotes if we have a string containing quotes |
|||
if (is_string($dataValue) && strpos($dataValue, '"') !== false) { |
|||
$dataValue = str_replace('"', '""', $dataValue); |
|||
} |
|||
$dataValue = (is_string($dataValue)) ? Calculation::wrapResult(strtoupper($dataValue)) : $dataValue; |
|||
} |
|||
|
|||
return str_replace('[:' . $criterion . ']', $dataValue, $conditions); |
|||
} |
|||
} |
|||
@ -0,0 +1,915 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation; |
|||
|
|||
use DateTimeInterface; |
|||
|
|||
/** |
|||
* @deprecated 1.18.0 |
|||
*/ |
|||
class DateTime |
|||
{ |
|||
/** |
|||
* Identify if a year is a leap year or not. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Helpers::isLeapYear() |
|||
* Use the isLeapYear method in the DateTimeExcel\Helpers class instead |
|||
* |
|||
* @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) |
|||
{ |
|||
return DateTimeExcel\Helpers::isLeapYear($year); |
|||
} |
|||
|
|||
/** |
|||
* getDateValue. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Helpers::getDateValue() |
|||
* Use the getDateValue method in the DateTimeExcel\Helpers class instead |
|||
* |
|||
* @param mixed $dateValue |
|||
* |
|||
* @return mixed Excel date/time serial value, or string if error |
|||
*/ |
|||
public static function getDateValue($dateValue) |
|||
{ |
|||
try { |
|||
return DateTimeExcel\Helpers::getDateValue($dateValue); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 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() |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Current::now() |
|||
* Use the now method in the DateTimeExcel\Current class instead |
|||
* |
|||
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
|||
* depending on the value of the ReturnDateType flag |
|||
*/ |
|||
public static function DATETIMENOW() |
|||
{ |
|||
return DateTimeExcel\Current::now(); |
|||
} |
|||
|
|||
/** |
|||
* 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() |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Current::today() |
|||
* Use the today method in the DateTimeExcel\Current class instead |
|||
* |
|||
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
|||
* depending on the value of the ReturnDateType flag |
|||
*/ |
|||
public static function DATENOW() |
|||
{ |
|||
return DateTimeExcel\Current::today(); |
|||
} |
|||
|
|||
/** |
|||
* 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) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Date::fromYMD() |
|||
* Use the fromYMD method in the DateTimeExcel\Date class instead |
|||
* |
|||
* 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 DATE($year = 0, $month = 1, $day = 1) |
|||
{ |
|||
return DateTimeExcel\Date::fromYMD($year, $month, $day); |
|||
} |
|||
|
|||
/** |
|||
* 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) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Time::fromHMS() |
|||
* Use the fromHMS method in the DateTimeExcel\Time class instead |
|||
* |
|||
* @param int $hour A number from 0 (zero) to 32767 representing the hour. |
|||
* Any value greater than 23 will be divided by 24 and the remainder |
|||
* will be treated as the hour value. For example, TIME(27,0,0) = |
|||
* TIME(3,0,0) = .125 or 3:00 AM. |
|||
* @param int $minute A number from 0 to 32767 representing the minute. |
|||
* Any value greater than 59 will be converted to hours and minutes. |
|||
* For example, TIME(0,750,0) = TIME(12,30,0) = .520833 or 12:30 PM. |
|||
* @param int $second A number from 0 to 32767 representing the second. |
|||
* Any value greater than 59 will be converted to hours, minutes, |
|||
* and seconds. For example, TIME(0,0,2000) = TIME(0,33,22) = .023148 |
|||
* or 12:33:20 AM |
|||
* |
|||
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
|||
* depending on the value of the ReturnDateType flag |
|||
*/ |
|||
public static function TIME($hour = 0, $minute = 0, $second = 0) |
|||
{ |
|||
return DateTimeExcel\Time::fromHMS($hour, $minute, $second); |
|||
} |
|||
|
|||
/** |
|||
* 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) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\DateValue::fromString() |
|||
* Use the fromString method in the DateTimeExcel\DateValue class instead |
|||
* |
|||
* @param string $dateValue Text that represents a date in a Microsoft Excel date format. |
|||
* For example, "1/30/2008" or "30-Jan-2008" are text strings within |
|||
* quotation marks that represent dates. Using the default date |
|||
* system in Excel for Windows, date_text must represent a date from |
|||
* January 1, 1900, to December 31, 9999. Using the default date |
|||
* system in Excel for the Macintosh, date_text must represent a date |
|||
* from January 1, 1904, to December 31, 9999. DATEVALUE returns the |
|||
* #VALUE! error value if date_text is out of this range. |
|||
* |
|||
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
|||
* depending on the value of the ReturnDateType flag |
|||
*/ |
|||
public static function DATEVALUE($dateValue) |
|||
{ |
|||
return DateTimeExcel\DateValue::fromString($dateValue); |
|||
} |
|||
|
|||
/** |
|||
* 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) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\TimeValue::fromString() |
|||
* Use the fromString method in the DateTimeExcel\TimeValue class instead |
|||
* |
|||
* @param string $timeValue A text string that represents a time in any one of the Microsoft |
|||
* Excel time formats; for example, "6:45 PM" and "18:45" text strings |
|||
* within quotation marks that represent time. |
|||
* Date information in time_text is ignored. |
|||
* |
|||
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
|||
* depending on the value of the ReturnDateType flag |
|||
*/ |
|||
public static function TIMEVALUE($timeValue) |
|||
{ |
|||
return DateTimeExcel\TimeValue::fromString($timeValue); |
|||
} |
|||
|
|||
/** |
|||
* DATEDIF. |
|||
* |
|||
* Excel Function: |
|||
* DATEDIF(startdate, enddate, unit) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Difference::interval() |
|||
* Use the interval method in the DateTimeExcel\Difference class instead |
|||
* |
|||
* @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 array|string $unit |
|||
* |
|||
* @return array|int|string Interval between the dates |
|||
*/ |
|||
public static function DATEDIF($startDate = 0, $endDate = 0, $unit = 'D') |
|||
{ |
|||
return DateTimeExcel\Difference::interval($startDate, $endDate, $unit); |
|||
} |
|||
|
|||
/** |
|||
* DAYS. |
|||
* |
|||
* Returns the number of days between two dates |
|||
* |
|||
* Excel Function: |
|||
* DAYS(endDate, startDate) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Days::between() |
|||
* Use the between method in the DateTimeExcel\Days class instead |
|||
* |
|||
* @param array|DateTimeInterface|float|int|string $endDate Excel date serial value (float), |
|||
* PHP date timestamp (integer), PHP DateTime object, or a standard date string |
|||
* @param array|DateTimeInterface|float|int|string $startDate Excel date serial value (float), |
|||
* PHP date timestamp (integer), PHP DateTime object, or a standard date string |
|||
* |
|||
* @return array|int|string Number of days between start date and end date or an error |
|||
*/ |
|||
public static function DAYS($endDate = 0, $startDate = 0) |
|||
{ |
|||
return DateTimeExcel\Days::between($endDate, $startDate); |
|||
} |
|||
|
|||
/** |
|||
* 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]) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Days360::between() |
|||
* Use the between method in the DateTimeExcel\Days360 class instead |
|||
* |
|||
* @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 array|bool $method US or European Method |
|||
* FALSE or omitted: U.S. (NASD) method. If the starting date is |
|||
* the last day of a month, it becomes equal to the 30th of the |
|||
* same month. If the ending date is the last day of a month and |
|||
* the starting date is earlier than the 30th of a month, the |
|||
* ending date becomes equal to the 1st of the next month; |
|||
* otherwise the ending date becomes equal to the 30th of the |
|||
* same month. |
|||
* TRUE: European method. Starting dates and ending dates that |
|||
* occur on the 31st of a month become equal to the 30th of the |
|||
* same month. |
|||
* |
|||
* @return array|int|string Number of days between start date and end date |
|||
*/ |
|||
public static function DAYS360($startDate = 0, $endDate = 0, $method = false) |
|||
{ |
|||
return DateTimeExcel\Days360::between($startDate, $endDate, $method); |
|||
} |
|||
|
|||
/** |
|||
* YEARFRAC. |
|||
* |
|||
* Calculates the fraction of the year represented by the number of whole days between two dates |
|||
* (the start_date and the end_date). |
|||
* Use the YEARFRAC worksheet function to identify the proportion of a whole year's benefits or |
|||
* obligations to assign to a specific term. |
|||
* |
|||
* Excel Function: |
|||
* YEARFRAC(startDate,endDate[,method]) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\YearFrac::fraction() |
|||
* Use the fraction method in the DateTimeExcel\YearFrac class instead |
|||
* |
|||
* 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 array|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 array|float|string fraction of the year, or a string containing an error |
|||
*/ |
|||
public static function YEARFRAC($startDate = 0, $endDate = 0, $method = 0) |
|||
{ |
|||
return DateTimeExcel\YearFrac::fraction($startDate, $endDate, $method); |
|||
} |
|||
|
|||
/** |
|||
* 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[,...]]]) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\NetworkDays::count() |
|||
* Use the count method in the DateTimeExcel\NetworkDays class instead |
|||
* |
|||
* @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 array|int|string Interval between the dates |
|||
*/ |
|||
public static function NETWORKDAYS($startDate, $endDate, ...$dateArgs) |
|||
{ |
|||
return DateTimeExcel\NetworkDays::count($startDate, $endDate, ...$dateArgs); |
|||
} |
|||
|
|||
/** |
|||
* 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[,...]]]) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\WorkDay::date() |
|||
* Use the date method in the DateTimeExcel\WorkDay class instead |
|||
* |
|||
* @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 WORKDAY($startDate, $endDays, ...$dateArgs) |
|||
{ |
|||
return DateTimeExcel\WorkDay::date($startDate, $endDays, ...$dateArgs); |
|||
} |
|||
|
|||
/** |
|||
* 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) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\DateParts::day() |
|||
* Use the day method in the DateTimeExcel\DateParts class instead |
|||
* |
|||
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* |
|||
* @return array|int|string Day of the month |
|||
*/ |
|||
public static function DAYOFMONTH($dateValue = 1) |
|||
{ |
|||
return DateTimeExcel\DateParts::day($dateValue); |
|||
} |
|||
|
|||
/** |
|||
* 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]) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Week::day() |
|||
* Use the day method in the DateTimeExcel\Week class instead |
|||
* |
|||
* @param float|int|string $dateValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* @param int $style A number that determines the type of return value |
|||
* 1 or omitted Numbers 1 (Sunday) through 7 (Saturday). |
|||
* 2 Numbers 1 (Monday) through 7 (Sunday). |
|||
* 3 Numbers 0 (Monday) through 6 (Sunday). |
|||
* |
|||
* @return array|int|string Day of the week value |
|||
*/ |
|||
public static function WEEKDAY($dateValue = 1, $style = 1) |
|||
{ |
|||
return DateTimeExcel\Week::day($dateValue, $style); |
|||
} |
|||
|
|||
/** |
|||
* STARTWEEK_SUNDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_SUNDAY instead |
|||
*/ |
|||
const STARTWEEK_SUNDAY = 1; |
|||
|
|||
/** |
|||
* STARTWEEK_MONDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_MONDAY instead |
|||
*/ |
|||
const STARTWEEK_MONDAY = 2; |
|||
|
|||
/** |
|||
* STARTWEEK_MONDAY_ALT. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_MONDAY_ALT instead |
|||
*/ |
|||
const STARTWEEK_MONDAY_ALT = 11; |
|||
|
|||
/** |
|||
* STARTWEEK_TUESDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_TUESDAY instead |
|||
*/ |
|||
const STARTWEEK_TUESDAY = 12; |
|||
|
|||
/** |
|||
* STARTWEEK_WEDNESDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_WEDNESDAY instead |
|||
*/ |
|||
const STARTWEEK_WEDNESDAY = 13; |
|||
|
|||
/** |
|||
* STARTWEEK_THURSDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_THURSDAY instead |
|||
*/ |
|||
const STARTWEEK_THURSDAY = 14; |
|||
|
|||
/** |
|||
* STARTWEEK_FRIDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_FRIDAY instead |
|||
*/ |
|||
const STARTWEEK_FRIDAY = 15; |
|||
|
|||
/** |
|||
* STARTWEEK_SATURDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_SATURDAY instead |
|||
*/ |
|||
const STARTWEEK_SATURDAY = 16; |
|||
|
|||
/** |
|||
* STARTWEEK_SUNDAY_ALT. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_SUNDAY_ALT instead |
|||
*/ |
|||
const STARTWEEK_SUNDAY_ALT = 17; |
|||
|
|||
/** |
|||
* DOW_SUNDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\DOW_SUNDAY instead |
|||
*/ |
|||
const DOW_SUNDAY = 1; |
|||
|
|||
/** |
|||
* DOW_MONDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\DOW_MONDAY instead |
|||
*/ |
|||
const DOW_MONDAY = 2; |
|||
|
|||
/** |
|||
* DOW_TUESDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\DOW_TUESDAY instead |
|||
*/ |
|||
const DOW_TUESDAY = 3; |
|||
|
|||
/** |
|||
* DOW_WEDNESDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\DOW_WEDNESDAY instead |
|||
*/ |
|||
const DOW_WEDNESDAY = 4; |
|||
|
|||
/** |
|||
* DOW_THURSDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\DOW_THURSDAY instead |
|||
*/ |
|||
const DOW_THURSDAY = 5; |
|||
|
|||
/** |
|||
* DOW_FRIDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\DOW_FRIDAY instead |
|||
*/ |
|||
const DOW_FRIDAY = 6; |
|||
|
|||
/** |
|||
* DOW_SATURDAY. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\DOW_SATURDAY instead |
|||
*/ |
|||
const DOW_SATURDAY = 7; |
|||
|
|||
/** |
|||
* STARTWEEK_MONDAY_ISO. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\STARTWEEK_MONDAY_ISO instead |
|||
*/ |
|||
const STARTWEEK_MONDAY_ISO = 21; |
|||
|
|||
/** |
|||
* METHODARR. |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @see Use DateTimeExcel\Constants\METHODARR instead |
|||
*/ |
|||
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, |
|||
]; |
|||
|
|||
/** |
|||
* 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]) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Week::number(() |
|||
* Use the number method in the DateTimeExcel\Week class instead |
|||
* |
|||
* @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 array|int|string Week Number |
|||
*/ |
|||
public static function WEEKNUM($dateValue = 1, $method = self::STARTWEEK_SUNDAY) |
|||
{ |
|||
return DateTimeExcel\Week::number($dateValue, $method); |
|||
} |
|||
|
|||
/** |
|||
* ISOWEEKNUM. |
|||
* |
|||
* Returns the ISO 8601 week number of the year for a specified date. |
|||
* |
|||
* Excel Function: |
|||
* ISOWEEKNUM(dateValue) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Week::isoWeekNumber() |
|||
* Use the isoWeekNumber method in the DateTimeExcel\Week class instead |
|||
* |
|||
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* |
|||
* @return array|int|string Week Number |
|||
*/ |
|||
public static function ISOWEEKNUM($dateValue = 1) |
|||
{ |
|||
return DateTimeExcel\Week::isoWeekNumber($dateValue); |
|||
} |
|||
|
|||
/** |
|||
* 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) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\DateParts::month() |
|||
* Use the month method in the DateTimeExcel\DateParts class instead |
|||
* |
|||
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* |
|||
* @return array|int|string Month of the year |
|||
*/ |
|||
public static function MONTHOFYEAR($dateValue = 1) |
|||
{ |
|||
return DateTimeExcel\DateParts::month($dateValue); |
|||
} |
|||
|
|||
/** |
|||
* YEAR. |
|||
* |
|||
* Returns the year corresponding to a date. |
|||
* The year is returned as an integer in the range 1900-9999. |
|||
* |
|||
* Excel Function: |
|||
* YEAR(dateValue) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\DateParts::year() |
|||
* Use the ear method in the DateTimeExcel\DateParts class instead |
|||
* |
|||
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* |
|||
* @return array|int|string Year |
|||
*/ |
|||
public static function YEAR($dateValue = 1) |
|||
{ |
|||
return DateTimeExcel\DateParts::year($dateValue); |
|||
} |
|||
|
|||
/** |
|||
* 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) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\TimeParts::hour() |
|||
* Use the hour method in the DateTimeExcel\TimeParts class instead |
|||
* |
|||
* @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard time string |
|||
* |
|||
* @return array|int|string Hour |
|||
*/ |
|||
public static function HOUROFDAY($timeValue = 0) |
|||
{ |
|||
return DateTimeExcel\TimeParts::hour($timeValue); |
|||
} |
|||
|
|||
/** |
|||
* MINUTE. |
|||
* |
|||
* Returns the minutes of a time value. |
|||
* The minute is given as an integer, ranging from 0 to 59. |
|||
* |
|||
* Excel Function: |
|||
* MINUTE(timeValue) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\TimeParts::minute() |
|||
* Use the minute method in the DateTimeExcel\TimeParts class instead |
|||
* |
|||
* @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard time string |
|||
* |
|||
* @return array|int|string Minute |
|||
*/ |
|||
public static function MINUTE($timeValue = 0) |
|||
{ |
|||
return DateTimeExcel\TimeParts::minute($timeValue); |
|||
} |
|||
|
|||
/** |
|||
* SECOND. |
|||
* |
|||
* Returns the seconds of a time value. |
|||
* The second is given as an integer in the range 0 (zero) to 59. |
|||
* |
|||
* Excel Function: |
|||
* SECOND(timeValue) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\TimeParts::second() |
|||
* Use the second method in the DateTimeExcel\TimeParts class instead |
|||
* |
|||
* @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard time string |
|||
* |
|||
* @return array|int|string Second |
|||
*/ |
|||
public static function SECOND($timeValue = 0) |
|||
{ |
|||
return DateTimeExcel\TimeParts::second($timeValue); |
|||
} |
|||
|
|||
/** |
|||
* EDATE. |
|||
* |
|||
* Returns the serial number that represents the date that is the indicated number of months |
|||
* before or after a specified date (the start_date). |
|||
* Use EDATE to calculate maturity dates or due dates that fall on the same day of the month |
|||
* as the date of issue. |
|||
* |
|||
* Excel Function: |
|||
* EDATE(dateValue,adjustmentMonths) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Month::adjust() |
|||
* Use the adjust method in the DateTimeExcel\Edate class instead |
|||
* |
|||
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* @param int $adjustmentMonths The number of months before or after start_date. |
|||
* A positive value for months yields a future date; |
|||
* a negative value yields a past date. |
|||
* |
|||
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
|||
* depending on the value of the ReturnDateType flag |
|||
*/ |
|||
public static function EDATE($dateValue = 1, $adjustmentMonths = 0) |
|||
{ |
|||
return DateTimeExcel\Month::adjust($dateValue, $adjustmentMonths); |
|||
} |
|||
|
|||
/** |
|||
* 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) |
|||
* |
|||
* @Deprecated 1.18.0 |
|||
* |
|||
* @See DateTimeExcel\Month::lastDay() |
|||
* Use the lastDay method in the DateTimeExcel\EoMonth class instead |
|||
* |
|||
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* @param int $adjustmentMonths The number of months before or after start_date. |
|||
* A positive value for months yields a future date; |
|||
* a negative value yields a past date. |
|||
* |
|||
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
|||
* depending on the value of the ReturnDateType flag |
|||
*/ |
|||
public static function EOMONTH($dateValue = 1, $adjustmentMonths = 0) |
|||
{ |
|||
return DateTimeExcel\Month::lastDay($dateValue, $adjustmentMonths); |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
<?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,59 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
|||
|
|||
use DateTimeImmutable; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Current |
|||
{ |
|||
/** |
|||
* 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 today() |
|||
{ |
|||
$dti = new DateTimeImmutable(); |
|||
$dateArray = Helpers::dateParse($dti->format('c')); |
|||
|
|||
return Helpers::dateParseSucceeded($dateArray) ? Helpers::returnIn3FormatsArray($dateArray, true) : Functions::VALUE(); |
|||
} |
|||
|
|||
/** |
|||
* 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 now() |
|||
{ |
|||
$dti = new DateTimeImmutable(); |
|||
$dateArray = Helpers::dateParse($dti->format('c')); |
|||
|
|||
return Helpers::dateParseSucceeded($dateArray) ? Helpers::returnIn3FormatsArray($dateArray) : Functions::VALUE(); |
|||
} |
|||
} |
|||
@ -0,0 +1,172 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
|||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
|||
|
|||
class Date |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* 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 array|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 array|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 array|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 |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function fromYMD($year, $month, $day) |
|||
{ |
|||
if (is_array($year) || is_array($month) || is_array($day)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $year, $month, $day); |
|||
} |
|||
|
|||
$baseYear = SharedDateHelper::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 = SharedDateHelper::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 = ($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 (int) $year; |
|||
} |
|||
|
|||
/** |
|||
* Convert month from multiple formats to int. |
|||
* |
|||
* @param mixed $month |
|||
*/ |
|||
private static function getMonth($month): int |
|||
{ |
|||
if (($month !== null) && (!is_numeric($month))) { |
|||
$month = SharedDateHelper::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 |
|||
{ |
|||
if (($day !== null) && (!is_numeric($day))) { |
|||
$day = SharedDateHelper::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,151 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
|||
|
|||
class DateParts |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* 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 |
|||
* Or can be an array of date values |
|||
* |
|||
* @return array|int|string Day of the month |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function day($dateValue) |
|||
{ |
|||
if (is_array($dateValue)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue); |
|||
} |
|||
|
|||
$weirdResult = self::weirdCondition($dateValue); |
|||
if ($weirdResult >= 0) { |
|||
return $weirdResult; |
|||
} |
|||
|
|||
try { |
|||
$dateValue = Helpers::getDateValue($dateValue); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
// Execute function |
|||
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue); |
|||
|
|||
return (int) $PHPDateObject->format('j'); |
|||
} |
|||
|
|||
/** |
|||
* 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 |
|||
* Or can be an array of date values |
|||
* |
|||
* @return array|int|string Month of the year |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function month($dateValue) |
|||
{ |
|||
if (is_array($dateValue)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue); |
|||
} |
|||
|
|||
try { |
|||
$dateValue = Helpers::getDateValue($dateValue); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) { |
|||
return 1; |
|||
} |
|||
|
|||
// Execute function |
|||
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue); |
|||
|
|||
return (int) $PHPDateObject->format('n'); |
|||
} |
|||
|
|||
/** |
|||
* YEAR. |
|||
* |
|||
* Returns the year corresponding to a date. |
|||
* The year is returned as an integer in the range 1900-9999. |
|||
* |
|||
* Excel Function: |
|||
* YEAR(dateValue) |
|||
* |
|||
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* Or can be an array of date values |
|||
* |
|||
* @return array|int|string Year |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function year($dateValue) |
|||
{ |
|||
if (is_array($dateValue)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue); |
|||
} |
|||
|
|||
try { |
|||
$dateValue = Helpers::getDateValue($dateValue); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) { |
|||
return 1900; |
|||
} |
|||
// Execute function |
|||
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue); |
|||
|
|||
return (int) $PHPDateObject->format('Y'); |
|||
} |
|||
|
|||
/** |
|||
* @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 (SharedDateHelper::getExcelCalendar() === SharedDateHelper::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,157 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
|||
|
|||
use DateTimeImmutable; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
|||
|
|||
class DateValue |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* 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 array|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. |
|||
* Or can be an array of date values |
|||
* |
|||
* @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 |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function fromString($dateValue) |
|||
{ |
|||
if (is_array($dateValue)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue); |
|||
} |
|||
|
|||
$dti = new DateTimeImmutable(); |
|||
$baseYear = SharedDateHelper::getExcelCalendar(); |
|||
$dateValue = trim($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. |
|||
*/ |
|||
private static function setUpArray(string $dateValue, DateTimeImmutable $dti): array |
|||
{ |
|||
$PHPDateArray = Helpers::dateParse($dateValue); |
|||
if (!Helpers::dateParseSucceeded($PHPDateArray)) { |
|||
// 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 = Helpers::dateParse($testVal1 . '-' . $testVal2 . '-' . $testVal3); |
|||
if (!Helpers::dateParseSucceeded($PHPDateArray)) { |
|||
$PHPDateArray = Helpers::dateParse($testVal2 . '-' . $testVal1 . '-' . $testVal3); |
|||
} |
|||
} |
|||
|
|||
return $PHPDateArray; |
|||
} |
|||
|
|||
/** |
|||
* Final results. |
|||
* |
|||
* @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(array $PHPDateArray, DateTimeImmutable $dti, int $baseYear) |
|||
{ |
|||
$retValue = Functions::Value(); |
|||
if (Helpers::dateParseSucceeded($PHPDateArray)) { |
|||
// 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,62 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
|||
|
|||
use DateTimeInterface; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
|||
|
|||
class Days |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* DAYS. |
|||
* |
|||
* Returns the number of days between two dates |
|||
* |
|||
* Excel Function: |
|||
* DAYS(endDate, startDate) |
|||
* |
|||
* @param array|DateTimeInterface|float|int|string $endDate Excel date serial value (float), |
|||
* PHP date timestamp (integer), PHP DateTime object, or a standard date string |
|||
* Or can be an array of date values |
|||
* @param array|DateTimeInterface|float|int|string $startDate Excel date serial value (float), |
|||
* PHP date timestamp (integer), PHP DateTime object, or a standard date string |
|||
* Or can be an array of date values |
|||
* |
|||
* @return array|int|string Number of days between start date and end date or an error |
|||
* If an array of values is passed for the $startDate or $endDays,arguments, then the returned result |
|||
* will also be an array with matching dimensions |
|||
*/ |
|||
public static function between($endDate, $startDate) |
|||
{ |
|||
if (is_array($endDate) || is_array($startDate)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $endDate, $startDate); |
|||
} |
|||
|
|||
try { |
|||
$startDate = Helpers::getDateValue($startDate); |
|||
$endDate = Helpers::getDateValue($endDate); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
// Execute function |
|||
$PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate); |
|||
$PHPEndDateObject = SharedDateHelper::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,118 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
|||
|
|||
class Days360 |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* 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 array|mixed $startDate Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* Or can be an array of date values |
|||
* @param array|mixed $endDate Excel date serial value (float), PHP date timestamp (integer), |
|||
* PHP DateTime object, or a standard date string |
|||
* Or can be an array of date values |
|||
* @param array|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. |
|||
* Or can be an array of methods |
|||
* |
|||
* @return array|int|string Number of days between start date and end date |
|||
* If an array of values is passed for the $startDate or $endDays,arguments, then the returned result |
|||
* will also be an array with matching dimensions |
|||
*/ |
|||
public static function between($startDate = 0, $endDate = 0, $method = false) |
|||
{ |
|||
if (is_array($startDate) || is_array($endDate) || is_array($method)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $startDate, $endDate, $method); |
|||
} |
|||
|
|||
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 = SharedDateHelper::excelToDateTimeObject($startDate); |
|||
$startDay = $PHPStartDateObject->format('j'); |
|||
$startMonth = $PHPStartDateObject->format('n'); |
|||
$startYear = $PHPStartDateObject->format('Y'); |
|||
|
|||
$PHPEndDateObject = SharedDateHelper::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,158 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
|||
|
|||
use DateInterval; |
|||
use DateTime; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
|||
|
|||
class Difference |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* DATEDIF. |
|||
* |
|||
* @param mixed $startDate Excel date serial value, PHP date/time stamp, PHP DateTime object |
|||
* or a standard date string |
|||
* Or can be an array of date values |
|||
* @param mixed $endDate Excel date serial value, PHP date/time stamp, PHP DateTime object |
|||
* or a standard date string |
|||
* Or can be an array of date values |
|||
* @param array|string $unit |
|||
* Or can be an array of unit values |
|||
* |
|||
* @return array|int|string Interval between the dates |
|||
* If an array of values is passed for the $startDate or $endDays,arguments, then the returned result |
|||
* will also be an array with matching dimensions |
|||
*/ |
|||
public static function interval($startDate, $endDate, $unit = 'D') |
|||
{ |
|||
if (is_array($startDate) || is_array($endDate) || is_array($unit)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $startDate, $endDate, $unit); |
|||
} |
|||
|
|||
try { |
|||
$startDate = Helpers::getDateValue($startDate); |
|||
$endDate = Helpers::getDateValue($endDate); |
|||
$difference = self::initialDiff($startDate, $endDate); |
|||
$unit = strtoupper($unit); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
// Execute function |
|||
$PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate); |
|||
$startDays = (int) $PHPStartDateObject->format('j'); |
|||
$startMonths = (int) $PHPStartDateObject->format('n'); |
|||
$startYears = (int) $PHPStartDateObject->format('Y'); |
|||
|
|||
$PHPEndDateObject = SharedDateHelper::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,202 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
|
|||
class ArrayArgumentHelper |
|||
{ |
|||
/** |
|||
* @var array |
|||
*/ |
|||
protected $arguments; |
|||
|
|||
/** |
|||
* @var int |
|||
*/ |
|||
protected $argumentCount; |
|||
|
|||
/** |
|||
* @var array |
|||
*/ |
|||
protected $rows; |
|||
|
|||
/** |
|||
* @var array |
|||
*/ |
|||
protected $columns; |
|||
|
|||
public function initialise(array $arguments): void |
|||
{ |
|||
$this->rows = $this->rows($arguments); |
|||
$this->columns = $this->columns($arguments); |
|||
|
|||
$this->argumentCount = count($arguments); |
|||
$this->arguments = $this->flattenSingleCellArrays($arguments, $this->rows, $this->columns); |
|||
|
|||
$this->rows = $this->rows($arguments); |
|||
$this->columns = $this->columns($arguments); |
|||
|
|||
if ($this->arrayArguments() > 2) { |
|||
throw new Exception('Formulae with more than two array arguments are not supported'); |
|||
} |
|||
} |
|||
|
|||
public function arguments(): array |
|||
{ |
|||
return $this->arguments; |
|||
} |
|||
|
|||
public function hasArrayArgument(): bool |
|||
{ |
|||
return $this->arrayArguments() > 0; |
|||
} |
|||
|
|||
public function getFirstArrayArgumentNumber(): int |
|||
{ |
|||
$rowArrays = $this->filterArray($this->rows); |
|||
$columnArrays = $this->filterArray($this->columns); |
|||
|
|||
for ($index = 0; $index < $this->argumentCount; ++$index) { |
|||
if (isset($rowArrays[$index]) || isset($columnArrays[$index])) { |
|||
return ++$index; |
|||
} |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
public function getSingleRowVector(): ?int |
|||
{ |
|||
$rowVectors = $this->getRowVectors(); |
|||
|
|||
return count($rowVectors) === 1 ? array_pop($rowVectors) : null; |
|||
} |
|||
|
|||
private function getRowVectors(): array |
|||
{ |
|||
$rowVectors = []; |
|||
for ($index = 0; $index < $this->argumentCount; ++$index) { |
|||
if ($this->rows[$index] === 1 && $this->columns[$index] > 1) { |
|||
$rowVectors[] = $index; |
|||
} |
|||
} |
|||
|
|||
return $rowVectors; |
|||
} |
|||
|
|||
public function getSingleColumnVector(): ?int |
|||
{ |
|||
$columnVectors = $this->getColumnVectors(); |
|||
|
|||
return count($columnVectors) === 1 ? array_pop($columnVectors) : null; |
|||
} |
|||
|
|||
private function getColumnVectors(): array |
|||
{ |
|||
$columnVectors = []; |
|||
for ($index = 0; $index < $this->argumentCount; ++$index) { |
|||
if ($this->rows[$index] > 1 && $this->columns[$index] === 1) { |
|||
$columnVectors[] = $index; |
|||
} |
|||
} |
|||
|
|||
return $columnVectors; |
|||
} |
|||
|
|||
public function getMatrixPair(): array |
|||
{ |
|||
for ($i = 0; $i < ($this->argumentCount - 1); ++$i) { |
|||
for ($j = $i + 1; $j < $this->argumentCount; ++$j) { |
|||
if (isset($this->rows[$i], $this->rows[$j])) { |
|||
return [$i, $j]; |
|||
} |
|||
} |
|||
} |
|||
|
|||
return []; |
|||
} |
|||
|
|||
public function isVector(int $argument): bool |
|||
{ |
|||
return $this->rows[$argument] === 1 || $this->columns[$argument] === 1; |
|||
} |
|||
|
|||
public function isRowVector(int $argument): bool |
|||
{ |
|||
return $this->rows[$argument] === 1; |
|||
} |
|||
|
|||
public function isColumnVector(int $argument): bool |
|||
{ |
|||
return $this->columns[$argument] === 1; |
|||
} |
|||
|
|||
public function rowCount(int $argument): int |
|||
{ |
|||
return $this->rows[$argument]; |
|||
} |
|||
|
|||
public function columnCount(int $argument): int |
|||
{ |
|||
return $this->columns[$argument]; |
|||
} |
|||
|
|||
private function rows(array $arguments): array |
|||
{ |
|||
return array_map( |
|||
function ($argument) { |
|||
return is_countable($argument) ? count($argument) : 1; |
|||
}, |
|||
$arguments |
|||
); |
|||
} |
|||
|
|||
private function columns(array $arguments): array |
|||
{ |
|||
return array_map( |
|||
function ($argument) { |
|||
return is_array($argument) && is_array($argument[array_keys($argument)[0]]) |
|||
? count($argument[array_keys($argument)[0]]) |
|||
: 1; |
|||
}, |
|||
$arguments |
|||
); |
|||
} |
|||
|
|||
public function arrayArguments(): int |
|||
{ |
|||
$count = 0; |
|||
foreach (array_keys($this->arguments) as $argument) { |
|||
if ($this->rows[$argument] > 1 || $this->columns[$argument] > 1) { |
|||
++$count; |
|||
} |
|||
} |
|||
|
|||
return $count; |
|||
} |
|||
|
|||
private function flattenSingleCellArrays(array $arguments, array $rows, array $columns): array |
|||
{ |
|||
foreach ($arguments as $index => $argument) { |
|||
if ($rows[$index] === 1 && $columns[$index] === 1) { |
|||
while (is_array($argument)) { |
|||
$argument = array_pop($argument); |
|||
} |
|||
$arguments[$index] = $argument; |
|||
} |
|||
} |
|||
|
|||
return $arguments; |
|||
} |
|||
|
|||
private function filterArray(array $array): array |
|||
{ |
|||
return array_filter( |
|||
$array, |
|||
function ($value) { |
|||
return $value > 1; |
|||
} |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,174 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class ArrayArgumentProcessor |
|||
{ |
|||
/** |
|||
* @var ArrayArgumentHelper |
|||
*/ |
|||
private static $arrayArgumentHelper; |
|||
|
|||
/** |
|||
* @param mixed ...$arguments |
|||
*/ |
|||
public static function processArguments( |
|||
ArrayArgumentHelper $arrayArgumentHelper, |
|||
callable $method, |
|||
...$arguments |
|||
): array { |
|||
self::$arrayArgumentHelper = $arrayArgumentHelper; |
|||
|
|||
if (self::$arrayArgumentHelper->hasArrayArgument() === false) { |
|||
return [$method(...$arguments)]; |
|||
} |
|||
|
|||
if (self::$arrayArgumentHelper->arrayArguments() === 1) { |
|||
$nthArgument = self::$arrayArgumentHelper->getFirstArrayArgumentNumber(); |
|||
|
|||
return self::evaluateNthArgumentAsArray($method, $nthArgument, ...$arguments); |
|||
} |
|||
|
|||
$singleRowVectorIndex = self::$arrayArgumentHelper->getSingleRowVector(); |
|||
$singleColumnVectorIndex = self::$arrayArgumentHelper->getSingleColumnVector(); |
|||
if ($singleRowVectorIndex !== null && $singleColumnVectorIndex !== null) { |
|||
// Basic logic for a single row vector and a single column vector |
|||
return self::evaluateVectorPair($method, $singleRowVectorIndex, $singleColumnVectorIndex, ...$arguments); |
|||
} |
|||
|
|||
$matrixPair = self::$arrayArgumentHelper->getMatrixPair(); |
|||
if ($matrixPair !== []) { |
|||
if ( |
|||
(self::$arrayArgumentHelper->isVector($matrixPair[0]) === true && |
|||
self::$arrayArgumentHelper->isVector($matrixPair[1]) === false) || |
|||
(self::$arrayArgumentHelper->isVector($matrixPair[0]) === false && |
|||
self::$arrayArgumentHelper->isVector($matrixPair[1]) === true) |
|||
) { |
|||
// Logic for a matrix and a vector (row or column) |
|||
return self::evaluateVectorMatrixPair($method, $matrixPair, ...$arguments); |
|||
} |
|||
// Logic for matrix/matrix, column vector/column vector or row vector/row vector |
|||
return self::evaluateMatrixPair($method, $matrixPair, ...$arguments); |
|||
} |
|||
|
|||
// Still need to work out the logic for more than two array arguments, |
|||
// For the moment, we're throwing an Exception when we initialise the ArrayArgumentHelper |
|||
return ['#VALUE!']; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed ...$arguments |
|||
*/ |
|||
private static function evaluateVectorMatrixPair(callable $method, array $matrixIndexes, ...$arguments): array |
|||
{ |
|||
$matrix2 = array_pop($matrixIndexes); |
|||
/** @var array $matrixValues2 */ |
|||
$matrixValues2 = $arguments[$matrix2]; |
|||
$matrix1 = array_pop($matrixIndexes); |
|||
/** @var array $matrixValues1 */ |
|||
$matrixValues1 = $arguments[$matrix1]; |
|||
|
|||
$rows = min(array_map([self::$arrayArgumentHelper, 'rowCount'], [$matrix1, $matrix2])); |
|||
$columns = min(array_map([self::$arrayArgumentHelper, 'columnCount'], [$matrix1, $matrix2])); |
|||
|
|||
if ($rows === 1) { |
|||
$rows = max(array_map([self::$arrayArgumentHelper, 'rowCount'], [$matrix1, $matrix2])); |
|||
} |
|||
if ($columns === 1) { |
|||
$columns = max(array_map([self::$arrayArgumentHelper, 'columnCount'], [$matrix1, $matrix2])); |
|||
} |
|||
|
|||
$result = []; |
|||
for ($rowIndex = 0; $rowIndex < $rows; ++$rowIndex) { |
|||
for ($columnIndex = 0; $columnIndex < $columns; ++$columnIndex) { |
|||
$rowIndex1 = self::$arrayArgumentHelper->isRowVector($matrix1) ? 0 : $rowIndex; |
|||
$columnIndex1 = self::$arrayArgumentHelper->isColumnVector($matrix1) ? 0 : $columnIndex; |
|||
$value1 = $matrixValues1[$rowIndex1][$columnIndex1]; |
|||
$rowIndex2 = self::$arrayArgumentHelper->isRowVector($matrix2) ? 0 : $rowIndex; |
|||
$columnIndex2 = self::$arrayArgumentHelper->isColumnVector($matrix2) ? 0 : $columnIndex; |
|||
$value2 = $matrixValues2[$rowIndex2][$columnIndex2]; |
|||
$arguments[$matrix1] = $value1; |
|||
$arguments[$matrix2] = $value2; |
|||
|
|||
$result[$rowIndex][$columnIndex] = $method(...$arguments); |
|||
} |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed ...$arguments |
|||
*/ |
|||
private static function evaluateMatrixPair(callable $method, array $matrixIndexes, ...$arguments): array |
|||
{ |
|||
$matrix2 = array_pop($matrixIndexes); |
|||
/** @var array $matrixValues2 */ |
|||
$matrixValues2 = $arguments[$matrix2]; |
|||
$matrix1 = array_pop($matrixIndexes); |
|||
/** @var array $matrixValues1 */ |
|||
$matrixValues1 = $arguments[$matrix1]; |
|||
|
|||
$result = []; |
|||
foreach ($matrixValues1 as $rowIndex => $row) { |
|||
foreach ($row as $columnIndex => $value1) { |
|||
if (isset($matrixValues2[$rowIndex][$columnIndex]) === false) { |
|||
continue; |
|||
} |
|||
|
|||
$value2 = $matrixValues2[$rowIndex][$columnIndex]; |
|||
$arguments[$matrix1] = $value1; |
|||
$arguments[$matrix2] = $value2; |
|||
|
|||
$result[$rowIndex][$columnIndex] = $method(...$arguments); |
|||
} |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed ...$arguments |
|||
*/ |
|||
private static function evaluateVectorPair(callable $method, int $rowIndex, int $columnIndex, ...$arguments): array |
|||
{ |
|||
$rowVector = Functions::flattenArray($arguments[$rowIndex]); |
|||
$columnVector = Functions::flattenArray($arguments[$columnIndex]); |
|||
|
|||
$result = []; |
|||
foreach ($columnVector as $column) { |
|||
$rowResults = []; |
|||
foreach ($rowVector as $row) { |
|||
$arguments[$rowIndex] = $row; |
|||
$arguments[$columnIndex] = $column; |
|||
|
|||
$rowResults[] = $method(...$arguments); |
|||
} |
|||
$result[] = $rowResults; |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* Note, offset is from 1 (for the first argument) rather than from 0. |
|||
* |
|||
* @param mixed ...$arguments |
|||
*/ |
|||
private static function evaluateNthArgumentAsArray(callable $method, int $nthArgument, ...$arguments): array |
|||
{ |
|||
$values = array_slice($arguments, $nthArgument - 1, 1); |
|||
/** @var array $values */ |
|||
$values = array_pop($values); |
|||
|
|||
$result = []; |
|||
foreach ($values as $value) { |
|||
$arguments[$nthArgument - 1] = $value; |
|||
$result[] = $method(...$arguments); |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine; |
|||
|
|||
class CyclicReferenceStack |
|||
{ |
|||
/** |
|||
* The call stack for calculated cells. |
|||
* |
|||
* @var mixed[] |
|||
*/ |
|||
private $stack = []; |
|||
|
|||
/** |
|||
* Return the number of entries on the stack. |
|||
* |
|||
* @return int |
|||
*/ |
|||
public function count() |
|||
{ |
|||
return count($this->stack); |
|||
} |
|||
|
|||
/** |
|||
* Push a new entry onto the stack. |
|||
* |
|||
* @param mixed $value |
|||
*/ |
|||
public function push($value): void |
|||
{ |
|||
$this->stack[$value] = $value; |
|||
} |
|||
|
|||
/** |
|||
* Pop the last entry from the stack. |
|||
* |
|||
* @return mixed |
|||
*/ |
|||
public function pop() |
|||
{ |
|||
return array_pop($this->stack); |
|||
} |
|||
|
|||
/** |
|||
* Test to see if a specified entry exists on the stack. |
|||
* |
|||
* @param mixed $value The value to test |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function onStack($value) |
|||
{ |
|||
return isset($this->stack[$value]); |
|||
} |
|||
|
|||
/** |
|||
* Clear the stack. |
|||
*/ |
|||
public function clear(): void |
|||
{ |
|||
$this->stack = []; |
|||
} |
|||
|
|||
/** |
|||
* Return an array of all entries on the stack. |
|||
* |
|||
* @return mixed[] |
|||
*/ |
|||
public function showStack() |
|||
{ |
|||
return $this->stack; |
|||
} |
|||
} |
|||
@ -0,0 +1,145 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class BesselI |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* BESSELI. |
|||
* |
|||
* Returns the modified Bessel function In(x), which is equivalent to the Bessel function evaluated |
|||
* for purely imaginary arguments |
|||
* |
|||
* Excel Function: |
|||
* BESSELI(x,ord) |
|||
* |
|||
* 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. |
|||
* Or can be an array of values |
|||
* @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. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string Result, or a string containing an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function BESSELI($x, $ord) |
|||
{ |
|||
if (is_array($x) || is_array($ord)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord); |
|||
} |
|||
|
|||
try { |
|||
$x = EngineeringValidations::validateFloat($x); |
|||
$ord = EngineeringValidations::validateInt($ord); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($ord < 0) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$fResult = self::calculate($x, $ord); |
|||
|
|||
return (is_nan($fResult)) ? Functions::NAN() : $fResult; |
|||
} |
|||
|
|||
private static function calculate(float $x, int $ord): float |
|||
{ |
|||
// special cases |
|||
switch ($ord) { |
|||
case 0: |
|||
return self::besselI0($x); |
|||
case 1: |
|||
return self::besselI1($x); |
|||
} |
|||
|
|||
return self::besselI2($x, $ord); |
|||
} |
|||
|
|||
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; |
|||
} |
|||
} |
|||
@ -0,0 +1,180 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class BesselJ |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* BESSELJ. |
|||
* |
|||
* Returns the Bessel function |
|||
* |
|||
* Excel Function: |
|||
* BESSELJ(x,ord) |
|||
* |
|||
* 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. |
|||
* Or can be an array of values |
|||
* @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. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string Result, or a string containing an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function BESSELJ($x, $ord) |
|||
{ |
|||
if (is_array($x) || is_array($ord)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord); |
|||
} |
|||
|
|||
try { |
|||
$x = EngineeringValidations::validateFloat($x); |
|||
$ord = EngineeringValidations::validateInt($ord); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($ord < 0) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$fResult = self::calculate($x, $ord); |
|||
|
|||
return (is_nan($fResult)) ? Functions::NAN() : $fResult; |
|||
} |
|||
|
|||
private static function calculate(float $x, int $ord): float |
|||
{ |
|||
// special cases |
|||
switch ($ord) { |
|||
case 0: |
|||
return self::besselJ0($x); |
|||
case 1: |
|||
return self::besselJ1($x); |
|||
} |
|||
|
|||
return self::besselJ2($x, $ord); |
|||
} |
|||
|
|||
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; |
|||
} |
|||
} |
|||
@ -0,0 +1,119 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class BesselK |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* BESSELK. |
|||
* |
|||
* Returns the modified Bessel function Kn(x), which is equivalent to the Bessel functions evaluated |
|||
* for purely imaginary arguments. |
|||
* |
|||
* Excel Function: |
|||
* BESSELK(x,ord) |
|||
* |
|||
* @param mixed $x A float value at which to evaluate the function. |
|||
* If x is nonnumeric, BESSELK returns the #VALUE! error value. |
|||
* Or can be an array of values |
|||
* @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, BESSELKI returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string Result, or a string containing an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function BESSELK($x, $ord) |
|||
{ |
|||
if (is_array($x) || is_array($ord)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord); |
|||
} |
|||
|
|||
try { |
|||
$x = EngineeringValidations::validateFloat($x); |
|||
$ord = EngineeringValidations::validateInt($ord); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (($ord < 0) || ($x <= 0.0)) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$fBk = self::calculate($x, $ord); |
|||
|
|||
return (is_nan($fBk)) ? Functions::NAN() : $fBk; |
|||
} |
|||
|
|||
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); |
|||
} |
|||
|
|||
return self::besselK2($x, $ord); |
|||
} |
|||
|
|||
private static function besselK0(float $x): float |
|||
{ |
|||
if ($x <= 2) { |
|||
$fNum2 = $x * 0.5; |
|||
$y = ($fNum2 * $fNum2); |
|||
|
|||
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 / $x; |
|||
|
|||
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 $x): float |
|||
{ |
|||
if ($x <= 2) { |
|||
$fNum2 = $x * 0.5; |
|||
$y = ($fNum2 * $fNum2); |
|||
|
|||
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))))))) / $x; |
|||
} |
|||
|
|||
$y = 2 / $x; |
|||
|
|||
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))))))); |
|||
} |
|||
|
|||
private static function besselK2(float $x, int $ord) |
|||
{ |
|||
$fTox = 2 / $x; |
|||
$fBkm = self::besselK0($x); |
|||
$fBk = self::besselK1($x); |
|||
for ($n = 1; $n < $ord; ++$n) { |
|||
$fBkp = $fBkm + $n * $fTox * $fBk; |
|||
$fBkm = $fBk; |
|||
$fBk = $fBkp; |
|||
} |
|||
|
|||
return $fBk; |
|||
} |
|||
} |
|||
@ -0,0 +1,126 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class BesselY |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* BESSELY. |
|||
* |
|||
* Returns the Bessel function, which is also called the Weber function or the Neumann function. |
|||
* |
|||
* Excel Function: |
|||
* BESSELY(x,ord) |
|||
* |
|||
* @param mixed $x A float value at which to evaluate the function. |
|||
* If x is nonnumeric, BESSELY returns the #VALUE! error value. |
|||
* Or can be an array of values |
|||
* @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. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string Result, or a string containing an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function BESSELY($x, $ord) |
|||
{ |
|||
if (is_array($x) || is_array($ord)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord); |
|||
} |
|||
|
|||
try { |
|||
$x = EngineeringValidations::validateFloat($x); |
|||
$ord = EngineeringValidations::validateInt($ord); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (($ord < 0) || ($x <= 0.0)) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$fBy = self::calculate($x, $ord); |
|||
|
|||
return (is_nan($fBy)) ? Functions::NAN() : $fBy; |
|||
} |
|||
|
|||
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); |
|||
} |
|||
|
|||
return self::besselY2($x, $ord); |
|||
} |
|||
|
|||
private static function besselY0(float $x): float |
|||
{ |
|||
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)))); |
|||
$ans2 = 40076544269.0 + $y * (745249964.8 + $y * (7189466.438 + $y * |
|||
(47447.26470 + $y * (226.1030244 + $y)))); |
|||
|
|||
return $ans1 / $ans2 + 0.636619772 * BesselJ::BESSELJ($x, 0) * log($x); |
|||
} |
|||
|
|||
$z = 8.0 / $x; |
|||
$y = ($z * $z); |
|||
$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 / $x) * (sin($xx) * $ans1 + $z * cos($xx) * $ans2); |
|||
} |
|||
|
|||
private static function besselY1(float $x): float |
|||
{ |
|||
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))))); |
|||
$ans2 = 0.2499580570e14 + $y * (0.4244419664e12 + $y * (0.3733650367e10 + $y * (0.2245904002e8 + $y * |
|||
(0.1020426050e6 + $y * (0.3549632885e3 + $y))))); |
|||
|
|||
return ($ans1 / $ans2) + 0.636619772 * (BesselJ::BESSELJ($x, 1) * log($x) - 1 / $x); |
|||
} |
|||
|
|||
$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): float |
|||
{ |
|||
$fTox = 2.0 / $x; |
|||
$fBym = self::besselY0($x); |
|||
$fBy = self::besselY1($x); |
|||
for ($n = 1; $n < $ord; ++$n) { |
|||
$fByp = $n * $fTox * $fBy - $fBym; |
|||
$fBym = $fBy; |
|||
$fBy = $fByp; |
|||
} |
|||
|
|||
return $fBy; |
|||
} |
|||
} |
|||
@ -0,0 +1,273 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class BitWise |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
const SPLIT_DIVISOR = 2 ** 24; |
|||
|
|||
/** |
|||
* Split a number into upper and lower portions for full 32-bit support. |
|||
* |
|||
* @param float|int $number |
|||
*/ |
|||
private static function splitNumber($number): array |
|||
{ |
|||
return [floor($number / self::SPLIT_DIVISOR), fmod($number, self::SPLIT_DIVISOR)]; |
|||
} |
|||
|
|||
/** |
|||
* BITAND. |
|||
* |
|||
* Returns the bitwise AND of two integer values. |
|||
* |
|||
* Excel Function: |
|||
* BITAND(number1, number2) |
|||
* |
|||
* @param array|int $number1 |
|||
* Or can be an array of values |
|||
* @param array|int $number2 |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|int|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function BITAND($number1, $number2) |
|||
{ |
|||
if (is_array($number1) || is_array($number2)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number1, $number2); |
|||
} |
|||
|
|||
try { |
|||
$number1 = self::validateBitwiseArgument($number1); |
|||
$number2 = self::validateBitwiseArgument($number2); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
$split1 = self::splitNumber($number1); |
|||
$split2 = self::splitNumber($number2); |
|||
|
|||
return self::SPLIT_DIVISOR * ($split1[0] & $split2[0]) + ($split1[1] & $split2[1]); |
|||
} |
|||
|
|||
/** |
|||
* BITOR. |
|||
* |
|||
* Returns the bitwise OR of two integer values. |
|||
* |
|||
* Excel Function: |
|||
* BITOR(number1, number2) |
|||
* |
|||
* @param array|int $number1 |
|||
* Or can be an array of values |
|||
* @param array|int $number2 |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|int|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function BITOR($number1, $number2) |
|||
{ |
|||
if (is_array($number1) || is_array($number2)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number1, $number2); |
|||
} |
|||
|
|||
try { |
|||
$number1 = self::validateBitwiseArgument($number1); |
|||
$number2 = self::validateBitwiseArgument($number2); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$split1 = self::splitNumber($number1); |
|||
$split2 = self::splitNumber($number2); |
|||
|
|||
return self::SPLIT_DIVISOR * ($split1[0] | $split2[0]) + ($split1[1] | $split2[1]); |
|||
} |
|||
|
|||
/** |
|||
* BITXOR. |
|||
* |
|||
* Returns the bitwise XOR of two integer values. |
|||
* |
|||
* Excel Function: |
|||
* BITXOR(number1, number2) |
|||
* |
|||
* @param array|int $number1 |
|||
* Or can be an array of values |
|||
* @param array|int $number2 |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|int|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function BITXOR($number1, $number2) |
|||
{ |
|||
if (is_array($number1) || is_array($number2)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number1, $number2); |
|||
} |
|||
|
|||
try { |
|||
$number1 = self::validateBitwiseArgument($number1); |
|||
$number2 = self::validateBitwiseArgument($number2); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$split1 = self::splitNumber($number1); |
|||
$split2 = self::splitNumber($number2); |
|||
|
|||
return self::SPLIT_DIVISOR * ($split1[0] ^ $split2[0]) + ($split1[1] ^ $split2[1]); |
|||
} |
|||
|
|||
/** |
|||
* BITLSHIFT. |
|||
* |
|||
* Returns the number value shifted left by shift_amount bits. |
|||
* |
|||
* Excel Function: |
|||
* BITLSHIFT(number, shift_amount) |
|||
* |
|||
* @param array|int $number |
|||
* Or can be an array of values |
|||
* @param array|int $shiftAmount |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|int|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function BITLSHIFT($number, $shiftAmount) |
|||
{ |
|||
if (is_array($number) || is_array($shiftAmount)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $shiftAmount); |
|||
} |
|||
|
|||
try { |
|||
$number = self::validateBitwiseArgument($number); |
|||
$shiftAmount = self::validateShiftAmount($shiftAmount); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$result = floor($number * (2 ** $shiftAmount)); |
|||
if ($result > 2 ** 48 - 1) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* BITRSHIFT. |
|||
* |
|||
* Returns the number value shifted right by shift_amount bits. |
|||
* |
|||
* Excel Function: |
|||
* BITRSHIFT(number, shift_amount) |
|||
* |
|||
* @param array|int $number |
|||
* Or can be an array of values |
|||
* @param array|int $shiftAmount |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|int|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function BITRSHIFT($number, $shiftAmount) |
|||
{ |
|||
if (is_array($number) || is_array($shiftAmount)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $shiftAmount); |
|||
} |
|||
|
|||
try { |
|||
$number = self::validateBitwiseArgument($number); |
|||
$shiftAmount = self::validateShiftAmount($shiftAmount); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$result = floor($number / (2 ** $shiftAmount)); |
|||
if ($result > 2 ** 48 - 1) { // possible because shiftAmount can be negative |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* Validate arguments passed to the bitwise functions. |
|||
* |
|||
* @param mixed $value |
|||
* |
|||
* @return float|int |
|||
*/ |
|||
private static function validateBitwiseArgument($value) |
|||
{ |
|||
$value = self::nullFalseTrueToNumber($value); |
|||
|
|||
if (is_numeric($value)) { |
|||
if ($value == floor($value)) { |
|||
if (($value > 2 ** 48 - 1) || ($value < 0)) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return floor($value); |
|||
} |
|||
|
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
throw new Exception(Functions::VALUE()); |
|||
} |
|||
|
|||
/** |
|||
* Validate arguments passed to the bitwise functions. |
|||
* |
|||
* @param mixed $value |
|||
* |
|||
* @return int |
|||
*/ |
|||
private static function validateShiftAmount($value) |
|||
{ |
|||
$value = self::nullFalseTrueToNumber($value); |
|||
|
|||
if (is_numeric($value)) { |
|||
if (abs($value) > 53) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return (int) $value; |
|||
} |
|||
|
|||
throw new Exception(Functions::VALUE()); |
|||
} |
|||
|
|||
/** |
|||
* Many functions accept null/false/true argument treated as 0/0/1. |
|||
* |
|||
* @param mixed $number |
|||
* |
|||
* @return mixed |
|||
*/ |
|||
private static function nullFalseTrueToNumber(&$number) |
|||
{ |
|||
if ($number === null) { |
|||
$number = 0; |
|||
} elseif (is_bool($number)) { |
|||
$number = (int) $number; |
|||
} |
|||
|
|||
return $number; |
|||
} |
|||
} |
|||
@ -0,0 +1,82 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
|
|||
class Compare |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* DELTA. |
|||
* |
|||
* Excel Function: |
|||
* DELTA(a[,b]) |
|||
* |
|||
* Tests whether two values are equal. Returns 1 if number1 = number2; returns 0 otherwise. |
|||
* Use this function to filter a set of values. For example, by summing several DELTA |
|||
* functions you calculate the count of equal pairs. This function is also known as the |
|||
* Kronecker Delta function. |
|||
* |
|||
* @param array|float $a the first number |
|||
* Or can be an array of values |
|||
* @param array|float $b The second number. If omitted, b is assumed to be zero. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|int|string (string in the event of an error) |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function DELTA($a, $b = 0.0) |
|||
{ |
|||
if (is_array($a) || is_array($b)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $a, $b); |
|||
} |
|||
|
|||
try { |
|||
$a = EngineeringValidations::validateFloat($a); |
|||
$b = EngineeringValidations::validateFloat($b); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return (int) (abs($a - $b) < 1.0e-15); |
|||
} |
|||
|
|||
/** |
|||
* GESTEP. |
|||
* |
|||
* Excel Function: |
|||
* GESTEP(number[,step]) |
|||
* |
|||
* Returns 1 if number >= step; returns 0 (zero) otherwise |
|||
* Use this function to filter a set of values. For example, by summing several GESTEP |
|||
* functions you calculate the count of values that exceed a threshold. |
|||
* |
|||
* @param array|float $number the value to test against step |
|||
* Or can be an array of values |
|||
* @param array|float $step The threshold value. If you omit a value for step, GESTEP uses zero. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|int|string (string in the event of an error) |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function GESTEP($number, $step = 0.0) |
|||
{ |
|||
if (is_array($number) || is_array($step)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $step); |
|||
} |
|||
|
|||
try { |
|||
$number = EngineeringValidations::validateFloat($number); |
|||
$step = EngineeringValidations::validateFloat($step); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return (int) ($number >= $step); |
|||
} |
|||
} |
|||
@ -0,0 +1,121 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use Complex\Complex as ComplexObject; |
|||
use Complex\Exception as ComplexException; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Complex |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* 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 |
|||
* Or can be an array of values |
|||
* @param mixed $imaginary the imaginary float coefficient of the complex number |
|||
* Or can be an array of values |
|||
* @param mixed $suffix The character suffix for the imaginary component of the complex number. |
|||
* If omitted, the suffix is assumed to be "i". |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function COMPLEX($realNumber = 0.0, $imaginary = 0.0, $suffix = 'i') |
|||
{ |
|||
if (is_array($realNumber) || is_array($imaginary) || is_array($suffix)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $realNumber, $imaginary, $suffix); |
|||
} |
|||
|
|||
$realNumber = $realNumber ?? 0.0; |
|||
$imaginary = $imaginary ?? 0.0; |
|||
$suffix = $suffix ?? 'i'; |
|||
|
|||
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 array|string $complexNumber the complex number for which you want the imaginary |
|||
* coefficient |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string (string if an error) |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMAGINARY($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the real coefficient |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string (string if an error) |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMREAL($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $complexNumber); |
|||
} |
|||
|
|||
try { |
|||
$complex = new ComplexObject($complexNumber); |
|||
} catch (ComplexException $e) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
return $complex->getReal(); |
|||
} |
|||
} |
|||
@ -0,0 +1,611 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use Complex\Complex as ComplexObject; |
|||
use Complex\Exception as ComplexException; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class ComplexFunctions |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* IMABS. |
|||
* |
|||
* Returns the absolute value (modulus) of a complex number in x + yi or x + yj text format. |
|||
* |
|||
* Excel Function: |
|||
* IMABS(complexNumber) |
|||
* |
|||
* @param array|string $complexNumber the complex number for which you want the absolute value |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMABS($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the argument theta |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMARGUMENT($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the conjugate |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMCONJUGATE($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the cosine |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMCOS($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the hyperbolic cosine |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMCOSH($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the cotangent |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMCOT($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the cosecant |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMCSC($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the hyperbolic cosecant |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMCSCH($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the sine |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMSIN($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the hyperbolic sine |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMSINH($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the secant |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMSEC($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the hyperbolic secant |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMSECH($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the tangent |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMTAN($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the square root |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMSQRT($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the natural logarithm |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMLN($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the common logarithm |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMLOG10($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the base-2 logarithm |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMLOG2($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number for which you want the exponential |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMEXP($complexNumber) |
|||
{ |
|||
if (is_array($complexNumber)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $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 array|string $complexNumber the complex number you want to raise to a power |
|||
* Or can be an array of values |
|||
* @param array|float|int|string $realNumber the power to which you want to raise the complex number |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMPOWER($complexNumber, $realNumber) |
|||
{ |
|||
if (is_array($complexNumber) || is_array($realNumber)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $complexNumber, $realNumber); |
|||
} |
|||
|
|||
try { |
|||
$complex = new ComplexObject($complexNumber); |
|||
} catch (ComplexException $e) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
if (!is_numeric($realNumber)) { |
|||
return Functions::VALUE(); |
|||
} |
|||
|
|||
return (string) $complex->pow((float) $realNumber); |
|||
} |
|||
} |
|||
@ -0,0 +1,133 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use Complex\Complex as ComplexObject; |
|||
use Complex\Exception as ComplexException; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class ComplexOperations |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* IMDIV. |
|||
* |
|||
* Returns the quotient of two complex numbers in x + yi or x + yj text format. |
|||
* |
|||
* Excel Function: |
|||
* IMDIV(complexDividend,complexDivisor) |
|||
* |
|||
* @param array|string $complexDividend the complex numerator or dividend |
|||
* Or can be an array of values |
|||
* @param array|string $complexDivisor the complex denominator or divisor |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMDIV($complexDividend, $complexDivisor) |
|||
{ |
|||
if (is_array($complexDividend) || is_array($complexDivisor)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $complexDividend, $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 array|string $complexNumber1 the complex number from which to subtract complexNumber2 |
|||
* Or can be an array of values |
|||
* @param array|string $complexNumber2 the complex number to subtract from complexNumber1 |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function IMSUB($complexNumber1, $complexNumber2) |
|||
{ |
|||
if (is_array($complexNumber1) || is_array($complexNumber2)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $complexNumber1, $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; |
|||
} |
|||
@ -0,0 +1,68 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
abstract class ConvertBase |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
protected static function validateValue($value): string |
|||
{ |
|||
if (is_bool($value)) { |
|||
if (Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE) { |
|||
throw new Exception(Functions::VALUE()); |
|||
} |
|||
$value = (int) $value; |
|||
} |
|||
|
|||
if (is_numeric($value)) { |
|||
if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) { |
|||
$value = floor((float) $value); |
|||
} |
|||
} |
|||
|
|||
return strtoupper((string) $value); |
|||
} |
|||
|
|||
protected static function validatePlaces($places = null): ?int |
|||
{ |
|||
if ($places === null) { |
|||
return $places; |
|||
} |
|||
|
|||
if (is_numeric($places)) { |
|||
if ($places < 0 || $places > 10) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return (int) $places; |
|||
} |
|||
|
|||
throw new Exception(Functions::VALUE()); |
|||
} |
|||
|
|||
/** |
|||
* Formats a number base string value with leading zeroes. |
|||
* |
|||
* @param string $value The "number" to pad |
|||
* @param ?int $places The length that we want to pad this value |
|||
* |
|||
* @return string The padded "number" |
|||
*/ |
|||
protected static function nbrConversionFormat(string $value, ?int $places): string |
|||
{ |
|||
if ($places !== null) { |
|||
if (strlen($value) <= $places) { |
|||
return substr(str_pad($value, $places, '0', STR_PAD_LEFT), -10); |
|||
} |
|||
|
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
return substr($value, -10); |
|||
} |
|||
} |
|||
@ -0,0 +1,163 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class ConvertBinary extends ConvertBase |
|||
{ |
|||
/** |
|||
* toDecimal. |
|||
* |
|||
* Return a binary value as decimal. |
|||
* |
|||
* Excel Function: |
|||
* BIN2DEC(x) |
|||
* |
|||
* @param array|string $value The binary number (as a string) that you want to convert. The number |
|||
* cannot contain more than 10 characters (10 bits). The most significant |
|||
* bit of number is the sign bit. The remaining 9 bits are magnitude bits. |
|||
* Negative numbers are represented using two's-complement notation. |
|||
* If number is not a valid binary number, or if number contains more than |
|||
* 10 characters (10 bits), BIN2DEC returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toDecimal($value) |
|||
{ |
|||
if (is_array($value)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateBinary($value); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (strlen($value) == 10) { |
|||
// Two's Complement |
|||
$value = substr($value, -9); |
|||
|
|||
return '-' . (512 - bindec($value)); |
|||
} |
|||
|
|||
return (string) bindec($value); |
|||
} |
|||
|
|||
/** |
|||
* toHex. |
|||
* |
|||
* Return a binary value as hex. |
|||
* |
|||
* Excel Function: |
|||
* BIN2HEX(x[,places]) |
|||
* |
|||
* @param array|string $value 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. |
|||
* Or can be an array of values |
|||
* @param array|int $places The number of characters to use. If places is omitted, BIN2HEX uses the |
|||
* minimum number of characters necessary. Places is useful for padding the |
|||
* return value with leading 0s (zeros). |
|||
* If places is not an integer, it is truncated. |
|||
* If places is nonnumeric, BIN2HEX returns the #VALUE! error value. |
|||
* If places is negative, BIN2HEX returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toHex($value, $places = null) |
|||
{ |
|||
if (is_array($value) || is_array($places)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateBinary($value); |
|||
$places = self::validatePlaces($places); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (strlen($value) == 10) { |
|||
$high2 = substr($value, 0, 2); |
|||
$low8 = substr($value, 2); |
|||
$xarr = ['00' => '00000000', '01' => '00000001', '10' => 'FFFFFFFE', '11' => 'FFFFFFFF']; |
|||
|
|||
return $xarr[$high2] . strtoupper(substr('0' . dechex((int) bindec($low8)), -2)); |
|||
} |
|||
$hexVal = (string) strtoupper(dechex((int) bindec($value))); |
|||
|
|||
return self::nbrConversionFormat($hexVal, $places); |
|||
} |
|||
|
|||
/** |
|||
* toOctal. |
|||
* |
|||
* Return a binary value as octal. |
|||
* |
|||
* Excel Function: |
|||
* BIN2OCT(x[,places]) |
|||
* |
|||
* @param array|string $value 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. |
|||
* Or can be an array of values |
|||
* @param array|int $places The number of characters to use. If places is omitted, BIN2OCT uses the |
|||
* minimum number of characters necessary. Places is useful for padding the |
|||
* return value with leading 0s (zeros). |
|||
* If places is not an integer, it is truncated. |
|||
* If places is nonnumeric, BIN2OCT returns the #VALUE! error value. |
|||
* If places is negative, BIN2OCT returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toOctal($value, $places = null) |
|||
{ |
|||
if (is_array($value) || is_array($places)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateBinary($value); |
|||
$places = self::validatePlaces($places); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (strlen($value) == 10 && substr($value, 0, 1) === '1') { // Two's Complement |
|||
return str_repeat('7', 6) . strtoupper(decoct((int) bindec("11$value"))); |
|||
} |
|||
$octVal = (string) decoct((int) bindec($value)); |
|||
|
|||
return self::nbrConversionFormat($octVal, $places); |
|||
} |
|||
|
|||
protected static function validateBinary(string $value): string |
|||
{ |
|||
if ((strlen($value) > preg_match_all('/[01]/', $value)) || (strlen($value) > 10)) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return $value; |
|||
} |
|||
} |
|||
@ -0,0 +1,213 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class ConvertDecimal extends ConvertBase |
|||
{ |
|||
const LARGEST_OCTAL_IN_DECIMAL = 536870911; |
|||
const SMALLEST_OCTAL_IN_DECIMAL = -536870912; |
|||
const LARGEST_BINARY_IN_DECIMAL = 511; |
|||
const SMALLEST_BINARY_IN_DECIMAL = -512; |
|||
const LARGEST_HEX_IN_DECIMAL = 549755813887; |
|||
const SMALLEST_HEX_IN_DECIMAL = -549755813888; |
|||
|
|||
/** |
|||
* toBinary. |
|||
* |
|||
* Return a decimal value as binary. |
|||
* |
|||
* Excel Function: |
|||
* DEC2BIN(x[,places]) |
|||
* |
|||
* @param array|string $value The decimal integer you want to convert. If number is negative, |
|||
* valid place values are ignored and DEC2BIN returns a 10-character |
|||
* (10-bit) binary number in which the most significant bit is the sign |
|||
* bit. The remaining 9 bits are magnitude bits. Negative numbers are |
|||
* represented using two's-complement notation. |
|||
* If number < -512 or if number > 511, DEC2BIN returns the #NUM! error |
|||
* value. |
|||
* If number is nonnumeric, DEC2BIN returns the #VALUE! error value. |
|||
* If DEC2BIN requires more than places characters, it returns the #NUM! |
|||
* error value. |
|||
* Or can be an array of values |
|||
* @param array|int $places The number of characters to use. If places is omitted, DEC2BIN uses |
|||
* the minimum number of characters necessary. Places is useful for |
|||
* padding the return value with leading 0s (zeros). |
|||
* If places is not an integer, it is truncated. |
|||
* If places is nonnumeric, DEC2BIN returns the #VALUE! error value. |
|||
* If places is zero or negative, DEC2BIN returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toBinary($value, $places = null) |
|||
{ |
|||
if (is_array($value) || is_array($places)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateDecimal($value); |
|||
$places = self::validatePlaces($places); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$value = (int) floor((float) $value); |
|||
if ($value > self::LARGEST_BINARY_IN_DECIMAL || $value < self::SMALLEST_BINARY_IN_DECIMAL) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$r = decbin($value); |
|||
// Two's Complement |
|||
$r = substr($r, -10); |
|||
|
|||
return self::nbrConversionFormat($r, $places); |
|||
} |
|||
|
|||
/** |
|||
* toHex. |
|||
* |
|||
* Return a decimal value as hex. |
|||
* |
|||
* Excel Function: |
|||
* DEC2HEX(x[,places]) |
|||
* |
|||
* @param array|string $value The decimal integer you want to convert. If number is negative, |
|||
* places is ignored and DEC2HEX returns a 10-character (40-bit) |
|||
* hexadecimal number in which the most significant bit is the sign |
|||
* bit. The remaining 39 bits are magnitude bits. Negative numbers |
|||
* are represented using two's-complement notation. |
|||
* If number < -549,755,813,888 or if number > 549,755,813,887, |
|||
* DEC2HEX returns the #NUM! error value. |
|||
* If number is nonnumeric, DEC2HEX returns the #VALUE! error value. |
|||
* If DEC2HEX requires more than places characters, it returns the |
|||
* #NUM! error value. |
|||
* Or can be an array of values |
|||
* @param array|int $places The number of characters to use. If places is omitted, DEC2HEX uses |
|||
* the minimum number of characters necessary. Places is useful for |
|||
* padding the return value with leading 0s (zeros). |
|||
* If places is not an integer, it is truncated. |
|||
* If places is nonnumeric, DEC2HEX returns the #VALUE! error value. |
|||
* If places is zero or negative, DEC2HEX returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toHex($value, $places = null) |
|||
{ |
|||
if (is_array($value) || is_array($places)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateDecimal($value); |
|||
$places = self::validatePlaces($places); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$value = floor((float) $value); |
|||
if ($value > self::LARGEST_HEX_IN_DECIMAL || $value < self::SMALLEST_HEX_IN_DECIMAL) { |
|||
return Functions::NAN(); |
|||
} |
|||
$r = strtoupper(dechex((int) $value)); |
|||
$r = self::hex32bit($value, $r); |
|||
|
|||
return self::nbrConversionFormat($r, $places); |
|||
} |
|||
|
|||
public static function hex32bit(float $value, string $hexstr, bool $force = false): string |
|||
{ |
|||
if (PHP_INT_SIZE === 4 || $force) { |
|||
if ($value >= 2 ** 32) { |
|||
$quotient = (int) ($value / (2 ** 32)); |
|||
|
|||
return strtoupper(substr('0' . dechex($quotient), -2) . $hexstr); |
|||
} |
|||
if ($value < -(2 ** 32)) { |
|||
$quotient = 256 - (int) ceil((-$value) / (2 ** 32)); |
|||
|
|||
return strtoupper(substr('0' . dechex($quotient), -2) . substr("00000000$hexstr", -8)); |
|||
} |
|||
if ($value < 0) { |
|||
return "FF$hexstr"; |
|||
} |
|||
} |
|||
|
|||
return $hexstr; |
|||
} |
|||
|
|||
/** |
|||
* toOctal. |
|||
* |
|||
* Return an decimal value as octal. |
|||
* |
|||
* Excel Function: |
|||
* DEC2OCT(x[,places]) |
|||
* |
|||
* @param array|string $value The decimal integer you want to convert. If number is negative, |
|||
* places is ignored and DEC2OCT returns a 10-character (30-bit) |
|||
* octal number in which the most significant bit is the sign bit. |
|||
* The remaining 29 bits are magnitude bits. Negative numbers are |
|||
* represented using two's-complement notation. |
|||
* If number < -536,870,912 or if number > 536,870,911, DEC2OCT |
|||
* returns the #NUM! error value. |
|||
* If number is nonnumeric, DEC2OCT returns the #VALUE! error value. |
|||
* If DEC2OCT requires more than places characters, it returns the |
|||
* #NUM! error value. |
|||
* Or can be an array of values |
|||
* @param array|int $places The number of characters to use. If places is omitted, DEC2OCT uses |
|||
* the minimum number of characters necessary. Places is useful for |
|||
* padding the return value with leading 0s (zeros). |
|||
* If places is not an integer, it is truncated. |
|||
* If places is nonnumeric, DEC2OCT returns the #VALUE! error value. |
|||
* If places is zero or negative, DEC2OCT returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toOctal($value, $places = null) |
|||
{ |
|||
if (is_array($value) || is_array($places)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateDecimal($value); |
|||
$places = self::validatePlaces($places); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$value = (int) floor((float) $value); |
|||
if ($value > self::LARGEST_OCTAL_IN_DECIMAL || $value < self::SMALLEST_OCTAL_IN_DECIMAL) { |
|||
return Functions::NAN(); |
|||
} |
|||
$r = decoct($value); |
|||
$r = substr($r, -10); |
|||
|
|||
return self::nbrConversionFormat($r, $places); |
|||
} |
|||
|
|||
protected static function validateDecimal(string $value): string |
|||
{ |
|||
if (strlen($value) > preg_match_all('/[-0123456789.]/', $value)) { |
|||
throw new Exception(Functions::VALUE()); |
|||
} |
|||
|
|||
return $value; |
|||
} |
|||
} |
|||
@ -0,0 +1,175 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class ConvertHex extends ConvertBase |
|||
{ |
|||
/** |
|||
* toBinary. |
|||
* |
|||
* Return a hex value as binary. |
|||
* |
|||
* Excel Function: |
|||
* HEX2BIN(x[,places]) |
|||
* |
|||
* @param array|string $value The hexadecimal number you want to convert. |
|||
* Number cannot contain more than 10 characters. |
|||
* The most significant bit of number is the sign bit (40th bit from the right). |
|||
* The remaining 9 bits are magnitude bits. |
|||
* Negative numbers are represented using two's-complement notation. |
|||
* If number is negative, HEX2BIN ignores places and returns a 10-character binary number. |
|||
* If number is negative, it cannot be less than FFFFFFFE00, |
|||
* and if number is positive, it cannot be greater than 1FF. |
|||
* If number is not a valid hexadecimal number, HEX2BIN returns the #NUM! error value. |
|||
* If HEX2BIN requires more than places characters, it returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* @param array|int $places The number of characters to use. If places is omitted, |
|||
* HEX2BIN uses the minimum number of characters necessary. Places |
|||
* is useful for padding the return value with leading 0s (zeros). |
|||
* If places is not an integer, it is truncated. |
|||
* If places is nonnumeric, HEX2BIN returns the #VALUE! error value. |
|||
* If places is negative, HEX2BIN returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toBinary($value, $places = null) |
|||
{ |
|||
if (is_array($value) || is_array($places)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateHex($value); |
|||
$places = self::validatePlaces($places); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$dec = self::toDecimal($value); |
|||
|
|||
return ConvertDecimal::toBinary($dec, $places); |
|||
} |
|||
|
|||
/** |
|||
* toDecimal. |
|||
* |
|||
* Return a hex value as decimal. |
|||
* |
|||
* Excel Function: |
|||
* HEX2DEC(x) |
|||
* |
|||
* @param array|string $value The hexadecimal number you want to convert. This number cannot |
|||
* contain more than 10 characters (40 bits). The most significant |
|||
* bit of number is the sign bit. The remaining 39 bits are magnitude |
|||
* bits. Negative numbers are represented using two's-complement |
|||
* notation. |
|||
* If number is not a valid hexadecimal number, HEX2DEC returns the |
|||
* #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toDecimal($value) |
|||
{ |
|||
if (is_array($value)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateHex($value); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (strlen($value) > 10) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$binX = ''; |
|||
foreach (str_split($value) as $char) { |
|||
$binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT); |
|||
} |
|||
if (strlen($binX) == 40 && $binX[0] == '1') { |
|||
for ($i = 0; $i < 40; ++$i) { |
|||
$binX[$i] = ($binX[$i] == '1' ? '0' : '1'); |
|||
} |
|||
|
|||
return (string) ((bindec($binX) + 1) * -1); |
|||
} |
|||
|
|||
return (string) bindec($binX); |
|||
} |
|||
|
|||
/** |
|||
* toOctal. |
|||
* |
|||
* Return a hex value as octal. |
|||
* |
|||
* Excel Function: |
|||
* HEX2OCT(x[,places]) |
|||
* |
|||
* @param array|string $value The hexadecimal number you want to convert. Number cannot |
|||
* contain more than 10 characters. The most significant bit of |
|||
* number is the sign bit. The remaining 39 bits are magnitude |
|||
* bits. Negative numbers are represented using two's-complement |
|||
* notation. |
|||
* If number is negative, HEX2OCT ignores places and returns a |
|||
* 10-character octal number. |
|||
* If number is negative, it cannot be less than FFE0000000, and |
|||
* if number is positive, it cannot be greater than 1FFFFFFF. |
|||
* If number is not a valid hexadecimal number, HEX2OCT returns |
|||
* the #NUM! error value. |
|||
* If HEX2OCT requires more than places characters, it returns |
|||
* the #NUM! error value. |
|||
* Or can be an array of values |
|||
* @param array|int $places The number of characters to use. If places is omitted, HEX2OCT |
|||
* uses the minimum number of characters necessary. Places is |
|||
* useful for padding the return value with leading 0s (zeros). |
|||
* If places is not an integer, it is truncated. |
|||
* If places is nonnumeric, HEX2OCT returns the #VALUE! error |
|||
* value. |
|||
* If places is negative, HEX2OCT returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toOctal($value, $places = null) |
|||
{ |
|||
if (is_array($value) || is_array($places)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateHex($value); |
|||
$places = self::validatePlaces($places); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$decimal = self::toDecimal($value); |
|||
|
|||
return ConvertDecimal::toOctal($decimal, $places); |
|||
} |
|||
|
|||
protected static function validateHex(string $value): string |
|||
{ |
|||
if (strlen($value) > preg_match_all('/[0123456789ABCDEF]/', $value)) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return $value; |
|||
} |
|||
} |
|||
@ -0,0 +1,174 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class ConvertOctal extends ConvertBase |
|||
{ |
|||
/** |
|||
* toBinary. |
|||
* |
|||
* Return an octal value as binary. |
|||
* |
|||
* Excel Function: |
|||
* OCT2BIN(x[,places]) |
|||
* |
|||
* @param array|string $value The octal number you want to convert. Number may not |
|||
* contain more than 10 characters. The most significant |
|||
* bit of number is the sign bit. The remaining 29 bits |
|||
* are magnitude bits. Negative numbers are represented |
|||
* using two's-complement notation. |
|||
* If number is negative, OCT2BIN ignores places and returns |
|||
* a 10-character binary number. |
|||
* If number is negative, it cannot be less than 7777777000, |
|||
* and if number is positive, it cannot be greater than 777. |
|||
* If number is not a valid octal number, OCT2BIN returns |
|||
* the #NUM! error value. |
|||
* If OCT2BIN requires more than places characters, it |
|||
* returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* @param array|int $places The number of characters to use. If places is omitted, |
|||
* OCT2BIN uses the minimum number of characters necessary. |
|||
* Places is useful for padding the return value with |
|||
* leading 0s (zeros). |
|||
* If places is not an integer, it is truncated. |
|||
* If places is nonnumeric, OCT2BIN returns the #VALUE! |
|||
* error value. |
|||
* If places is negative, OCT2BIN returns the #NUM! error |
|||
* value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toBinary($value, $places = null) |
|||
{ |
|||
if (is_array($value) || is_array($places)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateOctal($value); |
|||
$places = self::validatePlaces($places); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return ConvertDecimal::toBinary(self::toDecimal($value), $places); |
|||
} |
|||
|
|||
/** |
|||
* toDecimal. |
|||
* |
|||
* Return an octal value as decimal. |
|||
* |
|||
* Excel Function: |
|||
* OCT2DEC(x) |
|||
* |
|||
* @param array|string $value The octal number you want to convert. Number may not contain |
|||
* more than 10 octal characters (30 bits). The most significant |
|||
* bit of number is the sign bit. The remaining 29 bits are |
|||
* magnitude bits. Negative numbers are represented using |
|||
* two's-complement notation. |
|||
* If number is not a valid octal number, OCT2DEC returns the |
|||
* #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toDecimal($value) |
|||
{ |
|||
if (is_array($value)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateOctal($value); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$binX = ''; |
|||
foreach (str_split($value) as $char) { |
|||
$binX .= str_pad(decbin((int) $char), 3, '0', STR_PAD_LEFT); |
|||
} |
|||
if (strlen($binX) == 30 && $binX[0] == '1') { |
|||
for ($i = 0; $i < 30; ++$i) { |
|||
$binX[$i] = ($binX[$i] == '1' ? '0' : '1'); |
|||
} |
|||
|
|||
return (string) ((bindec($binX) + 1) * -1); |
|||
} |
|||
|
|||
return (string) bindec($binX); |
|||
} |
|||
|
|||
/** |
|||
* toHex. |
|||
* |
|||
* Return an octal value as hex. |
|||
* |
|||
* Excel Function: |
|||
* OCT2HEX(x[,places]) |
|||
* |
|||
* @param array|string $value The octal number you want to convert. Number may not contain |
|||
* more than 10 octal characters (30 bits). The most significant |
|||
* bit of number is the sign bit. The remaining 29 bits are |
|||
* magnitude bits. Negative numbers are represented using |
|||
* two's-complement notation. |
|||
* If number is negative, OCT2HEX ignores places and returns a |
|||
* 10-character hexadecimal number. |
|||
* If number is not a valid octal number, OCT2HEX returns the |
|||
* #NUM! error value. |
|||
* If OCT2HEX requires more than places characters, it returns |
|||
* the #NUM! error value. |
|||
* Or can be an array of values |
|||
* @param array|int $places The number of characters to use. If places is omitted, OCT2HEX |
|||
* uses the minimum number of characters necessary. Places is useful |
|||
* for padding the return value with leading 0s (zeros). |
|||
* If places is not an integer, it is truncated. |
|||
* If places is nonnumeric, OCT2HEX returns the #VALUE! error value. |
|||
* If places is negative, OCT2HEX returns the #NUM! error value. |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string Result, or an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toHex($value, $places = null) |
|||
{ |
|||
if (is_array($value) || is_array($places)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); |
|||
} |
|||
|
|||
try { |
|||
$value = self::validateValue($value); |
|||
$value = self::validateOctal($value); |
|||
$places = self::validatePlaces($places); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$hexVal = strtoupper(dechex((int) self::toDecimal($value))); |
|||
$hexVal = (PHP_INT_SIZE === 4 && strlen($value) === 10 && $value[0] >= '4') ? "FF{$hexVal}" : $hexVal; |
|||
|
|||
return self::nbrConversionFormat($hexVal, $places); |
|||
} |
|||
|
|||
protected static function validateOctal(string $value): string |
|||
{ |
|||
$numDigits = (int) preg_match_all('/[01234567]/', $value); |
|||
if (strlen($value) > $numDigits || $numDigits > 10) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return $value; |
|||
} |
|||
} |
|||
@ -0,0 +1,693 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class ConvertUOM |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
public const CATEGORY_WEIGHT_AND_MASS = 'Weight and Mass'; |
|||
public const CATEGORY_DISTANCE = 'Distance'; |
|||
public const CATEGORY_TIME = 'Time'; |
|||
public const CATEGORY_PRESSURE = 'Pressure'; |
|||
public const CATEGORY_FORCE = 'Force'; |
|||
public const CATEGORY_ENERGY = 'Energy'; |
|||
public const CATEGORY_POWER = 'Power'; |
|||
public const CATEGORY_MAGNETISM = 'Magnetism'; |
|||
public const CATEGORY_TEMPERATURE = 'Temperature'; |
|||
public const CATEGORY_VOLUME = 'Volume and Liquid Measure'; |
|||
public const CATEGORY_AREA = 'Area'; |
|||
public const CATEGORY_INFORMATION = 'Information'; |
|||
public const CATEGORY_SPEED = 'Speed'; |
|||
|
|||
/** |
|||
* Details of the Units of measure that can be used in CONVERTUOM(). |
|||
* |
|||
* @var mixed[] |
|||
*/ |
|||
private static $conversionUnits = [ |
|||
// Weight and Mass |
|||
'g' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Gram', 'AllowPrefix' => true], |
|||
'sg' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Slug', 'AllowPrefix' => false], |
|||
'lbm' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Pound mass (avoirdupois)', 'AllowPrefix' => false], |
|||
'u' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'U (atomic mass unit)', 'AllowPrefix' => true], |
|||
'ozm' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Ounce mass (avoirdupois)', 'AllowPrefix' => false], |
|||
'grain' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Grain', 'AllowPrefix' => false], |
|||
'cwt' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'U.S. (short) hundredweight', 'AllowPrefix' => false], |
|||
'shweight' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'U.S. (short) hundredweight', 'AllowPrefix' => false], |
|||
'uk_cwt' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Imperial hundredweight', 'AllowPrefix' => false], |
|||
'lcwt' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Imperial hundredweight', 'AllowPrefix' => false], |
|||
'hweight' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Imperial hundredweight', 'AllowPrefix' => false], |
|||
'stone' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Stone', 'AllowPrefix' => false], |
|||
'ton' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Ton', 'AllowPrefix' => false], |
|||
'uk_ton' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Imperial ton', 'AllowPrefix' => false], |
|||
'LTON' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Imperial ton', 'AllowPrefix' => false], |
|||
'brton' => ['Group' => self::CATEGORY_WEIGHT_AND_MASS, 'Unit Name' => 'Imperial ton', 'AllowPrefix' => false], |
|||
// Distance |
|||
'm' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Meter', 'AllowPrefix' => true], |
|||
'mi' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Statute mile', 'AllowPrefix' => false], |
|||
'Nmi' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Nautical mile', 'AllowPrefix' => false], |
|||
'in' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Inch', 'AllowPrefix' => false], |
|||
'ft' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Foot', 'AllowPrefix' => false], |
|||
'yd' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Yard', 'AllowPrefix' => false], |
|||
'ang' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Angstrom', 'AllowPrefix' => true], |
|||
'ell' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Ell', 'AllowPrefix' => false], |
|||
'ly' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Light Year', 'AllowPrefix' => false], |
|||
'parsec' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Parsec', 'AllowPrefix' => false], |
|||
'pc' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Parsec', 'AllowPrefix' => false], |
|||
'Pica' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Pica (1/72 in)', 'AllowPrefix' => false], |
|||
'Picapt' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Pica (1/72 in)', 'AllowPrefix' => false], |
|||
'pica' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'Pica (1/6 in)', 'AllowPrefix' => false], |
|||
'survey_mi' => ['Group' => self::CATEGORY_DISTANCE, 'Unit Name' => 'U.S survey mile (statute mile)', 'AllowPrefix' => false], |
|||
// Time |
|||
'yr' => ['Group' => self::CATEGORY_TIME, 'Unit Name' => 'Year', 'AllowPrefix' => false], |
|||
'day' => ['Group' => self::CATEGORY_TIME, 'Unit Name' => 'Day', 'AllowPrefix' => false], |
|||
'd' => ['Group' => self::CATEGORY_TIME, 'Unit Name' => 'Day', 'AllowPrefix' => false], |
|||
'hr' => ['Group' => self::CATEGORY_TIME, 'Unit Name' => 'Hour', 'AllowPrefix' => false], |
|||
'mn' => ['Group' => self::CATEGORY_TIME, 'Unit Name' => 'Minute', 'AllowPrefix' => false], |
|||
'min' => ['Group' => self::CATEGORY_TIME, 'Unit Name' => 'Minute', 'AllowPrefix' => false], |
|||
'sec' => ['Group' => self::CATEGORY_TIME, 'Unit Name' => 'Second', 'AllowPrefix' => true], |
|||
's' => ['Group' => self::CATEGORY_TIME, 'Unit Name' => 'Second', 'AllowPrefix' => true], |
|||
// Pressure |
|||
'Pa' => ['Group' => self::CATEGORY_PRESSURE, 'Unit Name' => 'Pascal', 'AllowPrefix' => true], |
|||
'p' => ['Group' => self::CATEGORY_PRESSURE, 'Unit Name' => 'Pascal', 'AllowPrefix' => true], |
|||
'atm' => ['Group' => self::CATEGORY_PRESSURE, 'Unit Name' => 'Atmosphere', 'AllowPrefix' => true], |
|||
'at' => ['Group' => self::CATEGORY_PRESSURE, 'Unit Name' => 'Atmosphere', 'AllowPrefix' => true], |
|||
'mmHg' => ['Group' => self::CATEGORY_PRESSURE, 'Unit Name' => 'mm of Mercury', 'AllowPrefix' => true], |
|||
'psi' => ['Group' => self::CATEGORY_PRESSURE, 'Unit Name' => 'PSI', 'AllowPrefix' => true], |
|||
'Torr' => ['Group' => self::CATEGORY_PRESSURE, 'Unit Name' => 'Torr', 'AllowPrefix' => true], |
|||
// Force |
|||
'N' => ['Group' => self::CATEGORY_FORCE, 'Unit Name' => 'Newton', 'AllowPrefix' => true], |
|||
'dyn' => ['Group' => self::CATEGORY_FORCE, 'Unit Name' => 'Dyne', 'AllowPrefix' => true], |
|||
'dy' => ['Group' => self::CATEGORY_FORCE, 'Unit Name' => 'Dyne', 'AllowPrefix' => true], |
|||
'lbf' => ['Group' => self::CATEGORY_FORCE, 'Unit Name' => 'Pound force', 'AllowPrefix' => false], |
|||
'pond' => ['Group' => self::CATEGORY_FORCE, 'Unit Name' => 'Pond', 'AllowPrefix' => true], |
|||
// Energy |
|||
'J' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Joule', 'AllowPrefix' => true], |
|||
'e' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Erg', 'AllowPrefix' => true], |
|||
'c' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Thermodynamic calorie', 'AllowPrefix' => true], |
|||
'cal' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'IT calorie', 'AllowPrefix' => true], |
|||
'eV' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Electron volt', 'AllowPrefix' => true], |
|||
'ev' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Electron volt', 'AllowPrefix' => true], |
|||
'HPh' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Horsepower-hour', 'AllowPrefix' => false], |
|||
'hh' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Horsepower-hour', 'AllowPrefix' => false], |
|||
'Wh' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Watt-hour', 'AllowPrefix' => true], |
|||
'wh' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Watt-hour', 'AllowPrefix' => true], |
|||
'flb' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'Foot-pound', 'AllowPrefix' => false], |
|||
'BTU' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'BTU', 'AllowPrefix' => false], |
|||
'btu' => ['Group' => self::CATEGORY_ENERGY, 'Unit Name' => 'BTU', 'AllowPrefix' => false], |
|||
// Power |
|||
'HP' => ['Group' => self::CATEGORY_POWER, 'Unit Name' => 'Horsepower', 'AllowPrefix' => false], |
|||
'h' => ['Group' => self::CATEGORY_POWER, 'Unit Name' => 'Horsepower', 'AllowPrefix' => false], |
|||
'W' => ['Group' => self::CATEGORY_POWER, 'Unit Name' => 'Watt', 'AllowPrefix' => true], |
|||
'w' => ['Group' => self::CATEGORY_POWER, 'Unit Name' => 'Watt', 'AllowPrefix' => true], |
|||
'PS' => ['Group' => self::CATEGORY_POWER, 'Unit Name' => 'Pferdestärke', 'AllowPrefix' => false], |
|||
'T' => ['Group' => self::CATEGORY_MAGNETISM, 'Unit Name' => 'Tesla', 'AllowPrefix' => true], |
|||
'ga' => ['Group' => self::CATEGORY_MAGNETISM, 'Unit Name' => 'Gauss', 'AllowPrefix' => true], |
|||
// Temperature |
|||
'C' => ['Group' => self::CATEGORY_TEMPERATURE, 'Unit Name' => 'Degrees Celsius', 'AllowPrefix' => false], |
|||
'cel' => ['Group' => self::CATEGORY_TEMPERATURE, 'Unit Name' => 'Degrees Celsius', 'AllowPrefix' => false], |
|||
'F' => ['Group' => self::CATEGORY_TEMPERATURE, 'Unit Name' => 'Degrees Fahrenheit', 'AllowPrefix' => false], |
|||
'fah' => ['Group' => self::CATEGORY_TEMPERATURE, 'Unit Name' => 'Degrees Fahrenheit', 'AllowPrefix' => false], |
|||
'K' => ['Group' => self::CATEGORY_TEMPERATURE, 'Unit Name' => 'Kelvin', 'AllowPrefix' => false], |
|||
'kel' => ['Group' => self::CATEGORY_TEMPERATURE, 'Unit Name' => 'Kelvin', 'AllowPrefix' => false], |
|||
'Rank' => ['Group' => self::CATEGORY_TEMPERATURE, 'Unit Name' => 'Degrees Rankine', 'AllowPrefix' => false], |
|||
'Reau' => ['Group' => self::CATEGORY_TEMPERATURE, 'Unit Name' => 'Degrees Réaumur', 'AllowPrefix' => false], |
|||
// Volume |
|||
'l' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Litre', 'AllowPrefix' => true], |
|||
'L' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Litre', 'AllowPrefix' => true], |
|||
'lt' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Litre', 'AllowPrefix' => true], |
|||
'tsp' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Teaspoon', 'AllowPrefix' => false], |
|||
'tspm' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Modern Teaspoon', 'AllowPrefix' => false], |
|||
'tbs' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Tablespoon', 'AllowPrefix' => false], |
|||
'oz' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Fluid Ounce', 'AllowPrefix' => false], |
|||
'cup' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cup', 'AllowPrefix' => false], |
|||
'pt' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'U.S. Pint', 'AllowPrefix' => false], |
|||
'us_pt' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'U.S. Pint', 'AllowPrefix' => false], |
|||
'uk_pt' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'U.K. Pint', 'AllowPrefix' => false], |
|||
'qt' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Quart', 'AllowPrefix' => false], |
|||
'uk_qt' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Imperial Quart (UK)', 'AllowPrefix' => false], |
|||
'gal' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Gallon', 'AllowPrefix' => false], |
|||
'uk_gal' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Imperial Gallon (UK)', 'AllowPrefix' => false], |
|||
'ang3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Angstrom', 'AllowPrefix' => true], |
|||
'ang^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Angstrom', 'AllowPrefix' => true], |
|||
'barrel' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'US Oil Barrel', 'AllowPrefix' => false], |
|||
'bushel' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'US Bushel', 'AllowPrefix' => false], |
|||
'in3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Inch', 'AllowPrefix' => false], |
|||
'in^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Inch', 'AllowPrefix' => false], |
|||
'ft3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Foot', 'AllowPrefix' => false], |
|||
'ft^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Foot', 'AllowPrefix' => false], |
|||
'ly3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Light Year', 'AllowPrefix' => false], |
|||
'ly^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Light Year', 'AllowPrefix' => false], |
|||
'm3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Meter', 'AllowPrefix' => true], |
|||
'm^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Meter', 'AllowPrefix' => true], |
|||
'mi3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Mile', 'AllowPrefix' => false], |
|||
'mi^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Mile', 'AllowPrefix' => false], |
|||
'yd3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Yard', 'AllowPrefix' => false], |
|||
'yd^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Yard', 'AllowPrefix' => false], |
|||
'Nmi3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Nautical Mile', 'AllowPrefix' => false], |
|||
'Nmi^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Nautical Mile', 'AllowPrefix' => false], |
|||
'Pica3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Pica', 'AllowPrefix' => false], |
|||
'Pica^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Pica', 'AllowPrefix' => false], |
|||
'Picapt3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Pica', 'AllowPrefix' => false], |
|||
'Picapt^3' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Cubic Pica', 'AllowPrefix' => false], |
|||
'GRT' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Gross Registered Ton', 'AllowPrefix' => false], |
|||
'regton' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Gross Registered Ton', 'AllowPrefix' => false], |
|||
'MTON' => ['Group' => self::CATEGORY_VOLUME, 'Unit Name' => 'Measurement Ton (Freight Ton)', 'AllowPrefix' => false], |
|||
// Area |
|||
'ha' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Hectare', 'AllowPrefix' => true], |
|||
'uk_acre' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'International Acre', 'AllowPrefix' => false], |
|||
'us_acre' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'US Survey/Statute Acre', 'AllowPrefix' => false], |
|||
'ang2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Angstrom', 'AllowPrefix' => true], |
|||
'ang^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Angstrom', 'AllowPrefix' => true], |
|||
'ar' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Are', 'AllowPrefix' => true], |
|||
'ft2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Feet', 'AllowPrefix' => false], |
|||
'ft^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Feet', 'AllowPrefix' => false], |
|||
'in2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Inches', 'AllowPrefix' => false], |
|||
'in^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Inches', 'AllowPrefix' => false], |
|||
'ly2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Light Years', 'AllowPrefix' => false], |
|||
'ly^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Light Years', 'AllowPrefix' => false], |
|||
'm2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Meters', 'AllowPrefix' => true], |
|||
'm^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Meters', 'AllowPrefix' => true], |
|||
'Morgen' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Morgen', 'AllowPrefix' => false], |
|||
'mi2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Miles', 'AllowPrefix' => false], |
|||
'mi^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Miles', 'AllowPrefix' => false], |
|||
'Nmi2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Nautical Miles', 'AllowPrefix' => false], |
|||
'Nmi^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Nautical Miles', 'AllowPrefix' => false], |
|||
'Pica2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Pica', 'AllowPrefix' => false], |
|||
'Pica^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Pica', 'AllowPrefix' => false], |
|||
'Picapt2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Pica', 'AllowPrefix' => false], |
|||
'Picapt^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Pica', 'AllowPrefix' => false], |
|||
'yd2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Yards', 'AllowPrefix' => false], |
|||
'yd^2' => ['Group' => self::CATEGORY_AREA, 'Unit Name' => 'Square Yards', 'AllowPrefix' => false], |
|||
// Information |
|||
'byte' => ['Group' => self::CATEGORY_INFORMATION, 'Unit Name' => 'Byte', 'AllowPrefix' => true], |
|||
'bit' => ['Group' => self::CATEGORY_INFORMATION, 'Unit Name' => 'Bit', 'AllowPrefix' => true], |
|||
// Speed |
|||
'm/s' => ['Group' => self::CATEGORY_SPEED, 'Unit Name' => 'Meters per second', 'AllowPrefix' => true], |
|||
'm/sec' => ['Group' => self::CATEGORY_SPEED, 'Unit Name' => 'Meters per second', 'AllowPrefix' => true], |
|||
'm/h' => ['Group' => self::CATEGORY_SPEED, 'Unit Name' => 'Meters per hour', 'AllowPrefix' => true], |
|||
'm/hr' => ['Group' => self::CATEGORY_SPEED, 'Unit Name' => 'Meters per hour', 'AllowPrefix' => true], |
|||
'mph' => ['Group' => self::CATEGORY_SPEED, 'Unit Name' => 'Miles per hour', 'AllowPrefix' => false], |
|||
'admkn' => ['Group' => self::CATEGORY_SPEED, 'Unit Name' => 'Admiralty Knot', 'AllowPrefix' => false], |
|||
'kn' => ['Group' => self::CATEGORY_SPEED, 'Unit Name' => 'Knot', 'AllowPrefix' => false], |
|||
]; |
|||
|
|||
/** |
|||
* Details of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM(). |
|||
* |
|||
* @var mixed[] |
|||
*/ |
|||
private static $conversionMultipliers = [ |
|||
'Y' => ['multiplier' => 1E24, 'name' => 'yotta'], |
|||
'Z' => ['multiplier' => 1E21, 'name' => 'zetta'], |
|||
'E' => ['multiplier' => 1E18, 'name' => 'exa'], |
|||
'P' => ['multiplier' => 1E15, 'name' => 'peta'], |
|||
'T' => ['multiplier' => 1E12, 'name' => 'tera'], |
|||
'G' => ['multiplier' => 1E9, 'name' => 'giga'], |
|||
'M' => ['multiplier' => 1E6, 'name' => 'mega'], |
|||
'k' => ['multiplier' => 1E3, 'name' => 'kilo'], |
|||
'h' => ['multiplier' => 1E2, 'name' => 'hecto'], |
|||
'e' => ['multiplier' => 1E1, 'name' => 'dekao'], |
|||
'da' => ['multiplier' => 1E1, 'name' => 'dekao'], |
|||
'd' => ['multiplier' => 1E-1, 'name' => 'deci'], |
|||
'c' => ['multiplier' => 1E-2, 'name' => 'centi'], |
|||
'm' => ['multiplier' => 1E-3, 'name' => 'milli'], |
|||
'u' => ['multiplier' => 1E-6, 'name' => 'micro'], |
|||
'n' => ['multiplier' => 1E-9, 'name' => 'nano'], |
|||
'p' => ['multiplier' => 1E-12, 'name' => 'pico'], |
|||
'f' => ['multiplier' => 1E-15, 'name' => 'femto'], |
|||
'a' => ['multiplier' => 1E-18, 'name' => 'atto'], |
|||
'z' => ['multiplier' => 1E-21, 'name' => 'zepto'], |
|||
'y' => ['multiplier' => 1E-24, 'name' => 'yocto'], |
|||
]; |
|||
|
|||
/** |
|||
* Details of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM(). |
|||
* |
|||
* @var mixed[] |
|||
*/ |
|||
private static $binaryConversionMultipliers = [ |
|||
'Yi' => ['multiplier' => 2 ** 80, 'name' => 'yobi'], |
|||
'Zi' => ['multiplier' => 2 ** 70, 'name' => 'zebi'], |
|||
'Ei' => ['multiplier' => 2 ** 60, 'name' => 'exbi'], |
|||
'Pi' => ['multiplier' => 2 ** 50, 'name' => 'pebi'], |
|||
'Ti' => ['multiplier' => 2 ** 40, 'name' => 'tebi'], |
|||
'Gi' => ['multiplier' => 2 ** 30, 'name' => 'gibi'], |
|||
'Mi' => ['multiplier' => 2 ** 20, 'name' => 'mebi'], |
|||
'ki' => ['multiplier' => 2 ** 10, 'name' => 'kibi'], |
|||
]; |
|||
|
|||
/** |
|||
* Details of the Units of measure conversion factors, organised by group. |
|||
* |
|||
* @var mixed[] |
|||
*/ |
|||
private static $unitConversions = [ |
|||
// Conversion uses gram (g) as an intermediate unit |
|||
self::CATEGORY_WEIGHT_AND_MASS => [ |
|||
'g' => 1.0, |
|||
'sg' => 6.85217658567918E-05, |
|||
'lbm' => 2.20462262184878E-03, |
|||
'u' => 6.02214179421676E+23, |
|||
'ozm' => 3.52739619495804E-02, |
|||
'grain' => 1.54323583529414E+01, |
|||
'cwt' => 2.20462262184878E-05, |
|||
'shweight' => 2.20462262184878E-05, |
|||
'uk_cwt' => 1.96841305522212E-05, |
|||
'lcwt' => 1.96841305522212E-05, |
|||
'hweight' => 1.96841305522212E-05, |
|||
'stone' => 1.57473044417770E-04, |
|||
'ton' => 1.10231131092439E-06, |
|||
'uk_ton' => 9.84206527611061E-07, |
|||
'LTON' => 9.84206527611061E-07, |
|||
'brton' => 9.84206527611061E-07, |
|||
], |
|||
// Conversion uses meter (m) as an intermediate unit |
|||
self::CATEGORY_DISTANCE => [ |
|||
'm' => 1.0, |
|||
'mi' => 6.21371192237334E-04, |
|||
'Nmi' => 5.39956803455724E-04, |
|||
'in' => 3.93700787401575E+01, |
|||
'ft' => 3.28083989501312E+00, |
|||
'yd' => 1.09361329833771E+00, |
|||
'ang' => 1.0E+10, |
|||
'ell' => 8.74890638670166E-01, |
|||
'ly' => 1.05700083402462E-16, |
|||
'parsec' => 3.24077928966473E-17, |
|||
'pc' => 3.24077928966473E-17, |
|||
'Pica' => 2.83464566929134E+03, |
|||
'Picapt' => 2.83464566929134E+03, |
|||
'pica' => 2.36220472440945E+02, |
|||
'survey_mi' => 6.21369949494950E-04, |
|||
], |
|||
// Conversion uses second (s) as an intermediate unit |
|||
self::CATEGORY_TIME => [ |
|||
'yr' => 3.16880878140289E-08, |
|||
'day' => 1.15740740740741E-05, |
|||
'd' => 1.15740740740741E-05, |
|||
'hr' => 2.77777777777778E-04, |
|||
'mn' => 1.66666666666667E-02, |
|||
'min' => 1.66666666666667E-02, |
|||
'sec' => 1.0, |
|||
's' => 1.0, |
|||
], |
|||
// Conversion uses Pascal (Pa) as an intermediate unit |
|||
self::CATEGORY_PRESSURE => [ |
|||
'Pa' => 1.0, |
|||
'p' => 1.0, |
|||
'atm' => 9.86923266716013E-06, |
|||
'at' => 9.86923266716013E-06, |
|||
'mmHg' => 7.50063755419211E-03, |
|||
'psi' => 1.45037737730209E-04, |
|||
'Torr' => 7.50061682704170E-03, |
|||
], |
|||
// Conversion uses Newton (N) as an intermediate unit |
|||
self::CATEGORY_FORCE => [ |
|||
'N' => 1.0, |
|||
'dyn' => 1.0E+5, |
|||
'dy' => 1.0E+5, |
|||
'lbf' => 2.24808923655339E-01, |
|||
'pond' => 1.01971621297793E+02, |
|||
], |
|||
// Conversion uses Joule (J) as an intermediate unit |
|||
self::CATEGORY_ENERGY => [ |
|||
'J' => 1.0, |
|||
'e' => 9.99999519343231E+06, |
|||
'c' => 2.39006249473467E-01, |
|||
'cal' => 2.38846190642017E-01, |
|||
'eV' => 6.24145700000000E+18, |
|||
'ev' => 6.24145700000000E+18, |
|||
'HPh' => 3.72506430801000E-07, |
|||
'hh' => 3.72506430801000E-07, |
|||
'Wh' => 2.77777916238711E-04, |
|||
'wh' => 2.77777916238711E-04, |
|||
'flb' => 2.37304222192651E+01, |
|||
'BTU' => 9.47815067349015E-04, |
|||
'btu' => 9.47815067349015E-04, |
|||
], |
|||
// Conversion uses Horsepower (HP) as an intermediate unit |
|||
self::CATEGORY_POWER => [ |
|||
'HP' => 1.0, |
|||
'h' => 1.0, |
|||
'W' => 7.45699871582270E+02, |
|||
'w' => 7.45699871582270E+02, |
|||
'PS' => 1.01386966542400E+00, |
|||
], |
|||
// Conversion uses Tesla (T) as an intermediate unit |
|||
self::CATEGORY_MAGNETISM => [ |
|||
'T' => 1.0, |
|||
'ga' => 10000.0, |
|||
], |
|||
// Conversion uses litre (l) as an intermediate unit |
|||
self::CATEGORY_VOLUME => [ |
|||
'l' => 1.0, |
|||
'L' => 1.0, |
|||
'lt' => 1.0, |
|||
'tsp' => 2.02884136211058E+02, |
|||
'tspm' => 2.0E+02, |
|||
'tbs' => 6.76280454036860E+01, |
|||
'oz' => 3.38140227018430E+01, |
|||
'cup' => 4.22675283773038E+00, |
|||
'pt' => 2.11337641886519E+00, |
|||
'us_pt' => 2.11337641886519E+00, |
|||
'uk_pt' => 1.75975398639270E+00, |
|||
'qt' => 1.05668820943259E+00, |
|||
'uk_qt' => 8.79876993196351E-01, |
|||
'gal' => 2.64172052358148E-01, |
|||
'uk_gal' => 2.19969248299088E-01, |
|||
'ang3' => 1.0E+27, |
|||
'ang^3' => 1.0E+27, |
|||
'barrel' => 6.28981077043211E-03, |
|||
'bushel' => 2.83775932584017E-02, |
|||
'in3' => 6.10237440947323E+01, |
|||
'in^3' => 6.10237440947323E+01, |
|||
'ft3' => 3.53146667214886E-02, |
|||
'ft^3' => 3.53146667214886E-02, |
|||
'ly3' => 1.18093498844171E-51, |
|||
'ly^3' => 1.18093498844171E-51, |
|||
'm3' => 1.0E-03, |
|||
'm^3' => 1.0E-03, |
|||
'mi3' => 2.39912758578928E-13, |
|||
'mi^3' => 2.39912758578928E-13, |
|||
'yd3' => 1.30795061931439E-03, |
|||
'yd^3' => 1.30795061931439E-03, |
|||
'Nmi3' => 1.57426214685811E-13, |
|||
'Nmi^3' => 1.57426214685811E-13, |
|||
'Pica3' => 2.27769904358706E+07, |
|||
'Pica^3' => 2.27769904358706E+07, |
|||
'Picapt3' => 2.27769904358706E+07, |
|||
'Picapt^3' => 2.27769904358706E+07, |
|||
'GRT' => 3.53146667214886E-04, |
|||
'regton' => 3.53146667214886E-04, |
|||
'MTON' => 8.82866668037215E-04, |
|||
], |
|||
// Conversion uses hectare (ha) as an intermediate unit |
|||
self::CATEGORY_AREA => [ |
|||
'ha' => 1.0, |
|||
'uk_acre' => 2.47105381467165E+00, |
|||
'us_acre' => 2.47104393046628E+00, |
|||
'ang2' => 1.0E+24, |
|||
'ang^2' => 1.0E+24, |
|||
'ar' => 1.0E+02, |
|||
'ft2' => 1.07639104167097E+05, |
|||
'ft^2' => 1.07639104167097E+05, |
|||
'in2' => 1.55000310000620E+07, |
|||
'in^2' => 1.55000310000620E+07, |
|||
'ly2' => 1.11725076312873E-28, |
|||
'ly^2' => 1.11725076312873E-28, |
|||
'm2' => 1.0E+04, |
|||
'm^2' => 1.0E+04, |
|||
'Morgen' => 4.0E+00, |
|||
'mi2' => 3.86102158542446E-03, |
|||
'mi^2' => 3.86102158542446E-03, |
|||
'Nmi2' => 2.91553349598123E-03, |
|||
'Nmi^2' => 2.91553349598123E-03, |
|||
'Pica2' => 8.03521607043214E+10, |
|||
'Pica^2' => 8.03521607043214E+10, |
|||
'Picapt2' => 8.03521607043214E+10, |
|||
'Picapt^2' => 8.03521607043214E+10, |
|||
'yd2' => 1.19599004630108E+04, |
|||
'yd^2' => 1.19599004630108E+04, |
|||
], |
|||
// Conversion uses bit (bit) as an intermediate unit |
|||
self::CATEGORY_INFORMATION => [ |
|||
'bit' => 1.0, |
|||
'byte' => 0.125, |
|||
], |
|||
// Conversion uses Meters per Second (m/s) as an intermediate unit |
|||
self::CATEGORY_SPEED => [ |
|||
'm/s' => 1.0, |
|||
'm/sec' => 1.0, |
|||
'm/h' => 3.60E+03, |
|||
'm/hr' => 3.60E+03, |
|||
'mph' => 2.23693629205440E+00, |
|||
'admkn' => 1.94260256941567E+00, |
|||
'kn' => 1.94384449244060E+00, |
|||
], |
|||
]; |
|||
|
|||
/** |
|||
* getConversionGroups |
|||
* Returns a list of the different conversion groups for UOM conversions. |
|||
* |
|||
* @return array |
|||
*/ |
|||
public static function getConversionCategories() |
|||
{ |
|||
$conversionGroups = []; |
|||
foreach (self::$conversionUnits as $conversionUnit) { |
|||
$conversionGroups[] = $conversionUnit['Group']; |
|||
} |
|||
|
|||
return array_merge(array_unique($conversionGroups)); |
|||
} |
|||
|
|||
/** |
|||
* getConversionGroupUnits |
|||
* Returns an array of units of measure, for a specified conversion group, or for all groups. |
|||
* |
|||
* @param string $category The group whose units of measure you want to retrieve |
|||
* |
|||
* @return array |
|||
*/ |
|||
public static function getConversionCategoryUnits($category = null) |
|||
{ |
|||
$conversionGroups = []; |
|||
foreach (self::$conversionUnits as $conversionUnit => $conversionGroup) { |
|||
if (($category === null) || ($conversionGroup['Group'] == $category)) { |
|||
$conversionGroups[$conversionGroup['Group']][] = $conversionUnit; |
|||
} |
|||
} |
|||
|
|||
return $conversionGroups; |
|||
} |
|||
|
|||
/** |
|||
* getConversionGroupUnitDetails. |
|||
* |
|||
* @param string $category The group whose units of measure you want to retrieve |
|||
* |
|||
* @return array |
|||
*/ |
|||
public static function getConversionCategoryUnitDetails($category = null) |
|||
{ |
|||
$conversionGroups = []; |
|||
foreach (self::$conversionUnits as $conversionUnit => $conversionGroup) { |
|||
if (($category === null) || ($conversionGroup['Group'] == $category)) { |
|||
$conversionGroups[$conversionGroup['Group']][] = [ |
|||
'unit' => $conversionUnit, |
|||
'description' => $conversionGroup['Unit Name'], |
|||
]; |
|||
} |
|||
} |
|||
|
|||
return $conversionGroups; |
|||
} |
|||
|
|||
/** |
|||
* getConversionMultipliers |
|||
* Returns an array of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM(). |
|||
* |
|||
* @return mixed[] |
|||
*/ |
|||
public static function getConversionMultipliers() |
|||
{ |
|||
return self::$conversionMultipliers; |
|||
} |
|||
|
|||
/** |
|||
* getBinaryConversionMultipliers |
|||
* Returns an array of the additional Multiplier prefixes that can be used with Information Units of Measure in CONVERTUOM(). |
|||
* |
|||
* @return mixed[] |
|||
*/ |
|||
public static function getBinaryConversionMultipliers() |
|||
{ |
|||
return self::$binaryConversionMultipliers; |
|||
} |
|||
|
|||
/** |
|||
* CONVERT. |
|||
* |
|||
* Converts a number from one measurement system to another. |
|||
* For example, CONVERT can translate a table of distances in miles to a table of distances |
|||
* in kilometers. |
|||
* |
|||
* Excel Function: |
|||
* CONVERT(value,fromUOM,toUOM) |
|||
* |
|||
* @param array|float|int|string $value the value in fromUOM to convert |
|||
* Or can be an array of values |
|||
* @param array|string $fromUOM the units for value |
|||
* Or can be an array of values |
|||
* @param array|string $toUOM the units for the result |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string Result, or a string containing an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function CONVERT($value, $fromUOM, $toUOM) |
|||
{ |
|||
if (is_array($value) || is_array($fromUOM) || is_array($toUOM)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $fromUOM, $toUOM); |
|||
} |
|||
|
|||
if (!is_numeric($value)) { |
|||
return Functions::VALUE(); |
|||
} |
|||
|
|||
try { |
|||
[$fromUOM, $fromCategory, $fromMultiplier] = self::getUOMDetails($fromUOM); |
|||
[$toUOM, $toCategory, $toMultiplier] = self::getUOMDetails($toUOM); |
|||
} catch (Exception $e) { |
|||
return Functions::NA(); |
|||
} |
|||
|
|||
if ($fromCategory !== $toCategory) { |
|||
return Functions::NA(); |
|||
} |
|||
|
|||
// @var float $value |
|||
$value *= $fromMultiplier; |
|||
|
|||
if (($fromUOM === $toUOM) && ($fromMultiplier === $toMultiplier)) { |
|||
// We've already factored $fromMultiplier into the value, so we need |
|||
// to reverse it again |
|||
return $value / $fromMultiplier; |
|||
} elseif ($fromUOM === $toUOM) { |
|||
return $value / $toMultiplier; |
|||
} elseif ($fromCategory === self::CATEGORY_TEMPERATURE) { |
|||
return self::convertTemperature($fromUOM, $toUOM, $value); |
|||
} |
|||
|
|||
$baseValue = $value * (1.0 / self::$unitConversions[$fromCategory][$fromUOM]); |
|||
|
|||
return ($baseValue * self::$unitConversions[$fromCategory][$toUOM]) / $toMultiplier; |
|||
} |
|||
|
|||
private static function getUOMDetails(string $uom) |
|||
{ |
|||
if (isset(self::$conversionUnits[$uom])) { |
|||
$unitCategory = self::$conversionUnits[$uom]['Group']; |
|||
|
|||
return [$uom, $unitCategory, 1.0]; |
|||
} |
|||
|
|||
// Check 1-character standard metric multiplier prefixes |
|||
$multiplierType = substr($uom, 0, 1); |
|||
$uom = substr($uom, 1); |
|||
if (isset(self::$conversionUnits[$uom], self::$conversionMultipliers[$multiplierType])) { |
|||
if (self::$conversionUnits[$uom]['AllowPrefix'] === false) { |
|||
throw new Exception('Prefix not allowed for UoM'); |
|||
} |
|||
$unitCategory = self::$conversionUnits[$uom]['Group']; |
|||
|
|||
return [$uom, $unitCategory, self::$conversionMultipliers[$multiplierType]['multiplier']]; |
|||
} |
|||
|
|||
$multiplierType .= substr($uom, 0, 1); |
|||
$uom = substr($uom, 1); |
|||
|
|||
// Check 2-character standard metric multiplier prefixes |
|||
if (isset(self::$conversionUnits[$uom], self::$conversionMultipliers[$multiplierType])) { |
|||
if (self::$conversionUnits[$uom]['AllowPrefix'] === false) { |
|||
throw new Exception('Prefix not allowed for UoM'); |
|||
} |
|||
$unitCategory = self::$conversionUnits[$uom]['Group']; |
|||
|
|||
return [$uom, $unitCategory, self::$conversionMultipliers[$multiplierType]['multiplier']]; |
|||
} |
|||
|
|||
// Check 2-character binary multiplier prefixes |
|||
if (isset(self::$conversionUnits[$uom], self::$binaryConversionMultipliers[$multiplierType])) { |
|||
if (self::$conversionUnits[$uom]['AllowPrefix'] === false) { |
|||
throw new Exception('Prefix not allowed for UoM'); |
|||
} |
|||
$unitCategory = self::$conversionUnits[$uom]['Group']; |
|||
if ($unitCategory !== 'Information') { |
|||
throw new Exception('Binary Prefix is only allowed for Information UoM'); |
|||
} |
|||
|
|||
return [$uom, $unitCategory, self::$binaryConversionMultipliers[$multiplierType]['multiplier']]; |
|||
} |
|||
|
|||
throw new Exception('UoM Not Found'); |
|||
} |
|||
|
|||
/** |
|||
* @param float|int $value |
|||
* |
|||
* @return float|int |
|||
*/ |
|||
protected static function convertTemperature(string $fromUOM, string $toUOM, $value) |
|||
{ |
|||
$fromUOM = self::resolveTemperatureSynonyms($fromUOM); |
|||
$toUOM = self::resolveTemperatureSynonyms($toUOM); |
|||
|
|||
if ($fromUOM === $toUOM) { |
|||
return $value; |
|||
} |
|||
|
|||
// Convert to Kelvin |
|||
switch ($fromUOM) { |
|||
case 'F': |
|||
$value = ($value - 32) / 1.8 + 273.15; |
|||
|
|||
break; |
|||
case 'C': |
|||
$value += 273.15; |
|||
|
|||
break; |
|||
case 'Rank': |
|||
$value /= 1.8; |
|||
|
|||
break; |
|||
case 'Reau': |
|||
$value = $value * 1.25 + 273.15; |
|||
|
|||
break; |
|||
} |
|||
|
|||
// Convert from Kelvin |
|||
switch ($toUOM) { |
|||
case 'F': |
|||
$value = ($value - 273.15) * 1.8 + 32.00; |
|||
|
|||
break; |
|||
case 'C': |
|||
$value -= 273.15; |
|||
|
|||
break; |
|||
case 'Rank': |
|||
$value *= 1.8; |
|||
|
|||
break; |
|||
case 'Reau': |
|||
$value = ($value - 273.15) * 0.80000; |
|||
|
|||
break; |
|||
} |
|||
|
|||
return $value; |
|||
} |
|||
|
|||
private static function resolveTemperatureSynonyms(string $uom) |
|||
{ |
|||
switch ($uom) { |
|||
case 'fah': |
|||
return 'F'; |
|||
case 'cel': |
|||
return 'C'; |
|||
case 'kel': |
|||
return 'K'; |
|||
} |
|||
|
|||
return $uom; |
|||
} |
|||
} |
|||
@ -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::fraction($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\DateParts::year($purchased); |
|||
$yearFrac = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis); |
|||
if (is_string($yearFrac)) { |
|||
return $yearFrac; |
|||
} |
|||
|
|||
if ( |
|||
($basis == FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL) && |
|||
($yearFrac < 1) && (Functions::scalar(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,53 @@ |
|||
<?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); |
|||
|
|||
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,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,19 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial; |
|||
|
|||
class Constants |
|||
{ |
|||
public const BASIS_DAYS_PER_YEAR_NASD = 0; |
|||
public const BASIS_DAYS_PER_YEAR_ACTUAL = 1; |
|||
public const BASIS_DAYS_PER_YEAR_360 = 2; |
|||
public const BASIS_DAYS_PER_YEAR_365 = 3; |
|||
public const BASIS_DAYS_PER_YEAR_360_EUROPEAN = 4; |
|||
|
|||
public const FREQUENCY_ANNUAL = 1; |
|||
public const FREQUENCY_SEMI_ANNUAL = 2; |
|||
public const FREQUENCY_QUARTERLY = 4; |
|||
|
|||
public const PAYMENT_END_OF_PERIOD = 0; |
|||
public const PAYMENT_BEGINNING_OF_PERIOD = 1; |
|||
} |
|||
@ -0,0 +1,415 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial; |
|||
|
|||
use DateTime; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Shared\Date; |
|||
|
|||
class Coupons |
|||
{ |
|||
private const PERIOD_DATE_PREVIOUS = false; |
|||
private const PERIOD_DATE_NEXT = true; |
|||
|
|||
/** |
|||
* COUPDAYBS. |
|||
* |
|||
* Returns the number of days from the beginning of the coupon period to the settlement date. |
|||
* |
|||
* Excel Function: |
|||
* COUPDAYBS(settlement,maturity,frequency[,basis]) |
|||
* |
|||
* @param mixed $settlement The security's settlement date. |
|||
* The security settlement date is the date after the issue |
|||
* date when the security is traded to the buyer. |
|||
* @param mixed $maturity The security's maturity date. |
|||
* The maturity date is the date when the security expires. |
|||
* @param mixed $frequency The number of coupon payments per year (int). |
|||
* Valid frequency values are: |
|||
* 1 Annual |
|||
* 2 Semi-Annual |
|||
* 4 Quarterly |
|||
* @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 |
|||
*/ |
|||
public static function COUPDAYBS( |
|||
$settlement, |
|||
$maturity, |
|||
$frequency, |
|||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
) { |
|||
$settlement = Functions::flattenSingleValue($settlement); |
|||
$maturity = Functions::flattenSingleValue($maturity); |
|||
$frequency = Functions::flattenSingleValue($frequency); |
|||
$basis = ($basis === null) |
|||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
: Functions::flattenSingleValue($basis); |
|||
|
|||
try { |
|||
$settlement = FinancialValidations::validateSettlementDate($settlement); |
|||
$maturity = FinancialValidations::validateMaturityDate($maturity); |
|||
self::validateCouponPeriod($settlement, $maturity); |
|||
$frequency = FinancialValidations::validateFrequency($frequency); |
|||
$basis = FinancialValidations::validateBasis($basis); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$daysPerYear = Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis); |
|||
if (is_string($daysPerYear)) { |
|||
return Functions::VALUE(); |
|||
} |
|||
$prev = self::couponFirstPeriodDate($settlement, $maturity, $frequency, self::PERIOD_DATE_PREVIOUS); |
|||
|
|||
if ($basis === FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL) { |
|||
return abs((float) DateTimeExcel\Days::between($prev, $settlement)); |
|||
} |
|||
|
|||
return (float) DateTimeExcel\YearFrac::fraction($prev, $settlement, $basis) * $daysPerYear; |
|||
} |
|||
|
|||
/** |
|||
* COUPDAYS. |
|||
* |
|||
* Returns the number of days in the coupon period that contains the settlement date. |
|||
* |
|||
* Excel Function: |
|||
* COUPDAYS(settlement,maturity,frequency[,basis]) |
|||
* |
|||
* @param mixed $settlement The security's settlement date. |
|||
* The security settlement date is the date after the issue |
|||
* date when the security is traded to the buyer. |
|||
* @param mixed $maturity The security's maturity date. |
|||
* The maturity date is the date when the security expires. |
|||
* @param mixed $frequency The number of coupon payments per year. |
|||
* Valid frequency values are: |
|||
* 1 Annual |
|||
* 2 Semi-Annual |
|||
* 4 Quarterly |
|||
* @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 |
|||
*/ |
|||
public static function COUPDAYS( |
|||
$settlement, |
|||
$maturity, |
|||
$frequency, |
|||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
) { |
|||
$settlement = Functions::flattenSingleValue($settlement); |
|||
$maturity = Functions::flattenSingleValue($maturity); |
|||
$frequency = Functions::flattenSingleValue($frequency); |
|||
$basis = ($basis === null) |
|||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
: Functions::flattenSingleValue($basis); |
|||
|
|||
try { |
|||
$settlement = FinancialValidations::validateSettlementDate($settlement); |
|||
$maturity = FinancialValidations::validateMaturityDate($maturity); |
|||
self::validateCouponPeriod($settlement, $maturity); |
|||
$frequency = FinancialValidations::validateFrequency($frequency); |
|||
$basis = FinancialValidations::validateBasis($basis); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
switch ($basis) { |
|||
case FinancialConstants::BASIS_DAYS_PER_YEAR_365: |
|||
// Actual/365 |
|||
return 365 / $frequency; |
|||
case FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL: |
|||
// Actual/actual |
|||
if ($frequency == FinancialConstants::FREQUENCY_ANNUAL) { |
|||
$daysPerYear = (int) Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis); |
|||
|
|||
return $daysPerYear / $frequency; |
|||
} |
|||
$prev = self::couponFirstPeriodDate($settlement, $maturity, $frequency, self::PERIOD_DATE_PREVIOUS); |
|||
$next = self::couponFirstPeriodDate($settlement, $maturity, $frequency, self::PERIOD_DATE_NEXT); |
|||
|
|||
return $next - $prev; |
|||
default: |
|||
// US (NASD) 30/360, Actual/360 or European 30/360 |
|||
return 360 / $frequency; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* COUPDAYSNC. |
|||
* |
|||
* Returns the number of days from the settlement date to the next coupon date. |
|||
* |
|||
* Excel Function: |
|||
* COUPDAYSNC(settlement,maturity,frequency[,basis]) |
|||
* |
|||
* @param mixed $settlement The security's settlement date. |
|||
* The security settlement date is the date after the issue |
|||
* date when the security is traded to the buyer. |
|||
* @param mixed $maturity The security's maturity date. |
|||
* The maturity date is the date when the security expires. |
|||
* @param mixed $frequency The number of coupon payments per year. |
|||
* Valid frequency values are: |
|||
* 1 Annual |
|||
* 2 Semi-Annual |
|||
* 4 Quarterly |
|||
* @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 |
|||
*/ |
|||
public static function COUPDAYSNC( |
|||
$settlement, |
|||
$maturity, |
|||
$frequency, |
|||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
) { |
|||
$settlement = Functions::flattenSingleValue($settlement); |
|||
$maturity = Functions::flattenSingleValue($maturity); |
|||
$frequency = Functions::flattenSingleValue($frequency); |
|||
$basis = ($basis === null) |
|||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
: Functions::flattenSingleValue($basis); |
|||
|
|||
try { |
|||
$settlement = FinancialValidations::validateSettlementDate($settlement); |
|||
$maturity = FinancialValidations::validateMaturityDate($maturity); |
|||
self::validateCouponPeriod($settlement, $maturity); |
|||
$frequency = FinancialValidations::validateFrequency($frequency); |
|||
$basis = FinancialValidations::validateBasis($basis); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$daysPerYear = Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis); |
|||
$next = self::couponFirstPeriodDate($settlement, $maturity, $frequency, self::PERIOD_DATE_NEXT); |
|||
|
|||
if ($basis === FinancialConstants::BASIS_DAYS_PER_YEAR_NASD) { |
|||
$settlementDate = Date::excelToDateTimeObject($settlement); |
|||
$settlementEoM = Helpers::isLastDayOfMonth($settlementDate); |
|||
if ($settlementEoM) { |
|||
++$settlement; |
|||
} |
|||
} |
|||
|
|||
return (float) DateTimeExcel\YearFrac::fraction($settlement, $next, $basis) * $daysPerYear; |
|||
} |
|||
|
|||
/** |
|||
* COUPNCD. |
|||
* |
|||
* Returns the next coupon date after the settlement date. |
|||
* |
|||
* Excel Function: |
|||
* COUPNCD(settlement,maturity,frequency[,basis]) |
|||
* |
|||
* @param mixed $settlement The security's settlement date. |
|||
* The security settlement date is the date after the issue |
|||
* date when the security is traded to the buyer. |
|||
* @param mixed $maturity The security's maturity date. |
|||
* The maturity date is the date when the security expires. |
|||
* @param mixed $frequency The number of coupon payments per year. |
|||
* Valid frequency values are: |
|||
* 1 Annual |
|||
* 2 Semi-Annual |
|||
* 4 Quarterly |
|||
* @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 mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
|||
* depending on the value of the ReturnDateType flag |
|||
*/ |
|||
public static function COUPNCD( |
|||
$settlement, |
|||
$maturity, |
|||
$frequency, |
|||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
) { |
|||
$settlement = Functions::flattenSingleValue($settlement); |
|||
$maturity = Functions::flattenSingleValue($maturity); |
|||
$frequency = Functions::flattenSingleValue($frequency); |
|||
$basis = ($basis === null) |
|||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
: Functions::flattenSingleValue($basis); |
|||
|
|||
try { |
|||
$settlement = FinancialValidations::validateSettlementDate($settlement); |
|||
$maturity = FinancialValidations::validateMaturityDate($maturity); |
|||
self::validateCouponPeriod($settlement, $maturity); |
|||
$frequency = FinancialValidations::validateFrequency($frequency); |
|||
$basis = FinancialValidations::validateBasis($basis); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return self::couponFirstPeriodDate($settlement, $maturity, $frequency, self::PERIOD_DATE_NEXT); |
|||
} |
|||
|
|||
/** |
|||
* COUPNUM. |
|||
* |
|||
* Returns the number of coupons payable between the settlement date and maturity date, |
|||
* rounded up to the nearest whole coupon. |
|||
* |
|||
* Excel Function: |
|||
* COUPNUM(settlement,maturity,frequency[,basis]) |
|||
* |
|||
* @param mixed $settlement The security's settlement date. |
|||
* The security settlement date is the date after the issue |
|||
* date when the security is traded to the buyer. |
|||
* @param mixed $maturity The security's maturity date. |
|||
* The maturity date is the date when the security expires. |
|||
* @param mixed $frequency The number of coupon payments per year. |
|||
* Valid frequency values are: |
|||
* 1 Annual |
|||
* 2 Semi-Annual |
|||
* 4 Quarterly |
|||
* @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 int|string |
|||
*/ |
|||
public static function COUPNUM( |
|||
$settlement, |
|||
$maturity, |
|||
$frequency, |
|||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
) { |
|||
$settlement = Functions::flattenSingleValue($settlement); |
|||
$maturity = Functions::flattenSingleValue($maturity); |
|||
$frequency = Functions::flattenSingleValue($frequency); |
|||
$basis = ($basis === null) |
|||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
: Functions::flattenSingleValue($basis); |
|||
|
|||
try { |
|||
$settlement = FinancialValidations::validateSettlementDate($settlement); |
|||
$maturity = FinancialValidations::validateMaturityDate($maturity); |
|||
self::validateCouponPeriod($settlement, $maturity); |
|||
$frequency = FinancialValidations::validateFrequency($frequency); |
|||
$basis = FinancialValidations::validateBasis($basis); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$yearsBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction( |
|||
$settlement, |
|||
$maturity, |
|||
FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
); |
|||
|
|||
return (int) ceil((float) $yearsBetweenSettlementAndMaturity * $frequency); |
|||
} |
|||
|
|||
/** |
|||
* COUPPCD. |
|||
* |
|||
* Returns the previous coupon date before the settlement date. |
|||
* |
|||
* Excel Function: |
|||
* COUPPCD(settlement,maturity,frequency[,basis]) |
|||
* |
|||
* @param mixed $settlement The security's settlement date. |
|||
* The security settlement date is the date after the issue |
|||
* date when the security is traded to the buyer. |
|||
* @param mixed $maturity The security's maturity date. |
|||
* The maturity date is the date when the security expires. |
|||
* @param mixed $frequency The number of coupon payments per year. |
|||
* Valid frequency values are: |
|||
* 1 Annual |
|||
* 2 Semi-Annual |
|||
* 4 Quarterly |
|||
* @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 mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
|||
* depending on the value of the ReturnDateType flag |
|||
*/ |
|||
public static function COUPPCD( |
|||
$settlement, |
|||
$maturity, |
|||
$frequency, |
|||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
) { |
|||
$settlement = Functions::flattenSingleValue($settlement); |
|||
$maturity = Functions::flattenSingleValue($maturity); |
|||
$frequency = Functions::flattenSingleValue($frequency); |
|||
$basis = ($basis === null) |
|||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
: Functions::flattenSingleValue($basis); |
|||
|
|||
try { |
|||
$settlement = FinancialValidations::validateSettlementDate($settlement); |
|||
$maturity = FinancialValidations::validateMaturityDate($maturity); |
|||
self::validateCouponPeriod($settlement, $maturity); |
|||
$frequency = FinancialValidations::validateFrequency($frequency); |
|||
$basis = FinancialValidations::validateBasis($basis); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return self::couponFirstPeriodDate($settlement, $maturity, $frequency, self::PERIOD_DATE_PREVIOUS); |
|||
} |
|||
|
|||
private static function monthsDiff(DateTime $result, int $months, string $plusOrMinus, int $day, bool $lastDayFlag): void |
|||
{ |
|||
$result->setDate((int) $result->format('Y'), (int) $result->format('m'), 1); |
|||
$result->modify("$plusOrMinus $months months"); |
|||
$daysInMonth = (int) $result->format('t'); |
|||
$result->setDate((int) $result->format('Y'), (int) $result->format('m'), $lastDayFlag ? $daysInMonth : min($day, $daysInMonth)); |
|||
} |
|||
|
|||
private static function couponFirstPeriodDate(float $settlement, float $maturity, int $frequency, bool $next): float |
|||
{ |
|||
$months = 12 / $frequency; |
|||
|
|||
$result = Date::excelToDateTimeObject($maturity); |
|||
$day = (int) $result->format('d'); |
|||
$lastDayFlag = Helpers::isLastDayOfMonth($result); |
|||
|
|||
while ($settlement < Date::PHPToExcel($result)) { |
|||
self::monthsDiff($result, $months, '-', $day, $lastDayFlag); |
|||
} |
|||
if ($next === true) { |
|||
self::monthsDiff($result, $months, '+', $day, $lastDayFlag); |
|||
} |
|||
|
|||
return (float) Date::PHPToExcel($result); |
|||
} |
|||
|
|||
private static function validateCouponPeriod(float $settlement, float $maturity): void |
|||
{ |
|||
if ($settlement >= $maturity) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,266 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Depreciation |
|||
{ |
|||
/** |
|||
* DB. |
|||
* |
|||
* Returns the depreciation of an asset for a specified period using the |
|||
* fixed-declining balance method. |
|||
* This form of depreciation is used if you want to get a higher depreciation value |
|||
* at the beginning of the depreciation (as opposed to linear depreciation). The |
|||
* depreciation value is reduced with every depreciation period by the depreciation |
|||
* already deducted from the initial cost. |
|||
* |
|||
* Excel Function: |
|||
* DB(cost,salvage,life,period[,month]) |
|||
* |
|||
* @param mixed $cost Initial cost of the asset |
|||
* @param mixed $salvage Value at the end of the depreciation. |
|||
* (Sometimes called the salvage value of the asset) |
|||
* @param mixed $life Number of periods over which the asset is depreciated. |
|||
* (Sometimes called the useful life of the asset) |
|||
* @param mixed $period The period for which you want to calculate the |
|||
* depreciation. Period must use the same units as life. |
|||
* @param mixed $month Number of months in the first year. If month is omitted, |
|||
* it defaults to 12. |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function DB($cost, $salvage, $life, $period, $month = 12) |
|||
{ |
|||
$cost = Functions::flattenSingleValue($cost); |
|||
$salvage = Functions::flattenSingleValue($salvage); |
|||
$life = Functions::flattenSingleValue($life); |
|||
$period = Functions::flattenSingleValue($period); |
|||
$month = Functions::flattenSingleValue($month); |
|||
|
|||
try { |
|||
$cost = self::validateCost($cost); |
|||
$salvage = self::validateSalvage($salvage); |
|||
$life = self::validateLife($life); |
|||
$period = self::validatePeriod($period); |
|||
$month = self::validateMonth($month); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($cost === 0.0) { |
|||
return 0.0; |
|||
} |
|||
|
|||
// Set Fixed Depreciation Rate |
|||
$fixedDepreciationRate = 1 - ($salvage / $cost) ** (1 / $life); |
|||
$fixedDepreciationRate = round($fixedDepreciationRate, 3); |
|||
|
|||
// Loop through each period calculating the depreciation |
|||
// TODO Handle period value between 0 and 1 (e.g. 0.5) |
|||
$previousDepreciation = 0; |
|||
$depreciation = 0; |
|||
for ($per = 1; $per <= $period; ++$per) { |
|||
if ($per == 1) { |
|||
$depreciation = $cost * $fixedDepreciationRate * $month / 12; |
|||
} elseif ($per == ($life + 1)) { |
|||
$depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate * (12 - $month) / 12; |
|||
} else { |
|||
$depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate; |
|||
} |
|||
$previousDepreciation += $depreciation; |
|||
} |
|||
|
|||
return $depreciation; |
|||
} |
|||
|
|||
/** |
|||
* DDB. |
|||
* |
|||
* Returns the depreciation of an asset for a specified period using the |
|||
* double-declining balance method or some other method you specify. |
|||
* |
|||
* Excel Function: |
|||
* DDB(cost,salvage,life,period[,factor]) |
|||
* |
|||
* @param mixed $cost Initial cost of the asset |
|||
* @param mixed $salvage Value at the end of the depreciation. |
|||
* (Sometimes called the salvage value of the asset) |
|||
* @param mixed $life Number of periods over which the asset is depreciated. |
|||
* (Sometimes called the useful life of the asset) |
|||
* @param mixed $period The period for which you want to calculate the |
|||
* depreciation. Period must use the same units as life. |
|||
* @param mixed $factor The rate at which the balance declines. |
|||
* If factor is omitted, it is assumed to be 2 (the |
|||
* double-declining balance method). |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function DDB($cost, $salvage, $life, $period, $factor = 2.0) |
|||
{ |
|||
$cost = Functions::flattenSingleValue($cost); |
|||
$salvage = Functions::flattenSingleValue($salvage); |
|||
$life = Functions::flattenSingleValue($life); |
|||
$period = Functions::flattenSingleValue($period); |
|||
$factor = Functions::flattenSingleValue($factor); |
|||
|
|||
try { |
|||
$cost = self::validateCost($cost); |
|||
$salvage = self::validateSalvage($salvage); |
|||
$life = self::validateLife($life); |
|||
$period = self::validatePeriod($period); |
|||
$factor = self::validateFactor($factor); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($period > $life) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
// Loop through each period calculating the depreciation |
|||
// TODO Handling for fractional $period values |
|||
$previousDepreciation = 0; |
|||
$depreciation = 0; |
|||
for ($per = 1; $per <= $period; ++$per) { |
|||
$depreciation = min( |
|||
($cost - $previousDepreciation) * ($factor / $life), |
|||
($cost - $salvage - $previousDepreciation) |
|||
); |
|||
$previousDepreciation += $depreciation; |
|||
} |
|||
|
|||
return $depreciation; |
|||
} |
|||
|
|||
/** |
|||
* SLN. |
|||
* |
|||
* Returns the straight-line depreciation of an asset for one period |
|||
* |
|||
* @param mixed $cost Initial cost of the asset |
|||
* @param mixed $salvage Value at the end of the depreciation |
|||
* @param mixed $life Number of periods over which the asset is depreciated |
|||
* |
|||
* @return float|string Result, or a string containing an error |
|||
*/ |
|||
public static function SLN($cost, $salvage, $life) |
|||
{ |
|||
$cost = Functions::flattenSingleValue($cost); |
|||
$salvage = Functions::flattenSingleValue($salvage); |
|||
$life = Functions::flattenSingleValue($life); |
|||
|
|||
try { |
|||
$cost = self::validateCost($cost, true); |
|||
$salvage = self::validateSalvage($salvage, true); |
|||
$life = self::validateLife($life, true); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($life === 0.0) { |
|||
return Functions::DIV0(); |
|||
} |
|||
|
|||
return ($cost - $salvage) / $life; |
|||
} |
|||
|
|||
/** |
|||
* SYD. |
|||
* |
|||
* Returns the sum-of-years' digits depreciation of an asset for a specified period. |
|||
* |
|||
* @param mixed $cost Initial cost of the asset |
|||
* @param mixed $salvage Value at the end of the depreciation |
|||
* @param mixed $life Number of periods over which the asset is depreciated |
|||
* @param mixed $period Period |
|||
* |
|||
* @return float|string Result, or a string containing an error |
|||
*/ |
|||
public static function SYD($cost, $salvage, $life, $period) |
|||
{ |
|||
$cost = Functions::flattenSingleValue($cost); |
|||
$salvage = Functions::flattenSingleValue($salvage); |
|||
$life = Functions::flattenSingleValue($life); |
|||
$period = Functions::flattenSingleValue($period); |
|||
|
|||
try { |
|||
$cost = self::validateCost($cost, true); |
|||
$salvage = self::validateSalvage($salvage); |
|||
$life = self::validateLife($life); |
|||
$period = self::validatePeriod($period); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($period > $life) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$syd = (($cost - $salvage) * ($life - $period + 1) * 2) / ($life * ($life + 1)); |
|||
|
|||
return $syd; |
|||
} |
|||
|
|||
private static function validateCost($cost, bool $negativeValueAllowed = false): float |
|||
{ |
|||
$cost = FinancialValidations::validateFloat($cost); |
|||
if ($cost < 0.0 && $negativeValueAllowed === false) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return $cost; |
|||
} |
|||
|
|||
private static function validateSalvage($salvage, bool $negativeValueAllowed = false): float |
|||
{ |
|||
$salvage = FinancialValidations::validateFloat($salvage); |
|||
if ($salvage < 0.0 && $negativeValueAllowed === false) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return $salvage; |
|||
} |
|||
|
|||
private static function validateLife($life, bool $negativeValueAllowed = false): float |
|||
{ |
|||
$life = FinancialValidations::validateFloat($life); |
|||
if ($life < 0.0 && $negativeValueAllowed === false) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return $life; |
|||
} |
|||
|
|||
private static function validatePeriod($period, bool $negativeValueAllowed = false): float |
|||
{ |
|||
$period = FinancialValidations::validateFloat($period); |
|||
if ($period <= 0.0 && $negativeValueAllowed === false) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return $period; |
|||
} |
|||
|
|||
private static function validateMonth($month): int |
|||
{ |
|||
$month = FinancialValidations::validateInt($month); |
|||
if ($month < 1) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return $month; |
|||
} |
|||
|
|||
private static function validateFactor($factor): float |
|||
{ |
|||
$factor = FinancialValidations::validateFloat($factor); |
|||
if ($factor <= 0.0) { |
|||
throw new Exception(Functions::NAN()); |
|||
} |
|||
|
|||
return $factor; |
|||
} |
|||
} |
|||
@ -0,0 +1,151 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\Securities; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\YearFrac; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class AccruedInterest |
|||
{ |
|||
public const ACCRINT_CALCMODE_ISSUE_TO_SETTLEMENT = true; |
|||
|
|||
public const ACCRINT_CALCMODE_FIRST_INTEREST_TO_SETTLEMENT = false; |
|||
|
|||
/** |
|||
* ACCRINT. |
|||
* |
|||
* Returns the accrued interest for a security that pays periodic interest. |
|||
* |
|||
* Excel Function: |
|||
* ACCRINT(issue,firstinterest,settlement,rate,par,frequency[,basis][,calc_method]) |
|||
* |
|||
* @param mixed $issue the security's issue date |
|||
* @param mixed $firstInterest the security's first interest date |
|||
* @param mixed $settlement The security's settlement date. |
|||
* The security settlement date is the date after the issue date |
|||
* when the security is traded to the buyer. |
|||
* @param mixed $rate The security's annual coupon rate |
|||
* @param mixed $parValue The security's par value. |
|||
* If you omit par, ACCRINT uses $1,000. |
|||
* @param mixed $frequency The number of coupon payments per year. |
|||
* Valid frequency values are: |
|||
* 1 Annual |
|||
* 2 Semi-Annual |
|||
* 4 Quarterly |
|||
* @param mixed $basis The type of day count to use. |
|||
* 0 or omitted US (NASD) 30/360 |
|||
* 1 Actual/actual |
|||
* 2 Actual/360 |
|||
* 3 Actual/365 |
|||
* 4 European 30/360 |
|||
* @param mixed $calcMethod |
|||
* |
|||
* @return float|string Result, or a string containing an error |
|||
*/ |
|||
public static function periodic( |
|||
$issue, |
|||
$firstInterest, |
|||
$settlement, |
|||
$rate, |
|||
$parValue = 1000, |
|||
$frequency = FinancialConstants::FREQUENCY_ANNUAL, |
|||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD, |
|||
$calcMethod = self::ACCRINT_CALCMODE_ISSUE_TO_SETTLEMENT |
|||
) { |
|||
$issue = Functions::flattenSingleValue($issue); |
|||
$firstInterest = Functions::flattenSingleValue($firstInterest); |
|||
$settlement = Functions::flattenSingleValue($settlement); |
|||
$rate = Functions::flattenSingleValue($rate); |
|||
$parValue = ($parValue === null) ? 1000 : Functions::flattenSingleValue($parValue); |
|||
$frequency = ($frequency === null) |
|||
? FinancialConstants::FREQUENCY_ANNUAL |
|||
: Functions::flattenSingleValue($frequency); |
|||
$basis = ($basis === null) |
|||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
: Functions::flattenSingleValue($basis); |
|||
|
|||
try { |
|||
$issue = SecurityValidations::validateIssueDate($issue); |
|||
$settlement = SecurityValidations::validateSettlementDate($settlement); |
|||
SecurityValidations::validateSecurityPeriod($issue, $settlement); |
|||
$rate = SecurityValidations::validateRate($rate); |
|||
$parValue = SecurityValidations::validateParValue($parValue); |
|||
$frequency = SecurityValidations::validateFrequency($frequency); |
|||
$basis = SecurityValidations::validateBasis($basis); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis)); |
|||
if (!is_numeric($daysBetweenIssueAndSettlement)) { |
|||
// return date error |
|||
return $daysBetweenIssueAndSettlement; |
|||
} |
|||
$daysBetweenFirstInterestAndSettlement = Functions::scalar(YearFrac::fraction($firstInterest, $settlement, $basis)); |
|||
if (!is_numeric($daysBetweenFirstInterestAndSettlement)) { |
|||
// return date error |
|||
return $daysBetweenFirstInterestAndSettlement; |
|||
} |
|||
|
|||
return $parValue * $rate * $daysBetweenIssueAndSettlement; |
|||
} |
|||
|
|||
/** |
|||
* ACCRINTM. |
|||
* |
|||
* Returns the accrued interest for a security that pays interest at maturity. |
|||
* |
|||
* Excel Function: |
|||
* ACCRINTM(issue,settlement,rate[,par[,basis]]) |
|||
* |
|||
* @param mixed $issue The security's issue date |
|||
* @param mixed $settlement The security's settlement (or maturity) date |
|||
* @param mixed $rate The security's annual coupon rate |
|||
* @param mixed $parValue The security's par value. |
|||
* If you omit parValue, ACCRINT uses $1,000. |
|||
* @param mixed $basis The type of day count to use. |
|||
* 0 or omitted US (NASD) 30/360 |
|||
* 1 Actual/actual |
|||
* 2 Actual/360 |
|||
* 3 Actual/365 |
|||
* 4 European 30/360 |
|||
* |
|||
* @return float|string Result, or a string containing an error |
|||
*/ |
|||
public static function atMaturity( |
|||
$issue, |
|||
$settlement, |
|||
$rate, |
|||
$parValue = 1000, |
|||
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
) { |
|||
$issue = Functions::flattenSingleValue($issue); |
|||
$settlement = Functions::flattenSingleValue($settlement); |
|||
$rate = Functions::flattenSingleValue($rate); |
|||
$parValue = ($parValue === null) ? 1000 : Functions::flattenSingleValue($parValue); |
|||
$basis = ($basis === null) |
|||
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
|||
: Functions::flattenSingleValue($basis); |
|||
|
|||
try { |
|||
$issue = SecurityValidations::validateIssueDate($issue); |
|||
$settlement = SecurityValidations::validateSettlementDate($settlement); |
|||
SecurityValidations::validateSecurityPeriod($issue, $settlement); |
|||
$rate = SecurityValidations::validateRate($rate); |
|||
$parValue = SecurityValidations::validateParValue($parValue); |
|||
$basis = SecurityValidations::validateBasis($basis); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis)); |
|||
if (!is_numeric($daysBetweenIssueAndSettlement)) { |
|||
// return date error |
|||
return $daysBetweenIssueAndSettlement; |
|||
} |
|||
|
|||
return $parValue * $rate * $daysBetweenIssueAndSettlement; |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Logical; |
|||
|
|||
class Boolean |
|||
{ |
|||
/** |
|||
* TRUE. |
|||
* |
|||
* Returns the boolean TRUE. |
|||
* |
|||
* Excel Function: |
|||
* =TRUE() |
|||
* |
|||
* @return bool True |
|||
*/ |
|||
public static function true(): bool |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
/** |
|||
* FALSE. |
|||
* |
|||
* Returns the boolean FALSE. |
|||
* |
|||
* Excel Function: |
|||
* =FALSE() |
|||
* |
|||
* @return bool False |
|||
*/ |
|||
public static function false(): bool |
|||
{ |
|||
return false; |
|||
} |
|||
} |
|||
@ -0,0 +1,205 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Logical; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Conditional |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* STATEMENT_IF. |
|||
* |
|||
* Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE. |
|||
* |
|||
* Excel Function: |
|||
* =IF(condition[,returnIfTrue[,returnIfFalse]]) |
|||
* |
|||
* Condition is any value or expression that can be evaluated to TRUE or FALSE. |
|||
* For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100, |
|||
* the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE. |
|||
* This argument can use any comparison calculation operator. |
|||
* ReturnIfTrue is the value that is returned if condition evaluates to TRUE. |
|||
* For example, if this argument is the text string "Within budget" and |
|||
* the condition argument evaluates to TRUE, then the IF function returns the text "Within budget" |
|||
* If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). |
|||
* To display the word TRUE, use the logical value TRUE for this argument. |
|||
* ReturnIfTrue can be another formula. |
|||
* ReturnIfFalse is the value that is returned if condition evaluates to FALSE. |
|||
* For example, if this argument is the text string "Over budget" and the condition argument evaluates |
|||
* to FALSE, then the IF function returns the text "Over budget". |
|||
* If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned. |
|||
* If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned. |
|||
* ReturnIfFalse can be another formula. |
|||
* |
|||
* @param mixed $condition Condition to evaluate |
|||
* @param mixed $returnIfTrue Value to return when condition is true |
|||
* Note that this can be an array value |
|||
* @param mixed $returnIfFalse Optional value to return when condition is false |
|||
* Note that this can be an array value |
|||
* |
|||
* @return mixed The value of returnIfTrue or returnIfFalse determined by condition |
|||
*/ |
|||
public static function statementIf($condition = true, $returnIfTrue = 0, $returnIfFalse = false) |
|||
{ |
|||
if (Functions::isError($condition)) { |
|||
return $condition; |
|||
} |
|||
|
|||
$condition = ($condition === null) ? true : (bool) Functions::flattenSingleValue($condition); |
|||
$returnIfTrue = $returnIfTrue ?? 0; |
|||
$returnIfFalse = $returnIfFalse ?? false; |
|||
|
|||
return ($condition) ? $returnIfTrue : $returnIfFalse; |
|||
} |
|||
|
|||
/** |
|||
* STATEMENT_SWITCH. |
|||
* |
|||
* Returns corresponding with first match (any data type such as a string, numeric, date, etc). |
|||
* |
|||
* Excel Function: |
|||
* =SWITCH (expression, value1, result1, value2, result2, ... value_n, result_n [, default]) |
|||
* |
|||
* Expression |
|||
* The expression to compare to a list of values. |
|||
* value1, value2, ... value_n |
|||
* A list of values that are compared to expression. |
|||
* The SWITCH function is looking for the first value that matches the expression. |
|||
* result1, result2, ... result_n |
|||
* A list of results. The SWITCH function returns the corresponding result when a value |
|||
* matches expression. |
|||
* Note that these can be array values to be returned |
|||
* default |
|||
* Optional. It is the default to return if expression does not match any of the values |
|||
* (value1, value2, ... value_n). |
|||
* Note that this can be an array value to be returned |
|||
* |
|||
* @param mixed $arguments Statement arguments |
|||
* |
|||
* @return mixed The value of matched expression |
|||
*/ |
|||
public static function statementSwitch(...$arguments) |
|||
{ |
|||
$result = Functions::VALUE(); |
|||
|
|||
if (count($arguments) > 0) { |
|||
$targetValue = Functions::flattenSingleValue($arguments[0]); |
|||
$argc = count($arguments) - 1; |
|||
$switchCount = floor($argc / 2); |
|||
$hasDefaultClause = $argc % 2 !== 0; |
|||
$defaultClause = $argc % 2 === 0 ? null : $arguments[$argc]; |
|||
|
|||
$switchSatisfied = false; |
|||
if ($switchCount > 0) { |
|||
for ($index = 0; $index < $switchCount; ++$index) { |
|||
if ($targetValue == $arguments[$index * 2 + 1]) { |
|||
$result = $arguments[$index * 2 + 2]; |
|||
$switchSatisfied = true; |
|||
|
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
if ($switchSatisfied !== true) { |
|||
$result = $hasDefaultClause ? $defaultClause : Functions::NA(); |
|||
} |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* IFERROR. |
|||
* |
|||
* Excel Function: |
|||
* =IFERROR(testValue,errorpart) |
|||
* |
|||
* @param mixed $testValue Value to check, is also the value returned when no error |
|||
* Or can be an array of values |
|||
* @param mixed $errorpart Value to return when testValue is an error condition |
|||
* Note that this can be an array value to be returned |
|||
* |
|||
* @return mixed The value of errorpart or testValue determined by error condition |
|||
* If an array of values is passed as the $testValue argument, then the returned result will also be |
|||
* an array with the same dimensions |
|||
*/ |
|||
public static function IFERROR($testValue = '', $errorpart = '') |
|||
{ |
|||
if (is_array($testValue)) { |
|||
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $testValue, $errorpart); |
|||
} |
|||
|
|||
$errorpart = $errorpart ?? ''; |
|||
|
|||
return self::statementIf(Functions::isError($testValue), $errorpart, $testValue); |
|||
} |
|||
|
|||
/** |
|||
* IFNA. |
|||
* |
|||
* Excel Function: |
|||
* =IFNA(testValue,napart) |
|||
* |
|||
* @param mixed $testValue Value to check, is also the value returned when not an NA |
|||
* Or can be an array of values |
|||
* @param mixed $napart Value to return when testValue is an NA condition |
|||
* Note that this can be an array value to be returned |
|||
* |
|||
* @return mixed The value of errorpart or testValue determined by error condition |
|||
* If an array of values is passed as the $testValue argument, then the returned result will also be |
|||
* an array with the same dimensions |
|||
*/ |
|||
public static function IFNA($testValue = '', $napart = '') |
|||
{ |
|||
if (is_array($testValue)) { |
|||
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $testValue, $napart); |
|||
} |
|||
|
|||
$napart = $napart ?? ''; |
|||
|
|||
return self::statementIf(Functions::isNa($testValue), $napart, $testValue); |
|||
} |
|||
|
|||
/** |
|||
* IFS. |
|||
* |
|||
* Excel Function: |
|||
* =IFS(testValue1;returnIfTrue1;testValue2;returnIfTrue2;...;testValue_n;returnIfTrue_n) |
|||
* |
|||
* testValue1 ... testValue_n |
|||
* Conditions to Evaluate |
|||
* returnIfTrue1 ... returnIfTrue_n |
|||
* Value returned if corresponding testValue (nth) was true |
|||
* |
|||
* @param mixed ...$arguments Statement arguments |
|||
* Note that this can be an array value to be returned |
|||
* |
|||
* @return mixed|string The value of returnIfTrue_n, if testValue_n was true. #N/A if none of testValues was true |
|||
*/ |
|||
public static function IFS(...$arguments) |
|||
{ |
|||
$argumentCount = count($arguments); |
|||
|
|||
if ($argumentCount % 2 != 0) { |
|||
return Functions::NA(); |
|||
} |
|||
// We use instance of Exception as a falseValue in order to prevent string collision with value in cell |
|||
$falseValueException = new Exception(); |
|||
for ($i = 0; $i < $argumentCount; $i += 2) { |
|||
$testValue = ($arguments[$i] === null) ? '' : Functions::flattenSingleValue($arguments[$i]); |
|||
$returnIfTrue = ($arguments[$i + 1] === null) ? '' : $arguments[$i + 1]; |
|||
$result = self::statementIf($testValue, $returnIfTrue, $falseValueException); |
|||
|
|||
if ($result !== $falseValueException) { |
|||
return $result; |
|||
} |
|||
} |
|||
|
|||
return Functions::NA(); |
|||
} |
|||
} |
|||
@ -0,0 +1,98 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
|||
|
|||
class Address |
|||
{ |
|||
public const ADDRESS_ABSOLUTE = 1; |
|||
public const ADDRESS_COLUMN_RELATIVE = 2; |
|||
public const ADDRESS_ROW_RELATIVE = 3; |
|||
public const ADDRESS_RELATIVE = 4; |
|||
|
|||
public const REFERENCE_STYLE_A1 = true; |
|||
public const REFERENCE_STYLE_R1C1 = false; |
|||
|
|||
/** |
|||
* ADDRESS. |
|||
* |
|||
* Creates a cell address as text, given specified row and column numbers. |
|||
* |
|||
* Excel Function: |
|||
* =ADDRESS(row, column, [relativity], [referenceStyle], [sheetText]) |
|||
* |
|||
* @param mixed $row Row number (integer) to use in the cell reference |
|||
* @param mixed $column Column number (integer) to use in the cell reference |
|||
* @param mixed $relativity Integer flag indicating the type of reference to return |
|||
* 1 or omitted Absolute |
|||
* 2 Absolute row; relative column |
|||
* 3 Relative row; absolute column |
|||
* 4 Relative |
|||
* @param mixed $referenceStyle A logical (boolean) value that specifies the A1 or R1C1 reference style. |
|||
* TRUE or omitted ADDRESS returns an A1-style reference |
|||
* FALSE ADDRESS returns an R1C1-style reference |
|||
* @param mixed $sheetName Optional Name of worksheet to use |
|||
* |
|||
* @return string |
|||
*/ |
|||
public static function cell($row, $column, $relativity = 1, $referenceStyle = true, $sheetName = '') |
|||
{ |
|||
$row = Functions::flattenSingleValue($row); |
|||
$column = Functions::flattenSingleValue($column); |
|||
$relativity = ($relativity === null) ? 1 : Functions::flattenSingleValue($relativity); |
|||
$referenceStyle = ($referenceStyle === null) ? true : Functions::flattenSingleValue($referenceStyle); |
|||
$sheetName = Functions::flattenSingleValue($sheetName); |
|||
|
|||
if (($row < 1) || ($column < 1)) { |
|||
return Functions::VALUE(); |
|||
} |
|||
|
|||
$sheetName = self::sheetName($sheetName); |
|||
|
|||
if ((!is_bool($referenceStyle)) || $referenceStyle === self::REFERENCE_STYLE_A1) { |
|||
return self::formatAsA1($row, $column, $relativity, $sheetName); |
|||
} |
|||
|
|||
return self::formatAsR1C1($row, $column, $relativity, $sheetName); |
|||
} |
|||
|
|||
private static function sheetName(string $sheetName) |
|||
{ |
|||
if ($sheetName > '') { |
|||
if (strpos($sheetName, ' ') !== false || strpos($sheetName, '[') !== false) { |
|||
$sheetName = "'{$sheetName}'"; |
|||
} |
|||
$sheetName .= '!'; |
|||
} |
|||
|
|||
return $sheetName; |
|||
} |
|||
|
|||
private static function formatAsA1(int $row, int $column, int $relativity, string $sheetName): string |
|||
{ |
|||
$rowRelative = $columnRelative = '$'; |
|||
if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
|||
$columnRelative = ''; |
|||
} |
|||
if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
|||
$rowRelative = ''; |
|||
} |
|||
$column = Coordinate::stringFromColumnIndex($column); |
|||
|
|||
return "{$sheetName}{$columnRelative}{$column}{$rowRelative}{$row}"; |
|||
} |
|||
|
|||
private static function formatAsR1C1(int $row, int $column, int $relativity, string $sheetName): string |
|||
{ |
|||
if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
|||
$column = "[{$column}]"; |
|||
} |
|||
if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
|||
$row = "[{$row}]"; |
|||
} |
|||
|
|||
return "{$sheetName}R{$row}C{$column}"; |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
|
|||
class Absolute |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* ABS. |
|||
* |
|||
* Returns the result of builtin function abs after validating args. |
|||
* |
|||
* @param mixed $number Should be numeric, or can be an array of numbers |
|||
* |
|||
* @return array|float|int|string rounded number |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function evaluate($number) |
|||
{ |
|||
if (is_array($number)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return abs($number); |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
|
|||
class Angle |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* DEGREES. |
|||
* |
|||
* Returns the result of builtin function rad2deg after validating args. |
|||
* |
|||
* @param mixed $number Should be numeric, or can be an array of numbers |
|||
* |
|||
* @return array|float|string Rounded number |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toDegrees($number) |
|||
{ |
|||
if (is_array($number)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return rad2deg($number); |
|||
} |
|||
|
|||
/** |
|||
* RADIANS. |
|||
* |
|||
* Returns the result of builtin function deg2rad after validating args. |
|||
* |
|||
* @param mixed $number Should be numeric, or can be an array of numbers |
|||
* |
|||
* @return array|float|string Rounded number |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function toRadians($number) |
|||
{ |
|||
if (is_array($number)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return deg2rad($number); |
|||
} |
|||
} |
|||
@ -0,0 +1,112 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Arabic |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
private const ROMAN_LOOKUP = [ |
|||
'M' => 1000, |
|||
'D' => 500, |
|||
'C' => 100, |
|||
'L' => 50, |
|||
'X' => 10, |
|||
'V' => 5, |
|||
'I' => 1, |
|||
]; |
|||
|
|||
/** |
|||
* Recursively calculate the arabic value of a roman numeral. |
|||
* |
|||
* @param int $sum |
|||
* @param int $subtract |
|||
* |
|||
* @return int |
|||
*/ |
|||
private static function calculateArabic(array $roman, &$sum = 0, $subtract = 0) |
|||
{ |
|||
$numeral = array_shift($roman); |
|||
if (!isset(self::ROMAN_LOOKUP[$numeral])) { |
|||
throw new Exception('Invalid character detected'); |
|||
} |
|||
|
|||
$arabic = self::ROMAN_LOOKUP[$numeral]; |
|||
if (count($roman) > 0 && isset(self::ROMAN_LOOKUP[$roman[0]]) && $arabic < self::ROMAN_LOOKUP[$roman[0]]) { |
|||
$subtract += $arabic; |
|||
} else { |
|||
$sum += ($arabic - $subtract); |
|||
$subtract = 0; |
|||
} |
|||
|
|||
if (count($roman) > 0) { |
|||
self::calculateArabic($roman, $sum, $subtract); |
|||
} |
|||
|
|||
return $sum; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $value |
|||
*/ |
|||
private static function mollifyScrutinizer($value): array |
|||
{ |
|||
return is_array($value) ? $value : []; |
|||
} |
|||
|
|||
private static function strSplit(string $roman): array |
|||
{ |
|||
$rslt = str_split($roman); |
|||
|
|||
return self::mollifyScrutinizer($rslt); |
|||
} |
|||
|
|||
/** |
|||
* ARABIC. |
|||
* |
|||
* Converts a Roman numeral to an Arabic numeral. |
|||
* |
|||
* Excel Function: |
|||
* ARABIC(text) |
|||
* |
|||
* @param mixed $roman Should be a string, or can be an array of strings |
|||
* |
|||
* @return array|int|string the arabic numberal contrived from the roman numeral |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function evaluate($roman) |
|||
{ |
|||
if (is_array($roman)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $roman); |
|||
} |
|||
|
|||
// An empty string should return 0 |
|||
$roman = substr(trim(strtoupper((string) $roman)), 0, 255); |
|||
if ($roman === '') { |
|||
return 0; |
|||
} |
|||
|
|||
// Convert the roman numeral to an arabic number |
|||
$negativeNumber = $roman[0] === '-'; |
|||
if ($negativeNumber) { |
|||
$roman = substr($roman, 1); |
|||
} |
|||
|
|||
try { |
|||
$arabic = self::calculateArabic(self::strSplit($roman)); |
|||
} catch (Exception $e) { |
|||
return Functions::VALUE(); // Invalid character detected |
|||
} |
|||
|
|||
if ($negativeNumber) { |
|||
$arabic *= -1; // The number should be negative |
|||
} |
|||
|
|||
return $arabic; |
|||
} |
|||
} |
|||
@ -0,0 +1,68 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Base |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* BASE. |
|||
* |
|||
* Converts a number into a text representation with the given radix (base). |
|||
* |
|||
* Excel Function: |
|||
* BASE(Number, Radix [Min_length]) |
|||
* |
|||
* @param mixed $number expect float |
|||
* Or can be an array of values |
|||
* @param mixed $radix expect float |
|||
* Or can be an array of values |
|||
* @param mixed $minLength expect int or null |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string the text representation with the given radix (base) |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function evaluate($number, $radix, $minLength = null) |
|||
{ |
|||
if (is_array($number) || is_array($radix) || is_array($minLength)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $radix, $minLength); |
|||
} |
|||
|
|||
try { |
|||
$number = (float) floor(Helpers::validateNumericNullBool($number)); |
|||
$radix = (int) Helpers::validateNumericNullBool($radix); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return self::calculate($number, $radix, $minLength); |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $minLength |
|||
*/ |
|||
private static function calculate(float $number, int $radix, $minLength): string |
|||
{ |
|||
if ($minLength === null || is_numeric($minLength)) { |
|||
if ($number < 0 || $number >= 2 ** 53 || $radix < 2 || $radix > 36) { |
|||
return Functions::NAN(); // Numeric range constraints |
|||
} |
|||
|
|||
$outcome = strtoupper((string) base_convert("$number", 10, $radix)); |
|||
if ($minLength !== null) { |
|||
$outcome = str_pad($outcome, (int) $minLength, '0', STR_PAD_LEFT); // String padding |
|||
} |
|||
|
|||
return $outcome; |
|||
} |
|||
|
|||
return Functions::VALUE(); |
|||
} |
|||
} |
|||
@ -0,0 +1,166 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Ceiling |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* CEILING. |
|||
* |
|||
* Returns number rounded up, away from zero, to the nearest multiple of significance. |
|||
* For example, if you want to avoid using pennies in your prices and your product is |
|||
* priced at $4.42, use the formula =CEILING(4.42,0.05) to round prices up to the |
|||
* nearest nickel. |
|||
* |
|||
* Excel Function: |
|||
* CEILING(number[,significance]) |
|||
* |
|||
* @param array|float $number the number you want the ceiling |
|||
* Or can be an array of values |
|||
* @param array|float $significance the multiple to which you want to round |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string Rounded Number, or a string containing an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function ceiling($number, $significance = null) |
|||
{ |
|||
if (is_array($number) || is_array($significance)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance); |
|||
} |
|||
|
|||
if ($significance === null) { |
|||
self::floorCheck1Arg(); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return self::argumentsOk((float) $number, (float) $significance); |
|||
} |
|||
|
|||
/** |
|||
* CEILING.MATH. |
|||
* |
|||
* Round a number down to the nearest integer or to the nearest multiple of significance. |
|||
* |
|||
* Excel Function: |
|||
* CEILING.MATH(number[,significance[,mode]]) |
|||
* |
|||
* @param mixed $number Number to round |
|||
* Or can be an array of values |
|||
* @param mixed $significance Significance |
|||
* Or can be an array of values |
|||
* @param array|int $mode direction to round negative numbers |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string Rounded Number, or a string containing an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function math($number, $significance = null, $mode = 0) |
|||
{ |
|||
if (is_array($number) || is_array($significance) || is_array($mode)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance, $mode); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1); |
|||
$mode = Helpers::validateNumericNullSubstitution($mode, null); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (empty($significance * $number)) { |
|||
return 0.0; |
|||
} |
|||
if (self::ceilingMathTest((float) $significance, (float) $number, (int) $mode)) { |
|||
return floor($number / $significance) * $significance; |
|||
} |
|||
|
|||
return ceil($number / $significance) * $significance; |
|||
} |
|||
|
|||
/** |
|||
* CEILING.PRECISE. |
|||
* |
|||
* Rounds number up, away from zero, to the nearest multiple of significance. |
|||
* |
|||
* Excel Function: |
|||
* CEILING.PRECISE(number[,significance]) |
|||
* |
|||
* @param mixed $number the number you want to round |
|||
* Or can be an array of values |
|||
* @param array|float $significance the multiple to which you want to round |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string Rounded Number, or a string containing an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function precise($number, $significance = 1) |
|||
{ |
|||
if (is_array($number) || is_array($significance)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
$significance = Helpers::validateNumericNullSubstitution($significance, null); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (!$significance) { |
|||
return 0.0; |
|||
} |
|||
$result = $number / abs($significance); |
|||
|
|||
return ceil($result) * $significance * (($significance < 0) ? -1 : 1); |
|||
} |
|||
|
|||
/** |
|||
* Let CEILINGMATH complexity pass Scrutinizer. |
|||
*/ |
|||
private static function ceilingMathTest(float $significance, float $number, int $mode): bool |
|||
{ |
|||
return ((float) $significance < 0) || ((float) $number < 0 && !empty($mode)); |
|||
} |
|||
|
|||
/** |
|||
* Avoid Scrutinizer problems concerning complexity. |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
private static function argumentsOk(float $number, float $significance) |
|||
{ |
|||
if (empty($number * $significance)) { |
|||
return 0.0; |
|||
} |
|||
if (Helpers::returnSign($number) == Helpers::returnSign($significance)) { |
|||
return ceil($number / $significance) * $significance; |
|||
} |
|||
|
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
private static function floorCheck1Arg(): void |
|||
{ |
|||
$compatibility = Functions::getCompatibilityMode(); |
|||
if ($compatibility === Functions::COMPATIBILITY_EXCEL) { |
|||
throw new Exception('Excel requires 2 arguments for CEILING'); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,91 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
|
|||
class Combinations |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* COMBIN. |
|||
* |
|||
* Returns the number of combinations for a given number of items. Use COMBIN to |
|||
* determine the total possible number of groups for a given number of items. |
|||
* |
|||
* Excel Function: |
|||
* COMBIN(numObjs,numInSet) |
|||
* |
|||
* @param mixed $numObjs Number of different objects, or can be an array of numbers |
|||
* @param mixed $numInSet Number of objects in each combination, or can be an array of numbers |
|||
* |
|||
* @return array|float|int|string Number of combinations, or a string containing an error |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function withoutRepetition($numObjs, $numInSet) |
|||
{ |
|||
if (is_array($numObjs) || is_array($numInSet)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet); |
|||
} |
|||
|
|||
try { |
|||
$numObjs = Helpers::validateNumericNullSubstitution($numObjs, null); |
|||
$numInSet = Helpers::validateNumericNullSubstitution($numInSet, null); |
|||
Helpers::validateNotNegative($numInSet); |
|||
Helpers::validateNotNegative($numObjs - $numInSet); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return round(Factorial::fact($numObjs) / Factorial::fact($numObjs - $numInSet)) / Factorial::fact($numInSet); |
|||
} |
|||
|
|||
/** |
|||
* COMBINA. |
|||
* |
|||
* Returns the number of combinations for a given number of items. Use COMBIN to |
|||
* determine the total possible number of groups for a given number of items. |
|||
* |
|||
* Excel Function: |
|||
* COMBINA(numObjs,numInSet) |
|||
* |
|||
* @param mixed $numObjs Number of different objects, or can be an array of numbers |
|||
* @param mixed $numInSet Number of objects in each combination, or can be an array of numbers |
|||
* |
|||
* @return array|float|int|string Number of combinations, or a string containing an error |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function withRepetition($numObjs, $numInSet) |
|||
{ |
|||
if (is_array($numObjs) || is_array($numInSet)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $numObjs, $numInSet); |
|||
} |
|||
|
|||
try { |
|||
$numObjs = Helpers::validateNumericNullSubstitution($numObjs, null); |
|||
$numInSet = Helpers::validateNumericNullSubstitution($numInSet, null); |
|||
Helpers::validateNotNegative($numInSet); |
|||
Helpers::validateNotNegative($numObjs); |
|||
$numObjs = (int) $numObjs; |
|||
$numInSet = (int) $numInSet; |
|||
// Microsoft documentation says following is true, but Excel |
|||
// does not enforce this restriction. |
|||
//Helpers::validateNotNegative($numObjs - $numInSet); |
|||
if ($numObjs === 0) { |
|||
Helpers::validateNotNegative(-$numInSet); |
|||
|
|||
return 1; |
|||
} |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return round( |
|||
Factorial::fact($numObjs + $numInSet - 1) / Factorial::fact($numObjs - 1) |
|||
) / Factorial::fact($numInSet); |
|||
} |
|||
} |
|||
@ -0,0 +1,64 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Helpers; |
|||
|
|||
class Cosecant |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* CSC. |
|||
* |
|||
* Returns the cosecant of an angle. |
|||
* |
|||
* @param array|float $angle Number, or can be an array of numbers |
|||
* |
|||
* @return array|float|string The cosecant of the angle |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function csc($angle) |
|||
{ |
|||
if (is_array($angle)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $angle); |
|||
} |
|||
|
|||
try { |
|||
$angle = Helpers::validateNumericNullBool($angle); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return Helpers::verySmallDenominator(1.0, sin($angle)); |
|||
} |
|||
|
|||
/** |
|||
* CSCH. |
|||
* |
|||
* Returns the hyperbolic cosecant of an angle. |
|||
* |
|||
* @param array|float $angle Number, or can be an array of numbers |
|||
* |
|||
* @return array|float|string The hyperbolic cosecant of the angle |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function csch($angle) |
|||
{ |
|||
if (is_array($angle)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $angle); |
|||
} |
|||
|
|||
try { |
|||
$angle = Helpers::validateNumericNullBool($angle); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return Helpers::verySmallDenominator(1.0, sinh($angle)); |
|||
} |
|||
} |
|||
@ -0,0 +1,116 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Helpers; |
|||
|
|||
class Cosine |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* COS. |
|||
* |
|||
* Returns the result of builtin function cos after validating args. |
|||
* |
|||
* @param mixed $number Should be numeric, or can be an array of numbers |
|||
* |
|||
* @return array|float|string cosine |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function cos($number) |
|||
{ |
|||
if (is_array($number)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return cos($number); |
|||
} |
|||
|
|||
/** |
|||
* COSH. |
|||
* |
|||
* Returns the result of builtin function cosh after validating args. |
|||
* |
|||
* @param mixed $number Should be numeric, or can be an array of numbers |
|||
* |
|||
* @return array|float|string hyperbolic cosine |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function cosh($number) |
|||
{ |
|||
if (is_array($number)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return cosh($number); |
|||
} |
|||
|
|||
/** |
|||
* ACOS. |
|||
* |
|||
* Returns the arccosine of a number. |
|||
* |
|||
* @param array|float $number Number, or can be an array of numbers |
|||
* |
|||
* @return array|float|string The arccosine of the number |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function acos($number) |
|||
{ |
|||
if (is_array($number)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return Helpers::numberOrNan(acos($number)); |
|||
} |
|||
|
|||
/** |
|||
* ACOSH. |
|||
* |
|||
* Returns the arc inverse hyperbolic cosine of a number. |
|||
* |
|||
* @param array|float $number Number, or can be an array of numbers |
|||
* |
|||
* @return array|float|string The inverse hyperbolic cosine of the number, or an error string |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function acosh($number) |
|||
{ |
|||
if (is_array($number)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return Helpers::numberOrNan(acosh($number)); |
|||
} |
|||
} |
|||
@ -0,0 +1,118 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Helpers; |
|||
|
|||
class Cotangent |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* COT. |
|||
* |
|||
* Returns the cotangent of an angle. |
|||
* |
|||
* @param array|float $angle Number, or can be an array of numbers |
|||
* |
|||
* @return array|float|string The cotangent of the angle |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function cot($angle) |
|||
{ |
|||
if (is_array($angle)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $angle); |
|||
} |
|||
|
|||
try { |
|||
$angle = Helpers::validateNumericNullBool($angle); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return Helpers::verySmallDenominator(cos($angle), sin($angle)); |
|||
} |
|||
|
|||
/** |
|||
* COTH. |
|||
* |
|||
* Returns the hyperbolic cotangent of an angle. |
|||
* |
|||
* @param array|float $angle Number, or can be an array of numbers |
|||
* |
|||
* @return array|float|string The hyperbolic cotangent of the angle |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function coth($angle) |
|||
{ |
|||
if (is_array($angle)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $angle); |
|||
} |
|||
|
|||
try { |
|||
$angle = Helpers::validateNumericNullBool($angle); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return Helpers::verySmallDenominator(1.0, tanh($angle)); |
|||
} |
|||
|
|||
/** |
|||
* ACOT. |
|||
* |
|||
* Returns the arccotangent of a number. |
|||
* |
|||
* @param array|float $number Number, or can be an array of numbers |
|||
* |
|||
* @return array|float|string The arccotangent of the number |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function acot($number) |
|||
{ |
|||
if (is_array($number)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
return (M_PI / 2) - atan($number); |
|||
} |
|||
|
|||
/** |
|||
* ACOTH. |
|||
* |
|||
* Returns the hyperbolic arccotangent of a number. |
|||
* |
|||
* @param array|float $number Number, or can be an array of numbers |
|||
* |
|||
* @return array|float|string The hyperbolic arccotangent of the number |
|||
* If an array of numbers is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function acoth($number) |
|||
{ |
|||
if (is_array($number)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
|||
} |
|||
|
|||
try { |
|||
$number = Helpers::validateNumericNullBool($number); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
$result = ($number === 1) ? NAN : (log(($number + 1) / ($number - 1)) / 2); |
|||
|
|||
return Helpers::numberOrNan($result); |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
abstract class AggregateBase |
|||
{ |
|||
/** |
|||
* MS Excel does not count Booleans if passed as cell values, but they are counted if passed as literals. |
|||
* OpenOffice Calc always counts Booleans. |
|||
* Gnumeric never counts Booleans. |
|||
* |
|||
* @param mixed $arg |
|||
* @param mixed $k |
|||
* |
|||
* @return int|mixed |
|||
*/ |
|||
protected static function testAcceptedBoolean($arg, $k) |
|||
{ |
|||
if ( |
|||
(is_bool($arg)) && |
|||
((!Functions::isCellValue($k) && (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_EXCEL)) || |
|||
(Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE)) |
|||
) { |
|||
$arg = (int) $arg; |
|||
} |
|||
|
|||
return $arg; |
|||
} |
|||
|
|||
/** |
|||
* @param mixed $arg |
|||
* @param mixed $k |
|||
* |
|||
* @return bool |
|||
*/ |
|||
protected static function isAcceptedCountable($arg, $k) |
|||
{ |
|||
if ( |
|||
((is_numeric($arg)) && (!is_string($arg))) || |
|||
((is_numeric($arg)) && (!Functions::isCellValue($k)) && |
|||
(Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_GNUMERIC)) |
|||
) { |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
} |
|||
@ -0,0 +1,259 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Averages extends AggregateBase |
|||
{ |
|||
/** |
|||
* AVEDEV. |
|||
* |
|||
* Returns the average of the absolute deviations of data points from their mean. |
|||
* AVEDEV is a measure of the variability in a data set. |
|||
* |
|||
* Excel Function: |
|||
* AVEDEV(value1[,value2[, ...]]) |
|||
* |
|||
* @param mixed ...$args Data values |
|||
* |
|||
* @return float|string (string if result is an error) |
|||
*/ |
|||
public static function averageDeviations(...$args) |
|||
{ |
|||
$aArgs = Functions::flattenArrayIndexed($args); |
|||
|
|||
// Return value |
|||
$returnValue = 0.0; |
|||
|
|||
$aMean = self::average(...$args); |
|||
if ($aMean === Functions::DIV0()) { |
|||
return Functions::NAN(); |
|||
} elseif ($aMean === Functions::VALUE()) { |
|||
return Functions::VALUE(); |
|||
} |
|||
|
|||
$aCount = 0; |
|||
foreach ($aArgs as $k => $arg) { |
|||
$arg = self::testAcceptedBoolean($arg, $k); |
|||
// Is it a numeric value? |
|||
// Strings containing numeric values are only counted if they are string literals (not cell values) |
|||
// and then only in MS Excel and in Open Office, not in Gnumeric |
|||
if ((is_string($arg)) && (!is_numeric($arg)) && (!Functions::isCellValue($k))) { |
|||
return Functions::VALUE(); |
|||
} |
|||
if (self::isAcceptedCountable($arg, $k)) { |
|||
$returnValue += abs($arg - $aMean); |
|||
++$aCount; |
|||
} |
|||
} |
|||
|
|||
// Return |
|||
if ($aCount === 0) { |
|||
return Functions::DIV0(); |
|||
} |
|||
|
|||
return $returnValue / $aCount; |
|||
} |
|||
|
|||
/** |
|||
* AVERAGE. |
|||
* |
|||
* Returns the average (arithmetic mean) of the arguments |
|||
* |
|||
* Excel Function: |
|||
* AVERAGE(value1[,value2[, ...]]) |
|||
* |
|||
* @param mixed ...$args Data values |
|||
* |
|||
* @return float|string (string if result is an error) |
|||
*/ |
|||
public static function average(...$args) |
|||
{ |
|||
$returnValue = $aCount = 0; |
|||
|
|||
// Loop through arguments |
|||
foreach (Functions::flattenArrayIndexed($args) as $k => $arg) { |
|||
$arg = self::testAcceptedBoolean($arg, $k); |
|||
// Is it a numeric value? |
|||
// Strings containing numeric values are only counted if they are string literals (not cell values) |
|||
// and then only in MS Excel and in Open Office, not in Gnumeric |
|||
if ((is_string($arg)) && (!is_numeric($arg)) && (!Functions::isCellValue($k))) { |
|||
return Functions::VALUE(); |
|||
} |
|||
if (self::isAcceptedCountable($arg, $k)) { |
|||
$returnValue += $arg; |
|||
++$aCount; |
|||
} |
|||
} |
|||
|
|||
// Return |
|||
if ($aCount > 0) { |
|||
return $returnValue / $aCount; |
|||
} |
|||
|
|||
return Functions::DIV0(); |
|||
} |
|||
|
|||
/** |
|||
* AVERAGEA. |
|||
* |
|||
* Returns the average of its arguments, including numbers, text, and logical values |
|||
* |
|||
* Excel Function: |
|||
* AVERAGEA(value1[,value2[, ...]]) |
|||
* |
|||
* @param mixed ...$args Data values |
|||
* |
|||
* @return float|string (string if result is an error) |
|||
*/ |
|||
public static function averageA(...$args) |
|||
{ |
|||
$returnValue = null; |
|||
|
|||
$aCount = 0; |
|||
// Loop through arguments |
|||
foreach (Functions::flattenArrayIndexed($args) as $k => $arg) { |
|||
if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) { |
|||
} else { |
|||
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) { |
|||
if (is_bool($arg)) { |
|||
$arg = (int) $arg; |
|||
} elseif (is_string($arg)) { |
|||
$arg = 0; |
|||
} |
|||
$returnValue += $arg; |
|||
++$aCount; |
|||
} |
|||
} |
|||
} |
|||
|
|||
if ($aCount > 0) { |
|||
return $returnValue / $aCount; |
|||
} |
|||
|
|||
return Functions::DIV0(); |
|||
} |
|||
|
|||
/** |
|||
* MEDIAN. |
|||
* |
|||
* Returns the median of the given numbers. The median is the number in the middle of a set of numbers. |
|||
* |
|||
* Excel Function: |
|||
* MEDIAN(value1[,value2[, ...]]) |
|||
* |
|||
* @param mixed ...$args Data values |
|||
* |
|||
* @return float|string The result, or a string containing an error |
|||
*/ |
|||
public static function median(...$args) |
|||
{ |
|||
$aArgs = Functions::flattenArray($args); |
|||
|
|||
$returnValue = Functions::NAN(); |
|||
|
|||
$aArgs = self::filterArguments($aArgs); |
|||
$valueCount = count($aArgs); |
|||
if ($valueCount > 0) { |
|||
sort($aArgs, SORT_NUMERIC); |
|||
$valueCount = $valueCount / 2; |
|||
if ($valueCount == floor($valueCount)) { |
|||
$returnValue = ($aArgs[$valueCount--] + $aArgs[$valueCount]) / 2; |
|||
} else { |
|||
$valueCount = floor($valueCount); |
|||
$returnValue = $aArgs[$valueCount]; |
|||
} |
|||
} |
|||
|
|||
return $returnValue; |
|||
} |
|||
|
|||
/** |
|||
* MODE. |
|||
* |
|||
* Returns the most frequently occurring, or repetitive, value in an array or range of data |
|||
* |
|||
* Excel Function: |
|||
* MODE(value1[,value2[, ...]]) |
|||
* |
|||
* @param mixed ...$args Data values |
|||
* |
|||
* @return float|string The result, or a string containing an error |
|||
*/ |
|||
public static function mode(...$args) |
|||
{ |
|||
$returnValue = Functions::NA(); |
|||
|
|||
// Loop through arguments |
|||
$aArgs = Functions::flattenArray($args); |
|||
$aArgs = self::filterArguments($aArgs); |
|||
|
|||
if (!empty($aArgs)) { |
|||
return self::modeCalc($aArgs); |
|||
} |
|||
|
|||
return $returnValue; |
|||
} |
|||
|
|||
protected static function filterArguments($args) |
|||
{ |
|||
return array_filter( |
|||
$args, |
|||
function ($value) { |
|||
// Is it a numeric value? |
|||
return (is_numeric($value)) && (!is_string($value)); |
|||
} |
|||
); |
|||
} |
|||
|
|||
// |
|||
// Special variant of array_count_values that isn't limited to strings and integers, |
|||
// but can work with floating point numbers as values |
|||
// |
|||
private static function modeCalc($data) |
|||
{ |
|||
$frequencyArray = []; |
|||
$index = 0; |
|||
$maxfreq = 0; |
|||
$maxfreqkey = ''; |
|||
$maxfreqdatum = ''; |
|||
foreach ($data as $datum) { |
|||
$found = false; |
|||
++$index; |
|||
foreach ($frequencyArray as $key => $value) { |
|||
if ((string) $value['value'] == (string) $datum) { |
|||
++$frequencyArray[$key]['frequency']; |
|||
$freq = $frequencyArray[$key]['frequency']; |
|||
if ($freq > $maxfreq) { |
|||
$maxfreq = $freq; |
|||
$maxfreqkey = $key; |
|||
$maxfreqdatum = $datum; |
|||
} elseif ($freq == $maxfreq) { |
|||
if ($frequencyArray[$key]['index'] < $frequencyArray[$maxfreqkey]['index']) { |
|||
$maxfreqkey = $key; |
|||
$maxfreqdatum = $datum; |
|||
} |
|||
} |
|||
$found = true; |
|||
|
|||
break; |
|||
} |
|||
} |
|||
|
|||
if ($found === false) { |
|||
$frequencyArray[] = [ |
|||
'value' => $datum, |
|||
'frequency' => 1, |
|||
'index' => $index, |
|||
]; |
|||
} |
|||
} |
|||
|
|||
if ($maxfreq <= 1) { |
|||
return Functions::NA(); |
|||
} |
|||
|
|||
return $maxfreqdatum; |
|||
} |
|||
} |
|||
@ -0,0 +1,304 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Database\DAverage; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Database\DCount; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Database\DMax; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Database\DMin; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Database\DSum; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Conditional |
|||
{ |
|||
private const CONDITION_COLUMN_NAME = 'CONDITION'; |
|||
private const VALUE_COLUMN_NAME = 'VALUE'; |
|||
private const CONDITIONAL_COLUMN_NAME = 'CONDITIONAL %d'; |
|||
|
|||
/** |
|||
* AVERAGEIF. |
|||
* |
|||
* Returns the average value from a range of cells that contain numbers within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* AVERAGEIF(range,condition[, average_range]) |
|||
* |
|||
* @param mixed[] $range Data values |
|||
* @param string $condition the criteria that defines which cells will be checked |
|||
* @param mixed[] $averageRange Data values |
|||
* |
|||
* @return null|float|string |
|||
*/ |
|||
public static function AVERAGEIF($range, $condition, $averageRange = []) |
|||
{ |
|||
$database = self::databaseFromRangeAndValue($range, $averageRange); |
|||
$condition = [[self::CONDITION_COLUMN_NAME, self::VALUE_COLUMN_NAME], [$condition, null]]; |
|||
|
|||
return DAverage::evaluate($database, self::VALUE_COLUMN_NAME, $condition); |
|||
} |
|||
|
|||
/** |
|||
* AVERAGEIFS. |
|||
* |
|||
* Counts the number of cells that contain numbers within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* AVERAGEIFS(average_range, criteria_range1, criteria1, [criteria_range2, criteria2]…) |
|||
* |
|||
* @param mixed $args Pairs of Ranges and Criteria |
|||
* |
|||
* @return null|float|string |
|||
*/ |
|||
public static function AVERAGEIFS(...$args) |
|||
{ |
|||
if (empty($args)) { |
|||
return 0.0; |
|||
} elseif (count($args) === 3) { |
|||
return self::AVERAGEIF($args[1], $args[2], $args[0]); |
|||
} |
|||
|
|||
$conditions = self::buildConditionSetForValueRange(...$args); |
|||
$database = self::buildDatabaseWithValueRange(...$args); |
|||
|
|||
return DAverage::evaluate($database, self::VALUE_COLUMN_NAME, $conditions); |
|||
} |
|||
|
|||
/** |
|||
* COUNTIF. |
|||
* |
|||
* Counts the number of cells that contain numbers within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* COUNTIF(range,condition) |
|||
* |
|||
* @param mixed[] $range Data values |
|||
* @param string $condition the criteria that defines which cells will be counted |
|||
* |
|||
* @return int |
|||
*/ |
|||
public static function COUNTIF($range, $condition) |
|||
{ |
|||
// Filter out any empty values that shouldn't be included in a COUNT |
|||
$range = array_filter( |
|||
Functions::flattenArray($range), |
|||
function ($value) { |
|||
return $value !== null && $value !== ''; |
|||
} |
|||
); |
|||
|
|||
$range = array_merge([[self::CONDITION_COLUMN_NAME]], array_chunk($range, 1)); |
|||
$condition = array_merge([[self::CONDITION_COLUMN_NAME]], [[$condition]]); |
|||
|
|||
return DCount::evaluate($range, null, $condition); |
|||
} |
|||
|
|||
/** |
|||
* COUNTIFS. |
|||
* |
|||
* Counts the number of cells that contain numbers within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* COUNTIFS(criteria_range1, criteria1, [criteria_range2, criteria2]…) |
|||
* |
|||
* @param mixed $args Pairs of Ranges and Criteria |
|||
* |
|||
* @return int |
|||
*/ |
|||
public static function COUNTIFS(...$args) |
|||
{ |
|||
if (empty($args)) { |
|||
return 0; |
|||
} elseif (count($args) === 2) { |
|||
return self::COUNTIF(...$args); |
|||
} |
|||
|
|||
$database = self::buildDatabase(...$args); |
|||
$conditions = self::buildConditionSet(...$args); |
|||
|
|||
return DCount::evaluate($database, null, $conditions); |
|||
} |
|||
|
|||
/** |
|||
* MAXIFS. |
|||
* |
|||
* Returns the maximum value within a range of cells that contain numbers within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* MAXIFS(max_range, criteria_range1, criteria1, [criteria_range2, criteria2]…) |
|||
* |
|||
* @param mixed $args Pairs of Ranges and Criteria |
|||
* |
|||
* @return null|float|string |
|||
*/ |
|||
public static function MAXIFS(...$args) |
|||
{ |
|||
if (empty($args)) { |
|||
return 0.0; |
|||
} |
|||
|
|||
$conditions = self::buildConditionSetForValueRange(...$args); |
|||
$database = self::buildDatabaseWithValueRange(...$args); |
|||
|
|||
return DMax::evaluate($database, self::VALUE_COLUMN_NAME, $conditions); |
|||
} |
|||
|
|||
/** |
|||
* MINIFS. |
|||
* |
|||
* Returns the minimum value within a range of cells that contain numbers within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* MINIFS(min_range, criteria_range1, criteria1, [criteria_range2, criteria2]…) |
|||
* |
|||
* @param mixed $args Pairs of Ranges and Criteria |
|||
* |
|||
* @return null|float|string |
|||
*/ |
|||
public static function MINIFS(...$args) |
|||
{ |
|||
if (empty($args)) { |
|||
return 0.0; |
|||
} |
|||
|
|||
$conditions = self::buildConditionSetForValueRange(...$args); |
|||
$database = self::buildDatabaseWithValueRange(...$args); |
|||
|
|||
return DMin::evaluate($database, self::VALUE_COLUMN_NAME, $conditions); |
|||
} |
|||
|
|||
/** |
|||
* SUMIF. |
|||
* |
|||
* Totals the values of cells that contain numbers within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* SUMIF(range, criteria, [sum_range]) |
|||
* |
|||
* @param mixed $range Data values |
|||
* @param mixed $sumRange |
|||
* @param mixed $condition |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function SUMIF($range, $condition, $sumRange = []) |
|||
{ |
|||
$database = self::databaseFromRangeAndValue($range, $sumRange); |
|||
$condition = [[self::CONDITION_COLUMN_NAME, self::VALUE_COLUMN_NAME], [$condition, null]]; |
|||
|
|||
return DSum::evaluate($database, self::VALUE_COLUMN_NAME, $condition); |
|||
} |
|||
|
|||
/** |
|||
* SUMIFS. |
|||
* |
|||
* Counts the number of cells that contain numbers within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* SUMIFS(average_range, criteria_range1, criteria1, [criteria_range2, criteria2]…) |
|||
* |
|||
* @param mixed $args Pairs of Ranges and Criteria |
|||
* |
|||
* @return null|float|string |
|||
*/ |
|||
public static function SUMIFS(...$args) |
|||
{ |
|||
if (empty($args)) { |
|||
return 0.0; |
|||
} elseif (count($args) === 3) { |
|||
return self::SUMIF($args[1], $args[2], $args[0]); |
|||
} |
|||
|
|||
$conditions = self::buildConditionSetForValueRange(...$args); |
|||
$database = self::buildDatabaseWithValueRange(...$args); |
|||
|
|||
return DSum::evaluate($database, self::VALUE_COLUMN_NAME, $conditions); |
|||
} |
|||
|
|||
private static function buildConditionSet(...$args): array |
|||
{ |
|||
$conditions = self::buildConditions(1, ...$args); |
|||
|
|||
return array_map(null, ...$conditions); |
|||
} |
|||
|
|||
private static function buildConditionSetForValueRange(...$args): array |
|||
{ |
|||
$conditions = self::buildConditions(2, ...$args); |
|||
|
|||
if (count($conditions) === 1) { |
|||
return array_map( |
|||
function ($value) { |
|||
return [$value]; |
|||
}, |
|||
$conditions[0] |
|||
); |
|||
} |
|||
|
|||
return array_map(null, ...$conditions); |
|||
} |
|||
|
|||
private static function buildConditions(int $startOffset, ...$args): array |
|||
{ |
|||
$conditions = []; |
|||
|
|||
$pairCount = 1; |
|||
$argumentCount = count($args); |
|||
for ($argument = $startOffset; $argument < $argumentCount; $argument += 2) { |
|||
$conditions[] = array_merge([sprintf(self::CONDITIONAL_COLUMN_NAME, $pairCount)], [$args[$argument]]); |
|||
++$pairCount; |
|||
} |
|||
|
|||
return $conditions; |
|||
} |
|||
|
|||
private static function buildDatabase(...$args): array |
|||
{ |
|||
$database = []; |
|||
|
|||
return self::buildDataSet(0, $database, ...$args); |
|||
} |
|||
|
|||
private static function buildDatabaseWithValueRange(...$args): array |
|||
{ |
|||
$database = []; |
|||
$database[] = array_merge( |
|||
[self::VALUE_COLUMN_NAME], |
|||
Functions::flattenArray($args[0]) |
|||
); |
|||
|
|||
return self::buildDataSet(1, $database, ...$args); |
|||
} |
|||
|
|||
private static function buildDataSet(int $startOffset, array $database, ...$args): array |
|||
{ |
|||
$pairCount = 1; |
|||
$argumentCount = count($args); |
|||
for ($argument = $startOffset; $argument < $argumentCount; $argument += 2) { |
|||
$database[] = array_merge( |
|||
[sprintf(self::CONDITIONAL_COLUMN_NAME, $pairCount)], |
|||
Functions::flattenArray($args[$argument]) |
|||
); |
|||
++$pairCount; |
|||
} |
|||
|
|||
return array_map(null, ...$database); |
|||
} |
|||
|
|||
private static function databaseFromRangeAndValue(array $range, array $valueRange = []): array |
|||
{ |
|||
$range = Functions::flattenArray($range); |
|||
|
|||
$valueRange = Functions::flattenArray($valueRange); |
|||
if (empty($valueRange)) { |
|||
$valueRange = $range; |
|||
} |
|||
|
|||
$database = array_map( |
|||
null, |
|||
array_merge([self::CONDITION_COLUMN_NAME], $range), |
|||
array_merge([self::VALUE_COLUMN_NAME], $valueRange) |
|||
); |
|||
|
|||
return $database; |
|||
} |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Confidence |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* CONFIDENCE. |
|||
* |
|||
* Returns the confidence interval for a population mean |
|||
* |
|||
* @param mixed $alpha As a float |
|||
* Or can be an array of values |
|||
* @param mixed $stdDev Standard Deviation as a float |
|||
* Or can be an array of values |
|||
* @param mixed $size As an integer |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function CONFIDENCE($alpha, $stdDev, $size) |
|||
{ |
|||
if (is_array($alpha) || is_array($stdDev) || is_array($size)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $alpha, $stdDev, $size); |
|||
} |
|||
|
|||
try { |
|||
$alpha = StatisticalValidations::validateFloat($alpha); |
|||
$stdDev = StatisticalValidations::validateFloat($stdDev); |
|||
$size = StatisticalValidations::validateInt($size); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (($alpha <= 0) || ($alpha >= 1) || ($stdDev <= 0) || ($size < 1)) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
return Functions::scalar(Distributions\StandardNormal::inverse(1 - $alpha / 2) * $stdDev / sqrt($size)); |
|||
} |
|||
} |
|||
@ -0,0 +1,95 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Counts extends AggregateBase |
|||
{ |
|||
/** |
|||
* COUNT. |
|||
* |
|||
* Counts the number of cells that contain numbers within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* COUNT(value1[,value2[, ...]]) |
|||
* |
|||
* @param mixed ...$args Data values |
|||
* |
|||
* @return int |
|||
*/ |
|||
public static function COUNT(...$args) |
|||
{ |
|||
$returnValue = 0; |
|||
|
|||
// Loop through arguments |
|||
$aArgs = Functions::flattenArrayIndexed($args); |
|||
foreach ($aArgs as $k => $arg) { |
|||
$arg = self::testAcceptedBoolean($arg, $k); |
|||
// Is it a numeric value? |
|||
// Strings containing numeric values are only counted if they are string literals (not cell values) |
|||
// and then only in MS Excel and in Open Office, not in Gnumeric |
|||
if (self::isAcceptedCountable($arg, $k)) { |
|||
++$returnValue; |
|||
} |
|||
} |
|||
|
|||
return $returnValue; |
|||
} |
|||
|
|||
/** |
|||
* COUNTA. |
|||
* |
|||
* Counts the number of cells that are not empty within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* COUNTA(value1[,value2[, ...]]) |
|||
* |
|||
* @param mixed ...$args Data values |
|||
* |
|||
* @return int |
|||
*/ |
|||
public static function COUNTA(...$args) |
|||
{ |
|||
$returnValue = 0; |
|||
|
|||
// Loop through arguments |
|||
$aArgs = Functions::flattenArrayIndexed($args); |
|||
foreach ($aArgs as $k => $arg) { |
|||
// Nulls are counted if literals, but not if cell values |
|||
if ($arg !== null || (!Functions::isCellValue($k))) { |
|||
++$returnValue; |
|||
} |
|||
} |
|||
|
|||
return $returnValue; |
|||
} |
|||
|
|||
/** |
|||
* COUNTBLANK. |
|||
* |
|||
* Counts the number of empty cells within the list of arguments |
|||
* |
|||
* Excel Function: |
|||
* COUNTBLANK(value1[,value2[, ...]]) |
|||
* |
|||
* @param mixed ...$args Data values |
|||
* |
|||
* @return int |
|||
*/ |
|||
public static function COUNTBLANK(...$args) |
|||
{ |
|||
$returnValue = 0; |
|||
|
|||
// Loop through arguments |
|||
$aArgs = Functions::flattenArray($args); |
|||
foreach ($aArgs as $arg) { |
|||
// Is it a blank cell? |
|||
if (($arg === null) || ((is_string($arg)) && ($arg == ''))) { |
|||
++$returnValue; |
|||
} |
|||
} |
|||
|
|||
return $returnValue; |
|||
} |
|||
} |
|||
@ -0,0 +1,141 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Deviations |
|||
{ |
|||
/** |
|||
* DEVSQ. |
|||
* |
|||
* Returns the sum of squares of deviations of data points from their sample mean. |
|||
* |
|||
* Excel Function: |
|||
* DEVSQ(value1[,value2[, ...]]) |
|||
* |
|||
* @param mixed ...$args Data values |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function sumSquares(...$args) |
|||
{ |
|||
$aArgs = Functions::flattenArrayIndexed($args); |
|||
|
|||
$aMean = Averages::average($aArgs); |
|||
if (!is_numeric($aMean)) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
// Return value |
|||
$returnValue = 0.0; |
|||
$aCount = -1; |
|||
foreach ($aArgs as $k => $arg) { |
|||
// Is it a numeric value? |
|||
if ( |
|||
(is_bool($arg)) && |
|||
((!Functions::isCellValue($k)) || |
|||
(Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE)) |
|||
) { |
|||
$arg = (int) $arg; |
|||
} |
|||
if ((is_numeric($arg)) && (!is_string($arg))) { |
|||
$returnValue += ($arg - $aMean) ** 2; |
|||
++$aCount; |
|||
} |
|||
} |
|||
|
|||
return $aCount === 0 ? Functions::VALUE() : $returnValue; |
|||
} |
|||
|
|||
/** |
|||
* KURT. |
|||
* |
|||
* Returns the kurtosis of a data set. Kurtosis characterizes the relative peakedness |
|||
* or flatness of a distribution compared with the normal distribution. Positive |
|||
* kurtosis indicates a relatively peaked distribution. Negative kurtosis indicates a |
|||
* relatively flat distribution. |
|||
* |
|||
* @param array ...$args Data Series |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function kurtosis(...$args) |
|||
{ |
|||
$aArgs = Functions::flattenArrayIndexed($args); |
|||
$mean = Averages::average($aArgs); |
|||
if (!is_numeric($mean)) { |
|||
return Functions::DIV0(); |
|||
} |
|||
$stdDev = StandardDeviations::STDEV($aArgs); |
|||
|
|||
if ($stdDev > 0) { |
|||
$count = $summer = 0; |
|||
|
|||
foreach ($aArgs as $k => $arg) { |
|||
if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) { |
|||
} else { |
|||
// Is it a numeric value? |
|||
if ((is_numeric($arg)) && (!is_string($arg))) { |
|||
$summer += (($arg - $mean) / $stdDev) ** 4; |
|||
++$count; |
|||
} |
|||
} |
|||
} |
|||
|
|||
if ($count > 3) { |
|||
return $summer * ($count * ($count + 1) / |
|||
(($count - 1) * ($count - 2) * ($count - 3))) - (3 * ($count - 1) ** 2 / |
|||
(($count - 2) * ($count - 3))); |
|||
} |
|||
} |
|||
|
|||
return Functions::DIV0(); |
|||
} |
|||
|
|||
/** |
|||
* SKEW. |
|||
* |
|||
* Returns the skewness of a distribution. Skewness characterizes the degree of asymmetry |
|||
* of a distribution around its mean. Positive skewness indicates a distribution with an |
|||
* asymmetric tail extending toward more positive values. Negative skewness indicates a |
|||
* distribution with an asymmetric tail extending toward more negative values. |
|||
* |
|||
* @param array ...$args Data Series |
|||
* |
|||
* @return float|int|string The result, or a string containing an error |
|||
*/ |
|||
public static function skew(...$args) |
|||
{ |
|||
$aArgs = Functions::flattenArrayIndexed($args); |
|||
$mean = Averages::average($aArgs); |
|||
if (!is_numeric($mean)) { |
|||
return Functions::DIV0(); |
|||
} |
|||
$stdDev = StandardDeviations::STDEV($aArgs); |
|||
if ($stdDev === 0.0 || is_string($stdDev)) { |
|||
return Functions::DIV0(); |
|||
} |
|||
|
|||
$count = $summer = 0; |
|||
// Loop through arguments |
|||
foreach ($aArgs as $k => $arg) { |
|||
if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) { |
|||
} elseif (!is_numeric($arg)) { |
|||
return Functions::VALUE(); |
|||
} else { |
|||
// Is it a numeric value? |
|||
if ((is_numeric($arg)) && (!is_string($arg))) { |
|||
$summer += (($arg - $mean) / $stdDev) ** 3; |
|||
++$count; |
|||
} |
|||
} |
|||
} |
|||
|
|||
if ($count > 2) { |
|||
return $summer * ($count / (($count - 1) * ($count - 2))); |
|||
} |
|||
|
|||
return Functions::DIV0(); |
|||
} |
|||
} |
|||
@ -0,0 +1,279 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Beta |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
private const MAX_ITERATIONS = 256; |
|||
|
|||
private const LOG_GAMMA_X_MAX_VALUE = 2.55e305; |
|||
|
|||
private const XMININ = 2.23e-308; |
|||
|
|||
/** |
|||
* BETADIST. |
|||
* |
|||
* Returns the beta distribution. |
|||
* |
|||
* @param mixed $value Float value at which you want to evaluate the distribution |
|||
* Or can be an array of values |
|||
* @param mixed $alpha Parameter to the distribution as a float |
|||
* Or can be an array of values |
|||
* @param mixed $beta Parameter to the distribution as a float |
|||
* Or can be an array of values |
|||
* @param mixed $rMin as an float |
|||
* Or can be an array of values |
|||
* @param mixed $rMax as an float |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function distribution($value, $alpha, $beta, $rMin = 0.0, $rMax = 1.0) |
|||
{ |
|||
if (is_array($value) || is_array($alpha) || is_array($beta) || is_array($rMin) || is_array($rMax)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $alpha, $beta, $rMin, $rMax); |
|||
} |
|||
|
|||
$rMin = $rMin ?? 0.0; |
|||
$rMax = $rMax ?? 1.0; |
|||
|
|||
try { |
|||
$value = DistributionValidations::validateFloat($value); |
|||
$alpha = DistributionValidations::validateFloat($alpha); |
|||
$beta = DistributionValidations::validateFloat($beta); |
|||
$rMax = DistributionValidations::validateFloat($rMax); |
|||
$rMin = DistributionValidations::validateFloat($rMin); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($rMin > $rMax) { |
|||
$tmp = $rMin; |
|||
$rMin = $rMax; |
|||
$rMax = $tmp; |
|||
} |
|||
if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$value -= $rMin; |
|||
$value /= ($rMax - $rMin); |
|||
|
|||
return self::incompleteBeta($value, $alpha, $beta); |
|||
} |
|||
|
|||
/** |
|||
* BETAINV. |
|||
* |
|||
* Returns the inverse of the Beta distribution. |
|||
* |
|||
* @param mixed $probability Float probability at which you want to evaluate the distribution |
|||
* Or can be an array of values |
|||
* @param mixed $alpha Parameter to the distribution as a float |
|||
* Or can be an array of values |
|||
* @param mixed $beta Parameter to the distribution as a float |
|||
* Or can be an array of values |
|||
* @param mixed $rMin Minimum value as a float |
|||
* Or can be an array of values |
|||
* @param mixed $rMax Maximum value as a float |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function inverse($probability, $alpha, $beta, $rMin = 0.0, $rMax = 1.0) |
|||
{ |
|||
if (is_array($probability) || is_array($alpha) || is_array($beta) || is_array($rMin) || is_array($rMax)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $probability, $alpha, $beta, $rMin, $rMax); |
|||
} |
|||
|
|||
$rMin = $rMin ?? 0.0; |
|||
$rMax = $rMax ?? 1.0; |
|||
|
|||
try { |
|||
$probability = DistributionValidations::validateProbability($probability); |
|||
$alpha = DistributionValidations::validateFloat($alpha); |
|||
$beta = DistributionValidations::validateFloat($beta); |
|||
$rMax = DistributionValidations::validateFloat($rMax); |
|||
$rMin = DistributionValidations::validateFloat($rMin); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($rMin > $rMax) { |
|||
$tmp = $rMin; |
|||
$rMin = $rMax; |
|||
$rMax = $tmp; |
|||
} |
|||
if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0.0)) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
return self::calculateInverse($probability, $alpha, $beta, $rMin, $rMax); |
|||
} |
|||
|
|||
/** |
|||
* @return float|string |
|||
*/ |
|||
private static function calculateInverse(float $probability, float $alpha, float $beta, float $rMin, float $rMax) |
|||
{ |
|||
$a = 0; |
|||
$b = 2; |
|||
|
|||
$i = 0; |
|||
while ((($b - $a) > Functions::PRECISION) && (++$i <= self::MAX_ITERATIONS)) { |
|||
$guess = ($a + $b) / 2; |
|||
$result = self::distribution($guess, $alpha, $beta); |
|||
if (($result === $probability) || ($result === 0.0)) { |
|||
$b = $a; |
|||
} elseif ($result > $probability) { |
|||
$b = $guess; |
|||
} else { |
|||
$a = $guess; |
|||
} |
|||
} |
|||
|
|||
if ($i === self::MAX_ITERATIONS) { |
|||
return Functions::NA(); |
|||
} |
|||
|
|||
return round($rMin + $guess * ($rMax - $rMin), 12); |
|||
} |
|||
|
|||
/** |
|||
* Incomplete beta function. |
|||
* |
|||
* @author Jaco van Kooten |
|||
* @author Paul Meagher |
|||
* |
|||
* The computation is based on formulas from Numerical Recipes, Chapter 6.4 (W.H. Press et al, 1992). |
|||
* |
|||
* @param float $x require 0<=x<=1 |
|||
* @param float $p require p>0 |
|||
* @param float $q require q>0 |
|||
* |
|||
* @return float 0 if x<0, p<=0, q<=0 or p+q>2.55E305 and 1 if x>1 to avoid errors and over/underflow |
|||
*/ |
|||
public static function incompleteBeta(float $x, float $p, float $q): float |
|||
{ |
|||
if ($x <= 0.0) { |
|||
return 0.0; |
|||
} elseif ($x >= 1.0) { |
|||
return 1.0; |
|||
} elseif (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > self::LOG_GAMMA_X_MAX_VALUE)) { |
|||
return 0.0; |
|||
} |
|||
|
|||
$beta_gam = exp((0 - self::logBeta($p, $q)) + $p * log($x) + $q * log(1.0 - $x)); |
|||
if ($x < ($p + 1.0) / ($p + $q + 2.0)) { |
|||
return $beta_gam * self::betaFraction($x, $p, $q) / $p; |
|||
} |
|||
|
|||
return 1.0 - ($beta_gam * self::betaFraction(1 - $x, $q, $p) / $q); |
|||
} |
|||
|
|||
// Function cache for logBeta function |
|||
private static $logBetaCacheP = 0.0; |
|||
|
|||
private static $logBetaCacheQ = 0.0; |
|||
|
|||
private static $logBetaCacheResult = 0.0; |
|||
|
|||
/** |
|||
* The natural logarithm of the beta function. |
|||
* |
|||
* @param float $p require p>0 |
|||
* @param float $q require q>0 |
|||
* |
|||
* @return float 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow |
|||
* |
|||
* @author Jaco van Kooten |
|||
*/ |
|||
private static function logBeta(float $p, float $q): float |
|||
{ |
|||
if ($p != self::$logBetaCacheP || $q != self::$logBetaCacheQ) { |
|||
self::$logBetaCacheP = $p; |
|||
self::$logBetaCacheQ = $q; |
|||
if (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > self::LOG_GAMMA_X_MAX_VALUE)) { |
|||
self::$logBetaCacheResult = 0.0; |
|||
} else { |
|||
self::$logBetaCacheResult = Gamma::logGamma($p) + Gamma::logGamma($q) - Gamma::logGamma($p + $q); |
|||
} |
|||
} |
|||
|
|||
return self::$logBetaCacheResult; |
|||
} |
|||
|
|||
/** |
|||
* Evaluates of continued fraction part of incomplete beta function. |
|||
* Based on an idea from Numerical Recipes (W.H. Press et al, 1992). |
|||
* |
|||
* @author Jaco van Kooten |
|||
*/ |
|||
private static function betaFraction(float $x, float $p, float $q): float |
|||
{ |
|||
$c = 1.0; |
|||
$sum_pq = $p + $q; |
|||
$p_plus = $p + 1.0; |
|||
$p_minus = $p - 1.0; |
|||
$h = 1.0 - $sum_pq * $x / $p_plus; |
|||
if (abs($h) < self::XMININ) { |
|||
$h = self::XMININ; |
|||
} |
|||
$h = 1.0 / $h; |
|||
$frac = $h; |
|||
$m = 1; |
|||
$delta = 0.0; |
|||
while ($m <= self::MAX_ITERATIONS && abs($delta - 1.0) > Functions::PRECISION) { |
|||
$m2 = 2 * $m; |
|||
// even index for d |
|||
$d = $m * ($q - $m) * $x / (($p_minus + $m2) * ($p + $m2)); |
|||
$h = 1.0 + $d * $h; |
|||
if (abs($h) < self::XMININ) { |
|||
$h = self::XMININ; |
|||
} |
|||
$h = 1.0 / $h; |
|||
$c = 1.0 + $d / $c; |
|||
if (abs($c) < self::XMININ) { |
|||
$c = self::XMININ; |
|||
} |
|||
$frac *= $h * $c; |
|||
// odd index for d |
|||
$d = -($p + $m) * ($sum_pq + $m) * $x / (($p + $m2) * ($p_plus + $m2)); |
|||
$h = 1.0 + $d * $h; |
|||
if (abs($h) < self::XMININ) { |
|||
$h = self::XMININ; |
|||
} |
|||
$h = 1.0 / $h; |
|||
$c = 1.0 + $d / $c; |
|||
if (abs($c) < self::XMININ) { |
|||
$c = self::XMININ; |
|||
} |
|||
$delta = $h * $c; |
|||
$frac *= $delta; |
|||
++$m; |
|||
} |
|||
|
|||
return $frac; |
|||
} |
|||
|
|||
private static function betaValue(float $a, float $b): float |
|||
{ |
|||
return (Gamma::gammaValue($a) * Gamma::gammaValue($b)) / |
|||
Gamma::gammaValue($a + $b); |
|||
} |
|||
|
|||
private static function regularizedIncompleteBeta(float $value, float $a, float $b): float |
|||
{ |
|||
return self::incompleteBeta($value, $a, $b) / self::betaValue($a, $b); |
|||
} |
|||
} |
|||
@ -0,0 +1,228 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Combinations; |
|||
|
|||
class Binomial |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* BINOMDIST. |
|||
* |
|||
* Returns the individual term binomial distribution probability. Use BINOMDIST in problems with |
|||
* a fixed number of tests or trials, when the outcomes of any trial are only success or failure, |
|||
* when trials are independent, and when the probability of success is constant throughout the |
|||
* experiment. For example, BINOMDIST can calculate the probability that two of the next three |
|||
* babies born are male. |
|||
* |
|||
* @param mixed $value Integer number of successes in trials |
|||
* Or can be an array of values |
|||
* @param mixed $trials Integer umber of trials |
|||
* Or can be an array of values |
|||
* @param mixed $probability Probability of success on each trial as a float |
|||
* Or can be an array of values |
|||
* @param mixed $cumulative Boolean value indicating if we want the cdf (true) or the pdf (false) |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function distribution($value, $trials, $probability, $cumulative) |
|||
{ |
|||
if (is_array($value) || is_array($trials) || is_array($probability) || is_array($cumulative)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $trials, $probability, $cumulative); |
|||
} |
|||
|
|||
try { |
|||
$value = DistributionValidations::validateInt($value); |
|||
$trials = DistributionValidations::validateInt($trials); |
|||
$probability = DistributionValidations::validateProbability($probability); |
|||
$cumulative = DistributionValidations::validateBool($cumulative); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (($value < 0) || ($value > $trials)) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
if ($cumulative) { |
|||
return self::calculateCumulativeBinomial($value, $trials, $probability); |
|||
} |
|||
|
|||
return Combinations::withoutRepetition($trials, $value) * $probability ** $value |
|||
* (1 - $probability) ** ($trials - $value); |
|||
} |
|||
|
|||
/** |
|||
* BINOM.DIST.RANGE. |
|||
* |
|||
* Returns returns the Binomial Distribution probability for the number of successes from a specified number |
|||
* of trials falling into a specified range. |
|||
* |
|||
* @param mixed $trials Integer number of trials |
|||
* Or can be an array of values |
|||
* @param mixed $probability Probability of success on each trial as a float |
|||
* Or can be an array of values |
|||
* @param mixed $successes The integer number of successes in trials |
|||
* Or can be an array of values |
|||
* @param mixed $limit Upper limit for successes in trials as null, or an integer |
|||
* If null, then this will indicate the same as the number of Successes |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function range($trials, $probability, $successes, $limit = null) |
|||
{ |
|||
if (is_array($trials) || is_array($probability) || is_array($successes) || is_array($limit)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $trials, $probability, $successes, $limit); |
|||
} |
|||
|
|||
$limit = $limit ?? $successes; |
|||
|
|||
try { |
|||
$trials = DistributionValidations::validateInt($trials); |
|||
$probability = DistributionValidations::validateProbability($probability); |
|||
$successes = DistributionValidations::validateInt($successes); |
|||
$limit = DistributionValidations::validateInt($limit); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (($successes < 0) || ($successes > $trials)) { |
|||
return Functions::NAN(); |
|||
} |
|||
if (($limit < 0) || ($limit > $trials) || $limit < $successes) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$summer = 0; |
|||
for ($i = $successes; $i <= $limit; ++$i) { |
|||
$summer += Combinations::withoutRepetition($trials, $i) * $probability ** $i |
|||
* (1 - $probability) ** ($trials - $i); |
|||
} |
|||
|
|||
return $summer; |
|||
} |
|||
|
|||
/** |
|||
* NEGBINOMDIST. |
|||
* |
|||
* Returns the negative binomial distribution. NEGBINOMDIST returns the probability that |
|||
* there will be number_f failures before the number_s-th success, when the constant |
|||
* probability of a success is probability_s. This function is similar to the binomial |
|||
* distribution, except that the number of successes is fixed, and the number of trials is |
|||
* variable. Like the binomial, trials are assumed to be independent. |
|||
* |
|||
* @param mixed $failures Number of Failures as an integer |
|||
* Or can be an array of values |
|||
* @param mixed $successes Threshold number of Successes as an integer |
|||
* Or can be an array of values |
|||
* @param mixed $probability Probability of success on each trial as a float |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string The result, or a string containing an error |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
* |
|||
* TODO Add support for the cumulative flag not present for NEGBINOMDIST, but introduced for NEGBINOM.DIST |
|||
* The cumulative default should be false to reflect the behaviour of NEGBINOMDIST |
|||
*/ |
|||
public static function negative($failures, $successes, $probability) |
|||
{ |
|||
if (is_array($failures) || is_array($successes) || is_array($probability)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $failures, $successes, $probability); |
|||
} |
|||
|
|||
try { |
|||
$failures = DistributionValidations::validateInt($failures); |
|||
$successes = DistributionValidations::validateInt($successes); |
|||
$probability = DistributionValidations::validateProbability($probability); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if (($failures < 0) || ($successes < 1)) { |
|||
return Functions::NAN(); |
|||
} |
|||
if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) { |
|||
if (($failures + $successes - 1) <= 0) { |
|||
return Functions::NAN(); |
|||
} |
|||
} |
|||
|
|||
return (Combinations::withoutRepetition($failures + $successes - 1, $successes - 1)) |
|||
* ($probability ** $successes) * ((1 - $probability) ** $failures); |
|||
} |
|||
|
|||
/** |
|||
* BINOM.INV. |
|||
* |
|||
* Returns the smallest value for which the cumulative binomial distribution is greater |
|||
* than or equal to a criterion value |
|||
* |
|||
* @param mixed $trials number of Bernoulli trials as an integer |
|||
* Or can be an array of values |
|||
* @param mixed $probability probability of a success on each trial as a float |
|||
* Or can be an array of values |
|||
* @param mixed $alpha criterion value as a float |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|int|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function inverse($trials, $probability, $alpha) |
|||
{ |
|||
if (is_array($trials) || is_array($probability) || is_array($alpha)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $trials, $probability, $alpha); |
|||
} |
|||
|
|||
try { |
|||
$trials = DistributionValidations::validateInt($trials); |
|||
$probability = DistributionValidations::validateProbability($probability); |
|||
$alpha = DistributionValidations::validateFloat($alpha); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($trials < 0) { |
|||
return Functions::NAN(); |
|||
} elseif (($alpha < 0.0) || ($alpha > 1.0)) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$successes = 0; |
|||
while ($successes <= $trials) { |
|||
$result = self::calculateCumulativeBinomial($successes, $trials, $probability); |
|||
if ($result >= $alpha) { |
|||
break; |
|||
} |
|||
++$successes; |
|||
} |
|||
|
|||
return $successes; |
|||
} |
|||
|
|||
/** |
|||
* @return float|int |
|||
*/ |
|||
private static function calculateCumulativeBinomial(int $value, int $trials, float $probability) |
|||
{ |
|||
$summer = 0; |
|||
for ($i = 0; $i <= $value; ++$i) { |
|||
$summer += Combinations::withoutRepetition($trials, $i) * $probability ** $i |
|||
* (1 - $probability) ** ($trials - $i); |
|||
} |
|||
|
|||
return $summer; |
|||
} |
|||
} |
|||
@ -0,0 +1,334 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class ChiSquared |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
private const MAX_ITERATIONS = 256; |
|||
|
|||
private const EPS = 2.22e-16; |
|||
|
|||
/** |
|||
* CHIDIST. |
|||
* |
|||
* Returns the one-tailed probability of the chi-squared distribution. |
|||
* |
|||
* @param mixed $value Float value for which we want the probability |
|||
* Or can be an array of values |
|||
* @param mixed $degrees Integer degrees of freedom |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function distributionRightTail($value, $degrees) |
|||
{ |
|||
if (is_array($value) || is_array($degrees)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $degrees); |
|||
} |
|||
|
|||
try { |
|||
$value = DistributionValidations::validateFloat($value); |
|||
$degrees = DistributionValidations::validateInt($degrees); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($degrees < 1) { |
|||
return Functions::NAN(); |
|||
} |
|||
if ($value < 0) { |
|||
if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) { |
|||
return 1; |
|||
} |
|||
|
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
return 1 - (Gamma::incompleteGamma($degrees / 2, $value / 2) / Gamma::gammaValue($degrees / 2)); |
|||
} |
|||
|
|||
/** |
|||
* CHIDIST. |
|||
* |
|||
* Returns the one-tailed probability of the chi-squared distribution. |
|||
* |
|||
* @param mixed $value Float value for which we want the probability |
|||
* Or can be an array of values |
|||
* @param mixed $degrees Integer degrees of freedom |
|||
* Or can be an array of values |
|||
* @param mixed $cumulative Boolean value indicating if we want the cdf (true) or the pdf (false) |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function distributionLeftTail($value, $degrees, $cumulative) |
|||
{ |
|||
if (is_array($value) || is_array($degrees) || is_array($cumulative)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $degrees, $cumulative); |
|||
} |
|||
|
|||
try { |
|||
$value = DistributionValidations::validateFloat($value); |
|||
$degrees = DistributionValidations::validateInt($degrees); |
|||
$cumulative = DistributionValidations::validateBool($cumulative); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($degrees < 1) { |
|||
return Functions::NAN(); |
|||
} |
|||
if ($value < 0) { |
|||
if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) { |
|||
return 1; |
|||
} |
|||
|
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
if ($cumulative === true) { |
|||
return 1 - self::distributionRightTail($value, $degrees); |
|||
} |
|||
|
|||
return (($value ** (($degrees / 2) - 1) * exp(-$value / 2))) / |
|||
((2 ** ($degrees / 2)) * Gamma::gammaValue($degrees / 2)); |
|||
} |
|||
|
|||
/** |
|||
* CHIINV. |
|||
* |
|||
* Returns the inverse of the right-tailed probability of the chi-squared distribution. |
|||
* |
|||
* @param mixed $probability Float probability at which you want to evaluate the distribution |
|||
* Or can be an array of values |
|||
* @param mixed $degrees Integer degrees of freedom |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function inverseRightTail($probability, $degrees) |
|||
{ |
|||
if (is_array($probability) || is_array($degrees)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $probability, $degrees); |
|||
} |
|||
|
|||
try { |
|||
$probability = DistributionValidations::validateProbability($probability); |
|||
$degrees = DistributionValidations::validateInt($degrees); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($degrees < 1) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$callback = function ($value) use ($degrees) { |
|||
return 1 - (Gamma::incompleteGamma($degrees / 2, $value / 2) |
|||
/ Gamma::gammaValue($degrees / 2)); |
|||
}; |
|||
|
|||
$newtonRaphson = new NewtonRaphson($callback); |
|||
|
|||
return $newtonRaphson->execute($probability); |
|||
} |
|||
|
|||
/** |
|||
* CHIINV. |
|||
* |
|||
* Returns the inverse of the left-tailed probability of the chi-squared distribution. |
|||
* |
|||
* @param mixed $probability Float probability at which you want to evaluate the distribution |
|||
* Or can be an array of values |
|||
* @param mixed $degrees Integer degrees of freedom |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|float|string |
|||
* If an array of numbers is passed as an argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function inverseLeftTail($probability, $degrees) |
|||
{ |
|||
if (is_array($probability) || is_array($degrees)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $probability, $degrees); |
|||
} |
|||
|
|||
try { |
|||
$probability = DistributionValidations::validateProbability($probability); |
|||
$degrees = DistributionValidations::validateInt($degrees); |
|||
} catch (Exception $e) { |
|||
return $e->getMessage(); |
|||
} |
|||
|
|||
if ($degrees < 1) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
return self::inverseLeftTailCalculation($probability, $degrees); |
|||
} |
|||
|
|||
/** |
|||
* CHITEST. |
|||
* |
|||
* Uses the chi-square test to calculate the probability that the differences between two supplied data sets |
|||
* (of observed and expected frequencies), are likely to be simply due to sampling error, |
|||
* or if they are likely to be real. |
|||
* |
|||
* @param mixed $actual an array of observed frequencies |
|||
* @param mixed $expected an array of expected frequencies |
|||
* |
|||
* @return float|string |
|||
*/ |
|||
public static function test($actual, $expected) |
|||
{ |
|||
$rows = count($actual); |
|||
$actual = Functions::flattenArray($actual); |
|||
$expected = Functions::flattenArray($expected); |
|||
$columns = count($actual) / $rows; |
|||
|
|||
$countActuals = count($actual); |
|||
$countExpected = count($expected); |
|||
if ($countActuals !== $countExpected || $countActuals === 1) { |
|||
return Functions::NAN(); |
|||
} |
|||
|
|||
$result = 0.0; |
|||
for ($i = 0; $i < $countActuals; ++$i) { |
|||
if ($expected[$i] == 0.0) { |
|||
return Functions::DIV0(); |
|||
} elseif ($expected[$i] < 0.0) { |
|||
return Functions::NAN(); |
|||
} |
|||
$result += (($actual[$i] - $expected[$i]) ** 2) / $expected[$i]; |
|||
} |
|||
|
|||
$degrees = self::degrees($rows, $columns); |
|||
|
|||
$result = Functions::scalar(self::distributionRightTail($result, $degrees)); |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
protected static function degrees(int $rows, int $columns): int |
|||
{ |
|||
if ($rows === 1) { |
|||
return $columns - 1; |
|||
} elseif ($columns === 1) { |
|||
return $rows - 1; |
|||
} |
|||
|
|||
return ($columns - 1) * ($rows - 1); |
|||
} |
|||
|
|||
private static function inverseLeftTailCalculation(float $probability, int $degrees): float |
|||
{ |
|||
// bracket the root |
|||
$min = 0; |
|||
$sd = sqrt(2.0 * $degrees); |
|||
$max = 2 * $sd; |
|||
$s = -1; |
|||
|
|||
while ($s * self::pchisq($max, $degrees) > $probability * $s) { |
|||
$min = $max; |
|||
$max += 2 * $sd; |
|||
} |
|||
|
|||
// Find root using bisection |
|||
$chi2 = 0.5 * ($min + $max); |
|||
|
|||
while (($max - $min) > self::EPS * $chi2) { |
|||
if ($s * self::pchisq($chi2, $degrees) > $probability * $s) { |
|||
$min = $chi2; |
|||
} else { |
|||
$max = $chi2; |
|||
} |
|||
$chi2 = 0.5 * ($min + $max); |
|||
} |
|||
|
|||
return $chi2; |
|||
} |
|||
|
|||
private static function pchisq($chi2, $degrees) |
|||
{ |
|||
return self::gammp($degrees, 0.5 * $chi2); |
|||
} |
|||
|
|||
private static function gammp($n, $x) |
|||
{ |
|||
if ($x < 0.5 * $n + 1) { |
|||
return self::gser($n, $x); |
|||
} |
|||
|
|||
return 1 - self::gcf($n, $x); |
|||
} |
|||
|
|||
// Return the incomplete gamma function P(n/2,x) evaluated by |
|||
// series representation. Algorithm from numerical recipe. |
|||
// Assume that n is a positive integer and x>0, won't check arguments. |
|||
// Relative error controlled by the eps parameter |
|||
private static function gser($n, $x) |
|||
{ |
|||
$gln = Gamma::ln($n / 2); |
|||
$a = 0.5 * $n; |
|||
$ap = $a; |
|||
$sum = 1.0 / $a; |
|||
$del = $sum; |
|||
for ($i = 1; $i < 101; ++$i) { |
|||
++$ap; |
|||
$del = $del * $x / $ap; |
|||
$sum += $del; |
|||
if ($del < $sum * self::EPS) { |
|||
break; |
|||
} |
|||
} |
|||
|
|||
return $sum * exp(-$x + $a * log($x) - $gln); |
|||
} |
|||
|
|||
// Return the incomplete gamma function Q(n/2,x) evaluated by |
|||
// its continued fraction representation. Algorithm from numerical recipe. |
|||
// Assume that n is a postive integer and x>0, won't check arguments. |
|||
// Relative error controlled by the eps parameter |
|||
private static function gcf($n, $x) |
|||
{ |
|||
$gln = Gamma::ln($n / 2); |
|||
$a = 0.5 * $n; |
|||
$b = $x + 1 - $a; |
|||
$fpmin = 1.e-300; |
|||
$c = 1 / $fpmin; |
|||
$d = 1 / $b; |
|||
$h = $d; |
|||
for ($i = 1; $i < 101; ++$i) { |
|||
$an = -$i * ($i - $a); |
|||
$b += 2; |
|||
$d = $an * $d + $b; |
|||
if (abs($d) < $fpmin) { |
|||
$d = $fpmin; |
|||
} |
|||
$c = $b + $an / $c; |
|||
if (abs($c) < $fpmin) { |
|||
$c = $fpmin; |
|||
} |
|||
$d = 1 / $d; |
|||
$del = $d * $c; |
|||
$h = $h * $del; |
|||
if (abs($del - 1) < self::EPS) { |
|||
break; |
|||
} |
|||
} |
|||
|
|||
return $h * exp(-$x + $a * log($x) - $gln); |
|||
} |
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
|||
|
|||
class CaseConvert |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* LOWERCASE. |
|||
* |
|||
* Converts a string value to upper case. |
|||
* |
|||
* @param mixed $mixedCaseValue The string value to convert to lower case |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of values is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function lower($mixedCaseValue) |
|||
{ |
|||
if (is_array($mixedCaseValue)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $mixedCaseValue); |
|||
} |
|||
|
|||
$mixedCaseValue = Helpers::extractString($mixedCaseValue); |
|||
|
|||
return StringHelper::strToLower($mixedCaseValue); |
|||
} |
|||
|
|||
/** |
|||
* UPPERCASE. |
|||
* |
|||
* Converts a string value to upper case. |
|||
* |
|||
* @param mixed $mixedCaseValue The string value to convert to upper case |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of values is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function upper($mixedCaseValue) |
|||
{ |
|||
if (is_array($mixedCaseValue)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $mixedCaseValue); |
|||
} |
|||
|
|||
$mixedCaseValue = Helpers::extractString($mixedCaseValue); |
|||
|
|||
return StringHelper::strToUpper($mixedCaseValue); |
|||
} |
|||
|
|||
/** |
|||
* PROPERCASE. |
|||
* |
|||
* Converts a string value to proper or title case. |
|||
* |
|||
* @param mixed $mixedCaseValue The string value to convert to title case |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string |
|||
* If an array of values is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function proper($mixedCaseValue) |
|||
{ |
|||
if (is_array($mixedCaseValue)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $mixedCaseValue); |
|||
} |
|||
|
|||
$mixedCaseValue = Helpers::extractString($mixedCaseValue); |
|||
|
|||
return StringHelper::strToTitle($mixedCaseValue); |
|||
} |
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class CharacterConvert |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* CHAR. |
|||
* |
|||
* @param mixed $character Integer Value to convert to its character representation |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string The character string |
|||
* If an array of values is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function character($character) |
|||
{ |
|||
if (is_array($character)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $character); |
|||
} |
|||
|
|||
$character = Helpers::validateInt($character); |
|||
$min = Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE ? 0 : 1; |
|||
if ($character < $min || $character > 255) { |
|||
return Functions::VALUE(); |
|||
} |
|||
$result = iconv('UCS-4LE', 'UTF-8', pack('V', $character)); |
|||
|
|||
return ($result === false) ? '' : $result; |
|||
} |
|||
|
|||
/** |
|||
* CODE. |
|||
* |
|||
* @param mixed $characters String character to convert to its ASCII value |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|int|string A string if arguments are invalid |
|||
* If an array of values is passed as the argument, then the returned result will also be an array |
|||
* with the same dimensions |
|||
*/ |
|||
public static function code($characters) |
|||
{ |
|||
if (is_array($characters)) { |
|||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $characters); |
|||
} |
|||
|
|||
$characters = Helpers::extractString($characters); |
|||
if ($characters === '') { |
|||
return Functions::VALUE(); |
|||
} |
|||
|
|||
$character = $characters; |
|||
if (mb_strlen($characters, 'UTF-8') > 1) { |
|||
$character = mb_substr($characters, 0, 1, 'UTF-8'); |
|||
} |
|||
|
|||
return self::unicodeToOrd($character); |
|||
} |
|||
|
|||
private static function unicodeToOrd(string $character): int |
|||
{ |
|||
$retVal = 0; |
|||
$iconv = iconv('UTF-8', 'UCS-4LE', $character); |
|||
if ($iconv !== false) { |
|||
$result = unpack('V', $iconv); |
|||
if (is_array($result) && isset($result[1])) { |
|||
$retVal = $result[1]; |
|||
} |
|||
} |
|||
|
|||
return $retVal; |
|||
} |
|||
} |
|||
@ -0,0 +1,97 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
|
|||
class Concatenate |
|||
{ |
|||
use ArrayEnabled; |
|||
|
|||
/** |
|||
* CONCATENATE. |
|||
* |
|||
* @param array $args |
|||
*/ |
|||
public static function CONCATENATE(...$args): string |
|||
{ |
|||
$returnValue = ''; |
|||
|
|||
// Loop through arguments |
|||
$aArgs = Functions::flattenArray($args); |
|||
|
|||
foreach ($aArgs as $arg) { |
|||
$returnValue .= Helpers::extractString($arg); |
|||
} |
|||
|
|||
return $returnValue; |
|||
} |
|||
|
|||
/** |
|||
* TEXTJOIN. |
|||
* |
|||
* @param mixed $delimiter The delimter to use between the joined arguments |
|||
* Or can be an array of values |
|||
* @param mixed $ignoreEmpty true/false Flag indicating whether empty arguments should be skipped |
|||
* Or can be an array of values |
|||
* @param mixed $args The values to join |
|||
* |
|||
* @return array|string The joined string |
|||
* If an array of values is passed for the $delimiter or $ignoreEmpty arguments, then the returned result |
|||
* will also be an array with matching dimensions |
|||
*/ |
|||
public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args) |
|||
{ |
|||
if (is_array($delimiter) || is_array($ignoreEmpty)) { |
|||
return self::evaluateArrayArgumentsSubset( |
|||
[self::class, __FUNCTION__], |
|||
2, |
|||
$delimiter, |
|||
$ignoreEmpty, |
|||
...$args |
|||
); |
|||
} |
|||
|
|||
// Loop through arguments |
|||
$aArgs = Functions::flattenArray($args); |
|||
foreach ($aArgs as $key => &$arg) { |
|||
if ($ignoreEmpty === true && is_string($arg) && trim($arg) === '') { |
|||
unset($aArgs[$key]); |
|||
} elseif (is_bool($arg)) { |
|||
$arg = Helpers::convertBooleanValue($arg); |
|||
} |
|||
} |
|||
|
|||
return implode($delimiter, $aArgs); |
|||
} |
|||
|
|||
/** |
|||
* REPT. |
|||
* |
|||
* Returns the result of builtin function round after validating args. |
|||
* |
|||
* @param mixed $stringValue The value to repeat |
|||
* Or can be an array of values |
|||
* @param mixed $repeatCount The number of times the string value should be repeated |
|||
* Or can be an array of values |
|||
* |
|||
* @return array|string The repeated string |
|||
* If an array of values is passed for the $stringValue or $repeatCount arguments, then the returned result |
|||
* will also be an array with matching dimensions |
|||
*/ |
|||
public static function builtinREPT($stringValue, $repeatCount) |
|||
{ |
|||
if (is_array($stringValue) || is_array($repeatCount)) { |
|||
return self::evaluateArrayArguments([self::class, __FUNCTION__], $stringValue, $repeatCount); |
|||
} |
|||
|
|||
$stringValue = Helpers::extractString($stringValue); |
|||
|
|||
if (!is_numeric($repeatCount) || $repeatCount < 0) { |
|||
return Functions::VALUE(); |
|||
} |
|||
|
|||
return str_repeat($stringValue, (int) $repeatCount); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
## |
|||
## PhpSpreadsheet |
|||
## |
|||
## |
|||
## |
|||
|
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
|
|||
## |
|||
## (For future use) |
|||
## |
|||
currencySymbol = лв |
|||
|
|||
|
|||
## |
|||
## Excel Error Codes (For future use) |
|||
|
|||
## |
|||
NULL = #ПРАЗНО! |
|||
DIV0 = #ДЕЛ/0! |
|||
VALUE = #СТОЙНОСТ! |
|||
REF = #РЕФ! |
|||
NAME = #ИМЕ? |
|||
NUM = #ЧИСЛО! |
|||
NA = #Н/Д |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Ceština (Czech) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL |
|||
DIV0 = #DĚLENÍ_NULOU! |
|||
VALUE = #HODNOTA! |
|||
REF = #ODKAZ! |
|||
NAME = #NÁZEV? |
|||
NUM = #ČÍSLO! |
|||
NA = #NENÍ_K_DISPOZICI |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Dansk (Danish) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #NUL! |
|||
DIV0 |
|||
VALUE = #VÆRDI! |
|||
REF = #REFERENCE! |
|||
NAME = #NAVN? |
|||
NUM |
|||
NA = #I/T |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Deutsch (German) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL |
|||
DIV0 |
|||
VALUE = #WERT! |
|||
REF = #BEZUG! |
|||
NAME |
|||
NUM = #ZAHL! |
|||
NA = #NV |
|||
@ -0,0 +1,8 @@ |
|||
## |
|||
## PhpSpreadsheet |
|||
## |
|||
|
|||
## |
|||
## (For future use) |
|||
## |
|||
currencySymbol = £ |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Español (Spanish) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #¡NULO! |
|||
DIV0 = #¡DIV/0! |
|||
VALUE = #¡VALOR! |
|||
REF = #¡REF! |
|||
NAME = #¿NOMBRE? |
|||
NUM = #¡NUM! |
|||
NA |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Suomi (Finnish) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #TYHJÄ! |
|||
DIV0 = #JAKO/0! |
|||
VALUE = #ARVO! |
|||
REF = #VIITTAUS! |
|||
NAME = #NIMI? |
|||
NUM = #LUKU! |
|||
NA = #PUUTTUU! |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Français (French) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #NUL! |
|||
DIV0 |
|||
VALUE = #VALEUR! |
|||
REF |
|||
NAME = #NOM? |
|||
NUM = #NOMBRE! |
|||
NA |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Magyar (Hungarian) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #NULLA! |
|||
DIV0 = #ZÉRÓOSZTÓ! |
|||
VALUE = #ÉRTÉK! |
|||
REF = #HIV! |
|||
NAME = #NÉV? |
|||
NUM = #SZÁM! |
|||
NA = #HIÁNYZIK |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Italiano (Italian) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL |
|||
DIV0 |
|||
VALUE = #VALORE! |
|||
REF = #RIF! |
|||
NAME = #NOME? |
|||
NUM |
|||
NA = #N/D |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Norsk Bokmål (Norwegian Bokmål) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL |
|||
DIV0 |
|||
VALUE = #VERDI! |
|||
REF |
|||
NAME = #NAVN? |
|||
NUM |
|||
NA = #N/D |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Nederlands (Dutch) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #LEEG! |
|||
DIV0 = #DEEL/0! |
|||
VALUE = #WAARDE! |
|||
REF = #VERW! |
|||
NAME = #NAAM? |
|||
NUM = #GETAL! |
|||
NA = #N/B |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Jezyk polski (Polish) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #ZERO! |
|||
DIV0 = #DZIEL/0! |
|||
VALUE = #ARG! |
|||
REF = #ADR! |
|||
NAME = #NAZWA? |
|||
NUM = #LICZBA! |
|||
NA = #N/D! |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Português Brasileiro (Brazilian Portuguese) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #NULO! |
|||
DIV0 |
|||
VALUE = #VALOR! |
|||
REF |
|||
NAME = #NOME? |
|||
NUM = #NÚM! |
|||
NA = #N/D |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Português (Portuguese) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #NULO! |
|||
DIV0 |
|||
VALUE = #VALOR! |
|||
REF |
|||
NAME = #NOME? |
|||
NUM = #NÚM! |
|||
NA = #N/D |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## русский язык (Russian) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #ПУСТО! |
|||
DIV0 = #ДЕЛ/0! |
|||
VALUE = #ЗНАЧ! |
|||
REF = #ССЫЛКА! |
|||
NAME = #ИМЯ? |
|||
NUM = #ЧИСЛО! |
|||
NA = #Н/Д |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Svenska (Swedish) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #SKÄRNING! |
|||
DIV0 = #DIVISION/0! |
|||
VALUE = #VÄRDEFEL! |
|||
REF = #REFERENS! |
|||
NAME = #NAMN? |
|||
NUM = #OGILTIGT! |
|||
NA = #SAKNAS! |
|||
@ -0,0 +1,20 @@ |
|||
############################################################ |
|||
## |
|||
## PhpSpreadsheet - locale settings |
|||
## |
|||
## Türkçe (Turkish) |
|||
## |
|||
############################################################ |
|||
|
|||
ArgumentSeparator = ; |
|||
|
|||
## |
|||
## Error Codes |
|||
## |
|||
NULL = #BOŞ! |
|||
DIV0 = #SAYI/0! |
|||
VALUE = #DEĞER! |
|||
REF = #BAŞV! |
|||
NAME = #AD? |
|||
NUM = #SAYI! |
|||
NA = #YOK |
|||
@ -0,0 +1,153 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Cell; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Exception; |
|||
|
|||
class AddressHelper |
|||
{ |
|||
public const R1C1_COORDINATE_REGEX = '/(R((?:\[-?\d*\])|(?:\d*))?)(C((?:\[-?\d*\])|(?:\d*))?)/i'; |
|||
|
|||
/** |
|||
* Converts an R1C1 format cell address to an A1 format cell address. |
|||
*/ |
|||
public static function convertToA1( |
|||
string $address, |
|||
int $currentRowNumber = 1, |
|||
int $currentColumnNumber = 1 |
|||
): string { |
|||
$validityCheck = preg_match('/^(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))$/i', $address, $cellReference); |
|||
|
|||
if ($validityCheck === 0) { |
|||
throw new Exception('Invalid R1C1-format Cell Reference'); |
|||
} |
|||
|
|||
$rowReference = $cellReference[2]; |
|||
// Empty R reference is the current row |
|||
if ($rowReference === '') { |
|||
$rowReference = (string) $currentRowNumber; |
|||
} |
|||
// Bracketed R references are relative to the current row |
|||
if ($rowReference[0] === '[') { |
|||
$rowReference = $currentRowNumber + (int) trim($rowReference, '[]'); |
|||
} |
|||
$columnReference = $cellReference[4]; |
|||
// Empty C reference is the current column |
|||
if ($columnReference === '') { |
|||
$columnReference = (string) $currentColumnNumber; |
|||
} |
|||
// Bracketed C references are relative to the current column |
|||
if (is_string($columnReference) && $columnReference[0] === '[') { |
|||
$columnReference = $currentColumnNumber + (int) trim($columnReference, '[]'); |
|||
} |
|||
|
|||
if ($columnReference <= 0 || $rowReference <= 0) { |
|||
throw new Exception('Invalid R1C1-format Cell Reference, Value out of range'); |
|||
} |
|||
$A1CellReference = Coordinate::stringFromColumnIndex($columnReference) . $rowReference; |
|||
|
|||
return $A1CellReference; |
|||
} |
|||
|
|||
protected static function convertSpreadsheetMLFormula(string $formula): string |
|||
{ |
|||
$formula = substr($formula, 3); |
|||
$temp = explode('"', $formula); |
|||
$key = false; |
|||
foreach ($temp as &$value) { |
|||
// Only replace in alternate array entries (i.e. non-quoted blocks) |
|||
if ($key = !$key) { |
|||
$value = str_replace(['[.', ':.', ']'], ['', ':', ''], $value); |
|||
} |
|||
} |
|||
unset($value); |
|||
|
|||
return implode('"', $temp); |
|||
} |
|||
|
|||
/** |
|||
* Converts a formula that uses R1C1/SpreadsheetXML format cell address to an A1 format cell address. |
|||
*/ |
|||
public static function convertFormulaToA1( |
|||
string $formula, |
|||
int $currentRowNumber = 1, |
|||
int $currentColumnNumber = 1 |
|||
): string { |
|||
if (substr($formula, 0, 3) == 'of:') { |
|||
// We have an old-style SpreadsheetML Formula |
|||
return self::convertSpreadsheetMLFormula($formula); |
|||
} |
|||
|
|||
// Convert R1C1 style references to A1 style references (but only when not quoted) |
|||
$temp = explode('"', $formula); |
|||
$key = false; |
|||
foreach ($temp as &$value) { |
|||
// Only replace in alternate array entries (i.e. non-quoted blocks) |
|||
if ($key = !$key) { |
|||
preg_match_all(self::R1C1_COORDINATE_REGEX, $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE); |
|||
// Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way |
|||
// through the formula from left to right. Reversing means that we work right to left.through |
|||
// the formula |
|||
$cellReferences = array_reverse($cellReferences); |
|||
// Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent, |
|||
// then modify the formula to use that new reference |
|||
foreach ($cellReferences as $cellReference) { |
|||
$A1CellReference = self::convertToA1($cellReference[0][0], $currentRowNumber, $currentColumnNumber); |
|||
$value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0])); |
|||
} |
|||
} |
|||
} |
|||
unset($value); |
|||
|
|||
// Then rebuild the formula string |
|||
return implode('"', $temp); |
|||
} |
|||
|
|||
/** |
|||
* Converts an A1 format cell address to an R1C1 format cell address. |
|||
* If $currentRowNumber or $currentColumnNumber are provided, then the R1C1 address will be formatted as a relative address. |
|||
*/ |
|||
public static function convertToR1C1( |
|||
string $address, |
|||
?int $currentRowNumber = null, |
|||
?int $currentColumnNumber = null |
|||
): string { |
|||
$validityCheck = preg_match(Coordinate::A1_COORDINATE_REGEX, $address, $cellReference); |
|||
|
|||
if ($validityCheck === 0) { |
|||
throw new Exception('Invalid A1-format Cell Reference'); |
|||
} |
|||
|
|||
$columnId = Coordinate::columnIndexFromString($cellReference['col_ref']); |
|||
if ($cellReference['absolute_col'] === '$') { |
|||
// Column must be absolute address |
|||
$currentColumnNumber = null; |
|||
} |
|||
|
|||
$rowId = (int) $cellReference['row_ref']; |
|||
if ($cellReference['absolute_row'] === '$') { |
|||
// Row must be absolute address |
|||
$currentRowNumber = null; |
|||
} |
|||
|
|||
if ($currentRowNumber !== null) { |
|||
if ($rowId === $currentRowNumber) { |
|||
$rowId = ''; |
|||
} else { |
|||
$rowId = '[' . ($rowId - $currentRowNumber) . ']'; |
|||
} |
|||
} |
|||
|
|||
if ($currentColumnNumber !== null) { |
|||
if ($columnId === $currentColumnNumber) { |
|||
$columnId = ''; |
|||
} else { |
|||
$columnId = '[' . ($columnId - $currentColumnNumber) . ']'; |
|||
} |
|||
} |
|||
|
|||
$R1C1Address = "R{$rowId}C{$columnId}"; |
|||
|
|||
return $R1C1Address; |
|||
} |
|||
} |
|||
@ -0,0 +1,205 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Cell; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
|||
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
|||
use PhpOffice\PhpSpreadsheet\Shared\Date; |
|||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
|||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
|||
|
|||
class AdvancedValueBinder extends DefaultValueBinder implements IValueBinder |
|||
{ |
|||
/** |
|||
* Bind value to a cell. |
|||
* |
|||
* @param Cell $cell Cell to bind value to |
|||
* @param mixed $value Value to bind in cell |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function bindValue(Cell $cell, $value = null) |
|||
{ |
|||
if ($value === null) { |
|||
return parent::bindValue($cell, $value); |
|||
} elseif (is_string($value)) { |
|||
// sanitize UTF-8 strings |
|||
$value = StringHelper::sanitizeUTF8($value); |
|||
} |
|||
|
|||
// Find out data type |
|||
$dataType = parent::dataTypeForValue($value); |
|||
|
|||
// Style logic - strings |
|||
if ($dataType === DataType::TYPE_STRING && !$value instanceof RichText) { |
|||
// Test for booleans using locale-setting |
|||
if ($value == Calculation::getTRUE()) { |
|||
$cell->setValueExplicit(true, DataType::TYPE_BOOL); |
|||
|
|||
return true; |
|||
} elseif ($value == Calculation::getFALSE()) { |
|||
$cell->setValueExplicit(false, DataType::TYPE_BOOL); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
// Check for fractions |
|||
if (preg_match('/^([+-]?)\s*(\d+)\s?\/\s*(\d+)$/', $value, $matches)) { |
|||
return $this->setProperFraction($matches, $cell); |
|||
} elseif (preg_match('/^([+-]?)(\d*) +(\d*)\s?\/\s*(\d*)$/', $value, $matches)) { |
|||
return $this->setImproperFraction($matches, $cell); |
|||
} |
|||
|
|||
// Check for percentage |
|||
if (preg_match('/^\-?\d*\.?\d*\s?\%$/', $value)) { |
|||
return $this->setPercentage($value, $cell); |
|||
} |
|||
|
|||
// Check for currency |
|||
$currencyCode = StringHelper::getCurrencyCode(); |
|||
$decimalSeparator = StringHelper::getDecimalSeparator(); |
|||
$thousandsSeparator = StringHelper::getThousandsSeparator(); |
|||
if (preg_match('/^' . preg_quote($currencyCode, '/') . ' *(\d{1,3}(' . preg_quote($thousandsSeparator, '/') . '\d{3})*|(\d+))(' . preg_quote($decimalSeparator, '/') . '\d{2})?$/', $value)) { |
|||
// Convert value to number |
|||
$value = (float) trim(str_replace([$currencyCode, $thousandsSeparator, $decimalSeparator], ['', '', '.'], $value)); |
|||
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC); |
|||
// Set style |
|||
$cell->getWorksheet()->getStyle($cell->getCoordinate()) |
|||
->getNumberFormat()->setFormatCode( |
|||
str_replace('$', $currencyCode, NumberFormat::FORMAT_CURRENCY_USD_SIMPLE) |
|||
); |
|||
|
|||
return true; |
|||
} elseif (preg_match('/^\$ *(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/', $value)) { |
|||
// Convert value to number |
|||
$value = (float) trim(str_replace(['$', ','], '', $value)); |
|||
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC); |
|||
// Set style |
|||
$cell->getWorksheet()->getStyle($cell->getCoordinate()) |
|||
->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_USD_SIMPLE); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
// Check for time without seconds e.g. '9:45', '09:45' |
|||
if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/', $value)) { |
|||
return $this->setTimeHoursMinutes($value, $cell); |
|||
} |
|||
|
|||
// Check for time with seconds '9:45:59', '09:45:59' |
|||
if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$/', $value)) { |
|||
return $this->setTimeHoursMinutesSeconds($value, $cell); |
|||
} |
|||
|
|||
// Check for datetime, e.g. '2008-12-31', '2008-12-31 15:59', '2008-12-31 15:59:10' |
|||
if (($d = Date::stringToExcel($value)) !== false) { |
|||
// Convert value to number |
|||
$cell->setValueExplicit($d, DataType::TYPE_NUMERIC); |
|||
// Determine style. Either there is a time part or not. Look for ':' |
|||
if (strpos($value, ':') !== false) { |
|||
$formatCode = 'yyyy-mm-dd h:mm'; |
|||
} else { |
|||
$formatCode = 'yyyy-mm-dd'; |
|||
} |
|||
$cell->getWorksheet()->getStyle($cell->getCoordinate()) |
|||
->getNumberFormat()->setFormatCode($formatCode); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
// Check for newline character "\n" |
|||
if (strpos($value, "\n") !== false) { |
|||
$cell->setValueExplicit($value, DataType::TYPE_STRING); |
|||
// Set style |
|||
$cell->getWorksheet()->getStyle($cell->getCoordinate()) |
|||
->getAlignment()->setWrapText(true); |
|||
|
|||
return true; |
|||
} |
|||
} |
|||
|
|||
// Not bound yet? Use parent... |
|||
return parent::bindValue($cell, $value); |
|||
} |
|||
|
|||
protected function setImproperFraction(array $matches, Cell $cell): bool |
|||
{ |
|||
// Convert value to number |
|||
$value = $matches[2] + ($matches[3] / $matches[4]); |
|||
if ($matches[1] === '-') { |
|||
$value = 0 - $value; |
|||
} |
|||
$cell->setValueExplicit((float) $value, DataType::TYPE_NUMERIC); |
|||
|
|||
// Build the number format mask based on the size of the matched values |
|||
$dividend = str_repeat('?', strlen($matches[3])); |
|||
$divisor = str_repeat('?', strlen($matches[4])); |
|||
$fractionMask = "# {$dividend}/{$divisor}"; |
|||
// Set style |
|||
$cell->getWorksheet()->getStyle($cell->getCoordinate()) |
|||
->getNumberFormat()->setFormatCode($fractionMask); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
protected function setProperFraction(array $matches, Cell $cell): bool |
|||
{ |
|||
// Convert value to number |
|||
$value = $matches[2] / $matches[3]; |
|||
if ($matches[1] === '-') { |
|||
$value = 0 - $value; |
|||
} |
|||
$cell->setValueExplicit((float) $value, DataType::TYPE_NUMERIC); |
|||
|
|||
// Build the number format mask based on the size of the matched values |
|||
$dividend = str_repeat('?', strlen($matches[2])); |
|||
$divisor = str_repeat('?', strlen($matches[3])); |
|||
$fractionMask = "{$dividend}/{$divisor}"; |
|||
// Set style |
|||
$cell->getWorksheet()->getStyle($cell->getCoordinate()) |
|||
->getNumberFormat()->setFormatCode($fractionMask); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
protected function setPercentage(string $value, Cell $cell): bool |
|||
{ |
|||
// Convert value to number |
|||
$value = ((float) str_replace('%', '', $value)) / 100; |
|||
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC); |
|||
|
|||
// Set style |
|||
$cell->getWorksheet()->getStyle($cell->getCoordinate()) |
|||
->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_PERCENTAGE_00); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
protected function setTimeHoursMinutes(string $value, Cell $cell): bool |
|||
{ |
|||
// Convert value to number |
|||
[$hours, $minutes] = explode(':', $value); |
|||
$days = ($hours / 24) + ($minutes / 1440); |
|||
$cell->setValueExplicit($days, DataType::TYPE_NUMERIC); |
|||
|
|||
// Set style |
|||
$cell->getWorksheet()->getStyle($cell->getCoordinate()) |
|||
->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_DATE_TIME3); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
protected function setTimeHoursMinutesSeconds(string $value, Cell $cell): bool |
|||
{ |
|||
// Convert value to number |
|||
[$hours, $minutes, $seconds] = explode(':', $value); |
|||
$days = ($hours / 24) + ($minutes / 1440) + ($seconds / 86400); |
|||
$cell->setValueExplicit($days, DataType::TYPE_NUMERIC); |
|||
|
|||
// Set style |
|||
$cell->getWorksheet()->getStyle($cell->getCoordinate()) |
|||
->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_DATE_TIME4); |
|||
|
|||
return true; |
|||
} |
|||
} |
|||
@ -0,0 +1,732 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Cell; |
|||
|
|||
use DateTime; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
|||
use PhpOffice\PhpSpreadsheet\Collection\Cells; |
|||
use PhpOffice\PhpSpreadsheet\Exception; |
|||
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
|||
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDate; |
|||
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\CellStyleAssessor; |
|||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
|||
use PhpOffice\PhpSpreadsheet\Style\Style; |
|||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
|||
use Throwable; |
|||
|
|||
class Cell |
|||
{ |
|||
/** |
|||
* Value binder to use. |
|||
* |
|||
* @var IValueBinder |
|||
*/ |
|||
private static $valueBinder; |
|||
|
|||
/** |
|||
* Value of the cell. |
|||
* |
|||
* @var mixed |
|||
*/ |
|||
private $value; |
|||
|
|||
/** |
|||
* Calculated value of the cell (used for caching) |
|||
* This returns the value last calculated by MS Excel or whichever spreadsheet program was used to |
|||
* create the original spreadsheet file. |
|||
* Note that this value is not guaranteed to reflect the actual calculated value because it is |
|||
* possible that auto-calculation was disabled in the original spreadsheet, and underlying data |
|||
* values used by the formula have changed since it was last calculated. |
|||
* |
|||
* @var mixed |
|||
*/ |
|||
private $calculatedValue; |
|||
|
|||
/** |
|||
* Type of the cell data. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $dataType; |
|||
|
|||
/** |
|||
* Collection of cells. |
|||
* |
|||
* @var Cells |
|||
*/ |
|||
private $parent; |
|||
|
|||
/** |
|||
* Index to cellXf. |
|||
* |
|||
* @var int |
|||
*/ |
|||
private $xfIndex = 0; |
|||
|
|||
/** |
|||
* Attributes of the formula. |
|||
*/ |
|||
private $formulaAttributes; |
|||
|
|||
/** |
|||
* Update the cell into the cell collection. |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function updateInCollection(): self |
|||
{ |
|||
$this->parent->update($this); |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
public function detach(): void |
|||
{ |
|||
// @phpstan-ignore-next-line |
|||
$this->parent = null; |
|||
} |
|||
|
|||
public function attach(Cells $parent): void |
|||
{ |
|||
$this->parent = $parent; |
|||
} |
|||
|
|||
/** |
|||
* Create a new Cell. |
|||
* |
|||
* @param mixed $value |
|||
* @param string $dataType |
|||
*/ |
|||
public function __construct($value, $dataType, Worksheet $worksheet) |
|||
{ |
|||
// Initialise cell value |
|||
$this->value = $value; |
|||
|
|||
// Set worksheet cache |
|||
$this->parent = $worksheet->getCellCollection(); |
|||
|
|||
// Set datatype? |
|||
if ($dataType !== null) { |
|||
if ($dataType == DataType::TYPE_STRING2) { |
|||
$dataType = DataType::TYPE_STRING; |
|||
} |
|||
$this->dataType = $dataType; |
|||
} elseif (!self::getValueBinder()->bindValue($this, $value)) { |
|||
throw new Exception('Value could not be bound to cell.'); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Get cell coordinate column. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getColumn() |
|||
{ |
|||
return $this->parent->getCurrentColumn(); |
|||
} |
|||
|
|||
/** |
|||
* Get cell coordinate row. |
|||
* |
|||
* @return int |
|||
*/ |
|||
public function getRow() |
|||
{ |
|||
return $this->parent->getCurrentRow(); |
|||
} |
|||
|
|||
/** |
|||
* Get cell coordinate. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getCoordinate() |
|||
{ |
|||
try { |
|||
$coordinate = $this->parent->getCurrentCoordinate(); |
|||
} catch (Throwable $e) { |
|||
$coordinate = null; |
|||
} |
|||
if ($coordinate === null) { |
|||
throw new Exception('Coordinate no longer exists'); |
|||
} |
|||
|
|||
return $coordinate; |
|||
} |
|||
|
|||
/** |
|||
* Get cell value. |
|||
* |
|||
* @return mixed |
|||
*/ |
|||
public function getValue() |
|||
{ |
|||
return $this->value; |
|||
} |
|||
|
|||
/** |
|||
* Get cell value with formatting. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getFormattedValue() |
|||
{ |
|||
return (string) NumberFormat::toFormattedString( |
|||
$this->getCalculatedValue(), |
|||
$this->getStyle() |
|||
->getNumberFormat()->getFormatCode() |
|||
); |
|||
} |
|||
|
|||
/** |
|||
* Set cell value. |
|||
* |
|||
* Sets the value for a cell, automatically determining the datatype using the value binder |
|||
* |
|||
* @param mixed $value Value |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setValue($value) |
|||
{ |
|||
if (!self::getValueBinder()->bindValue($this, $value)) { |
|||
throw new Exception('Value could not be bound to cell.'); |
|||
} |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Set the value for a cell, with the explicit data type passed to the method (bypassing any use of the value binder). |
|||
* |
|||
* @param mixed $value Value |
|||
* @param string $dataType Explicit data type, see DataType::TYPE_* |
|||
* |
|||
* @return Cell |
|||
*/ |
|||
public function setValueExplicit($value, $dataType) |
|||
{ |
|||
// set the value according to data type |
|||
switch ($dataType) { |
|||
case DataType::TYPE_NULL: |
|||
$this->value = $value; |
|||
|
|||
break; |
|||
case DataType::TYPE_STRING2: |
|||
$dataType = DataType::TYPE_STRING; |
|||
// no break |
|||
case DataType::TYPE_STRING: |
|||
// Synonym for string |
|||
case DataType::TYPE_INLINE: |
|||
// Rich text |
|||
$this->value = DataType::checkString($value); |
|||
|
|||
break; |
|||
case DataType::TYPE_NUMERIC: |
|||
if (is_string($value) && !is_numeric($value)) { |
|||
throw new Exception('Invalid numeric value for datatype Numeric'); |
|||
} |
|||
$this->value = 0 + $value; |
|||
|
|||
break; |
|||
case DataType::TYPE_FORMULA: |
|||
$this->value = (string) $value; |
|||
|
|||
break; |
|||
case DataType::TYPE_BOOL: |
|||
$this->value = (bool) $value; |
|||
|
|||
break; |
|||
case DataType::TYPE_ISO_DATE: |
|||
if (!is_string($value)) { |
|||
throw new Exception('Non-string supplied for datatype Date'); |
|||
} |
|||
$date = new DateTime($value); |
|||
$newValue = SharedDate::PHPToExcel($date); |
|||
if ($newValue === false) { |
|||
throw new Exception("Invalid string $value supplied for datatype Date"); |
|||
} |
|||
if (preg_match('/^\\d\\d:\\d\\d:\\d\\d/', $value) == 1) { |
|||
$newValue = fmod($newValue, 1.0); |
|||
} |
|||
$this->value = $newValue; |
|||
$dataType = DataType::TYPE_NUMERIC; |
|||
|
|||
break; |
|||
case DataType::TYPE_ERROR: |
|||
$this->value = DataType::checkErrorCode($value); |
|||
|
|||
break; |
|||
default: |
|||
throw new Exception('Invalid datatype: ' . $dataType); |
|||
|
|||
break; |
|||
} |
|||
|
|||
// set the datatype |
|||
$this->dataType = $dataType; |
|||
|
|||
return $this->updateInCollection(); |
|||
} |
|||
|
|||
/** |
|||
* Get calculated cell value. |
|||
* |
|||
* @param bool $resetLog Whether the calculation engine logger should be reset or not |
|||
* |
|||
* @return mixed |
|||
*/ |
|||
public function getCalculatedValue($resetLog = true) |
|||
{ |
|||
if ($this->dataType == DataType::TYPE_FORMULA) { |
|||
try { |
|||
$index = $this->getWorksheet()->getParent()->getActiveSheetIndex(); |
|||
$selected = $this->getWorksheet()->getSelectedCells(); |
|||
$result = Calculation::getInstance( |
|||
$this->getWorksheet()->getParent() |
|||
)->calculateCellValue($this, $resetLog); |
|||
$this->getWorksheet()->setSelectedCells($selected); |
|||
$this->getWorksheet()->getParent()->setActiveSheetIndex($index); |
|||
// We don't yet handle array returns |
|||
if (is_array($result)) { |
|||
while (is_array($result)) { |
|||
$result = array_shift($result); |
|||
} |
|||
} |
|||
} catch (Exception $ex) { |
|||
if (($ex->getMessage() === 'Unable to access External Workbook') && ($this->calculatedValue !== null)) { |
|||
return $this->calculatedValue; // Fallback for calculations referencing external files. |
|||
} elseif (preg_match('/[Uu]ndefined (name|offset: 2|array key 2)/', $ex->getMessage()) === 1) { |
|||
return \PhpOffice\PhpSpreadsheet\Calculation\Functions::NAME(); |
|||
} |
|||
|
|||
throw new \PhpOffice\PhpSpreadsheet\Calculation\Exception( |
|||
$this->getWorksheet()->getTitle() . '!' . $this->getCoordinate() . ' -> ' . $ex->getMessage() |
|||
); |
|||
} |
|||
|
|||
if ($result === '#Not Yet Implemented') { |
|||
return $this->calculatedValue; // Fallback if calculation engine does not support the formula. |
|||
} |
|||
|
|||
return $result; |
|||
} elseif ($this->value instanceof RichText) { |
|||
return $this->value->getPlainText(); |
|||
} |
|||
|
|||
return $this->value; |
|||
} |
|||
|
|||
/** |
|||
* Set old calculated value (cached). |
|||
* |
|||
* @param mixed $originalValue Value |
|||
* |
|||
* @return Cell |
|||
*/ |
|||
public function setCalculatedValue($originalValue) |
|||
{ |
|||
if ($originalValue !== null) { |
|||
$this->calculatedValue = (is_numeric($originalValue)) ? (float) $originalValue : $originalValue; |
|||
} |
|||
|
|||
return $this->updateInCollection(); |
|||
} |
|||
|
|||
/** |
|||
* Get old calculated value (cached) |
|||
* This returns the value last calculated by MS Excel or whichever spreadsheet program was used to |
|||
* create the original spreadsheet file. |
|||
* Note that this value is not guaranteed to reflect the actual calculated value because it is |
|||
* possible that auto-calculation was disabled in the original spreadsheet, and underlying data |
|||
* values used by the formula have changed since it was last calculated. |
|||
* |
|||
* @return mixed |
|||
*/ |
|||
public function getOldCalculatedValue() |
|||
{ |
|||
return $this->calculatedValue; |
|||
} |
|||
|
|||
/** |
|||
* Get cell data type. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getDataType() |
|||
{ |
|||
return $this->dataType; |
|||
} |
|||
|
|||
/** |
|||
* Set cell data type. |
|||
* |
|||
* @param string $dataType see DataType::TYPE_* |
|||
* |
|||
* @return Cell |
|||
*/ |
|||
public function setDataType($dataType) |
|||
{ |
|||
if ($dataType == DataType::TYPE_STRING2) { |
|||
$dataType = DataType::TYPE_STRING; |
|||
} |
|||
$this->dataType = $dataType; |
|||
|
|||
return $this->updateInCollection(); |
|||
} |
|||
|
|||
/** |
|||
* Identify if the cell contains a formula. |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function isFormula() |
|||
{ |
|||
return $this->dataType == DataType::TYPE_FORMULA; |
|||
} |
|||
|
|||
/** |
|||
* Does this cell contain Data validation rules? |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function hasDataValidation() |
|||
{ |
|||
if (!isset($this->parent)) { |
|||
throw new Exception('Cannot check for data validation when cell is not bound to a worksheet'); |
|||
} |
|||
|
|||
return $this->getWorksheet()->dataValidationExists($this->getCoordinate()); |
|||
} |
|||
|
|||
/** |
|||
* Get Data validation rules. |
|||
* |
|||
* @return DataValidation |
|||
*/ |
|||
public function getDataValidation() |
|||
{ |
|||
if (!isset($this->parent)) { |
|||
throw new Exception('Cannot get data validation for cell that is not bound to a worksheet'); |
|||
} |
|||
|
|||
return $this->getWorksheet()->getDataValidation($this->getCoordinate()); |
|||
} |
|||
|
|||
/** |
|||
* Set Data validation rules. |
|||
*/ |
|||
public function setDataValidation(?DataValidation $dataValidation = null): self |
|||
{ |
|||
if (!isset($this->parent)) { |
|||
throw new Exception('Cannot set data validation for cell that is not bound to a worksheet'); |
|||
} |
|||
|
|||
$this->getWorksheet()->setDataValidation($this->getCoordinate(), $dataValidation); |
|||
|
|||
return $this->updateInCollection(); |
|||
} |
|||
|
|||
/** |
|||
* Does this cell contain valid value? |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function hasValidValue() |
|||
{ |
|||
$validator = new DataValidator(); |
|||
|
|||
return $validator->isValid($this); |
|||
} |
|||
|
|||
/** |
|||
* Does this cell contain a Hyperlink? |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function hasHyperlink() |
|||
{ |
|||
if (!isset($this->parent)) { |
|||
throw new Exception('Cannot check for hyperlink when cell is not bound to a worksheet'); |
|||
} |
|||
|
|||
return $this->getWorksheet()->hyperlinkExists($this->getCoordinate()); |
|||
} |
|||
|
|||
/** |
|||
* Get Hyperlink. |
|||
* |
|||
* @return Hyperlink |
|||
*/ |
|||
public function getHyperlink() |
|||
{ |
|||
if (!isset($this->parent)) { |
|||
throw new Exception('Cannot get hyperlink for cell that is not bound to a worksheet'); |
|||
} |
|||
|
|||
return $this->getWorksheet()->getHyperlink($this->getCoordinate()); |
|||
} |
|||
|
|||
/** |
|||
* Set Hyperlink. |
|||
* |
|||
* @return Cell |
|||
*/ |
|||
public function setHyperlink(?Hyperlink $hyperlink = null) |
|||
{ |
|||
if (!isset($this->parent)) { |
|||
throw new Exception('Cannot set hyperlink for cell that is not bound to a worksheet'); |
|||
} |
|||
|
|||
$this->getWorksheet()->setHyperlink($this->getCoordinate(), $hyperlink); |
|||
|
|||
return $this->updateInCollection(); |
|||
} |
|||
|
|||
/** |
|||
* Get cell collection. |
|||
* |
|||
* @return Cells |
|||
*/ |
|||
public function getParent() |
|||
{ |
|||
return $this->parent; |
|||
} |
|||
|
|||
/** |
|||
* Get parent worksheet. |
|||
* |
|||
* @return Worksheet |
|||
*/ |
|||
public function getWorksheet() |
|||
{ |
|||
try { |
|||
$worksheet = $this->parent->getParent(); |
|||
} catch (Throwable $e) { |
|||
$worksheet = null; |
|||
} |
|||
|
|||
if ($worksheet === null) { |
|||
throw new Exception('Worksheet no longer exists'); |
|||
} |
|||
|
|||
return $worksheet; |
|||
} |
|||
|
|||
/** |
|||
* Is this cell in a merge range. |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function isInMergeRange() |
|||
{ |
|||
return (bool) $this->getMergeRange(); |
|||
} |
|||
|
|||
/** |
|||
* Is this cell the master (top left cell) in a merge range (that holds the actual data value). |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function isMergeRangeValueCell() |
|||
{ |
|||
if ($mergeRange = $this->getMergeRange()) { |
|||
$mergeRange = Coordinate::splitRange($mergeRange); |
|||
[$startCell] = $mergeRange[0]; |
|||
if ($this->getCoordinate() === $startCell) { |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* If this cell is in a merge range, then return the range. |
|||
* |
|||
* @return false|string |
|||
*/ |
|||
public function getMergeRange() |
|||
{ |
|||
foreach ($this->getWorksheet()->getMergeCells() as $mergeRange) { |
|||
if ($this->isInRange($mergeRange)) { |
|||
return $mergeRange; |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* Get cell style. |
|||
*/ |
|||
public function getStyle(): Style |
|||
{ |
|||
return $this->getWorksheet()->getStyle($this->getCoordinate()); |
|||
} |
|||
|
|||
/** |
|||
* Get cell style. |
|||
*/ |
|||
public function getAppliedStyle(): Style |
|||
{ |
|||
if ($this->getWorksheet()->conditionalStylesExists($this->getCoordinate()) === false) { |
|||
return $this->getStyle(); |
|||
} |
|||
$range = $this->getWorksheet()->getConditionalRange($this->getCoordinate()); |
|||
if ($range === null) { |
|||
return $this->getStyle(); |
|||
} |
|||
|
|||
$matcher = new CellStyleAssessor($this, $range); |
|||
|
|||
return $matcher->matchConditions($this->getWorksheet()->getConditionalStyles($this->getCoordinate())); |
|||
} |
|||
|
|||
/** |
|||
* Re-bind parent. |
|||
* |
|||
* @return Cell |
|||
*/ |
|||
public function rebindParent(Worksheet $parent) |
|||
{ |
|||
$this->parent = $parent->getCellCollection(); |
|||
|
|||
return $this->updateInCollection(); |
|||
} |
|||
|
|||
/** |
|||
* Is cell in a specific range? |
|||
* |
|||
* @param string $range Cell range (e.g. A1:A1) |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function isInRange($range) |
|||
{ |
|||
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range); |
|||
|
|||
// Translate properties |
|||
$myColumn = Coordinate::columnIndexFromString($this->getColumn()); |
|||
$myRow = $this->getRow(); |
|||
|
|||
// Verify if cell is in range |
|||
return ($rangeStart[0] <= $myColumn) && ($rangeEnd[0] >= $myColumn) && |
|||
($rangeStart[1] <= $myRow) && ($rangeEnd[1] >= $myRow); |
|||
} |
|||
|
|||
/** |
|||
* Compare 2 cells. |
|||
* |
|||
* @param Cell $a Cell a |
|||
* @param Cell $b Cell b |
|||
* |
|||
* @return int Result of comparison (always -1 or 1, never zero!) |
|||
*/ |
|||
public static function compareCells(self $a, self $b) |
|||
{ |
|||
if ($a->getRow() < $b->getRow()) { |
|||
return -1; |
|||
} elseif ($a->getRow() > $b->getRow()) { |
|||
return 1; |
|||
} elseif (Coordinate::columnIndexFromString($a->getColumn()) < Coordinate::columnIndexFromString($b->getColumn())) { |
|||
return -1; |
|||
} |
|||
|
|||
return 1; |
|||
} |
|||
|
|||
/** |
|||
* Get value binder to use. |
|||
* |
|||
* @return IValueBinder |
|||
*/ |
|||
public static function getValueBinder() |
|||
{ |
|||
if (self::$valueBinder === null) { |
|||
self::$valueBinder = new DefaultValueBinder(); |
|||
} |
|||
|
|||
return self::$valueBinder; |
|||
} |
|||
|
|||
/** |
|||
* Set value binder to use. |
|||
*/ |
|||
public static function setValueBinder(IValueBinder $binder): void |
|||
{ |
|||
self::$valueBinder = $binder; |
|||
} |
|||
|
|||
/** |
|||
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
|||
*/ |
|||
public function __clone() |
|||
{ |
|||
$vars = get_object_vars($this); |
|||
foreach ($vars as $key => $value) { |
|||
if ((is_object($value)) && ($key != 'parent')) { |
|||
$this->$key = clone $value; |
|||
} else { |
|||
$this->$key = $value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Get index to cellXf. |
|||
* |
|||
* @return int |
|||
*/ |
|||
public function getXfIndex() |
|||
{ |
|||
return $this->xfIndex; |
|||
} |
|||
|
|||
/** |
|||
* Set index to cellXf. |
|||
* |
|||
* @param int $indexValue |
|||
* |
|||
* @return Cell |
|||
*/ |
|||
public function setXfIndex($indexValue) |
|||
{ |
|||
$this->xfIndex = $indexValue; |
|||
|
|||
return $this->updateInCollection(); |
|||
} |
|||
|
|||
/** |
|||
* Set the formula attributes. |
|||
* |
|||
* @param mixed $attributes |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setFormulaAttributes($attributes) |
|||
{ |
|||
$this->formulaAttributes = $attributes; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get the formula attributes. |
|||
*/ |
|||
public function getFormulaAttributes() |
|||
{ |
|||
return $this->formulaAttributes; |
|||
} |
|||
|
|||
/** |
|||
* Convert to string. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function __toString() |
|||
{ |
|||
return (string) $this->getValue(); |
|||
} |
|||
} |
|||
@ -0,0 +1,570 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Cell; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Exception; |
|||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
|||
|
|||
/** |
|||
* Helper class to manipulate cell coordinates. |
|||
* |
|||
* Columns indexes and rows are always based on 1, **not** on 0. This match the behavior |
|||
* that Excel users are used to, and also match the Excel functions `COLUMN()` and `ROW()`. |
|||
*/ |
|||
abstract class Coordinate |
|||
{ |
|||
public const A1_COORDINATE_REGEX = '/^(?<absolute_col>\$?)(?<col_ref>[A-Z]{1,3})(?<absolute_row>\$?)(?<row_ref>\d{1,7})$/i'; |
|||
|
|||
/** |
|||
* Default range variable constant. |
|||
* |
|||
* @var string |
|||
*/ |
|||
const DEFAULT_RANGE = 'A1:A1'; |
|||
|
|||
/** |
|||
* Coordinate from string. |
|||
* |
|||
* @param string $cellAddress eg: 'A1' |
|||
* |
|||
* @return array{0: string, 1: string} Array containing column and row (indexes 0 and 1) |
|||
*/ |
|||
public static function coordinateFromString($cellAddress) |
|||
{ |
|||
if (preg_match(self::A1_COORDINATE_REGEX, $cellAddress, $matches)) { |
|||
return [$matches['absolute_col'] . $matches['col_ref'], $matches['absolute_row'] . $matches['row_ref']]; |
|||
} elseif (self::coordinateIsRange($cellAddress)) { |
|||
throw new Exception('Cell coordinate string can not be a range of cells'); |
|||
} elseif ($cellAddress == '') { |
|||
throw new Exception('Cell coordinate can not be zero-length string'); |
|||
} |
|||
|
|||
throw new Exception('Invalid cell coordinate ' . $cellAddress); |
|||
} |
|||
|
|||
/** |
|||
* Get indexes from a string coordinates. |
|||
* |
|||
* @param string $coordinates eg: 'A1', '$B$12' |
|||
* |
|||
* @return array{0: int, 1: int} Array containing column index and row index (indexes 0 and 1) |
|||
*/ |
|||
public static function indexesFromString(string $coordinates): array |
|||
{ |
|||
[$col, $row] = self::coordinateFromString($coordinates); |
|||
|
|||
return [ |
|||
self::columnIndexFromString(ltrim($col, '$')), |
|||
(int) ltrim($row, '$'), |
|||
]; |
|||
} |
|||
|
|||
/** |
|||
* Checks if a Cell Address represents a range of cells. |
|||
* |
|||
* @param string $cellAddress eg: 'A1' or 'A1:A2' or 'A1:A2,C1:C2' |
|||
* |
|||
* @return bool Whether the coordinate represents a range of cells |
|||
*/ |
|||
public static function coordinateIsRange($cellAddress) |
|||
{ |
|||
return (strpos($cellAddress, ':') !== false) || (strpos($cellAddress, ',') !== false); |
|||
} |
|||
|
|||
/** |
|||
* Make string row, column or cell coordinate absolute. |
|||
* |
|||
* @param string $cellAddress e.g. 'A' or '1' or 'A1' |
|||
* Note that this value can be a row or column reference as well as a cell reference |
|||
* |
|||
* @return string Absolute coordinate e.g. '$A' or '$1' or '$A$1' |
|||
*/ |
|||
public static function absoluteReference($cellAddress) |
|||
{ |
|||
if (self::coordinateIsRange($cellAddress)) { |
|||
throw new Exception('Cell coordinate string can not be a range of cells'); |
|||
} |
|||
|
|||
// Split out any worksheet name from the reference |
|||
[$worksheet, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true); |
|||
if ($worksheet > '') { |
|||
$worksheet .= '!'; |
|||
} |
|||
|
|||
// Create absolute coordinate |
|||
if (ctype_digit($cellAddress)) { |
|||
return $worksheet . '$' . $cellAddress; |
|||
} elseif (ctype_alpha($cellAddress)) { |
|||
return $worksheet . '$' . strtoupper($cellAddress); |
|||
} |
|||
|
|||
return $worksheet . self::absoluteCoordinate($cellAddress); |
|||
} |
|||
|
|||
/** |
|||
* Make string coordinate absolute. |
|||
* |
|||
* @param string $cellAddress e.g. 'A1' |
|||
* |
|||
* @return string Absolute coordinate e.g. '$A$1' |
|||
*/ |
|||
public static function absoluteCoordinate($cellAddress) |
|||
{ |
|||
if (self::coordinateIsRange($cellAddress)) { |
|||
throw new Exception('Cell coordinate string can not be a range of cells'); |
|||
} |
|||
|
|||
// Split out any worksheet name from the coordinate |
|||
[$worksheet, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true); |
|||
if ($worksheet > '') { |
|||
$worksheet .= '!'; |
|||
} |
|||
|
|||
// Create absolute coordinate |
|||
[$column, $row] = self::coordinateFromString($cellAddress); |
|||
$column = ltrim($column, '$'); |
|||
$row = ltrim($row, '$'); |
|||
|
|||
return $worksheet . '$' . $column . '$' . $row; |
|||
} |
|||
|
|||
/** |
|||
* Split range into coordinate strings. |
|||
* |
|||
* @param string $range e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4' |
|||
* |
|||
* @return array Array containing one or more arrays containing one or two coordinate strings |
|||
* e.g. ['B4','D9'] or [['B4','D9'], ['H2','O11']] |
|||
* or ['B4'] |
|||
*/ |
|||
public static function splitRange($range) |
|||
{ |
|||
// Ensure $pRange is a valid range |
|||
if (empty($range)) { |
|||
$range = self::DEFAULT_RANGE; |
|||
} |
|||
|
|||
$exploded = explode(',', $range); |
|||
$counter = count($exploded); |
|||
for ($i = 0; $i < $counter; ++$i) { |
|||
$exploded[$i] = explode(':', $exploded[$i]); |
|||
} |
|||
|
|||
return $exploded; |
|||
} |
|||
|
|||
/** |
|||
* Build range from coordinate strings. |
|||
* |
|||
* @param array $range Array containing one or more arrays containing one or two coordinate strings |
|||
* |
|||
* @return string String representation of $pRange |
|||
*/ |
|||
public static function buildRange(array $range) |
|||
{ |
|||
// Verify range |
|||
if (empty($range) || !is_array($range[0])) { |
|||
throw new Exception('Range does not contain any information'); |
|||
} |
|||
|
|||
// Build range |
|||
$counter = count($range); |
|||
for ($i = 0; $i < $counter; ++$i) { |
|||
$range[$i] = implode(':', $range[$i]); |
|||
} |
|||
|
|||
return implode(',', $range); |
|||
} |
|||
|
|||
/** |
|||
* Calculate range boundaries. |
|||
* |
|||
* @param string $range Cell range (e.g. A1:A1) |
|||
* |
|||
* @return array Range coordinates [Start Cell, End Cell] |
|||
* where Start Cell and End Cell are arrays (Column Number, Row Number) |
|||
*/ |
|||
public static function rangeBoundaries($range) |
|||
{ |
|||
// Ensure $pRange is a valid range |
|||
if (empty($range)) { |
|||
$range = self::DEFAULT_RANGE; |
|||
} |
|||
|
|||
// Uppercase coordinate |
|||
$range = strtoupper($range); |
|||
|
|||
// Extract range |
|||
if (strpos($range, ':') === false) { |
|||
$rangeA = $rangeB = $range; |
|||
} else { |
|||
[$rangeA, $rangeB] = explode(':', $range); |
|||
} |
|||
|
|||
// Calculate range outer borders |
|||
$rangeStart = self::coordinateFromString($rangeA); |
|||
$rangeEnd = self::coordinateFromString($rangeB); |
|||
|
|||
// Translate column into index |
|||
$rangeStart[0] = self::columnIndexFromString($rangeStart[0]); |
|||
$rangeEnd[0] = self::columnIndexFromString($rangeEnd[0]); |
|||
|
|||
return [$rangeStart, $rangeEnd]; |
|||
} |
|||
|
|||
/** |
|||
* Calculate range dimension. |
|||
* |
|||
* @param string $range Cell range (e.g. A1:A1) |
|||
* |
|||
* @return array Range dimension (width, height) |
|||
*/ |
|||
public static function rangeDimension($range) |
|||
{ |
|||
// Calculate range outer borders |
|||
[$rangeStart, $rangeEnd] = self::rangeBoundaries($range); |
|||
|
|||
return [($rangeEnd[0] - $rangeStart[0] + 1), ($rangeEnd[1] - $rangeStart[1] + 1)]; |
|||
} |
|||
|
|||
/** |
|||
* Calculate range boundaries. |
|||
* |
|||
* @param string $range Cell range (e.g. A1:A1) |
|||
* |
|||
* @return array Range coordinates [Start Cell, End Cell] |
|||
* where Start Cell and End Cell are arrays [Column ID, Row Number] |
|||
*/ |
|||
public static function getRangeBoundaries($range) |
|||
{ |
|||
// Ensure $pRange is a valid range |
|||
if (empty($range)) { |
|||
$range = self::DEFAULT_RANGE; |
|||
} |
|||
|
|||
// Uppercase coordinate |
|||
$range = strtoupper($range); |
|||
|
|||
// Extract range |
|||
if (strpos($range, ':') === false) { |
|||
$rangeA = $rangeB = $range; |
|||
} else { |
|||
[$rangeA, $rangeB] = explode(':', $range); |
|||
} |
|||
|
|||
return [self::coordinateFromString($rangeA), self::coordinateFromString($rangeB)]; |
|||
} |
|||
|
|||
/** |
|||
* Column index from string. |
|||
* |
|||
* @param string $columnAddress eg 'A' |
|||
* |
|||
* @return int Column index (A = 1) |
|||
*/ |
|||
public static function columnIndexFromString($columnAddress) |
|||
{ |
|||
// Using a lookup cache adds a slight memory overhead, but boosts speed |
|||
// caching using a static within the method is faster than a class static, |
|||
// though it's additional memory overhead |
|||
static $indexCache = []; |
|||
|
|||
if (isset($indexCache[$columnAddress])) { |
|||
return $indexCache[$columnAddress]; |
|||
} |
|||
// It's surprising how costly the strtoupper() and ord() calls actually are, so we use a lookup array rather than use ord() |
|||
// and make it case insensitive to get rid of the strtoupper() as well. Because it's a static, there's no significant |
|||
// memory overhead either |
|||
static $columnLookup = [ |
|||
'A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7, 'H' => 8, 'I' => 9, 'J' => 10, 'K' => 11, 'L' => 12, 'M' => 13, |
|||
'N' => 14, 'O' => 15, 'P' => 16, 'Q' => 17, 'R' => 18, 'S' => 19, 'T' => 20, 'U' => 21, 'V' => 22, 'W' => 23, 'X' => 24, 'Y' => 25, 'Z' => 26, |
|||
'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10, 'k' => 11, 'l' => 12, 'm' => 13, |
|||
'n' => 14, 'o' => 15, 'p' => 16, 'q' => 17, 'r' => 18, 's' => 19, 't' => 20, 'u' => 21, 'v' => 22, 'w' => 23, 'x' => 24, 'y' => 25, 'z' => 26, |
|||
]; |
|||
|
|||
// We also use the language construct isset() rather than the more costly strlen() function to match the length of $columnAddress |
|||
// for improved performance |
|||
if (isset($columnAddress[0])) { |
|||
if (!isset($columnAddress[1])) { |
|||
$indexCache[$columnAddress] = $columnLookup[$columnAddress]; |
|||
|
|||
return $indexCache[$columnAddress]; |
|||
} elseif (!isset($columnAddress[2])) { |
|||
$indexCache[$columnAddress] = $columnLookup[$columnAddress[0]] * 26 + $columnLookup[$columnAddress[1]]; |
|||
|
|||
return $indexCache[$columnAddress]; |
|||
} elseif (!isset($columnAddress[3])) { |
|||
$indexCache[$columnAddress] = $columnLookup[$columnAddress[0]] * 676 + $columnLookup[$columnAddress[1]] * 26 + $columnLookup[$columnAddress[2]]; |
|||
|
|||
return $indexCache[$columnAddress]; |
|||
} |
|||
} |
|||
|
|||
throw new Exception('Column string index can not be ' . ((isset($columnAddress[0])) ? 'longer than 3 characters' : 'empty')); |
|||
} |
|||
|
|||
/** |
|||
* String from column index. |
|||
* |
|||
* @param int $columnIndex Column index (A = 1) |
|||
* |
|||
* @return string |
|||
*/ |
|||
public static function stringFromColumnIndex($columnIndex) |
|||
{ |
|||
static $indexCache = []; |
|||
|
|||
if (!isset($indexCache[$columnIndex])) { |
|||
$indexValue = $columnIndex; |
|||
$base26 = null; |
|||
do { |
|||
$characterValue = ($indexValue % 26) ?: 26; |
|||
$indexValue = ($indexValue - $characterValue) / 26; |
|||
$base26 = chr($characterValue + 64) . ($base26 ?: ''); |
|||
} while ($indexValue > 0); |
|||
$indexCache[$columnIndex] = $base26; |
|||
} |
|||
|
|||
return $indexCache[$columnIndex]; |
|||
} |
|||
|
|||
/** |
|||
* Extract all cell references in range, which may be comprised of multiple cell ranges. |
|||
* |
|||
* @param string $cellRange Range: e.g. 'A1' or 'A1:C10' or 'A1:E10,A20:E25' or 'A1:E5 C3:G7' or 'A1:C1,A3:C3 B1:C3' |
|||
* |
|||
* @return array Array containing single cell references |
|||
*/ |
|||
public static function extractAllCellReferencesInRange($cellRange): array |
|||
{ |
|||
[$ranges, $operators] = self::getCellBlocksFromRangeString($cellRange); |
|||
|
|||
$cells = []; |
|||
foreach ($ranges as $range) { |
|||
$cells[] = self::getReferencesForCellBlock($range); |
|||
} |
|||
|
|||
$cells = self::processRangeSetOperators($operators, $cells); |
|||
|
|||
if (empty($cells)) { |
|||
return []; |
|||
} |
|||
|
|||
$cellList = array_merge(...$cells); |
|||
$cellList = self::sortCellReferenceArray($cellList); |
|||
|
|||
return $cellList; |
|||
} |
|||
|
|||
private static function processRangeSetOperators(array $operators, array $cells): array |
|||
{ |
|||
$operatorCount = count($operators); |
|||
for ($offset = 0; $offset < $operatorCount; ++$offset) { |
|||
$operator = $operators[$offset]; |
|||
if ($operator !== ' ') { |
|||
continue; |
|||
} |
|||
|
|||
$cells[$offset] = array_intersect($cells[$offset], $cells[$offset + 1]); |
|||
unset($operators[$offset], $cells[$offset + 1]); |
|||
$operators = array_values($operators); |
|||
$cells = array_values($cells); |
|||
--$offset; |
|||
--$operatorCount; |
|||
} |
|||
|
|||
return $cells; |
|||
} |
|||
|
|||
private static function sortCellReferenceArray(array $cellList): array |
|||
{ |
|||
// Sort the result by column and row |
|||
$sortKeys = []; |
|||
foreach ($cellList as $coord) { |
|||
[$column, $row] = sscanf($coord, '%[A-Z]%d'); |
|||
$sortKeys[sprintf('%3s%09d', $column, $row)] = $coord; |
|||
} |
|||
ksort($sortKeys); |
|||
|
|||
return array_values($sortKeys); |
|||
} |
|||
|
|||
/** |
|||
* Get all cell references for an individual cell block. |
|||
* |
|||
* @param string $cellBlock A cell range e.g. A4:B5 |
|||
* |
|||
* @return array All individual cells in that range |
|||
*/ |
|||
private static function getReferencesForCellBlock($cellBlock) |
|||
{ |
|||
$returnValue = []; |
|||
|
|||
// Single cell? |
|||
if (!self::coordinateIsRange($cellBlock)) { |
|||
return (array) $cellBlock; |
|||
} |
|||
|
|||
// Range... |
|||
$ranges = self::splitRange($cellBlock); |
|||
foreach ($ranges as $range) { |
|||
// Single cell? |
|||
if (!isset($range[1])) { |
|||
$returnValue[] = $range[0]; |
|||
|
|||
continue; |
|||
} |
|||
|
|||
// Range... |
|||
[$rangeStart, $rangeEnd] = $range; |
|||
[$startColumn, $startRow] = self::coordinateFromString($rangeStart); |
|||
[$endColumn, $endRow] = self::coordinateFromString($rangeEnd); |
|||
$startColumnIndex = self::columnIndexFromString($startColumn); |
|||
$endColumnIndex = self::columnIndexFromString($endColumn); |
|||
++$endColumnIndex; |
|||
|
|||
// Current data |
|||
$currentColumnIndex = $startColumnIndex; |
|||
$currentRow = $startRow; |
|||
|
|||
self::validateRange($cellBlock, $startColumnIndex, $endColumnIndex, $currentRow, $endRow); |
|||
|
|||
// Loop cells |
|||
while ($currentColumnIndex < $endColumnIndex) { |
|||
while ($currentRow <= $endRow) { |
|||
$returnValue[] = self::stringFromColumnIndex($currentColumnIndex) . $currentRow; |
|||
++$currentRow; |
|||
} |
|||
++$currentColumnIndex; |
|||
$currentRow = $startRow; |
|||
} |
|||
} |
|||
|
|||
return $returnValue; |
|||
} |
|||
|
|||
/** |
|||
* Convert an associative array of single cell coordinates to values to an associative array |
|||
* of cell ranges to values. Only adjacent cell coordinates with the same |
|||
* value will be merged. If the value is an object, it must implement the method getHashCode(). |
|||
* |
|||
* For example, this function converts: |
|||
* |
|||
* [ 'A1' => 'x', 'A2' => 'x', 'A3' => 'x', 'A4' => 'y' ] |
|||
* |
|||
* to: |
|||
* |
|||
* [ 'A1:A3' => 'x', 'A4' => 'y' ] |
|||
* |
|||
* @param array $coordinateCollection associative array mapping coordinates to values |
|||
* |
|||
* @return array associative array mapping coordinate ranges to valuea |
|||
*/ |
|||
public static function mergeRangesInCollection(array $coordinateCollection) |
|||
{ |
|||
$hashedValues = []; |
|||
$mergedCoordCollection = []; |
|||
|
|||
foreach ($coordinateCollection as $coord => $value) { |
|||
if (self::coordinateIsRange($coord)) { |
|||
$mergedCoordCollection[$coord] = $value; |
|||
|
|||
continue; |
|||
} |
|||
|
|||
[$column, $row] = self::coordinateFromString($coord); |
|||
$row = (int) (ltrim($row, '$')); |
|||
$hashCode = $column . '-' . (is_object($value) ? $value->getHashCode() : $value); |
|||
|
|||
if (!isset($hashedValues[$hashCode])) { |
|||
$hashedValues[$hashCode] = (object) [ |
|||
'value' => $value, |
|||
'col' => $column, |
|||
'rows' => [$row], |
|||
]; |
|||
} else { |
|||
$hashedValues[$hashCode]->rows[] = $row; |
|||
} |
|||
} |
|||
|
|||
ksort($hashedValues); |
|||
|
|||
foreach ($hashedValues as $hashedValue) { |
|||
sort($hashedValue->rows); |
|||
$rowStart = null; |
|||
$rowEnd = null; |
|||
$ranges = []; |
|||
|
|||
foreach ($hashedValue->rows as $row) { |
|||
if ($rowStart === null) { |
|||
$rowStart = $row; |
|||
$rowEnd = $row; |
|||
} elseif ($rowEnd === $row - 1) { |
|||
$rowEnd = $row; |
|||
} else { |
|||
if ($rowStart == $rowEnd) { |
|||
$ranges[] = $hashedValue->col . $rowStart; |
|||
} else { |
|||
$ranges[] = $hashedValue->col . $rowStart . ':' . $hashedValue->col . $rowEnd; |
|||
} |
|||
|
|||
$rowStart = $row; |
|||
$rowEnd = $row; |
|||
} |
|||
} |
|||
|
|||
if ($rowStart !== null) { |
|||
if ($rowStart == $rowEnd) { |
|||
$ranges[] = $hashedValue->col . $rowStart; |
|||
} else { |
|||
$ranges[] = $hashedValue->col . $rowStart . ':' . $hashedValue->col . $rowEnd; |
|||
} |
|||
} |
|||
|
|||
foreach ($ranges as $range) { |
|||
$mergedCoordCollection[$range] = $hashedValue->value; |
|||
} |
|||
} |
|||
|
|||
return $mergedCoordCollection; |
|||
} |
|||
|
|||
/** |
|||
* Get the individual cell blocks from a range string, removing any $ characters. |
|||
* then splitting by operators and returning an array with ranges and operators. |
|||
* |
|||
* @param string $rangeString |
|||
* |
|||
* @return array[] |
|||
*/ |
|||
private static function getCellBlocksFromRangeString($rangeString) |
|||
{ |
|||
$rangeString = str_replace('$', '', strtoupper($rangeString)); |
|||
|
|||
// split range sets on intersection (space) or union (,) operators |
|||
$tokens = preg_split('/([ ,])/', $rangeString, -1, PREG_SPLIT_DELIM_CAPTURE); |
|||
// separate the range sets and the operators into arrays |
|||
$split = array_chunk($tokens, 2); |
|||
$ranges = array_column($split, 0); |
|||
$operators = array_column($split, 1); |
|||
|
|||
return [$ranges, $operators]; |
|||
} |
|||
|
|||
/** |
|||
* Check that the given range is valid, i.e. that the start column and row are not greater than the end column and |
|||
* row. |
|||
* |
|||
* @param string $cellBlock The original range, for displaying a meaningful error message |
|||
* @param int $startColumnIndex |
|||
* @param int $endColumnIndex |
|||
* @param int $currentRow |
|||
* @param int $endRow |
|||
*/ |
|||
private static function validateRange($cellBlock, $startColumnIndex, $endColumnIndex, $currentRow, $endRow): void |
|||
{ |
|||
if ($startColumnIndex >= $endColumnIndex || $currentRow > $endRow) { |
|||
throw new Exception('Invalid range: "' . $cellBlock . '"'); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,86 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Cell; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
|||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
|||
|
|||
class DataType |
|||
{ |
|||
// Data types |
|||
const TYPE_STRING2 = 'str'; |
|||
const TYPE_STRING = 's'; |
|||
const TYPE_FORMULA = 'f'; |
|||
const TYPE_NUMERIC = 'n'; |
|||
const TYPE_BOOL = 'b'; |
|||
const TYPE_NULL = 'null'; |
|||
const TYPE_INLINE = 'inlineStr'; |
|||
const TYPE_ERROR = 'e'; |
|||
const TYPE_ISO_DATE = 'd'; |
|||
|
|||
/** |
|||
* List of error codes. |
|||
* |
|||
* @var array |
|||
*/ |
|||
private static $errorCodes = [ |
|||
'#NULL!' => 0, |
|||
'#DIV/0!' => 1, |
|||
'#VALUE!' => 2, |
|||
'#REF!' => 3, |
|||
'#NAME?' => 4, |
|||
'#NUM!' => 5, |
|||
'#N/A' => 6, |
|||
]; |
|||
|
|||
/** |
|||
* Get list of error codes. |
|||
* |
|||
* @return array |
|||
*/ |
|||
public static function getErrorCodes() |
|||
{ |
|||
return self::$errorCodes; |
|||
} |
|||
|
|||
/** |
|||
* Check a string that it satisfies Excel requirements. |
|||
* |
|||
* @param null|RichText|string $textValue Value to sanitize to an Excel string |
|||
* |
|||
* @return null|RichText|string Sanitized value |
|||
*/ |
|||
public static function checkString($textValue) |
|||
{ |
|||
if ($textValue instanceof RichText) { |
|||
// TODO: Sanitize Rich-Text string (max. character count is 32,767) |
|||
return $textValue; |
|||
} |
|||
|
|||
// string must never be longer than 32,767 characters, truncate if necessary |
|||
$textValue = StringHelper::substring($textValue, 0, 32767); |
|||
|
|||
// we require that newline is represented as "\n" in core, not as "\r\n" or "\r" |
|||
$textValue = str_replace(["\r\n", "\r"], "\n", $textValue); |
|||
|
|||
return $textValue; |
|||
} |
|||
|
|||
/** |
|||
* Check a value that it is a valid error code. |
|||
* |
|||
* @param mixed $value Value to sanitize to an Excel error code |
|||
* |
|||
* @return string Sanitized value |
|||
*/ |
|||
public static function checkErrorCode($value) |
|||
{ |
|||
$value = (string) $value; |
|||
|
|||
if (!isset(self::$errorCodes[$value])) { |
|||
$value = '#NULL!'; |
|||
} |
|||
|
|||
return $value; |
|||
} |
|||
} |
|||
@ -0,0 +1,497 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Cell; |
|||
|
|||
class DataValidation |
|||
{ |
|||
// Data validation types |
|||
const TYPE_NONE = 'none'; |
|||
const TYPE_CUSTOM = 'custom'; |
|||
const TYPE_DATE = 'date'; |
|||
const TYPE_DECIMAL = 'decimal'; |
|||
const TYPE_LIST = 'list'; |
|||
const TYPE_TEXTLENGTH = 'textLength'; |
|||
const TYPE_TIME = 'time'; |
|||
const TYPE_WHOLE = 'whole'; |
|||
|
|||
// Data validation error styles |
|||
const STYLE_STOP = 'stop'; |
|||
const STYLE_WARNING = 'warning'; |
|||
const STYLE_INFORMATION = 'information'; |
|||
|
|||
// Data validation operators |
|||
const OPERATOR_BETWEEN = 'between'; |
|||
const OPERATOR_EQUAL = 'equal'; |
|||
const OPERATOR_GREATERTHAN = 'greaterThan'; |
|||
const OPERATOR_GREATERTHANOREQUAL = 'greaterThanOrEqual'; |
|||
const OPERATOR_LESSTHAN = 'lessThan'; |
|||
const OPERATOR_LESSTHANOREQUAL = 'lessThanOrEqual'; |
|||
const OPERATOR_NOTBETWEEN = 'notBetween'; |
|||
const OPERATOR_NOTEQUAL = 'notEqual'; |
|||
|
|||
/** |
|||
* Formula 1. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $formula1 = ''; |
|||
|
|||
/** |
|||
* Formula 2. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $formula2 = ''; |
|||
|
|||
/** |
|||
* Type. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $type = self::TYPE_NONE; |
|||
|
|||
/** |
|||
* Error style. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $errorStyle = self::STYLE_STOP; |
|||
|
|||
/** |
|||
* Operator. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $operator = self::OPERATOR_BETWEEN; |
|||
|
|||
/** |
|||
* Allow Blank. |
|||
* |
|||
* @var bool |
|||
*/ |
|||
private $allowBlank = false; |
|||
|
|||
/** |
|||
* Show DropDown. |
|||
* |
|||
* @var bool |
|||
*/ |
|||
private $showDropDown = false; |
|||
|
|||
/** |
|||
* Show InputMessage. |
|||
* |
|||
* @var bool |
|||
*/ |
|||
private $showInputMessage = false; |
|||
|
|||
/** |
|||
* Show ErrorMessage. |
|||
* |
|||
* @var bool |
|||
*/ |
|||
private $showErrorMessage = false; |
|||
|
|||
/** |
|||
* Error title. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $errorTitle = ''; |
|||
|
|||
/** |
|||
* Error. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $error = ''; |
|||
|
|||
/** |
|||
* Prompt title. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $promptTitle = ''; |
|||
|
|||
/** |
|||
* Prompt. |
|||
* |
|||
* @var string |
|||
*/ |
|||
private $prompt = ''; |
|||
|
|||
/** |
|||
* Create a new DataValidation. |
|||
*/ |
|||
public function __construct() |
|||
{ |
|||
} |
|||
|
|||
/** |
|||
* Get Formula 1. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getFormula1() |
|||
{ |
|||
return $this->formula1; |
|||
} |
|||
|
|||
/** |
|||
* Set Formula 1. |
|||
* |
|||
* @param string $formula |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setFormula1($formula) |
|||
{ |
|||
$this->formula1 = $formula; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Formula 2. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getFormula2() |
|||
{ |
|||
return $this->formula2; |
|||
} |
|||
|
|||
/** |
|||
* Set Formula 2. |
|||
* |
|||
* @param string $formula |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setFormula2($formula) |
|||
{ |
|||
$this->formula2 = $formula; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Type. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getType() |
|||
{ |
|||
return $this->type; |
|||
} |
|||
|
|||
/** |
|||
* Set Type. |
|||
* |
|||
* @param string $type |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setType($type) |
|||
{ |
|||
$this->type = $type; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Error style. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getErrorStyle() |
|||
{ |
|||
return $this->errorStyle; |
|||
} |
|||
|
|||
/** |
|||
* Set Error style. |
|||
* |
|||
* @param string $errorStyle see self::STYLE_* |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setErrorStyle($errorStyle) |
|||
{ |
|||
$this->errorStyle = $errorStyle; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Operator. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getOperator() |
|||
{ |
|||
return $this->operator; |
|||
} |
|||
|
|||
/** |
|||
* Set Operator. |
|||
* |
|||
* @param string $operator |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setOperator($operator) |
|||
{ |
|||
$this->operator = $operator; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Allow Blank. |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function getAllowBlank() |
|||
{ |
|||
return $this->allowBlank; |
|||
} |
|||
|
|||
/** |
|||
* Set Allow Blank. |
|||
* |
|||
* @param bool $allowBlank |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setAllowBlank($allowBlank) |
|||
{ |
|||
$this->allowBlank = $allowBlank; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Show DropDown. |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function getShowDropDown() |
|||
{ |
|||
return $this->showDropDown; |
|||
} |
|||
|
|||
/** |
|||
* Set Show DropDown. |
|||
* |
|||
* @param bool $showDropDown |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setShowDropDown($showDropDown) |
|||
{ |
|||
$this->showDropDown = $showDropDown; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Show InputMessage. |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function getShowInputMessage() |
|||
{ |
|||
return $this->showInputMessage; |
|||
} |
|||
|
|||
/** |
|||
* Set Show InputMessage. |
|||
* |
|||
* @param bool $showInputMessage |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setShowInputMessage($showInputMessage) |
|||
{ |
|||
$this->showInputMessage = $showInputMessage; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Show ErrorMessage. |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function getShowErrorMessage() |
|||
{ |
|||
return $this->showErrorMessage; |
|||
} |
|||
|
|||
/** |
|||
* Set Show ErrorMessage. |
|||
* |
|||
* @param bool $showErrorMessage |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setShowErrorMessage($showErrorMessage) |
|||
{ |
|||
$this->showErrorMessage = $showErrorMessage; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Error title. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getErrorTitle() |
|||
{ |
|||
return $this->errorTitle; |
|||
} |
|||
|
|||
/** |
|||
* Set Error title. |
|||
* |
|||
* @param string $errorTitle |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setErrorTitle($errorTitle) |
|||
{ |
|||
$this->errorTitle = $errorTitle; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Error. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getError() |
|||
{ |
|||
return $this->error; |
|||
} |
|||
|
|||
/** |
|||
* Set Error. |
|||
* |
|||
* @param string $error |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setError($error) |
|||
{ |
|||
$this->error = $error; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Prompt title. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getPromptTitle() |
|||
{ |
|||
return $this->promptTitle; |
|||
} |
|||
|
|||
/** |
|||
* Set Prompt title. |
|||
* |
|||
* @param string $promptTitle |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setPromptTitle($promptTitle) |
|||
{ |
|||
$this->promptTitle = $promptTitle; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get Prompt. |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function getPrompt() |
|||
{ |
|||
return $this->prompt; |
|||
} |
|||
|
|||
/** |
|||
* Set Prompt. |
|||
* |
|||
* @param string $prompt |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function setPrompt($prompt) |
|||
{ |
|||
$this->prompt = $prompt; |
|||
|
|||
return $this; |
|||
} |
|||
|
|||
/** |
|||
* Get hash code. |
|||
* |
|||
* @return string Hash code |
|||
*/ |
|||
public function getHashCode() |
|||
{ |
|||
return md5( |
|||
$this->formula1 . |
|||
$this->formula2 . |
|||
$this->type . |
|||
$this->errorStyle . |
|||
$this->operator . |
|||
($this->allowBlank ? 't' : 'f') . |
|||
($this->showDropDown ? 't' : 'f') . |
|||
($this->showInputMessage ? 't' : 'f') . |
|||
($this->showErrorMessage ? 't' : 'f') . |
|||
$this->errorTitle . |
|||
$this->error . |
|||
$this->promptTitle . |
|||
$this->prompt . |
|||
$this->sqref . |
|||
__CLASS__ |
|||
); |
|||
} |
|||
|
|||
/** |
|||
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
|||
*/ |
|||
public function __clone() |
|||
{ |
|||
$vars = get_object_vars($this); |
|||
foreach ($vars as $key => $value) { |
|||
if (is_object($value)) { |
|||
$this->$key = clone $value; |
|||
} else { |
|||
$this->$key = $value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/** @var ?string */ |
|||
private $sqref; |
|||
|
|||
public function getSqref(): ?string |
|||
{ |
|||
return $this->sqref; |
|||
} |
|||
|
|||
public function setSqref(?string $str): self |
|||
{ |
|||
$this->sqref = $str; |
|||
|
|||
return $this; |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Cell; |
|||
|
|||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
|||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|||
use PhpOffice\PhpSpreadsheet\Exception; |
|||
|
|||
/** |
|||
* Validate a cell value according to its validation rules. |
|||
*/ |
|||
class DataValidator |
|||
{ |
|||
/** |
|||
* Does this cell contain valid value? |
|||
* |
|||
* @param Cell $cell Cell to check the value |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function isValid(Cell $cell) |
|||
{ |
|||
if (!$cell->hasDataValidation()) { |
|||
return true; |
|||
} |
|||
|
|||
$cellValue = $cell->getValue(); |
|||
$dataValidation = $cell->getDataValidation(); |
|||
|
|||
if (!$dataValidation->getAllowBlank() && ($cellValue === null || $cellValue === '')) { |
|||
return false; |
|||
} |
|||
|
|||
// TODO: write check on all cases |
|||
switch ($dataValidation->getType()) { |
|||
case DataValidation::TYPE_LIST: |
|||
return $this->isValueInList($cell); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* Does this cell contain valid value, based on list? |
|||
* |
|||
* @param Cell $cell Cell to check the value |
|||
* |
|||
* @return bool |
|||
*/ |
|||
private function isValueInList(Cell $cell) |
|||
{ |
|||
$cellValue = $cell->getValue(); |
|||
$dataValidation = $cell->getDataValidation(); |
|||
|
|||
$formula1 = $dataValidation->getFormula1(); |
|||
if (!empty($formula1)) { |
|||
// inline values list |
|||
if ($formula1[0] === '"') { |
|||
return in_array(strtolower($cellValue), explode(',', strtolower(trim($formula1, '"'))), true); |
|||
} elseif (strpos($formula1, ':') > 0) { |
|||
// values list cells |
|||
$matchFormula = '=MATCH(' . $cell->getCoordinate() . ', ' . $formula1 . ', 0)'; |
|||
$calculation = Calculation::getInstance($cell->getWorksheet()->getParent()); |
|||
|
|||
try { |
|||
$result = $calculation->calculateFormula($matchFormula, $cell->getCoordinate(), $cell); |
|||
|
|||
return $result !== Functions::NA(); |
|||
} catch (Exception $ex) { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
} |
|||
@ -0,0 +1,83 @@ |
|||
<?php |
|||
|
|||
namespace PhpOffice\PhpSpreadsheet\Cell; |
|||
|
|||
use DateTimeInterface; |
|||
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
|||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
|||
|
|||
class DefaultValueBinder implements IValueBinder |
|||
{ |
|||
/** |
|||
* Bind value to a cell. |
|||
* |
|||
* @param Cell $cell Cell to bind value to |
|||
* @param mixed $value Value to bind in cell |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public function bindValue(Cell $cell, $value) |
|||
{ |
|||
// sanitize UTF-8 strings |
|||
if (is_string($value)) { |
|||
$value = StringHelper::sanitizeUTF8($value); |
|||
} elseif (is_object($value)) { |
|||
// Handle any objects that might be injected |
|||
if ($value instanceof DateTimeInterface) { |
|||
$value = $value->format('Y-m-d H:i:s'); |
|||
} elseif (!($value instanceof RichText)) { |
|||
// Attempt to cast any unexpected objects to string |
|||
$value = (string) $value; |
|||
} |
|||
} |
|||
|
|||
// Set value explicit |
|||
$cell->setValueExplicit($value, static::dataTypeForValue($value)); |
|||
|
|||
// Done! |
|||
return true; |
|||
} |
|||
|
|||
/** |
|||
* DataType for value. |
|||
* |
|||
* @param mixed $value |
|||
* |
|||
* @return string |
|||
*/ |
|||
public static function dataTypeForValue($value) |
|||
{ |
|||
// Match the value against a few data types |
|||
if ($value === null) { |
|||
return DataType::TYPE_NULL; |
|||
} elseif (is_float($value) || is_int($value)) { |
|||
return DataType::TYPE_NUMERIC; |
|||
} elseif (is_bool($value)) { |
|||
return DataType::TYPE_BOOL; |
|||
} elseif ($value === '') { |
|||
return DataType::TYPE_STRING; |
|||
} elseif ($value instanceof RichText) { |
|||
return DataType::TYPE_INLINE; |
|||
} elseif (is_string($value) && strlen($value) > 1 && $value[0] === '=') { |
|||
return DataType::TYPE_FORMULA; |
|||
} elseif (preg_match('/^[\+\-]?(\d+\\.?\d*|\d*\\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $value)) { |
|||
$tValue = ltrim($value, '+-'); |
|||
if (is_string($value) && strlen($tValue) > 1 && $tValue[0] === '0' && $tValue[1] !== '.') { |
|||
return DataType::TYPE_STRING; |
|||
} elseif ((strpos($value, '.') === false) && ($value > PHP_INT_MAX)) { |
|||
return DataType::TYPE_STRING; |
|||
} elseif (!is_numeric($value)) { |
|||
return DataType::TYPE_STRING; |
|||
} |
|||
|
|||
return DataType::TYPE_NUMERIC; |
|||
} elseif (is_string($value)) { |
|||
$errorCodes = DataType::getErrorCodes(); |
|||
if (isset($errorCodes[$value])) { |
|||
return DataType::TYPE_ERROR; |
|||
} |
|||
} |
|||
|
|||
return DataType::TYPE_STRING; |
|||
} |
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue