Unable to create file while using Driveapp

699 views
Skip to first unread message

MenGue

unread,
Jan 15, 2022, 6:17:04 PM1/15/22
to Google Apps Script Community
Hello!

Could anyone guide me to understand why this is not working?

I am creating a new folder and adding a file to it but adding the file type as Google docs is not working. I am getting the error below.

Exception: Invalid argument: file.contentType
makeFolder @ Code.gs:22

Code and screenshot below.

function onOpen(){


var ui = SpreadsheetApp.getUi()
ui.createMenu("Adv").addItem("Create folder","makeFolder").addToUi();

}

function makeFolder(){

var parFolder = DriveApp.getFolderById("13_wyAVDEd79KCruiO6oce1xAHpexIQFZ")
var ui = SpreadsheetApp.getUi();
var response = ui.prompt("What is the folder name?",ui.ButtonSet.OK_CANCEL);

if(response.getSelectedButton()==ui.Button.OK){

Logger.log("The folder is " + response.getResponseText())

var folder = parFolder.createFolder(response.getResponseText())
var folderId = folder.getId();
ui.alert("New folder has been created " + folderId);
folder.createFile("test","test",MimeType.GOOGLE_DOCS)

}

console.log(parFolder.getName())

}


ss.PNG


Tanaike

unread,
Jan 15, 2022, 7:53:28 PM1/15/22
to Google Apps Script Community
In your script, Google Document is tried to be directly created with `createFile` method. `createFile` cannot directly create Google Document. I thought that this is the reason for your issue. In this case, how about the following modification?

From
folder.createFile("test", "test", MimeType.GOOGLE_DOCS)

To:
var doc = DocumentApp.create("test");
doc.getBody().setText("test");
DriveApp.getFileById(doc.getId()).moveTo(folder);

In this modification, a new Google Document is created and put the text, and then, the Document is moved to the folder.
Reply all
Reply to author
Forward
0 new messages