通过RegEx在细胞范围内改变多次替换的功能

时间:2017-02-15 11:15:59

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

我使用此代码在单元格范围内进行多个正则表达式替换,但它的工作速度太慢。我知道最好使用“push Array”而不是Range.getValue,但我真的不明白它是如何工作的,我将不胜感激任何帮助。

 function patternizer(){
      var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var firstrow = 1
      var lastrow = 1000
      var column = 11
      regexrep(/( |)\d+-\d+/,'',firstrow,lastrow,column);
      regexrep(/\d{4}-\d{4}/,'',firstrow,lastrow,column);
      regexrep(/.*?(\D\d.*)/,'$1',firstrow,lastrow,column);
      regexrep(/(.*\d\D).*/,'$1',firstrow,lastrow,column);
      regexrep(/[a-zA-Z]{3,}/,'',firstrow,lastrow,column);
    }

    function regexrep(rxp,rxR,firstrow,lastrow,column){
//(regex pattern, replace pattern, firstrow, lastrow, column)
      var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var r1 = new RegExp(rxp);
      for (i=firstrow; i<=lastrow; i++){
        try{
          var tr = ss.getRange(i, column)
          var t = tr.getValue()
          t = t.replace(r1, rxR);
          tr.setValue(t)
        }catch(e){
        }
      }
    }

测试数据:

Plate 21410 T15K6 (16h10h3h25) (to end and keyway cutters, reamers and tsekovok)
Plate 24070 BK8 (10h5h3) (for disk and end-end cylindrical. Mills to modular machine tools)
Plate 36410 BK8 (16h6h3h18) (for mechanical, slip-and end mills with spiral tooth)
Knife torts.frez 2020-0161 R6M5
Knife 2020-0162 for tripartite cutter d100-224h12 (23,8h11h4,72) R6M5 grooved wedge left
Knife 2020-0164 for tripartite cutter d100-224h14 (23,8h13h4,72) R6M5 grooved wedge left
Knife 2020-0165 for tripartite cutter d100-125 (23,8h15h4,72) R6M5 grooved wedge right
Knife 2020-0166 for tripartite cutter d100-125 (23,8h15h4,72) R6M5 grooved wedge left
Plate 21350 BK8 (14h8h3h25) (to end and keyway cutters, reamers and tsekovok)
Plate 36390 BK8 (21h6h3h24) (for mechanical, slip-and end mills with spiral tooth)
Knife 2020-0167 for tripartite cutter d160-250 (28,3h15h5,72) R6M5 grooved wedge right
Knife 2020-0172 to the end mill d125-315 (28,3h28,5h5,72) R6M5 grooved wedge left
Cutter veneer. c / x 3
Knife 2020-0026 to the end mill d160-315 (33,8h25,5h7,72) R6M5 grooved wedge left
Intermediate ring d16, D27, L 2mm to the mandrel for milling machines GOST15071-75
Knife 2020-0169 for tripartite cutter d100-315 (28,3h26,5h5,72) R6M5 grooved wedge right
Knife 2020-0022 to the end mill d80-100 (28,3h18,5h5,72) R6M5 grooved wedge left
Intermediate ring d16, D27, L10mm to the mandrel for milling machines GOST15071-75
Intermediate ring d22, D34, L 1mm to the mandrel for milling machines GOST15071-75
Intermediate ring d22, D34, L 2.0mm to the mandrel for milling machines GOST15071-75
Intermediate ring d22, D34, L 2.3mm to the mandrel for milling machines
Intermediate ring d22, D34, L 3mm to the mandrel for milling machines GOST15071-75
Intermediate ring d22, D50, L 5mm to the mandrel for milling machines

1 个答案:

答案 0 :(得分:1)

我真的很想有一些数据来检查这个,因为我并不是那些第一次100%正确的程序员之一。但这是我的第一个通过解决方案,我认为它会起作用。

function patternizer()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var firstrow = 1
  var lastrow = 1000
  var column = 11
  regexrep(/( |)\d+-\d+/,'',firstrow,lastrow,column);
  regexrep(/\d{4}-\d{4}/,'',firstrow,lastrow,column);
  regexrep(/.*?(\D\d.*)/,'$1',firstrow,lastrow,column);
  regexrep(/(.*\d\D).*/,'$1',firstrow,lastrow,column);
  regexrep(/[a-zA-Z]{3,}/,'',firstrow,lastrow,column);
}
//(regex pattern, replace pattern, firstrow, lastrow, column)
function regexrep(rxp,rxR,firstrow,lastrow,column){ 
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();  //Ranges start at1
  var rng = ss.getRange(firstrow,column,lastrow,1);
  var rngA = rng.getValues(); //[[],[],[],[],....] Arrays start at zero
  var r1 = new RegExp(rxp);
  for(i = 0;i < lastrow;i++)
  {
    rngA[i][0]=rngA[i][0].replace(r1, rxR);//you might need a toString() in here before the replace.  Give me some data and I can check it out. 
  }
  rng.setValues(rngA);
}

让我知道它是如何运作的。