简体   繁体   中英

How i ready null cell in Google Spreadsheet Using C#?

I have a table where your data is changed every week, and in some cells it is null, occasionally it is bringing me an error because apparently it can only read values that are not null, I would like to know how I can read it anyway, even if it doesn't bring me any data.

例如,空单元格是 E1

i want to read the E2 null cell for example.

what i have did so far in my project

 {
        UserCredential credential;

        using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
        {
            string creadPath = "token.json";
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(creadPath, true)).Result;
            label1.Text = "Conected with sheet";


        }
        var service = new SheetsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName
        });
        {
            var LimiteOP = $"{sheet}!B3:E3";
            SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(SpreadSheetId, LimiteOP);
            var response = request.Execute();

            IList<IList<Object>> values = response.Values;

            if (values.Count > 0)
            {
                foreach (var row in values)
                {

                    Console.WriteLine(row[0]+" "+ row[1]+" " + row[2]);
                }
             }
              else
             {
                Console.WriteLine("Nothing");
             }

Update (from comments): "the consolewrite give the exception"

EDIT

to solve the problem that was the way as a simple process, transform the object to a string and then use a Split to remove the null spaces

  String[] str = row[0].ToString().Split(null);
  Console.WriteLine(str[0]);

This is what I did in my solution:

  row.ElementAtOrDefault(1)?.ToString() ?? ""

The "" at the end is the default value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM