Create a Google Doc file directly in a Google Drive folder

Oliver Edholm picture Oliver Edholm · Jul 31, 2015 · Viewed 11.1k times · Source

How do I create a Google document in a Google Drive folder?

I know that I can create a file in a folder like this.

var folder = DriveApp.createFolder("A Folder")
folder.createFile("filename.file", "Some text")

But how I can create a document in a folder using Google Apps Script.

Answer

Serge insas picture Serge insas · Jul 31, 2015

The other answer is correct but the file will exist in your folder AND in your "drive" folder (the root folder).

To avoid that, simply remove it from there !

code :

  var doc = DocumentApp.create('Document Name'),
      docFile = DriveApp.getFileById( doc.getId() );
  DriveApp.getFolderById('0B3°°°°°°°1lXWkk').addFile( docFile );
  DriveApp.getRootFolder().removeFile(docFile);