Reputation: 69
I am familiar with writing conditional formatting rules in Google script.
I have a Google sheet that I have inherited that has been developed over a long period and conditional formatting rules have been manually inserted.
I am looking to copy all of the conditional formatting out of that google sheet using google apps script. The sheet has around 50 columns with many drop downs, and each dropdown, or combination of dropdowns, applies formatting. I think there are over 100 rules.
It would be VERY useful if I could get the conditional formatting out in a format that I could modify as needed and apply other (similar) sheets.
Any suggestions?
Mark
Upvotes: 1
Views: 965
Reputation: 4038
Note: We normally do not code for you, but in this case I have a sample script that I can share with you that was derived from the samples of these Spreadsheet App classes listed below:
You can try using these Spreadsheet App classes in Apps Script:
var ui = SpreadsheetApp.getUi();
function onOpen() { //Sets the custom menu
ui.createMenu('Extract Existing Conditional Formatting')
.addItem('Copy Conditonal Formatting', 'checkRangeSelection')
.addToUi();
}
function checkRangeSelection() { //checks the selected range & asks user if he/she wants to copy the range's conditonal formatting values to another sheet range
var rule = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getConditionalFormatRules()[0];
var ranges = rule.getRanges();
var selectedRange = SpreadsheetApp.getActiveSheet().getSelection().getActiveRange().getA1Notation();
var newCriteriaValue;
for (var i = 0; i < ranges.length; i++) {
if (selectedRange == ranges[i].getA1Notation()) {
var response = ui.alert("Selected Range: " + selectedRange +
"\n\nThis range has these criteria:\n" +
"\nTYPE: " + rule.getBooleanCondition().getCriteriaType() +
"\nVALUE: " + rule.getBooleanCondition().getCriteriaValues() + '\n\nDo you want to update it and apply it to a new sheet range?',
ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {
var valueNew = ui.prompt("Current Criteria Value is: " + rule.getBooleanCondition().getCriteriaValues() + "\n\n Type here to change it:\n");
valueNew.getResponseText() == '' ? newCriteriaValue = rule.getBooleanCondition().getCriteriaValues() : newCriteriaValue = valueNew.getResponseText();
var destSheet = ui.prompt("Type the \"Sheet Name\" where you would like to apply the conditional formatting:\n\n");
var newRange = ui.prompt("Type the \"Range\" (e.g. A1:A100) where you would like to apply the conditional formatting:\n\n");
try {
var destinationSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(destSheet.getResponseText());
var range = destinationSheet.getRange(newRange.getResponseText());
var copiedrule = rule.copy().withCriteria(rule.getBooleanCondition().getCriteriaType(), [newCriteriaValue.toString()]).setRanges([range]).build();
var rules = destinationSheet.getConditionalFormatRules();
rules.push(copiedrule);
destinationSheet.setConditionalFormatRules(rules);
} catch {
ui.alert('Double check Sheet Name and the Range you have used & try again.');
}
} else {
ui.alert('Cancelled');
}
} else {
ui.alert('Selected range \"' + selectedRange + '\" doesn\'t contain any criteria.');
return;
}
}
}
Note: On this demonstration, I have two sample sheets named the CURRENT (the main sheet that contains the conditional formatting rules) & DESTINATION
onOpen
function once on the Apps Script editor to load the custom menu.Upvotes: 0