简体   繁体   English

通过 Google Apps 脚本在 Google 表格中匹配整个单元格以进行多次查找和替换

[英]Match Entire Cell for Multiple Find and Replace in Google Sheets via Google Apps Script

First question here - I'm trying to use the Multiple Find and Replace in Google App Scripts for Google Sheets from this thread , however, I need to do an exact match on the cell.这里的第一个问题 - 我正在尝试使用 Google 应用程序脚本中的多个查找和替换来自此线程的 Google 表格,但是,我需要对单元格进行精确匹配。 I did some research and see mentions of the class TextFinder and method matchExactCell, but I am stumped on where to add it.我做了一些研究,看到提到了 class TextFinder 和方法 matchExactCell,但我不知道在哪里添加它。

When the script is run multiple times, then the first name in the replace is appended multiple times so the replaced cell reads: "John John John Smith" if script is run 3 times.当脚本运行多次时,替换中的名字会多次附加,因此被替换的单元格显示为:如果脚本运行 3 次,则为“John John John Smith”。

Any recommendations are appreciated!任何建议表示赞赏! Thanks!谢谢!

function runReplaceInSheet(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  //  get the current data range values as an array
  //  Fewer calls to access the sheet -> lower overhead 
  var values = sheet.getDataRange().getValues();  

  // Replace Names
  replaceInSheet(values, 'Smith', 'John Smith');
  replaceInSheet(values, 'Doe', 'Jane Doe');

  // Write all updated values to the sheet, at once
  sheet.getDataRange().setValues(values);
}

function replaceInSheet(values, to_replace, replace_with) {
  //loop over the rows in the array
  for(var row in values){
    //use Array.map to execute a replace call on each of the cells in the row.
    var replaced_values = values[row].map(function(original_value) {
      return original_value.toString().replace(to_replace,replace_with);
    });

    //replace the original row values with the replaced values
    values[row] = replaced_values;
  }
}

You can try with this little modification that does not use the function "replace" but compares the whole value with "replace_to" and returns "replace_with" if it's equal or "original_values" if it's not:您可以尝试这个不使用 function“替换”但将整个值与“replace_to”进行比较的小修改,如果相等则返回“replace_with”,否则返回“original_values”:

function replaceInSheet(values, to_replace, replace_with) {
  //loop over the rows in the array
  for(var row in values){
    //use Array.map to execute a replace call on each of the cells in the row.
    var replaced_values = values[row].map(function(original_value) {
      if(original_value == to_replace) {return replace_with}
      else {return original_value};
    });

    //replace the original row values with the replaced values
    values[row] = replaced_values;
  }
}

As another approach, from I did some research and see mentions of the class TextFinder and method matchExactCell, but I am stumped on where to add it.作为另一种方法, I did some research and see mentions of the class TextFinder and method matchExactCell, but I am stumped on where to add it. , if you want to use TextFinder, how about the following sample script? ,如果你想使用 TextFinder,下面的示例脚本怎么样?

Sample script:示例脚本:

function sample1() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var obj = [
    { find: "Smith", replacement: "John Smith" },
    { find: "Doe", replacement: "Jane Doe" }
  ];
  var range = sheet.getDataRange();
  obj.forEach(({ find, replacement }) => range.createTextFinder(find).matchEntireCell(true).replaceAllWith(replacement));
}
  • Although the process cost of TextFinder is low, in this case, the replacement is run in a loop.虽然 TextFinder 的处理成本很低,但在这种情况下,替换是循环运行的。 If you want to reduce the process cost more, how about the following sample script?如果你想更多地降低流程成本,下面的示例脚本怎么样? In this sample, Sheets API is used.在此示例中,使用了工作表 API。 By this, the process cost is lower a little than that of the above.这样一来,工艺成本比上面的要低一些。 Before you use this script, please enable Sheets API at Advanced Google services .在使用此脚本之前, 请在 Advanced Google services 中启用 Sheets API

     function sample2() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheetId = ss.getSheetByName("Sheet1").getSheetId(); var obj = [ { find: "Smith", replacement: "John Smith" }, { find: "Doe", replacement: "Jane Doe" } ]; var requests = obj.map(({ find, replacement }) => ({ findReplace: { find, replacement, range: { sheetId }, matchEntireCell: true } })); Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId()); }

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM