问题描述
是否可以创建一个 Google Apps 脚本来列出:
Is it possible to create a Google Apps Script which would list :
1) 特定主文件夹(及其每个相关子文件夹)内的所有文件名?
例子:
根照片 2.jpg
根照片 3.jpg
...
根照片2010 2.jpg
RootPhotos2010 2.jpg
根照片20102010-02 3.jpg
RootPhotos20102010-02 3.jpg
...
2) 与 1) 相同,但只有以.ini"结尾的文件名(例如,我有一些不需要的.picasa.ini"文件在我的在线谷歌驱动器中)?
2) same than with 1) but only filenames finishing by ".ini" (as for instance I have some unwanted ".picasa.ini" files that are inside my online google drive) ?
推荐答案
既然你的驱动器中有(真的)很多文件和文件夹,我想首先要做的是了解所有文件夹和如果您的驱动器中有子文件夹,下面的脚本将在您的电子表格中创建第二张工作表,并显示所有文件夹及其各自的树 + url.
Since it appears you have (really) a lot of files and folders in your drive, I guess the first thing to do would be to know all the folders and subfolders you have in your drive, the script below will create a second sheet in your spreadsheet and show all the folders and their respective trees + urls.
它还以秒为单位显示总执行时间,我不必提醒你这个值不应该超过+-300秒......
It shows also the total execution time in seconds, I don't have to remind you that this value should not exceed +-300 seconds...
所以我建议您尝试一下并在此处报告结果时间,然后我们才能更进一步.
So I suggest you try it and report the resulting time here before we can go further.
function listFolders(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
if(!ss.getSheetByName('Sheet2')){ss.insertSheet(1).setName('Sheet2')};
var sh = ss.getSheetByName('Sheet2');
var start = new Date();
var dateString = Utilities.formatDate(new Date(),Session.getTimeZone(), 'MMM-dd-yyyy');
var topFolder = DocsList.getRootFolder() ; // start point
var foldersArray = [];
foldersArray = getFolders(topFolder.getName().replace('Root','MyDrive'),topFolder,foldersArray);
foldersArray.unshift(['Folders url','path','# SubFolders']);
// Logger.log(foldersArray)
var l = foldersArray.length
var duration = (new Date().getTime()-start.getTime())/1000;
var durationString = Utilities.formatString("%01.1f", duration)
sh.clear();
sh.setColumnWidth(2,500);
sh.getRange(1,1,1,foldersArray[0].length).setBackground('#ffffaa').setBorder(true,true,true,true,true,true).setFontWeight('bold');
sh.getRange(1,1,l,foldersArray[0].length).setValues(foldersArray).setVerticalAlignment('middle').setWrap(false);
sh.getRange(l+1,2,1,foldersArray[0].length-1)
sh.getRange(l+1,1).setFontColor('grey').setFontSize(9).setVerticalAlignment('middle').setWrap(true).setHorizontalAlignment('center').setValue('execution time: '+durationString+' Seconds');
sh.setFrozenRows(1);
}
function getFolders(path, container,arrayin) {
var folders = container.getFolders(0, 500);
var folderCount = folders.length;
if(path=='MyDrive'){arrayin.push(['https://drive.google.com/?hl=fr&tab=wo#my-drive',path,folderCount])}
else{arrayin.push([container.getUrl(),path,folderCount])}
for (var i=0;i<folders.length;i++) {
var thisFolder = folders[i].getName();
var thisPath = path+"/"+thisFolder;
getFolders(thisPath,folders[i],arrayin)
}
return arrayin;
}
这篇关于如何使用 GAS 列出主文件夹中的所有文件(或仅某些类型的文件)及其在 Google Drive 中的所有子文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!