google-sheetsgoogle-apps-scripthyperlinkrichtext

Google App Script is not copying the hyperlinks when the rows gets moved from one tab to another -


I need all my hyperlinks cells to copy over to the tabs once it's moved - My client needs to be able to go back to that linked tab when it's moved from "open" to "delivered"

I'm not a pro but this is my current script - not sure what I am doing wrong:

function onEdit(e){
let col = range.getColumn();
let row = range.getRow();
let val = range.getValue();
let source = e.source.getActiveSheet();

if (col == 9 && val != ''){
let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName(source.getName());
let targetSheet = ss.getSheetByName(val);
let data = sheet.getRange(row,1,1,sheet.getLastColumn()).getRichTextValues();
targetSheet.getRange(targetSheet.getLastRow()+1,1,1,data[0].length).setRichTextValues(data);
targetSheet.getRange(targetSheet.getLastRow(),1).clearContent();
  sheet.deleteRow(row);
 }
 }

It copies the rest of the row but now the cell with the hyperlink-

Thank you!

I haven't tried much to edit the script as I am not sure where the error is happening as nothing shows incorrectly when I run it


Solution

  • Retain the Format of Hyperlink Using copyTo

    Just like what @Saddles said, you may use copyTo if you wish to keep the HyperLink format when it moved to the other Google Sheet tab.

    I modified and deleted some of your codes to come up with your desired output.


    I changed this code:

    let data = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getRichTextValues();
    

    to:

    let data = sheet.getRange(row, 1, 1, sheet.getLastColumn());
    

    And replace this code:

    targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, data[0].length).setRichTextValues(data);
    targetSheet.getRange(targetSheet.getLastRow(), 1).clearContent();
    

    with:

    data.copyTo(targetSheet.getRange(targetSheet.getLastRow() + 1, 1), { contentsOnly: false })
    

    And also, I added e.range to your code.

    Full Code:

    function onEdit(e) {
      let range = e.range;
      let col = range.getColumn();
      let row = range.getRow();
      let val = range.getValue();
      let source = e.source.getActiveSheet();
    
      if (col == 9 && val != '') {
        let ss = SpreadsheetApp.getActiveSpreadsheet();
        let sheet = ss.getSheetByName(source.getName());
        let targetSheet = ss.getSheetByName(val);
        let data = sheet.getRange(row, 1, 1, sheet.getLastColumn());
        data.copyTo(targetSheet.getRange(targetSheet.getLastRow() + 1, 1), { contentsOnly: false })
        sheet.deleteRow(row);
      }
    }
    

    Sample Output:

    Sample output

    Note: I just assumed that your sample sheet was like this.

    Reference: