-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathActionHelper.cs
More file actions
225 lines (201 loc) · 9.5 KB
/
Copy pathActionHelper.cs
File metadata and controls
225 lines (201 loc) · 9.5 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Desky.Datatable
{
internal static class ActionHelper
{
public static DataTable ConvertColumnToNumeric(DataTable InputDatatable, string columnName)
{
// Check if the DataTable is not empty
if (InputDatatable != null && InputDatatable.Rows.Count > 0)
{
Console.WriteLine("Converting column to double for " + columnName);
// Check if the column exists in the DataTable
if (InputDatatable.Columns.Contains(columnName))
{
// Get the ordinal position of the column
int pos = InputDatatable.Columns[columnName].Ordinal;
// Create a new column with "_new" appended to the column name
string newColumnName = columnName + "_new";
InputDatatable.Columns.Add(newColumnName, typeof(decimal)).SetOrdinal(pos + 1);
// Iterate through each row and update the new column with the parsed double
foreach (DataRow row in InputDatatable.Rows)
{
string valueString = row[columnName].ToString().Trim();
double parsedValue;
// Try to parse the string to double
if (double.TryParse(valueString, out parsedValue))
{
row[newColumnName] = parsedValue;
}
else
{
// Optionally handle cases where parsing fails (e.g., set to DBNull)
row[newColumnName] = DBNull.Value;
}
}
// Remove the old column and rename the new one to the original column name
InputDatatable.Columns.Remove(columnName);
InputDatatable.Columns[newColumnName].ColumnName = columnName;
}
else
{
throw new Exception("the column named ["+columnName+"] does not exist in referenced table");
}
}
else
{
throw new Exception("referenced table should not be null");
}
return InputDatatable;
}
// The aim of this method is to convert a specific column in a datatable to a date
public static DataTable ConvertColumnToDate(DataTable InputDatatable, string columnName, string expectedDateFormat)
{
// Check if the DataTable is not empty
if (InputDatatable != null && InputDatatable.Rows.Count > 0)
{
Console.WriteLine("fixing date column for " + columnName);
// Check if the column exists in the DataTable
if (InputDatatable.Columns.Contains(columnName))
{
// Get the ordinal position of the column
int pos = InputDatatable.Columns[columnName].Ordinal;
// Create a new column with "_new" appended to the column name
string newColumnName = columnName + "_new";
InputDatatable.Columns.Add(newColumnName, typeof(DateTime)).SetOrdinal(pos + 1);
// Iterate through each row and update the new column with the parsed date
foreach (DataRow row in InputDatatable.Rows)
{
string dateString = row[columnName].ToString().Trim().Replace(" ", string.Empty);
DateTime parsedDate;
// Try to parse the date string using the specified format
if (DateTime.TryParseExact(dateString, expectedDateFormat, null, System.Globalization.DateTimeStyles.None, out parsedDate))
{
row[newColumnName] = parsedDate;
}
}
// Remove the old column and rename the new one to the original column name
InputDatatable.Columns.Remove(columnName);
InputDatatable.Columns[newColumnName].ColumnName = columnName;
}
else
{
throw new ArgumentException("the column named [" + columnName + "] does not exist in referenced table");
}
}
else
{
throw new ArgumentException("referenced table should not be null");
}
return InputDatatable;
}
public static DataTable ConvertAllColumnsToString(DataTable InputDatatable)
{
DataTable dtFix = new DataTable();
// Check if the DataTable is not empty
if (InputDatatable != null && InputDatatable.Rows.Count > 0)
{
foreach (DataColumn col in InputDatatable.Columns)
{
dtFix.Columns.Add(col.ColumnName, typeof(string));
}
foreach (DataRow row in InputDatatable.Rows)
{
object[] newRow = new object[row.ItemArray.Length];
for (int i = 0; i < row.ItemArray.Length; i++)
{
newRow[i] = row[i]?.ToString();
}
dtFix.Rows.Add(newRow);
}
}
else
{
throw new ArgumentException("referenced table should not be null");
}
return dtFix;
}
public static DataTable ReorderColumns(DataTable InputDatatable, List<string> expectedColumnOrder)
{
if (InputDatatable != null && InputDatatable.Columns.Count > 0)
{
int currentPosition = 0;
foreach (string columnName in expectedColumnOrder)
{
if (InputDatatable.Columns.Contains(columnName))
{
InputDatatable.Columns[columnName].SetOrdinal(currentPosition);
currentPosition++;
}
}
return InputDatatable;
}
else
{
throw new ArgumentException("referenced table should have one or more column");
}
}
public static DataTable MergeDatatables(DataTable sourceDataTable, DataTable destinationDataTable)
{
if (sourceDataTable != null && sourceDataTable.Rows.Count > 0)
{
if (destinationDataTable == null)
{
destinationDataTable = sourceDataTable.Clone(); // Clone the structure of sourceDataTable
}
destinationDataTable.Merge(sourceDataTable, false, MissingSchemaAction.Ignore); // Merge the sourceDataTable into destinationDataTable
}
return destinationDataTable;
}
public static double SumColumnValues(DataTable dataTable, string columnName)
{
if (dataTable == null || !dataTable.Columns.Contains(columnName))
{
throw new ArgumentException("the column named [" + columnName + "] does not exist in referenced table");
}
double sum = dataTable.AsEnumerable().Sum(row =>
{
string cellValue = row[columnName]?.ToString().Trim();
if (string.IsNullOrEmpty(cellValue) || !double.TryParse(cellValue, out double numericValue))
{
return 0; // Treat non-numeric or empty values as zero
}
return numericValue;
});
return sum;
}
public static string ConvertDataTableToHtml(DataTable dt)
{
StringBuilder builder = new StringBuilder();
string tableOpeningTag = "<table border='1' style='border-collapse:collapse'>";
Console.WriteLine("Setting HTML mail table header....");
if (dt != null && dt.Rows.Count > 0)
{
builder.Append("<h2><b>Total computation :</b></h2>");
builder.Append(tableOpeningTag);
// Create table header
var tableHeader = "<tr>" +
string.Join(Environment.NewLine, dt.Columns.Cast<DataColumn>()
.Select(c => $"<th style=\"text-align: center\">{c.ColumnName}</th>")) +
"</tr>";
builder.Append(tableHeader);
// Generate the body of the table
Console.WriteLine("Now generating body of HTML table....");
string tdTag = "<td style=\"text-align: center\">{0}</td>";
var rows = dt.AsEnumerable().Select(r =>
"<tr>" +
string.Join(Environment.NewLine, r.ItemArray.Select(e => string.Format(tdTag, e.ToString()))) +
"</tr>");
builder.Append(string.Join(Environment.NewLine, rows));
// Add table closing tag
builder.Append("</table>");
}
return builder.ToString();
}
}
}