libreoffice_idgen.bas 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. REM ***** BASIC *****
  2. Sub generate_id()
  3. REM Generate a 16-character long random ID for Pocket Archive.
  4. REM Use this macro in Libreoffice Calc on a single or multiple cells for
  5. REM quickly filling in any range of cells with fresh IDs.
  6. REM The CurrentSelection will even contain the stacked ranges
  7. REM of additional sheets IF MORE THAN ONE SHEET IS SELECTED.
  8. Const CHARS As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  9. Const STRING_LENGTH As Integer = 16
  10. Dim oSelection As Object
  11. Dim oCell As Object
  12. Dim sRandomString As String
  13. Dim i As Integer
  14. Dim iCharIndex As Integer
  15. Dim iCharsLength As Integer
  16. ' Get the current selection.
  17. iCharsLength = Len(CHARS)
  18. sel =ThisComponent.CurrentSelection
  19. If sel.supportsService("com.sun.star.sheet.SheetCellRanges") Then
  20. rgs = sel
  21. Else
  22. If NOT sel.supportsService("com.sun.star.sheet.SheetCellRange") Then Exit Sub
  23. rgs = ThisComponent.createInstance("com.sun.star.sheet.SheetCellRanges")
  24. rgs.addRangeAddress(sel.RangeAddress, False)
  25. End If
  26. For Each rg In rgs
  27. uR = rg.Rows.Count - 1
  28. uC = rg.Columns.Count - 1
  29. Randomize
  30. For r = 0 To uR
  31. For c = 0 To uC
  32. rcCell = rg.getCellByPosition(c, r)
  33. sRandomString = ""
  34. For i = 1 To STRING_LENGTH
  35. iCharIndex = Int((iCharsLength * Rnd) + 1)
  36. sRandomString = sRandomString & Mid(CHARS, iCharIndex, 1)
  37. Next i
  38. rcCell.setString(sRandomString)
  39. Next c
  40. Next r
  41. Next rg
  42. End Sub