Table of Contents

Class GoogleSheetsApiService

Namespace
SunamoGoogleSheets
Assembly
SunamoGoogleSheets.dll

Wrapper around Google Sheets v4 + Drive v3 API. Initialize once, then create / update / list spreadsheets. OAuth token is cached per app name in the user's ApplicationData folder.

public sealed class GoogleSheetsApiService
Inheritance
GoogleSheetsApiService
Inherited Members
Extension Methods

Constructors

GoogleSheetsApiService(ILogger)

Wrapper around Google Sheets v4 + Drive v3 API. Initialize once, then create / update / list spreadsheets. OAuth token is cached per app name in the user's ApplicationData folder.

public GoogleSheetsApiService(ILogger logger)

Parameters

logger ILogger

Methods

CreateSpreadsheetAsync(string, IList<IList<object>>)

Creates a new spreadsheet with the given title and writes the values starting at A1. Returns the spreadsheet's web URL, or null on failure.

public Task<string?> CreateSpreadsheetAsync(string title, IList<IList<object>> values)

Parameters

title string

Spreadsheet title.

values IList<IList<object>>

Rows of cell values; the first row is typically headers.

Returns

Task<string>

CreateSpreadsheetInFolderAsync(string, string, IList<IList<object>>)

Creates a spreadsheet inside the given Drive folder, writes values, and returns its ID + URL. Returns (null, null) on failure.

public Task<(string? Id, string? Url)> CreateSpreadsheetInFolderAsync(string folderId, string title, IList<IList<object>> values)

Parameters

folderId string
title string
values IList<IList<object>>

Returns

Task<(string Id, string Url)>

DeleteFileAsync(string)

Deletes a Drive file (spreadsheet) by id. Returns true on 200/204 or 404 (already gone).

public Task<bool> DeleteFileAsync(string fileId)

Parameters

fileId string

Returns

Task<bool>

InitializeAsync(string, string, bool)

Initializes the service using an OAuth desktop client_secret JSON. Opens the system browser on first run for user consent; subsequent runs reuse the cached token.

public Task<bool> InitializeAsync(string clientSecretsPath, string applicationName, bool forceRefresh = false)

Parameters

clientSecretsPath string

Path to the client_secret*.json file (installed/desktop type).

applicationName string

Name shown in Google API logs and used for the local token cache folder.

forceRefresh bool

If true, deletes the cached token and forces re-authorization.

Returns

Task<bool>

ListSpreadsheetsInFolderAsync(string)

Lists Google Sheets files in the given Drive folder, ordered by modifiedTime desc. Returns null on failure.

public Task<List<DriveSheetInfo>?> ListSpreadsheetsInFolderAsync(string folderId)

Parameters

folderId string

Returns

Task<List<DriveSheetInfo>>

ReadValuesAsync(string, string)

Reads cell values from an existing spreadsheet within the given A1-style range. Returns null on failure (caller should treat as "no data available").

public Task<IList<IList<object>>?> ReadValuesAsync(string spreadsheetId, string range)

Parameters

spreadsheetId string
range string

Returns

Task<IList<IList<object>>>

WriteValuesToExistingAsync(string, IList<IList<object>>, string)

Clears existing cells in the given range and writes the new values starting at A1 of the same range. Returns true on success.

public Task<bool> WriteValuesToExistingAsync(string spreadsheetId, IList<IList<object>> values, string range = "Sheet1!A1:Z1000")

Parameters

spreadsheetId string
values IList<IList<object>>
range string

Returns

Task<bool>