英文:
Extract values of conditional formating
问题
I have a spreadsheet that has literally hundreds of conditional formatting rules within it. All the rules are 'text is exactly' rules and includes postcodes such as BH17, CA24 etc.
I have recently amended the spreadsheet as per the following as the sheet was becoming slow due to the volume of rules: https://stackoverflow.com/questions/75952533/conditional-format-based-on-text-in-cells
Is there any way to extract the text from the rules based on the color of conditional formatting i.e pull all green rules and place the postcodes extracted into a column so that I can enter them into the correct columns or am I looking at the tedious task of starting from scratch as I am highly expecting?
Edit
To confirm, I am looking to see if I could extract the text from conditional rules so an example using the image below would be: if I wanted to extract the postcodes for all rules that have an orange color attached it would put these values into a column somewhere, AB10,AB11,AB12,AB13 etc
Hope that makes sense but if not feel free to ask.
英文:
I have a spreadsheet that has literally hundreds of conditional formatting rules within it. All the rules are 'text is exactly' rules and includes postcodes such as BH17, CA24 etc.
I have recently amended the spreadsheet as per the following as the sheet was becoming slow due to the volume of rules: https://stackoverflow.com/questions/75952533/conditional-format-based-on-text-in-cells
Is there any way to extract the text from the rules based on the color of conditional formatting i.e pull all green rules and place the postcodes extracted into a column so that I can enter them into the correct columns or am I looking at the tedious task of starting from scratch as I am highly expecting?
Edit
To confirm, I am looking to see if I could extract the text from conditional rules so an example using the image below would be: if I wanted to extract the postcodes for all rules that have an orange color attached it would put these values into a column somewhere, AB10,AB11,AB12,AB13 etc
Hope that makes sense but if not feel free to ask.
答案1
得分: 2
以下是您要翻译的内容:
我相信您的目标如下。
- 您希望从条件格式规则中检索值,并将这些值放入电子表格的一列中。
在这种情况下,以下示例脚本如何?
模式1:
在此模式中,从活动电子表格中的所有工作表中检索值。
function sample1() {
const color = "#FF9900"; // 请设置您设置的背景颜色。此示例为橙色。
const dstSheetName = "Sheet1"; // 请设置目标工作表名称。
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
const values = sheets.reduce((ar, s) => {
s.getConditionalFormatRules().forEach(c => {
const t = c.getBooleanCondition();
if (t.getCriteriaType() == SpreadsheetApp.BooleanCriteria.TEXT_EQUAL_TO && t.getBackground().toUpperCase() == color) {
ar.push(c.getBooleanCondition().getCriteriaValues()); // 或 ar.push([c.getBooleanCondition().getCriteriaValues().join(",")]);
}
});
return ar;
}, []);
const dstSheet = ss.getSheetByName(dstSheetName);
dstSheet.getRange(1, 1, values.length).setValues(values);
}
- 运行此脚本时,将从所有工作表的条件格式规则中检索值。然后,将这些值放入目标工作表的“A”列中。
模式2:
在此模式中,从活动电子表格中的单个工作表中检索值。
function sample2() {
const color = "#FF9900"; // 请设置您设置的背景颜色。此示例为橙色。
const srcSheetName = "Sheet1"; // 请设置源工作表名称。
const dstSheetName = "Sheet1"; // 请设置目标工作表名称。
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(srcSheetName);
const values = sheet.getConditionalFormatRules().reduce((ar, c) => {
const t = c.getBooleanCondition();
if (t.getCriteriaType() == SpreadsheetApp.BooleanCriteria.TEXT_EQUAL_TO && t.getBackground().toUpperCase() == color) {
ar.push(c.getBooleanCondition().getCriteriaValues()); // 或 ar.push([c.getBooleanCondition().getCriteriaValues().join(",")]);
}
return ar;
}, []);
const dstSheet = ss.getSheetByName(dstSheetName);
dstSheet.getRange(1, 1, values.length).setValues(values);
}
- 运行此脚本时,将从“Sheet1”工作表的条件格式规则中检索值。然后,将这些值放入目标工作表的“A”列中。
注意:
- 在此示例脚本中,使用了来自您问题中的“所有具有橙色”的十六进制颜色代码
#FF9900
。如果此颜色代码与您的实际情况不同,请进行修改。
参考文献:
英文:
I believe your goal is as follows.
- You want to retrieve the values from the conditional formatting rules and want to put the values in a column of the Spreadsheet.
In this case, how about the following sample script?
Pattern 1:
In this pattern, the values are retrieved from all sheets in the active Spreadsheet.
function sample1() {
const color = "#FF9900"; // Please set the background color you set. This sample is Orange in color.
const dstSheetName = "Sheet1"; // Please set destination sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
const values = sheets.reduce((ar, s) => {
s.getConditionalFormatRules().forEach(c => {
const t = c.getBooleanCondition();
if (t.getCriteriaType() == SpreadsheetApp.BooleanCriteria.TEXT_EQUAL_TO && t.getBackground().toUpperCase() == color) {
ar.push(c.getBooleanCondition().getCriteriaValues()); // or ar.push([c.getBooleanCondition().getCriteriaValues().join(",")]);
}
});
return ar;
}, []);
const dstSheet = ss.getSheetByName(dstSheetName);
dstSheet.getRange(1, 1, values.length).setValues(values);
}
- When this script is run, the values are retrieved from the conditional formatting rules of all sheets. And, put the values in column "A" of the destination sheet.
Pattern 2:
In this pattern, the values are retrieved from a single sheet in the active Spreadsheet.
function sample2() {
const color = "#FF9900"; // Please set the background color you set. This sample is Orange in color.
const srcSheetName = "Sheet1"; // Please set source sheet name.
const dstSheetName = "Sheet1"; // Please set destination sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(srcSheetName);
const values = sheet.getConditionalFormatRules().reduce((ar, c) => {
const t = c.getBooleanCondition();
if (t.getCriteriaType() == SpreadsheetApp.BooleanCriteria.TEXT_EQUAL_TO && t.getBackground().toUpperCase() == color) {
ar.push(c.getBooleanCondition().getCriteriaValues()); // or ar.push([c.getBooleanCondition().getCriteriaValues().join(",")]);
}
return ar;
}, []);
const dstSheet = ss.getSheetByName(dstSheetName);
dstSheet.getRange(1, 1, values.length).setValues(values);
}
- When this script is run, the values are retrieved from the conditional formatting rules of "Sheet1" sheet. And, put the values in column "A" of the destination sheet.
Note:
- In this sample script, the hex color of
#FF9900
is used fromall rules that have an orange color
of your question. If this color code was different from your actual situation, please modify it.
References:
答案2
得分: 1
你可以使用此脚本从应用于名为Sheet1
的工作表的所有条件格式规则中提取值和颜色,并将其粘贴到名为Result
的工作表中:
代码:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("Sheet1");
const resultSheet = ss.getSheetByName("Result");
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Solution')
.addItem('提取数值', 'extractValues')
.addToUi();
}
function extractValues() {
const conditionalFormatRules = sheet.getConditionalFormatRules();
if (conditionalFormatRules.length > 0) {
const colors = {};
conditionalFormatRules.forEach(rule => {
var color = rule.getBooleanCondition().getBackground();
if (!(color in colors)) {
colors[color] = [];
}
colors[color].push(rule.getBooleanCondition().getCriteriaValues());
})
for (var i = 0; i < Object.keys(colors).length; i++) {
color = Object.keys(colors)[i];
resultSheet.getRange(1, i + 1).setValue(color).setBackground(color);
resultSheet.getRange(2, i + 1, colors[color].length).setValues(colors[color]).setBackground(color);
}
}
}
工作示例:
- 它将将结果分组到其相应的颜色下。
英文:
You can use this script to extract the values and colors from all conditional formatting rules applied to a given Sheet1
sheet and paste it to a sheet named Result
:
The code:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("Sheet1");
const resultSheet = ss.getSheetByName("Result");
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Solution')
.addItem('ExtractValues', 'extractValues')
.addToUi();
}
function extractValues() {
const conditionalFormatRules = sheet.getConditionalFormatRules();
if (conditionalFormatRules.length > 0) {
const colors = {};
conditionalFormatRules.forEach(rule => {
var color = rule.getBooleanCondition().getBackground();
if (!(color in colors)) {
colors[color] = [];
}
colors[color].push(rule.getBooleanCondition().getCriteriaValues());
})
for (var i = 0; i < Object.keys(colors).length; i++) {
color = Object.keys(colors)[i];
resultSheet.getRange(1, i + 1).setValue(color).setBackground(color);
resultSheet.getRange(2, i + 1, colors[color].length).setValues(colors[color]).setBackground(color);
}
}
}
Working example:
- It will group the results under its respective colors.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论