An example

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ponandrew
Posts: 1
Joined: Sun Dec 23, 2018 9:26 am

An example

Post by ponandrew »

Synchronization some values:

Code: Select all

Initial state
date	operations		
01.01.18	05.01.18	credit	1000
02.01.18	06.01.18	credit	500
03.01.18	12.01.18	debet	50
04.01.18	18.01.18	credit	100
05.01.18	25.01.18	debet	330
06.01.18			
07.01.18			
08.01.18			
09.01.18			
10.01.18			
11.01.18			
12.01.18			
13.01.18			
14.01.18			
15.01.18			
16.01.18			
17.01.18			
18.01.18			
19.01.18			
20.01.18			
21.01.18			
22.01.18			
23.01.18			
24.01.18			
25.01.18			
26.01.18			
27.01.18			
28.01.18			
29.01.18			
30.01.18			
31.01.18			

synchronous state	
date	operations		
01.01.18			
02.01.18			
03.01.18			
04.01.18			
05.01.18	05.01.18	credit	1000
06.01.18	06.01.18	credit	500
07.01.18			
08.01.18			
09.01.18			
10.01.18			
11.01.18			
12.01.18	12.01.18	debet	50
13.01.18			
14.01.18			
15.01.18			
16.01.18			
17.01.18			
18.01.18	18.01.18	credit	100
19.01.18			
20.01.18			
21.01.18			
22.01.18			
23.01.18			
24.01.18			
25.01.18	25.01.18	debet	330
26.01.18			
27.01.18			
28.01.18			
29.01.18			
30.01.18			
31.01.18			


Sub sinchronization
Dim oComp, oSheet, column0,row0, numRows, oView, oRange, oCurSelection, val, val2, i
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
dim dispatcher as object, oFrame
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
	oComp = StarDesktop.CurrentComponent
        oView = oComp.CurrentController
        oFrame = oView.Frame
	oSheet = oView.ActiveSheet()
	oCurSelection = oComp.getCurrentSelection
	column0 = oCurSelection.CellAddress.Column
	row0 = oCurSelection.CellAddress.Row
	numRows = GetCountOfTokens
	for i = 0 to numRows-1
		val = oSheet.getCellByPosition(column0, row0+i).Value
		val2 = oSheet.getCellByPosition(column0+1, row0+i).Value
		if val <> val2 then
			oView.Select(oSheet.getCellRangeByPosition(column0+1, row0+i, column0 + 5, row0+i+numRows-1))
			dispatch.executeDispatch(oFrame, ".uno:Cut", "", 0, Array())
			dispatch.executeDispatch(oFrame, ".uno:GoDown", "", 0, Array())
			dispatch.executeDispatch(oFrame, ".uno:Paste", "", 0, Array())
		endif
	next
	
End Sub
Function GetCountOfTokens
	Dim oSheet, val, i, column,row
	column = ThisComponent.getCurrentSelection.CellAddress.Column
	row = ThisComponent.getCurrentSelection.CellAddress.Row
	oSheet = ThisComponent.CurrentController.ActiveSheet()
	val = oSheet.getCellByPosition(column, row).Value
	i = 0
	Do While not IsEmpty(val) and val <> 0 and i < 1000
		i = i + 1
		val = oSheet.getCellByPosition(column, row+i).Value
	Loop
	GetCountOfTokens = i
End Function
LibreOffice 6.0.7.3 ubuntu 18.04.2
Post Reply