I can get and update values in Google Sheets using the following code:
private void btnUpdate_Click(object sender, EventArgs e)
{
if (cbYards.Text == "Select Yard")
{
MessageBox.Show(@"Please select a yard.");
return;
}
UserCredential credential;
using (var stream =
new FileStream("client_secret.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);
}
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
IList<IList<Object>> list = new List<IList<Object>>() { };
for (var i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
var formula = "=IFERROR(VLOOKUP(B"+(i+2)+",Names!$A$2:$B,2,FALSE),\"No Record\")";
List<object> lists = new List<object>() { formula, dataGridView1.Rows[i].Cells[0].Value.ToString(),
dataGridView1.Rows[i].Cells[1].Value.ToString() };
list.Add(lists);
}
var range = cbYards.Text+"!A2:C";
ValueRange VRange = new ValueRange();
VRange.Range = range;
VRange.Values = list;
ValueRange valueRange = new ValueRange();
valueRange.MajorDimension = "COLUMNS";
SpreadsheetsResource.ValuesResource.UpdateRequest upd = service.Spreadsheets.Values.Update(VRange, spreadsheetId, range);
upd.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
UpdateValuesResponse response = upd.Execute();
}
My question is how to create a new sheet in the google sheet that I am using now. I thought that all I need to do is replace
SpreadsheetsResource.ValuesResource.UpdateRequest upd = service.Spreadsheets.Values.Update(VRange, spreadsheetId, range);
to
SpreadsheetsResource.ValuesResource.UpdateRequest upd = service.Spreadsheets.create();
but apparently this is wrong ...
I cannot figure out how to implement the instructions in the documentation for the Method: spreadsheets.create
What does JSon do with the way I will code in C #? Truly appreciate the help.
UPDATE
I found this here, but it is not complete since it still gives me an error
Additional information: The reference to the object is not installed in the instance of the object.
This is the updated code:
private void button1_Click(object sender, EventArgs e)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.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);
}
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
string sheetName = string.Format("{0} - {1}-{2}", cbYards.Text, fromDate.Value.ToShortDateString(), toDate.Value.ToShortDateString());
var myNewSheet = new Google.Apis.Sheets.v4.Data.Spreadsheet();
myNewSheet.Properties = new SpreadsheetProperties();
myNewSheet.Properties.Title = sheetName;
var newSheet = service.Spreadsheets.Create(myNewSheet).Execute();
}