[英]MS Graph API/Workbooks (Excel API): Updating a cell format in a UseRange
我目前正在创建一个 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 请求手动更新单元格。
工作要求:
PATCH https://graph.microsoft.com/v1.0/me/drive/items/<file id>/workbook/worksheets/{sheetId}/range(address='F35:F35')/format/fill
{"color": "red"}
Microsoft.Graph
api 从 C#生成此 HTTP 请求。 (文档过时后,有没有.Format
上IWorkbookWorksheetRangeRequestBuilder
。这个错误似乎证明在GitHub上。有没有使用图形SDK发送arbritary http请求一个简单的方法?5,34
转换为地址F35
。套餐:
Microsoft.Graph
: v3.15.0
(最新发布版本)Microsoft.Identity.Client
: (MSAL) v4.15.0
(不是最新版本,但这里应该不是问题)我可以确认,在IWorkbookWorksheetRangeRequestBuilder
上没有.Format
这个问题也让我感到沮丧 ;) 我已经在 GitHub 上报告了这个问题:
https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/233
对我来说,解决方法是这样做(我是基于这个 SOF: REST call to Microsoft Graph
var requestUrl = $@"https://graph.microsoft.com/v1.0/users/{user id}/drive/items/{Consts.DriveId}/workbook/worksheets/{Consts.SheetId}/range(address='{range}')/{resource}";
string workbookRangeFill = GraphServiceClient.HttpProvider.Serializer.SerializeObject(workbookRange);
// Create the request message and add the content.
HttpRequestMessage hrm = new HttpRequestMessage(new HttpMethod(httpMethod), requestUrl);
hrm.Content = new StringContent(workbookRangeFill, System.Text.Encoding.UTF8, "application/json");
// Authenticate (add access token) our HttpRequestMessage
await GraphServiceClient.AuthenticationProvider.AuthenticateRequestAsync(hrm);
// Send the request and get the response.
HttpResponseMessage response = await GraphServiceClient.HttpProvider.SendAsync(hrm);
而workbookRange
变量是以下任一类型: WorkbookRangeFill
或WorkbookRangeFont
(根据需要)我假设您会对WorkbookRangeFill
感兴趣(更改范围/单元格中的颜色)
range
变量是电子表格中的范围,格式为:“A1:B3”
resource
变量是format/fill
为WorkbookRangeFill
和format/font
为WorkbookRangeFont
当然{user id}
是拥有该文档的用户(我正在使用具有application permisions
权限的client credentials flow with
对于其他场景,我假设您可以只更改上面代码中的一件事。这样,而不是:
https://graph.microsoft.com/v1.0/users/{user id}/drive
使用
https://graph.microsoft.com/v1.0/me/drive
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.