2
votes

I need to take a spreadsheet, duplicate it (there should be two separate spreadsheet files), and then rename the copy with the value of a specific cell from the original spreadsheet.

I did some research in Google's documentation, then looked in stackoverflow. I slightly modified what I found at "Google script to copy and rename a sheet and name is based on a cell reference"

function CreateNewTimesheet() {

  // The code below makes a duplicate of the active sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();

  // The code below will rename the active sheet to a date based on cell O49 of the source spreadsheet

 var myValue = SpreadsheetApp.getActiveSheet( ).getRange("O49").getDisplayValue();
 SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(myValue);

}

Here's my problem:

The code snippet above creates a copy of the original spreadsheet as a new tab within the original (source) spreadsheet and renames the tab properly. This is not exactly what I need. I need the original (source) spreadsheet duplicated, not copied, so that the duplicate of the original (source) spreadsheet and its duplicate are two separate files, not a tab within the original (source) file.

I need something like: Right-click, left-click "Make a Copy", rename "Copy of ..." sheet to desired name.

1

1 Answers

3
votes

Refer the below code...

function createCopy() {
  var myValue = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("O49").getValue();
  var copiedSpreadSheet = SpreadsheetApp.getActiveSpreadsheet().copy(myValue);
}

Edit : To create the copy of the spreadsheet and place it into a particular drive folder

function createCopy() {
  var myValue = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("O49").getValue();
  var destinationFolder = DriveApp.getFolderById("<<Drive Folder ID>>");
  DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).makeCopy(myValue,destinationFolder);
}