excel4node icon indicating copy to clipboard operation
excel4node copied to clipboard

How would I auto size a cell width to expand to show all the cell's content?

Open JemiloII opened this issue 9 years ago • 17 comments

I can't seem to find the option for this. I see things similar to it, but its not it exactly and its api isn't documented well.

JemiloII avatar Aug 17 '16 21:08 JemiloII

I agree, this would be a good feature, especially for row height when wrapText is true.

chrishakai avatar Aug 19 '16 22:08 chrishakai

Any more development on this? A Cell AutoFit parameter would be great. I'm sure the previous repo had something similar to this.

andrewminton avatar Mar 27 '17 13:03 andrewminton

any workaround for this?

amit-coditas avatar May 22 '17 10:05 amit-coditas

Any fix for this ?

cesardmoro avatar Jan 10 '18 13:01 cesardmoro

expect this feature!

lerit avatar Jan 15 '18 12:01 lerit

This feature was brought up before in Issue 23. The problem is that there is no "100%" width in the XML standard. The fix would basically be to add a rendering engine to the library and measure the width of the actual rendered text in the font/size that had been specified. If anyone has another idea, I'd be very open to a pull request.

natergj avatar Jan 15 '18 15:01 natergj

@natergj I would start by approximating it. See PHPOffice's approach here https://github.com/PHPOffice/PhpSpreadsheet/blob/257c3eca5870dbfaf89eb344fb31c07dfbb74456/src/PhpSpreadsheet/Shared/Font.php#L315

alasdaircr avatar Jan 22 '18 01:01 alasdaircr

I am using a workaround for this issue:

  1. find max data length for each column
  2. set column width equal to found max value

example for typescript:

const names = ["Alice", "Bob", "Clara"]
const lengthArr = names.map(name => name.length)
const maxWidth = Math.max(...lengthArr)
ws.column(0).setWidth(maxWidth)

AlexMesser avatar Dec 26 '19 09:12 AlexMesser

that's a pretty good idea. I guess a monospaced font would be ideal then?

JemiloII avatar Jan 02 '20 16:01 JemiloII

I am using a workaround for this issue:

  1. find max data length for each column
  2. set column width equal to found max value

example for typescript:

const names = ["Alice", "Bob", "Clara"]
const lengthArr = names.map(name => name.length)
const maxWidth = Math.max(...lengthArr)
ws.column(0).setWidth(maxWidth)

thanks. it's a good solution for one line. how about the wrapText is true? I need to find a way to adjust the row height automatically.

surfingtomchen avatar May 23 '20 18:05 surfingtomchen

@surfingtomchen, you can divide the text length by the column width (rounded up). E.g. text length is 27 and col width is 10 - row height will be 3. Btw, I don't remember what unit of measure is used for row height, is it a point, pixel or something else, but anyway you can use the found value as coefficient.

AlexMesser avatar Jun 16 '20 15:06 AlexMesser

@AlexMesser thanks for that one, good solution. It looks like the wrapText: true does not work on excel4node hey? For now just adujsted the width of the column and that seems to do the job :)

giordifungula avatar Oct 25 '20 12:10 giordifungula

Hi Guys, any update on this?

BruneXX avatar Nov 29 '20 23:11 BruneXX

No update?

appfrilans avatar May 11 '21 09:05 appfrilans

No update?

@appfrilans "For now just adujsted the width of the column and that seems to do the job :)" That was what I did when I was working on this, did you try it? That solution that was posted above does seem to also get the job done too at least for now.

giordifungula avatar May 11 '21 09:05 giordifungula

No update?

@appfrilans "For now just adujsted the width of the column and that seems to do the job :)" That was what I did when I was working on this, did you try it? That solution that was posted above does seem to also get the job done too at least for now.

Yes, setting manual width work but not quite ok if texts vary in length a lot. The issue was for auto-width, not manual.

appfrilans avatar May 11 '21 10:05 appfrilans

I am using a workaround for this issue:

  1. find max data length for each column
  2. set column width equal to found max value

example for typescript:

const names = ["Alice", "Bob", "Clara"]
const lengthArr = names.map(name => name.length)
const maxWidth = Math.max(...lengthArr)
ws.column(0).setWidth(maxWidth)

Thanks. It's worked for me!

iVinny avatar Jun 04 '21 22:06 iVinny