-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadCSV_RegEx.bsl
50 lines (39 loc) · 1.21 KB
/
ReadCSV_RegEx.bsl
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
Function ReadCSV_RegEx(FileName, ColumnsCount, Separator=";", SkipFirstRows = 0)
Res = New ValueTable;
For ColNum = 1 To ColumnsCount Do
Res.Columns.Add("Column"+ColNum, New TypeDescription("String"));
EndDo;
TextDoc = New TextDocument;
TextDoc.Read(FileName, TextEncoding.UTF8);
ColNum = 0;
RowNum = 0;
NewRow = Undefined;
RegExp = New COMObject("VBScript.RegExp");
RegExp.IgnoreCase = False;
RegExp.Global = True;
RegExp.Pattern = "(?:,|\n|^)(""(?:(?:"""")*[^""]*)*""|[^"",\n]*|(?:\n|$))";
Matches=RegExp.Execute(TextDoc.GetText());
If Matches.Count() > 0 Then
NewRow = Res.Add();
For Each Item In Matches Do
ColNum = ColNum + 1;
If ColNum > ColumnsCount Then
RowNum = RowNum + 1;
ColNum = 1;
If SkipFirstRows > 0 AND RowNum <= SkipFirstRows Then
continue;
EndIf;
NewRow = Res.Add();
EndIf;
If SkipFirstRows > 0 AND RowNum <= SkipFirstRows Then
continue;
EndIf;
Value = Item.SubMatches.Item(0);
If left(Value, 1) = """" AND Right(Value, 1) = """" Then
Value = Mid(Value, 2, StrLen(Value) - 2);
EndIf;
NewRow[ColNum -1] = StrReplace(Value, """""", """");
EndDo;
EndIf;
Return Res;
EndFunction