I'm trying to set up a script that will pull files from Google Drive and send them to the corresponding customer email on a google sheet tab. Files are named by the inv# The current code I have found online creates a tab with the inv# and a hyperlink to the file. I'm not even sure if I'm on the right path here... any help would be much appreciated!
My sheet is set up like this:
Inv# | Customer | |
---|---|---|
234 | ABC Co | [email protected] |
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("1XBH6m0WsAI2thQW9miZ3"); // replace FOLDER-ID with your folder's ID
var thesheet = 'Phone Scans PDFs' // 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&"")))');
}