5

I'm trying to get the URL of a range of cells, but getLinkUrl always returns null.

Here is what I tried:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Liste");
var url = sheet.getRange("A1").getRichTextValue().getLinkUrl();

This returns: null

var test = sheet.getRange("A1").getRichTextValue().getText(); 

This returns: text (which is the text in A1 so it's working, and so then I tried)

var test2 = sheet.getRange("A1").getRichTextValue().getLinkUrl(); 

This returns: null (I didn't understand why, and so tried this instead)

var test3 = sheet.getRange("A1:A10").getRichTextValues(); 

This returns:

[ [ {} ],
  [ {} ],
  [ {} ],
  [ {} ],
  [ {} ],
  [ {} ],
  [ {} ],
  [ {} ],
  [ {} ],
  [ {} ] ]

And so I'm lost, I really don't know how I can get the URL of a / many cell.

In the documentation, there is an explanation about getLinkUrl returning null if there are multiple URLs but I don't know how to extract only one URL.

The sheet I'm working with is really simple, there are words only in the A column,

appli
info
motivation
anime
dev perso
reflexion
funny
sad
Madar
  • 196
  • 2
  • 15
  • would you provide a sample with a link that we can edit? Thanks – David Leal Sep 24 '21 at 23:14
  • I'm afraid that I don't understand what you need.. If it's something in the sheet, I only got a list of word in the A column : appli info motivation anime dev perso reflexion funny sad – Madar Sep 25 '21 at 17:22
  • You can use getFormula() and it will return something like "=HYPERLINK()" then you can probably extract the link there. Please share a copy of your sheet for further details on your concern. – Jason E. Sep 27 '21 at 14:53

1 Answers1

4

Although I'm not sure about your actual Spreadsheet, from I'm trying to get the url of a range of cell but it always return null to me., I thought that in your situation, the hyperlink might be set in a part of text of a cell. So, can you test the following sample script?

Sample script:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Liste");
var res = sheet
  .getRange("A1")
  .getRichTextValue()
  .getRuns()
  .reduce((ar, e) => {
    var url = e.getLinkUrl();
    if (url) ar.push({url: url, text: e.getText()});
    return ar;
  }, []);
console.log(res)

Or, if you want to retrieve the URLs from multiple cells, you can also the following script.

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Liste");
var res = sheet
  .getRange("A1:A10")
  .getRichTextValues()
  .map((r, i) =>
    r.flatMap((c, j) =>
      c.getRuns().reduce((ar, e) => {
        var url = e.getLinkUrl();
        if (url) ar.push({ url: url, text: e.getText(), row: i + 1, col: j + 1 });
        return ar;
      }, [])
    )
  );
console.log(res)
  • var test3 = sheet.getRange("A1:A10").getRichTextValues(); returns the object of RichTextValue[][]. By this, such result is obtained in your question. When you want to retrieve the URL, it is required to retrieve it from the object.

References:

Edit:

From the thing I try to obtain is this something like this : (which is the link to the cell A1) https://docs.google.com/spreadsheets/d/###/edit#gid=0&range=A1, I understood that you wanted to retrieve the hyperlink of the cell. In this case, getRichTextValue cannot be used. So, how about the following sample script?

Sample script:

function myFunction() {
  // Ref: https://stackoverflow.com/a/21231012/7108653
  const columnToLetter = column => {
    let temp,
      letter = "";
    while (column > 0) {
      temp = (column - 1) % 26;
      letter = String.fromCharCode(temp + 65) + letter;
      column = (column - temp - 1) / 26;
    }
    return letter;
  };

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Liste");
  var range = sheet.getRange("A1:A10");
  
  var spreadsheetUrl = ss.getUrl();
  var sheetId = sheet.getSheetId();
  var startRow = range.getRow();
  var endRow = startRow + range.getNumRows() - 1;
  var startCol = range.getColumn();
  var endCol = startCol + range.getNumColumns() - 1;
  var cellLinks = [];
  for (var r = startRow; r <= endRow; r++) {
    for (var c = startCol; c <= endCol; c++) {
      var a1Notation = columnToLetter(c) + r;
      cellLinks.push({cell: a1Notation, url: `${spreadsheetUrl}#gid=${sheetId}&range=${a1Notation}`});
    }
  }
  console.log(cellLinks)
}
  • In above script, the hyperlinks of cells "A1:A10" are returned. For example, when var range = sheet.getRange("A1"); is used, the hyperlink of only cell "A1" is returned.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I tried both code and it returns [] and [[],[],[],[]] I really don't understand why. Are we okay to say that this code it supposed to return the url of the cell ? Cause maybe I wasn't that clear when I asked. But yeah those one doesn't worked for me. – Madar Sep 25 '21 at 17:21
  • @Madar Thank you for replying. I apologize for the inconvenience. In my answer, each element of the array is JSON object. So in this case, it's like `[{ur:"###",text:"###",row:##,col:##}, {,,,}, ]`. From `I tried both code and it returns [] and [[],[],[],[]]`, I'm worried that you might have not used my proposed script. How about this? And, in order to confirm your current situation, can you provide the sample Spreadsheet for replicating your issue? By this, I would like to confirm it. – Tanaike Sep 26 '21 at 02:42
  • @Tainake Thank you for your help, I'm afraid I did not understand the story with the JSON object. The sample Spreadsheet is really simple, there is only one sheet named "Liste" and the following word starting A1 to A8 : appli info motivation anime dev perso reflexion funny sad – Madar Sep 26 '21 at 09:05
  • But I'm now thinking that maybe I'm not using the appropriate function.. Are we okay to say that this function is supposed to give me an url like this one : https://docs.google.com/spreadsheets/d/1SwrhvedjQUP8q5_2uXS6JI62Jk6FDa4gSwr88g7tgFc/edit#gid=0&range=C5 ?? Thanks in advance – Madar Sep 26 '21 at 09:12
  • @Madar Thank you for replying and providing the sample Spreadsheet. Unfortunately, I couldn't open your sample Spreadsheet. I apologize for this. Can you confirm it again? – Tanaike Sep 26 '21 at 11:07
  • @Tainake hey ! my bad https://docs.google.com/spreadsheets/d/17k1op4QPmGKMh5cBraZ8v0FsG3wWqZyfN6choEKkaTg/edit?usp=sharing here you got ! the thing I try to obtain is this something like this : (which is the link to the cell A1) https://docs.google.com/spreadsheets/d/17k1op4QPmGKMh5cBraZ8v0FsG3wWqZyfN6choEKkaTg/edit#gid=0&range=A1 – Madar Sep 26 '21 at 11:13
  • @Madar Thank you for replying. I could open your sample Spreadsheet. From your replying, I added one more sample script. Could you please confirm it? If I misunderstood your goal, I apologize, again. – Tanaike Sep 26 '21 at 11:28
  • Hey @Tainake ! The Edit totally work thank you so much ! – Madar Sep 26 '21 at 18:01
  • 1
    @Madar Thank you for replying. I'm glad your issue was resolved. – Tanaike Sep 27 '21 at 00:05