Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. REM  *****  BASIC  *****
  2.  
  3. REM Remove duplicates
  4. Dim oDialog1 As Object, oDoc As Object, oController As Object, oSheet As Object
  5.  
  6. Sub RemoveDuplicates
  7.     oDoc = ThisComponent
  8.     oController = oDoc.getCurrentController
  9.     oSheet = oController.activeSheet
  10.  
  11.     Dim oSel As Object, oRange As Object
  12.     oSel = oDoc.getCurrentSelection()
  13.  
  14.     InitLanguage
  15.  
  16.     If not oSel.supportsService("com.sun.star.sheet.SheetCellRange") then
  17.         MsgBox getText(0)
  18.         Exit sub
  19.     End If
  20.  
  21.     Dim sourceAddress
  22.     sourceAddress = oSel.getRangeAddress
  23.  
  24.     If oSel.supportsService("com.sun.star.sheet.SheetCell") then
  25.         ExtendSingleSelection(oSel)
  26.         oSel = oDoc.getCurrentSelection()
  27.     Else
  28.         Dim cursor As Object, endAddr As Object, newAddr As Object, modified As Boolean
  29.         cursor = oSheet.createCursor()
  30.         cursor.gotoEndOfUsedArea(False)
  31.         endAddr = cursor.getRangeAddress()
  32.         newAddr = sourceAddress
  33.         modified = False
  34.         If (newAddr.EndColumn > endAddr.EndColumn) Then
  35.             newAddr.EndColumn = endAddr.EndColumn
  36.             modified = True
  37.         End If
  38.         If (newAddr.EndRow > endAddr.EndRow) Then
  39.             newAddr.EndRow = endAddr.EndRow
  40.             modified = True
  41.         End If
  42.         If (modified) Then
  43.             oRange = oSheet.getCellRangeByPosition(newAddr.StartColumn, newAddr.StartRow, newAddr.EndColumn, newAddr.EndRow)
  44.             oController.select(oRange)
  45.             oSel = oDoc.getCurrentSelection()
  46.         End If
  47.     End If
  48.  
  49.     If oSel.supportsService("com.sun.star.sheet.SheetCell") then
  50.         MsgBox getText(1)
  51.         Exit Sub
  52.     End If
  53.  
  54.     ' Do not allow selecting the whole sheet: it would simply be too long ("hang")
  55.     ' In the future, the max column coult may be increased - use ">=" here
  56.     If ((oSel.Columns.Count >= 1024) And (oSel.Rows.Count = 1024*1024)) Then
  57.         MsgBox "The whole sheet selected; this will hang the operation - aborting"
  58.         Exit Sub
  59.     End If
  60.  
  61.     DialogLibraries.LoadLibrary("RemoveDuplicates")
  62.     oDialog1 = CreateUnoDialog( DialogLibraries.RemoveDuplicates.Options )
  63.     UpdateDialogUI
  64.     cbIncludeTitle
  65.     If oDialog1.Execute() = 0 Then
  66.         oRange = oSheet.getCellRangeByPosition(sourceAddress.StartColumn, sourceAddress.StartRow, sourceAddress.EndColumn, sourceAddress.EndRow)
  67.         oController.select(oRange)
  68.     End If
  69. End Sub
  70.  
  71. Sub UpdateDialogUI
  72.     oDialog1.GetControl("OptionHint").Text = getText(5)
  73.     oDialog1.GetControl("btnSelectAll").Label = getText(3)
  74.     oDialog1.GetControl("cbTitle").Label = getText(4)
  75.     oDialog1.GetControl("btnOK").Label = getText(6)
  76.     oDialog1.GetControl("btnCancel").Label = getText(7)
  77. End Sub
  78.  
  79. Sub UpdateListBoxItems (hasTitle As Boolean)
  80.     Dim oSel, lbList
  81.     oSel = oDoc.getCurrentSelection()
  82.     lbList = oDialog1.GetControl("lbList")
  83.     Dim ColDesc()
  84.     If hasTitle Then
  85.         Dim FirstRowRange As Object ' First row of selection
  86.         FirstRowRange = oSel.getCellRangeByPosition(0, 0, oSel.Columns.Count - 1, 0)
  87.         ColDesc = FirstRowRange.getDataArray()(0)
  88.     Else
  89.         Dim i As Long, sPrefix As String
  90.         ColDesc = oSel.Columns.ElementNames
  91.         sPrefix = getText(2)
  92.         For i = lBound(ColDesc) To uBound(ColDesc)
  93.             ColDesc(i) = sPrefix & ColDesc(i)
  94.         Next i
  95.     End If
  96.     lbList.setVisible(False) ' this speeds up the update manyfold
  97.     lbList.removeItems(0, lbList.getItemCount())
  98.     lbList.addItems(ColDesc, 0)
  99.     btnSelectAllClick
  100.     lbList.setVisible(True)
  101. End Sub
  102.  
  103. Sub ExtendSingleSelection(oSel)
  104. Dim oCursor
  105.     oCursor = oSel.SpreadSheet.createCursorByRange(oSel)
  106.     oCursor.collapseToCurrentRegion
  107.     oController.select(oCursor)
  108. End Sub
  109.  
  110. Function CheckInArray(checkRow As String, byRef UnionArray)
  111.     CheckInArray = True
  112.     On Error GoTo duplicateRow
  113.     UnionArray.Add(1, checkRow) ' Fails if exists
  114.     CheckInArray = False
  115. duplicateRow:
  116. End Function
  117.  
  118. Function GetCellRangeAddress(Sheet, StartColumn, StartRow, EndColumn, EndRow)
  119.     Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
  120.     CellRangeAddress.Sheet = Sheet
  121.     CellRangeAddress.StartColumn = StartColumn
  122.     CellRangeAddress.EndColumn = EndColumn
  123.     CellRangeAddress.StartRow = StartRow
  124.     CellRangeAddress.EndRow = EndRow
  125.     GetCellRangeAddress = CellRangeAddress
  126. End Function
  127.  
  128. Function GetCellAddress(Sheet, Column, Row)
  129.     Dim CellAddress As New com.sun.star.table.CellAddress
  130.     CellAddress.Sheet = Sheet
  131.     CellAddress.Column = Column
  132.     CellAddress.Row = Row
  133.     GetCellAddress = CellAddress
  134. End Function
  135.  
  136. Sub MoveCells(Sheet, StartColumn, StartRow, EndColumn, EndRow, NewRow)
  137.     oSheet.copyRange(GetCellAddress(Sheet, StartColumn, NewRow), _
  138.                      GetCellRangeAddress(Sheet, StartColumn, StartRow, EndColumn, EndRow))
  139. End Sub
  140.  
  141. Sub RemoveCells(Sheet, StartColumn, StartRow, EndColumn, EndRow)
  142.     oSheet.removeRange(GetCellRangeAddress(Sheet, StartColumn, StartRow, EndColumn, EndRow), _
  143.                        com.sun.star.sheet.CellDeleteMode.UP)
  144. End Sub
  145.  
  146. Function GetRowString(aDataRow(), aCompInd()) As String
  147.     Dim l As Long, u As Long
  148.     l = lBound(aCompInd)
  149.     u = uBound(aCompInd)
  150.     If (l = u) Then
  151.         GetRowString = aDataRow(aCompInd(l))
  152.         Exit Function
  153.     End If
  154.     Dim NewArr(l To u), i As Long
  155.     For i = lBound(aCompInd) To uBound(aCompInd)
  156.         NewArr(i) = aDataRow(aCompInd(i))
  157.     Next i
  158.     GetRowString = Join(NewArr, Chr(1))
  159. End Function
  160.  
  161. ' Returns 1 if successful, 0 otherwise
  162. Function doRemove() As Long
  163.  
  164.     Dim StartTime As Date
  165.     StartTime = Now()
  166.  
  167.     Dim bResult As Boolean, bModified As Boolean
  168.     bResult = False
  169.     bModified = False
  170.  
  171.     Dim oUndoManager As Object
  172.  
  173.     oDoc.lockControllers()
  174.     oDoc.addActionLock()
  175.     oUndoManager = ThisComponent.getUndoManager()
  176.     oUndoManager.enterUndoContext("Remove Duplicates")
  177.     On Error GoTo cleanup
  178.  
  179.     Dim oSel As Object, oAddress As Object, aDataArray(), selectedLst(), bHasTitle As Boolean
  180.     oSel = oDoc.getCurrentSelection()
  181.     oAddress = oSel.getRangeAddress()
  182.     aDataArray = oSel.getDataArray()
  183.  
  184.     selectedLst = oDialog1.GetControl("lbList").getSelectedItemsPos()
  185.     bHasTitle = oDialog1.GetControl("cbTitle").getState() > 0
  186.  
  187.     Dim FirstRow As Long, LastRow As Long
  188.     FirstRow = lBound(aDataArray)
  189.     If bHasTitle Then
  190.         FirstRow = FirstRow + 1
  191.     End If
  192.     LastRow = uBound(aDataArray)
  193.  
  194.     Dim ProgressBar As Object
  195.     ProgressBar = oDialog1.GetControl("ProgressBar1")
  196.     ResetProgress(ProgressBar, FirstRow, LastRow)
  197.  
  198.     Dim UnionArray As New Collection, LastRowDone As Long, nUnique As Long, Pos As Long, checkStr As String
  199.     LastRowDone = FirstRow - 1
  200.     nUnique = 0
  201.     For Pos = FirstRow To LastRow
  202.         checkStr = GetRowString(aDataArray(Pos), selectedLst)
  203.         If (CheckInArray(checkStr, UnionArray)) Then ' Duplicate
  204.             If (nUnique > 0) Then
  205.                 If (nUnique < (Pos - FirstRow)) Then
  206.                     MoveCells(oAddress.Sheet, _
  207.                               oAddress.StartColumn, _
  208.                               oAddress.StartRow + Pos - nUnique, _
  209.                               oAddress.EndColumn, _
  210.                               oAddress.StartRow + Pos - 1, _
  211.                               oAddress.StartRow + LastRowDone + 1)
  212.                     bModified = True
  213.                 End If
  214.                 LastRowDone = LastRowDone + nUnique
  215.                 nUnique = 0
  216.             End If
  217.         Else ' Unique
  218.             nUnique = nUnique + 1
  219.         End If
  220.  
  221.         If ((Pos > FirstRow) And (Pos Mod 100 = 0)) Then
  222.             StepProgress(ProgressBar, 100)
  223.         End If
  224.     Next Pos
  225.  
  226.     If (nUnique > 0) Then
  227.         If (nUnique < (Pos - FirstRow)) Then
  228.             MoveCells(oAddress.Sheet, _
  229.                       oAddress.StartColumn, _
  230.                       oAddress.StartRow + Pos - nUnique, _
  231.                       oAddress.EndColumn, _
  232.                       oAddress.StartRow + Pos - 1, _
  233.                       oAddress.StartRow + LastRowDone + 1)
  234.             bModified = True
  235.         End If
  236.         LastRowDone = LastRowDone + nUnique
  237.     End If
  238.  
  239.     If (LastRowDone < LastRow) Then
  240.         RemoveCells(oAddress.Sheet, _
  241.                     oAddress.StartColumn, _
  242.                     oAddress.StartRow + LastRowDone + 1, _
  243.                     oAddress.EndColumn, _
  244.                     oAddress.StartRow + LastRow)
  245.         bModified = True
  246.     End If
  247.  
  248.     ' Make sure to set it to 100% before reporting success: it could not account yet for the last <100 elements
  249.     SetProgress(ProgressBar, LastRow)
  250.  
  251.     bResult = True
  252.  
  253. cleanup:
  254.     oUndoManager.leaveUndoContext()
  255.     oDoc.unLockControllers()
  256.     oDoc.removeActionLock()
  257.  
  258.     Dim TotalCount As Long, DuplicatesCount As Long, Message As String
  259.     Message = "Finished in " & Format((Now()-StartTime), "[s]") & " seconds." & Chr$(13)
  260.     If (bResult) Then
  261.         doRemove = 1
  262.         TotalCount = LastRow - FirstRow + 1
  263.         DuplicatesCount = LastRow - LastRowDone
  264.         If (DuplicatesCount > 0) Then
  265.             Message = Message & "We found and deleted " & DuplicatesCount & " duplicated values." & Chr$(13) & _
  266.                                 "Now we have only " & TotalCount - DuplicatesCount & " unique vaues."
  267.         Else
  268.             Message = Message & "No duplicates found."
  269.         End If
  270.     Else
  271.         doRemove = 0
  272.         If (bModified) Then
  273.             Message = Message & "An error occured! The changes will now be undone."
  274.         Else
  275.             Message = Message & "An error occured! No changes have been made."
  276.         End If
  277.     End If
  278.     MsgBox Message
  279.     If (Not bResult And bModified) Then oUndoManager.Undo()
  280. End Function
  281.  
  282. Sub ResetProgress(ByRef ProgressBar, nMinVal As Long, nMaxVal As Long)
  283.     With ProgressBar
  284.         .setRange(nMinVal, nMaxVal)
  285.         .setValue(nMinVal)
  286.     End With
  287. End Sub
  288.  
  289. Sub SetProgress(ByRef ProgressBar, nVal As Long)
  290.     ProgressBar.setValue(nVal)
  291. End Sub
  292.  
  293. Sub StepProgress(ByRef ProgressBar, nStep As Long)
  294.     With ProgressBar
  295.         .setValue(.getValue() + nStep)
  296.     End With
  297. End Sub
  298.  
  299. Sub btnCancelClick
  300.     oDialog1.endExecute()
  301. End Sub
  302.  
  303. Sub btnOKClick
  304.     oDialog1.endDialog(DoRemove())
  305. End Sub
  306.  
  307. Sub btnSelectAllClick
  308.     Dim lbList, count As Long, i As Long
  309.     lbList = oDialog1.GetControl("lbList")
  310.     count = lbList.getItemCount()
  311.     Dim SelectItems(count) As Integer
  312.     For i = 0 To count - 1
  313.         SelectItems(i) = i
  314.     Next i
  315.     lbList.selectItemsPos(SelectItems, true)
  316. End Sub
  317.  
  318. Sub cbIncludeTitle
  319.     Dim cbTitle
  320.     cbTitle = oDialog1.GetControl("cbTitle")
  321.     If cbTitle.getState() > 0 Then
  322.         UpdateListBoxItems(True)
  323.     Else
  324.         UpdateListBoxItems(False)
  325.     End If
  326. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement