Search code examples
google-apps-scriptgoogle-drive-apimime-types

Mimetype (using Google Script) for Google Doc Creation Working in Some Instances and Not Others


I am trying to create a new spreadsheet in Google Drive using Google Script. When using the code below the Mimetypes work in some instances (like OPENDOCUMENT_SPREADSHEET and JAVASCRIPT) but not in others (like GOOGLE_DRAWINGS and GOOGLE_SHEETS). Can someone explain the reason for this?

function createSheet() {
  var mimeTest = MimeType.OPENDOCUMENT_SPREADSHEET;
  var quickReferenceSheet = DriveApp.createFile("test", "", mimeTest);
}

Error message comes up as "Invalid argument: file.contentType (line 3, file "Sheet Formula Check")"

Thank you in advance for your time.


Solution

    • You want to create new Spreadsheet using Google Apps Script.

    If my understanding is correct, how about this sample script? Unfortunately, Google Docs cannot be created using DriveApp.createFile(). I think that this might be the specification. So how about using the create method of Class SpreadsheetApp?

    Sample script 1:

    var spreadsheet = SpreadsheetApp.create("test");
    

    Sample script 2:

    If you want to create new Spreadsheet using Drive API, how about this sample script? When you use this script, please enable Drive API at Advanced Google Services.

    var spreadsheet = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: "test", parents: [{id: "### folderId ###"}]});
    
    • When Drive API is used, the new Spreadsheet can be directly created in the specific folder. This cannot be done by the create method of Class SpreadsheetApp.

    References:

    If I misunderstood your question, I apologize.