Invalid argument: file.contentType

513 views
Skip to first unread message

Alistair George

unread,
Apr 17, 2020, 12:11:00 AM4/17/20
to Google Apps Script Community

This question has been asked elsewhere and there seems no satisfactory answer:

  if (Rsheet==0)
 
{
   
// We didn't find the file, so create it.
while (folders.hasNext()){files = folders.next();}
files
.createFile(folderlisting,"",MimeType.GOOGLE_SHEETS); //error here; GOOGLE_SHEETS
Rsheet = SpreadsheetApp.open(folderlisting);    
 
}

Thanks for any suggestions. Al.

Tanaike

unread,
Apr 17, 2020, 2:32:37 AM4/17/20
to google-apps-sc...@googlegroups.com
  • You want to create new Spreadsheet in the specific folder using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Issue and workaround

Unfortunately, the method of "createFile" cannot create the Google Docs. I think that this is the specification of Google side. So it is required to use the workaround. Here, I would like to propose the following 2 patterns.

Pattern 1
In this pattern, Drive Service is used. In this case, the Spreadsheet is created to the root folder and the Spreadsheet is moved to the specific folder.

var Rsheet;
if (folders.hasNext()) {
 
var folder = folders.next();
 
Rsheet = SpreadsheetApp.create(folderlisting);
 
var file = DriveApp.getFileById(Rsheet.getId());
 
DriveApp.getRootFolder().removeFile(file);
  folder
.addFile(file);
}


Pattern 2
In this pattern, Drive API is used. In this case, the Spreadsheet is directly created to the specific folder. Before you run the script, please enable Drive API at Advanced Google services.

var Rsheet, fileId;
if (folders.hasNext()) {
 
var folder = folders.next();
 
var file = Drive.Files.insert({title: folderlisting, mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: folder.getId()}]});
  fileId
= file.id;
}
if (fileId) Rsheet = SpreadsheetApp.openById(fileId);

References

If this was not the direction you expect, I apologize.


Alistair George

unread,
Apr 17, 2020, 4:41:36 AM4/17/20
to Google Apps Script Community
You reminded me to use your first response, but the answer is your first comment, (the API is faulty and remains so) as the original call is listed in Google API docs as valid.
Regards, and thanks kindly, Al.

Reply all
Reply to author
Forward
0 new messages