0

理想的にはハイパーリンクに変換したいフィールドを再フォーマットするために、Google Apps Script を完成させようとしています。

これは、スプレッドシート内のテキストの一般的な形式です。

tistaff: その他のセクション: person: ランダムな名前

これは私がそれをどのように表示したいかです:

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

私はうまくいかない最後のビットを除いて、ほとんどの作業を行いました。誰でも助けることができます。

これが私のスクリプトです:

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

ある程度は機能しますが、これが出力です。

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

私は何を間違っていますか?

4

1 に答える 1

1

完成したコードは次のとおりです。

サブパターンを複数回挿入できるとは知りませんでした。

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);
  }
}
于 2010-10-05T16:04:22.467 に答える