7
votes

I have a google spreadsheet. In some cells, it has multiple names(strings) that I would like to associate with individual hyperlinks.

E.g. if I have a cell such as "Charles Darwin", it's easy for me to create a hyperlink out of this name by doing something like =Hyperlink(VLOOKUP("Charles Darwin", People!$A$1:$B$738, 2, false), "Charles Darwin") (note that I have a "People" sheet from which I grab the hyperlink)

But if I happen to have multiple entries in that cell, say ";" or newline separated, e.g., "Charles Darwin; George Washington", I can't do that. I'd like to give the user an ability to click on the cell, have the contents of the cell be sent (as argument) to some kind of script, and for that script to find the hyperlinks in my "People" sheet for those strings, and then to present the user with a little "pop-up" right next to that cell, where the desired hyperlink could be clicked on.

I tried to find something along those lines on this site, but nothing similar seemed to come up. Might someone have a link or two for me (or basic example code) that I could start with to try to solve this? (I am assuming this is possible).

1

1 Answers

4
votes

It's not possible to have two hyperlinks on the same cell.

It is possible to write scripts to Google Spreadsheets, but I'm not sure it's going to suit your use case well. The solution I see would be like this:

  • The user click on the desired cell, selecting it.
  • Then he clicks on a custom menu and picks an entry there, e.g. show links
  • A popup will show up (not besides the cell, but centered on the screen) with the links.

Do you think this is fine? The code would look like this (open the menu Tools > Script Editor)

function onOpen() {
  SpreadsheetApp.getActive().
    addMenu("Test", [{name: 'Show Links', functionName:'showLinks'}]);
}

function showLinks() {
  var values = SpreadsheetApp.getActiveRange().getValue().split(';');

  var app = UiApp.createApplication().setTitle('Links'); 
  var grid = app.createGrid(values.length, 2);

  for( var i = 0; i < values.length; ++i ) {
    var url = findLink(values[i]);
    grid.setWidget(
      i, 0, app.createLabel(values[i])).setWidget(
      i, 1, url ? app.createAnchor(url, url) : app.createLabel('Not Found'));
  }

  app.add(grid);
  SpreadsheetApp.getActive().show(app);
}

var mapName2Url = null;
function findLink(name) {
  if( mapName2Url == null ) { //lazy load
    mapName2Url = {};
    var data = SpreadsheetApp.getActive().getSheetByName('People').getDataRange().getValues();
    for( var i = 1; i < data.length; ++i ) //skipping the header
      mapName2Url[data[i][0]] = data[i][1];
  }
  return mapName2Url[name];
}

After you paste it on the script editor, run the onOpen function twice to authorize it and have the menu created for you. Next time you open the spreadsheet, the menu should be created automatically.

By the way, I have not tested this code, so it might contain dumb mistakes.