0
votes

I have the following script that duplicates a specified sheet, duplicates in and renames it to todays date.

function Duplicate() {
  
 var source = SpreadsheetApp.getActiveSpreadsheet();
 
 const patternX = 'Master';
 
 var masterX = source.getSheetByName(patternX);

 masterX.copyTo(source).setName(getDates()[1]).showSheet();
  
 masterX.hideSheet();
}

I am struggling with making the following amendments to it:

  • Is it possible to automatically add the date of duplication into Cell A1 of the duplicated new sheet?
  • How does one make the duplicate sheet active/move it to first sheet in the Workbook?
1

1 Answers

2
votes

This should work:

function Duplicate() {
 const source = SpreadsheetApp.getActiveSpreadsheet();
 const patternX = 'Master';
 const masterX = source.getSheetByName(patternX);
 const new_dt = getDates()[1];
 const new_sheet = masterX.copyTo(source).setName(new_dt).showSheet();
 new_sheet.getRange("A1").setValue(new_dt);
 source.setActiveSheet(new_sheet);
 source.moveActiveSheet(1);
 masterX.hideSheet();
}