【问题标题】:MS Graph API/Workbooks (Excel API): Updating a cell format in a UseRangeMS Graph API/Workbooks (Excel API):在 UseRange 中更新单元格格式
【发布时间】: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"}

问题

  1. 我不知道如何使用Microsoft.Graph api 从 C# 生成此 HTTP 请求。 (文档已过时,IWorkbookWorksheetRangeRequestBuilder 上没有 .Format。这个错误似乎是documented on GitHub。有没有一种简单的方法可以使用图形 SDK 发送任意 http 请求?
  2. 更重要的是:为此,我需要将单元格偏移量转换为范围地址。有没有实用程序可以做到这一点?在我的示例中,我手动将范围内的偏移量 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


【解决方案1】:

我可以确认,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 变量是电子表格中的范围,格式为:“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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多