I am using a script to show me new files in google drive so people can see that a file has been updated. The script worked fine with one or two users using the system but after migrating a few more we started getting errors about exceeding maximum execution time. Currently the script deletes the current list and recreates it each and every hour
function listFilesInFolder() {
//try {
// If you want a tree of any sub folder
//var parent = DriveApp.getFoldersByName("FOLDER_NAME").next();
// If you want to search from the top (root) folder
var files = DriveApp.getFiles();
var ss = SpreadsheetApp.openById("1XbAaoNTiRXKSUAovY-QUvnc8V2q68g7Q3z4NIH_ioTc");
var sheet = ss.getSheetByName("Sheet1");
var file,folder,folders,folderTop,newFiles,lastUpdated,difference;
var j = 0;
var data = []
var parentFolders = []
var newDate = new Date();
while (files.hasNext()) {
file = files.next();
lastUpdated = file.getLastUpdated();
difference = newDate.getTime() - lastUpdated.getTime();
if ((difference/86400000) < 100) {
folders = file.getParents();
while (folders.hasNext()) {
folder = folders.next();
folders = folder.getParents();
parentFolders.push(folder.getName())
}
folderTop = parentFolders[parentFolders.length -2]
data.push (new Array());
data[j].push(file.getName());
data[j].push(file.getLastUpdated());
data[j].push("open_in_new");
data[j].push("https://drive.google.com/file/d/"+file.getId()+"/edit");
data[j].push("folder_open");
if (file.getParents().hasNext())
{
data[j].push("https://drive.google.com/drive/u/0/folders/" + file.getParents().next().getId());
}
else
{
data[j].push("https://drive.google.com/drive/");
}
data[j].push(folderTop+"@xxx.net");
j = j+1;
}
}
sheet.deleteRows(2, sheet.getMaxRows()-1)
sheet.deleteColumns(2, sheet.getMaxColumns()-1)
sheet.clear()
sheet.appendRow(["File Name","Date Updated","View File","File Link", "Open Folder","Folder Link", "Cell" ]);
sheet.appendRow(["","","HyperlinkType(D)-iconType","", "HyperlinkType(F)-iconType","", "Permissions-Hidden"] );
var destinationRange = sheet.getRange(3, 1,j , 7);
destinationRange.setValues(data);
}
// catch (e) {
// Logger.log(e.toString());
// }
// }
function getFilesFromFolder(parent,targetSheet,topLevelName) {
var files = parent.getFiles();
var data
var file
while (files.hasNext()) {
file =files.next()
data = [
file.getName(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
file.getDescription(),
file.getLastUpdated(),
file.getMimeType(),
topLevelName+"@xxx.net"
];
targetSheet.appendRow(data);
row.push([file.getId(),
file.getName(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
file.getDescription(),
file.getLastUpdated(),
file.getMimeType()
]);
}
}
function getChildFolders(parent,targetSheet,topLevelName) {
var childFolders = parent.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
getFilesFromFolder(childFolder,targetSheet,topLevelName);
getChildFolders(childFolder,targetSheet,topLevelName);
}
// Recursive call for any sub-folders
}
Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…