Search code examples
google-apps-scriptmime-types

Apps Script Drive App Service - Create a file of Google Type - Mime Type


I'm using the .createFile method of the DriveApp Folder class. This is the syntax:

createFile(name, content, mimeType)

The documentation is here: createFile Google DriveApp Service

The example shows a mimeType setting of MimeType.HTML.

// Create an HTML file with the content "Hello, world!"
 DriveApp.createFile('New HTML File', '<b>Hello, world!</b>', MimeType.HTML);

I can type in MimeType. and get a list of MimeTypes, one of them being GOOGLE_DOCS. So I entered MimeType.GOOGLE_DOCS. But I'm getting an execution error stating that one of the arguments is invalid. If I type in a Mime Type of 'text/plain', I get no error.

How do I create a file with the document type of a Google Type?

If I enter a Mime Type of 'GOOGLE_DOCS' as text, it creates a file, but it creates a file with a file type of application/octet-stream.

If I use a MimeType of MimeType.HTML, I don't get an error, and the file is viewable from my Google Drive.


Solution

  • You can use DocumentApp.create('File_Name') to create a fileType of GOOGLE_DOCS. DocumentApp service allows to create and manipulate Google Documents. For more details, you may check the reference.

    References:

    In order to place the recently create Document in a Folder, you may use DriveApp. Here is a sample code.

    function createandPlaceInFolder(){
      //Create the document
      var doc = DocumentApp.create('My new file');
      //get it as a drive file
      var file = DriveApp.getFileById(doc.getId());
      //get the target folder
      var targetFolder  = DriveApp.getFolderById('MY_FOLDER_ID');//Change it to the folder_Id
      //place the file in target folder
      targetFolder.addFile(file);
    }