如何使用Google Apps脚本在个人Google云端硬盘和小组云端硬盘之间移动文件? [英] How can I use Google Apps Script to move files between personal Google Drives and Team Drives?

查看:63
本文介绍了如何使用Google Apps脚本在个人Google云端硬盘和小组云端硬盘之间移动文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我通过Google的

Recently, I gained access to Google Team Drive via Google's Team Drive early adopter program.

创建了一个Google文档文件,其名称为世界,您好!,然后编写了一个简短的Google Apps脚本函数,该函数使用

I created a Google Docs file called Hello, world!, and then wrote a short Google Apps Script function which uses an addFile() method to update which Google Drive folder the file is attached to:

function move_or_link_file() {
  var source = DriveApp.getFolderById("<sourceID>");
  var fileiter = source.getFilesByName("Hello, world!");
  var dest = DriveApp.getFolderById("<destID>");
  while (fileiter.hasNext()) {
    var file = fileiter.next();
    dest.addFile(file);
  }
}

通常,Google云端硬盘文件夹的网址与以下格式匹配: https://drive.google.com/drive/folders/<alphanumericID> .尽管可能有点不雅致,但我可以通过在网络浏览器中打开Goog​​le Drive文件夹的不同组合,选择文件夹URL的< alphanumericID> 部分来在各种操作场景和条件下测试我的代码,然后手动将此字符串的值复制并粘贴到< sourceID> < destID> .

Typically, a Google Drive folder has a URL which matches the following pattern: https://drive.google.com/drive/folders/<alphanumericID>. Although it's perhaps a bit inelegant, I can test my code under various operating scenarios and conditions by simply opening different combinations of Google Drive folders in a web browser, selecting the <alphanumericID> portion of the folder URL, and then manually copy-and-pasting values for this string into <sourceID> and <destID>.

经过测试,我能够识别出四种不同的输入条件,它们会导致三种不同的行为:

After testing, I am able to identify four different input conditions which result in three distinct behaviors:

在这种情况下,脚本的行为就像创建符号链接一样: Hello,world!文件现在出现在两个目录中.请注意,它实际上是同一文件,不是两个相同的副本:例如,如果我打开文档,则文档URL(如文件夹URL)也遵循以下模式: https://docs.google.com/document/d/<documentID>/edit .我可以说文件是相同的,因为当我打开它时,文档的URL共享相同的< documentID> ,而不管我使用哪个父文件夹来访问它.

In this case, the script behaves in effect as if it's creating a symbolic link: the Hello, world! file now appears in both directories. Note that it really is the same file, not two identical copies: for example, if I open the document, then the document URL, like the folder URLs, also follows a pattern: https://docs.google.com/document/d/<documentID>/edit. I can tell the file is the same because when I open it, the URL for the document shares the same <documentID>, regardless of which parent folder I use to access it.

对于第一种情况,我还可以通过简单地在行的末尾附加 source.removeFile(file); 这行来使脚本的行为更像 mv 命令.文件迭代器循环的结尾.

For case 1, I can also get the script to behave more like a mv command by simply appending an additional line, source.removeFile(file); to the end of the file iterator loop.

在这种情况下,即使没有额外的

In this case, the script behaves like a mv command by default, rather than as a symbolic link, even without the additional call to the removeFile() method that I mentioned in case 1: i.e., the Hello, world! file simply disappears from my personal drive and reappears in the Team Drive.

这会导致来自Google Apps脚本的错误消息:无法在Team Drive项目上使用此操作.(第7行,文件"move_or_link_file").

This results in an error message from Google Apps Script: Cannot use this operation on a Team Drive item. (line 7, file "move_or_link_file").

与情况3相同的错误.

现在这是真正很奇怪的部分:GSuite图形用户界面(即,当您通过网络浏览器访问Google云端硬盘文件和文件夹时所使用的内容)通过弹出窗口提供了 Move 命令右键单击文件时出现的窗口.类似于Unix的 mv 命令的GUI版本在上述所有四种情况下的行为都相同:在个人驱动器或团队驱动器之间来回移动文件夹,或在驱动器内部,它可以正常工作,并且每次都将文件移动到您期望的位置.

