Question

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.

Était-ce utile?

La 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);
}

Autres conseils

I'm using this modified script for making sheet backups.Maybe it can be useful, it has fixed this same issue for me. It needs to enable Drive API before using it.

function makeCopy(fileSheetId, dstFolderId) {
  var fileSheet = DriveApp.getFileById(fileSheetId);
  var dstFolder = DriveApp.getFolderById(dstFolderId);
  var f = fileSheet.makeCopy(dstFolder);
    if (file.getMimeType() == MimeType.GOOGLE_APPS_SCRIPT) {
      Drive.Files.update({"parents": [{"id": dstFolderId}]}, f.getId());
    }
}

function run() {
  var fileSheetId = "<Sheet ID>";
  var dstFolderId = "<Destination folder ID>";
  makeCopy(fileSheetId, dstFolderId);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top