0
votes

I am trying to locate in the documentation but don't seem to find how to copy/move a newly created spreadsheet in Google Script, into a folder. I can see how to do this with files, but apparently Documents/Spreadsheets are not regular files for Google script.

This code does not work:

var folder = DriveApp.createFolder('Reports');
newSheet = SpreadsheetApp.create(clientNames[0][0]);
nsc = newSheet.copy('new1');
folder.addFile(nsc);

error: Cannot find method addFile(Spreadsheet)

https://developers.google.com/apps-script/reference/spreadsheet

I was trying to locate in the docs in Spreadsheets, Drive, Folder but still no result.

1

1 Answers

1
votes

You can get the ID of the copied spreadsheet, then get a reference to the copied spreadsheet with DriveApp with the ID, which is a file type. Then use the DriveApp file type in the .addFile() method:

function copySheet() {
  var folder = DriveApp.createFolder('Reports ' + "2/14/15");
  var newSheet = SpreadsheetApp.create("clientNamesABC" + "2/14/15");

  var nsc = newSheet.copy('new1');
  var nscID = nsc.getId();

  var theFileReference = DriveApp.getFileById(nscID);
  folder.addFile(theFileReference);

}