0

I want to insert in Google sheets cell text with hyperlink:

enter image description here

I tried this Java code:

values.add(new CellData()
                .setUserEnteredValue(new ExtendedValue()
                    .setStringValue(get.getTitle())).setHyperlink(get.getUrl())

But I get only text without hyperlink. How I can implement this?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • What is the error that you are encountering? Anyway, as a workaround, why don't you try the idea from this SO Post: https://stackoverflow.com/questions/527719/how-to-add-hyperlink-in-jlabel? Play with it until you get a working link. Hopefully it helps. – Danee Jul 16 '17 at 11:22
  • I don't get any exception. – Peter Penzov Jul 16 '17 at 16:01

1 Answers1

3

In Documentation

A hyperlink this cell points to, if any. This field is read-only. (To set it, use a =HYPERLINK formula in the userEnteredValue.formulaValue field.)

You can't set multiple properties for an ExtendedValue object it only accepts single property to set. All properties point a value for Cell but only difference is those setters determines the type of cell value (bool, string, formula, number, error), so you can't set multiple value for Single Cell thats why you get the error( (oneof), oneof field 'value' is already set ).

Here is the new code, you can add hyperlink like this:

values.add(
    new CellData()
        .setUserEnteredValue(new ExtendedValue()
        .setFormulaValue("=HYPERLINK(\"http://stackoverflow.com\",\"SO label\")")
);
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
  • @PeterPenzov added the snippet it should work like this. – Ömer Erden Jul 17 '17 at 14:23
  • In general it's working wit this code: `.setFormulaValue("=HYPERLINK(\"" + get.getUrl() + "\",\"" + get.getTitle() + "\")"))` but I have a issue when I have " " into the title. Is there any solution? Probably to parse the title first? – Peter Penzov Jul 18 '17 at 05:50
  • @PeterPenzov from [here](https://stackoverflow.com/a/2608682) get.setTitle(get.getTitle().replaceAll("^\"|\"$", "")); will solve your problem – Ömer Erden Jul 18 '17 at 06:06
  • @PeterPenzov the solution for quotes in HYPERLINK `replaceAll("\"", "\"\""); ` – Ömer Erden Jul 18 '17 at 07:35
  • I sterted this topic: https://stackoverflow.com/questions/45230873/set-custom-column-width-size-in-google-sheets Can you give some advice, please? – Peter Penzov Jul 21 '17 at 06:51