问题描述
是否可以创建Google Apps脚本来列出以下内容: 子文件夹)?
示例:
Root \Photos\\\02.jpg p>
Root \Photos\\\\\\\\\\\\\\\\\\\\\\\\\\\\ $ b
Root \Photos\2010\02.jpg
照片\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' / p>
...
2)只有文件名以.ini结尾(例如,我在我的谷歌在线驱动器中有一些不需要的.picasa.ini文件)?
由于看起来你的驱动器中有(很多)文件和文件夹,我想首先要做的是知道驱动器中的所有文件夹和子文件夹,下面的脚本将在电子表格中显示第二张图所有的文件夹和他们各自的树+网址。
它还显示总执行时间(以秒为单位),我不必提醒您,此值不应超过+300秒...
所以我建议你试试看,并在这里报告所得到的时间,然后才能继续。
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(); //开始点
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('gray')。setFontSize(9 ).setVerticalAlignment('middle')。setWrap(true).setHorizontalAlignment('center')。setValue('execution time:'+ durationString +'Seconds');
sh.setFrozenRows(1);
函数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;
}
Is it possible to create a Google Apps Script which would list :
1) all filenames inside a specific main folder (and in each of its related subfolders) ?
Example :
Root\Photos\02.jpg
Root\Photos\03.jpg
...
Root\Photos\2010\02.jpg
Root\Photos\2010\2010-02\03.jpg
...
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) ?
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.
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云端硬盘中列出主文件夹及其所有子文件夹中的所有文件(或仅限某些类型的文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!