-
-
Notifications
You must be signed in to change notification settings - Fork 637
Add Column Widths #118
Comments
Hey @5e112, thanks for filing this "issue". Column width is pretty hard to deal with. Another way to do it would be to have an auto-width, where we can detect the size of each cell and resize the column to the max size of the columns' cells. Unfortunately, if you want to do this well, it gets pretty complex. Because Spout supports multiple fonts, we can't rely on the size of each character using default font and need to have specific code to determine it in some other ways (this could be done by drawing the text with the given font and get the dimensions of the drawn text). The drawback of this solution is that it takes quite some time for every cell. So for a large spreadsheet, the performance of Spout would be severely degraded. Now, I may have misunderstood what you were trying to accomplish. Please let me know if that's the case. Or if you had another solution in mind, I'd be happy to discuss about it! |
If I add text wrapping to the cell, The width is primarily cosmetic. Maybe Im missing something, but I never did any calculations to figure out the widths, I was just suggesting it might be a decent feature to expose through the library, but if not, thats fine, It still suits my purposes. |
Oh I see. Do you think the default width is never good? Too small maybe? I could add the |
For large strings, or even paragraphs of text, the default width is a bit rough, definitely too small, sometimes it's a good thing to be able to set a width + text wrapping. |
Yes, that makes sense. Thinking about how this could be done, the fact that Spout only works on rows and does not know about cells makes it tricky. // terrible interface but it's just an illustration
$sheet->addRowWithCustomWidth(['foo1', 'bar1'], 30);
$sheet->addRowWithCustomWidth(['foo2', 'bar2'], 20); // this width will be ignored Because of this, it only makes sense to move the scope of this one level higher: defining a custom width for ALL columns. This could be an option on the writer: $writer = WriterFactory::create(Type::XLSX);
$writer->setColumnsWidth(25);
$writer->openToFile(...); I'm still unsure if that's going to be very useful. My feeling is that, as a developer, you'd want to control the width of each column separately (which Spout can't do) and not necessarily have a global setting. Increasing the size of the default column width though (without providing a setter) can be useful. I'll reopen the issue and wait a bit to see if more people are interested in this improvement. |
<col min="1" max="1" width="25" customWidth="1"/>
<col min="2" max="2" width="35" customWidth="1"/>
<col min="3" max="3" width="45" customWidth="1"/> etc. min and max define the range of columns being affected. What I would do is, use your method and add two parameters: $writer->setColumnsWidth(width, min, max) //can be called on numerous columns/column ranges and you could set the width for each column cleanly. Its not really a big deal, but its another feature that can be added, which doesnt compromise the library's essential mission of being lightweight, low memory, etc. |
Hello, |
#129 will be the solution for this. It has the benefits of being transparent for the developer. Spout should be able to automatically adjust the dimensions of the cells based on its content. Developers will be able to specify the max width and max height and Spout will take care of the rest! I'm closing this issue and will track the progress in #129 |
I'm posting a little late on this, but it seems obvious to me that the developer should have the power, if he or she so chooses, to "globally" set a column width. This is a valid decision, and it can and will often mean a) "This is what I know I want the width to be" and/or b) "Content can get large in this column, I don't want it to go beyond x width, so this way I can set it and forget it" Giving this option in Box/spout is analogous to the user being able to do this manually in the Excel UI. Those who have never done that before, please raise your hand :) |
I understand your point and agree with you on that. However we'd rather do the auto-sizing of columns first before tackling the manual option. This is because auto-sizing will benefit more people than manual sizing. But eventually we'll have all options! |
@adrilo but wouldn't the manual sizing be a "quick win" that could solve a lot of our issues? Auto-sizing is a nice-to-have feature, but I would be able to create "good enough" files with manual sizing as well. Manual sizing and cell merging are the things I'm missing the most in Spout (and consider going back to the PHPExcel, even if it means using 10 times more time and memory). |
Although this issue is closed, what's the current thinking around the |
This is not an issue for me necessarily , just letting you guys know in Worksheet.php right where you begin the sheet (in startSheet()) you can define column widths.
I ended up just prepending the following string to give columns 1 (min) through 7 (max) columns (A:G) with the specified parameters, right before the tag.
Great library, Really prefer it to some of the memory intensive alternatives. If no one has time I can do it myself, but not immediately as I've got a bunch of work to catch up with at the moment. Thanks for your great work. Really came in handy.
The text was updated successfully, but these errors were encountered: