【问题标题】:Regex query with Google spreadsheet scripts使用 Google 电子表格脚本进行正则表达式查询
【发布时间】:2010-09-29 19:57:00
【问题描述】:

我正在尝试完成一个 Google Apps 脚本以重新格式化我希望将其转换为超链接的字段。

这是电子表格中文本的常用格式:

tstaff:其他部分: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>**

我做错了什么?

【问题讨论】:

    标签: regex google-apps-script google-sheets


    【解决方案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);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 1970-01-01
      • 2020-03-17
      • 2012-04-16
      • 2019-08-20
      • 2018-05-23
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      相关资源
      最近更新 更多