-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataGenerator.bas
499 lines (366 loc) · 14 KB
/
DataGenerator.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
Attribute VB_Name = "DataGenerator"
' Use this script to generate data from excel file and convert it into test scripts
' Procedurtes to run from the UI:
'
' MakeDataExtract
' Extract data into XML file.
' XML file will be saved in the same folder as original Excel file with name <filename>.testdata.xml
'
' MakeDateExtractAndTransform
' Extracts data and transforms using the stylesheet
' Default stylesheet name is:
Const DEFAULT_STYLESHEET_NAME = "TestData_to_DMCTestSQL.xsl"
' File will be saved under the same folder with the name <file name>.testclass.sql
Const DEFAULT_FILE_EXTENSION = ".testclass.sql"
Const MAX_BLANK_LINES_BETWEEN_BLOCKS = 2
Sub MakeDataExtract()
Dim WB As Workbook
Dim Data As String
Set WB = Application.ActiveWorkbook
Data = "<?xml version='1.0' encoding='UTF-8'?>"
AppendLine Data, GetWorkbookData(WB)
ExportFileName = WB.FullName
ExportFileName = Mid(ExportFileName, 1, InStr(ExportFileName, ".") - 1)
ExportFileName = ExportFileName + ".data.xml"
Open ExportFileName For Output As #1
Print #1, Data
Close #1
End Sub
Sub MakeDataExtractAndTransformOne()
Dim xml As DOMDocument
Dim xslt As DOMDocument
Dim WB As Workbook
Dim W As Workbook
Dim Data As String
Dim StylesheetName As String
Dim FileExtension As String
Dim N As name
Dim R As Range
Set WB = Application.ActiveWorkbook
StylesheetName = DEFAULT_STYLESHEET_NAME
FileExtension = DEFAULT_FILE_EXTENSION
On Error Resume Next
Set N = WB.Names("stylesheet_name")
Set R = N.RefersToRange
If Not (R Is Nothing) Then
ssName = R.value
If ssName <> "" Then
StylesheetName = ssName
End If
End If
On Error GoTo 0
On Error Resume Next
Set N = WB.Names("ouput_file_extenstion")
Set R = N.RefersToRange
If Not (R Is Nothing) Then
feName = R.value
If feName <> "" Then
FileExtension = feName
End If
End If
On Error GoTo 0
Data = "<?xml version='1.0' encoding='UTF-8'?>"
AppendLine Data, GetWorkbookData(WB)
Set xml = New DOMDocument
If xml.LoadXML(Data) Then
Set xslt = New DOMDocument
xsltPath = Application.ThisWorkbook.Path
If xslt.Load(xsltPath + "/" + StylesheetName) Then
Data = xml.transformNode(xslt)
ExportFileName = WB.FullName
ExportFileName = Mid(ExportFileName, 1, InStr(ExportFileName, ".") - 1)
ExportFileName = ExportFileName + FileExtension
Open ExportFileName For Output As #1
Print #1, Data
Close #1
End If
End If
End Sub
Sub MakeDataExtractAndTransform()
Dim xml As DOMDocument
Dim xslt As DOMDocument
Dim WB As Workbook
Dim Opts As Worksheet
Dim Data As String
Dim StylesheetName As String
Dim FileExtension As String
Dim N As name
Dim R As Range
Dim LO As ListObject
Set WB = Application.ActiveWorkbook
Set Opts = WB.Sheets("Options")
On Error Resume Next
Set LO = Opts.ListObjects("TransformationOptions")
On Error GoTo 0
If Not (LO Is Nothing) Then
Data = "<?xml version='1.0' encoding='UTF-8'?>"
AppendLine Data, GetWorkbookData(WB)
Set xml = New DOMDocument
If xml.LoadXML(Data) Then
Set xslt = New DOMDocument
xsltPath = Application.ThisWorkbook.Path
For idx = 1 To LO.ListRows.Count
StylesheetName = LO.ListRows(idx).Range(1)
FileExtension = LO.ListRows(idx).Range(2)
If xslt.Load(xsltPath + "/" + StylesheetName) Then
Data = xml.transformNode(xslt)
ExportFileName = WB.FullName
ExportFileName = Mid(ExportFileName, 1, InStr(ExportFileName, ".") - 1)
ExportFileName = ExportFileName + FileExtension
Open ExportFileName For Output As #1
Print #1, Data
Close #1
End If
Next idx
End If
Else
MakeDataExtractAndTransformOne
End If
End Sub
Sub FileList_MakeDateExtractAndTransform()
Dim WB As Workbook
Dim SList As Worksheet
Dim aName As String
Set SList = Application.ActiveWorkbook.ActiveSheet
row = 1
cnt = 0
cntTotal = 0
While SList.Cells(row, 1).value <> "" Or row = 1
aName = SList.Cells(row, 1).value
On Error Resume Next
If aName > "" Then
Set WB = Application.Workbooks.Open(aName)
cntTotal = cntTotal + 1
If Not (WB Is Empty) Then
WB.Activate
MakeDateExtractAndTransform
WB.Close
cnt = cnt + 1
End If
End If
On Error GoTo 0
row = row + 1
Wend
MsgBox Str(cnt) + " out of " + Str(cntTotal) + " tests has been processed"
End Sub
Function GetWorkbookData(WB As Workbook) As String
Dim S As Worksheet
Dim OptionsWS As Worksheet
Dim Res As String
Dim dataset_name As String
Dim R As Range
Dim N As name
Dim optionName As String
Dim optionValue As String
'On Error Resume Next
dataset_name = WB.name
dataset_name = Mid(dataset_name, 1, InStr(dataset_name, ".") - 1)
GetWorkbookData = "<dataset>"
AppendLine GetWorkbookData, "<source-info>"
AppendLine GetWorkbookData, getTextElementString("DatasetName", dataset_name)
AppendLine GetWorkbookData, getTextElementString("FileName", WB.FullName)
AppendLine GetWorkbookData, getTextElementString("GeneratedAt", Format(Now, "yyyy-MM-dd hh:mm:ss"))
AppendLine GetWorkbookData, "</source-info>"
On Error Resume Next
AppendLine GetWorkbookData, "<options>"
For Each N In WB.Names
Set R = N.RefersToRange
If Not (R Is Nothing) Then
If R.Worksheet.name = "Options" Then
optionName = N.name
optionValue = R.value
If optionValue <> "" Then
AppendLine GetWorkbookData, getTextElementString(optionName, optionValue)
End If
End If
End If
Next N
AppendLine GetWorkbookData, "</options>"
On Error GoTo 0
For Each S In WB.Worksheets
If Not (S.name = "Options" Or S.name = "Documentation" Or Mid(S.name, 1, 1) = "_") Then
AppendLine GetWorkbookData, GetSheetData(S)
End If
Next S
AppendLine GetWorkbookData, "</dataset>"
End Function
Function GetSheetData(S As Worksheet) As String
Dim R As Range
Dim rowIdx As Integer
Dim colIdx As Integer
Dim endOfSheet As Boolean
Dim blankLinesSkipped As Integer
Dim Data As String
colIdx = 1
rowIdx = 1
endOfSheet = False
blankLinesSkipped = 0
Data = "<data ref='" + S.name + "'>"
blankLinesSkipped = skipBlankLine(S, colIdx, rowIdx, MAX_BLANK_LINES_BETWEEN_BLOCKS)
endOfSheet = blankLinesSkipped > MAX_BLANK_LINES_BETWEEN_BLOCKS
While Not endOfSheet
Set R = S.Cells(rowIdx, colIdx)
If R.value = "Name" Or R.value = "Description" Then
AppendLine Data, GetDescriptionData(R)
ElseIf Mid(R.value, 1, 1) <> "_" Then
AppendLine Data, GetBlockData(R)
End If
skipTillBlankLine S, colIdx, rowIdx
blankLinesSkipped = skipBlankLine(S, colIdx, rowIdx, MAX_BLANK_LINES_BETWEEN_BLOCKS)
endOfSheet = blankLinesSkipped > MAX_BLANK_LINES_BETWEEN_BLOCKS
Wend
AppendLine Data, "</data>"
GetSheetData = Data
End Function
Private Sub skipTillBlankLine(ByRef S As Worksheet, ByRef col As Integer, ByRef row As Integer)
Dim C As Range
While S.Cells(row, col).value <> ""
row = row + 1
Wend
End Sub
Private Function skipBlankLine(ByRef S As Worksheet, ByRef col As Integer, ByRef row As Integer, maxLines As Integer) As Integer
Dim skippedLines As Integer
skippedLines = 0
While S.Cells(row, col).value = "" And skippedLines <= maxLines
row = row + 1
skippedLines = skippedLines + 1
Wend
skipBlankLine = skippedLines
End Function
Private Function GetDescriptionData(Start As Range) As String
Dim S As Worksheet
Dim DescriptionID As String
Dim DescriptionValue As String
DescriptionID = Start.value
Set S = Start.Worksheet
DescriptionValue = S.Cells(Start.row, Start.Column + 1).value
GetDescriptionData = getTextElementString(DescriptionID, DescriptionValue)
End Function
Private Function GetBlockData(Start As Range) As String
Dim S As Worksheet
Dim Result As String
Dim BlockID As String
Dim BlockTypeID As String
Set S = Start.Worksheet
BlockID = Start.value
BlockTypeID = S.Cells(Start.row, Start.Column + 1).value
Result = "<" + BlockID
If BlockTypeID <> "" Then
Append Result, " type='" + BlockTypeID + "'"
Else
BlockTypeID = BlockID
End If
Append Result, ">"
AppendLine Result, GetColumnDescription(Start)
AppendLine Result, GetData(Start, BlockTypeID)
AppendLine Result, "</" + BlockID + ">"
GetBlockData = Result
End Function
Private Function GetColumnDescription(Start As Range) As String
Dim S As Worksheet
Dim col As Integer
Dim DataTypeRow As Integer
Dim ColumnNameRow As Integer
Dim CaptionRow As Integer
Dim columnType As String
Dim columnName As String
Dim columnCaption As String
Dim cellStr As String
DataTypeRow = Start.row + 1
ColumnNameRow = Start.row + 2
CaptionRow = Start.row + 3
Set S = Start.Worksheet
GetColumnDescription = "<columns>"
col = Start.Column
While S.Cells(DataTypeRow, col).value <> ""
columnType = S.Cells(DataTypeRow, col).value
columnName = S.Cells(ColumnNameRow, col).value
columnCaption = S.Cells(CaptionRow, col).value
If Mid(columnType, 1, 1) <> "_" Then
cellStr = "<column>" _
+ getTextElementString("type", columnType) _
+ getTextElementString("name", columnName) _
+ getTextElementString("caption", columnCaption) _
+ "</column>"
Else
cellStr = ""
End If
AppendLine GetColumnDescription, cellStr
col = col + 1
Wend
AppendLine GetColumnDescription, "</columns>"
End Function
Private Function GetData(Start As Range, parentID As String) As String
Dim S As Worksheet
Dim col As Integer
Dim DataTypeRow As Integer
Dim ColumnNameRow As Integer
Dim DataRow As Integer
Dim columnType As String
Dim name As String
Dim value As String
Dim cellStr As String
DataTypeRow = Start.row + 1
ColumnNameRow = Start.row + 2
DataRow = Start.row + 4
Set S = Start.Worksheet
While S.Cells(DataRow, Start.Column).value <> ""
AppendLine GetData, "<row id='" + parentID + "_" + Format(DataRow - Start.row - 3) + "'>"
col = Start.Column
While S.Cells(DataTypeRow, col).value <> ""
columnType = S.Cells(DataTypeRow, col).value
name = S.Cells(ColumnNameRow, col).value
value = S.Cells(DataRow, col).value
If Mid(columnType, 1, 1) <> "_" Then
cellStr = getElementString(columnType, name, value)
Else
cellStr = ""
End If
AppendLine GetData, cellStr
col = col + 1
Wend
AppendLine GetData, "</row>"
DataRow = DataRow + 1
Wend
End Function
Private Function getElementValue(value As String) As String
Dim badStrings As String
badstring = "<>?/\&%@"
doEscape = False
For i = 1 To Len(badstring)
searchChr = Mid(badstring, i, 1)
If InStr(value, searchChr) > 0 Then
doEscape = True
Exit For
End If
Next i
getElementValue = value
If doEscape Then
getElementValue = "<![CDATA[" + getElementValue + "]]>"
End If
End Function
Private Function getElementString(elementName As String, name As String, value As String) As String
getElementString = "<" + elementName
If name <> "" Then
getElementString = getElementString + " name='" + name + "'"
End If
getElementString = getElementString + ">" + getElementValue(value) + "</" + elementName + ">"
End Function
Private Function getTextElementString(elementName As String, value As String) As String
getTextElementString = getElementString(elementName, "", value)
End Function
Sub Append(ByRef Trg As String, ByRef newString As String)
Trg = Trg + newString
End Sub
Sub AppendLine(ByRef Trg As String, ByRef newString As String)
Append Trg, newString
Append Trg, Chr(10)
End Sub
Sub AppendLineOffsetTab(ByRef Trg As String, ByRef newString As String)
Append Trg, Chr(9)
AppendLine Trg, newString
End Sub
Sub AppendLineOffset(ByRef Trg As String, ByRef newString As String)
Append Trg, " "
AppendLine Trg, newString
End Sub