google-apps-script - Drive.Files.Copy 和 "parents"不工作

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

我正在尝试将团队云端硬盘中的文件复制到团队云端硬盘中的新文件夹位置。我在代码的最后一行收到“文件未找到”错误。 newFileID 已使用 DriveApp.getFileByID 进行检查并通过 Google API Try-It 进行测试。

我认为“ parent ”部分的格式不正确。当我尝试 Google API Try-It 时,文件被复制到正确的文件夹中。耶!那么 Google 脚本代码有什么问题呢?

https://developers.google.com/drive/api/v3/reference/files/copy#try-it

Google 脚本代码(不起作用):

function test() {

  // find Teacher's Learner Guides folder
  var destinationFolderId = "1qQJhDMlHZixBO9KZkkoSNYdMuqg0vBPU";

  var newFile = {
    "name": "Learner Guide - test",
    "description": "New student learner guide",
    "parents": [destinationFolderId]
  };

  // create duplicate document
  var newFileID = "1g6cjUn1BWVqRAIhrOyXXsTwTmPZ4QW6qGhUAeTHJSUs";
  var newDoc = Drive.Files.copy(newFile, newFileID);

}

Google API Try-It 代码有效。这是 JavaScript(工作):

return gapi.client.drive.files.copy({
      "fileId": "1g6cjUn1BWVqRAIhrOyXXsTwTmPZ4QW6qGhUAeTHJSUs",
      "supportsTeamDrives": true,
      "resource": {
        "parents": [
          "1qQJhDMlHZixBO9KZkkoSNYdMuqg0vBPU"
        ],
        "name": "Learner Test2"
      }
    })

在 Google 脚本代码中使用 Drive.Files.Copy 将复制的文件放入不同的文件夹中的有效和/或正确的方法是什么?

最佳答案

与请求关联的 parents 元数据需要 ParentReference Drive API v2 的资源,它至少是一个具有 id 属性和关联的 fileId 的对象,例如{id:“某个 id”}

由于您正在使用团队云端硬盘,因此您必须告诉 Google 您(即您的代码)知道如何 handle the associated differences在常规云端硬盘和团队云端硬盘之间,使用 supportsTeamDrives 可选参数。

注意:

A parent does not appear in the parents list if the requesting user is a not a member of the Team Drive and does not have access to the parent. In addition, with the exception of the top level folder, the parents list must contain exactly one item if the file is located within a Team Drive.

假设代码运行程序满足条件,将给定文件复制到给定团队云端硬盘文件夹的最简单代码是:

function duplicate_(newName, sourceId, targetFolderId) {
  if (!newName || !sourceId || !targetFolderId)
    return;
  const options = {
    fields: "id,title,parents", // properties sent back to you from the API
    supportsTeamDrives: true, // needed for Team Drives
  };
  const metadata = {
    title: newName,
    // Team Drives files & folders can have only 1 parent
    parents: [ {id: targetFolderId} ],
    // other possible fields you can supply: 
    // https://developers.google.com/drive/api/v2/reference/files/copy#request-body
  };

  return Drive.Files.copy(metadata, sourceId, options);
}

补充阅读:

关于google-apps-script - Drive.Files.Copy 和 "parents"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52244239/

相关文章:

email - 在 Gmail 中,使用 Google Apps Script,是否可以将我收到的已翻译电子邮件转发到另一个电子邮件地址?

javascript - 使用 Google Apps 脚本从 Google Drive 中的多个上传元素上传本地文件

javascript - Google Sheets 脚本 - 获取第一个空白单元格

javascript - 如何在 Google 脚本中使用 HTML 文件

php - Google Drive API - 未接收推送通知(基本身份验证)

python - 使用 Pythonanywhere 上传到 Google Drive

google-drive-api - Google云端硬盘服务帐户-查看其他帐户中的文件

google-apps-script - 如何将链接转换为文件名?

android-intent - Android OPEN_DOCUMENT_TREE 意图根位置

c# - Google.Apis.Util.Utilities.GetStringValue(System.Enum) 和 Google.Apis.Util.Utilities.GetStringValue(System.Enum) 之间的调用不明确