Creating a document in a folder

I searched all over the network but did not find how to create a document in a folder. I know that I can create a file in such a folder.

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

You know, I’m wondering how I can create a document in a folder in Google Apps Script.

+5
source share
3 answers

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

To avoid this, just 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); 
+14
source

This is slightly different with the document, first create it, then go to the folder:

 var doc = DocumentApp.create('Document Name'), docFile = DriveApp.getFileById( doc.getId() ); DriveApp.getFolderById(foldId).addFile( docFile ); 
+7
source

To add @Kriggs to the answer.

To remove from the root folder:

DriveApp.getRootFolder().removeFile(file)

Otherwise, it will be at the root level and a new location.

enter image description here

0
source

All Articles