I have the following code that uses an onEdit function to duplicate a sheet and then name the sheet based on the entry in the first sheet. Everything works perfectly except (Sometimes) it creates two new sheets instead of one. The original is called MASTER. it creates a "copy of MASTER" and then renames it. Some how the .copyto is creating 2 new sheets. One is being renamed properly to tabName and the other remains "copy of MASTER1". I had to create a final section to delete the extra sheet if it is made. I would rather it not be made to start with. Any ideas of how this is happening?
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = getSheetByName("MASTER")
Browser.msgbox("Copy"); //This message box only comes up once so I know the script is not repeating
var copyNewSheet = masterSheet.copyTo(activeSpreadsheet); // this copy function is creating 2 sheets
copyNewSheet.setName(tabName); // this setName online names one of the 2 created sheets
//I had to created this section to deal with the problem
//Clean Up Sheets Error checking fo accidental sheet creation
var copyOfMasterSheet = activeSpreadsheet.getSheetByName("Copy of Master 1");
if (copyOfMasterSheet != null) {
activeSpreadsheet.deleteSheet(copy1OfMasterSheet);
}