0

I have the Column "A" and I want to insert a Hyperlink to the data in the cell bases on depending on how many characters have the cell starting from 2nd row. For Example:

  • If the Cell A2 have the data (1-2345678901) = 12 characteres insert the link www.mysite.com/ + value of the cell A2

Result should be www.mysite.com/1-2345678901

  • If the Cell A2 have the data (1-2345678) = 9 characteres insert the link www.mysite2.com/ + value of the cell A2.

Result should be Hyperlink www.mysite.com/1-2345678

I needs to know if this script can be tunned or this script is very Ok. If anyone have a new idea please feel free to let me know.

SCRIPT:

    function onEdit(e)
    {
      var activeCell = SpreadsheetApp.getActiveSpreadsheet().getActiveCell();
      var activeCellValue = activeCell.getValue();


      var row = activeCell.getRow();
      var column = activeCell.getColumn();

      if(row < 2)   return; //If header row then return
      var colNums  = [1]; //Coulmns, whose edit is considered
      if(colNums.indexOf(column) == -1) return; //If column other than considered then return

      var length = String(activeCellValue).length;

      if(length == 12)
      {
        activeCell.setValue('=HYPERLINK' + '("www.mysite.com='+activeCellValue+'";"'+activeCellValue+'")')
      }
      else if(length == 9)
      {
        activeCell.setValue('=HYPERLINK' + '("www.mysite2.com='+activeCellValue+'";"'+activeCellValue+'")');
      }
    }

1 Answer 1

1

Your script can be tweaked for better performance, by making use of the event data delivered to it instead of resorting to Service calls. You have done the right thing by testing for bail-out conditions early, but even those can be reordered to increase the chance of exiting early.

function onEdit(e)
{
  var activeCell = e.range;
  var activeCellValue = e.value;

  if (!e.value) return;    // do nothing if value is 'undefined'

  var column = activeCell.getColumn();

  var colNums  = [1]; //Columns, whose edit is considered
  if(colNums.indexOf(column) == -1) return; //If column other than considered then return

  var row = activeCell.getRow();
  if(row < 2)   return; //If header row then return

  var length = String(activeCellValue).length;

  if(length == 12)
  {
    activeCell.setValue('=HYPERLINK' + '("www.mysite.com/'+activeCellValue+'";"'+activeCellValue+'")')
  }
  else if(length == 9)
  {
    activeCell.setValue('=HYPERLINK' + '("www.mysite2.com/'+activeCellValue+'";"'+activeCellValue+'")');
  }
}
5
  • I have a problem, I implement the script but when I supr the data into de cell, the script instert the following information: /////=HYPERLINK("www.mysite2.com/undefined";"undefined")//// If the cell is null or doest'n have any information not run the script. The script add the link with the data "undefined" Aug 13, 2014 at 3:22
  • Well, a cell with no value will convert to a String of "undefined", which is 9 characters long, matching the second condition for inserting a hyperlink. See updated script - it checks for empty cells first.
    – Mogsdad
    Aug 13, 2014 at 14:59
  • I tried with if(length =="") didn´t work... how can I check if the cell is empty? Aug 13, 2014 at 16:11
  • I added if (!e.value)... to the answer above. Did you try that?
    – Mogsdad
    Aug 13, 2014 at 18:32
  • I use this if (!e.value) { activeCell.setValue() } Tell me is that correct Aug 13, 2014 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.