根据名称中的 ID 匹配文件后,无法弄清楚如何将文件移动到文件夹 Google 应用程序脚本

Unable to figure out how to move a file to a folder after they have been matched based on an ID that they both have in their names Google apps script

在 Google 驱动器中,我正在尝试将文件名中的 ID 与以该 ID 命名的文件夹进行比较,如果它们匹配,则将该文件移动到该文件夹​​。

程序必须解决的一些问题是:

  1. 由于 id 是根据客户的姓名生成的,因此它没有固定长度
  2. id 可以在文件名中的任何位置,而不仅仅是开头或结尾
  3. 可以有多个不同名称的文件包含相同的id,所以程序 必须循环

使这更容易的事情:

  1. 所有文件夹都在同一个父文件夹中,并以 id 作为名称,因此 程序可以使用文件夹名称列表作为 ID 列表
  2. 文件都在同一个父文件夹中,不会有其他文件在 文件夹,所以循环可以 运行 直到文件夹为空

到目前为止,我已经成功编写了一个代码:

  1. 通过 id 访问两个父文件夹
  2. 遍历文件夹,直到检查完所有文件,并将文件名推送到 名为“fileNames”的数组
  3. 遍历文件夹并将当前文件夹的名称与文件名进行比较 匹配,并将文件夹名称推送到名为“childNames”的数组

我在“files”文件夹中创建了两个文件“john1111_test.pdf”和“thomas2222_test.pdf”,并在“childName”文件夹中创建了两个文件夹“john1111”和“托马斯2222

我卡住的部分是将文件复制到它匹配的文件夹。

var childFolder = DriveApp.getFolderById('****').getFolders(); // Folder containing folders
var files = DriveApp.getFolderById('****').getFiles(); // Folder containing the files

  var fileNames = [];
   while (files.hasNext()) {
     var file1 = files.next();
     var fName = file1.getName()

     Logger.log(fName);

     fileNames.push(fName);
     
   }

  var childNames = [];
   while (childFolder.hasNext()) {
     var child = childFolder.next();
     var cdName = child.getName();

     Logger.log(cdName);
     
     childNames.push(cdName);
     
     const match = fileNames.find(element => {
       if (element.includes(cdName)) {

// This is the area that I am having issues with         
         let folderMatch = childFolder.getFoldersByName(cdName);
         let fileMatch = files.getFilesByName(element);
         folderMatch.addFile(fileMatch)


         return true;
        }
     });
     console.log(match);
   }

我收到一条错误消息,指出 foldermatch.addFile 不是一个函数。我也试过 cdName.addFile(element)。

移动文件

function movingFile() {
  const files = DriveApp.getFolderById('1jeAihkCiA--EfAl150IXrXDUa-MhYSKY');
  const folders = DriveApp.getFolderById('1xB4XTG8d1DYtthVQQqkSQCqvzv-TGWtW');
  const fldrs = folders.getFolders();
  let fldrNames = [];
  let fldrA = [];
  while (fldrs.hasNext()) {
    let fldr = fldrs.next()
    fldrNames.push(fldr.getName());
    fldrA.push(fldr);
  }
  const fs = files.getFiles();
  while (fs.hasNext()) {
    let file = fs.next();
    let idx = fldrNames.indexOf(file.getName().slice(0,file.getName().indexOf('_')));
    if(~fldrNames.indexOf(file.getName().slice(0,file.getName().indexOf('_')))) {//if folder  is present move it here
     //SpreadsheetApp.getUi().alert(file.getName().slice(0,file.getName().indexOf('_')));
      Drive.Files.update({"parents": [{"id": fldrA[idx].getId()}]}, file.getId());
    } else {//if it is not then create it first and xfer
      let f = folders.createFolder(file.getName().slice(0,file.getName().indexOf('_')));
      fldrA.push(f);
      Drive.Files.update({"parents": [{"id": f.getId()}]}, file.getId());
      fldrNames.push(file.getName().slice(0,file.getName().indexOf('_')));//added folder name to fldrNames so that it will be used again if there are more files with that name
      
    }
  }
}

之前:

之后:

需要启用驱动器 API 版本 2