Search code examples
javascriptgoogle-apps-scriptgoogle-docsgoogle-drive-api

Create a Google Doc in a Specific Folder


I am trying to create a google doc in a specific folder held in the variable chapterOne.

I've googled and googled and the closest I've found to an answer is this code here:

function Test() {
  DocsList.createFolder('Folder1').createFolder('Subfolder1').createFile('File1', 'Empty');
}

But the problem is that it creates a file, not a google doc. What I'm looking for would call DocumentApp somehow...

Any help would be greatly appreciated!

~Noelle


Solution

  • You will probably want to create the document using DocumentApp, then move it to a different folder.

    var docName = "YOUR_DOC_NAME";
    var doc = DocumentApp.create(docName); //this creates the doc in the root folder
    var file = DocsList.getFileById(doc.getId());
    file.removeFromFolder(DocsList.getRootFolder());
    file.addToFolder(DocsList.getFolder("path/to/folder"));