Does anyone have a good C# example for updating a cell with the v4 API?
I have the get cell values c# example from the developer website working with Google Sheets API v4. I am trying to modify the example to update a cell with a value of "Tom". I'm stuck on the settings for SpreadSheets.Values.Update
.
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 GoogleSheetsAPI4_v1console
{
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
static string[] Scopes = { SheetsService.Scope.Spreadsheet };
static string ApplicationName = "TestSpreadsheet";
static void Main(string[] args)
{
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);
}
// Create Google Sheets API service.
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define request parameters.
String spreadsheetId = "<<myspreadsheetid>>";
String range = "Sheet1!D5"; // single cell D5
String myNewCellValue = "Tom";
SpreadsheetsResource.ValuesResource.UpdateRequest request = service.Spreadsheets.Values.Update(<<what goes here?>>, spreadsheetId, range);
// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
ValueRange response = request.Execute();
IList<IList<Object>> values = response.Values;
Console.WriteLine(values);
}
}
}
First Create value range object like given below:
I think you just have access to read your sheet because I can see :
Try to change "SheetsService.Scope.SpreadsheetsReadonly" by "SheetsService.Scope.Spreadsheet" or by "SheetsService.Scope.Drive". Maybe it's another syntax... If it's not a problem of authorization I can't help you, sorry... Good luck my friend.
Ps: On Xcode (objective-c) you must reset the simulator after modification of scope. For you (C#) I don't know.
If you are using this tutorial https://developers.google.com/sheets/quickstart/dotnet Ensure you comment out the following line of code.
Also this is what I used syntax wise and got it working.
Here is a working example using a console to write a singe cell. Thank you everyone who assisted !!
The file "Google.Apis.Sheets.v4.SpreadsheetsResource.cs" contains error: url "https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}" not contains "?valueInputOption=RAW". Use this code temporarily: