简体   繁体   中英

How do I make a copy of a google sheet tab and rename it

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```
 

The variable newMonthTab is undefined. Use this:

  oldMonthTab.copyTo(destination).setname(newMonthName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM