create google doc in existing folder in drive

897 views
Skip to first unread message

Shivani Shinde

unread,
Jan 21, 2020, 1:31:40 AM1/21/20
to Google Apps Script Community
Hello all,
I am creating a google doc from apps script as follows:

 var newDoc = DocumentApp.create(docName);

I want this new document to be created in an existing folder in my drive. I  tried the following way:
var dir = DriveApp.getFolderById("1WmbL7gvU-ghuRhZ44VQXbz2iDevF00XU");
dir.addFile(newDoc);

But I get the error as 
ReferenceError: "DocsList" is not defined.

Is there any way to create a new doc in my folder or move my file to an existing folder via apps script? Any help will be appreciated.

Tanaike

unread,
Jan 21, 2020, 2:08:22 AM1/21/20
to Google Apps Script Community

How about this answer?

Issue and solution:
Unfortunately, I cannot find "DocsList" in your script. But if you use the following script,

var newDoc = DocumentApp.create(docName);

var dir = DriveApp.getFolderById("1WmbL7gvU-ghuRhZ44VQXbz2iDevF00XU");
dir
.addFile(newDoc);

An error occurs at "dir.addFile(newDoc);". Because "child" of "addFile(child)" is required to be the file object. In your script, the document object is used. So the error occurs.

If you want to create new Google Document and move it to the specific folder of "1WmbL7gvU-ghuRhZ44VQXbz2iDevF00XU", how about the following sample script?

Sample script:
var newDoc = DocumentApp.create(docName);

var dir = DriveApp.getFolderById("1WmbL7gvU-ghuRhZ44VQXbz2iDevF00XU");

var file = DriveApp.getFileById(newDoc.getId()); // Added
dir
.addFile(file); // Modified
DriveApp.getRootFolder().removeFile(file); // Added

  1. Retrieve the file object with "DriveApp.getFileById(newDoc.getId())".
  2. Add the parent folder to the file with "dir.addFile(file)"
  3. Remove the original parent folder with "DriveApp.getRootFolder().removeFile(file)".
    • In this case, when new Document is created with "DocumentApp.create(docName)", the file is created to the root folder. So the root folder is removed from the file. By this, the file has only the parent folder of "1WmbL7gvU-ghuRhZ44VQXbz2iDevF00XU". If "DriveApp.getRootFolder().removeFile(file)" is not used, the file has 2 parent folders.
References:


Andrew Roberts

unread,
Jan 21, 2020, 4:59:04 AM1/21/20
to google-apps-sc...@googlegroups.com
DocsList, wasn't that one of those services that went the same way as ScriptDB?? It's nice how they have seemed to stabilise a little more now - (touch wood) we've not lost any big services recently.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/70b1533f-1a90-4886-81d9-82c0e1054eee%40googlegroups.com.

Thomas P.

unread,
Jan 21, 2020, 10:03:16 AM1/21/20
to google-apps-sc...@googlegroups.com
Correct, DocsList was what we had before GDrive's DriveApp.
The DocsList was "sunset" 20.Apr.2015
Reply all
Reply to author
Forward
0 new messages