簡體   English   中英

使用 Google 電子表格腳本進行正則表達式查詢

[英]Regex query with Google spreadsheet scripts

我正在嘗試完成一個 Google Apps 腳本來重新格式化我希望將其轉換為超鏈接的字段。

這是電子表格中文本的常見格式:

tistaff: 其他部分: person: randomname

這是我希望它出現的方式:

<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>**

我做錯了什么?

這是完成的代碼:

我沒有意識到您可以多次插入子模式。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM