Skip to content

7.2 Protected Google Sheets

Jos de Weger edited this page Feb 28, 2022 · 1 revision

Getting the data for a protected document requires the use of the ProtectedGoogleSheetAdapter. This adapter has one method for getting data: Task<Sheet> GetAsync(string authenticationJsonFilePath, string documentName, string sheetId, string range).

Parameters explained:

  • authenticationJsonFilePath: json file containing auth details, see Enable Security for the Google Sheet below
  • documentName: the name of the Google Sheets document
  • sheetId: the id of the sheet, which you can find in the url when viewing the sheet on Google Sheets
  • range: for example 'my sheet'!A1:E4

Example:

var adapter = new ProtectedGoogleSheetAdapter();
var sheetData = await adapter.GetAsync("secret.json", "My Document", "my-sheet-id", "A1:E4");
var result = sheetMapper.Map<MyModel>(sheetData);          

Enable security for the Google Sheet

The steps to create a protected sheet:

Google Console

  • Download a json file with the credentials

Secret 1

Secret 2

  • Add the json file to your project and make sure to set the "Copy to output directory" to "Copy Allways":

Copy allways

  • Add the email address from the secret.json to the Google Sheets document

Permissions 1

Permissions 2