Google apps script 在Google应用程序脚本中使用高级工作表服务清除条件格式
我正在尝试使用以下代码删除范围的条件格式化:Google apps script 在Google应用程序脚本中使用高级工作表服务清除条件格式,google-apps-script,google-sheets,google-sheets-api,Google Apps Script,Google Sheets,Google Sheets Api,我正在尝试使用以下代码删除范围的条件格式化: var sheet = range.getSheet(); var address = range.getA1Notation(); var conditionalFormatRules = sheet.getConditionalFormatRules(); var conditionalFormatRule = []; var sheetId = sheet.getSheetId(); for (let index = 0; index <
var sheet = range.getSheet();
var address = range.getA1Notation();
var conditionalFormatRules = sheet.getConditionalFormatRules();
var conditionalFormatRule = [];
var sheetId = sheet.getSheetId();
for (let index = 0; index < conditionalFormatRules.length; index++) {
let ranges = conditionalFormatRules[index].getRanges();
for (let j = 0; j < ranges.length; j++) {
if (ranges[j].getA1Notation() == address) {
conditionalFormatRule.push({
"deleteConditionalFormatRule": {
"index": index,
"sheetId": sheetId
}
});
}
}
}
if (conditionalFormatRule.length > 0) {
var spreadsheet = SpreadsheetApp.getActive();
var ssId = spreadsheet.getId();
var format_req = {
"requests": conditionalFormatRule
};
Sheets.Spreadsheets.batchUpdate(format_req, ssId);
}
它说索引8没有条件格式,但该特定范围有11个条件格式规则(通过记录条件格式规则确认)
我想删除特定范围的所有条件格式规则,如果有更好的方法,请建议
提前感谢。使用、和类的方法解决了问题
现有规则
李>
newRules
和existingRules
李>
allRules
设置到工作表var existingRules=sheet.getConditionalFormatures();
var removedRules=[];
for(让index=0;index=0;i--)
现有规则拼接(removedRules[i],1);
var newRules=[]//跳过创建新规则的逻辑
var allRules=existingRules.concat(newRules);
//首先清除所有规则,然后再次添加
sheet.ClearConditionalFormatures();
表.设置条件格式(所有规则);
如果要在一个范围内更新/添加新规则,同时在不同范围内保留工作表中的旧规则,可以通过更改条件并跳过代码中的拼接来实现,如下所示:
var existingRules = sheet.getConditionalFormatRules();
var rulesToKeep = [];
var rangeToUpdate ="A1:B10"
for (let index = 0; index < existingRules.length; index++) {
let ranges = conditionalFormatRules[index].getRanges();
for (let j = 0; j < ranges.length; j++) {
if (ranges[j].getA1Notation() != rangeToUpdate.getA1Notation()) {
rulesToKeep.push(existingRules[index]);
}
}
}
var newRules = [] //skipping the logic to create new rules
var allRules = rulesToKeep.concat(newRules);
//clear all rules first and then add again
sheet.clearConditionalFormatRules();
sheet.setConditionalFormatRules(allRules);
var existingRules=sheet.getConditionalFormatures();
var-rulestokep=[];
var rangeToUpdate=“A1:B10”
for(让index=0;index
非常重要的是为两个范围获取
getA1Notation()。Goole脚本仅支持clearConditionalFormatRules()删除一张工作表中的所有条件格式。因此,要实现目标,只需执行以下步骤:1。删除工作表中的所有条件格式。2.添加要保留的条件规则。不要试图解析getConditionalFormatRules()的数组,其中没有有用的信息。它看起来像[com.google.apps.maestro.server.beans.trix.impl]。ConditionalFormatRuleApiAdapter@1c15adca].
var existingRules = sheet.getConditionalFormatRules();
var removedRules = [];
for (let index = 0; index < existingRules.length; index++) {
let ranges = conditionalFormatRules[index].getRanges();
for (let j = 0; j < ranges.length; j++) {
if (ranges[j].getA1Notation() == address) {
removedRules.push(existingRules[index]);
}
}
}
for (var i = removedRules.length - 1; i >= 0; i--)
existingRules.splice(removedRules[i], 1);
var newRules = [] //skipping the logic to create new rules
var allRules = existingRules.concat(newRules);
//clear all rules first and then add again
sheet.clearConditionalFormatRules();
sheet.setConditionalFormatRules(allRules);
var existingRules = sheet.getConditionalFormatRules();
var rulesToKeep = [];
var rangeToUpdate ="A1:B10"
for (let index = 0; index < existingRules.length; index++) {
let ranges = conditionalFormatRules[index].getRanges();
for (let j = 0; j < ranges.length; j++) {
if (ranges[j].getA1Notation() != rangeToUpdate.getA1Notation()) {
rulesToKeep.push(existingRules[index]);
}
}
}
var newRules = [] //skipping the logic to create new rules
var allRules = rulesToKeep.concat(newRules);
//clear all rules first and then add again
sheet.clearConditionalFormatRules();
sheet.setConditionalFormatRules(allRules);