summaryrefslogtreecommitdiff
path: root/wizards
diff options
context:
space:
mode:
authorJean-Pierre Ledure <jp@ledure.be>2021-11-14 17:28:04 +0100
committerJean-Pierre Ledure <jp@ledure.be>2021-11-15 13:47:26 +0100
commit6b4f02396b1c0e113be88c82126e23099dc78737 (patch)
tree008f83cee65557b0e83c936e62e710969072237a /wizards
parent97c59688a074dc24092c2887ed665b6940f4c97f (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.mk1
-rw-r--r--wizards/source/scriptforge/python/scriptforge.py3
-rw-r--r--wizards/source/sfdocuments/SF_Calc.xba82
-rw-r--r--wizards/source/sfdocuments/SF_DocumentListener.xba114
-rw-r--r--wizards/source/sfdocuments/script.xlb1
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
, &quot;MoveRange&quot; _
, &quot;MoveSheet&quot; _
, &quot;Offset&quot; _
+ , &quot;OpenRangeSelector&quot; _
, &quot;Printf&quot; _
, &quot;PrintOut&quot; _
, &quot;RemoveSheet&quot; _
@@ -1862,6 +1863,87 @@ Catch:
End Function &apos; 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
+&apos;&apos;&apos; Activates the Calc document, opens a non-modal dialog with a text box,
+&apos;&apos;&apos; let the user make a selection in the current or another sheet and
+&apos;&apos;&apos; returns the selected area as a string.
+&apos;&apos;&apos; This method does not change the current selection.
+&apos;&apos;&apos; Args:
+&apos;&apos;&apos; Title: the title to display on the top of the dialog
+&apos;&apos;&apos; Selection: a default preselection as a String. When absent, the first element of the
+&apos;&apos;&apos; current selection is preselected.
+&apos;&apos;&apos; SingleCell: When True, only a single cell may be selected. Default = False
+&apos;&apos;&apos; CloseAfterSelect: When True (default-, the dialog is closed immediately after
+&apos;&apos;&apos; the selection. When False, the user may change his/her mind and must close
+&apos;&apos;&apos; the dialog manually.
+&apos;&apos;&apos; Returns:
+&apos;&apos;&apos; The selected range as a string, or the empty string when the user cancelled the request (close window button)
+&apos;&apos;&apos; Exceptions:
+&apos;&apos;&apos; Examples:
+&apos;&apos;&apos; Dim sSelect As String, vValues As Variant
+&apos;&apos;&apos; sSelect = oDoc.OpenRangeSelector(&quot;Select a range ...&quot;)
+&apos;&apos;&apos; If sSelect = &quot;&quot; Then Exit Function
+&apos;&apos;&apos; vValues = oDoc.GetValue(sSelect)
+
+Dim sSelector As String &apos; Return value
+Dim vPropertyValues As Variant &apos; Array of com.sun.star.beans.PropertyValue
+Dim oSelection As Object &apos; The current selection before opening the selector
+Dim oAddress As Object &apos; Preselected address as _Address
+
+Const cstThisSub = &quot;SFDocuments.Calc.OpenRangeSelector&quot;
+Const cstSubArgs = &quot;[Title=&quot;&quot;&quot;&quot;], [Selection=&quot;&quot;~&quot;&quot;], [SingleCell=False], [CloseAfterSelect=True]&quot;
+
+ If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
+ sSelector = &quot;&quot;
+
+Check:
+ If IsMissing(Title) Or IsEmpty(Title) Then Title = &quot;&quot;
+ If IsMissing(Selection) Or IsEmpty(Selection) Then Selection = &quot;~&quot;
+ 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, &quot;Title&quot;, V_STRING) Then GoTo Finally
+ If Not ScriptForge.SF_Utils._Validate(Selection, &quot;Selection&quot;, V_STRING) Then GoTo Finally
+ If Not ScriptForge.SF_Utils._Validate(SingleCell, &quot;SingleCell&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
+ If Not ScriptForge.SF_Utils._Validate(CloseAfterSelect, &quot;CloseAfterSelect&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
+ End If
+
+Try:
+ &apos; Save the current selections
+ Set oSelection = _Component.CurrentController.getSelection()
+
+ &apos; Process preselection and select its containing sheet
+ Set oAddress = _ParseAddress(Selection)
+ Activate(oAddress.SheetName)
+
+ &apos; Build arguments array and execute the dialog box
+ With ScriptForge.SF_Utils
+ vPropertyValues = Array( _
+ ._MakePropertyValue(&quot;Title&quot;, Title) _
+ , ._MakePropertyValue(&quot;CloseOnMouseRelease&quot;, CloseAfterSelect) _
+ , ._MakePropertyValue(&quot;InitialValue&quot;, oAddress.XCellRange.AbsoluteName) _
+ , ._MakePropertyValue(&quot;SingleCellMode&quot;, SingleCell) _
+ )
+ End With
+ sSelector = SF_DocumentListener.RunRangeSelector(_Component, vPropertyValues)
+
+ &apos; Restore the saved selections
+ _RestoreSelections(_Component, oSelection)
+
+Finally:
+ OpenRangeSelector = sSelector
+ ScriptForge.SF_Utils._ExitFunction(cstThisSub)
+ Exit Function
+Catch:
+ GoTo Finally
+End Function &apos; 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
+
+&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
+&apos;&apos;&apos; SF_DocumentListener
+&apos;&apos;&apos; ===================
+&apos;&apos;&apos; The current module is dedicated to the management of document events + listeners, triggered by user actions,
+&apos;&apos;&apos; which cannot be defined with the Basic IDE
+&apos;&apos;&apos;
+&apos;&apos;&apos; Concerned listeners:
+&apos;&apos;&apos; com.sun.star.sheet.XRangeSelectionListener
+&apos;&apos;&apos; allowing a user to select a cell range at any moment
+&apos;&apos;&apos;
+&apos;&apos;&apos; The described events/listeners are processed by UNO listeners
+&apos;&apos;&apos;
+&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
+
+REM ================================================================= DEFINITIONS
+
+REM ============================================================= PRIVATE MEMBERS
+
+Private _SelectedRange As String &apos; The selected range is returned by a &quot;done&quot; event
+Private _RangeSelectionFinished As Boolean &apos; 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
+&apos;&apos;&apos; Called from the SF_Calc.OpenRangeSelector() method
+&apos;&apos;&apos; Opens a non-modal dialog with a text box,
+&apos;&apos;&apos; let the user make a selection in the current or another sheet and
+&apos;&apos;&apos; returns the selected area as a string.
+
+Dim oController As Object &apos; com.sun.star.frame.Controller
+Dim oListener As Object &apos; com.sun.star.sheet.XRangeSelectionListener
+Dim lCountLoops As Long &apos; Sleep cycles counter
+
+Const cstListenerPrefix = &quot;_SFRGSEL_&quot; &apos; Prefix used for naming events Subs
+Const cstSleep = 50 &apos; Sleep steps in ms while waiting for the end of the interaction
+Const cstMaxSleep = (60 * 5 * 1000) / cstSleep &apos; Never sleep more than 5 minutes. Afterwards, processing continues
+
+ On Local Error GoTo Catch &apos; Avoid stopping event scripts
+
+Try:
+ &apos; Create the listener
+ Set oController = poComponent.CurrentController
+ Set oListener = CreateUnoListener(cstListenerPrefix, &quot;com.sun.star.sheet.XRangeSelectionListener&quot;)
+ oController.addRangeSelectionListener(oListener)
+
+ &apos; Open the selector
+ _SelectedRange = &quot;&quot;
+ _RangeSelectionFinished = False
+ oController.startRangeSelection(pvPropertyValues)
+
+ &apos; Dummy thread synchonization
+ lCountLoops = 0
+ Do While Not _RangeSelectionFinished And lCountLoops &lt; 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 &apos; SFDocuments.SF_DocumentListener.RunRangeSelector
+
+REM ============================================================= PRIVATE METHODS
+
+REM -----------------------------------------------------------------------------
+Sub _SFRGSEL_done(Optional poEvent As Object) &apos; com.sun.star.sheet.RangeSelectionEvent
+
+ On Local Error GoTo Catch &apos; 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) &apos; com.sun.star.sheet.RangeSelectionEvent
+
+ On Local Error GoTo Catch &apos; 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