I am very new to using google scripts. I have a spreadsheet with tabs that are titled by date. I found the following script that will enable me to create a variable number of duplicate sheets. The script will also automatically rename the new sheets by changing the date to the next date. The only problem is that all of the new sheets are added to the far right of the document. I want each copy to be placed to the left of the active sheet. So if I have a tab titled 1/3/21 and I make 3 copies of that tab, the new tabs will read from left to right 1/6/21; 1/5/21; 1/4/21. This is the script I am using. I had to disable "Enable Chrome V8 runtime" for this script to work.
function duplicatesheet() {
var as = SpreadsheetApp.getActiveSpreadsheet(); // active spreadsheet
var s = as.getActiveSheet(); // first sheet object
var dateCell = "A6:N6"; // cell containing first date
var N = 5; // number of copies to make
var startDate = new Date(s.getRange(dateCell).getValue()); // get the date stored in dateCell
var day = startDate.getDate(); // extract the day
var month = startDate.getMonth(); // extract the month
var year = startDate.getFullYear(); // extract the year
// loop over N times
for (var i = 0; i < N; i++) {
var asn = s.copyTo(as); // make a duplicate of the first sheet
var thisSheetDate = new Date(year, month, day+(i+1)); // store the new date as a variable temporarily
asn.getRange(dateCell).setValue(thisSheetDate); // writes the date in cell "B3"
asn.setName(Utilities.formatDate(thisSheetDate, undefined, "MM/dd/yy")); // sets the name of the new sheet
}
}
