【发布时间】:2020-10-29 05:23:45
【问题描述】:
我做得很好的地方
我目前正在创建一个 dotnet 核心应用程序来使用和处理存储在个人 OneDrive 中的 Excel 工作表中的数据。我正在使用MSAL 创建会话令牌,并且数据消耗效果很好。这是我的工作代码:
// Get the range for data to process
var dataRangeRequest = myGraphServiceClient // an instance of GraphServiceClient
.Me.Drive.Items[fileItemId]
.Workbook
.Sheets[sheetId]
.UsedRange(valuesOnly: false)
.Request();
var dataRange = await dataRangeRequest.GetAsync(ct)
// Extract column names (headers) from the data range
var columnNames = dataRange.Text.First.ToObject<string[]>();
// Extract data cells from the data range
var lines = dataRange.Text.Skip(1).Select(line=>line.ToObject<string[]>).ToArray();
[...] // Here I process the lines using the columnNames.
// --EVERYTHING WORKS UNTIL HERE--
我做得不好的地方
现在,我想将原始 Excel 文档中的错误数据单元格变为红色
var faultyCell = (row: 34, column: 5); // the row/column offset of the faulty cell in dataRange
// ---------------------------------
// --FOLLOWING CODE IS NOT WORKING--
// ---------------------------------
var changeRange = new WorkbookRange
{
RowIndex = faultyCell.row,
ColumnIndex = faultyCell.column,
RowCount = 1,
ColumnCount = 1,
Format = new WorkbookRangeFormat {Fill = new WorkbookRangeFill {Color = "red"}}
};
await dataRangeRequest.PatchAsync(changeRange, ct); // Throwing a Microsoft.Graph.ServiceException
我截获了 HTTP 请求和响应,如下:
请求
PATCH https://graph.microsoft.com/v1.0/me/drive/items/<file id>/workbook/worksheets/{sheetId}/microsoft.graph.usedRange(valuesOnly=true) HTTP/1.1
{
"columnIndex": 5,
"rowIndex": 34,
"columnCount": 1,
"rowCount": 1,
"format": {
"fill": {
"color": "Red",
"@odata.type": "microsoft.graph.workbookRangeFill"
},
"@odata.type": "microsoft.graph.workbookRangeFormat"
},
"@odata.type": "microsoft.graph.workbookRange"
}
回应
400 Bad Request
{
"error": {
"code": "BadRequest",
"message": "Bad Request - Error in query syntax.",
"innerError": {
"date": "<the date>",
"request-id": "<a guid>",
"client-request-id": "<another guid>"
}
}
}
手动 HTTP 请求成功
我成功通过following the documentation 使用 HTTP 请求手动更新单元格。
工作要求:
PATCH https://graph.microsoft.com/v1.0/me/drive/items/<file id>/workbook/worksheets/{sheetId}/range(address='F35:F35')/format/fill
{"color": "red"}
问题
- 我不知道如何使用
Microsoft.Graphapi 从 C# 生成此 HTTP 请求。 (文档已过时,IWorkbookWorksheetRangeRequestBuilder上没有.Format。这个错误似乎是documented on GitHub。有没有一种简单的方法可以使用图形 SDK 发送任意 http 请求? - 更重要的是:为此,我需要将单元格偏移量转换为范围地址。有没有实用程序可以做到这一点?在我的示例中,我手动将范围内的偏移量
5,34转换为地址F35。
规格
包:
-
Microsoft.Graph:v3.15.0(最新发布版本) -
Microsoft.Identity.Client: (MSAL)v4.15.0(不是最新版本,但在这里应该没有问题)
【问题讨论】:
-
在 Graph SDK 之外,您是否尝试过使用 POSTMAN/MS Graph Explorer 测试相同的调用?有用吗?
-
另外,你知道的最后一个版本(MS Graph SDK)是什么?
标签: c# excel microsoft-graph-api onedrive microsoft-graph-sdks