Drive.Files.Copy和“父母"不工作 [英] Drive.Files.Copy and "parents" not working

查看:149
本文介绍了Drive.Files.Copy和“父母"不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Team Drives中的文件复制到新的文件夹位置,也复制到Team Drives中.我在代码的最后一行收到找不到文件"错误.已使用DriveApp.getFileByID并通过Google API Try-It中的测试来检查newFileID.

I'm trying to copy a file in Team Drives to a new folder location, also in Team Drives. I get a "File not found" error on the last line of code. The newFileID has been checked using DriveApp.getFileByID and by testing in Google API Try-It.

我认为父母"作品的格式不正确.当我尝试Google API Try-It时,文件被复制到正确的文件夹中.耶!那么Google脚本代码有什么问题呢?

I think the "parents" piece is incorrectly formed. When I try Google API Try-It, the file is copied into the correct folder. Yay! So what's wrong with the Google Script code?

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

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

Google Script code (not working):

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(有效):

The Google API Try-It code works. Here's the javascript (working):

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

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

What would be an efficient and/or correct way of using Drive.Files.Copy in Google Script code to place the copied file into a different folder?

推荐答案

与请求关联的parents元数据需要.

The parents metadata associated with the request expects a ParentReference resource for Drive API v2, which is at minimum an object with an id property and the associated fileId, e.g. {id: "some id"}.

由于您使用的是Team Drive,因此必须告诉Google您(即您的代码)知道如何处理常规& amp;之间的关联差异 Team Drives,带有supportsTeamDrives可选参数.

Since you are working with Team Drives, you must tell Google that you (i.e. your code) know how to handle the associated differences between regular & Team Drives, with the supportsTeamDrives optional parameter.

注意:

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

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.

假设代码运行程序符合条件,将给定文件复制到给定Team Drive文件夹的最简单代码是:

Assuming the code runner meets the criteria, the most simple code to copy a given file to a given Team Drive folder is:

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

其他阅读内容:

  • Standard Query Parameters (these can always be passed in the optional argument)
  • Partial Responses (aka "fields")

这篇关于Drive.Files.Copy和“父母"不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