DriveApp.removeFile的真正目的是什么? [英] What is the real purpose of DriveApp.removeFile?

查看:148
本文介绍了DriveApp.removeFile的真正目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google AppScript是一项相当有用的服务,但有一种文件删除机制,我不明白。相同的讨论适用于文件夹删除。我已经在需要删除文件的地方回答了一些问题,但仍然有些问题对我来说并不明确。



DriveApp文档解释了 DriveApp.removeFile 的方法:


从用户云端硬盘的根目录中移除给定的文件。此方法不会删除文件,但如果文件从其所有父母中删除,则无法在云端硬盘中看到该文件,除非通过搜索或使用所有项目视图。


我尝试用这样的测试脚本来检查这种方法的行为:

  function remove(){
var files = DriveApp.getFilesByName(file_to_remove);
while(files.hasNext())
DriveApp.removeFile(files.next());
}

但是这段代码的结果是只删除 (我没有使用客户端),但文件(如文档中所示)仍然存在于所有项目中。



删除文件的另一种方法是将其设置为垃圾箱,因此在第一次手动清除垃圾箱时实际上将其删除:

  function trash(){
var files = DriveApp.getFilesByName(file_to_remove);
while(files.hasNext())
files.next()。setTrashed(true);
}

但是,我不喜欢这种方法(该文件仍然使用部分我的 - 极其有限的:P - 配额)。

最后一种方法(这就是我现在使用的)雇用高级Google服务,例如:

 函数advanced_remove(){
var files = DriveApp.getFilesByName(file_to_remove);
while(files.hasNext())
Drive.Files.remove(files.next()。id);
}

但我不喜欢的是服务的使用操作次数有限)。也可以通过 Drive 对象来清空垃圾箱( Drive.emptyTrash()),如果我使用前面的例子)

恕我直言,文件删除是一项非常基础的操作,应该通过标准的DriveApp API提供,而不是采用不完全删除文件或要求我手动清空垃圾。



我的问题是:

$ ul

  • removeFile 的真正目的是什么?什么意思是从它的所有父母(我假设父母是包含文件的文件夹)中删除?

  • 是否有任何实际的方法来彻底删除文件而不通过垃圾箱而不使用广告服务?

  • 我不是以英语为母语的人:也许Google Developers已实施考虑语义删除和删除之间的区别


  • 解决方案

    将Google云端硬盘文件夹视为附加到文件的标签,这是我们在Gmail中添加标签的方式。这就是他们的实际情况,他们不像硬盘上的目录那样工作。可能存在一个没有任何标签的文件,也可能有多个标签。 folder.remove(file ) 从驱动器中删除该文件夹的标签。和 DriveApp.remove(file) 的功能与标签My Drive相同。 从其所有父母中删除意味着您从文件中删除所有标签,包括我的云端硬盘。您仍然有该文件。


    是否有任何实际的方法可以完全删除文件,而无需通过垃圾桶和不使用高级服务?


    不,没有。考虑到脚本是共享的,其中一些是恶意的,脚本文件删除是一个危险的操作。因此,Google决定以这种方式限制可能的损害,因为如果不明确启用高级服务,则不允许通过GAS彻底删除。

    Google AppScript is quite an useful service but there is a mechanism for files deletion that I don't understand. The same discussion applies to folder removal. I've replied some questions on SO where file removal was required, but there is still something that is not clear to me.

    The DriveApp documentation explains the method DriveApp.removeFile:

    Removes the given file from the root of the user's Drive. This method does not delete the file, but if a file is removed from all of its parents, it cannot be seen in Drive except by searching for it or using the "All items" view.

    I tried to inspect the behavior of such method with some testing script like this one:

    function remove() {
      var files = DriveApp.getFilesByName("file_to_remove");
      while (files.hasNext()) 
        DriveApp.removeFile(files.next());
    }
    

    but the result of this code is to remove only visually the file from my Drive web-interface (I'm not using clients), but the file (as from the documentation) still exist in "All Items".

    Another way to remove the file is by setting it as trashed, thus it is actually removed the first time I clear the trash bin manually:

    function trash() {
      var files = DriveApp.getFilesByName("file_to_remove");
      while (files.hasNext()) 
        files.next().setTrashed(true);
    }
    

    but still, I do not like this approach (the file is still using part of my - extremely limited :P - quota).

    A final method (that is what I'm using right now) employes the Advanced Google Services, for example:

    function advanced_remove() {
      var files = DriveApp.getFilesByName("file_to_remove");
      while (files.hasNext()) 
        Drive.Files.remove(files.next().id);    
    }
    

    But what I don't like is the usage of the Services (that are limited in number of operations). Also it is possible through the Drive object to empty the trash bin (Drive.emptyTrash(), if I'm using the previous example)

    IMHO the file removal is quite a basical operation that should be available through standard DriveApp API, and not by employing different approaches that do not completely remove the file or do require me to manually empty the trash.

    My questions are:

    • what is the real purpose of removeFile? And what means removed from all of its parents (I'm assuming parents are folders that contain the files)?
    • is there any actual way to remove completely the file without passing by the trash bin and without using advenced services?
    • I'm not a native English speaker: Maybe Google Developers have implemented the methods considering a "semantic" difference between remove and delete?

    解决方案

    Think of Google Drive "folders" as labels attached to a file, the way that we attach labels in Gmail. This is what they actually are, they do not function like directories on a hard drive. A file may exist that does not have any labels, or it can have multiple labels. The method folder.remove(file) removes the label of that folder from the drive. And DriveApp.remove(file) does the same but for the label "My Drive". Being "removed from all of its parents" means you remove all labels from the file, including My Drive. You still have the file.

    is there any actual way to remove completely the file without passing by the trash bin and without using advanced services?

    No, there is not. Scripted file deletion is a dangerous operation, considering that scripts are shared and some of them are malicious. So Google decided to limit the possible damage in this way, by not allowing outright deletion through GAS without explicitly enabled advanced services.

    这篇关于DriveApp.removeFile的真正目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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