: bharatvaj/lobasic-macros

LibreOffice macros I use often

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
REM  *****  BASIC  *****

Sub _ColumnJoin(joinChar$, idColumnName, dstSheet as Object, dstColumnName$, dstStartRow%, srcSheet as Object, srcColumnName$, srcStartRow, statusBar as Variant)
	Dim i&, j&
	Dim dstEndRow&, dstEndColumn%, srcEndRow&, srcEndColumn%
	Dim dstColumnIdx%, srcColumnIdx%
	Dim dstStr$, dstCell
	Dim dstRange, srcRange, srcRangeAddress, dstRangeAddress
	Dim oTextCursor
	
	dstRange = UsedRangeCursor(dstSheet)
	dstRangeAddress = dstRange.RangeAddress
	srcRange = UsedRangeCursor(srcSheet)
	srcRangeAddress = srcRange.RangeAddress

	dstEndRow = dstRangeAddress.EndRow
	srcEndRow = srcRangeAddress.EndRow
	
	dstColumnIdx = GetHeaderPosition(dstCtx, dstColumnName, dstStartRow)
	srcColumnIdx = GetHeaderPosition(srcCtx, srcColumnName, srcStartRow)

	Dim srcColumnIdIdx: srcColumnIdIdx = GetHeaderPosition(srcCtx, idColumnName, srcStartRow)
	'srcRange = srcSheet.getCellRangeByPosition(srcColumnIdIdx, srcStartRow, srcValueOffset, srcEndRow)
	
	Dim srcLastColumnIdx%: srcLastColumnIdx = srcRangeAddress.EndColumn - 1
	Dim dstColumnIdIdx: dstColumnIdIdx = GetHeaderPosition(dstCtx, idColumnName, dstStartRow)

	statusBar.start("Merging Columns", 100)
	For i = dstStartRow To dstEndRow
		Dim srcCell
		Dim srcStr$
		Dim what$

		statusBar.setText("Merging" & CStr(i) & "/" & dstEndRow)
		statusBar.setValue((i / dstEndRow) * 100)

		dstCell = dstRange.getCellByPosition(dstColumnIdx, i)
		dstStr = dstCell.getText().String
		what = dstRange.getCellByPosition(dstColumnIdIdx, i).getText().String

		srcCell = GetValueForKey(srcRange, idColumnName, srcLastColumnIdx)
		For j = srcStartRow To srcEndRow
			If srcRange.getCellByPosition(srcColumnIdIdx, j).getText().String = what Then
				srcCell = srcRange.getCellByPosition(srcColumnIdx, j)
				'j = srcEndRow
				Exit For
			End If
		Next

		If Not IsNull(srcCell) Then
			srcStr = srcCell.getText().String
		End If
		dstCell.getText().setString(dstStr & joinChar & srcStr)

		oTextCursor = dstCell.getText().createTextCursor()
	    oTextCursor.gotoStart(False)
	    oTextCursor.goRight(Len(dstStr), True)
	    'oTextCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
	    oTextCursor.goRight(1, False)
	    oTextCursor.gotoEnd(True)
	    oTextCursor.CharHeight = 7
	    srcCell = Nothing
	    srcStr = ""
	    what = ""
	Next
	statusBar.end()
End Sub