繁体   English   中英

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

[英]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 请求成功

按照文档成功使用 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 请求 (文档过时后,有没有.FormatIWorkbookWorksheetRangeRequestBuilder 。这个错误似乎证明在GitHub上。有没有使用图形SDK发送arbritary http请求一个简单的方法?
  2. 更重要的是:为此,我需要将单元格偏移量转换为范围地址 是否有实用程序可以做到这一点? 在我的示例中,我手动将范围内的偏移量5,34转换为地址F35

规格

套餐:

  • Microsoft.Graphv3.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变量是以下任一类型: WorkbookRangeFillWorkbookRangeFont (根据需要)我假设您会对WorkbookRangeFill感兴趣(更改范围/单元格中的颜色)

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

resource变量是format/fillWorkbookRangeFillformat/fontWorkbookRangeFont

当然{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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM