This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
software:libreoffice:calc:macros:basic:button [2022/01/19 16:41] – [Basic code] admin | software:libreoffice:calc:macros:basic:button [2022/01/24 00:53] (current) – admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Using buttons within calc to perform basic function ====== | ====== Using buttons within calc to perform basic function ====== | ||
This describes how to write some code and setup your worksheet to perform a certain action when a button is pressed. | This describes how to write some code and setup your worksheet to perform a certain action when a button is pressed. | ||
+ | |||
==== Create a button in cell ==== | ==== Create a button in cell ==== | ||
View-> | View-> | ||
+ | |||
==== Configure button ==== | ==== Configure button ==== | ||
Change the label to something meaningful to the user: | Change the label to something meaningful to the user: | ||
* Right click button-> click on " | * Right click button-> click on " | ||
* Assigning a macro to the button-> | * Assigning a macro to the button-> | ||
+ | |||
==== Exit design mode ==== | ==== Exit design mode ==== | ||
All buttons will be useless to a user of the spreadsheet if the design mode is active. To change from design mode to active mode two more steps are required: | All buttons will be useless to a user of the spreadsheet if the design mode is active. To change from design mode to active mode two more steps are required: | ||
* the design mode is turned off by clicking the " | * the design mode is turned off by clicking the " | ||
- | ===== Basic code example | + | ===== Basic code examples |
+ | |||
+ | ==== elementary cell handling | ||
Paste code below into: | Paste code below into: | ||
Tools-> | Tools-> | ||
Line 36: | Line 41: | ||
</ | </ | ||
+ | ==== Create button to quickly navigate to certain sheet ==== | ||
+ | Or an example to create a button which, when clicked, jumps to a designated sheet: | ||
+ | - Copy-paste the script gotoSheet from below in the basic editor, which is reachable via the menu: Tools -> Macros -> Organize Macros -> Basic | ||
+ | - In a worksheet, create a button via the menu: View -> Toolbars -> Form Controls | ||
+ | - Click on the Design mode button | ||
+ | - Add a push button | ||
+ | - Right click on the button placed and select the Control Properties... | ||
+ | - In the General tab, Name field, add the name of the target sheet (As appears at the bottom tab of each sheet.) | ||
+ | - In the General tab, Label field, add a descriptive name which should appear on the button | ||
+ | - In the Events tab, Mouse button pressed, click on the ... and navigate to the script which you copy-pasted earlier. | ||
+ | - Leave the design mode. | ||
+ | |||
+ | < | ||
+ | Sub gotoSheet(oEvent) | ||
+ | Dim oCaller As Object | ||
+ | Dim sName As String | ||
+ | Dim oSheet As Object | ||
+ | oCaller=oEvent.Source.Model | ||
+ | sName = oCaller.Name | ||
+ | oSheet = ThisComponent.getSheets().getByName(sName) | ||
+ | ThisComponent.CurrentController.setActiveSheet(oSheet) | ||
+ | End Sub | ||
+ | </ | ||
===== Further reading ===== | ===== Further reading ===== | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// |