1

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

4

2 回答 2

1

The reason for the "File not found" error is that you are attempting to access a file located in a Team Drive, but do not indicate in the optional parameters that your code knows how to handle the differences between Google Drive and Team Drives.

You have set this parameter, but you set it in the metadata that is associated with the file you are inserting/copying, and not as an optional parameter to the Drive API.

Thus, to resolve the "File not found" error, you need to change the metadata definition:

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

to metadata and parameters:

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

I'm not sure what you were trying to do by supplying the mimetype as a generic file (you should let the Drive API infer this for a Copy operation), or why you try to set the kind parameter, which is generally a read-only description of the contents of an API response.

With that change, you then pass optional parameters as the last call to the client library method:

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

becomes

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

Related reading:

于 2018-09-10T20:36:55.237 回答
0

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

Drive.Files.Copy 和“父母”不起作用

于 2018-09-09T11:05:48.123 回答