Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Google Doc in a Specific Folder [duplicate]

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...

like image 450
nellygrl Avatar asked Dec 29 '25 18:12

nellygrl


1 Answers

DocsList is depreciated now, so for anyone who may come 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

like image 153
Lemons Avatar answered Jan 01 '26 10:01

Lemons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!