Skip to content

Commit

Permalink
SheetAppender custom CsvConfiguration implementation #40
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveWinward committed Jun 3, 2024
1 parent e7ba432 commit d9cbe78
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

```
Expand Down
2 changes: 1 addition & 1 deletion src/GoogleSheetsWrapper/GoogleSheetsWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/SteveWinward/GoogleSheetsWrapper</PackageProjectUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/SteveWinward/GoogleSheetsWrapper</RepositoryUrl>
<Version>2.0.13</Version>
<Version>2.0.14</Version>
<PackageTags>Google Sheets</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Description>A simple wrapper library that makes it easier to perform CRUD operations against Google Sheets spreadsheets.</Description>
Expand Down
21 changes: 17 additions & 4 deletions src/GoogleSheetsWrapper/SheetAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,24 @@ public void AppendCsv(string filePath, bool includeHeaders, int batchWaitTime =
/// <param name="batchWaitTime"></param>
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);
}

/// <summary>
/// Appends a CSV file and all its rows into the current Google Sheets tab
/// </summary>
/// <param name="stream"></param>
/// <param name="csvConfig"></param>
/// <param name="batchWaitTime"></param>
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;

Expand All @@ -84,7 +97,7 @@ public void AppendCsv(Stream stream, bool includeHeaders, int batchWaitTime = 10

var currentBatchCount = 0;

if (includeHeaders)
if (csvConfig.HasHeaderRecord)
{
currentBatchCount++;

Expand Down

0 comments on commit d9cbe78

Please sign in to comment.