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

Convert cell value to numbeic (float) in processDomElementDataFormat #3443

Closed
wants to merge 1 commit into from

Conversation

PouriaSeyfi
Copy link
Contributor

If we want to use data-format attribute in html, we have to cast cell value to float (number). because format not applied to string values. We should know excel put Leading apostrophes " ' " to numeric string values. Leading apostrophes force excel to treat the cell's contents as a text value.

If we want to use data-format attribute in html, we have to cast cell value to float (number). because format not applied to string values.
We should know excel put Leading apostrophes " ' " to numeric string values. Leading apostrophes force excel to treat the cell's contents as a text value.
@oleibman
Copy link
Collaborator

oleibman commented Mar 9, 2023

I think you have identified a problem, but not a proper solution. Here's a basic program to parse html.

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
$spreadsheet = $reader->loadFromString($html);
$sheet = $spreadsheet->getActiveSheet();
var_dump($sheet->getCell('A1')->getValue());
var_dump($sheet->getCell('A1')->getFormattedValue());
var_dump($sheet->getStyle('A1')->getNumberFormat()->getFormatCode());

Now, let's look at some possible input:

<table>
    <tr>
         <td data-format="#.000">3</td>
    </tr>
</table>

This works correctly with or without your change - getFormattedValue returns 3.000.
Here's different html:

<table>
    <tr>
         <td data-format="#.000">x</td>
    </tr>
</table>

This works correctly, or at least the same way as Excel, before your change - getFormattedValue returns x. I believe that your change breaks this behavior.
Here is still different html, taken from HtmlTest testCanApplyInlineDataFormat.

<table>
    <tr>
         <td data-format="mmm-yy">2019-02-02 12:34:00</td>
    </tr>
</table>

Here, the existing test does not go far enough. It verifies that the format is set correctly, but doesn't check what getFormattedValue returns, which, in this case, is 2019-02-02 12:34:00. The Html load has not treated the value as a date, and possibly should (I have no idea yet how difficult that would be). Your change doesn't do much better; casting to float converts the string to 2019.0, losing the month, day, and time.

Of course, date/time strings may not be the only problem. I think you have a specific problem in mind. What is the specific problem that prompted you to submit this PR?

@oleibman oleibman marked this pull request as draft March 9, 2023 07:05
@PouriaSeyfi
Copy link
Contributor Author

Yes thank you for your good explanation. you are right I think I had this problem in older versions.
now it works without problem. I identified a problem and I will create new PR

@PouriaSeyfi PouriaSeyfi closed this Mar 9, 2023
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this pull request Mar 16, 2023
Fix PHPOffice#3443. A mysterious implementation by Excel. The style tags have an attribute applyAlignment, which nominally says whether or not the style should use its own Alignment. Except ... Excel ignores that attribute and uses the alignment tag if it is supplied ... and, another mystery, uses not the default style for the spreadsheet if not supplied, but rather uses the default alignment style for all spreadsheets even if the spreadsheet's default style uses a non-default alignment. I am changing Xlsx Writer to generate alignment tag unless the alignment matches both the default alignment for the spreadsheet and the default alignment for all spreadsheets (which I expect to happen most of the time).
oleibman added a commit that referenced this pull request Mar 18, 2023
* Xlsx Writer Honor Alignment in Default Font

Fix #3443. A mysterious implementation by Excel. The style tags have an attribute applyAlignment, which nominally says whether or not the style should use its own Alignment. Except ... Excel ignores that attribute and uses the alignment tag if it is supplied ... and, another mystery, uses not the default style for the spreadsheet if not supplied, but rather uses the default alignment style for all spreadsheets even if the spreadsheet's default style uses a non-default alignment. I am changing Xlsx Writer to generate alignment tag unless the alignment matches both the default alignment for the spreadsheet and the default alignment for all spreadsheets (which I expect to happen most of the time).

* Improve Performance

Also don't change horizontal/vertical on Xlsx Read if they aren't explicitly set.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants