获取不同文件夹中文件的ID [英] Get the IDs of the files in different folders
本文介绍了获取不同文件夹中文件的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是此
这是我正在使用的代码.
function myFunction(){const ss = SpreadsheetApp.getActive();var SSID = ss.getId();//工作表编号var电子表格文件= DriveApp.getFileById(SSID);var folderId =电子表格File.getParents().next().getId();const sh = ss.getSheetByName('Sheet4');//将其更改为工作表的名称const文件名= sh.getRange('B3:B').getValues().flat().filter(r => r!='');const ID = [];const Folder = DriveApp.getFolderById(folderId);filenames.forEach(fn => {让Files = Folder.getFilesByName(fn);while(Files.hasNext()){让文件= Files.next();IDs.push([file.getId()]);}});sh.getRange(3,3,IDs.length,1).setValues(IDs);}
如果文件位于文件夹中,则此代码可以正常工作,但不适用于子文件夹或其他文件夹.
下面是我正在处理的工作表.
解决方案
在这种情况下,为了实现您的目标,接下来的流程如何?
- 检索
folderId
中所有文件的文件列表. - 创建一个数组,将值放入电子表格.
修改脚本后,它如下所示.
修改后的脚本:
从: const ID = [];const Folder = DriveApp.getFolderById(folderId);filenames.forEach(fn => {让Files = Folder.getFilesByName(fn);while(Files.hasNext()){让文件= Files.next();IDs.push([file.getId()]);}});
至:
const文件夹= DriveApp.getFolderById(folderId);//1.检索`folderId`中所有文件的文件列表.const getFileList =(f,文件夹= [],fileList = {})=>{const fs = f.getFiles();而(fs.hasNext()){const文件= fs.next()fileList [file.getName()] = file.getId();}const fols = f.getFolders();const folderObjs = [];while(fols.hasNext())folderObjs.push(fols.next());如果(folderObjs.length> 0){folder.push(folderObjs);folderObjs.forEach(fol => getFileList(fol,文件夹,fileList));}返回fileList;};const fileList = getFileList(Folder);//2.创建一个数组,用于将值放入电子表格.const ID = filenames.map(fn => [fileList [fn] ||]]);
注意:
- 在这种情况下,假定在"B"列中不存在相同的文件名.电子表格.请注意这一点.
This is a follow-up question for this thread.
Is there a way for me to get the IDs of the file in a different folder? Below is the directory of what I'm working on.
And this is the code that I'm using.
function myFunction() {
const ss = SpreadsheetApp.getActive();
var SSID = ss.getId(); //sheet id
var spreadsheetFile = DriveApp.getFileById(SSID);
var folderId = spreadsheetFile.getParents().next().getId();
const sh = ss.getSheetByName('Sheet4'); // change that to the name of your sheet
const filenames = sh.getRange('B3:B').getValues().flat().filter(r=>r!='');
const IDs = [];
const Folder = DriveApp.getFolderById(folderId);
filenames.forEach(fn=>{
let Files = Folder.getFilesByName(fn);
while(Files.hasNext()){
let file = Files.next();
IDs.push([file.getId()]);
}
});
sh.getRange(3,3,IDs.length,1).setValues(IDs);
}
This code works fine if the files are located in a folder, but it doesn't work for subfolders or a different folder.
Below is the sheet that I'm working on.
解决方案
In this case, in order to achieve your goal, how about the following flow?
- Retrieve the file list of all files in
folderId
. - Create an array for putting values to Spreadsheet.
When your script is modified, it becomes as follows.
Modified script:
From:const IDs = [];
const Folder = DriveApp.getFolderById(folderId);
filenames.forEach(fn=>{
let Files = Folder.getFilesByName(fn);
while(Files.hasNext()){
let file = Files.next();
IDs.push([file.getId()]);
}
});
To:
const Folder = DriveApp.getFolderById(folderId);
// 1. Retrieve the file list of all files in `folderId`.
const getFileList = (f, folders = [], fileList = {}) => {
const fs = f.getFiles();
while (fs.hasNext()) {
const file = fs.next()
fileList[file.getName()] = file.getId();
}
const fols = f.getFolders();
const folderObjs = [];
while (fols.hasNext()) folderObjs.push(fols.next());
if (folderObjs.length > 0) {
folders.push(folderObjs);
folderObjs.forEach(fol => getFileList(fol, folders, fileList));
}
return fileList;
};
const fileList = getFileList(Folder);
// 2. Create an array for putting values to Spreadsheet.
const IDs = filenames.map(fn => [fileList[fn] || ""]);
Note:
- In this case, it supposes that the same file names are not existing in the column "B" of the Spreadsheet. Please be careful this.
这篇关于获取不同文件夹中文件的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文