-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Description
This is:
- [X] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)
What is the expected behavior?
The DAYS($endDate, $startDate) should calculate the number of days between an starting date and an endate.
What is the current behavior?
The function works correctly after XLSX opening in view only mode, but after enabling edit mode every CELL where is used returns an NAME? error indicating that invalid characters where introduced in the formula.
If you manually edit the formula (F2) and click enter, the correct value is recalculated for the cell.
As a workaround, you can substitute the formula by a simple subtract of the start_date from the end_date, but using the formula will be great, as it sort of self-document the purpose of the operation.
What are the steps to reproduce?
Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:
<?php
require __DIR__ . '/vendor/autoload.php';
// Enable AdvancedValueBinder and RETURNDATE_EXCEL
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL);
// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
// add code that show the issue here...
// $credit->plazo_meses = 120;
for ($x = 0; $x < $credit->plazo_meses; $x++) {
// Interes: =IF(ISBLANK(B12),($B$7/12)*K11,(((DAYS(B12,B11))/365)*$B$7)*K11)
$fInteres = '=IF(ISBLANK(B'.(12+$x).'),($B$7/12)*K'.(11+$x).',(((DAYS(B'.(12+$x).',B'.(11+$x).'))/365)*$B$7)*K'.(11+$x).')';
$sheet->setCellValue('I'.(12+$x), $fInteres);
$sheet->getStyle('I'.(12+$x))
->getNumberFormat()
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_00);
}
// In the other hand, replacing DAYS for a subtraction eliminates the error, as with the following code:
// Interes: =IF(ISBLANK(B12),($B$7/12)*K11,(((B12-B11)/365)*$B$7)*K11)
$fInteres = '=IF(ISBLANK(B'.(12+$x).'),($B$7/12)*K'.(11+$x).',(((B'.(12+$x).',B'.(11+$x).')/365)*$B$7)*K'.(11+$x).')';
Which versions of PhpSpreadsheet and PHP are affected?
Only tested on version ## [1.9.0] - 2019-08-17