简体   繁体   中英

google apps script how to copy conditional formating in antoher sheet

I have a Google spreadsheet with dozens of identical sheets regarding the format. I have to apply the same conditional formating to all the sheets, so I would like to make it manually on the first sheet, then use Google Apps Script to apply the conditional formating to all the other sheets. I have done the coding below but I get an error that says, translated from french, "The conditional formating rule can't refer to antoher sheet".

function myFunction() {
  var my_spreadsheet = SpreadsheetApp.getActiveSpreadSheet(); 

  var all_sheets = my_spreadsheet.getAllSheets();  
  var source_sheet = all_sheets[0];  
  var source_rules = source_sheet.getConditionalFormatRules();

  var destination_sheet;

  for (var i=1; i<=all_sheets.length-1; i++){
    destination_sheet = all_sheets[i];
    destination_sheet.setConditionalFormatRules(source_rules); 
  }    
}

Do you know how can I copy conditional formating from one sheet to another using Google Apps Script or any other way?

  • You want to copy the conditional formats in a source sheet to all other sheets.

If my understanding is correct, how about using Sheets API? I thought the the reason that such error occurs might be the information of the source sheet is included in the conditional formats. By this consideration, I modified the sheet information using Sheets API for copying the conditional formats. I think that there are several answers for your situation. So please think of this as just one of them.

When you use Sheets API, please enable Sheets API at Advanced Google Services and API console. You can see about how to enable Sheets API at here .

Sample script:

function myFunction() {
  var my_spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = my_spreadsheet.getId();
  var all_sheets = my_spreadsheet.getSheets();
  var source_sheet = all_sheets[0].getSheetName();

  var srcConditionalFormats = Sheets.Spreadsheets.get(spreadsheetId, {ranges: source_sheet, fields: "sheets/conditionalFormats"});
  all_sheets.shift();
  var cf = srcConditionalFormats.sheets[0].conditionalFormats;
  var reqs = all_sheets.reduce(function(ar, e, i) {
    return ar.concat(cf.map(function(f, j) {
      var temp = JSON.parse(JSON.stringify(f));
      temp.ranges = temp.ranges.map(function(g, k) {
        g.sheetId = e.getSheetId();
        return g;
      });
      return {addConditionalFormatRule: {rule: temp}};
    }));
  }, []);
  Sheets.Spreadsheets.batchUpdate({requests: reqs}, spreadsheetId);
}

In this script, the following flow is run.

  1. Retrieve the conditional format object from the source sheet using Spreadsheets.get() .
    • From your script, it supposes that the source sheet is the 1st index of the sheets.
  2. Create the request body for Spreadsheets.batchUpdate() .
    • At this time, the sheet information (sheetId) is modified.
  3. Request the created request body using Spreadsheets.batchUpdate() .

Note:

  • In your script, there is a spelling miss at SpreadsheetApp.getActiveSpreadSheet() and no method is used at my_spreadsheet.getAllSheets() . But in your question, it is written that the error of The conditional formating rule can't refer to antoher sheet occurs. From this situation, I thought this might be due to miswriting of when you posted the question.

References:

If I misunderstood your question and this was not the result you want, I apologize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM