Skip to content

Commit

Permalink
add append object method so values can be imported correctly instead …
Browse files Browse the repository at this point in the history
…of just all being strings
  • Loading branch information
drgrieve committed Oct 31, 2024
1 parent d3a0928 commit a67f015
Showing 1 changed file with 113 additions and 2 deletions.
115 changes: 113 additions & 2 deletions src/GoogleSheetsWrapper/SheetAppender.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -155,7 +156,69 @@ public void AppendCsv(Stream stream, CsvConfiguration csvConfig, int batchWaitTi
AppendRows(rowData);
}
}
}
}

public void AppendObject<T>(IEnumerable<T> dataRecords, int batchWaitTime = 1000, int batchSize = 100, bool skipWritingHeaderRow = false) where T : class

Check warning on line 161 in src/GoogleSheetsWrapper/SheetAppender.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SheetAppender.AppendObject<T>(IEnumerable<T>, int, int, bool)'

Check warning on line 161 in src/GoogleSheetsWrapper/SheetAppender.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SheetAppender.AppendObject<T>(IEnumerable<T>, int, int, bool)'
{
var batchRowLimit = batchSize;

var rowData = new List<RowData>();

var currentBatchCount = 0;

var properties = typeof(T).GetProperties().Where(x => !x.CustomAttributes.Any(a => a.AttributeType == typeof(CsvHelper.Configuration.Attributes.IgnoreAttribute)));

// Only write the header record if its specified to not skip writing the header row
if (!skipWritingHeaderRow)
{
currentBatchCount++;

var row = new RowData()
{
Values = new List<CellData>()
};

foreach (var header in properties)
{
row.Values.Add(StringToCellData(header.Name));
}

rowData.Add(row);
}

foreach (var record in dataRecords)
{
currentBatchCount++;

var row = new RowData()
{
Values = new List<CellData>()
};

foreach (var property in properties)
{
var propertyValue = property.GetValue(record);
row.Values.Add(ObjectToCellData(propertyValue));
}

rowData.Add(row);

if (currentBatchCount >= batchRowLimit)
{
AppendRows(rowData);

rowData = new List<RowData>();
currentBatchCount = 0;

Thread.Sleep(batchWaitTime);
}
}

if (rowData.Count > 0)
{
AppendRows(rowData);
}
}

/// <summary>
/// Append a single row to the spreadsheet
Expand Down Expand Up @@ -253,6 +316,54 @@ private static CellData StringToCellData(object value)
}

return cell;
}
}

/// <summary>
/// Converts a object into its appropriate cell data object
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static CellData ObjectToCellData(object value)
{
var cell = new CellData
{
UserEnteredValue = new ExtendedValue()
};

if (value != null)
{
if (value is bool b)
{
cell.UserEnteredValue.BoolValue = b;
}
else if (value is int i)
{
cell.UserEnteredValue.NumberValue = i;
}
else if (value is double dbl)
{
cell.UserEnteredValue.NumberValue = dbl;
}
else if (value is decimal dec)
{
cell.UserEnteredValue.NumberValue = Decimal.ToDouble(dec);
}
else if (value is DateTime dt)
{
cell.UserEnteredFormat = new CellFormat { NumberFormat = new NumberFormat { Type = "Date" } };
cell.UserEnteredValue.NumberValue = dt.ToOADate();
}
else
{
cell.UserEnteredValue.StringValue = value.ToString();
}
}
else
{
cell.UserEnteredValue.StringValue = string.Empty;
}

return cell;
}
}
}

0 comments on commit a67f015

Please sign in to comment.