Macros/ScriptForge/ListExtractorExample
Appearance
< Macros | ScriptForge
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
How to make a list extractor
Authored by Jean-Pierre Ledure.
Use the right, left, up and down arrows to move items from a listbox to another listbox and rearrange them.

How to run it in BASIC
Create a new document.
- Open the Basic IDE
- Select the first available blank module
- Copy and paste the Basic code
- Verify line 16
- Run Main()
How to run it in Python
Create a new document.
- Run APSO
- Create a new module, call it 'Module1'
- Copy and paste the Python code below
- Verify line 15
- Save and run the Main() method
Code
REM SCRIPTFORGE WIKI EXAMPLE
REM How to make a list selector ?
REM Minimal required version: LibreOffice 7.6
REM Used services
REM SFDialogs.Dialog, SFDialogs.DialogControl, ScriptForge.Array
Option Explicit
Public dialog As Object ' The SFDialogs.Dialog object
Public inputlist As Object ' A SFDialogs.DialogControl object
Public outputlist As Object ' Id.
'*******************************************************************
'*** Adjust module name if not "Module1'
'*******************************************************************
Const onaction = "vnd.sun.star.script:Standard.Module1.ProcessLists?language=Basic&location=document"
'*******************************************************************
'* Run the demo below
'*******************************************************************
Sub Main(Optional event As Object)
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
SetupDialog()
dialog.Execute()
dialog.Terminate()
End Sub
'*******************************************************************
'* Specific demo code
'*******************************************************************
Sub ProcessLists(event As Object)
' Routine triggered by each of the btnMove... buttons
Dim button As Object ' The SFDialogs.DialogControl instance representing the clicked button
Dim arr As Object ' ScriptForge.SF_Array module
Dim inList As Variant ' Input list content as an array
Dim outlist As Variant ' Output list content as an array
Dim invalue As String ' The selected item in the input list
Dim outvalue As String ' The selected item in the output list
Dim inindex As Integer ' Index of the selected item in the input list
Dim outindex As Integer ' Index of the selected item in the output list
Set arr = CreateScriptService("Array")
' Identify the clicked button
If IsNull(event) Then Exit Sub
Set button = CreateScriptService("DialogEvent", event)
' Load the actual list contents
inlist = inputlist.RowSource
outlist = outputlist.RowSource
invalue = inputlist.Value
outvalue = outputlist.Value
inindex = inputlist.ListIndex
outindex = outputlist.ListIndex
' Act depending on the clicked button
Select Case button.Name
Case "btnMoveToRight" ' Move the selected item to the end of the output list
If UBound(inlist) < 0 Then Exit Sub ' Do nothing if the input list is empty
inlist(inindex) = ""
inlist = arr.TrimArray(inlist)
inindex = 0
outlist = arr.Append(outlist, invalue)
outindex = UBound(outlist)
Case "btnMoveToLeft" ' Bring back the item selected in the output list
If UBound(outlist) < 0 Then Exit Sub ' Do nothing if the output list is empty
outlist(outindex) = ""
outlist = arr.TrimArray(outlist)
outindex = 0
inlist = arr.InsertSorted(inlist, outvalue)
inindex = arr.IndexOf(inlist, outvalue, sortorder := "ASC")
Case "btnMoveUp" ' Exchange the selected output item with its predecessor
If UBound(outlist) < 1 Then Exit Sub ' Do nothing if there is less than 2 items
If outindex = 0 Then Exit Sub ' Do nothing if the first item is selected
outlist(outindex) = ""
outindex = outindex - 1
outlist = arr.TrimArray(arr.Insert(outlist, outindex, outvalue))
Case "btnMoveDown" ' Exchange the selected output item with its successor
If UBound(outlist) < 1 Then Exit Sub ' Do nothing if there is less than 1 item
If outindex = UBound(outlist) Then Exit Sub ' Do nothing if the last item is selected
outlist(outindex) = ""
outindex = outindex + 1
outlist = arr.TrimArray(arr.Insert(outlist, outindex + 1, outvalue))
Case Else
Exit Sub
End Select
' Adapt the new lists
inputlist.RowSource = inlist
inputlist.ListIndex = inindex
outputlist.RowSource = outlist
outputlist.ListIndex = outindex
End Sub
'*******************************************************************
'* May be defined with the Basic IDE
'*******************************************************************
Sub SetupDialog()
' Build from scratch the example dialog and its controls
Dim control As Object ' A SFDialogs.DialogControl object
Set dialog = CreateScriptService("NewDialog", "ListSelector", Array(150, 75, 180, 125))
dialog.Caption = "List extractor"
With dialog
' The close button
Set control = .CreateButton("btnClose", place := Array(140, 10, 30, 10), push := "OK")
control.Caption = "Close"
' The listboxes
Set inputlist = .CreateListBox("lstIn", place := Array(25, 40, 40, 70), _
dropdown := False, linecount := 8, multiselect := False)
inputlist.RowSource = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", ",")
inputlist.ListIndex = 0 ' Select the first item
Set outputlist = .CreateListBox("lstOut", place := Array(100, 40, 40, 70), _
dropdown := False, linecount := 8, multiselect := False)
' The buttons
Set control = .CreateButton("btnMoveToRight", place := Array(80, 45, 15, 10))
control.Caption = "→" ' U+2192
control.OnActionPerformed = onaction
Set control = .CreateButton("btnMoveToLeft", place := Array(70, 60, 15, 10))
control.Caption = "←" ' U+2190
control.OnActionPerformed = onaction
Set control = .CreateButton("btnMoveUp", place := Array(150, 45, 15, 10))
control.Caption = "↑" ' U+2191
control.OnActionPerformed = onaction
Set control = .CreateButton("btnMoveDown", place := Array(150, 60, 15, 10))
control.Caption = "↓" ' U+2193
control.OnActionPerformed = onaction
End With
End Sub
# coding: utf-8
from __future__ import unicode_literals
### SCRIPTFORGE WIKI EXAMPLE
### How to make a list selector ?
### Minimal required version: LibreOffice 7.6
### Used services
### SFDialogs.Dialog, SFDialogs.DialogControl
from scriptforge import CreateScriptService
#*******************************************************************
# Adjust module name if not 'Module1'
#*******************************************************************
onaction = 'vnd.sun.star.script:Module1.py$ProcessLists?language=Python&location=document'
#*******************************************************************
#* Run the demo below
#*******************************************************************
def Main(event = None):
# dialog The SFDialogs.Dialog object
dialog = SetupDialog()
dialog.Execute()
dialog.Terminate()
#*******************************************************************
#* Specific demo code
#*******************************************************************
def ProcessLists(event = None):
# Routine triggered at run-time by each of the btnMove... buttons
# button The dialog control instance representing the clicked button
# dialog The Dialog object
# inputlist The input listbox
# outputlist The input listbox
# inlist Input list content as an array
# outlist Output list content as an array
# invalue The selected item in the input list
# outvalue The selected item in the output list
# inindex Index of the selected item in the input list
# outindex Index of the selected item in the output list
# Identify the clicked button and the listboxes
if event is None:
return
button = CreateScriptService('DialogEvent', event)
dialog = button.Parent
inputlist = dialog.Controls('lstIn')
outputlist = dialog.Controls('lstOut')
# Load the actual list contents
inlist = list(inputlist.RowSource) # RowSource returns a not modifiable tuple
outlist = list(outputlist.RowSource)
invalue = inputlist.Value
outvalue = outputlist.Value
inindex = inputlist.ListIndex
outindex = outputlist.ListIndex
# Act depending on the clicked button
if button.Name == 'btnMoveToRight': # Move the selected item to the end of the output list
if len(inlist) == 0: # Do nothing if the input list is empty
return
inlist.pop(inindex)
inindex = 0
outlist.append(invalue)
outindex = len(outlist) - 1
elif button.Name == 'btnMoveToLeft': # Bring back the item selected in the output list
if len(outlist) <= 0: # Do nothing if the output list is empty
return
outlist.pop(outindex)
outindex = 0
inlist.append(outvalue)
inlist.sort()
inindex = inlist.index(outvalue)
elif button.Name == 'btnMoveUp': # Exchange the selected output item with its predecessor
if len(outlist) < 2: # Do nothing if there is less than 2 items
return
if outindex == 0: # Do nothing if the first item is selected
return
outlist.pop(outindex)
outindex = outindex - 1
outlist.insert(outindex, outvalue)
elif button.Name == 'btnMoveDown': # Exchange the selected output item with its successor
if len(outlist) < 1: # Do nothing if there is less than 1 item
return
if outindex == len(outlist) - 1: # Do nothing if the last item is selected
return
outlist.pop(outindex)
outindex = outindex + 1
outlist.insert(outindex, outvalue)
else:
return
# Adapt the new lists
inputlist.RowSource = inlist
inputlist.ListIndex = inindex
outputlist.RowSource = outlist
outputlist.ListIndex = outindex
return
#*******************************************************************
#* May be defined with the Basic IDE
#*******************************************************************
def SetupDialog():
# Build from scratch the example dialog and its controls
# dialog The Dialog object
# inputlist The input listbox
# outputlist The input listbox
# control DialogControl object
dialog = CreateScriptService('NewDialog', 'ListSelector', (150, 75, 180, 125))
dialog.Caption = 'List extractor'
# The close button
control = dialog.CreateButton('btnClose', place = (140, 10, 30, 10), push = 'OK')
control.Caption = 'Close'
# The listboxes
inputlist = dialog.CreateListBox('lstIn', place = (25, 40, 40, 70),
dropdown = False, linecount = 8, multiselect = False)
inputlist.RowSource = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'.split(',')
inputlist.ListIndex = 0 # Select the first item
outputlist = dialog.CreateListBox('lstOut', place = (100, 40, 40, 70),
dropdown = False, linecount = 8, multiselect = False)
# The buttons
control = dialog.CreateButton('btnMoveToRight', place = (80, 45, 15, 10))
control.Caption = '→' # U+2192
control.OnActionPerformed = onaction
control = dialog.CreateButton('btnMoveToLeft', place = (70, 60, 15, 10))
control.Caption = '←' # U+2190
control.OnActionPerformed = onaction
control = dialog.CreateButton('btnMoveUp', place = (150, 45, 15, 10))
control.Caption = '↑' # U+2191
control.OnActionPerformed = onaction
control = dialog.CreateButton('btnMoveDown', place = (150, 60, 15, 10))
control.Caption = '↓' # U+2193
control.OnActionPerformed = onaction
return dialog
g_exportedScripts = (Main,)
if __name__ == '__main__':
Main()