What is the best way to get one value from Column & row?

Viewed 52

What would be the best way to get this "TEST"? Unsure on how to get just one value from one column and one row. Any Suggestions would be great. Please note that I do have more values than just "TEST", so a way each one on its own.1

This is my code, just the standard from https://developers.google.com/sheets/api/quickstart/dotnet 2

UserCredential credential;
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
    string credPath = "token.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 spreadsheetId = "-------------------------------------------------";
String range = "Sheet1!A:E";
SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(spreadsheetId, range);

ValueRange response = request.Execute();
IList<IList<Object>> values = response.Values;
1 Answers

Both Excel and Google Sheets deal with cell ranges, not individual cells. To retrieve a single cell you need to provide its address, eg Sheet1!A1. The result will be a ValueRange whose Values contains a single row with a single column.

The code should look something like this:

var range="Sheet1!A1";
var request = service.Spreadsheets.Values.Get(spreadsheetId, range);

ValueRange response = request.Execute();
var value = response.Values[0][0];

Writing also works with ranges. The Writing to a single range paragraph in Reading and Writing Cell Values shows how this is done in many languages except C#. It's relatively easy to translate the examples though:

var values=new object[][]
{
    new object[]{someText}
};
ValueRange body=new ValueRanges
                { 
                    Values=values
                };
var request=service.Spreadsheets.Values.Update(spreadsheetId, range,values);
UpdateValuesResponse result=request.Execute();
Related