使用 Google Apps 脚本在所有文件夹(和子文件夹)中查找 Docs/Slides/Sheets
Using Google Apps Script to find Docs/Slides/Sheets in all folders (and subfolders)
从文件夹的 link 开始,我试图获取有关该文件夹中所有 Docs/Slides/Sheets、子文件夹(以及子文件夹的子文件夹)的信息。
通过一些在线搜索,我终于得到了一些可以找到所有文件的代码,但我需要将其修改为仅查找文档、幻灯片和表格。最好的方法是什么?
这是我目前使用的代码:
function generateFolderTree(my_id) {
var parentFolder = DriveApp.getFolderById(my_id);
var f = parentFolder.getFiles();
Logger.log(f.next().getName())
getChildFolders(parentFolder);
}
function getChildFolders(parent) {
var childFolders = parent.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
var files = childFolder.getFiles();
while (files.hasNext()) {
Logger.log(files.next().getName()); // Print list of files inside the folder
}
getChildFolders(childFolder); // Recursive call for any sub-folders
}
}
稍微尝试一下,我肯定你可以将它实现到你的代码中;)
https://developers.google.com/apps-script/reference/base/mime-type
// Use MimeType enum to log the name of every Google Doc in the user's Drive.
var docs = DriveApp.getFilesByType(MimeType.GOOGLE_DOCS);
while (docs.hasNext()) {
var doc = docs.next();
Logger.log(doc.getName())
}
// Use plain string to log the size of every PNG in the user's Drive.
var pngs = DriveApp.getFilesByType('image/png');
while (pngs.hasNext()) {
var png = pngs.next();
Logger.log(png.getSize());
}
function myFunction()
{
var folders = DriveApp.getFoldersByName("Projects");
var folder = folders.next();
var sheets = getFilesInFolderByType(folder, MimeType.GOOGLE_SHEETS);
var docs = getFilesInFolderByType(folder, MimeType.GOOGLE_DOCS);
for each (var sheet in sheets)
{
console.log(sheet);
}
for each (var doc in docs)
{
console.log(doc);
}
}
function getFilesInFolderByType(folder, type)
{
var files = []
var filesInFolder = folder.getFilesByType(type);
while (filesInFolder.hasNext())
{
files.push(filesInFolder.next());
}
// Use method to recurse through sub folders.
var subFolders = folder.getFolders();
while (subFolders.hasNext())
{
var fileArray = getFilesInFolderByType(subFolders.next(), type);
files.concat(fileArray);
}
return files;
}
这是我最后使用的代码,它似乎可以完成我所做的一切 wanted/needed:
function getFolders() {
var my_link = Browser.inputBox('Enter Folder Link', 'Paste a folder link below...', Browser.Buttons.OK);
var my_id = getIdFromUrl(my_link);
fcount = 2;
SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for SLIDES.", "WORKING...",600);
generateFolderTree(my_id,MimeType.GOOGLE_SLIDES);
SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for DOCS.", "WORKING...",600);
generateFolderTree(my_id,MimeType.GOOGLE_DOCS);
SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for SHEETS.", "WORKING...",600);
generateFolderTree(my_id,MimeType.GOOGLE_SHEETS);
SpreadsheetApp.getActiveSpreadsheet().toast("Your search is complete!", "FINISHED!");
};
// -------------------------------------------------------------------------------
function generateFolderTree(my_id,type) {
var parentFolder = DriveApp.getFolderById(my_id);
var f = parentFolder.getFilesByType(type);
while (f.hasNext()) {
file = f.next();
display(file);
}
getChildFolders(parentFolder,type);
}
function getChildFolders(parent,type) {
var childFolders = parent.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
var files = childFolder.getFilesByType(type);
while (files.hasNext()) {
mfs = files.next();
display(mfs);
}
getChildFolders(childFolder,type); // Recursive call for any sub-folders
}
}
从文件夹的 link 开始,我试图获取有关该文件夹中所有 Docs/Slides/Sheets、子文件夹(以及子文件夹的子文件夹)的信息。
通过一些在线搜索,我终于得到了一些可以找到所有文件的代码,但我需要将其修改为仅查找文档、幻灯片和表格。最好的方法是什么?
这是我目前使用的代码:
function generateFolderTree(my_id) {
var parentFolder = DriveApp.getFolderById(my_id);
var f = parentFolder.getFiles();
Logger.log(f.next().getName())
getChildFolders(parentFolder);
}
function getChildFolders(parent) {
var childFolders = parent.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
var files = childFolder.getFiles();
while (files.hasNext()) {
Logger.log(files.next().getName()); // Print list of files inside the folder
}
getChildFolders(childFolder); // Recursive call for any sub-folders
}
}
稍微尝试一下,我肯定你可以将它实现到你的代码中;)
https://developers.google.com/apps-script/reference/base/mime-type
// Use MimeType enum to log the name of every Google Doc in the user's Drive.
var docs = DriveApp.getFilesByType(MimeType.GOOGLE_DOCS);
while (docs.hasNext()) {
var doc = docs.next();
Logger.log(doc.getName())
}
// Use plain string to log the size of every PNG in the user's Drive.
var pngs = DriveApp.getFilesByType('image/png');
while (pngs.hasNext()) {
var png = pngs.next();
Logger.log(png.getSize());
}
function myFunction()
{
var folders = DriveApp.getFoldersByName("Projects");
var folder = folders.next();
var sheets = getFilesInFolderByType(folder, MimeType.GOOGLE_SHEETS);
var docs = getFilesInFolderByType(folder, MimeType.GOOGLE_DOCS);
for each (var sheet in sheets)
{
console.log(sheet);
}
for each (var doc in docs)
{
console.log(doc);
}
}
function getFilesInFolderByType(folder, type)
{
var files = []
var filesInFolder = folder.getFilesByType(type);
while (filesInFolder.hasNext())
{
files.push(filesInFolder.next());
}
// Use method to recurse through sub folders.
var subFolders = folder.getFolders();
while (subFolders.hasNext())
{
var fileArray = getFilesInFolderByType(subFolders.next(), type);
files.concat(fileArray);
}
return files;
}
这是我最后使用的代码,它似乎可以完成我所做的一切 wanted/needed:
function getFolders() {
var my_link = Browser.inputBox('Enter Folder Link', 'Paste a folder link below...', Browser.Buttons.OK);
var my_id = getIdFromUrl(my_link);
fcount = 2;
SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for SLIDES.", "WORKING...",600);
generateFolderTree(my_id,MimeType.GOOGLE_SLIDES);
SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for DOCS.", "WORKING...",600);
generateFolderTree(my_id,MimeType.GOOGLE_DOCS);
SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for SHEETS.", "WORKING...",600);
generateFolderTree(my_id,MimeType.GOOGLE_SHEETS);
SpreadsheetApp.getActiveSpreadsheet().toast("Your search is complete!", "FINISHED!");
};
// -------------------------------------------------------------------------------
function generateFolderTree(my_id,type) {
var parentFolder = DriveApp.getFolderById(my_id);
var f = parentFolder.getFilesByType(type);
while (f.hasNext()) {
file = f.next();
display(file);
}
getChildFolders(parentFolder,type);
}
function getChildFolders(parent,type) {
var childFolders = parent.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
var files = childFolder.getFilesByType(type);
while (files.hasNext()) {
mfs = files.next();
display(mfs);
}
getChildFolders(childFolder,type); // Recursive call for any sub-folders
}
}