Conditional formatting requests in Google Sheets in .NET client

巧了我就是萌 提交于 2021-01-28 04:54:10

问题


I know how to do batch spreadsheet update requests for values and other formatting in the Google Sheets API, but conditional formatting seems to be different. I have the request set up properly:

AddConditionalFormatRuleRequest formatRequest = new AddConditionalFormatRuleRequest
{
    Rule = new ConditionalFormatRule
    {
        Ranges = new List<GridRange>
        {
            new GridRange
            {
                // omitted
            }
        },
        BooleanRule = new BooleanRule
        {
            Condition = new BooleanCondition
            {
                // omitted
            },
            Format = new CellFormat
            {
                // omitted
            }
        },
    },
};
formatRequests.Add(formatRequest);

The question is, how do you actually execute this? Here's the problem:

BatchUpdateSpreadSheetRequest updateRequest = new BatchUpdateSpreadsheetRequest 
{ 
    Requests = formatRequests  // this doesn't work
};
// resource is a SpreadsheetsResource object
SpreadsheetsResource.BatchUpdateRequest batchRequest = resource.BatchUpdate(updateRequest, spreadsheet.SpreadsheetId);

This part doesn't work because BatchUpdateSpreadSheetRequest.Requests is of type IList<Request>, but AddConditionalFormatRuleRequest doesn't inherit from Request. It only implements IDirectResponseActionSchema (see source code line 2254), so how does one actually execute these requests??

So there must be some other object/method that I'm supposed to use to do this, but where is it?

Thanks in advance.


回答1:


Coming from a .NET background, I really have a hard time wrapping my head around how to use the Google API. Things are just organized strangely (to me) and the documentation for the .NET components looks nothing like MSDN, so I just find it difficult. Anyway, I figured it out. You don't use the AddConditionalFormatRuleRequest class by itself. You have to wrap it in a regular Request object.

Request formatRequest = new Request
{
    AddConditionalFormatRule = new AddConditionalFormatRuleRequest
    {
        // etc
    }
};


来源:https://stackoverflow.com/questions/59515870/conditional-formatting-requests-in-google-sheets-in-net-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!