I have these two functions. One takes a folder name the other takes a folder ID. I can't figure out why the folder ID one gives me "Exception: Invalid argument: parent.mimeType". I'm using the same code in both
folder.createFile(fileName, content)
I have tried various third parameters like 'text/plain' and others with no results
// this works fine but will not work for subfolders plus I know the folderID
// so why bother walking the list of folders
function createOrAppendFile1(folderName, fileName, content) {
// get list of folders with matching name
var folderList = DriveApp.getFoldersByName(folderName);
if (folderList.hasNext()) {
// found matching folder
var folder = folderList.next();
// search for files with matching name
var fileList = folder.getFilesByName(fileName);
if (fileList.hasNext()) {
// found matching file - append text
var file = fileList.next();
var combinedContent = content + "\n\n" + file.getBlob().getDataAsString();
file.setContent(combinedContent);
}
else {
// file not found - create new
folder.createFile(fileName, content); // *** this creates the file without an error
}
}
}
// this should be simple and faster since the folderID is known
function createOrAppendFile2(folderID, fileName, content) {
let folder = DriveApp.getFolderById(folderID);
let file, combinedContent;
// search for files with matching name
let fileList = folder.getFilesByName(fileName);
if (fileList.hasNext()) {
// found matching file - append text
file = fileList.next();
combinedContent = content + "\n\n" + file.getBlob().getDataAsString();
file.setContent(combinedContent);
}
else {
// file not found - create new
folder.createFile(fileName, content); // Exception: Invalid argument: parent.mimeType
}
}
Exception: Invalid argument: parent.mimeType
and the situations when createOrAppendFile2(folderID, fileName, content)
occurs the error while createOrAppendFile1(folderName, fileName, content)
is used, no error occurs, I guessed that in your situation when you run createOrAppendFile2(folderID, fileName, content)
with the arguments, the folder of folderID
might not be the folder.
folderID
, I confirmed the same error of Exception: Invalid argument: parent.mimeType
.let folder = DriveApp.getFolderById(folderID);
, no error occurs. Exception: Unexpected error while getting the method or property getFolderById on object DriveApp.
occurs. So, I thought that the ID is a valid ID. But, the ID might be not the folder. In order to confirm this, how about adding a script for checking whether the value of folderID
is the ID of the folder? When this is reflected in your script, how about the following modification?
function createOrAppendFile2(folderID, fileName, content) {
const mimeType = DriveApp.getFileById(folderID).getMimeType();
if (mimeType != MimeType.FOLDER) {
throw new Error(`Your folder ID "${folderID}" is not folder ID of the folder. MimeType is ${mimeType}.`);
}
let folder = DriveApp.getFolderById(folderID);
let file, combinedContent;
let fileList = folder.getFilesByName(fileName);
if (fileList.hasNext()) {
file = fileList.next();
combinedContent = content + "\n\n" + file.getBlob().getDataAsString();
file.setContent(combinedContent);
} else {
folder.createFile(fileName, content);
}
}
folderID
is checked whether the ID is the ID of the folder. When the ID is for the folder, the script is run. When the ID is not for the folder, an error occurs.found matching file - append text
, if you want to append the text value of content
to the existing text file, combinedContent = content + "\n\n" + file.getBlob().getDataAsString();
might be required to be modified to combinedContent = file.getBlob().getDataAsString() + "\n\n" + content;
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.