Skip to content

Commit

Permalink
Fix time column type (#2)
Browse files Browse the repository at this point in the history
* Fix time column type

* codeql v1 to v2
  • Loading branch information
Yanal-Yves committed Aug 29, 2023
1 parent 848e363 commit 77e9d20
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -53,7 +53,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -67,4 +67,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
2 changes: 1 addition & 1 deletion src/ConsoleApp/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Player
[CellDefinition(CellDataType.Time)]
[Header(typeof(PlayerRes), "PracticeTimeColumnName")]
[Index(8)]
public string? PracticeTime { get; set; }
public TimeSpan? PracticeTime { get; set; }

[CellDefinition(CellDataType.Number)]
[Header(typeof(PlayerRes), "NumberOfVictoryColumnName")]
Expand Down
20 changes: 10 additions & 10 deletions src/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void GenerateBigSpreadsheetFromAnnotatedData(DirectoryInfo tempDi
{
PlayerCode = $"Code{i}",
PlayerName = $"Player{i}",
PracticeTime = $"{rnd.Next(0, 1)}{rnd.Next(0, 9)}:{rnd.Next(0, 5)}{rnd.Next(0, 9)}",
PracticeTime = new TimeSpan(rnd.Next(0, 23), rnd.Next(0, 59), 0),
Size = rnd.Next(18, 100),
DateOfBirth = randomDate.Next(),
IsActiveFlag = Convert.ToBoolean(rnd.Next(0, 100) % 2),
Expand Down Expand Up @@ -114,7 +114,7 @@ private static void GenerateBigSpreadsheetFromWorkBookDfn(DirectoryInfo tempDi)
new CellDfn(Convert.ToBoolean(rnd.Next(0, 100) % 2), cellDataType: CellDataType.Boolean),
new CellDfn(rnd.Next(0, 100), cellDataType: CellDataType.Number),
new CellDfn(Convert.ToDouble(rnd.Next(0, 100)) / 100, cellDataType: CellDataType.Percentage),
new CellDfn($"{rnd.Next(0, 1)}{rnd.Next(0, 9)}:{rnd.Next(0, 5)}{rnd.Next(0, 9)}", cellDataType: CellDataType.Time),
new CellDfn(new TimeSpan(rnd.Next(0, 23), rnd.Next(0, 59), 0), cellDataType: CellDataType.Time),
},
};
worksheetDfn.Rows.Add(rowDfn);
Expand Down Expand Up @@ -154,7 +154,7 @@ private static void GenerateSpreadSheetFromAnnotatedData(DirectoryInfo tempDi)
{
PlayerCode = null,
PlayerName = "Alexandre",
PracticeTime = "09:01",
PracticeTime = new TimeSpan(9, 1, 0),
Size = 1.93d,
DateOfBirth = new DateTime(1974, 02, 01),
IsActiveFlag = true,
Expand All @@ -166,7 +166,7 @@ private static void GenerateSpreadSheetFromAnnotatedData(DirectoryInfo tempDi)
{
PlayerCode = "02",
PlayerName = "Elina",
PracticeTime = "09:02",
PracticeTime = new TimeSpan(9, 2, 0),
Size = 1.72d,
DateOfBirth = new DateTime(1990, 10, 13),
IsActiveFlag = true,
Expand All @@ -178,7 +178,7 @@ private static void GenerateSpreadSheetFromAnnotatedData(DirectoryInfo tempDi)
{
PlayerCode = "03",
PlayerName = "Franck",
PracticeTime = "09:03",
PracticeTime = new TimeSpan(9, 3, 0),
Size = 1.85d,
DateOfBirth = new DateTime(1976, 3, 1),
IsActiveFlag = true,
Expand All @@ -190,7 +190,7 @@ private static void GenerateSpreadSheetFromAnnotatedData(DirectoryInfo tempDi)
{
PlayerCode = "04",
PlayerName = "Yann",
PracticeTime = "09:04",
PracticeTime = new TimeSpan(9, 4, 0),
Size = 1.79d,
DateOfBirth = new DateTime(1979, 3, 1),
IsActiveFlag = false,
Expand Down Expand Up @@ -246,7 +246,7 @@ private static void GenerateSpreadSheetFromWorkbookDfn(DirectoryInfo tempDi)
row1.Cells.Add(new CellDfn("01090", cellDataType: CellDataType.String));
row1.Cells.Add(new CellDfn(DateTime.Now, cellDataType: CellDataType.Date));
row1.Cells.Add(new CellDfn(0.0111, cellDataType: CellDataType.Percentage));
row1.Cells.Add(new CellDfn("08:01", cellDataType: CellDataType.Time));
row1.Cells.Add(new CellDfn(new TimeSpan(8, 1, 0), cellDataType: CellDataType.Time));
worksheet1Dfn.Rows.Add(row1);
var row2 = new RowDfn();
row2.Cells.Add(new CellDfn("Bob", cellDataType: CellDataType.String));
Expand All @@ -255,7 +255,7 @@ private static void GenerateSpreadSheetFromWorkbookDfn(DirectoryInfo tempDi)
row2.Cells.Add(new CellDfn("01080", cellDataType: CellDataType.String));
row2.Cells.Add(new CellDfn(DateTime.Now, cellDataType: CellDataType.Date));
row2.Cells.Add(new CellDfn(0.0222, cellDataType: CellDataType.Percentage));
row2.Cells.Add(new CellDfn("08:01", cellDataType: CellDataType.Time));
row2.Cells.Add(new CellDfn(new TimeSpan(8, 1, 0), cellDataType: CellDataType.Time));
worksheet1Dfn.Rows.Add(row2);

// Second sheet
Expand All @@ -279,7 +279,7 @@ private static void GenerateSpreadSheetFromWorkbookDfn(DirectoryInfo tempDi)
row31.Cells.Add(new CellDfn(DateTime.Now, cellDataType: CellDataType.Date));
row31.Cells.Add(new CellDfn(true, cellDataType: CellDataType.Boolean));
row31.Cells.Add(new CellDfn(0.11, cellDataType: CellDataType.Percentage));
row31.Cells.Add(new CellDfn("08:01", cellDataType: CellDataType.Time));
row31.Cells.Add(new CellDfn(new TimeSpan(8, 1, 0), cellDataType: CellDataType.Time));
worksheet3Dfn.Rows.Add(row31);
var row32 = new RowDfn();
row32.Cells.Add(new CellDfn("Bob", cellDataType: CellDataType.String));
Expand All @@ -289,7 +289,7 @@ private static void GenerateSpreadSheetFromWorkbookDfn(DirectoryInfo tempDi)
row32.Cells.Add(new CellDfn(DateTime.Now, cellDataType: CellDataType.Date));
row32.Cells.Add(new CellDfn(false, cellDataType: CellDataType.Boolean));
row32.Cells.Add(new CellDfn(22, cellDataType: CellDataType.Percentage));
row32.Cells.Add(new CellDfn("08:02", cellDataType: CellDataType.Time));
row32.Cells.Add(new CellDfn(new TimeSpan(8, 2, 0), cellDataType: CellDataType.Time));
worksheet3Dfn.Rows.Add(row32);

SpreadsheetWriter spreadsheetWriter = new SpreadsheetWriter(streamWriter.BaseStream, workbookDfn);
Expand Down
7 changes: 7 additions & 0 deletions src/SimpleExcelExporter/SpreadSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -299,6 +300,12 @@ private Cell CreateCell(CellDfn cellDfn)
cell.CellValue = new CellValue(stringValue);
cell.DataType = new EnumValue<CellValues>(CellValues.String);
}
else if (cellDfn.Value is TimeSpan timeSpanValue)
{
// Excel saves time in seconds divided by maximum seconds of a day
double cellValue = timeSpanValue.TotalSeconds / 86400; // 86400 = 24 * 60 *60
cell.CellValue = new CellValue(cellValue.ToString(CultureInfo.InvariantCulture));
}
else
{
throw new NotSupportedException($"Type {cellDfn.Value.GetType()} is not supported as a Cell value");
Expand Down
2 changes: 1 addition & 1 deletion test/SimpleExcelExporterTests/Models/PlayerDummyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class PlayerDummyObject
[CellDefinition(CellDataType.Time)]
[Header(typeof(PlayerDummyObjectRes), "PracticeTimeColumnName")]
[Index(8)]
public string? PracticeTime { get; set; }
public TimeSpan? PracticeTime { get; set; }

[CellDefinition(CellDataType.Number)]
[Header(typeof(PlayerDummyObjectRes), "NumberOfVictoryColumnName")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static WorkbookDfn FirstFirstWithCollections()
row1.Cells.Add(new CellDfn("01090", cellDataType: CellDataType.String));
row1.Cells.Add(new CellDfn(DateTime.Now, cellDataType: CellDataType.Date));
row1.Cells.Add(new CellDfn(0.1111, cellDataType: CellDataType.Percentage));
row1.Cells.Add(new CellDfn("09:01", cellDataType: CellDataType.Time));
row1.Cells.Add(new CellDfn(new TimeSpan(9, 1, 0), cellDataType: CellDataType.Time));
worksheet1Dfn.Rows.Add(row1);
var row2 = new RowDfn();
row2.Cells.Add(new CellDfn("Bob", cellDataType: CellDataType.String));
Expand All @@ -40,7 +40,7 @@ public static WorkbookDfn FirstFirstWithCollections()
row2.Cells.Add(new CellDfn("01080", cellDataType: CellDataType.String));
row2.Cells.Add(new CellDfn(DateTime.Now, cellDataType: CellDataType.Date));
row2.Cells.Add(new CellDfn(0.2222, cellDataType: CellDataType.Percentage));
row2.Cells.Add(new CellDfn("09:02", cellDataType: CellDataType.Time));
row2.Cells.Add(new CellDfn(new TimeSpan(9, 2, 0), cellDataType: CellDataType.Time));
worksheet1Dfn.Rows.Add(row2);

return workbookDfn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class PlayerDummyObjectPreparator
{
PlayerCode = null,
PlayerName = "Player\bName1<a href=\"https://www.google.com\" /> &lt;b /&gt; \r\n\t",
PracticeTime = "09:01",
PracticeTime = new TimeSpan(9, 1, 0),
Size = 1.93d,
DateOfBirth = new DateTime(1974, 02, 01),
IsActiveFlag = true,
Expand All @@ -24,7 +24,7 @@ public static class PlayerDummyObjectPreparator
{
PlayerCode = "02",
PlayerName = "PlayerName2",
PracticeTime = "09:02",
PracticeTime = new TimeSpan(9, 2, 0),
Size = 1.72d,
DateOfBirth = new DateTime(1990, 10, 13),
IsActiveFlag = true,
Expand All @@ -39,7 +39,7 @@ public static class PlayerDummyObjectPreparator
{
PlayerCode = "03",
PlayerName = "PlayerName3",
PracticeTime = "09:03",
PracticeTime = new TimeSpan(9, 3, 0),
Size = 1.85d,
DateOfBirth = new DateTime(1976, 3, 1),
IsActiveFlag = true,
Expand All @@ -55,7 +55,7 @@ public static class PlayerDummyObjectPreparator
{
PlayerCode = "04",
PlayerName = "PlayerName4",
PracticeTime = "09:04",
PracticeTime = new TimeSpan(9, 4, 0),
Size = 1.79d,
DateOfBirth = new DateTime(1979, 3, 1),
IsActiveFlag = false,
Expand Down

0 comments on commit 77e9d20

Please sign in to comment.