Now here is the really weird part: the GSuite graphical user interface (i.e., what you are using when you access Google Drive files and folders via the web browser) offers a Move command via a popup window that appears when you right-click on a file. This GUI version of the Unix-like mv command behaves identically for all four of the above cases: it doesn't matter whether you are moving a folder back and forth between a personal drive or team drive, or internally within a drive, it works correctly and moves the file to where you would expect it to go, every time.

因此,我想通过Google的API实现 mv 命令必须是 possible ,因为他们显然已经为GUI用户完成了此操作界面.

So, I presume it must be possible to implement a mv command via Google's API, somehow, given that they've evidently done it already for users of the GUI interface.

因此,我的问题是:鉴于有可能凭经验在个人驱动器和团队驱动器中的文件夹的任意组合之间来回移动文件,实际上我将如何使用仅提供的API调用来做到这一点?通过Google Apps脚本?

Thus my question: given that it's empirically possible to move files back and forth between arbitrary combinations of folders in personal drives and team drives, how would I actually do it, using only the API calls provided by Google Apps Script?

还有一个额外的问题:假设与案例1相似,我不是想在同一Team Drive中的两个不同文件夹之间移动文件,而是想创建一个符号链接,将文件附加到两个文件夹上,怎么办?我也使用Google API来做到这一点吗?(即,如何使案例3表现得更像案例1?)

Also, a bonus question: suppose that, similar to Case 1, instead of moving a file between two different folders in the same Team Drive, I actually wanted to create a symbolic link attaching the file to both folders--how would I use the Google API to do that as well? (I.e., how can I get Case 3 to behave more like Case 1?)

推荐答案

Google小组云端硬盘直到最近才开始允许总体上使用脚本.我可以想象,如果情况2不是您想要的,那么您可以实现的文件移动.团队驱动器上仍然存在一些限制(例如,您不能移动文件夹).

The Google Team Drives only recently started allowing scripts in general. I would imagine the file move you were able to achieve in case 2 is not even intended. There are still several limitations on the team drive (for example you cannot move folders).

对于情况1,我可以简单指出您的脚本实际上不是移动命令.您实际上应该将Google云端硬盘文件夹想象为Gmail标签.一个文件根本没有文件夹.您的脚本只为文件分配了一个标签,因此它可以显示在许多文件夹中(就像电子邮件可以包含许多Gmail标签并显示在每个标签的文件夹"中一样.)

For Case 1 I can simply point out that your script is not actually a move command. You should actually imagine Google Drive folders as Gmail tags. A file can have no folders at all. Your script merely assigns a tag to the file and as such it can appear in many folders (just like an email can have many Gmail tags and appear in each tags "folder").

在情况2中可以使用,因为Team Drive是与您的个人驱动器不同的实体.本质上,当您将其添加到团队驱动器时,您必须放弃文件的所有权.据我所知,团队精神认为添加文件意味着应将其从所有其他父级中删除.我想这就是为什么在情况3和4中您不能移动任何物品的原因.所有者是团队驱动器本身,但是命令是作为Gsuite的常规用户发送的.

It works in case 2 because Team Drive is a seperate entity from your personal drive. In essence, when you added it to the team drive you had to give up the ownership of the file. As far as I have seen, team drive considers that adding a file to it means that it should be removed from all other parents. I would assume that is why in cases 3 and 4 you cannot move any items. The owner is the team drive itself, however the commands are being sent as a regular Gsuite user.

Drive REST api最近(至三月初)已更新,可与团队驱动器一起使用:

Drive REST api was recently (~begining of March) updated to work with team drives: https://developers.google.com/drive/v3/web/about-teamdrives so I believe that technically what you are looking for can be done, however considering there are still several limitations on team drives, I don't think it will be documented as well as it could be.

这篇关于如何使用Google Apps脚本在个人Google云端硬盘和小组云端硬盘之间移动文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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