简体   繁体   English

如何使用Google AppScript删除Google云端硬盘中的特定文件夹

[英]How to delete a specific folder in Google Drive using Google AppScript

If you're just trying to delete a folder, nothing fancy, you can use this. 如果您只是想删除一个文件夹,别无所求,则可以使用它。

var folderIteratorName = DriveApp.getRootFolder().getFoldersByName("Folder Name"); // folder iterator
folderIteratorName.next().setTrashed(true);

Background info follows. 背景信息如下。 The DriveApp getFolder methods return a Folder Iterator, whose documentation gives you only three methods. DriveApp的getFolder方法返回一个Folder Iterator,其文档仅提供三种方法。 What's not mentioned in there is that you must invoke the .next() method, even though the iterator has only one item in this case, to actually get the folder, which you can then invoke setTrashed(true) on to get rid of it. 在这里没有提到的是,即使迭代器在这种情况下只有一个项目,您也必须调用.next()方法才能实际获取该文件夹,然后可以调用setTrashed(true)摆脱该文件夹。 。

I couldn't find an answer to this simple question for two days until I ran across a bit of code by ScampMichael in a tangentially related question. 在我两天都找不到ScampMichael的一个与切向相关的问题的代码之前,我找不到这个简单问题的答案。

function myFunction() {
var files = DriveApp.getFilesByName('data');
 while (files.hasNext()) {
   files.next().setTrashed(true);
   }
  }

The documentation for Folder lists removeFolder(child) as an option but it isn't made clear how to actually invoke this as there are few examples for any of the DriveApp, Folder, or FolderIterator classes provided in the documentation. Folder的文档列出了removeFolder(child)作为一个选项,但是由于该文档中提供的DriveApp,Folder或FolderIterator类的示例很少,因此不清楚如何实际调用它。

DriveApp.removeFolder("folder name");

Does not work either because removeFolder expects a 'child'. 也不起作用,因为removeFolder需要一个“孩子”。 Conceptually most programmers know what a child is but finding documentation for what the 'child' data structure is or how precisely to specify it for that method isn't made clear in the documentation, nor are any examples provided. 从概念上讲,大多数程序员都知道孩子是什么,但是在文档中并未明确说明“孩子”数据结构是什么或为该方法指定精确度的文档,也未提供任何示例。

If anyone has a more elegant solution please share. 如果有人有更好的解决方案,请分享。 I've made this question and answer mainly for others with a similar problem to mine to find just so they have at least one method that works. 我主要针对其他遇到类似问题的人进行了问答,以便他们找到至少一种可行的方法。

You don't need to use getRootFolder() . 您不需要使用getRootFolder()

Other than that, there is no better, faster way to delete a folder with DriveApp . 除此之外,没有更好,更快的方法通过DriveApp删除文件夹。 In Google Drive, you can name files and folders the exact same name. 在Google云端硬盘中,您可以使用完全相同的名称命名文件和文件夹。 You can have 100 folders all named "My Folder". 您可以有100个文件夹,都名为“我的文件夹”。 You can have 500 files named "Untitled spreadsheet". 您可以有500个名为“无标题电子表格”的文件。 This is why there must be a folder iterator and file iterator. 这就是为什么必须有一个文件夹迭代器和文件迭代器的原因。

function deleteFolder(folderName) {
  if (folderName===undefined) {
    folderName = "Test"
  };

  var folderToDelete = DriveApp.getFoldersByName(folderName).next();
  var returnedFolder = folderToDelete.setTrashed(true);
  Logger.log('returnedFolder is trashed: ' + returnedFolder.isTrashed());
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 AppScript 在 Google Drive 中使用 MimeType 在文件夹中创建文件 - Create a file inside a folder using MimeType inside Google Drive using AppScript 如何使用Google Appscript删除(日历或Gmail)任务? - How to delete a (calendar or gmail) task using google appscript? 如何使用共享的Google驱动器文件夹上的Google App脚本删除特定文件夹中的特定类型的文件(CSV文件) - How do I delete a specific type of file (CSV files) within a specific folder with google app script on a shared google drive folder 如何使用Google App脚本在Google驱动器中的特定文件夹中生成Google电子表格的JSON? - how to generate json of google spreadsheet at specific folder in google drive using google app script? 使用Google Appscript选择文档中的特定表格 - Selecting specific table in a document using Google Appscript 如何使用 Google Script 在特定的 Google Drive 文件夹中创建新的 PDF - How to create new PDF in specific Google Drive folder using Google Script Google AppScript - 使用 appscript 更新谷歌表单 - Google AppScript - Update google Form using appscript Google Appscript 使用 for 循环和 if - Google Appscript using for loop with if 如何使用 Google Apps 脚本在 Google Drive 中移动文件夹 - How to move a folder in Google Drive using Google Apps Script 根据Google表格中的列表文件夹删除或删除Google驱动器上的文件夹 - Remove or delete folder on google drive base on list folder in google sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM