MS Graph API/Workbooks (Excel API):更新 UseRange 中的单元格格式

MS Graph API/Workbooks (Excel API): Updating a cell format in a UseRange

我擅长什么

我目前正在创建一个 dotnet 核心应用程序来使用和处理来自存储在个人 OneDrive 中的 Excel sheet 的数据。我正在使用 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"}

问题

  1. 我不知道如何使用 Microsoft.Graph api 从 C# 生成此 HTTP 请求。 (文档已过时,IWorkbookWorksheetRangeRequestBuilder上没有.Format。这个错误似乎是documented on GitHub。有没有简单的方法可以使用graph SDK发送任意http请求?
  2. 更重要的是:为了使其工作,我需要将单元格偏移量转换为范围地址。某处有实用程序可以做到这一点吗?在我的示例中,我手动将范围内的偏移量 5,34 翻译为地址 F35.

规格

包:

我可以确认,这个在 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 变量属于以下任一类型:WorkbookRangeFillWorkbookRangeFont(取决于需要)我假设您会对 WorkbookRangeFill 感兴趣(更改range/cell)

中的颜色

range 变量在电子表格中的范围格式为:“A1:B3”

resource 变量是 format/fill 对于 WorkbookRangeFillformat/font 对于 WorkbookRangeFont

当然 {user id} 是拥有该文档的用户(我正在使用 client credentials flow withapplication permisions 对于其他情况,我假设您可以在上面的代码中更改一件事. 这样,而不是:

https://graph.microsoft.com/v1.0/users/{user id}/drive

使用

https://graph.microsoft.com/v1.0/me/drive