forked from SteveWinward/GoogleSheetsWrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
92 lines (77 loc) · 3.31 KB
/
Program.cs
File metadata and controls
92 lines (77 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using static Google.Apis.Sheets.v4.SpreadsheetsResource.ValuesResource.GetRequest;
namespace GoogleSheetsWrapper.SampleClient
{
class Program
{
static void Main(string[] args)
{
var config = BuildConfig();
// Get the Google Spreadsheet Config Values
var serviceAccount = config["GOOGLE_SERVICE_ACCOUNT"];
var documentId = config["GOOGLE_SPREADSHEET_ID"];
var jsonCredsPath = config["GOOGLE_JSON_CREDS_PATH"];
// In this case the json creds file is stored locally, but you can store this however you want to (Azure Key Vault, HSM, etc)
var jsonCredsContent = File.ReadAllText(jsonCredsPath);
// Create a new SheetHelper class
var sheetHelper = new SheetHelper(documentId, serviceAccount, "");
sheetHelper.Init(jsonCredsContent);
// Get all the rows for the first 2 columns in the spreadsheet
var rows = sheetHelper.GetRows(new SheetRange("", 1, 1, 2),
ValueRenderOptionEnum.FORMATTEDVALUE,
DateTimeRenderOptionEnum.FORMATTEDSTRING);
// Write all the values from the result set
foreach (var row in rows)
{
foreach (var col in row)
{
Console.Write($"{col}\t");
}
Console.Write("\n");
}
var appender = new SheetAppender(sheetHelper);
// Appends weakly typed rows to the spreadsheeet
appender.AppendRows(new List<List<string>>()
{
new List<string>(){"7/1/2022", "abc"},
new List<string>(){"8/1/2022", "def"}
});
// Get all the rows for the first 2 columns in the spreadsheet
var rows2 = sheetHelper.GetRows(new SheetRange("", 1, 1, 2),
ValueRenderOptionEnum.FORMATTEDVALUE,
DateTimeRenderOptionEnum.FORMATTEDSTRING);
// Write all the values from the result set
foreach (var row in rows2)
{
foreach (var col in row)
{
Console.Write($"{col}\t");
}
Console.Write("\n");
}
}
/// <summary>
/// Really simple method to build the config locally. This requires you to setup User Secrets locally with Visual Studio
/// </summary>
/// <returns></returns>
private static IConfigurationRoot BuildConfig()
{
var devEnvironmentVariable = Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT");
var isDevelopment = string.IsNullOrEmpty(devEnvironmentVariable) ||
devEnvironmentVariable.ToLower() == "development";
var builder = new ConfigurationBuilder();
// tell the builder to look for the appsettings.json file
builder
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
//only add secrets in development
if (isDevelopment)
{
builder.AddUserSecrets<Program>();
}
return builder.Build();
}
}
}