I have a Google Sheet with Google Drive documents names in each cells. So each cells contain a Google Drive documents name. Together, the various cells form an array.
I'm trying to retrieve hyperlinks of these Google Drive documents based on the name in each Google Sheet cell, and set these hyperlink in each cell (by replacing documents name in each cell). To be noted that the Google Drive documents are not in the same Google Drive folders.
I managed to modify an existing code so that it works in my context; however, it only works for 1-column array. When I try to extend the range to several columns, all hyperlinks return "undefine" url (even for the first column that was previously working).
See below my code. Any help would be much appreciated. Thanks a lot.
function getFile() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet1 = ss.getSheetByName('Sheet1');
var lastRow = sheet1.getLastRow();
var range = sheet1.getRange(2,1,lastRow - 1,4);
var names = range.getValues();
var nameUrl ={};
for (let i in names) {
var files = DriveApp.getFilesByName(names[i]);
while (files.hasNext()) {
var file = files.next();
var fullName = file.getName();
var url = file.getUrl();
nameUrl[fullName] = url;
}
}
var links = names.map(function(e) {
return [
'=HYPERLINK("' + nameUrl[e] + '","' + e + '")'
];
});
range.setValues(links);
}
links
is a 2D array of 1 column. If you want to paste data to multiple columns, then you need to adjustlinks
to make it like:[a,b]
instead of[a]
wherea
is'=HYPERLINK("' + nameUrl[e] + '","' + e + '")'
. Alsonames
is a 2D array, you can't iterate element wise a 2D array with only one iteration (i
). – soMario