I’ve been trying to find a solution for a while but now it’s time to ask for aid :)
Goal: Add the hyperlinks to specific PDF documents stored in G Drive to all the individual cells in a column in google sheet depending on the document's name.
I explain myself better. Here you have the screenshot:
Sheet Layout
I’m trying to have the content of the cells in column A replaced by a hyperlink (maintaining the link label as it is). The hyperlink should point to the document in google drive where the filename contains the name shown in the relative cell in this spreadsheet.
The function should look (for each value of column A) for the file in the G Drive folder (where all the documents are) with the name containing the value of the cell (the cell has just the invoice number, like the one in the screenshot, while the file also has “Invoice-“ before the number and ends with “.pdf”). If there’s a match each cell value should be replaced with a hyperlink pointing to the relative document while maintaining the cell value as link label.
Sorry for the long description.
I was able to create a function that does exactly what I need for one row. The problem for me is to understand how to do it for each row and how to do the “search and match” with the relative document in google drive.
function invoiceLinking() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// set active sheet to the second tab of the spreadsheet
var activeSheet = ss.getSheets()[1];
var data = activeSheet.getDataRange().getValues();
var invoiceNumber = activeSheet.getRange(2,1).getValue();
// get the drive folder containing the invoices by ID
var folder = DriveApp.getFolderById("----Folder ID ----");
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var fullname = file.getName();
var url = file.getUrl();
}
// check if the number of the invoice in the cell is contained in the name of the PDF file in the folder
// applies an hyperlink to the realitve PDF to the cell with invoice number
if (fullname.indexOf(invoiceNumber) !== -1 ) {
activeSheet.getRange(2,1).setValue('=HYPERLINK("'+url+'","'+invoiceNumber+'")');
}
}