Skip to content

Commit f04b6c1

Browse files
author
Doug Schmidt
authored
Merge pull request #313 from DougSchmidt-AI/feature/PF-1448-ExelCsvExtractorImprovements
PF-1448 - Default to a comma, with no trailing space, as the separator
2 parents 29b0b43 + fa106f4 commit f04b6c1

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

TimeSeries/PublicApis/SdkExamples/ExcelCsvExtractor/Context.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public class Context
1010
public bool Overwrite { get; set; }
1111
public string DateTimeFormat { get; set; }
1212
public bool TrimEmptyColumns { get; set; } = true;
13+
public string ColumnSeparator { get; set; } = ",";
1314
}
1415
}

TimeSeries/PublicApis/SdkExamples/ExcelCsvExtractor/Program.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ private static Context GetContext(string[] args)
100100
Description = $"Output path of CSVs (default: {{{Splitter.ExcelPathPattern}}}.{{{Splitter.SheetNamePattern}}}.csv"
101101
},
102102
new Option
103+
{
104+
Key = nameof(context.ColumnSeparator),
105+
Setter = value => context.ColumnSeparator = value,
106+
Getter = ()=> context.ColumnSeparator,
107+
Description = "Separator between columns"
108+
},
109+
new Option
103110
{
104111
Key = nameof(context.Overwrite),
105112
Setter = value => context.Overwrite = bool.Parse(value),
@@ -127,7 +134,7 @@ var usageMessage
127134
+ $"\n"
128135
+ $"\nusage: {GetProgramName()} [-option=value] [@optionsFile] [location] ..."
129136
+ $"\n"
130-
+ $"\nSupported -option=value settings (/option=value works too):\n\n -{string.Join("\n -", options.Select(o => o.UsageText()))}"
137+
+ $"\nSupported -option=value settings (/option=value works too):\n\n {string.Join("\n ", options.Select(o => o.UsageText()))}"
131138
+ $"\n"
132139
+ $"\nUse the @optionsFile syntax to read more options from a file."
133140
+ $"\n"
@@ -196,6 +203,9 @@ private static void ValidateContext(Context context)
196203
{
197204
if (!File.Exists(context.ExcelPath))
198205
throw new ExpectedException($"The /{nameof(context.ExcelPath)} argument is required.");
206+
207+
if (string.IsNullOrWhiteSpace(context.ColumnSeparator))
208+
throw new ExpectedException($"The /{nameof(context.ColumnSeparator)} argument cannot be empty or blank.");
199209
}
200210

201211
private static bool ResolvePositionalArgument(Context context, string arg)

TimeSeries/PublicApis/SdkExamples/ExcelCsvExtractor/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Supported -option=value settings (/option=value works too):
8181
-ExcelPath Specifies the Excel workbook to split
8282
-Sheets Split out the named sheets. [default: All sheets]
8383
-OutputPath Output path of CSVs (default: {ExcelPath}.{SheetName}.csv
84+
-ColumnSeparator Separator between columns [default: ,]
8485
-Overwrite Set to true to overwrite existing files. [default: False]
8586
-TrimEmptyColumns Set to false to retain empty columns at the end of each row. [default: True]
8687
-DateTimeFormat Sets the format of any timestamps, using .NET datetime format [default: ISO8601]

TimeSeries/PublicApis/SdkExamples/ExcelCsvExtractor/Splitter.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void ExtractCsv(DataTable table)
129129
}
130130
}
131131

132-
writer.WriteLine(string.Join(", ", columns));
132+
writer.WriteLine(string.Join(Context.ColumnSeparator, columns));
133133
}
134134
}
135135
}
@@ -138,10 +138,18 @@ private void ExtractCsv(DataTable table)
138138

139139
private string FormatCell(object cell)
140140
{
141+
if (cell == null)
142+
return string.Empty;
143+
141144
if (cell is DateTime dateTime)
142145
return dateTime.ToString(Context.DateTimeFormat ?? "O");
143146

144-
return $"{cell}";
147+
var text = cell.ToString();
148+
149+
if (Context.TrimEmptyColumns)
150+
text = text.Trim();
151+
152+
return text;
145153
}
146154

147155
private static string CsvEscapedColumn(string text)

0 commit comments

Comments
 (0)