r/GoogleAppsScript • • Dec 16 '19

Add hyperlink to a cell value without using the =HYPERLINK() formula?

Comment removed in protest of Reddit's API changes forcing third-party apps to shut down

6 Upvotes

6 comments sorted by

3

u/lordph8 Dec 16 '19

If I understand you correctly, no. If you copy data that has a hyperlink attached the copyto command keeps it by default (can turn it off with options).

Taking a wild guess, are you trying to attach hyperlinks to an existing range of data?

3

u/Strel0k Dec 16 '19

Exactly.

For example, if I wanted a script that converted cell values into google searches. So a cell with the value "BANANA" would stay as "BANANA" but get a link of "https://www.google.com/search?q=BANANA"

Could there be a way to modify the copy data (clipboard data?) before executing the copyTo command?

2

u/lordph8 Dec 16 '19

The answer lies in for loops. And if the google search link remains constant it should work nicely. I'm on Mobile so bear with me (going from memory).

function googleSearch() {

var ss =SpreadsheetApp.openById('put id in here'); var s = ss.getSheetByName('put sheet name here'); var range = ss.getRange('A2:A50').getValues();

// Whatever range has your search terms

var google = "https://www.google.com/search?q="; var row = 2;

for (var i=0; i<range.length; i++) {

var target = s.getRange('A' + row);

target.setFormula('=HYPERLINK(google + range[i], range[i])';

var row = row + 1;

} }

This should run through and replace all your search terms with hyperlinks with those terms.

Probably screwed up something, but it should be close.

1

u/Bruh-ism Dec 17 '19

He said in his post he wanted to accomplish this without using the hyperlink formula in the cell lol

1

u/lordph8 Dec 17 '19

No otherway to get a hyperlink into cell. Besides he may not of understood loops and that you can iterate over all the search terms.

0

u/[deleted] Dec 20 '19

[deleted]

1

u/Strel0k Dec 20 '19

Ah, that sucks. Thanks for the explanation.