Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not formatting cell correctly with [MM] code #342

Closed
garrettgjb opened this issue Jan 20, 2018 · 10 comments
Closed

Not formatting cell correctly with [MM] code #342

garrettgjb opened this issue Jan 20, 2018 · 10 comments

Comments

@garrettgjb
Copy link

garrettgjb commented Jan 20, 2018

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?

I expect for the column containing "Duration" should come in as "mm" format as defined in excel. Meaning it should look like an integer as you see when you are in excel.
screen shot 2018-01-19 at 9 07 59 pm

The column subtracts one date from another date.
I.e.

2018-01-22 02:00:00 PM - 2018-01-22 01:00:00 PM

We format is as [mm] so it would be 60.

I think what is happening is this library doesn't know how to do binary operations with dates. If you multiple the decimal number by 1440 you get the correct number.

What is the current behavior?

Duration column comes in as a decimal in the php code.
screen shot 2018-01-19 at 9 00 56 pm

What are the steps to reproduce?

Import the following file with phpspreadsheet:
importtest-phpspreadsheet.xlsx

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';

	$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

	$file="importtest-phpspreadsheet.xlsx";
	$spreadsheet = $reader->load($file);
	$worksheet = $spreadsheet->getActiveSheet();

	echo "Should be 60";
	echo "\nActual: " . ($spreadsheet->getActiveSheet()->getCell('K2')->getFormattedValue());
	echo "\nFinished";

toArray return will have incorrect values.

Which versions of PhpSpreadsheet and PHP are affected?

PHP 5.6, Latest phpspreadsheet from composer

@PowerKiKi
Copy link
Member

The code provided is not valid PHP code and cannot be run. And even if it could it would not show anything useful regarding this issue.

Anyway the described behavior is the expected behavior. The value you got are indeed the values stored in the file. What you see in Excel is not the raw values, but formatted value. You should have a look at getFormattedValue()

@garrettgjb
Copy link
Author

Please check again. I updated the code so it is runnable, just put the spreadsheet in the same dir.

Output:
Should be 60
Actual: 0.041666666671517

My previous example called toArray with no params which my research showed should have formatted. In my updated sample, I have called getFormattedValue as you said so you can see its not working.

@garrettgjb
Copy link
Author

I've reposted the issue here since this one is closed:
#343

@PowerKiKi PowerKiKi changed the title PhpSpreadsheet is not doing math with dates right. Not formatting cell correctly with [MM] code Jan 21, 2018
@garrettgjb
Copy link
Author

garrettgjb commented Jan 21, 2018

@PowerKiKi Can this issue be reopened? I'm guessing you were able to recreate it since you changed the title.

@PowerKiKi
Copy link
Member

Please refrain from opening new issue for the same problem, we can continue the discussion here and re-open as needed.

The MCVE should not rely on an external file and the code should have been simplified down to:

<?php

require __DIR__ . '/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

var_dump(NumberFormat::toFormattedString(0.041666666671517, '[MM]'));

@garrettgjb
Copy link
Author

@PowerKiKi sorry, I'm new to github issue reporting process and didn't realize the problem was with toFormattedString.

@garrettgjb
Copy link
Author

I did a little debugging and it looks like in an attempt to remove color information, we clear out the format variable so it doesn't go into the date format code.

From toFormattedString:

$color_regex = '/^\\[[a-zA-Z]+\\]/'; $format = preg_replace($color_regex, '', $format);
It goes from "[MM]" to ""

screen shot 2018-01-22 at 7 24 59 pm

I'm not an expert and regexp's and I don't know what color formatting looks like or I would attempt a fix.

@PowerKiKi
Copy link
Member

I suggest you add a case for colors in tests/data/Style/NumberFormat.php and case for [MM] in tests/data/Style/NumberFormatDates.php and keep trying to debug/fix it while testing tests/PhpSpreadsheetTests/Style/NumberFormatTest.php.

The following page should help you find out what syntaxes should be supported: https://support.office.com/en-us/article/create-or-delete-a-custom-number-format-78f2a361-936b-4c03-8772-09fab54be7f4

@stale
Copy link

stale bot commented Mar 24, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Mar 24, 2018
@stale stale bot closed this as completed Mar 31, 2018
@patrickomeara patrickomeara mentioned this issue Sep 8, 2018
7 tasks
PowerKiKi pushed a commit that referenced this issue Jan 2, 2019
When using format `[h]:mm` it should convert to the "total hours:minutes"

Closes #666
Fixes #664
Fixes #446
Fixes #342
guillaume-ro-fr pushed a commit to guillaume-ro-fr/PhpSpreadsheet that referenced this issue Jun 12, 2019
When using format `[h]:mm` it should convert to the "total hours:minutes"

Closes PHPOffice#666
Fixes PHPOffice#664
Fixes PHPOffice#446
Fixes PHPOffice#342
@oleibman
Copy link
Collaborator

Fixed by 699da09

@oleibman oleibman removed the stale label Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants