-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a basic interface with Google Sheets API
A demo of reading and writing from a private Google Sheet. Nice credential storage has not been set up nicely yet, credentials and access to the private Google Sheet can be provided if you DM me on Slack.
- Loading branch information
1 parent
febcc9c
commit 995b29b
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Google.Apis.Sheets.v4" Version="1.49.0.2111" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="credentials.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
<None Update="example_credentials.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Google.Apis.Auth.OAuth2; | ||
using Google.Apis.Services; | ||
using Google.Apis.Sheets.v4; | ||
using Google.Apis.Sheets.v4.Data; | ||
using static Google.Apis.Sheets.v4.SpreadsheetsResource.ValuesResource; | ||
|
||
namespace DiscordBot.DataAccess | ||
{ | ||
public class EventsSheetsService | ||
{ | ||
private static readonly string[] scopes = { SheetsService.Scope.Spreadsheets }; | ||
private static readonly string applicationName = "Softwire Discord Bot"; | ||
private static readonly string spreadsheetId = ""; | ||
|
||
private SheetsService sheetsService; | ||
|
||
private static async Task<ServiceAccountCredential> GetCredentialsAsync(string path = "credentials.json") | ||
{ | ||
await using var stream = | ||
new FileStream(path, FileMode.Open, FileAccess.Read); | ||
|
||
var googleCredential = GoogleCredential.FromStreamAsync(stream, CancellationToken.None); | ||
|
||
return (await googleCredential) | ||
.CreateScoped(scopes) | ||
.UnderlyingCredential as ServiceAccountCredential; | ||
} | ||
|
||
public EventsSheetsService() { } | ||
|
||
public async Task StartService() | ||
{ | ||
var credential = await GetCredentialsAsync(); | ||
|
||
// Create Google Sheets API service. | ||
sheetsService = new SheetsService(new BaseClientService.Initializer | ||
{ | ||
HttpClientInitializer = credential, | ||
ApplicationName = applicationName | ||
}); | ||
} | ||
|
||
public async Task ReadColumns() | ||
{ | ||
var range = "A2:B27"; | ||
var request = | ||
sheetsService.Spreadsheets.Values.Get(spreadsheetId, range); | ||
|
||
var response = await request.ExecuteAsync(); | ||
var values = response.Values; | ||
if (values != null && values.Count > 0) | ||
{ | ||
Console.WriteLine("Alpha, Numeric"); | ||
foreach (var row in values) | ||
Console.WriteLine($"{row[0]}, {row[1]}"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("No data found."); | ||
} | ||
} | ||
|
||
public async Task WriteRow() | ||
{ | ||
var valueRange = new ValueRange | ||
{ | ||
Values = new List<IList<object>> | ||
{ | ||
new List<object> | ||
{ | ||
"Test write:", | ||
1 | ||
} | ||
}, | ||
Range = "A30:B30" | ||
}; | ||
var request = sheetsService.Spreadsheets.Values.Update(valueRange, spreadsheetId, "A30:B30"); | ||
request.ValueInputOption = UpdateRequest.ValueInputOptionEnum.RAW; | ||
|
||
var response = await request.ExecuteAsync(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace DiscordBot.DataAccess | ||
{ | ||
class Program | ||
{ | ||
private static async Task Main(string[] args) | ||
{ | ||
var service = new EventsSheetsService(); | ||
await service.StartService(); | ||
|
||
await service.ReadColumns(); | ||
|
||
await service.WriteRow(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters