diff options
author | Jean-Pierre Ledure <jp@ledure.be> | 2021-11-14 17:28:04 +0100 |
---|---|---|
committer | Jean-Pierre Ledure <jp@ledure.be> | 2021-11-15 13:47:26 +0100 |
commit | 6b4f02396b1c0e113be88c82126e23099dc78737 (patch) | |
tree | 008f83cee65557b0e83c936e62e710969072237a /wizards | |
parent | 97c59688a074dc24092c2887ed665b6940f4c97f (diff) |
ScriptForge - (SF_Calc) new OpenRangeSelector() method
The method activates the Calc document, opens a non-modal dialog
with a text box, let the user make a selection in the current
or another sheet and returns the selected area as a string.
This method does not change the current selection.
Arguments:
Title: the title to display on the top of the dialog
Selection: a default preselection as a String.
When absent, the first element of the current selection is preselected.
SingleCell: When True, only a single cell may be selected. Default = False
CloseAfterSelect: When True (default-, the dialog is closed immediately
after the selection. When False, the user may change his/her mind
and must close the dialog manually.
Returns:
The selected range as a string, or the empty string
when the user cancelled the request (close window button)
Example:
Dim sSelect As String, vValues As Variant
sSelect = oDoc.OpenRangeSelector("Select a range ...")
If sSelect = "" Then Exit Function
vValues = oDoc.GetValue(sSelect)
The implementation requires a new module: SF_DocumentListener.xba
The method is available for Basic and Python user scripts.
Change-Id: I2d67a9c900ea8ac661cd6b6fb43bb4a34e6abd8e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125201
Tested-by: Jean-Pierre Ledure <jp@ledure.be>
Reviewed-by: Jean-Pierre Ledure <jp@ledure.be>
Diffstat (limited to 'wizards')
-rw-r--r-- | wizards/Package_sfdocuments.mk | 1 | ||||
-rw-r--r-- | wizards/source/scriptforge/python/scriptforge.py | 3 | ||||
-rw-r--r-- | wizards/source/sfdocuments/SF_Calc.xba | 82 | ||||
-rw-r--r-- | wizards/source/sfdocuments/SF_DocumentListener.xba | 114 | ||||
-rw-r--r-- | wizards/source/sfdocuments/script.xlb | 1 |
5 files changed, 201 insertions, 0 deletions
diff --git a/wizards/Package_sfdocuments.mk b/wizards/Package_sfdocuments.mk index bec0d11f5a9a..a2a03178736a 100644 --- a/wizards/Package_sfdocuments.mk +++ b/wizards/Package_sfdocuments.mk @@ -24,6 +24,7 @@ $(eval $(call gb_Package_add_files,wizards_basicsrvsfdocuments,$(LIBO_SHARE_FOLD SF_Calc.xba \ SF_Chart.xba \ SF_Document.xba \ + SF_DocumentListener.xba \ SF_Form.xba \ SF_FormControl.xba \ SF_Register.xba \ diff --git a/wizards/source/scriptforge/python/scriptforge.py b/wizards/source/scriptforge/python/scriptforge.py index 8b6af8ecdc99..9dc95d21f715 100644 --- a/wizards/source/scriptforge/python/scriptforge.py +++ b/wizards/source/scriptforge/python/scriptforge.py @@ -2010,6 +2010,9 @@ class SFDocuments: width = ScriptForge.cstSymEmpty): return self.ExecMethod(self.vbMethod, 'Offset', range, rows, columns, height, width) + def OpenRangeSelector(self, title = '', selection = '~', singlecell = False, closeafterselect = True): + return self.ExecMethod(self.vbMethod, 'OpenRangeSelector', title, selection, singlecell, closeafterselect) + def Printf(self, inputstr, range, tokencharacter = '%'): return self.ExecMethod(self.vbMethod, 'Printf', inputstr, range, tokencharacter) diff --git a/wizards/source/sfdocuments/SF_Calc.xba b/wizards/source/sfdocuments/SF_Calc.xba index 56fc83c18cb4..0eca75aa25f3 100644 --- a/wizards/source/sfdocuments/SF_Calc.xba +++ b/wizards/source/sfdocuments/SF_Calc.xba @@ -1662,6 +1662,7 @@ Public Function Methods() As Variant , "MoveRange" _ , "MoveSheet" _ , "Offset" _ + , "OpenRangeSelector" _ , "Printf" _ , "PrintOut" _ , "RemoveSheet" _ @@ -1862,6 +1863,87 @@ Catch: End Function ' SF_Documents.SF_Calc.Offset REM ----------------------------------------------------------------------------- +Public Function OpenRangeSelector(Optional ByVal Title As Variant _ + , Optional ByVal Selection As Variant _ + , Optional ByVal SingleCell As Variant _ + , Optional ByVal CloseAfterSelect As Variant _ + ) As String +''' Activates the Calc document, opens a non-modal dialog with a text box, +''' let the user make a selection in the current or another sheet and +''' returns the selected area as a string. +''' This method does not change the current selection. +''' Args: +''' Title: the title to display on the top of the dialog +''' Selection: a default preselection as a String. When absent, the first element of the +''' current selection is preselected. +''' SingleCell: When True, only a single cell may be selected. Default = False +''' CloseAfterSelect: When True (default-, the dialog is closed immediately after +''' the selection. When False, the user may change his/her mind and must close +''' the dialog manually. +''' Returns: +''' The selected range as a string, or the empty string when the user cancelled the request (close window button) +''' Exceptions: +''' Examples: +''' Dim sSelect As String, vValues As Variant +''' sSelect = oDoc.OpenRangeSelector("Select a range ...") +''' If sSelect = "" Then Exit Function +''' vValues = oDoc.GetValue(sSelect) + +Dim sSelector As String ' Return value +Dim vPropertyValues As Variant ' Array of com.sun.star.beans.PropertyValue +Dim oSelection As Object ' The current selection before opening the selector +Dim oAddress As Object ' Preselected address as _Address + +Const cstThisSub = "SFDocuments.Calc.OpenRangeSelector" +Const cstSubArgs = "[Title=""""], [Selection=""~""], [SingleCell=False], [CloseAfterSelect=True]" + + If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch + sSelector = "" + +Check: + If IsMissing(Title) Or IsEmpty(Title) Then Title = "" + If IsMissing(Selection) Or IsEmpty(Selection) Then Selection = "~" + If IsMissing(SingleCell) Or IsEmpty(SingleCell) Then SingleCell = False + If IsMissing(CloseAfterSelect) Or IsEmpty(CloseAfterSelect) Then CloseAfterSelect = True + If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then + If Not _IsStillAlive() Then GoTo Finally + If Not ScriptForge.SF_Utils._Validate(Title, "Title", V_STRING) Then GoTo Finally + If Not ScriptForge.SF_Utils._Validate(Selection, "Selection", V_STRING) Then GoTo Finally + If Not ScriptForge.SF_Utils._Validate(SingleCell, "SingleCell", ScriptForge.V_BOOLEAN) Then GoTo Finally + If Not ScriptForge.SF_Utils._Validate(CloseAfterSelect, "CloseAfterSelect", ScriptForge.V_BOOLEAN) Then GoTo Finally + End If + +Try: + ' Save the current selections + Set oSelection = _Component.CurrentController.getSelection() + + ' Process preselection and select its containing sheet + Set oAddress = _ParseAddress(Selection) + Activate(oAddress.SheetName) + + ' Build arguments array and execute the dialog box + With ScriptForge.SF_Utils + vPropertyValues = Array( _ + ._MakePropertyValue("Title", Title) _ + , ._MakePropertyValue("CloseOnMouseRelease", CloseAfterSelect) _ + , ._MakePropertyValue("InitialValue", oAddress.XCellRange.AbsoluteName) _ + , ._MakePropertyValue("SingleCellMode", SingleCell) _ + ) + End With + sSelector = SF_DocumentListener.RunRangeSelector(_Component, vPropertyValues) + + ' Restore the saved selections + _RestoreSelections(_Component, oSelection) + +Finally: + OpenRangeSelector = sSelector + ScriptForge.SF_Utils._ExitFunction(cstThisSub) + Exit Function +Catch: + GoTo Finally +End Function ' SF_Documents.SF_Calc.OpenRangeSelector + +REM ----------------------------------------------------------------------------- Public Function Printf(Optional ByVal InputStr As Variant _ , Optional ByVal Range As Variant _ , Optional ByVal TokenCharacter As Variant _ diff --git a/wizards/source/sfdocuments/SF_DocumentListener.xba b/wizards/source/sfdocuments/SF_DocumentListener.xba new file mode 100644 index 000000000000..9ccc04f62d19 --- /dev/null +++ b/wizards/source/sfdocuments/SF_DocumentListener.xba @@ -0,0 +1,114 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> +<script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_DocumentListener" script:language="StarBasic" script:moduleType="normal">REM ======================================================================================================================= +REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. === +REM === The SFDocuments library is one of the associated libraries. === +REM === Full documentation is available on https://help.libreoffice.org/ === +REM ======================================================================================================================= + +Option Compatible +Option Explicit + +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' +''' SF_DocumentListener +''' =================== +''' The current module is dedicated to the management of document events + listeners, triggered by user actions, +''' which cannot be defined with the Basic IDE +''' +''' Concerned listeners: +''' com.sun.star.sheet.XRangeSelectionListener +''' allowing a user to select a cell range at any moment +''' +''' The described events/listeners are processed by UNO listeners +''' +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' + +REM ================================================================= DEFINITIONS + +REM ============================================================= PRIVATE MEMBERS + +Private _SelectedRange As String ' The selected range is returned by a "done" event +Private _RangeSelectionFinished As Boolean ' Flag indicating that the interaction with the user has stopped + +REM ================================================================== EXCEPTIONS + +REM ============================================================== PUBLIC METHODS + +REM ----------------------------------------------------------------------------- +Public Function RunRangeSelector(ByRef poComponent As Object _ + , ByRef pvPropertyValues As Variant _ + ) As String +''' Called from the SF_Calc.OpenRangeSelector() method +''' Opens a non-modal dialog with a text box, +''' let the user make a selection in the current or another sheet and +''' returns the selected area as a string. + +Dim oController As Object ' com.sun.star.frame.Controller +Dim oListener As Object ' com.sun.star.sheet.XRangeSelectionListener +Dim lCountLoops As Long ' Sleep cycles counter + +Const cstListenerPrefix = "_SFRGSEL_" ' Prefix used for naming events Subs +Const cstSleep = 50 ' Sleep steps in ms while waiting for the end of the interaction +Const cstMaxSleep = (60 * 5 * 1000) / cstSleep ' Never sleep more than 5 minutes. Afterwards, processing continues + + On Local Error GoTo Catch ' Avoid stopping event scripts + +Try: + ' Create the listener + Set oController = poComponent.CurrentController + Set oListener = CreateUnoListener(cstListenerPrefix, "com.sun.star.sheet.XRangeSelectionListener") + oController.addRangeSelectionListener(oListener) + + ' Open the selector + _SelectedRange = "" + _RangeSelectionFinished = False + oController.startRangeSelection(pvPropertyValues) + + ' Dummy thread synchonization + lCountLoops = 0 + Do While Not _RangeSelectionFinished And lCountLoops < cstMaxSleep + Wait(cstSleep) + lCountLoops = lCountLoops + 1 + Loop + +Finally: + If Not IsNull(oListener) Then oController.removeRangeSelectionListener(oListener) + RunRangeSelector = _SelectedRange + Exit Function +Catch: + GoTo Finally +End Function ' SFDocuments.SF_DocumentListener.RunRangeSelector + +REM ============================================================= PRIVATE METHODS + +REM ----------------------------------------------------------------------------- +Sub _SFRGSEL_done(Optional poEvent As Object) ' com.sun.star.sheet.RangeSelectionEvent + + On Local Error GoTo Catch ' Avoid stopping event scripts + +Try: + _SelectedRange = poEvent.RangeDescriptor + _RangeSelectionFinished = True + +Finally: + Exit Sub +Catch: + GoTo Finally +End Sub + +REM ----------------------------------------------------------------------------- +Sub _SFRGSEL_aborted(Optional poEvent As Object) ' com.sun.star.sheet.RangeSelectionEvent + + On Local Error GoTo Catch ' Avoid stopping event scripts + +Try: + _RangeSelectionFinished = True + +Finally: + Exit Sub +Catch: + GoTo Finally +End Sub + +REM ============================================ END OF SFDIALOGS.SF_DIALOGLISTENER +</script:module>
\ No newline at end of file diff --git a/wizards/source/sfdocuments/script.xlb b/wizards/source/sfdocuments/script.xlb index 0663d7b64e6a..ff4495124f98 100644 --- a/wizards/source/sfdocuments/script.xlb +++ b/wizards/source/sfdocuments/script.xlb @@ -10,4 +10,5 @@ <library:element library:name="SF_FormControl"/> <library:element library:name="SF_Writer"/> <library:element library:name="SF_Chart"/> + <library:element library:name="SF_DocumentListener"/> </library:library>
\ No newline at end of file |