Javascript 使用google apps脚本在google工作表中进行字符串相似性检查(范围拼写更新程序)

Javascript 使用google apps脚本在google工作表中进行字符串相似性检查(范围拼写更新程序),javascript,arrays,google-apps-script,google-sheets,data-cleaning,Javascript,Arrays,Google Apps Script,Google Sheets,Data Cleaning,我正在写一个程序: 在一个范围内迭代 将每个当前单元格与字符串进行比较(字符串拼写正确。这假定每个新词拼写正确) 更新当前单元格以匹配字符串(如果相似)(>=设置百分比) 重要提示:这意味着在一列中迭代一个范围,而我目前不需要迭代多个列 该程序目前处于工作状态,但它在长字符串方面有很多问题,我不知道为什么。我想看看是否有人能给我指出正确的方向 本课程的目的: 我目前一直在处理OCR导入的脏数据,这是不受欢迎的 我的许多重复值拼写错误(而且不是以拼写检查友好的方式) 例如: 测试 t3st1

我正在写一个程序:

  • 在一个范围内迭代
  • 将每个当前单元格与字符串进行比较(字符串拼写正确。这假定每个新词拼写正确)
  • 更新当前单元格以匹配字符串(如果相似)(>=设置百分比)
重要提示:这意味着在一列中迭代一个范围,而我目前不需要迭代多个列

该程序目前处于工作状态,但它在长字符串方面有很多问题,我不知道为什么。我想看看是否有人能给我指出正确的方向

本课程的目的:

我目前一直在处理OCR导入的脏数据,这是不受欢迎的

我的许多重复值拼写错误(而且不是以拼写检查友好的方式)

例如:

  • 测试
  • t3st1ng
  • t3st1g
  • tes5fng
  • te5f1n@
  • 香蕉
  • 3abanas
  • 贝纳斯
  • 8n4anans
更正为:

  • 测试
  • 测试
  • 测试
  • 测试
  • 测试
  • 香蕉
  • 香蕉
  • 香蕉
  • 香蕉
我使所有这些值都等于第一个值。 当它到达香蕉时,它使用香蕉作为新的第一个值。 我希望这是有道理的

使用第一个示例,程序可以完美地执行

但是

问题是当我有这样的字符串时:

  • 这是对应急广播无线电系统的测试
  • 这是对紧急广播的测试r@io系统
  • th1s是对紧急广播无线电系统的测试
  • 紧急广播无线电系统正在测试中
  • 紧急br0adcast无线电系统现已启动
  • 紧急宽带系统正在测试中
  • 请不要惊慌,因为这只是一个测试
  • 请不要惊慌,因为这只是2次测试
  • 恳求se不要被@larm3d,因为t7is只是一个最好的工具
  • 现在回到你的常规节目
  • 现在回到你的常规计划
  • 现在回到你的re@flarly定时程序设计
更正为:

  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
  • 这是对应急广播无线电系统的测试
这是我的节目

function repairDuplicates() {
  try {

    // necessities
    var ui = SpreadsheetApp.getUi();
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var as = ss.getActiveSheet();


    var sr = as.getDataRange();   // entire sheet range
    var ar = as.getActiveRange(); // active range

    var srv = sr.getValues(); // values of entire sheet
    var arv = ar.getValues(); // values of active range

    function minify(x){ // take a string. trim, remove all white space, convert to array, capitalize, remove special characters
      var txt = x.trim();
      txt = txt.replace(/ /g, '');
      var arr = txt.split('');
      arr = arr.map(function(a){return a.toUpperCase()});
      var std = arr.sort();
      var reg = new RegExp(/\W{1}/);
      for (var z = 0; z < std.length; z++){
        if (reg.exec(std[z]) != null){
          std.splice(z,1);
        }
      }
      return std;
    }


    function compareLength(x,y){ //take two arrays and compare length. if the length is different, then make it the same length by adding "x" n number of times. this will modify the arrays
      var xl = x.length;
      var yl = y.length;
      if (xl < yl){
        for (xl; xl < yl; xl++){
          x.push("x");
        }
      }
      else if (yl < xl){
        for (yl; yl < xl; yl++){
          y.push("x");
        }
      }
    }

    function compareData(x,y){ //take two arrays and compare data. if the arrays match, return "match". else if the arrays are more than (50)% similar, return "pass". else, return "fail"
      var z = [];
      var result;
      if (x.toString() == y.toString()){
        result = "match";
      }
      else if (x.toString() != y.toString()){
        var z = y.filter(function (word) {
          return x.indexOf(word) != -1;
        });

        //ui.alert(x);
        //ui.alert(x.length);
        //ui.alert(z);
        //ui.alert(z.length);

        var score = z.length/x.length;

        if(score > 0.5){
          result = "pass";
        }
        else{
          result = "fail";
        }
      }
      return result;
    }

//    function checkFirst(r, c, x, y){ // deprecated. no need to verify first cell
//      if(r == 0 && c == 0){
//        x = y;
//      }
//      return x;
//    }


    function iterate(range, values, text){ // takes a data range, the values of the range, and a string. 
                                           // iterates over range data (row by row, and cell by cell in each row).
                                           // each current cell is checked and compared against the string.
                                           // if the current cell passes the similarity check (compareData), then make the current cell equal to the previous cell
                                           // if the current cell fails the similarity check, then do not change the value of the cell, but make the string(text) equal to the current cell
      for (var r = 0; r < values.length; r++) {
        var row = values[r];
        for (var c = 0; c < row.length; c++) {
          var cell = row[c];
          //ui.alert("using: " + text);
          //ui.alert(cell);
          var arr = minify(cell);
          var minTxt = minify(text);
          compareLength(minTxt, arr);
          var t = compareData(minTxt, arr);
          //ui.alert(cell + ": " + t)
          if (t == "pass"){
            values[r][c] = text;
          }
          else if (t == "fail"){
            ui.alert("\"" + text + "\"" + "\n \n" +"does not match" + "\n \n" + "\"" + cell + "\"" +  " on row number " + (r + 2) + "..." + "\n \n" + "\"" + cell + "\"" + " is the new comparison string");
            text = cell;
          }
        }
      }
      range.setValues(values);
    }



    // main loop
    var txt = ar.getValue();
    //ui.alert(txt);
    iterate(ar, arv, txt);


  }
  catch (err) {
    ui.alert(err);
  }
}
功能修复重复(){
试一试{
//必需品
var ui=SpreadsheetApp.getUi();
var ss=SpreadsheetApp.getActiveSpreadsheet();
var as=ss.getActiveSheet();
var sr=as.getDataRange();//整个工作表范围
var ar=as.getActiveRange();//活动范围
var srv=sr.getValues();//整个工作表的值
var arv=ar.getValues();//活动范围的值
函数minify(x){//获取字符串。修剪,删除所有空白,转换为数组,大写,删除特殊字符
var txt=x.trim();
txt=txt.replace(//g',);
var arr=txt.split(“”);
arr=arr.map(函数(a){返回a.toUpperCase()});
var std=arr.sort();
var reg=newregexp(/\W{1}/);
对于(变量z=0;z0.5){
结果=“通过”;
}
否则{
结果=“失败”;
}
}
返回结果;
}
//函数checkFirst(r,c,x,y){//已弃用。无需验证第一个单元格
//如果(r==0&&c==0){
//x=y;
//      }
//返回x;
//    }
函数iterate(范围、值、文本){//获取数据范围、范围的值和字符串。
//迭代范围数据(逐行和每行中的逐单元格)。
//检查每个当前单元格并与字符串进行比较。
//如果当前单元格通过了相似性检查(compareData),则将当前单元格设置为