Skip to content

Commit

Permalink
bug fix #38, ensure service is first initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveWinward committed Jan 20, 2024
1 parent 8d75b7c commit 20f3593
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
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.10</Version>
<Version>2.0.11</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
53 changes: 53 additions & 0 deletions src/GoogleSheetsWrapper/SheetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public class SheetHelper
/// </summary>
public SheetsService Service { get; private set; }

/// <summary>
/// Private field indicating if the Init() method has been executed
/// </summary>
private bool IsInitialized;

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -67,6 +72,8 @@ public SheetHelper(string spreadsheetID, string serviceAccountEmail, string tabN
public void Init(string jsonCredentials)
{
Init(jsonCredentials, default);

IsInitialized = true;
}

/// <summary>
Expand Down Expand Up @@ -98,6 +105,20 @@ public void Init(string jsonCredentials, Google.Apis.Http.IHttpClientFactory htt
Service = service;

UpdateTabName(TabName);

IsInitialized = true;
}

/// <summary>
/// Throws ArgumentException if the Init() method has not been called yet.
/// </summary>
/// <exception cref="ArgumentException"></exception>
protected void EnsureServiceInitialized()
{
if (!IsInitialized)
{
throw new ArgumentException("SheetHelper requires the Init(string jsonCredentials) method to be called before using any of its methods.");
}
}

/// <summary>
Expand All @@ -106,6 +127,8 @@ public void Init(string jsonCredentials, Google.Apis.Http.IHttpClientFactory htt
/// <param name="newTabName"></param>
public void UpdateTabName(string newTabName)
{
this.EnsureServiceInitialized();

var spreadsheet = Service.Spreadsheets.Get(SpreadsheetID);

var result = spreadsheet.Execute();
Expand Down Expand Up @@ -139,6 +162,8 @@ public void UpdateTabName(string newTabName)
/// <returns></returns>
public List<string> GetAllTabNames()
{
this.EnsureServiceInitialized();

var spreadsheet = Service.Spreadsheets.Get(SpreadsheetID);

var result = spreadsheet.Execute();
Expand All @@ -161,6 +186,8 @@ public List<string> GetAllTabNames()
ValueRenderOptionEnum valueRenderOption = ValueRenderOptionEnum.UNFORMATTEDVALUE,
DateTimeRenderOptionEnum dateTimeRenderOption = DateTimeRenderOptionEnum.SERIALNUMBER)
{
this.EnsureServiceInitialized();

var rangeValue = range.CanSupportA1Notation ? range.A1Notation : range.R1C1Notation;

var request =
Expand All @@ -180,6 +207,8 @@ public List<string> GetAllTabNames()
/// <returns></returns>
public IList<IList<object>>? GetRowsFormatted(SheetRange range)
{
this.EnsureServiceInitialized();

var rangeValue = range.CanSupportA1Notation ? range.A1Notation : range.R1C1Notation;

var request =
Expand All @@ -201,6 +230,8 @@ public List<string> GetAllTabNames()
/// <returns></returns>
public ClearValuesResponse ClearRange(SheetRange range)
{
this.EnsureServiceInitialized();

var rangeValue = range.CanSupportA1Notation ? range.A1Notation : range.R1C1Notation;

var requestBody = new ClearValuesRequest();
Expand All @@ -216,6 +247,8 @@ public ClearValuesResponse ClearRange(SheetRange range)
/// <returns></returns>
public BatchUpdateSpreadsheetResponse DeleteColumn(int col)
{
this.EnsureServiceInitialized();

var request = new Request()
{
DeleteDimension = new DeleteDimensionRequest()
Expand Down Expand Up @@ -246,6 +279,8 @@ public BatchUpdateSpreadsheetResponse DeleteColumn(int col)
/// <returns></returns>
public BatchUpdateSpreadsheetResponse DeleteColumn(string columnLetter)
{
this.EnsureServiceInitialized();

var columnId = SheetRange.GetColumnIDFromLetters(columnLetter);

return DeleteColumn(columnId);
Expand All @@ -258,6 +293,8 @@ public BatchUpdateSpreadsheetResponse DeleteColumn(string columnLetter)
/// <returns></returns>
public BatchUpdateSpreadsheetResponse DeleteRow(int row)
{
this.EnsureServiceInitialized();

var request = new Request()
{
DeleteDimension = new DeleteDimensionRequest()
Expand Down Expand Up @@ -289,6 +326,8 @@ public BatchUpdateSpreadsheetResponse DeleteRow(int row)
/// <returns></returns>
public BatchUpdateSpreadsheetResponse DeleteRows(int startRow, int endRow)
{
this.EnsureServiceInitialized();

var request = new Request()
{
DeleteDimension = new DeleteDimensionRequest()
Expand Down Expand Up @@ -319,6 +358,8 @@ public BatchUpdateSpreadsheetResponse DeleteRows(int startRow, int endRow)
/// <returns></returns>
public BatchUpdateSpreadsheetResponse InsertBlankColumn(int column)
{
this.EnsureServiceInitialized();

if (column < 1)
{
throw new ArgumentException("column index value must be 1 or greater");
Expand Down Expand Up @@ -355,6 +396,8 @@ public BatchUpdateSpreadsheetResponse InsertBlankColumn(int column)
/// <returns></returns>
public BatchUpdateSpreadsheetResponse InsertBlankColumn(string columnLetter)
{
this.EnsureServiceInitialized();

var columnId = SheetRange.GetColumnIDFromLetters(columnLetter);

return InsertBlankColumn(columnId);
Expand All @@ -367,6 +410,8 @@ public BatchUpdateSpreadsheetResponse InsertBlankColumn(string columnLetter)
/// <returns></returns>
public BatchUpdateSpreadsheetResponse InsertBlankRow(int row)
{
this.EnsureServiceInitialized();

if (row < 1)
{
throw new ArgumentException("row index value must be 1 or greater");
Expand Down Expand Up @@ -409,6 +454,8 @@ public BatchUpdateSpreadsheetResponse InsertBlankRow(int row)
/// <returns></returns>
public BatchUpdateSpreadsheetResponse BatchUpdate(List<BatchUpdateRequestObject> updates, string fieldMask = "userEnteredValue")
{
this.EnsureServiceInitialized();

var bussr = new BatchUpdateSpreadsheetRequest();

var requests = new List<Request>(updates.Count);
Expand Down Expand Up @@ -461,6 +508,8 @@ public BatchUpdateSpreadsheetResponse BatchUpdate(List<BatchUpdateRequestObject>
/// <param name="newTabName"></param>
private BatchUpdateSpreadsheetResponse CreateNewTab(string newTabName)
{
this.EnsureServiceInitialized();

var batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest
{
Requests = new List<Request>
Expand Down Expand Up @@ -507,6 +556,8 @@ public SheetHelper(string spreadsheetID, string serviceAccountEmail, string tabN
/// <returns></returns>
public BatchUpdateSpreadsheetResponse AppendRow(T record)
{
this.EnsureServiceInitialized();

return AppendRows(new T[] { record });
}

Expand All @@ -517,6 +568,8 @@ public BatchUpdateSpreadsheetResponse AppendRow(T record)
/// <returns></returns>
public BatchUpdateSpreadsheetResponse AppendRows(IList<T> records)
{
this.EnsureServiceInitialized();

var rows = new List<RowData>(records.Count);

foreach (var record in records)
Expand Down

0 comments on commit 20f3593

Please sign in to comment.