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

蹲街弑〆低调 提交于 2021-02-19 07:57:17

问题


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.


回答1:


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:

  • https://developers.google.com/apps-script/reference/document/document-app

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


来源:https://stackoverflow.com/questions/23419586/apps-script-drive-app-service-create-a-file-of-google-type-mime-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!