Home > Software design >  How do I make a copy of a google sheet tab and rename it
How do I make a copy of a google sheet tab and rename it

Time:01-04

Here is my code, Im trying to get the oldmonthtab to copy itself and name itself to the newmonthname, but I just cant figure out how to get that last part to work.

  var oldMonthName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("X2").getRange("U4").getValue();//name for the old month, formula in X2
  var newMonthName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("X2").getRange("U3").getValue();//name for new month, formula in X2
  var oldMonthTab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(oldMonthName);//old month tab, used to be able to hide it
  
  var destination = SpreadsheetApp.openById('1lQ10rgciG5WiUQ0uVQc1E7r7Qwd6hOkZQSaH');//chooses this spreadsheet, the ID should come from the URL of the sheet it will be copied to.
  oldMonthTab.copyTo(destination).setname(newMonthTab);//makes a copy of old month and sets the name to new month```
 

CodePudding user response:

The variable newMonthTab is undefined. Use this:

  oldMonthTab.copyTo(destination).setName(newMonthName);
  • Related