How to set optimal width programmingly by C#

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
winchell
Posts: 10
Joined: Thu Mar 31, 2022 5:45 am

How to set optimal width programmingly by C#

Post by winchell »

Hi i am developing with libre calc.
I want to set optimal width automitically.
I recorded the macro.
It's like blow:

Code: Select all

dispatcher.executeDispatch(document, ".uno:SetOptimalColumnWidthDirect", "", 0, Array())
Or

Code: Select all

dispatcher.executeDispatch(document, ".uno:SetOptimalColumnWidth", "", 0, Array())
But it didnt work with set property for SetOptimalColumnWidthDirect.

Can anyone help me on this one?
OpenOffice 7.3 on Windows 10
John_Ha
Volunteer
Posts: 9583
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: How to set optimal width programmingly by C#

Post by John_Ha »

IIRC I once recorded a macro to set View > Optimal width ..., and used Events to run the macro each time Writer opened a document. Does the same work in Calc?
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to set optimal width programmingly by C#

Post by JeJe »

What do you mean it didn't work?

SetOptimalColumnWidthDirect looks to always use the default value for the added width and ignore the aExtraWidth property value. As does this method:

Code: Select all

thiscomponent.sheets(0).columns(0).optimalwidth = true
(note just looked at this in OO not LO)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: How to set optimal width programmingly by C#

Post by karolus »

Just assign the following one-line-python to (→Rightclick Sheettab → Events →Content changed ):

Code: Select all

def optimal_width(event):
    event.Columns.OptimalWidth = True
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Post Reply