From a67f01559a26b449e79fbaf2b825417bd0af705a Mon Sep 17 00:00:00 2001 From: drgrieve543 Date: Thu, 31 Oct 2024 12:56:02 +1100 Subject: [PATCH] add append object method so values can be imported correctly instead of just all being strings --- src/GoogleSheetsWrapper/SheetAppender.cs | 115 ++++++++++++++++++++++- 1 file changed, 113 insertions(+), 2 deletions(-) diff --git a/src/GoogleSheetsWrapper/SheetAppender.cs b/src/GoogleSheetsWrapper/SheetAppender.cs index cf45a3a..7513ffe 100644 --- a/src/GoogleSheetsWrapper/SheetAppender.cs +++ b/src/GoogleSheetsWrapper/SheetAppender.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -155,7 +156,69 @@ public void AppendCsv(Stream stream, CsvConfiguration csvConfig, int batchWaitTi AppendRows(rowData); } } - } + } + + public void AppendObject(IEnumerable dataRecords, int batchWaitTime = 1000, int batchSize = 100, bool skipWritingHeaderRow = false) where T : class + { + var batchRowLimit = batchSize; + + var rowData = new List(); + + 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() + }; + + 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() + }; + + 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(); + currentBatchCount = 0; + + Thread.Sleep(batchWaitTime); + } + } + + if (rowData.Count > 0) + { + AppendRows(rowData); + } + } /// /// Append a single row to the spreadsheet @@ -253,6 +316,54 @@ private static CellData StringToCellData(object value) } return cell; - } + } + + /// + /// Converts a object into its appropriate cell data object + /// + /// + /// + 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; + } } } \ No newline at end of file