sheet.copyTo is creating two copies at one time

52 views
Skip to first unread message

Pastor John

unread,
May 17, 2020, 9:06:15 PM5/17/20
to Google Apps Script Community
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);
      }

Marko Kolombo

unread,
Jun 1, 2020, 8:06:45 AM6/1/20
to Google Apps Script Community
Hi John,

how do you trigger this function? I suppose through the spreadsheet interface since you are calling the browser class

I'm asking this because the code seems correct, so I suspect that somehow the script is triggered multiple times
Reply all
Reply to author
Forward
0 new messages