natergj / excel4node

Node module to allow for easy Excel file creation
MIT License
1.38k stars 214 forks source link

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

Open JemiloII opened 8 years ago

JemiloII commented 8 years ago

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.

chrishakai commented 8 years ago

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

andrewminton commented 7 years ago

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

amit-coditas commented 7 years ago

any workaround for this?

cesardmoro commented 6 years ago

Any fix for this ?

lerit commented 6 years ago

expect this feature!

natergj commented 6 years ago

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.

alasdaircr commented 6 years ago

@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

AlexMesser commented 4 years ago

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)
JemiloII commented 4 years ago

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

surfingtomchen commented 4 years ago

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.

AlexMesser commented 4 years ago

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

giordifungula commented 3 years ago

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

BruneXX commented 3 years ago

Hi Guys, any update on this?

appfrilans commented 3 years ago

No update?

giordifungula commented 3 years ago

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.

appfrilans commented 3 years ago

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.

iVinny commented 3 years ago

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!