0
votes

Hopefully relatively simple, but can't seem to find an answer.

I have a function that copies (archives) the current sheet to another spreadsheet, but then it's called "Copy of [sheet name]" in the destination spreadsheet.

Hoping I can get rid of the Copy of part, but setName() hasn't seemed to work.

Everything in the below code works until the last two lines.

function archiveCurrentSheet() {
  SpreadsheetApp.getUi() 
  var areyousure = Browser.msgBox('Are you sure you want to archive this sheet?', Browser.Buttons.OK_CANCEL);
   if (areyousure == "cancel" ) {
    }
    else {
      var source = SpreadsheetApp.getActiveSpreadsheet();
      var currentSheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
      var sheet = source.getActiveSheet();
      var destination = SpreadsheetApp.openById("ID");
      sheet.copyTo(destination);    
      var sheetRename = SpreadsheetApp.openById("ID").getSheets();
      sheetRename.setName(currentSheetName);    
    }
}

It gives error: "TypeError: Cannot find function setName in object Sheet,Sheet,Sheet,Sheet,Sheet,Sheet,Sheet,Sheet."

Thanks!

p.s. bonus: would be how do I delete the current

1

1 Answers

1
votes

The sheet.copyTo(destination) method returns the new sheet so you can do something like this.

  sheet.copyTo(destination).setName(currentSheetName);