I want to compare column headers in excel file with IF activity, Column headers may not come in order, how can I do this according to the colon index number?
Hi @tolgademir ,
Could you let us know what do you want to compare the column names with ?
Do you have a list of column names stored ? Do you want to check if all the column names in list are present in the excel column headers ?
I want to do the operation in your last statement, I want to check for errors such as typos
Could you give us an example of what you have already setup or what inputs you already have and also provide us with the Expected Output ?
This way we can be clear of your requirements.
This is the excel table I have, I will rarely compare it with the data here, if the column bases do not match, I will give an error in the if parts.
Assuming you want to compare if all column names are present in the Excel sheet, then we can check the below Steps :
- Store the Column Names in an Array or a List like below :
columnNames = {"First Name","Last Name","Company Name","Role in Company","Address","Email","Phone Number"}
Here, columnNames
is a variable of type Array of String.
-
We can then Read the Excel sheet as a Datatable using Workbook
Read Range
activity withAdd Headers
enabled. Let’s say the datatable variable beDT
. -
Next, we can compare the column names present in the Datatable with the column names in the List prepared using an
If
activity with the condition as below :
columnNames.All(Function(x)DT.Columns.Cast(Of DataColumn).Any(Function(y)y.ColumnName.ToString.Equals(x)))
The above expression would return a Boolean value which will be true if all the column names in the list are present in the Datatable/Excel sheet.
Hi @tolgademir
To check the column is there or not in the datatable use the below linq query.
- Assign -> ColumnExists = datatable.Columns.Cast(Of DataColumn)().Any(Function(c) c.ColumnName = "ColumnName")
The ColumnExists variable gives boolean value.
After this use the If condition to check the boolean value is true or false
If it is true then you can proceed further
If it is false then throw the error by using throw activity.
Hope it helps!!
You can use this directly…assuming data is read into datatable dt and column names are present in colNames variable of type array of strings
colNames = {"First Name","Last Name","Company Name","Role in Company","Address","Email","Phone Number"}
Now use below in if condition …on then side you have all columns matched and on else side you have atleast one unmatched
(From dc In dt.Columns.Cast(Of DataColumn) Select dc.ColumnName).ToArray().Except(colNames).Count=0
Hope this helps
Cheers
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.