15

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.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
Oliver Edholm
  • 151
  • 1
  • 1
  • 3
  • 1
    For anyone who landed here because of a search result but only wanted to learn how to construct a URL manually for this purpose, `https://docs.google.com/document/create?folder=1d4AX96kMR34J0iNGABrrNg8` works for me: https://webapps.stackexchange.com/questions/6437/save-a-link-to-create-new-document-in-google-docs?rq=1#comment76082_6488 – Ryan Sep 30 '21 at 18:03

7 Answers7

26

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);
Serge insas
  • 45,904
  • 7
  • 105
  • 131
9

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 );
Kriggs
  • 3,731
  • 1
  • 15
  • 23
3

This is how to create a new Google Doc in a specific folder. When the Doc is created, the mime type must be set.

Note that this code does NOT first create a Doc file, move it, and then delete the file in the Root Drive. This code creates a new Google Doc type file directly in a folder.

You will need to use the Advanced Drive API. It must be enabled in two places.

  • From the code editor choose "Resources" and then choose, "Advanced Google services"
  • Click the button for "Drive API" so that it is ON.
  • Click the link: Google Cloud Platform API Dashboard.
  • Search Drive API
  • Choose Google Drive API from search list
  • Enable the Drive API

Code:

function myFunction(e) {
  var doc,fileResource,folder,ID_of_newFile,multipleFolders,newFile,
      newDocName,rng,sh,ss,ssFile,ssID;

  var name = e.values[1];
  var kantoor = e.values[2];

  newDocName = 'Sleuteloverdracht ' + name;//Set the name of the new file

  rng = e.range;//Get range in the spreadsheet that received the data
  sh = rng.getSheet();//Get sheet tab
  ss = sh.getParent();//Get spreadsheet
  ssID = ss.getSheetId();//Get file ID of spreadsheet - Integer

  ssFile = DriveApp.getFileById(ssID);//Get the spreadsheet file in order to get the
    //folder to put the new file in
  multipleFolders = ssFile.getParents();//Get all parent folders of the spreadsheet file

  folder = multipleFolders.next();//Get the folder of the spreadsheet

  fileResource = {
    'title': newDocName,
    "parents": [{'id':folder.getId()}],  //<--By setting this parent ID to the 
    //folder's ID, it creates this file in the correct folder
    'mimeType': 'application/vnd.google-apps.document'
  };

  newFile = Drive.Files.insert(fileResource);//Create the new document file
   // directly into the same folder as the spreadsheet
  ID_of_newFile = newFile.getId();

  //Logger.log(ID_of_newFile)

  doc = DocumentApp.openById(newFile.getId());//Open the new file as a Google document

  var body = doc.getBody();
  body.appendParagraph('Sleuteloverdracht ' + name);
  body.appendParagraph('Uw naam is '+ name + '\n' + 
    'U heeft de sleutel van kantoor '+ kantoor);
  doc.saveAndClose();

}
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
2

Serge insas's answer helped me, but the methods are now deprecated in 2021. Here is what worked for me.

var folder = 'Your Folder Id'
var title = 'Your File Name'    
var doc = DocumentApp.create(title);
docFile = DriveApp.getFileById(doc.getId());
docFile.moveTo(folder);
  • When you use '.moveTo' instead of '.makeCopy' you can save the trouble to delete the file in the root folder. '.moveTo' actually moves to file to your target folder: `var targetFolder = DriveApp.getFolderById(FOLDER_ID); var docFile = DriveApp.getFileById(doc.getId()); docFile.moveTo(targetFolder);` – Oliver Jakoubek Jul 27 '21 at 08:28
  • Updated the above to include Oliver's comment. – Bryan Monesson-Olson Aug 09 '21 at 14:55
0

To add to @Kriggs answer.

To remove from the root folder:

DriveApp.getRootFolder().removeFile(file)

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

enter image description here

Mycah
  • 4,602
  • 5
  • 24
  • 32
0
var folder = DriveApp.createFolder ("A Folder");
var file = DriveApp.createFile ("filename.file", "Some text");
file.moveTo (folder);
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
-1

You can also use the Drive App to create a copy of a Google document (e.g. a template)

use this:

DriveApp.getFileById(templateFileId).makeCopy(name, destination);
Jacopo Mosconi
  • 972
  • 8
  • 22
nick
  • 1