【问题标题】:Conditional formatting requests in Google Sheets in .NET client.NET 客户端中 Google 表格中的条件格式请求
【发布时间】:2019-12-28 23:53:10
【问题描述】:

我知道如何在 Google Sheets API 中对值和其他格式进行批量电子表格更新请求,但条件格式似乎有所不同。我已正确设置请求:

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);

问题是,您实际上是如何执行此操作的?问题来了:

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

这部分不起作用,因为BatchUpdateSpreadSheetRequest.Requests 的类型为IList&lt;Request&gt;,但AddConditionalFormatRuleRequest 不继承自Request。它只实现了IDirectResponseActionSchema(参见source code 第2254 行),那么如何实际执行这些请求??

所以我应该使用其他一些对象/方法来执行此操作,但它在哪里?

提前致谢。

【问题讨论】:

    标签: c# google-sheets-api google-api-dotnet-client gs-conditional-formatting


    【解决方案1】:

    来自 .NET 背景,我真的很难思考如何使用 Google API。事情只是组织得很奇怪(对我来说),.NET 组件的文档看起来不像 MSDN,所以我觉得很难。无论如何,我想通了。您不单独使用 AddConditionalFormatRuleRequest 类。您必须将其包装在常规的 Request 对象中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      相关资源
      最近更新 更多