I use the following function to duplicate a sheet, and it works great.
function duplicateDataSheet() {
const date = getTodayDateAsString();
const newDataName = `${date}_data`;
const oldDataName = 'data';
Logger.log(`newDataName = ${newDataName}`);
const spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName(oldDataName), true);
spreadsheet.duplicateActiveSheet();
const newDataSheet = spreadsheet.getActiveSheet();
newDataSheet.setName(newDataName);
};
The sheet named 'data' has a Named Range called 'csv_paste_area'.
After duplicating the sheet, I then call a custom function of mine:
clearRangeByName('csv_paste_area');
But it ends up clearing the range of the most recently duplicated version of the 'data' sheet rather than the true 'csv_paste_area' Named Range, which exists on the original 'data' sheet!
This has been driving me crazy!
It feels like a bug in Google Sheets. I wonder if there is a work-around.