0

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&"")))'); 
}
0

2 Answers 2

0

Instead of:

  var mypdfs = myPDFfolder.getFilesByType(MimeType.PDF);

...use Folder.getFiles(), like this:

  const myFiles = myPDFfolder.getFiles();
1
  • Thanks! That's what I needed!
    – dvdp
    Commented Jan 9, 2023 at 22:18
0

For anyone interested, here's the updated script:

'function ListFldFiless() {
  // replace FOLDER-ID with your folder's ID
  // replace SHEET-NAME with the name of the sheet that will hold the list
  var myFilesfolder = 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 newFilessheet = ss.getSheetByName(thesheet); 
     if (!newFilessheet) { 
       ss.insertSheet(thesheet, 0); 
       var newFilessheet = ss.getSheetByName(thesheet) ;
  Logger.log(SpreadsheetApp.getActiveSheet().getName());
     }
  // clear all existing content
  newFilessheet.clear();
  // append the header row
  newFilessheet.appendRow([ "Name", "VIEW", "HYPERLINK"]);
  var results = [];
  // list all files in the folder
  const myFiles = myFilesfolder.getFiles();
  // loop through found files in the folder
  while (myFiles.hasNext()) { 
    var myfile = myFiles.next(); 
    var fname = myfile.getName();
    var furl = myfile.getUrl();
    results = [ fname, furl, ];
    //Logger.log("results = "+results); // for de-bugging
    newFilessheet.appendRow(results); 
       } 
     var fcell = ss.getSheetByName(thesheet).getRange("C2"); 
               fcell.setFormula('=arrayformula(if(A2:A10="",,hyperlink(B2:B10,""&A2:A10&"")))'); 
}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.