我正在尝试获取保存在 Google Drive 中的文件夹和子文件夹中的文件链接的 Google Sheet 列表。它看起来应该像这样:
我在 Google 上搜索和阅读论坛已经有一段时间了,但我只找到了几个脚本示例,而且并不是所有示例都能正常工作。我发现了一个还不错的脚本,因为它会在一个文件夹中列出带有链接的文件列表。但我想获取子文件夹中带有链接的文件列表。理想情况下,所有文件夹链接和项目名称都列在一个单元格中,并使用分隔符“|”。到目前为止,我发现的最好的脚本示例之一是:
function listFilesInFolder(folderName) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["Name", "File-Id"]);
//change the folder ID below to reflect your folder's ID (look in the URL when you're in your folder)
var folder = DriveApp.getFolderById("0ByQ9Zp2ge-pzSjhKdHVWWk0wNnc");
var contents = folder.getFiles();
var cnt = 0;
var file;
while (contents.hasNext()) {
var file = contents.next();
cnt++;
data = [
file.getName(),
file.getId(),
];
sheet.appendRow(data);
};
};
并将结果显示在表格中
如果有人能帮助获取该代码的更好版本,我将不胜感激。
ps我将来还需要一些更大的脚本,我愿意为此付费。如果您有兴趣,请给我留言。
答案1
// replace Guitar_Score below with the folder for which you want a listing with all subfolders
function listFolderContents() {
var foldername = 'Guitar_Score';
// file with this name will be saved in your goolge files
var folderlisting = 'listing of folder ' + foldername;
var ss = SpreadsheetApp.create(folderlisting);
var sheet = ss.getActiveSheet();
var folders = DriveApp.getFoldersByName(foldername)
var folder = folders.next();
//listFilesInFolder(folder, sheet);
traverseFolder(folder, sheet);
};
function traverseFolder(folder, sheet) {
listFilesInFolder(folder, sheet);
var subFolders = folder.getFolders();
while (subFolders.hasNext()) {
traverseFolder(subFolders.next(), sheet);
}
}
function listFilesInFolder(folder, sheet) {
var foldername = folder.getName();
var contents = folder.getFiles();
sheet.appendRow( ['...', foldername] );
while(contents.hasNext()) {
var file = contents.next();
var name = file.getName();
var link = file.getUrl();
sheet.appendRow( [name, link] );
}
}