Last active
September 13, 2023 18:50
-
-
Save Diana-Pham/ee40d2d2dc73996372747dd80dd8a71e to your computer and use it in GitHub Desktop.
Script to list all files in your Google Drive (in a Google Sheet)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createTimeDrivenTriggers() { | |
ScriptApp.newTrigger('listFilesInDrive') //run "listFilesInDrive()" every 5 minutes | |
.timeBased() | |
.everyMinutes(5) //Runs every 5 minutes | |
.create(); | |
}; | |
function listFilesInDrive() { | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
var MAX_FILES = 3; //use a safe value, don't be greedy! | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getActiveSheet(); | |
var files = DriveApp.getFiles(); | |
var data, sheet = SpreadsheetApp.getActiveSheet(); | |
var lastExecution = scriptProperties.getProperty('LAST_EXECUTION'); | |
if( lastExecution === null ){ | |
lastExecution = ''; | |
} | |
var continuationToken = scriptProperties.getProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN'); | |
var iterator = continuationToken == null ? | |
DriveApp.getFiles() : DriveApp.continueFileIterator(continuationToken); | |
if (continuationToken == null) { | |
sheet.clear(); | |
sheet.appendRow(["Name", "DateCreated", "LastUpdated", "Size", "Type", "ID"]); | |
sheet.setFrozenRows(1); | |
} | |
try { //while script has processed less than MAX_FILES | |
for( var i = 0; i < MAX_FILES && iterator.hasNext(); ++i ) { | |
var file = iterator.next(); | |
var dateCreated = formatDate(file.getDateCreated()); | |
if(dateCreated > lastExecution) | |
processFile(file, sheet); | |
} | |
} catch(err) { | |
Logger.log(err); | |
} | |
//if there is still more files after MAX_FILES were processed | |
if( iterator.hasNext() ) { | |
scriptProperties.setProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN', iterator.getContinuationToken()); | |
} else { // Finished processing files so delete continuation token | |
// Deletes all triggers in the current project. | |
var triggers = ScriptApp.getProjectTriggers(); | |
for (var j = 0; j < triggers.length; j++) { | |
ScriptApp.deleteTrigger(triggers[j]); | |
} | |
scriptProperties.setProperty('LAST_EXECUTION', formatDate(new Date())); | |
sheet.appendRow(["All files found", formatDate(new Date())]); | |
//visual confirmation all files found | |
var lastRow = sheet.getLastRow(); //turn last row YELLOW and BOLD | |
var lastRowCells = sheet.getRange(lastRow,1,1,2); | |
lastRowCells.setFontWeight("bold"); | |
lastRowCells.setBackground("#ffffb8"); | |
var HeaderRowCells = sheet.getRange("A1:H1"); //turn Header row ORANGE and BOLD | |
HeaderRowCells.setBackground("#f0b75b"); | |
HeaderRowCells.setFontWeight("bold"); | |
} | |
}; | |
function formatDate(date) { | |
return Utilities.formatDate(date, "GMT", "yyyy-MM-dd HH:mm:ss"); | |
}; | |
function processFile(file, sheet) { | |
var tempfile = file; | |
var tempsheet = sheet; | |
var fileData = [ | |
tempfile.getName(), | |
tempfile.getDateCreated(), | |
tempfile.getLastUpdated(), | |
tempfile.getSize(), | |
tempfile.getMimeType().toString(), | |
tempfile.getId() | |
]; | |
tempsheet.appendRow(fileData); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment