Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script: Create New Document (Spreadsheet) from Template

I'm currently doing this:

//set active spreadsheet to my template
      var ss = SpreadsheetApp.openById("MYSHEETKEY");
      SpreadsheetApp.setActiveSpreadsheet(ss);


//duplicate active sheet and set a new name
       SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet().setName(rData[0][1]);

which is creating a new sheet in my template Spreadsheet, quite rightly. What I really need is to create an entirely new copy of the document as a whole, ideally in to a specified folder in my Google Drive. Is this possible?

Thanks as advance!

like image 771
James Avatar asked Sep 27 '22 21:09

James


1 Answers

There is a copy() function. The docs are at: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#copy(String)

var ss = SpreadsheetApp.openById("MYSHEETKEY");
var newSS = ss.copy("Copy of " + ss.getName());
// Move to original folder
var originalFolder = DriveApp.getFileById("MYSHEETKEY").getParents().next();
var newSSFile = DriveApp.getFileById(newSS.getId());
originalFolder.addFile(newSSFile);
DriveApp.getRootFolder().removeFile(newSSFile);
like image 103
Spencer Easton Avatar answered Oct 01 '22 16:10

Spencer Easton