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

Automatically calculate columns width / rows height #129

Open

Description

@adrilo

Instead of using the default width/height, Spout could calculate the optimal width/height for each cell, based on its content.

This involves using a graphics library, render the text content with the chosen font and get the width/height of the rendered text.

This calculation may be slow so it should be optional. It can also be used when using "wrap text" as wrapping does not work well without a custom row height in LibreOffice.

Spout should expose an option to automatically set the optimal width/height but should also allow developers to specify a max width / height. When the max width is reached, the content will automatically wrap and the cell height will increase. It will increase until it reaches the max height.

Activity

  1. zendre4 commented on Feb 25, 2016

    @zendre4

    +1

  2. linaspasv commented on Mar 2, 2016

    @linaspasv

    +1

  3. ttkulm commented on Apr 20, 2016

    @ttkulm

    +1 Would be a really good feature!

  4. arisdario commented on Apr 27, 2016

    @arisdario

    +10

  5. jordanade commented on May 25, 2016

    @jordanade

    I don't need auto-fit, just a way to set widths for each column! Any way to workaround/hack this for now?

  6. jordanade commented on May 25, 2016

    @jordanade

    Oh, figured it out--just added this line to Worksheet.php, startSheet(), between the header and the data:
    fwrite($this->sheetFilePointer, '<cols><col min="1" max="1" width="100" customWidth="1"/><col min="2" max="6" width="30" customWidth="1"/></cols>');

  7. adrilo commented on May 26, 2016

    @adrilo
    CollaboratorAuthor

    @jordanade You can also look at #225 if you need to hack something that works.
    I'm not a big fan of this as it creates an API that is specific to XLSX. Also, if Spout could automatically calculate the width of each column, it would solve 95% of the problems. So I'd rather have this transparent solution in place, instead of having to manually specify the width of each column.

  8. jordanade commented on Jun 9, 2016

    @jordanade

    Surely there is room for APIs that may not work for all possible file outputs? Best effort, etc...

  9. adrilo commented on Jun 15, 2016

    @adrilo
    CollaboratorAuthor

    I'm actually trying to avoid this as much as possible. Although I'll definitely reconsider after implementing the automated method.

  10. modified the milestone: 3.1.0 on Jul 6, 2016
  11. nimmneun commented on Aug 29, 2016

    @nimmneun
    Contributor

    Hi there, have you been able to come up with a solution for the autosizing of column widths and row heights already?

    If not, I've come with an intermediary solution, which - while less accurate than imagettfbbox - is IMHO a decent tradeoff between performance and accuracy.
    On average it increases the runtime by about 50%-100% and the widths are on average about 1 character off (usually too wide).

    It's a bit hacky though (requires serious refinement! =) and involves a csv that holds base sizes for each font and character in the ASCII range, which is why I'm hesitating to fork->code->pull spout, since I dont know if it's a viable solution to integrate that into spout.

  12. adrilo commented on Aug 30, 2016

    @adrilo
    CollaboratorAuthor

    @nimmneun Nobody started working on this enhancement. If you already built something, feel free to share it. Either share a link to your commit or create a pull request.
    It's always good to get inspiration from others :)

  13. 36 remaining items

  14. jeanvcastro commented on Sep 10, 2020

    @jeanvcastro

    +1

  15. smartniazi commented on Oct 20, 2020

    @smartniazi

    +1

  16. gleydsonruan commented on Oct 21, 2020

    @gleydsonruan

    +1

  17. jadamec commented on Feb 10, 2021

    @jadamec

    +1

  18. chuoke commented on Apr 13, 2021

    @chuoke

    sounds so sad

  19. xwiz commented on Apr 16, 2021

    @xwiz

    @adrilo I understand you're busy but this is a super useful feature. I've read the guide to contributing and I'm willing to work on this. Do you have any pointers to what has already been tried or your recommendations? I also noticed it's now possible to write cells in v3, this makes the issue of cell merging easier from a technical angle. I can create PRs for both but would love to get pointers so I don't duplicate someone's work.

  20. adrilo commented on Apr 29, 2021

    @adrilo
    CollaboratorAuthor

    @xwiz This specific issue is about the automation of the calculation. What's very difficult about this is the fact that you can only determine the width of the column after the last row is written. Which means for each new row, you need to compute the width of each cell and keep the max width. Before closing the writer, the info about the max width needs to be added to the right XML file. Computing the width of a cell may be computationally intensive, especially since Spout supports different fonts (the width of a given text differs from font to font).

    Also, we're trying to keep the same features for all file formats (usually XLSX and ODS). So if you're interested to work on that, please remember that. Thanks!

  21. sergiokessler commented on Sep 9, 2021

    @sergiokessler

    absolutely rude hack for someone who need a default width somewhat wider:

    in the file WorksheetManager.php change the code:

        const SHEET_XML_FILE_HEADER = <<<'EOD'
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
    EOD;
    

    by

        const SHEET_XML_FILE_HEADER = <<<XML
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <worksheet
        xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac"
        xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
        <sheetFormatPr baseColWidth="10" defaultColWidth="30.69921875" defaultRowHeight="15.6" x14ac:dyDescent="0.3"/>
    XML;
    

    note the defaultColWidth attributte, play with it...

  22. xwiz commented on Sep 12, 2021

    @xwiz

    @adrilo Missed your reply earlier... will add to my calendar..

  23. jadamec commented on Oct 10, 2021

    @jadamec

    +1

  24. cetver commented on Nov 3, 2021

    @cetver

    +1

  25. deivid11 commented on Jan 6, 2022

    @deivid11

    +1

  26. sanchezfauste commented on Feb 3, 2022

    @sanchezfauste

    +1

  27. Slamdunk commented on Mar 3, 2022

    @Slamdunk

    Static column width settings merged in openspout/openspout#4

  28. Slamdunk commented on Mar 23, 2022

    @Slamdunk

    Hi, just like ODS already did it, with openspout/openspout#45 now also XLSX writes the rows in a temporary file and only after that it writes all the remaining data, thanks to the speed of stream_copy_to_stream.

    So now column with, alongside auto-sizing, could be easily supported and already achievable by user code 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions