【问题标题】:Copying using Drive.Files.copy is resulting as a PDF, not Google doc使用 Drive.Files.copy 复制会生成 PDF,而不是 Google 文档
【发布时间】:2018-09-11 04:36:55
【问题描述】:

我需要使用 Drive.Files.copy 函数来复制团队云端硬盘中的文件。功能是将模板 Google Doc 复制到新文件和文件夹。

下面的函数似乎是复制文件,但生成的文件是 PDF(原始文件是 Google Doc)。这可能是一些我没有看到的简单的东西。

teacherFolder 是目的地。 learnerDoc 是原始文件。 newDocc 是新文件。

function test() {
  var newFile = {
    title: "Learner Guide - test",
    description: "New student learner guide",
    mimetype: 'application/vnd.google-apps.file',
    supportsTeamDrives: true,
    kind: "drive#user",
    includeTeamDriveItems: true
  };
  // find Teacher's Learner Guides folder
  var teacherFolder = DriveApp.getFolderById('1qQJhDMlHZixBO9KZkkoSNYdMuqg0vBPU');

  // create duplicate Learner Guide Template document
  var learnerDoc = DriveApp.getFileById('1g6cjUn1BWVqRAIhrOyXXsTwTmPZ4QW6qGhUAeTHJSUs');

  //var newDocc = Drive.Files.copy(newFile, learnerDoc.getId());
  var newDocc = Drive.Files.insert(newFile, learnerDoc.getBlob(), newFile);
  var DriveAppFile = DriveApp.getFileById(newDocc.id);
  teacherFolder.addFile(DriveAppFile);
  Logger.log('file = ' + newDocc.fileExtension);
}

如何在团队云端硬盘中创建重复的 Google 文档并将其移至其他文件夹?

【问题讨论】:

  • 虽然我不确定我是否能正确理解您的情况,但您的脚本中似乎没有使用Drive.Files.copy()。如果这不是您的最新脚本,您可以更新它吗?然后,在 Google Docs 的情况下,当 getBlob() 检索文件的 blob 时,blob 的 mimeType 变为 application/pdf。我认为这是 Google 的规范。
  • 当我取消注释 Drive.Files.copy() 行时,会抛出错误,“找不到文件:1g6cjUn1BWVqRAIhrOyXXsTwTmPZ4QW6qGhUAeTHJSUs”,所以我尝试插入文件。
  • @Tanaike 副本正在运行。父母不是。文件不在父母的文件夹 ID 中。 var newFile = { "title": "Learner Guide - test", "description": "New student learning guide", "parents": [teacherFolder.getId()], "supportsTeamDrives": true, "kind": "drive #file", "includeTeamDriveItems": true }; var learnerDoc = DriveApp.getFileById('1g6cjUn1BWVqRAIhrOyXXsTwTmPZ4QW6qGhUAeTHJSUs'); var newDocc = Drive.Files.copy(newFile, learnerDoc.getId(), newFile);
  • 感谢您的回复。虽然我发布了一个显示修改点的答案,但我注意到您刚才发布为an another question。所以我删除了我的答案。如果您添加问题的更多信息,请使用编辑按钮将它们添加到您的问题中。因为您发布的答案没有解决您的问题。通过更新您的问题,其他有相同问题的用户可以看到您的问题。

标签: google-apps-script google-drive-api google-drive-shared-drive


【解决方案1】:

“找不到文件”错误的原因是您尝试访问位于团队云端硬盘中的文件,但未在可选参数中表明您的代码知道如何处理 Google 云端硬盘和团队之间的差异驱动器。

您已设置此参数,但您将其设置在与您正在插入/复制的文件相关联的元数据中,而不是作为 Drive API 的可选参数。

因此,要解决“找不到文件”错误,您需要更改元数据定义:

var newFile = {
  title: "Learner Guide - test",
  description: "New student learner guide",
  mimetype: 'application/vnd.google-apps.file',
  supportsTeamDrives: true,
  kind: "drive#user",
  includeTeamDriveItems: true
};

到元数据和参数:

const newFile = {
  title: "Learner Guide - test",
  description: "New student learner guide",
};
const options = {
  supportsTeamDrives: true,
  includeTeamDriveItems: true
};

我不确定您通过将 mimetype 提供为通用文件来尝试做什么(您应该让 Drive API 为 Copy 操作推断此内容),或者您为什么尝试设置 kind 参数,通常是对 API 响应内容的只读描述。

通过该更改,您可以将可选参数作为最后一次调用传递给客户端库方法:

var newDocc = Drive.Files.copy(newFile, learnerDoc.getId());

变成

var newDocc = Drive.Files.copy(newFile, learnerDoc.getId(), options);

相关阅读:

【讨论】:

  • 感谢@tehhowch 提供的详细信息!这正是问题所在,我现在对可选参数的使用更加清楚了。赞赏!
【解决方案2】:

感谢@Tanaike 的帮助和解答。有关此工作解决方案的更多详细信息,请访问:

Drive.Files.Copy and "parents" not working

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 2020-08-09
    • 2017-11-18
    • 2011-02-14
    • 1970-01-01
    • 2023-03-18
    • 2022-06-24
    • 2020-03-20
    • 1970-01-01
    相关资源
    最近更新 更多