Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // How can I easily link to a bunch of files in my Google Drive from a Google Sheet (in the same account/drive)?
- // https://webapps.stackexchange.com/questions/135181/how-can-i-easily-link-to-a-bunch-of-files-in-my-google-drive-from-a-google-sheet
- // Answer: https://webapps.stackexchange.com/a/135188/117311
- function extendListFldFiles() {
- // Replace FOLDER-ID with the folder's ID
- // Replace MimeType.PDF with your MimeType
- // Replace SheetWithNamesOfFiles with the name of the sheet that ALREADY holds the list of your files
- var myFilesFolder = DriveApp.getFolderById('19c0ZGBBSyRXT8LmauSzYRneDTn0nYnTQ');
- // replace FOLDER-ID with the folder's ID
- var myNeededMimeType = MimeType.PDF ; // MimeType.PNG, MimeType.GOOGLE_SHEETS, MimeType.MICROSOFT_EXCEL, MimeType.MICROSOFT_EXCEL_LEGACY, etc
- // Choose and place the MimeType you want. (MORE INFO about mime-types: https://developers.google.com/apps-script/reference/base/mime-type)
- var oldList = 'SheetWithNamesOfFiles';
- // We assume we ALREADY have a sheet named SheetWithNamesOfFiles where we have the names of our files.
- // We only need columns A and B where row 1 is reserved for headers, names of the files start in cell A2, column B is empty AND
- // in cell B2 we place the formula:
- // =IFERROR(ArrayFormula(VLOOKUP(OFFSET(SheetWithNamesOfFiles!A2,,,COUNTA(exlistedFiles!A2:J)),exlistedFiles!A2:J,7,0)),"Wrong Type/Name")
- // **************************************
- // ***** END MODIFICATIONS HERE *****
- // **************************************
- var thesheet = 'exlistedFiles';
- var ss = SpreadsheetApp.getActiveSpreadsheet();
- var newFILESsheet = ss.getSheetByName(thesheet);
- if (!newFILESsheet) {
- ss.insertSheet(thesheet, 0);
- var newFILESsheet = ss.getSheetByName(thesheet) ;
- Logger.log("SHEET IS: " +SpreadsheetApp.getActiveSheet().getName());
- }
- // clear all existing content
- newFILESsheet.clear();
- // append the header row
- newFILESsheet.appendRow(["Name", "VIEW", "Type", "File ID", "DOWNLOAD", "OPEN", "HYPER", "Date", "Size", "Description"]);
- var results = [];
- // list all pdf files in the folder of CHOSEN MimeType.
- var myFiles = myFilesFolder.getFilesByType(myNeededMimeType);
- // loop through found files in the folder
- while (myFiles.hasNext()) {
- var myfile = myFiles.next();
- var fname = myfile.getName();
- var furl = myfile.getUrl();
- var ftype = myfile.getMimeType();
- var fid= myfile.getId();
- var fdownurl = "https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + myfile.getId();
- var fopenurl = "https://drive.google.com/open?id=" + myfile.getId();
- var fdate = myfile.getDateCreated();
- var fsize = myfile.getSize();
- var fdescription = myfile.getDescription();
- Logger.log("File Name is: "+myfile.getName());
- // Logger.log("URL is: "+myfile.getUrl());
- // Logger.log("File Type is: "+myfile.getMimeType());
- results = [
- fname,
- furl,
- ftype,
- fid,
- fdownurl,
- fopenurl,
- "", // Column reserved for the formula further down in fcell, that will give the Hyperlink
- fdate,
- fsize,
- fdescription
- ];
- // Logger.log("results = "+results); // for de-bugging
- newFILESsheet.appendRow(results);
- }
- // Now create the Hyperlinks in the sheet exlistedFiles by placing a formula in cell G2
- var fcell = ss.getSheetByName(thesheet).getRange("G2");
- fcell.setFormula('=arrayformula(if(A2:A10="",,hyperlink(B2:B10,""&A2:A10&"")))');
- // Finally, hide the newly created exlistedFiles (thesheet)
- var thesheet = ss.getSheetByName(thesheet);
- thesheet.hideSheet();
- // We can now check our already existing sheet (SheetWithNamesOfFiles) to find the hyperlinks of our files in column B, starting in cell B2
- // By ONLY changing the columns' numbers to be shown in the formula on the sheet SheetWithNamesOfFiles, more columns can be shown with more of the info. It is all here :)
- // EXAMPLE to also show the fileID and size: =IFERROR(ArrayFormula(VLOOKUP(OFFSET(A2,,,COUNTA(A2:A)),exlistedFiles!A2:J,{7,4,9},0)),"OUPS")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement