lobasic-macros

LibreOffice macros I use often
Log | Files | Refs

commit eb97a0eb5088fe1192a737243be809285ed40546
parent dfeca12b63083958b3bdc493c1430aa7e6a83d6e
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date:   Thu,  4 Jul 2024 13:07:13 +0530

Add isPendingReport to distinguish from safety

Fix GetHeaderPosition by returning -1 on error

Remove Highlighted columns automatically after highlighting the rows

Set PageStyles to HorizontallyCenter and enable PageGrid

Diffstat:
MStandard/Bharatgas.xba | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 71 insertions(+), 31 deletions(-)

diff --git a/Standard/Bharatgas.xba b/Standard/Bharatgas.xba @@ -4,13 +4,18 @@ &apos;Option VBASupport 1 Option Explicit -Global Const exportFolder = &quot;C:\Users\bhara\export&quot; +Global Const exportFolder = &quot;C:\Users\bhara\tSync\Pending-01-07-2024&quot; Global Const rowsToSkip = 1 Global Const insertNewRowFor = False -Global Const printOnlySummary = True -Global Const highlightBasedOn = &quot;Payment Option&quot; +Global Const printOnlySummary = False Global Const roughHeaderMatch = True +Global Const highlightBasedOn = &quot;Payment Option&quot; Global Const highlightSearchString = &quot;Online Payment&quot; +Global Const highlightRemoveColumn = True +&apos; TODO change suffix based on the report type +Global Const sheetNameSuffix = &quot;- PENDING&quot; +Global Const shouldExportPDF = True +Global Const isPendingReport = True Function UsedRangeCursor(Optional ByRef oSheet as Object) as Object &apos;FIXME Check if the split has already happen @@ -53,7 +58,7 @@ End Sub Sub SortAreaName(Optional oSheet As Variant, Optional cursor as Variant) Dim oRange as Object - Dim oSortFields(0) as new com.sun.star.util.SortField + Dim oSortFields(1) as new com.sun.star.util.SortField Dim oSortDesc(0) as new com.sun.star.beans.PropertyValue Dim endRow as Integer Dim endColumn as Integer @@ -75,8 +80,8 @@ Sub SortAreaName(Optional oSheet As Variant, Optional cursor as Variant) &apos; FIXME This is not working &apos; Date - &apos;oSortFields(1).Field = 4 - &apos;oSortFields(1).SortAscending = True + oSortFields(1).Field = 4 + oSortFields(1).SortAscending = True oSortDesc(0).Name = &quot;SortFields&quot; oSortDesc(0).Value = oSortFields @@ -146,6 +151,7 @@ Function GetHeaderPosition(ByRef oSheet as Object, searchString as String, endCo Dim iColumn as Integer Dim oCell as Object Dim cellString as String + GetHeaderPosition = -1 If roughHeaderMatch Then searchString = UserFriendlyName(searchString) End If @@ -173,7 +179,7 @@ Sub HighlightOnline(ByRef oSheet as Object, endColumn as Long, endRow as Long) &apos; Search for column position that has highlightBasedOn iSearchColumn = GetHeaderPosition(oSheet, highlightBasedOn, endRow) - + If iSearchColumn = -1 Then Exit Sub For i = 0 To endRow - 1 oCell = oSheet.getCellByPosition(iSearchColumn, i) If InStr(oCell.getString(), highlightSearchString) &gt; 0 Then @@ -181,6 +187,10 @@ Sub HighlightOnline(ByRef oSheet as Object, endColumn as Long, endRow as Long) oCellRange.CellBackColor = RGB(255, 255, 0) &apos; Yellow End If Next i + + If highlightRemoveColumn Then + oSheet.Columns.removeByIndex(iSearchColumn, 1) + End If End Sub @@ -188,10 +198,21 @@ Sub MainNew Dim oRange as Object Dim oSheet as Object Dim oCellStyle as Object + Dim pageStyle as Object + Dim oStyle as Object oSheet = ThisComponent.Sheets.getByIndex(0) - oRange = oSheet.getCellRangeByPosition(1, 1, 4, 1) + &apos;oRange = oSheet.getCellRangeByPosition(1, 1, 4, 1) &apos;oCellStyle = ThisComponent.StyleFamilies.getByName(&quot;CellStyles&quot;).getByName(&quot;InterHeader&quot;) - oRange.CellStyle = &quot;InterHeader&quot; + &apos;oRange.CellStyle = &quot;InterHeader&quot; + pageStyle = ThisComponent.StyleFamilies.getByName(&quot;PageStyles&quot;) + oStyle = pageStyle.getByName(&quot;Default&quot;) + oStyle.CenterVertically = True + pageStyle.insertByName(&quot;NewStyle&quot;) + + if NOT IsNull(pageStyle) then + Print pageStyle.dbg_methods + end if + End Sub Sub Main @@ -212,6 +233,7 @@ Sub Main Dim areaRange, idRange, bookRange Dim headerRange as Object Dim cellRangeToCopy as Object + Dim pageStyle as Object s = ThisComponent.Sheets(0) cursor = UsedRangeCursor(s) @@ -225,7 +247,9 @@ Sub Main &apos; Transformations to be applied to the Main sheet before splitting the sheet by Area Name &apos;Call UnFreezeSelection cursor = UsedRangeCursor(s) - Call HighlightOnline(s, endColumn, endRow) + If isPendingReport Then + Call HighlightOnline(s, endColumn, endRow) + End If Call SortAreaName(s, cursor) Call CleanColumnHeaders(s, endColumn) @@ -241,6 +265,7 @@ Sub Main headerRange = s.getCellRangeByPosition(0, 0, endColumn, 0) headerRange.CellStyle = &quot;Heading 1&quot; + &apos;On Error Goto ErrorHandler For iArea = rowsToSkip To endRow - 1 d = areaColumn.getCellByPosition(0, iArea).String @@ -261,13 +286,18 @@ Sub Main areaRange = s.getCellRangeByPosition(0, startRow, endColumn, iArea) If printOnlySummary Then + &apos; TODO maybe there is an elegant way than + &apos; writing this two times + If shouldExportPDF Then + ExportPDF(destSheet) + End If GoTo Continue End If &apos; TODO subtotals might solve this &apos;s.group(areaRange.RangeAddress, 1) - sheetName = a &amp; &quot; - PENDING&quot; + sheetName = a &amp; sheetNameSuffix &apos; Copy the Headers from the Main Document &apos; FIXME This DOES NOT work when using with filtered data @@ -282,12 +312,29 @@ Sub Main NewSheet(sheetName) destSheet = ThisComponent.Sheets().getByName(sheetName) + + pageStyle = ThisComponent.StyleFamilies.getByName(&quot;PageStyles&quot;).getByName(&quot;Default&quot;) + pageStyle.setPropertyValue(&quot;PrintGrid&quot;, True) + pageStyle.setPropertyValue(&quot;CenterHorizontally&quot;, True) + &apos; Make the margins 0.2&quot; thick + pageStyle.setPropertyValue(&quot;LeftMargin&quot;, 0.2 * 2540) + pageStyle.setPropertyValue(&quot;RightMargin&quot;, 0.2 * 2540) &apos; Customize the Destination Sheet&apos;s Columns &apos; TODO Make snuggly calculation. There should be a minimum width and AutoFit - destSheet.getColumns().getByName(&quot;B&quot;).Width = 2500 - destSheet.getColumns().getByName(&quot;C&quot;).Width = 4500 - &apos;destSheet.getColumns().getByName(&quot;F&quot;).Width = 8000 + With destSheet.getColumns() + If isPendingReport Then + .getByName(&quot;B&quot;).Width = 2500 + .getByName(&quot;C&quot;).Width = 8000 + .getByName(&quot;D&quot;).Width = 2500 + Else + .getByName(&quot;B&quot;).Width = 1700 + .getByName(&quot;C&quot;).Width = 5000 + .getByName(&quot;D&quot;).Width = 11000 + .getByName(&quot;E&quot;).Width = 2200 + End If + End With + &apos; Copy Header &apos; TODO Check if it&apos;s possible to use UsedRange instead of endColumn @@ -303,8 +350,11 @@ Sub Main destSheet.getColumns().removeByIndex(0, 1) destSheet.getColumns().removeByIndex(4, 1) + &apos;TODO Maybe use Dispatcher? - ExportPDF(destSheet) + If shouldExportPDF Then + ExportPDF(destSheet) + End If Continue: startRow = (iArea + 1) a = d @@ -344,27 +394,17 @@ Sub ExportPDF(Optional ByVal oSheet as Object) fileName = exportFolder &amp; &quot;\&quot; &amp; oSheet.Name &amp; &quot;.pdf&quot; fileUrl = ConvertToUrl(fileName) - args(0).Name = &quot;FilterName&quot; - args(0).Value = &quot;calc_pdf_Export&quot; - - fd(0).Name = &quot;Selection&quot; - fd(0).Value = cursor + With args(0) : .Name = &quot;FilterName&quot; : .Value = &quot;calc_pdf_Export&quot; : End With + WIth fd(0) : .Name = &quot;Selection&quot; : .Value = cursor : End With &apos; conflicts with the Selection - fd(1).Name = &quot;SinglePageSheets&quot; - fd(1).Value = False - - fd(2).Name = &quot;IsSkipEmptyPages&quot; - fd(2).Value = True + With fd(1) : .Name = &quot;SinglePageSheets&quot; : .Value = False : End With + With fd(2) : .Name = &quot;IsSkipEmptyPages&quot; : .Value = True : End With - args(1).Name = &quot;FilterData&quot; - args(1).Value = fd - - args(2).Name = &quot;Overwrite&quot; - args(2).Value = True + With args(1) : .Name = &quot;FilterData&quot; : .Value = fd : End With + With args(2) : .Name = &quot;Overwrite&quot; : .Value = True : End With ThisComponent.storeToURL(fileUrl, args) End Sub - </script:module> \ No newline at end of file