Nick Rabbets
Nick Rabbets

Reputation: 23

Script to Save a copy of Google Sheets

I am trying to find a way of creating a script that will copy a GoogleSheet, which is the Master file, into a new location with a new name. The name of the file would be located in Cell A1 with the folder location in Cell A2. With Excel it was relatively easy, but I just can't seem to get anything to work with GoogleSheets.

Upvotes: 0

Views: 803

Answers (1)

Marios
Marios

Reputation: 27348

Assuming this is the Master file:

example

Replace cell A2 with the folder id of the destination folder, choose the name of the copy in cell A1 and change Sheet1 to your specific case.

Copy and execute this function from the script editor:

function myFunction() {
  
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet1");
  
  const new_name = sh.getRange('A1').getValue();
  const dest_folder_id = sh.getRange('A2').getValue();
 
  const dest_folder = DriveApp.getFolderById(dest_folder_id);
  DriveApp.getFileById(ss.getId()).makeCopy(new_name, dest_folder);
}

References:

Upvotes: 3

Related Questions