You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Drive按文件夹ID创建文件报错:Invalid argument: parent.mimeType

问题分析与解决方案

错误原因

抛出「Exception: Invalid argument: parent.mimeType」异常的核心原因是:你传入的folderID对应的并非实际的Google Drive文件夹,而是其他类型的Drive项目(最常见的是文件夹快捷方式)。这类对象虽然能通过DriveApp.getFolderById()获取到实例,但它的MIME类型不是标准文件夹的application/vnd.google-apps.folder,因此无法执行createFile操作。

验证方法

在createOrAppendFile2函数中添加日志代码,确认目标对象的MIME类型:

function createOrAppendFile2(folderID, fileName, content) {
  let folder = DriveApp.getFolderById(folderID);
  // 新增日志验证MIME类型
  Logger.log("Folder MIME Type: " + folder.getMimeType());
  let file, combinedContent;

  // 剩余代码不变...
}

执行后查看日志,如果输出不是application/vnd.google-apps.folder,说明传入的ID不是真实文件夹ID。

修复方案

情况1:传入的是文件夹快捷方式ID

如果目标是快捷方式,需要先获取它指向的实际文件夹ID,再重新获取文件夹对象:

function createOrAppendFile2(folderID, fileName, content) {
  let folder = DriveApp.getFolderById(folderID);
  
  // 处理快捷方式:获取实际目标文件夹
  if (folder.getMimeType() === "application/vnd.google-apps.shortcut") {
    const targetId = folder.getTargetId();
    folder = DriveApp.getFolderById(targetId);
  }

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

情况2:确认传入的是真实文件夹ID

如果日志显示MIME类型是标准文件夹,那可能是权限问题:

  • 确保当前脚本拥有该文件夹的编辑权限;
  • 如果是共享驱动器(Shared Drive)中的文件夹,需要确认脚本的授权账号具备在该共享驱动器中创建文件的权限。

另外,也可以尝试显式指定MIME类型创建文件,替代原有的createFile调用:

folder.createFile(fileName, content, MimeType.PLAIN_TEXT);

内容的提问来源于stack exchange,提问作者gbarnabic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 02:10:20