-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
164 lines (145 loc) · 5.86 KB
/
Program.cs
File metadata and controls
164 lines (145 loc) · 5.86 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.Windows.Forms;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ppData
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
static string ApplicationName = "Google Sheets API .NET Quickstart";
static SheetsService service;
[STAThread]
static void Main()
{
UserCredential credential = doAccess();
// Create Google Sheets API service.
service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
doOutput();
Console.Read();
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
}
private static void doOutput()
{
// Define request parameters.
String spreadsheetId = "1Y9YeG-P5pfwqDfozgeVYiJJyxBOK2G7F15dedowvrCU";
String range = "Subsheets!A1:AP50";
SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(spreadsheetId, range);
request.ValueRenderOption = SpreadsheetsResource.ValuesResource.GetRequest.ValueRenderOptionEnum.FORMATTEDVALUE;
request.MajorDimension = SpreadsheetsResource.ValuesResource.GetRequest.MajorDimensionEnum.COLUMNS;
string error = "none";
try
{
ValueRange response = request.Execute();
IList<IList<Object>> sites = response.Values;
foreach (var site in sites)
{
Queue<string> data = new Queue<string>(site.Cast<String>());
var siteName = data.Dequeue();
if (siteName == "Site")
continue;
var siteURL = getKey(data.Dequeue());
if (String.IsNullOrEmpty(siteURL))
Console.WriteLine("No datasheet for {0}", siteName);
else
{
pullSiteData(siteName, siteURL, data);
}
}
}
catch (Exception e)
{
error = e.ToString();
Console.WriteLine("No global data ERROR={0}", error);
};
}
private static string getKey(object urlobj)
{
string url = urlobj.ToString();
if (String.IsNullOrEmpty(url))
return "";
if ('/'.Equals(url[url.Length - 1]))
url = url.Substring(0, url.Length - 1);
int i = url.LastIndexOf('/');
return url.Substring(i + 1);
}
private static void pullSiteData(string siteName, String sheetKey, Queue<string> sheets)
{
SpreadsheetsResource.GetRequest request = service.Spreadsheets.Get(sheetKey);
request.IncludeGridData = true;
try
{
Google.Apis.Sheets.v4.Data.Spreadsheet response = request.Execute();
foreach(var sheetName in sheets)
{
try
{
Sheet sheet = response.Sheets.FirstOrDefault<Sheet>(s => s.Properties.Title == sheetName);
if (sheet == null)
{
Console.WriteLine("Couldn't find sheet {0} in spreadsheet {1} {2}", sheetName, siteName, sheetKey);
continue;
}
foreach (var row in sheet.Data.First<GridData>().RowData)
{
Console.Write("{0}\t{1}\t", siteName, sheetName);
foreach (var cell in row.Values)
{
Console.Write("{0}\t", (cell.FormattedValue ?? "").Trim());
}
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine("Error processing sheet data for {0}:{1}:{2}={3}", siteName, sheetKey, sheetName, e.Message);
};
}
}
catch (Exception e)
{
Console.WriteLine("Couldn't get sheet data for {0} on {1} = {2}", siteName, sheetKey, e.GetType());
};
}
private static UserCredential doAccess()
{
// Check/Referesh Access Token
UserCredential credential;
using (var stream =
new FileStream("client_id.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
return credential;
}
}
}