How to bold a row in macro and how to auto-width columns?

Hi,

I import data into Calc (LibreOffice 6.1.2) all the time. After I do that I always do two tasks:

  1. Mark whole row and bold it.
  2. Mark whole file and auto-width it.

I have recorded short video what I do: https://i.imgur.com/Q5yG2kh.gif

How to write a macro to do the above tasks?

Note: I have different number of columns in every data import, so solution should take into consideration that all of the cells in the row are bolded and all of the columns are auto-widthed.

Thanks

Hello,

you’ve got to get a range so that you may configure it.

As you want to process all possible columns (and only the first row as for bold-ing characters), just get A1:AMJ1. Then, use this range and set it according to your needs, as shown in the code below.
The first line gets the range object.
The next one sets the font weight as bold.
The third sets the column optimal widths.

For the record, the code processes the first sheet in the current document (ThisComponent.Sheet(0)).

Sub Main

	Dim oRange As Object
	
	oRange = ThisComponent.Sheets(0).getCellRangeByName("A1:AMJ1")
	oRange.CharWeight = com.sun.star.awt.FontWeight.BOLD
	oRange.Columns.OptimalWidth = True

End Sub

Best regards,

Thanks a lot. It works exactly as I need.

I am curious if this is an outdated solution or the LO is seriously effed up
-in my case on Linux and v 7.5.3.2. - as I am trying to to the same macro and does not make bold anything on those cells…

Hi Eddy,

Version: 7.5.3.2 (X86_64) / LibreOffice Community
Build ID: 9f56dff12ba03b9acd7730a5a481eea045e468f3
CPU threads: 4; OS: Linux 5.4; UI render: default; VCL: gtk3
Locale: fr-FR (fr_FR.UTF-8); UI: fr-FR
Calc: threaded

under Linux Mint 20.3 Cinnamon

the code above works as “advertized” :wink:

thanks, its a bit weird, now I noticed that macro works ok on vanilla sheet but not on a specific sheet I have - its an XLSX btw - that may be the issue…

ahah! Yes, the XLSX format is probably the culprit here.

A rule of thumb is, to get the best of an application, to always use the native format of that tool. Thus, in LibreOffice, use the ODF format (ODS for speadsheets).