使用 Drive.Files.copy 复制结果为 PDF,而不是 Google 文档

Copying using Drive.Files.copy is resulting as a PDF, not Google doc

我需要使用 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 文档并将其移动到其他文件夹?

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

"File not found" 错误的原因是您试图访问位于团队驱动器中的文件,但没有在可选参数中表明您的代码知道如何处理 Google 驱动器和团队驱动器。

您已设置此参数,但您在与您所在的文件关联的元数据中设置了它 inserting/copying,而不是作为驱动器的可选参数 API。

因此,要解决 "File not found" 错误,您需要更改元数据定义:

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

相关阅读: