从新文件自动创建新的 Google Drive 文件夹

Automatically create new Google Drive folder from new files

我目前有一个 Zapier 进程 运行,它会自动创建 3 个从表单上传到我的 Google 驱动器中名为 "New Users" 的文件夹中的新文件。每个文件都被格式化为 firstname_lastname-filename.ext 但这在组织方面并不好。

相反,我想动态创建一个标记为 firstname_lastname 的新文件夹,其中包含每次进来时都带有相同 firstname_lastname 的 3 个新文件,而不是使用通用的 [=22] =] 包含数百或数千个文件的文件夹。

不幸的是,我是一个非常新手的程序员,所以我不太确定如何使用 Apps 脚本来解决这个问题。

有什么建议吗?

我想过类似的事情:

var files = DriveApp.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    Logger.log(file.getName());
    if file.getName().includes("firstname_lastname") { // Check if a file name contains the string firstname_lastname
       var folders = DriveApp.getFolders();
       while (folders.hasNext()) {
          var folder = folders.next();
          Logger.log(folder.getName());
          if folder.getName().includes("firstname_lastname") { // Check if a folder exists with a name that contains the string firstname_lastname
              makeCopy(file.getName(), folder.getName()) // if said folder exists, make a copy of the the file and move it to that folder
          } else { // if said folder does not exist... 
          var newFolderName = file.getName() // let the newFolderName be the same name as the file (I know this isn't right if I want the folder name to be firstname_lastname without the actual uploaded file name plus extension)
          createFolder(newFolderName); // then create a folder that has the name newFolderName
          makeCopy(file.getName(), folder.getName(newFolderName)) // then make a copy of the file and put it into the folder
        } 
      }   
    }
  }

您可能需要先建立根文件夹,"New Users"。我为流程创建了一个片段:

//New Users Folder
var rootFolder = DriveApp.getFolderById("FOLDER_ID");

function myFunction() {
  var files = rootFolder.getFolders();
  while (files.hasNext()) {
    var file = files.next();
    Logger.log(file.getName());

    if(file.getName() == fileuploadName){
    //copy file if folder is existing
      file.addFile(child)
    }
    else{
    //create folder
    var newFolder =  rootFolder.addFolder(child);
      newFolder.addFile(child)
    }
 }
 Logger.log("Done")

}

当您想要动态创建文件夹然后将文件迁移到特定文件夹名称时,此流程应该没问题。

希望对您有所帮助。