Advertisement
Guest User

Untitled

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