I have a .xlsx file and it have many sheets , i want to find is there any columns which are NullOrEmty in each sheets and if found one i want to perform some logging and throw
Currently I’m trying to do it with a flowchart inside for each datatble (each sheets) i have did it for two sheets but theere are 7 of them and some of them have more than 10 columns in it and possibly can be added more in the future
Is there any solution? I want to perform logging and a throw for each column which is empty in each sheet
Hi @indiedev91
Follow the below steps to achieve your case.
Use the Read Range activity to read the Excel file into a data table.
Use a For Each loop to iterate through each sheet in the file. Inside the loop:
Use the Read Range activity to read the current sheet into a data table.
Use a For Each loop to iterate through each column in the data table. Inside the loop:
i). Use an If activity to check if all rows in the column are null or empty.
ii). If the column is empty, use a Log Message activity to log a message and use a Throw activity to throw an exception.
The Any Function will help you out here.
This is what I was able to come up with:
(From c In Enumerable.Range(0,dt.Columns.Count)
Let colData = String.Join("",Enumerable.Range(0,dt.Rows.Count).Select(Function(r) dt.Rows(r)(c)))
Let containsBlank = IsNothing(colData) OrElse String.IsNullOrEmpty(colData.ToString.Trim)
Select containsBlank).
Any(Function(a) a)
I bet there is an easier way to solve this, but this is what I came up with off the top of my head. CheckifColumnsareBlank.xaml (17.5 KB)
Throwing exceptions is tricky, since the execution stops there.
You can either use a Log Message with the type set to error, or if you want to use a BE, then encase it inside a try catch like so:
If you want to get a list of the columns that are empty, then you can use this:
(From c In Enumerable.Range(0,dt.Columns.Count)
Let colData = String.Join("",Enumerable.Range(0,dt.Rows.Count).Select(Function(r) dt.Rows(r)(c)))
Where IsNothing(colData) OrElse String.IsNullOrEmpty(colData.ToString.Trim)
Select dt.Columns(c).ColumnName).ToArray()
(From c In Enumerable.Range(0,dt.Columns.Count)
Let colData = String.Join("",Enumerable.Range(0,dt.Rows.Count).Select(Function(r) dt.Rows(r)(c)))
Let containsBlank = IsNothing(colData) OrElse String.IsNullOrEmpty(colData.ToString.Trim)
Select containsBlank).
Any(Function(a) a)
This consists of nested loops - one which iterates over the columns, while the inner loop iterates over the rows.
The loop is designed to collect data horizontally, and that data is joined together using String.Join.
From c In Enumerable.Range(0,dt.Columns.Count)
Let colData = String.Join("",Enumerable.Range(0,dt.Rows.Count).Select(Function(r) dt.Rows(r)(c)))
This is done to check if there is any data present in the column, which is taken care of in the containsBlank segment.
Let containsBlank = IsNothing(colData) OrElse String.IsNullOrEmpty(colData.ToString.Trim)
Lastly, we check if Any columns in the datatable are blank, and if so then it returns true.
Any(Function(a) a)
If you’d like to learn more on LINQ, I’ve written few posts on it which you can check out from here.