Ted
Ted

Reputation: 3885

google script copy document

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.

Upvotes: 1

Views: 853

Answers (1)

Alan Wells
Alan Wells

Reputation: 31310

First get the ID of the copied spreadsheet. Then use the ID to get a reference to the copied spreadsheet with DriveApp. The reference is the file type. Then use the DriveApp file type in the .addFile() method:

function copySheet(date) {
  var folder, newSheet, nsc, nscID, theFileReference;

  folder = DriveApp.createFolder('Reports ' + date);
  newSheet = SpreadsheetApp.create("clientNamesABC" + date);
  
  nsc = newSheet.copy('new1');
  nscID = nsc.getId();//Get ID of copied Sheet
  
  theFileReference = DriveApp.getFileById(nscID);
  folder.addFile(theFileReference);
  DriveApp.getRootFolder().removeFilie(theFileReference);//Remove file from the root Drive
}

Upvotes: 2

Related Questions