Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel WorkbookRange always empty using Microsoft Graph SDK v5

I'm struggling with reading data out of an Excel Worksheet after upgrading to Microsoft Graph .NET SDK to v5.

This code is working with v4 of the SDK:

WorkbookRange range = await client.Me
    .Drive.Items[bookId]
    .Workbook.Worksheets[tabId]
    .Range("A1:XX1")
    .Request()
    .GetAsync(cancellation)
    .ConfigureAwait(false);

var data = range.Text.Deserialize<List<List<string>>>()!;

After moving to v5, I am able to get the range, but Text is always empty:

WorkbookRange? used = await client
    .Drives["Me"].Items[bookId]
    .Workbook.Worksheets[tabId]
    .UsedRangeWithValuesOnly(true)
    .GetAsync(cancellationToken: cancellation)
    .ConfigureAwait(false);

WorkbookRange? range = await client
    .Drives["Me"].Items[bookId]
    .Workbook.Worksheets[tabId]
    .RangeWithAddress("A1:XX1")
    .GetAsync(cancellationToken: cancellation)
    .ConfigureAwait(false);

How do I get data from these ranges?

like image 701
Stephan Hoffmann Avatar asked Sep 16 '26 12:09

Stephan Hoffmann


1 Answers

It should be fixed in the latest version 5.56 of SDK

WorkbookRange? range = await client
    .Drives["Me"].Items[bookId]
    .Workbook.Worksheets[tabId]
    .RangeWithAddress("A1:XX1")
    .GetAsync(cancellationToken: cancellation)
    .ConfigureAwait(false);

WorkbookRange contains property Text. To read all values, you need to cast Text to UntypedArray, because it contains a list of rows. Each row has also type UntypedArray (it contains a list of columns)

if (range.Text is UntypedArray array)
{
    // access items (rows) in array
    foreach(var row in array.GetValue())
    {
        if (row is UntypedArray value)
        {
            foreach (var column in value.GetValue())
            {
                // access cell value
                var cell = column as UntypedString;
                Console.WriteLine(cell.GetValue());
            }
        }
    }
}
like image 51
user2250152 Avatar answered Sep 18 '26 01:09

user2250152



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!