I am trying to copy conditional formatting from a GS range to another range and nothing is copied (no error). This is the snippet:
spreadsheet.getSheetByName('sheet_source').getRange(14,7,200,19).copyTo(spreadsheet.getSheetByName('sheet_target').getRange(14,7,200,19), SpreadsheetApp.CopyPasteType.PASTE_CONDITIONAL_FORMATTING, false);
If I use the same code but change the CopyPasteType to a different option, it works (see example below). The problem is that I only want conditional formatting copied so I need the PASTE_CONDITIONAL_FORMATTING to work. Please let me know if you have any suggestions.
spreadsheet.getSheetByName('sheet_source').getRange(14,7,200,19).copyTo(spreadsheet.getSheetByName('sheet_target').getRange(14,7,200,19), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
Per doubleunary's suggestion, I reformatted the script as follows, and it works:
function CndFmtFromTmplt_PasteCndFmt() {
//get spreadsheet
const spreadsheet = SpreadsheetApp.getActive();
//get sheets
const sheetSource = spreadsheet.getSheetByName("Template");
const sheetTarget = spreadsheet.getSheetByName("Test Sheet");
//clear rules
sheetTarget.clearConditionalFormatRules();
//get copy ranges
//const rangeSource = sheetSource.getRange(1, 1, sheetSource.getMaxRows(), sheetSource.getMaxColumns());
const rangeSource = sheetSource.getRange('A1:AF200');
const rangeTarget = sheetTarget.getRange('A1:AF200');
// copy values to destination range
rangeSource.copyTo(rangeTarget, SpreadsheetApp.CopyPasteType.PASTE_CONDITIONAL_FORMATTING,false);
};
CodePudding user response:
The code you show looks fine, so it is unclear where the problem is. To make debugging easier, try structuring your code a bit, like this:
function test() {
const ss = SpreadsheetApp.getActive();
const sourceRange = ss.getRange('sheet_source!N7:S200');
const targetRange = ss.getRange('sheet_target!N7');
sourceRange.copyTo(targetRange, SpreadsheetApp.CopyPasteType.PASTE_CONDITIONAL_FORMATTING, false);
}
Check My Executions for failed executions, and view those logs to learn more about why the function fails. You can run the test()
function in the script editor when testing.