Drive.Files.Copy和“父母”无法正常工作

问题描述 投票:0回答:2

我正在尝试将Team Drives中的文件复制到新的文件夹位置,也在Team Drives中。我在最后一行代码中收到“找不到文件”错误。已使用DriveApp.getFileByID和Google API试用版中的测试检查newFileID。

我认为“父母”的作品不正确。当我尝试使用Google API Try-It时,该文件将被复制到正确的文件夹中。好极了!那么Google Script代码有什么问题?

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

Google Script代码(不工作):

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 Script代码中使用Drive.Files.Copy将复制的文件放入不同的文件夹的有效和/或正确方法是什么?

google-apps-script google-drive-api
2个回答
1
投票

与请求相关联的parents元数据需要用于Drive API v2的ParentReference资源,该资源至少是具有id属性和关联的fileId的对象,例如, {id: "some id"}

由于您正在与Team Drives合作,因此您必须告诉Google您(即您的代码)知道如何使用handle the associated differences可选参数在常规和团队驱动之间进行supportsTeamDrives

注意:

如果请求用户不是Team Drive的成员且无权访问父级,则父级不会出现在父级列表中。此外,除顶级文件夹外,如果文件位于Team Drive中,则父列表必须包含一个项目。

假设代码运行器符合条件,将给定文件复制到给定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);
}

补充阅读:


0
投票

这是在Team Drives中复制文件的解决方案。 @tehhowch有一个关于需要可选参数的重要部分(你需要使用复制API v2的所有三个参数)。然后“parents”参数需要File对象,而不是字符串。下面的代码通过复制文件并将其移动到另一个Team Drives文件夹来工作。

function test() {

  // find Teacher's Learner Guides folder
  var destFolderId = "1qQJhDMlHZixBO9KZkkoSNYdMuqg0vBPU";
  var originalDocID = "1g6cjUn1BWVqRAIhrOyXXsTwTmPZ4QW6qGhUAeTHJSUs";
  var destFolder = Drive.Files.get(destFolderId, {"supportsTeamDrives": true});

  var newFile = {
    "fileId": originalDocID,
    "parents": [
      destFolder // this needed to be an object, not a string
    ]
  };
  var args = {
    "resource": {
      "parents": [
        destFolder // this needed to be an object, not a string
      ],
      "title": "new name of document here"
    },
    "supportsTeamDrives": true
  };

  // create duplicate Learner Guide Template document
  var newTargetDoc = Drive.Files.copy(newFile, originalDocID, args);
}
© www.soinside.com 2019 - 2024. All rights reserved.