【发布时间】:2019-05-03 09:21:16
【问题描述】:
我有以下非常精简的电子表格版本:
Sect | Lbl | A | B | C | D | E
==========================================================
Sec1 | Lbl1 | 1 | 8 | 6 | 10 |
----------------------------------------------------------
Sec2 | Lbl2 | 2 | 1 | 1 | >100 |
----------------------------------------------------------
etc...
我想在所有值上应用一个规则/规则说:
Bg 颜色 = 绿色,如果: - 右边的单元格不是空白的并且高于这个值
Bg 颜色 = 红色,如果: - 右边的单元格不是空白的并且小于这个值
Bg 颜色 = 白色(无动作)如果: - 右边的单元格具有相同的值
此外,如果该值设置为非数字“>100”,我需要将其转换为 100 作为此格式的一部分。
我正在使用 C# 通过电子表格 v4 API 来执行此操作。 到目前为止,我有下面的代码,但我不确定如何将多个条件应用于格式化规则。
更新
在阅读下文之前,请注意上面更新的表格示例
感谢 TheMaster,我已经启动并运行了一些东西,但还不是很正确。我还有一个额外的因素:
- 第一行应从条件格式中排除
- 前两列是标签,也应该被忽略/排除
- 需要将所有其他列和数据行与其右侧的列(如果可用)进行比较并相应地着色。
到目前为止,这是我用于 Red 规则 (where cell value > cell value to the right) 的代码。
除此之外,我还有一个Green 规则(where cell value < cell value to the right)和一个White 规则(where cell value = cell value to the right)
它们在批量更新请求中被索引如下: 0 = 红色 1 = 绿色 2 = 白色
Red 规则的代码:
formatRequest.Requests.Add(new Google.Apis.Sheets.v4.Data.Request()
{
AddConditionalFormatRule = new AddConditionalFormatRuleRequest()
{
Rule = new ConditionalFormatRule()
{
BooleanRule = new BooleanRule()
{
Condition = new BooleanCondition()
{
Type = "CUSTOM_FORMULA",
Values = new List<ConditionValue>() {
new ConditionValue()
{
UserEnteredValue = "=AND(NOT(ISBLANK(A2)),(1*REGEXEXTRACT(A2,\"\\d+\"))>(1*REGEXEXTRACT(B2,\"\\d+\")))"
}
}
},
Format = new CellFormat()
{
BackgroundColor = new Color()
{
Red = 0.8f,
Green = 0f,
Blue = 0f,
Alpha = 1f
}
}
},
Ranges = new List<GridRange>()
{
new GridRange()
{
SheetId = Convert.ToInt32(sheetId)
,StartRowIndex = 1
},
}
},
Index = 0
}
});
问题是它没有将条件格式应用于整个工作表...仅第一列数据。
【问题讨论】:
-
查看gs-conditional-formatting中投票最多的问题。
-
默认为白色。您不必添加白色条件格式,除非您更改了默认值。
标签: asp.net-core google-sheets google-sheets-api google-api-dotnet-client gs-conditional-formatting