From d9cbe782fbce6308bfe2b71d3f125cb53f378ee5 Mon Sep 17 00:00:00 2001 From: SteveWinward <2002602+SteveWinward@users.noreply.github.com> Date: Mon, 3 Jun 2024 11:12:40 -0400 Subject: [PATCH] SheetAppender custom CsvConfiguration implementation #40 --- README.md | 18 ++++++++++++++++ .../GoogleSheetsWrapper.csproj | 2 +- src/GoogleSheetsWrapper/SheetAppender.cs | 21 +++++++++++++++---- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 18a0ea1..66f115b 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,8 @@ var filepath = @"C:\Input\input.csv"; using (var stream = new FileStream(filepath, FileMode.Open)) { + // OPTION 1: Default to CultureInfo.InvariantCulture and "," as the delimiter + // Append the csv file to Google sheets, include the header row // and wait 1000 milliseconds between batch updates // Google Sheets API throttles requests per minute so you may need to play @@ -360,6 +362,22 @@ using (var stream = new FileStream(filepath, FileMode.Open)) stream, // The CSV FileStrem true, // true indicating to include the header row 1000); // 1000 milliseconds to wait every 100 rows that are batch sent to the Google Sheets API + + // OPTION 2: Create your own CsvConfiguration object with full control on Culture and delimiter + var csvConfig = new CsvConfiguration(CultureInfo.CurrentCulture) + { + HasHeaderRecord = true, + Delimiter = ",", + }; + + // Append the csv file to Google sheets, include the header row + // and wait 1000 milliseconds between batch updates + // Google Sheets API throttles requests per minute so you may need to play + // with this setting. + appender.AppendCsv( + stream, // The CSV FileStrem + csvConfig, // The custom CsvConfiguration object defined previously + 1000); // 1000 milliseconds to wait every 100 rows that are batch sent to the Google Sheets API } ``` diff --git a/src/GoogleSheetsWrapper/GoogleSheetsWrapper.csproj b/src/GoogleSheetsWrapper/GoogleSheetsWrapper.csproj index 0ec4a31..ec7229b 100644 --- a/src/GoogleSheetsWrapper/GoogleSheetsWrapper.csproj +++ b/src/GoogleSheetsWrapper/GoogleSheetsWrapper.csproj @@ -8,7 +8,7 @@ https://github.com/SteveWinward/GoogleSheetsWrapper LICENSE https://github.com/SteveWinward/GoogleSheetsWrapper - 2.0.13 + 2.0.14 Google Sheets README.md A simple wrapper library that makes it easier to perform CRUD operations against Google Sheets spreadsheets. diff --git a/src/GoogleSheetsWrapper/SheetAppender.cs b/src/GoogleSheetsWrapper/SheetAppender.cs index a7e22b3..1b22d5c 100644 --- a/src/GoogleSheetsWrapper/SheetAppender.cs +++ b/src/GoogleSheetsWrapper/SheetAppender.cs @@ -67,11 +67,24 @@ public void AppendCsv(string filePath, bool includeHeaders, int batchWaitTime = /// public void AppendCsv(Stream stream, bool includeHeaders, int batchWaitTime = 1000) { - using var streamReader = new StreamReader(stream); - using var csv = new CsvReader(streamReader, new CsvConfiguration(CultureInfo.InvariantCulture) + var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = includeHeaders - }); + }; + + AppendCsv(stream, csvConfig, batchWaitTime); + } + + /// + /// Appends a CSV file and all its rows into the current Google Sheets tab + /// + /// + /// + /// + public void AppendCsv(Stream stream, CsvConfiguration csvConfig, int batchWaitTime = 1000) + { + using var streamReader = new StreamReader(stream); + using var csv = new CsvReader(streamReader, csvConfig); { var batchRowLimit = 100; @@ -84,7 +97,7 @@ public void AppendCsv(Stream stream, bool includeHeaders, int batchWaitTime = 10 var currentBatchCount = 0; - if (includeHeaders) + if (csvConfig.HasHeaderRecord) { currentBatchCount++;