I am utilizing an Apps Script to fetch hyperlinks from cells in Google Sheets. The following code successfully pulls the link from cell B1:
function getHyperlink(reference) {
var link = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(reference).getRichTextValue().getLinkUrl();
return link;
}
Although it works perfectly for cell B1, I’m facing issues with extracting the hyperlink from cell B2. I don’t have extensive knowledge in Apps Script. Can someone guide me on how to retrieve a hyperlink from cell B2?
It seems like you might be encountering an issue with how you’re referencing the cell. If your code works for B1, it should theoretically work for B2 as well. Make sure you’re using the correct cell reference when calling the function, like “B2” instead of “B1”. Here’s how you can do it:
function getHyperlinkFromB2() {
var linkB2 = getHyperlink('B2');
return linkB2;
}
Call this getHyperlinkFromB2()
function, and it should provide you with the hyperlink from cell B2. Double-check to ensure that cell B2 indeed contains a hyperlink, as this might be the cause of the issue too.
If the problem persists despite using the correct reference like others have suggested, you might want to check the type of data in cell B2 as well. Sometimes, the Apps Script can face trouble if the format or data in the cell is different from what it expects. For instance, if B2 is blank or holds text with no underlying hyperlink, the script will return null. Another option is to use a logger to see what the script retrieves, which can provide a clue if something’s amiss. Consider using Logger.log(getHyperlink('B2'));
to debug.