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
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
Share Improve this question asked Mar 2, 2013 at 23:56 nellygrlnellygrl 6573 gold badges16 silver badges34 bronze badges3 Answers
Reset to default 5DocsList is depreciated now, so for anyone who may e across this thread, here is an updated answer:
function createDoc() {
// Creates doc in root directory
var doc = DocumentApp.create('MyDocument');
var docFile = DriveApp.getFileById(doc.getId());
// Copy doc to the directory we want it to be in. Delete it from root.
DriveApp.getFoldersByName('Subfolder1').next().addFile(docFile);
DriveApp.getRootFolder().removeFile(docFile);
// Returns the ID of the newly created Doc
return DriveApp.getFilesByName('MyDocument').next().getId();
}
Here is a link to the post where I found this answer: Create a Google Doc file directly in a Google Drive folder
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"));
Instead of creating a Google Doc in your Google Drive root and then moving to a specific folder, I was able to create a file directly in a specific Drive folder. But for this, I am maintaining an empty Google Document somewhere else.
function createNewDocInDriveFolder(){
var emptyDocId = "XXX-XXXXXvqadhYQiYoAno8aam2HJhRNxgO84qF9VXXXX";
var destFolderId = "XXXXPsu8uKcmkhJ1-oiSRNzE1-XXXX_XX";
var destFolder = DriveApp.getFolderById(destFolderId);
var newDocFile = DriveApp.getFileById(emptyDocId).makeCopy("New Doc", destFolder);
return DocumentApp.openById(newDocFile.getId());
}