Google apps script Google Sheets脚本条件格式,用于WhenNumberlesthanorequalto(字符串)
在GoogleSheets中,我添加了一些与各种字母代码相关的条件格式。此处,A-D为红色,E-F为黄色,G和更高版本为绿色: 这就是我所期望和想要的 在尝试通过应用程序脚本执行相同操作时,这似乎不受支持,因为有比UI更严格的类型检查:Google apps script Google Sheets脚本条件格式,用于WhenNumberlesthanorequalto(字符串),google-apps-script,google-sheets,gs-conditional-formatting,Google Apps Script,Google Sheets,Gs Conditional Formatting,在GoogleSheets中,我添加了一些与各种字母代码相关的条件格式。此处,A-D为红色,E-F为黄色,G和更高版本为绿色: 这就是我所期望和想要的 在尝试通过应用程序脚本执行相同操作时,这似乎不受支持,因为有比UI更严格的类型检查: SpreadsheetApp.newConditionalFormatRule() .whenNumberLessThanOrEqualTo('D') .setBackground('#ff0000') .setRanges([ra
SpreadsheetApp.newConditionalFormatRule()
.whenNumberLessThanOrEqualTo('D')
.setBackground('#ff0000')
.setRanges([range])
.build());
这将显示错误消息:
Cannot find method whenNumberLessThanOrEqualTo(string)
因此我无法创建规则对象来将其应用于工作表
有没有办法通过应用程序脚本在UI中创建相同的规则?还是其他方法?最好的方法似乎只是在JS中重新实现这一点,并在字母值的完整枚举上使用
TEXT_EQUAL_to
,或者类似于CUSTOM_公式
()。不幸的是,内置服务方法whenNumberLessThanOrEqualTo()
希望将数值作为参数而不是字符串
但是,您可以利用。AdvancedSheets服务基本上是一个包装器。它更复杂,并且要求您了解构成API模式的许多REST资源对象(请参阅)
此外,您需要在GAS项目中启用该表的API高级服务,然后才能使用它()
一旦这样做,您就可以利用该服务添加格式规则
下面的脚本是如何执行此操作的示例:
function buildRule() {
var conditionValue = Sheets.newConditionValue();
var booleanCondition = Sheets.newBooleanCondition();
var color = Sheets.newColor();
var cellFormat = Sheets.newCellFormat();
var booleanRule = Sheets.newBooleanRule();
var gridRange = Sheets.newGridRange();
var formatRule = Sheets.newConditionalFormatRule();
var addConditionalFormatRuleRequest = Sheets.newAddConditionalFormatRuleRequest();
var request = Sheets.newRequest();
var batchRequest = Sheets.newBatchUpdateSpreadsheetRequest();
conditionValue.userEnteredValue = "D";
booleanCondition.type = "NUMBER_GREATER_THAN_EQ";
booleanCondition.values = [conditionValue];
// #ff0000 in RGB format
color.red = 1; // values range from 0 to 1
color.blue = 0;
color.green = 0;
cellFormat.backgroundColor = color;
booleanRule.condition = booleanCondition;
booleanRule.format = cellFormat;
// selects E2 as range
gridRange.sheetId = 0;
gridRange.startColumnIndex = 4;
gridRange.endColumnIndex = 5;
gridRange.startRowIndex = 1;
gridRange.endRowIndex = 2;
formatRule.booleanRule = booleanRule;
formatRule.ranges = [gridRange];
addConditionalFormatRuleRequest.rule = formatRule;
addConditionalFormatRuleRequest.index = 0; // index of rule; increment to add other rules
request.addConditionalFormatRule = addConditionalFormatRuleRequest;
batchRequest.requests = [request];
Sheets.Spreadsheets.batchUpdate(batchRequest, SpreadsheetApp.getActive().getId());
}
上面的脚本非常详细,因此,一旦您了解了Google Sheets API模式的资源类型,以下内容也就足够了:
function buildRule() {
var batchRequest = {
"requests":[
{
"addConditionalFormatRule":{
"rule":{
"booleanRule":{
"condition":{
"type":"NUMBER_GREATER_THAN_EQ",
"values":[
{
"userEnteredValue":"D"
}
]
},
"format":{
"backgroundColor":{
"red":1,
"blue":0,
"green":0
}
}
},
"ranges":[
{
"sheetId":0,
"startColumnIndex":4,
"endColumnIndex":5,
"startRowIndex":1,
"endRowIndex":2
}
]
},
"index":0
}
}
]
};
Sheets.Spreadsheets.batchUpdate(batchRequest, SpreadsheetApp.getActive().getId());
}