-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
58 lines (52 loc) · 1.68 KB
/
Program.cs
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
using System;
using System.IO;
using Google.Apis.Services;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
namespace GoogleSheetsAndCsharp
{
class Program
{
static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets };
static readonly string ApplicationName = "Aplication Name";
static readonly string SpreadsheetId = "spreadsheet ID";
static readonly string sheet = "spreadsheet name";
static SheetsService service;
static void Main(string[] args)
{
GoogleCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(Scopes);
}
// Create Google Sheets API service.
service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
ReadEntries();
}
static void ReadEntries()
{
var range = $"{sheet}!A:N";
SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(SpreadsheetId, range);
var response = request.Execute();
var values = response.Values;
if (values != null && values.Count > 0)
{
foreach (var row in values)
{
// Print columns A to N, which correspond to indices 0 and 4.
Console.WriteLine("{0} | {1} | {2} | {3} | {4} | {5}", row[0], row[1], row[2], row[3], row[4], row[5]);
}
}
else
{
Console.WriteLine("No data found.");
}
}
}
}