Reputation: 17
I am trying to create a Google Document (and would be useful for any other type of file) in a specific folder in Drive. I have consulted previous questions here on stack overflow, but cannot get my results. I have tried two different ways:
function createDoc(){
var destFolder = DriveApp.getFolderById('1SsLunZh5sn5HDGkcCcTBoXkvDdc70J4g');
var resource = {
title: "New Test Doc",
mimeType: MimeType.GOOGLE_DOCS,
parents: [{ id: destFolder }]
}
Logger.log(destFolder);
var fileJson = Drive.Files.insert(resource);
var fileId = fileJson.id;
Logger.log(fileId);
}
I've got the Advanced Drive API activated, but I get an error message stating:
GoogleJsonResponseException: API call to drive.files.insert failed with error: File not found: Udemy GAS Complete Course (line 25, file "Code")
I've also tried this:
function createDoc2(){
var destFolder = DriveApp.getFolderById('1SsLunZh5sn5HDGkcCcTBoXkvDdc70J4g');
var newDoc = DriveApp.createFile("New Test Doc", "", MimeType.GOOGLE_DOCS);
}
But I get an error stating: Exception: Invalid argument: file.contentType (line 35, file "Code")
Would very much appreciate some insight. Thanks in advance
Upvotes: 0
Views: 2387
Reputation: 4915
For those who are trying to create the file in a shared drive, you have to add the supportsAllDrives
parameters.
Taking the chosen answer as example:
function createDoc(){
var destFolder = DriveApp.getFolderById('folderId');
var resource = {"title": "New Test Doc",mimeType:MimeType.GOOGLE_DOCS,parents: [{id:destFolder.getId() }]}
var fileJson = Drive.Files.insert(resource, null, {supportsAllDrives: true});
var fileId = fileJson.id;
}
Upvotes: 1
Reputation: 198
@Cooper My script is bound to a spreadsheet creating a form in the same directory as the spreadsheet. It works fine in a folder on my drive but gets the error when run from a shared drive. (educational account)
let file = DriveApp.getFileById(ssId);
console.info('file = ' + file);
let folders = file.getParents();
let parFldr, parFldrId;
while (folders.hasNext()) {
let parFldr = folders.next();
console.log('folder Name(): ', parFldr.getName() );
parFldrId = parFldr.getId();
console.log('folder Id(): ', parFldrId );
console.log('folder Url(): ', parFldr.getUrl() );
}
const fileResource = {
title: qTitle,
description :qDesc,
confirmationMessage:qConfMsg,
collectsEmail:true, // this worked ! ! ! ! !
requireLogin:true, // worked limited to school district
// isQuiz:false, // did not set - did it in another ways
"parents": [{'id':parFldrId}],
mimeType: 'application/vnd.google-apps.form'
};
let dummyForm = Drive.Files.insert(fileResource);
let newForm = FormApp.openById(dummyForm.id);
console.log
file = Preposition Quizzes (Form from SS)
folder Name(): Grammar
folder Id(): 1rpezlYt6xWJBIOtYinF9_bAGd46y9-Er
folder Url(): https://drive.google.com/drive/folders/1rpezlYt6xWJBIOtYinF9_bAGd46y9-Er
GoogleJsonResponseException: API call to drive.files.insert failed with error: File not found: 1rpezlYt6xWJBIOtYinF9_bAGd46y9-Er
I do not see the difference between my code and your solution. Am I missing something or is there a problem with working on a shared drive? Thanks. I know I am not supposed to publish my own question but I think it is useful to keep this thread together.
Upvotes: 1
Reputation: 64032
Maybe this would work for you. I didn't test it. I just found that you didn't include the getId() method in id: key for the resource.
function createDoc(){
var destFolder = DriveApp.getFolderById('folderId');
var resource = {"title": "New Test Doc",mimeType:MimeType.GOOGLE_DOCS,parents: [{id:destFolder.getId() }]}
var fileJson = Drive.Files.insert(resource);
var fileId = fileJson.id;
}
If this doesn't work let me know I'll be glad to take a better look at it.
Upvotes: 1