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

中文自动列宽设置后无效 #3405

Closed
hmwdt opened this issue Feb 27, 2023 · 3 comments · Fixed by #3416
Closed

中文自动列宽设置后无效 #3405

hmwdt opened this issue Feb 27, 2023 · 3 comments · Fixed by #3416

Comments

@hmwdt
Copy link

hmwdt commented Feb 27, 2023

This is:

- [ ] 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?

What is the current behavior?

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
           
 $dataSheet->getColumnDimension("G")->setAutoSize(true); 

如果某一列是CJK 其中的一种,这样的设置方式无效

 version     "phpoffice/phpspreadsheet": "^1.19",





If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

### What features do you think are causing the issue

- [ ] Reader
- [ ] Writer
- [ ] Styles
- [ ] Data Validations
- [ ] Formula Calculations
- [ ] Charts
- [ ] AutoFilter
- [ ] Form Elements

### Does an issue affect all spreadsheet file formats? If not, which formats are affected?


### Which versions of PhpSpreadsheet and PHP are affected?
@MarkBaker
Copy link
Member

We don't have any CJK fonts defined in the library for autosizing.
What font are you using? Is it the standard Excel font for your locale?

@hmwdt
Copy link
Author

hmwdt commented Feb 28, 2023

My system is a Chinese system, so the text in excel includes numbers, English and Chinese.

At present, I calculate the column width by myself according to the content in the cell, and each Chinese text occupies 2 lengths

I used this method 'mb_strwidth' to determine the width of each cell, take the largest value in the column, and set it as the width of the column. Although this is not a good job, it also meets my current needs. I hope you can be compatible with CJK content.

$checkStrLength = mb_strwidth($string);
https://www.php.net/manual/zh/function.mb-strwidth.php

demo cell value like : 你好

@hmwdt hmwdt closed this as completed Feb 28, 2023
@hmwdt hmwdt reopened this Feb 28, 2023
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Mar 1, 2023
Fix PHPOffice#3405. Autosize is definitely not working well with CJK characters (column is not wide enough). User reports a workaround using `mb_strwidth` to calculate and set the column width. PhpSpreadsheet uses `mb_strlen` for width calculations. Change it to use mb_strwidth instead. For non-CJK strings, the results will be identical (and there are already unit tests on such strings which assert the expected results, and these tests did not need to change). For CJK strings, the results will be wider. The string I'm using to test comes from the issue. It currently results in a column width of 30.564. When I open the resulting sheet in Excel and auto-fit the column width, the width winds up as 43.00. So, as long as the computed width exceeds 43.00, the spreadsheet will show the full cell. With the new calculation, the computed width is 55.2722, satisfying our condition. This is wider than expected, but that is generally true for this type of computation. For example, for 'abcdefghijklmnopqrstuvwxyz', the computed width (before and after this change) is 31.7065, but Excel auto-fit actually uses 24.73.

Disappointingly, "exact width calculation" does not solve this problem. It does seem to do a little better than "approximate" for non-CJK, but its CJK calculation is not wide enough. This might or might not indicate a bug in Php function `imagegetttfbbox`; I do not know enough about it to report a bug. Anyhow, since we're dependent on that result, there is no equivalent in this case for swapping mb_strlen out for mb_strwidth.
@oleibman
Copy link
Collaborator

oleibman commented Mar 1, 2023

If you can, please test against PR 3416. I'm sure you can give it a more thorough test than I can. Thank you for mentioning mb_strwidth; I was not familiar with it, and the PR depends on it.

oleibman added a commit that referenced this issue Mar 4, 2023
Fix #3405. Autosize is definitely not working well with CJK characters (column is not wide enough). User reports a workaround using `mb_strwidth` to calculate and set the column width. PhpSpreadsheet uses `mb_strlen` for width calculations. Change it to use mb_strwidth instead. For non-CJK strings, the results will be identical (and there are already unit tests on such strings which assert the expected results, and these tests did not need to change). For CJK strings, the results will be wider. The string I'm using to test comes from the issue. It currently results in a column width of 30.564. When I open the resulting sheet in Excel and auto-fit the column width, the width winds up as 43.00. So, as long as the computed width exceeds 43.00, the spreadsheet will show the full cell. With the new calculation, the computed width is 55.2722, satisfying our condition. This is wider than expected, but that is generally true for this type of computation. For example, for 'abcdefghijklmnopqrstuvwxyz', the computed width (before and after this change) is 31.7065, but Excel auto-fit actually uses 24.73.

Disappointingly, "exact width calculation" does not solve this problem. It does seem to do a little better than "approximate" for non-CJK, but its CJK calculation is not wide enough. This might or might not indicate a bug in Php function `imagegetttfbbox`; I do not know enough about it to report a bug. Anyhow, since we're dependent on that result, there is no equivalent in this case for swapping mb_strlen out for mb_strwidth.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants