Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A script for google spreadsheet to provide multiple hyperlink choice for one cell

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).

like image 871
a1s2d3f4 Avatar asked May 22 '12 23:05

a1s2d3f4


People also ask

Can you add multiple hyperlinks to one cell in Google Sheets?

Unlike Excel, Google Sheets lets you insert multiple hyperlinks into a cell without using text boxes. Select the text you want to link (in this case COUNTA) and click Insert Link in the menu (or use the keyboard shortcut CTRL + K).

How do I put multiple hyperlinks in one cell?

The short answer is that this cannot be done. Excel only allows you to enter hyperlinks on a cell-by-cell basis. Even if you add the hyperlinks to adjacent cells, and then merge the cells into one, only the first hyperlink is maintained by Excel.

How do you dynamically link in Google Sheets?

Creating a Dynamic Hyperlink URL in Google Sheets Here are those cool steps. Go to Sheet2 and right-click on 'Sheet2! A1' and select “Get link to this cell”. When you paste the copied link in any blank cell, you will get a URL like this.


1 Answers

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.

like image 178
Henrique G. Abreu Avatar answered Oct 01 '22 03:10

Henrique G. Abreu