1

I know I can use

var doc = DocumentApp.getActiveDocument();

to create a new document on GoogleDocs, but how do I create it on a specific folder?

Further more, how do I write a code to open the newly created document? I´ve searched https://developers.google.com/apps-script/reference/document/document-app#create(String), but I could not find the answer.

Thank You Very Much!

user3347814
  • 1,138
  • 9
  • 28
  • 50

1 Answers1

5

your code can get an instance of an already existing Document, and is used for script running "inside" the document (custom options, functions, etc.)

To create a new document instead, use:

// Create and open a document.
doc = DocumentApp.create('Document Name');

this works in any script.

To move the document on a specific folder, you have the method:

myFolder.addFile(doc);

For more info, look at DriveApp, DocumentApp, Document, File, Folder reference.

  • Is there a way of running the script without opening it from a Document? – user3347814 May 06 '14 at 18:31
  • 1
    the snippets above can run on any script: DriveApp or DocumentApp are standalone "factory" classes. Only methods like getActiveDocument() can run only from a Document that is currently opened by the user. – Massimo Coletti May 08 '14 at 07:33
  • 1
    Look also at [this post](http://stackoverflow.com/questions/11339500/how-to-fetch-the-id-of-a-google-spread-sheet-via-google-apps-script/18892153#18892153) that explains the differences between document id in Document/Spreadsheet App and DriveApp – Massimo Coletti May 08 '14 at 07:34