簡體   English   中英

無法弄清楚如何根據他們的名字中的 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 Drive 中,我試圖將文件名中的 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”和“thomas2222”

我堅持的部分是將文件復制到與之匹配的文件夾中。

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
      
    }
  }
}

前:

在此處輸入圖像描述

在此處輸入圖像描述

后:

在此處輸入圖像描述

在此處輸入圖像描述

需要啟用 Drive API 版本 2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM