spreadsheet: auto fit columns?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

spreadsheet: auto fit columns?

John W-7
I'm writing out .ODS files via a program.
I'd like to find a way to adjust all the column widths such that no
cells "spill over" when the file is opened in Calc.
That is: for each column, I want the column width to be that of the
widest cell in that column.

I'm not using UNO since that requires at least URE to be installed on
the machine generating the files. But if someone has a solid way of
getting this to work with UNO, I'd be interested to hear, regardless.

Again, this is for generated data - I realize I can just click on
things in Calc to get the desired effect (:

Things I've tried:

(1)
Using style:use-optimal-column-width, like so:
    ...
    <style:table-column-properties style:use-optimal-column-width="true" ... />
    ...

This does not work, as far as I can tell. Calc does not seem to save
out this property (thus it's hard to get a working example), nor does
it seem to respect the attribute when it reads the file.
Also, the spec says this attribute "specifies that the column width
should be recalculated automatically if some content in the column
changes" so I'm not even sure if it would achieve the effect I want
unless someone actually changed the contents of the column...

(2)
Calculate the width manually and hardcode it into the
style:column-width attribute.
This might work - I'm still working on it.
I happen to be using C# on Windows, and I can get the width of some
text in a given font with
System.Windows.Forms.TextRenderer.MeasureText, but that returns a
value in pixels.
Calc only uses "real world" units such as inches, centimeters, points,
etc. I don't know the DPI conversion that Calc will use.
Perhaps I can just guess that it's 72 or 96 DPI, but that's fragile.
Probably I can discover the conversion factor TextRenderer.MeasureText
uses and thus get "real world" units to begin with.

But that still leaves the issue of knowing what font will be used by default.
Calc seems to use Arial 10 point, but is that guaranteed?

(3)
Insert a macro into the spreadsheet that adjusts the widths on document load.
This actually works (!!) but is a bit of a pain.
It requires people to enable macros in Calc (with nasty security warnings).
It also means a bunch more stuff has to get written out to the
document - the macro itself, the link information, etc.


Anyone have other ideas?
Surely others have encountered this issue... Yet I have googled quite
a bit and searched the mailing lists with no luck.

Any help is appreciated (:
-John

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: spreadsheet: auto fit columns?

Niklas Nebel
On 02/23/10 03:38, John W wrote:
> I'm writing out .ODS files via a program.
> I'd like to find a way to adjust all the column widths such that no
> cells "spill over" when the file is opened in Calc.
> That is: for each column, I want the column width to be that of the
> widest cell in that column.

This doesn't really belong into the "dba" lists, but anyway...

> I'm not using UNO since that requires at least URE to be installed on
> the machine generating the files. But if someone has a solid way of
> getting this to work with UNO, I'd be interested to hear, regardless.

Using the API, you can set the column's property "OptimalWidth" to true.
This doesn't really set a property, but applies the optimal width instead.

> (2)
> Calculate the width manually and hardcode it into the
> style:column-width attribute.
> This might work - I'm still working on it.

> But that still leaves the issue of knowing what font will be used by default.
> Calc seems to use Arial 10 point, but is that guaranteed?

If you create the file, you can specify the font (in the default-style
element).

Niklas

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]