Home > Mobile >  How to get linked text from cell and set text in other cell with link attached using Google Apps Scr
How to get linked text from cell and set text in other cell with link attached using Google Apps Scr

Time:03-11

I want to get the linked Text value from a cell and set it in another cell with the URL attached.

For example: I have the following linked text value in cell A1. I want to get this URL-linked text and set it in another cell with the link attached. When I used getValue() method, the link with this value is lost. Is there any other way that we can do it using Google Apps Script? Thank you

enter image description here

CodePudding user response:

In your situation, how about using getRichTextValue() instead of getValue() as follows?

Sample script:

function myFunction() {
  const src = "A1"; // Source cell.
  const dst = "B1"; // Destination cell.

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  const richTextValue = sheet.getRange(src).getRichTextValue();
  sheet.getRange(dst).setRichTextValue(richTextValue);
}
  • This sample script copies from "A1" to "B1" with the richTextValue.

  • In this case, you can also use the following sample script.

      const src = "A1"; // Source cell.
      const dst = "B1"; // Destination cell.
    
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
      sheet.getRange(src).copyTo(sheet.getRange(dst));
    

References:

CodePudding user response:

You can use getRichTextValue() to get the link from text.

Sample script will look like this:-

  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sheet = ss.getSheetByName('Sheet1')
  var range  = sheet.getRange('A1')
  var linkURL = range.getRichTextValue().getLinkUrl()

Reference:-

getRichTextValue()

  • Related