//--------------------------------------------------------------------------------
// Set of functions to read (recursively) a top level folder, get the files information
// and then add the info to a sheet for the files that are there.
// USE AT YOUR OWN RISK!
// Very little error checking!!
// If a file is found - the function gets its information (line 69 currently) and writes it to the sheet
// It also changes the view permissions information to the file so that it can be accessed anywhere with the link
// There's also a description added to the file property to do a partial check and update any new files found
// Uses named ranges in the sheet to hold the URL information, and last imported date info
// Only logs to built in Logger
//--------------------------------------------------------------------------------
// The main function to extract the images from the folders
function extractImagesFull() {
start(true);
}
function extractImagesUpdate() {
start(false);
}
/*
Recursively add a list of files from a named folder to a sheet
*/
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('FullPhotoInformation');
function start(fullScan=false) {
if(fullScan){
//clear sheet if we are starting from scratch
sheet.getRange('A3:K').clear();
}
var folderId=getIdFromUrl(SpreadsheetApp.getActiveSpreadsheet().getRangeByName('RootFolderUrl').getValue());
Logger.log(folderId);
var folder=DriveApp.getFolderById(folderId);
Logger.log('Processing root folder: '+ folder.getName());
processFolder(folder,'',fullScan);
Logger.log('file scrape complete');
return;
}
function processFolder(folder,parentName,fullScan) {
Logger.log('New folder: '+folder.getName());
var contents = folder.getFiles();
if(contents.hasNext()){
addFilesToSheet(contents, folder,parentName,fullScan);
}
var subFolder = folder.getFolders();
while (subFolder.hasNext()){
processFolder(subFolder.next(),folder.getName());
}
return;
}
function addFilesToSheet(files, folder,parentName,fullScan) {
var data=[];
var folderName = folder.getName();
while (files.hasNext()) {
var file = files.next();
var tmp_filename=file.getName();
var tmp_desc=file.getDescription();
//check to see if we have found this photo before by checking description - if it's blank we haven't, so need to add it - If function times out - need to be careful here as it may not be written into sheet at timeout time
if(!file.getDescription()||fullScan){
//Check File Sharing Access - needs to be Anyone on the internet
if(file.getSharingAccess()!=DriveApp.Access.ANYONE_WITH_LINK){
//need to change it here
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK,DriveApp.Permission.VIEW);
}
//set description
file.setDescription('Found: '+new Date())
// and add into sheet by adding to data array
data.push([
//sheet.appendRow([
parentName +'/'+folderName,
file.getName(),
file.getMimeType(),
file.getDateCreated(),
file.getUrl(),
file.getLastUpdated(),
file.getDescription(),
file.getSize(),
file.getId(),
file.getSharingPermission(),
file.getSharingAccess()
]);
}
}
if(data.length>0){
sheet.getRange(sheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
//set last updated date to now
SpreadsheetApp.getActiveSpreadsheet().getRangeByName('LastImported').setValue(new Date());
SpreadsheetApp.flush();
}
function getIdFromUrl(url) {
return url.match(/[-\w]{25,}/);
}