google apps script - Improove function of multiple replacement by RegEx in range of cells -
i use code multiple regex replace in range of cells, work slow. know better use "push array" instead of range.getvalue, dont understand how works , grateful help.
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); (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){ } } }
data test:
plate 21410 t15k6 (16h10h3h25) (to end , keyway cutters, reamers , tsekovok) plate 24070 bk8 (10h5h3) (for disk , end-end cylindrical. mills modular machine tools) plate 36410 bk8 (16h6h3h18) (for mechanical, slip-and end mills spiral tooth) knife torts.frez 2020-0161 r6m5 knife 2020-0162 tripartite cutter d100-224h12 (23,8h11h4,72) r6m5 grooved wedge left knife 2020-0164 tripartite cutter d100-224h14 (23,8h13h4,72) r6m5 grooved wedge left knife 2020-0165 tripartite cutter d100-125 (23,8h15h4,72) r6m5 grooved wedge right knife 2020-0166 tripartite cutter d100-125 (23,8h15h4,72) r6m5 grooved wedge left plate 21350 bk8 (14h8h3h25) (to end , keyway cutters, reamers , tsekovok) plate 36390 bk8 (21h6h3h24) (for mechanical, slip-and end mills spiral tooth) knife 2020-0167 tripartite cutter d160-250 (28,3h15h5,72) r6m5 grooved wedge right knife 2020-0172 end mill d125-315 (28,3h28,5h5,72) r6m5 grooved wedge left cutter veneer. c / x 3 knife 2020-0026 end mill d160-315 (33,8h25,5h7,72) r6m5 grooved wedge left intermediate ring d16, d27, l 2mm mandrel milling machines gost15071-75 knife 2020-0169 tripartite cutter d100-315 (28,3h26,5h5,72) r6m5 grooved wedge right knife 2020-0022 end mill d80-100 (28,3h18,5h5,72) r6m5 grooved wedge left intermediate ring d16, d27, l10mm mandrel milling machines gost15071-75 intermediate ring d22, d34, l 1mm mandrel milling machines gost15071-75 intermediate ring d22, d34, l 2.0mm mandrel milling machines gost15071-75 intermediate ring d22, d34, l 2.3mm mandrel milling machines intermediate ring d22, d34, l 3mm mandrel milling machines gost15071-75 intermediate ring d22, d50, l 5mm mandrel milling machines
i'd have data check on because i'm not 1 of programmers gets things 100% right first time. here's first pass solution , think work.
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 @ 0 var r1 = new regexp(rxp); for(i = 0;i < lastrow;i++) { rnga[i][0]=rnga[i][0].replace(r1, rxr);//you might need tostring() in here before replace. give me data , can check out. } rng.setvalues(rnga); }
let me know how works out.
Comments
Post a Comment