一段时间以来,我一直在使用以下脚本在电子表格的一个标签中列出我的Google Drive的一个文件夹中包含的文件:
我现在想实现的是,如果可能的话,用一个类似的脚本,一次列出我的驱动器上的几个文件夹的内容。文件夹可以被称为"缩略图1","缩略图2","缩略图3","缩略图4"one_answers"缩略图5"。
另外,我想知道这个脚本是否可以在每次提交表单后使用触发器执行,该表单的响应链接到该脚本将运行的相同电子表格。每次提交此表单时,要列出的文件夹的内容可能会更改,因为表单的文件上传直接指向这些文件夹。
非常感谢。
一段时间以来,我一直在使用以下脚本在电子表格的一个标签中列出我的Google Drive的一个文件夹中包含的文件:
function List_Thumbnails() {
/* Adapted from Code written by @hubgit https://gist.github.com/hubgit/3755293
Updated since DocsList is deprecated https://ctrlq.org/code/19854-list-files-in-google-drive-folder
*/
// List all files and sub-folders in a single folder on Google Drive
// declare the folder name
var foldername = 'Thumbnails 1';
// declare this sheet
var sheet = SpreadsheetApp.getActive().getSheetByName('Thumbs Import');
// clear any existing contents
sheet.clear();
// append a header row
sheet.appendRow(["Folder","Name", "Date Last Updated", "Size", "URL", "ID", "Description", "Type", "Download", "Date Created"]);
// getFoldersByName = Gets a collection of all folders in the user's Drive that have the given name.
// folders is a "Folder Iterator" but there is only one unique folder name called, so it has only one value (next)
var folders = DriveApp.getFoldersByName(foldername);
var foldersnext = folders.next();
// Logger.log("THE FOLDER IS "+foldersnext);// DEBUG
// declare an array to push data into the spreadsheet
var data = [];
// list files in this folder
// myfiles is a File Iterator
var myfiles = foldersnext.getFiles();
// Logger.log("FILES IN THIS FOLDER"); DEBUG
// loop through files in this folder
while (myfiles.hasNext()) {
var myfile = myfiles.next();
var fname = myfile.getName();
var fdate = myfile.getLastUpdated();
var fsize = myfile.getSize();
var furl = myfile.getUrl();
var fid = myfile.getId();
var fdesc = myfile.getDescription();
var ftype = myfile.getMimeType();
var fdown = myfile.getDownloadUrl();
var fcreate = myfile.getDateCreated()
//Logger.log("File Name is "+myfile.getName()); //Logger.log("Date is "+myfile.getLastUpdated()); //Logger.log("Size is "+myfile.getSize());
//Logger.log("URL is "+myfile.getUrl()); //Logger.log("ID is "+myfile.getId()); //Logger.log("Description is "+myfile.getDescription());
//Logger.log("File Type is "+myfile.getMimeType());
// Populate the array for this file
data = [
foldersnext,
fname,
fdate,
fsize,
furl,
fid,
fdesc,
ftype,
fdown,
fcreate
];
//Logger.log("data = "+data); //DEBUG
sheet.appendRow(data);
} // Completes listing of the files in the named folder
// Now get the subfolder
// subfolders is a Folder Iterator
var subfolders = foldersnext.getFolders();
//Logger.log("THE SUBFOLDER(S) ARE"); DEBUG HEADING
// now start a loop on the SubFolder list
while (subfolders.hasNext()) {
var subfolderdata = [];
var mysubfolders = subfolders.next();
var mysubfolder = mysubfolders.getName();
//Logger.log("Subfolder name:"+mysubfolder); //DEBUG
// Get the files
// mysubfiles is a File Iterator
var mysubfiles = mysubfolders.getFiles();
//Logger.log("FILES IN THIS FOLDER"); //DEBUG HEADING
// now start a loop on the files in the subfolder
while (mysubfiles.hasNext()) {
var smyfile = mysubfiles.next();
var sfname = smyfile.getName();
var sfdate = smyfile.getLastUpdated();
var sfsize = smyfile.getSize();
var sfurl = smyfile.getUrl();
var sfid = smyfile.getId();
var sfdesc = smyfile.getDescription();
var sftype = smyfile.getMimeType();
var sfdown = myfile.getDownloadUrl();
var sfcreate = myfile.getDateCreated()
//Logger.log("Subfolder is "+foldersnext+"/"+mysubfolder);
//Logger.log("File Name is "+ smyfile.getName()); Logger.log("Date is "+ smyfile.getLastUpdated()); Logger.log("Size is "+ smyfile.getSize());
//Logger.log("URL is "+ smyfile.getUrl()); Logger.log("ID is "+ smyfile.getId()); Logger.log("Description is "+ smyfile.getDescription());Logger.log("File Type is "+ smyfile.getMimeType());
subfolderdata = [
(foldersnext+"/"+mysubfolder),
sfname,
sfdate,
sfsize,
sfurl,
sfid,
sfdesc,
sftype,
sfdown,
sfcreate
];
//Logger.log("subfolderdata = "+subfolderdata);
sheet.appendRow(subfolderdata);
}
}
}
我现在想实现的是,如果可能的话,用一个类似的脚本,一次列出我的驱动器上的几个文件夹的内容。文件夹可以被称为"缩略图1","缩略图2","缩略图3","缩略图4"one_answers"缩略图5"。
另外,我想知道这个脚本是否可以在每次提交表单后使用触发器执行,该表单的响应链接到该脚本将运行的相同电子表格。每次提交此表单时,要列出的文件夹的内容可能会更改,因为表单的文件上传直接指向这些文件夹。
非常感谢。
您的脚本只需要少量的修改就可以达到您的目标。
- 这个回答不是指定一个文件夹,然后列出文件夹中的文件和所有子文件夹,而是列出特定的文件夹,忽略子文件夹。
- 脚本可以被一个可安装的
onFormSubmit
触发
Delete:var foldername = 'Thumbnails 1';
替代:var folderNames = ['Thumbnails 1','Thumbnails 2','Thumbnails 3','Thumbnails 4','Thumbnails 5']
var folders = DriveApp.getFoldersByName(foldername);
代入:for (var i=0;i<folderNames.length;i++){
var folders = DriveApp.getFoldersByName(folderNames[i]);
删除:
- from:
// Now get the subfolder
- to:
}
(第115行)注意:第116行的右括号保留下来,并关闭在前一次编辑中开始的for
循环。
脚本
function List_ThumbnailsOnly() {
/* Adapted from Code written by @hubgit https://gist.github.com/hubgit/3755293
Updated since DocsList is deprecated https://ctrlq.org/code/19854-list-files-in-google-drive-folder
*/
// List all files in a single folder on Google Drive
// declare an array of folders
var folderNames = ['Thumbnails 1','Thumbnails 2','Thumbnails 3','Thumbnails 4','Thumbnails 5']
// declare this sheet
var sheet = SpreadsheetApp.getActive().getSheetByName('Thumbs Import');
// clear any existing contents
sheet.clear();
// append a header row
sheet.appendRow(["Folder","Name", "Date Last Updated", "Size", "URL", "ID", "Description", "Type", "Download", "Date Created"]);
// getFoldersByName = Gets a collection of all folders in the user's Drive that have the given name.
// folders is a "Folder Iterator" but there is only one unique folder name called, so it has only one value (next)
for (var i=0;i<folderNames.length;i++){
var folders = DriveApp.getFoldersByName(folderNames[i]);
var foldersnext = folders.next();
// Logger.log("THE FOLDER IS "+foldersnext);// DEBUG
// declare an array to push data into the spreadsheet
var data = [];
// list files in this folder
// myfiles is a File Iterator
var myfiles = foldersnext.getFiles();
// Logger.log("FILES IN THIS FOLDER"); DEBUG
// loop through files in this folder
while (myfiles.hasNext()) {
var myfile = myfiles.next();
var fname = myfile.getName();
var fdate = myfile.getLastUpdated();
var fsize = myfile.getSize();
var furl = myfile.getUrl();
var fid = myfile.getId();
var fdesc = myfile.getDescription();
var ftype = myfile.getMimeType();
var fdown = myfile.getDownloadUrl();
var fcreate = myfile.getDateCreated()
//Logger.log("File Name is "+myfile.getName()); //Logger.log("Date is "+myfile.getLastUpdated()); //Logger.log("Size is "+myfile.getSize());
//Logger.log("URL is "+myfile.getUrl()); //Logger.log("ID is "+myfile.getId()); //Logger.log("Description is "+myfile.getDescription());
//Logger.log("File Type is "+myfile.getMimeType());
// Populate the array for this file
data = [
foldersnext,
fname,
fdate,
fsize,
furl,
fid,
fdesc,
ftype,
fdown,
fcreate
];
//Logger.log("data = "+data); //DEBUG
sheet.appendRow(data);
}// Completes listing of the files in the named folder
} // Completes loop of Folder names
}