Home > other >  How do I create a file in an existing Google Drive folder
How do I create a file in an existing Google Drive folder

Time:01-23

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
  }
}

CodePudding user response:

Modification points:

  • From both the error message of 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.
    • When I tested this, when I used the file ID (for example, it's spreadsheet.) which is the folder id of the valid folder as folderID, I confirmed the same error of Exception: Invalid argument: parent.mimeType.
    • Unfortunately, in the current stage, when the ID which is not a folder is used with let folder = DriveApp.getFolderById(folderID);, no error occurs.
    • For example, when the invalid folder ID is used, an error like 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?

Modified script:

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);
  }
}
  • When this script is run, first, the value of 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.

Note:

  • By the way, about 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;.

References:

  • Related