问题
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