I've been on a hunt to find a script that will return file names and hyperlinks into a Google Sheet. I found the following script that works perfectly, but is limited to PDFs. How do I modify this so it returns all files within a given folder?
function ListFldPdfs() {
// replace FOLDER-ID with your folder's ID
// replace SHEET-NAME with the name of the sheet that will hold the list
var myPDFfolder = DriveApp.getFolderById('FOLDER-ID'); // replace FOLDER-ID with your folder's ID
var thesheet = 'SHEET-NAME' // give a name to YOUR list sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var newPDFsheet = ss.getSheetByName(thesheet);
if (!newPDFsheet) {
ss.insertSheet(thesheet, 0);
var newPDFsheet = ss.getSheetByName(thesheet) ;
Logger.log(SpreadsheetApp.getActiveSheet().getName());
}
// clear all existing content
newPDFsheet.clear();
// append the header row
newPDFsheet.appendRow([ "Name", "VIEW", "HYPERLINK"]);
var results = [];
// list all pdf files in the folder
var mypdfs = myPDFfolder.getFilesByType(MimeType.PDF);
// loop through found files in the folder
while (mypdfs.hasNext()) {
var myfile = mypdfs.next();
var fname = myfile.getName();
var furl = myfile.getUrl();
results = [ fname, furl, ];
//Logger.log("results = "+results); // for de-bugging
newPDFsheet.appendRow(results);
}
var fcell = ss.getSheetByName(thesheet).getRange("C2");
fcell.setFormula('=arrayformula(if(A2:A10="",,hyperlink(B2:B10,""&A2:A10&"")))');
}