views:

36

answers:

1

Hi,

I'm trying to finish off a Google script to reformat a field that I would ideally like to turn into a hyperlink.

This is the common format of the text in the spreadsheet:

tistaff: other sections: person: randomname

This is how I would like it to appear:

<li><a href="http://www.thisisstaffordshire.co.uk/topics/person/randomname"&gt;randomname&lt;/a&gt;&lt;/li&gt;

I've done most of the work except the end bit, which I just can't work out. Can anyone help.

Here's my script:

function HTMLtransform() {
  var sheet = SpreadsheetApp.getActiveSheet();
  for(var c=1; c<sheet.getLastRow(); c++){
    var rng = sheet.getRange("B"+c);
    var rplc = sheet.getRange("F"+c);
    var value = String(rng.getValue());

    var replVal = new String('test');

    var target = 'test'; // this variable is designed to capture the unique bit of the variable

    target = value.replace(/tistaff: other sections:[a-z]*: ([a-z]*)$/, '$1');// this variable is designed to capture the unique bit of the variable

    replVal = value;
    replVal = replVal.replace(/ company:/, 'company/');
    replVal = replVal.replace(/ person:/, 'person/');
    replVal = replVal.replace(/ place:/, 'place/');
    replVal = replVal.replace(/tistaff: other sections:/, '<li><a href="http://www.thisisstaffordshire.co.uk/topics/');

    replVal = replVal.replace(/ ([a-z]*)$/, '');
    replVal = replVal + target + '">' + target + '</a></li>';



    rplc.setValue(replVal);
  }
}

It works to a point, but this is the output:

**<li><a href="http://www.thisisstaffordshire.co.uk/topics/person/tistaff: other sections: person: randomname">tistaff: other sections: person: randomname</a></li>**

What am I doing wrong?

A: 

Here's the finished code:

I didn't realise that you could insert a subpattern more than once.

function HTMLtransform() {
  var sheet = SpreadsheetApp.getActiveSheet();
  for(var c=1; c<sheet.getLastRow(); c++){
    var rng = sheet.getRange("B"+c);
    var rplc = sheet.getRange("F"+c);
   var value = String(rng.getValue());

    regexFormat = /tistaff: other sections: (place|company|person): ([a-z]*)$/

   // var replVal = new String('test');


    replVal = value.replace(regexFormat, '<li><a href="http://www.thisisstaffordshire.co.uk/topics/$1/$2'+'"&gt;$2&lt;/a&gt;&lt;/li&gt;');




      rplc.setValue(replVal);
  }
}
elksie5000
I'm learning very slowly
elksie5000