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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 <?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="CylinderAutomation" script:language="StarBasic">REM ***** BASIC *****
'Option VBASupport 1
Option Explicit
Global Const rowsToSkip = 1
Global Const roughHeaderMatch = True
Global Const highlightBasedOn = "Payment Option" ' A yellow background is drawn that matches this column and 'highlightSearchString'
Global Const highlightSearchString = "Online" ' Does partial match
Global Const highlightRemoveColumn = True ' True if you want remove the column once the highlight is done
Global Const reportType = "PENDING" ' Allowed values are "PENDING", "SAFETY" and "SBC". The number of columns that are copied and the column size will be affected
Global Const shouldExportPDF = True ' If True this will export all the created sheets with Area specific data individually TODO change name to easily understand
Global Const badDayThreshold = 4 ' The days below 'the maximum date in report' to show as bad
Global Const shouldSegregateAreaWise = True' True if you want to create sheets with Area specific data
Global Const exportFolderPrefix = "C:\Users\bhara\Sync\"
' Values are loaded from the document
Private tillDate as Date
Private tillDateStr as String
Private sheetNameSuffix as String
Private exportFolder as String
'Private shouldExportSummaryPDF as Boolean' If True the first page will be exported as PDF
Sub Main
Dim oDoc : oDoc = ThisComponent
Dim s as Object
Dim cursor as Object
Dim c as Integer
Dim destSheet as Variant
Dim sheetName$, d$, a$
Dim areaColumn%, areaNames as New Collection
Dim startColumn%, startRow%, endColumn%, endRow%
Dim rowsToRemove%, iArea%
Dim areaRange, idRange, bookRange
Dim dColumns as Object
Dim headerRange as Object
Dim cellRangeToCopy as Object
Dim pageStyle as Object
Dim oCellStyles
Dim oConFormat
Dim oCondition(2) as new com.sun.star.beans.PropertyValue
Dim T4Style
' Initialize Globals
' Casting to Long removes the time component
tillDate = CLng(Now())
s = oDoc.Sheets(0)
cursor = UsedRangeCursor(s)
' NaiveLastTable gives us the last non-blank table's
rowsToRemove = NaiveLastTable(s)
If rowsToRemove <> 0 Then
s.getRows().removeByIndex(0, rowsToRemove + 1)
End If
startRow = 1
startColumn = 0
endRow = cursor.RangeAddress.EndRow
endColumn = cursor.RangeAddress.EndColumn
' TODO move this to knowledge base
oCellStyles = ThisComponent.StyleFamilies("CellStyles")
If Not oCellStyles.hasByName("T4") Then
T4Style = oDoc.createInstance("com.sun.star.style.CellStyle")
oCellStyles.insertByName("T4", T4Style)
oCellStyles.getByName("T4").CellBackColor = RGB(255, 0, 0)
End If
If ThisComponent.CurrentController.hasFrozenPanes() Then
Call UnFreezeSelection
End If
' TODO pass the column name/index as an argument
' Transformations to be applied to the Main sheet before splitting the sheet by Area Name
cursor = UsedRangeCursor(s)
Call CleanColumnHeaders(s, endColumn)
areaColumn = GetHeaderPosition(s, endColumn, "Area")
Call ShortenDirections(s, areaColumn)
If Left(reportType, 1) = "P" Then
Call ApplyTheme "millennium.ots"
endColumn = RemoveColumnsExcept(s, endColumn, Array( _
"Area", _
"ID", _
"Name", _
"Mobile", _
"Book", _
"Payment Option" _
))
endColumn = HighlightRowWithColumn(s, endColumn, endRow, _
"Payment Option", "Online Payment", True, _
RGB(255, 255, 0))
' Justify Leftmost cells to left and Rightmost cells to right
bookRange = GetColumnRange(s, endColumn, endRow, "Book")
bookRange.setPropertyValue("HoriJustify", com.sun.star.table.CellHoriJustify.RIGHT)
FormatRangeAsNumber(s, new com.sun.star.lang.Locale, oDoc.getNumberFormats(), bookRange, "DD/MM")
tillDate = FindHighestDateAsString(s, "Book", endColumn, endRow)
With oCondition(0) : .Name = "Operator" : .Value = com.sun.star.sheet.ConditionOperator.LESS_EQUAL : End With
oCondition(1).Name = "Formula1" : oCondition(1).Value = CLng(tillDate - badDayThreshold)
With oCondition(2) : .Name = "StyleName" : .Value = "T4" : End With
oConFormat = bookRange.ConditionalFormat
oConFormat.clear() : oConFormat.addNew(oCondition)
SetWidths(s, endColumn, Array( _
Array("Area", 0), _
Array("ID", 0), _
Array("Name", 7000), _
Array("Mobile", 0), _
Array("Book", 0) _
))
ElseIf reportType = "SBC" Then
Call ApplyTheme "pumpkin.ots"
endColumn = RemoveColumnsExcept(s, endColumn, Array( _
"ID", _
"Name", _
"Address", _
"Mobile", _
"Area", _
"No Of Cylinder" _
))
SetWidths(s, endColumn, Array( _
Array("ID", 0), _
Array("Name", 3000), _
Array("Address", 8000), _
Array("Mobile", 0), _
Array("Area", 0) _
))
End If
idRange = GetColumnRange(s , endColumn, endRow, "ID") _
.setPropertyValue("HoriJustify", com.sun.star.table.CellHoriJustify.LEFT)
Call SortColumn(s, endColumn, endRow, "Area")
' Setup exportFolder and sheetName
tillDateStr = Replace(CDate(tillDate), "/", "-")
tillDateStr = Trim(tillDateStr)
sheetNameSuffix = " - " & reportType & " " & tillDateStr
exportFolder = exportFolderPrefix & "/" & reportType & " " & tillDateStr
headerRange = s.getCellRangeByPosition(0, 0, endColumn, 0)
headerRange.CellStyle = "Heading 1"
pageStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName("Default")
pageStyle.setPropertyValue("PrintGrid", True)
pageStyle.setPropertyValue("CenterHorizontally", True)
' Make the margins 0.2" thick
pageStyle.setPropertyValue("LeftMargin", 0.2 * 2540)
pageStyle.setPropertyValue("RightMargin", 0.2 * 2540)
' Change original sheet name
s.Name = "SUMMARY" & sheetNameSuffix
' Export summary
If shouldExportPDF Then
ExportPDF(s, exportFolder)
End If
If NOT shouldSegregateAreaWise Then Exit Sub
Dim statusBar
statusBar = oDoc.CurrentController.StatusIndicator
statusBar.start("Creating Area Wise", 10)
areaRange = s.getCellRangeByPosition(areaColumn, startRow, areaColumn, endRow)
a = areaRange.getCellByPosition(0, startRow).getString()
'On Error Goto ErrorHandler
For iArea = startRow To endRow - 1
d = areaRange.getCellByPosition(0, iArea).getString()
If d = "" Then
Exit For
End If
If a <> d Or iArea = (endRow - 1) Then
statusBar.setValue((iArea / endRow) * 100)
' FIXME Get the columns automatically from the sheet
' FIXME Covert the end column from endColumn above
Goto Con:
Dim cName
s.Rows.insertByIndex(startRow, 1)
cName = s.getCellByPosition(startColumn, startRow)
cName.setString(d)
startRow = startRow + 1
endRow = endRow + 1
Con:
' TODO maybe there is an elegant way than
' writing this two times
sheetName = a & sheetNameSuffix
' TODO subtotals might solve this
's.group(areaRange.RangeAddress, 1)
' Prepare Destination sheet
destSheet = NewSheet(sheetName)
' Copy the Headers from the Main Document
If areaColumn > 0 And areaColumn < endColumn Then
MsgBox "Can't have Area column in the middle", 16
Exit Sub
End If
' start end
'1 : endColumn 0 4
'areaColumn + 1 : endColumn 2 4
'0 : endColumn - 1 4 4
' Copy Header
s.copyRange(destSheet.getCellRangeByName("A1").CellAddress, _
s.getCellRangeByPosition( _
(endColumn - areaColumn)/ endColumn, _
0, endcolumn - CInt(areaColumn/endColumn), 0).RangeAddress)
' Copy Contents
s.copyRange(destSheet.getCellRangeByName("A2").CellAddress, _
s.getCellRangeByPosition(_
(endColumn - areaColumn)/ endColumn, _
startRow, _
endcolumn - CInt(areaColumn/endColumn), _
iArea).RangeAddress)
' Customize the Destination Sheet's Columns
If Left(reportType, 1) = "P" Then
SetWidths(destSheet, endColumn, Array( _
Array("ID", 0), _
Array("Name", 7000), _
Array("Mobile", 0), _
Array("Book", 0) _
))
ElseIf reportType = "SBC" Then
SetWidths(destSheet, endColumn, Array( _
Array("ID", 0), _
Array("Name", 4000), _
Array("Address", 11000), _
Array("Mobile", 0) _
))
End If
If shouldExportPDF Then
ExportPDF(destSheet, exportFolder)
End If
Continue:
startRow = (iArea + 1)
a = d
End If
Next iArea
ErrorHandler:
statusBar.end()
Reset
End Sub
</script:module>