1

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

8
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand your question. Can I ask you about the detail of your question? Commented Jul 12, 2022 at 0:22
  • Thanks for looking at it anyway. Let me try to explain it some more... I know that I can upload conditional formatting rules TO a spreadsheet using the API, BUT can I read the conditional formatting rules FROM a spreadsheet using the API (and then copy them out) Commented Jul 12, 2022 at 1:08
  • Thank you for replying. About can I read the conditional formatting rules FROM a spreadsheet using the API (and then copy them out), I think that this can be achieved. But I cannot imagine your expected output situation. By this, unfortunately, I cannot still propose an answer. This is due to my poor English skill. I deeply apologize for this. By the way, what is 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.? This is 2nd question? Commented Jul 12, 2022 at 1:16
  • developers.google.com/sheets/api/guides/conditional-format Commented Jul 12, 2022 at 1:26
  • Please give a specific example of how you would like to extract conditional formatting from a sheet to another sheet based on your statement about "get the conditional formatting out in a format that I could modify as needed and apply other (similar) sheets"? This is to fully grasp & visualize the step-by-step process that you're trying to accomplish. Commented Jul 12, 2022 at 1:31

1 Answer 1

0

SUGGESTION

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:

Sample 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;
    }
  }
}

Demonstration

Note: On this demonstration, I have two sample sheets named the CURRENT (the main sheet that contains the conditional formatting rules) & DESTINATION enter image description here

  • Save the script as a bound script in your spreadsheet file
  • Then, run the onOpen function once on the Apps Script editor to load the custom menu.
  • After that, you will see a custom menu named "Extract Existing Conditional Formatting" on your spreadsheet as seen below:

enter image description here

  • You can highlight any range that contains the Conditional Formatting that you'd like to copy to another sheet tab, then press the custom menu. It will show you the criteria type used and its value, as seen here (this sample was done on the CURRENT sheet) :

enter image description here

  • If you press the Yes button, you will be prompted to change these details below:
  1. Update the Criteria Value Or you could use the default value by just pressing Ok to go to the next step:
  2. Type the Destination Sheet name
  3. Type the Range (in A1 Notation format) where you want to apply the copied Conditional Formatting

enter image description here

  • After that, on the DESTINATION sample sheet, the Conditional Formatting has been applied to the range.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.