How would I make a hotkey for freezing and auto-width?

Two very common operations which I do on opened document is to freeze its rows and columns, and then do automatic width to see full column names. But I struggled to find a simple way to add hotkeys for this action.

So, I would appreciate a lot if somebody could give me a straightforward way to do this. Thank you!

Did you want a single hotkey to perform both actions or separate hot key for each? Macro can be recorded for the freeze row and columns but doesn’t seem to record for column width. It can probably be done from macro written in BASIC, I’ll take a look at this and try to provide example.

Example Code in Basic for a single macro to perform both functions

REM  *****  BASIC  *****

Option Explicit

Sub SetFreezeAndWidth
	ThisComponent.CurrentController.freezeAtPosition(1, 1)
	ThisComponent.CurrentSelection.Spreadsheet.Columns.OptimalWidth = True
End Sub

To place this in a module. From the menu “Tools”, “Macros”, “Organise Macros”, “LibreOffice Basic…” then expand “My Macros”, select “Standard” and click on “New”. You can name the module or just keep the default and click “OK”. Copy the above code into the new module replacing the existing Main sub.

Assign the macro to a hot key using the menu “Tools”, “Customise…”. On the “Keyboard” tab. At the top right select the “Calc” radio button. Select an un-used shortcut key combination. In “Category” expand “LibreOffice Macros”, expand “user”, expand “Standard”, and select the module where you placed the code. In “Function” select the name of the sub “SetFreezeAndWidth”. Then click on “Modify” to set the macro to the hot key selected. Click on OK to exit the dialog.

For more details on macros check the Andrew Pitonyak book