This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro_randomize.bas
67 lines (60 loc) · 1.92 KB
/
macro_randomize.bas
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
REM ***** BASIC *****
Sub RandomizeContent()
On Error GoTo Except
' Grabs config file in directory of spreadsheet file
config = "config.txt"
configFile = GetFilePath(config)
numTag = FreeFile()
Open configFile for input as #numTag
Input #numTag, fileToUse
Close #numTag
' Reads file specified by config
textFile = GetFilePath(fileToUse)
Open textFile for input as #numTag
totalLines = 0
Do While Not eof(numTag)
ReDim Preserve elements(totalLines)
Input #numTag, elements(totalLines)
totalLines = totalLines + 1
Loop
Close #numTag
' Assigns elements to cell contents
oService = createUnoService("com.sun.star.sheet.addin.Analysis")
currSheet = ThisComponent.CurrentController.ActiveSheet
activeRange = ThisComponent.CurrentSelection
rangeCoor = activeRange.RangeAddress
totalElements = ArrayLen(elements) - 1
For iCol = rangeCoor.StartColumn To rangeCoor.EndColumn
For iRow = rangeCoor.StartRow To rangeCoor.EndRow
Cel = currSheet.getCellByPosition(iCol, iRow)
RandBetween = oService.getRandbetween(0, totalElements)
Cel.String = elements(RandBetween)
Next
Next
Except:
If Err = 57 Then
If Not IsEmpty(fileToUse) Then
msgbox "Missing file. Create a new '" & fileToUse & "'."
Else
msgbox "Missing file. Create a new '" & config & "'."
End If
ElseIf Error = "Reading exceeds EOF." Then
msgbox "Invalid content in '" & fileToUse & "'. Please remove any empty lines then try again."
ElseIf Err = 91 Then
msgbox "'" & fileToUse & "' is empty. Please add content then try again."
ElseIf Err <> 0 Then
msgbox Error
End If
End Sub
Function ArrayLen(arr as Variant) as Integer
ArrayLen = UBound(arr) - LBound(arr) + 1
End Function
Function GetFilePath(query as String) as String
pathURL = ThisComponent.getURL()
pathOS = ConvertFromURL(pathURL)
fileName = Dir(pathOS)
pathLen = Len(pathOS)
nameLen = Len(fileName)
path = Left(pathOS, pathLen - nameLen)
GetFilePath = path & query
End Function