DriveApp:获取文件 ID 以及父文件夹 ID/名称和文件名

Set*_*eth 2 google-apps-script google-drive-api

我正在尝试使用 DriveApp 来确定文件 ID:

  • 父文件夹名称或父文件夹 ID
  • 有问题的文件名

我想我会用这个: https://yagisanatode.com/2018/10/05/google-apps-script-get-file-by-name-with-optional-parent-folder-crosscheck/

但它总是返回:

有多个名为:(文件名)的文件。但它们都不在文件夹(文件夹名称)中。

...这不是真的。有什么想法或更简单的方法来做到这一点吗?

提前致谢。

这是一个最小的例子:

/*
* ****Get File By Name***
*
*param 1: File Name 
*param 2: Parent Folder of File (optional)
*
*returns: Dictionary of file "id" and "error" message {"id": ,"error": }
*  -if there is no error, "id" returns file id and "error" returns false
*  -if there is an error, "id" returns false and "error" returns type of error as a string for user to display or log.
*/

function getFileByName(fileName, fileInFolder){
 
  var filecount = 0;
  var dupFileArray = [];
  var folderID = "";
  
  var files = DriveApp.getFilesByName(fileName);
  
  while(files.hasNext()){
    var file = files.next();
    dupFileArray.push(file.getId());
    
    filecount++;
  };
  
  if(filecount > 1){
    if(typeof fileInFolder === 'undefined'){
        folderID = {"id":false,"error":"More than one file with name: "+fileName+". \nTry adding the file's folder name as a reference in Argument 2 of this function."}
    
    }else{
     //iterate through list of files with the same name
     for(fl = 0; fl < dupFileArray.length; fl++){
       var activeFile = DriveApp.getFileById(dupFileArray[fl]);
       var folders = activeFile.getParents();
       var folder = ""
       var foldercount = 0;
       
       //Get the folder name for each file
       while(folders.hasNext()){
         folder = folders.next().getName(); 
         foldercount++;
       };
       
       if(folder === fileInFolder && foldercount > 1){
         folderID = {"id":false,"error":"There is more than one parent folder: "+fileInFolder+" for file "+fileName}
       };
       
       if(folder === fileInFolder){
           folderID = {"id":dupFileArray[fl],"error":false};
           
       }else{
         folderID = {"id":false,"error":"There are multiple files named: "+fileName+". \nBut none of them are in folder, "+fileInFolder}
       };
     };
   };
   
  }else if(filecount === 0){
      folderID = {"id":false,"error":"No file in your drive exists with name: "+fileName};
      
  }else{ //IF there is only 1 file with fileName
    folderID = {"id":dupFileArray[0],"error":false};
    };
 
  return folderID;
};

function test() {
//My arguments for the function
var myFileName = "Invoices";
var myFileParentFolderName = "testing";
 
//Run the function
var getFileID = getFileByName(myFileName, myFileParentFolderName);
 
//Check if folder exists
if(getFileID.id === false){ //if file cannot be accurately found.
  Logger.log(getFileID.error); //alert or log error. Give option to try another FileName
}else{
  // If the file ID exists then proceed with the program. 
  Logger.log(getFileID.id);
};
}
Run Code Online (Sandbox Code Playgroud)

soM*_*rio 6

解决方案比您想象的要简单得多。

  • 在以下脚本中,根据您的具体情况调整folderIDfileName,如果此文件夹中存在具有所选名称的多个文件,它将在数组中存储所有 ID。
  • 如果文件仅存在一个,那么您将得到一个包含单个元素的数组。

解决方案:

function findFilesInfo() {

  const folderId = 'folderID';
  const fileName = 'fileName';
  
  const folder = DriveApp.getFolderById(folderId);
  const files = folder.getFilesByName(fileName);
  const fileIDs = [];

  while (files.hasNext()) {
      var file = files.next();
      fileIDs.push(file.getId());
  }

  Logger.log(fileIDs);  
}
Run Code Online (Sandbox Code Playgroud)

fileIDsfileName在 ID 的文件夹下有特定的 ID 列表folderID