Skip to content

Commit a4c8c34

Browse files
committed
Improvements: integration with VBAexpressions core modules.
1 parent fc27278 commit a4c8c34

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

docs/api/csvarraylist.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ Class module developed to emulate some functionalities from the `ArrayList` pres
5252
<td style="text-align: left;">Concatenates the values from the current instance with the specified values and returns a <code>CSVArraylist</code> object as result. The <code>AValues</code> parameter is a <code>Variant</code> data type containing the array, <code>CSVArraylist</code> or value to concatenate.</td>
5353
</tr>
5454
<tr>
55+
<td style="text-align: left; color:blue;"><em>Concat2</em></td>
56+
<td style="text-align: left;">Method</td>
57+
<td style="text-align: left;">Concatenates the values from the current instance with the specified values and returns a <code>CSVArraylist</code> object as result. The <code>AValues</code> parameter is a <code>CSVArraylist</code> with the values to concatenate.</td>
58+
</tr>
59+
<tr>
5560
<td style="text-align: left; color:blue;"><em>Copy</em></td>
5661
<td style="text-align: left;">Method</td>
5762
<td style="text-align: left;">Returns a <code>CSVArraylist</code> object with a copy of the current instance from and to a given index. The <code>StartIndex</code> parameter indicates where the copy will start and the <code>EndIndex</code> determines where the operation will end. If the <code>EndIndex</code> parameter is set to <code>-1</code>, the operation will end at the maximum index available for the current instance.</td>
@@ -72,6 +77,11 @@ Class module developed to emulate some functionalities from the `ArrayList` pres
7277
<td style="text-align: left;">Creates an empty jagged array. The operation will turns the array <code>ArrVar</code> into an jagged array with <code>ArraySize + 1</code> rows and each row with <code>VectorSize</code> columns. To access to an individual element user must use something like <code>expression(i)(j)</code>, where <code>i</code> denotes an index in the main array and <code>j</code> denotes an index in the child array.</td>
7378
</tr>
7479
<tr>
80+
<td style="text-align: left; color:blue;"><em>Filter</em></td>
81+
<td style="text-align: left;">Method</td>
82+
<td style="text-align: left;">Returns a filtered array list using the <code>CSVexpressions</code> class module.</td>
83+
</tr>
84+
<tr>
7585
<td style="text-align: left; color:blue;"><em>Insert</em></td>
7686
<td style="text-align: left;">Method</td>
7787
<td style="text-align: left;">Inserts an Item, at the given Index, in the current instance of the class.</td>

docs/api/methods/filter.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ nav_order: 10
1111
New
1212
{: .label .label-purple }
1313

14-
Returns a list of records as a result of applying filters on the target CSV file using expression evaluation.
14+
Returns a list of records as a result of applying filters on the target CSV file or imported CSV data using expression evaluation.
1515
{: .fs-4 .fw-300 }
1616

1717
---
1818

1919
## Syntax
2020

21-
*expression*.`Filter`*(FilePath, Pattern, ExcludeFirstRecord)*
21+
*expression*.`Filter`*(Pattern, [FilePath], [ExcludeFirstRecord])*
2222

2323
### Parameters
2424

@@ -31,14 +31,14 @@ Returns a list of records as a result of applying filters on the target CSV file
3131
</thead>
3232
<tbody>
3333
<tr>
34-
<td style="text-align: left;"><em>FilePath</em></td>
35-
<td style="text-align: left;">Required. Identifier specifying a <code>String</code> Type variable. Represents the full file path, including file extension, of the CSV file used for data filtering.</td>
36-
</tr>
37-
<tr>
3834
<td style="text-align: left;"><em>Pattern</em></td>
3935
<td style="text-align: left;">Required. Identifier specifying a <code>String</code> Type variable. Represents a valid string expression to evaluate when filtering records</td>
4036
</tr>
4137
<tr>
38+
<td style="text-align: left;"><em>FilePath</em></td>
39+
<td style="text-align: left;">Optional. Identifier specifying a <code>String</code> Type variable. Represents the full file path, including file extension, of the CSV file used for data filtering.</td>
40+
</tr>
41+
<tr>
4242
<td style="text-align: left;"><em>ExcludeFirstRecord</em></td>
4343
<td style="text-align: left;">Optional. Identifier specifying a <code>Boolean</code> Type variable. When <code>True</code>, the file headers will be excluded.</td>
4444
</tr>
@@ -61,6 +61,8 @@ The `Pattern` parameter is evaluated according to the number of records in the C
6161
* If the user needs to compare literal strings, the values must be enclosed in apostrophes. Example, `Region = 'Central America'` is a valid string assigned to the variable `Region`.
6262
* User can use functions in the `Pattern` definition, including custom UDFs (refer to [VBAexpressions documentation](https://github.com/ws-garcia/VBA-Expressions)). I.e.: `min(f5;f2)>=100`
6363

64+
When the `FilePath` argument is omitted, the method will proceed to filter the data stored in the current instance, otherwise it will filter the content of the CSV file specified with the referred argument.
65+
6466
### ☕Example
6567

6668
```vb

src/CSVinterface.cls

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -655,32 +655,42 @@ End Function
655655
''' <summary>
656656
''' Filter the target CSV file using expression evaluation.
657657
''' </summary>
658-
''' <param name="FilePath">Full file path, including file extension.</param>
659658
''' <param name="Pattern">A valid string expression to evaluate when filtering records.</param>
659+
''' <param name="FilePath">Full file path, including file extension.</param>
660660
''' <param name="ExcludeFirstRecord">Used if the user wants to exclude the headers.</param>
661-
Public Function Filter(FilePath As String, Pattern As String, _
661+
Public Function Filter(Pattern As String, Optional FilePath As String = vbNullString, _
662662
Optional ExcludeFirstRecord As Boolean = True) As CSVArrayList
663+
663664
Dim CSVparser As CSVinterface
664-
Dim StreamReader As CSVTextStream
665-
Dim streamsCounter As Long
666665

667-
Set CSVparser = New CSVinterface
668-
Set StreamReader = New CSVTextStream
669666
Set Filter = New CSVArrayList
667+
If FilePath <> vbnullestring Then 'Filter CSV file
668+
Dim StreamReader As CSVTextStream
669+
Dim streamsCounter As Long
670+
671+
Set CSVparser = New CSVinterface
672+
Set StreamReader = New CSVTextStream
670673

671-
With CSVparser.parseConfig
672-
.delimitersGuessing = True
673-
End With
674-
With StreamReader
675-
.endStreamOnLineBreak = True
676-
.OpenStream FilePath
677-
Do While Not .atEndOfStream
678-
.ReadText
679-
streamsCounter = streamsCounter + 1
680-
CSVparser.ImportFromCSVString .bufferString, CSVparser.parseConfig
681-
Filter.Concat2 CSVparser.items.Filter(Pattern, 1 + Abs(ExcludeFirstRecord And streamsCounter = 1))
682-
Loop
683-
End With
674+
With CSVparser.parseConfig
675+
.delimitersGuessing = True
676+
End With
677+
With StreamReader
678+
.endStreamOnLineBreak = True
679+
.OpenStream FilePath
680+
Do While Not .atEndOfStream
681+
.ReadText
682+
streamsCounter = streamsCounter + 1
683+
CSVparser.ImportFromCSVString .bufferString, CSVparser.parseConfig
684+
Filter.Concat2 CSVparser.items.Filter(Pattern, 1 + Abs(ExcludeFirstRecord And streamsCounter = 1))
685+
Loop
686+
End With
687+
Else
688+
Set CSVparser = Me
689+
If CSVparser.importSuccess Then
690+
ExcludeFirstRecord = (CSVparser.parseConfig.Headers = True)
691+
Filter.Concat2 CSVparser.items.Filter(Pattern, 1 + Abs(ExcludeFirstRecord))
692+
End If
693+
End If
684694
End Function
685695
''' <summary>
686696
''' Filter imported data using VBA like patterns.

0 commit comments

Comments
 (0)