Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Google Doc file directly in a Google Drive folder

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.

like image 396
Oliver Edholm Avatar asked Jul 31 '15 06:07

Oliver Edholm


People also ask

How do I automatically add files to Google Drive?

Sync Files Using Google Drive Backup and Sync Simply install Backup and Sync and you can add any folder on your computer to automatically upload all files to Google Drive. You'll also get a new Google Drive folder on your computer.


2 Answers

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);
like image 141
Serge insas Avatar answered Sep 19 '22 17:09

Serge insas


With document it's a bit different, you first create it then move to the folder:

var doc = DocumentApp.create('Document Name'),
      docFile = DriveApp.getFileById( doc.getId() );

DriveApp.getFolderById(foldId).addFile( docFile );
like image 27
Kriggs Avatar answered Sep 22 '22 17:09

Kriggs