- 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.
- 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.
- Create the request body for
Spreadsheets.batchUpdate()
.
- At this time, the sheet information (sheetId) is modified.
- 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.