2010-09-29 4 views
0

J'essaie de terminer un script Google pour reformater un champ que je voudrais idéalement transformer en un lien hypertexte.Requête Regex avec des scripts de feuille de calcul Google

C'est le format commun du texte dans la feuille de calcul:

tistaff: autres sections: Personne: randomname

Voilà comment je voudrais qu'il apparaisse:

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

J'ai fait la plupart du travail sauf le bit de fin, que je ne peux tout simplement pas travailler. Quelqu'un peut-il aider.

Voici mon 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); 
    } 
} 

Il fonctionne à un point, mais cela est la sortie:

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

Qu'est-ce que je fais mal?

Répondre

0

Voici le code fini:

Je ne savais pas que vous pouvez insérer un subpattern plus d'une fois.

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'+'">$2</a></li>'); 




     rplc.setValue(replVal); 
    } 
} 
+0

J'apprends très lentement – elksie5000

Questions connexes