Search code examples
javascriptemailmailto

Automatic clickable email address on next cell. Apps Script


I want my spreadsheet to automatically create a clickable email address using mailto in the cell next to where I input the email address.

My code is this:

function onEdit3() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 4 ) { //checks the column
      var nextCell = r.offset(0, 1);
      if( nextCell.getValue() === '' ) //is empty?
        var merge = "mailto:"+r;
        nextCell.setValue(merge);
    }
  }
}

The problem is that this is what I get in the next cell: mailto:Range

I know this is an easy fix but I can't figure it out.

Thank you, Rodrigo


Solution

  • Problem is you did not get the value of the cell by calling r.getValues() :

    var merge = "mailto:"+r.getValues();
    

    So the code should be :

    function onEdit3() {
      var s = SpreadsheetApp.getActiveSheet();
      if( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
        var r = s.getActiveCell();
        if( r.getColumn() == 0 ) { //checks the column
          var nextCell = r.offset(0, 1);
          if( nextCell.getValue() === '' ) //is empty?
            var merge = "mailto:"+r.getValues();
            nextCell.setValue(merge);
        }
      }
    }