Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Add Column Widths #118

Closed
5e112 opened this issue Sep 24, 2015 · 12 comments · May be fixed by #715
Closed

Add Column Widths #118

5e112 opened this issue Sep 24, 2015 · 12 comments · May be fixed by #715

Comments

@5e112
Copy link

5e112 commented Sep 24, 2015

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.

@5e112 5e112 changed the title Add Cell Widths Add Column/Cell Widths Sep 24, 2015
@5e112 5e112 changed the title Add Column/Cell Widths Add Column Widths Sep 24, 2015
@adrilo
Copy link
Collaborator

adrilo commented Sep 24, 2015

Hey @5e112, thanks for filing this "issue". Column width is pretty hard to deal with.
In your case, you probably knew in advance the data you were going to output and could therefore adjust the width accordingly. But most of the time, you don't know how long the cell values will be and won't be able to select the appropriate width for the column.

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!

@5e112
Copy link
Author

5e112 commented Sep 24, 2015

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.
Again, Thanks for all your hard work. Nice library, really does what I need it to.

@5e112 5e112 closed this as completed Sep 24, 2015
@adrilo
Copy link
Collaborator

adrilo commented Sep 25, 2015

Oh I see. Do you think the default width is never good? Too small maybe? I could add the <col> part that you mentioned to change the default width of the columns

@5e112
Copy link
Author

5e112 commented Sep 25, 2015

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.

@adrilo
Copy link
Collaborator

adrilo commented Sep 25, 2015

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.
You would only be able to set the same width for all the cells of a row. If you can set the columns width for every row, you would basically use the max width in the end:

// 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.

@5e112
Copy link
Author

5e112 commented Sep 25, 2015

"My feeling is that, as a developer, you'd want to control the width of each column separately"
Keep in mind you could set all the column widths individually, I just opened the thread with a lousy example.
i.e,

<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.
is defining separate widths for columns 1, 2, and 3 (respectively widths of 25, 35, and 45).

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.

@effetb
Copy link

effetb commented Sep 30, 2015

Hello,
I just discover your library and I think @5e112 has a good idea.
Specify column width would really help in my case.
Thank you

@adrilo
Copy link
Collaborator

adrilo commented Oct 22, 2015

#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

@samfullman
Copy link

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 :)

@adrilo
Copy link
Collaborator

adrilo commented Sep 25, 2017

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!

@borys-p
Copy link

borys-p commented Nov 22, 2018

@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).

@jmitchell38488
Copy link

Although this issue is closed, what's the current thinking around the <cols> definition in the worksheet xml?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants