How to add a "scale sheet to fit number of pages" button?

Is it possible to define a new custom toolbar key which is a shortcut to format->page style->scaling mode->fit on number of pages->1 ?.


Maintain one or more page styles prepared for expectable purposes in your respective template for spreadsheet documents. Then simply apply the wanted style via the ‘Stylist’ (F11).

As far as I can tell the option can’t be set via the customization dialog.
You need to create a “macro” for the purpose, and to bind it to a sensitive element. I would suggest to not use a button (FormControl), but a sensitive area (text or icon) of a toolbar. You surely know how to add elements to a toolbar, or to create an extra toolbar.

If you aren’t familiar with macro programming, you can use the following code I wrote for you:

Sub setScaleToPages(Optional pPageStyleName As String, Optional pNumPages As Long)
REM Be aware of the fact that this is NOT just for the next printout, but persisting
REM with the document, and in case you use it for a template, also for derived documents.
If IsMissing(pNumPages) Then pNumPages = 1
doc = ThisComponent
If IsMissing(pPageStyleName) Then
 actSheet = doc.CurrentController.ActiveSheet
 pPageStyleName = actSheet.PageStyle
docStyleFamilies          = doc.StyleFamilies
docPageStyles             = docStyleFamilies.getByName("PageStyles")
thePageStyle              = docPageStyles.getByName(pPageStyleName)
thePageStyle.ScaleToPages = 1
End Sub

((Sorry. Automatic formatting by the “software” used for this site didn’t accept my choice. It shouldn’t spoil the code itself, however.))

<edit datetimeabout="2022-07-05 10:00 UTC">
Missed to mention in the original answer that you should call the finally working Sub via an interposed Sub selecting and processing cases. It depends on the way the Sub is called what kind of parameter it gets passed (if any). Called from a menu item (or the>Macros>Run dialog) there is no parameter passed. A call from a toolbar passes an Integer telling what modifier keys were pressed, and called from a FormControl the Sub gets passed an event object. </edit>

  1. EndIf => End If
  2. While / Wend => Do While / Loop
  3. as Variant

I would expect “preformatted text” to be shown as IIIII enter/paste it.
You see, I’m an ego-maniac.
Concerning the reserved words of StarBasic; EndIf is a lso accepted, and I prefer this variant.

BTW The above code was successfully tested under LibO V

The built-in Markdown interpreter prefers its own rules with maniacal persistence. Otherwise, he will not agree to the recognition of LO Basic. :smile:
See also here.

Thank you.