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?
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());
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With