繁体   English   中英

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

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

这里的第一个问题 - 我正在尝试使用 Google 应用程序脚本中的多个查找和替换来自此线程的 Google 表格,但是,我需要对单元格进行精确匹配。 我做了一些研究,看到提到了 class TextFinder 和方法 matchExactCell,但我不知道在哪里添加它。

当脚本运行多次时,替换中的名字会多次附加,因此被替换的单元格显示为:如果脚本运行 3 次,则为“John John John Smith”。

任何建议表示赞赏! 谢谢!

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;
  }
}

您可以尝试这个不使用 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;
  }
}

作为另一种方法, I did some research and see mentions of the class TextFinder and method matchExactCell, but I am stumped on where to add it. ,如果你想使用 TextFinder,下面的示例脚本怎么样?

示例脚本:

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));
}
  • 虽然 TextFinder 的处理成本很低,但在这种情况下,替换是循环运行的。 如果你想更多地降低流程成本,下面的示例脚本怎么样? 在此示例中,使用了工作表 API。 这样一来,工艺成本比上面的要低一些。 在使用此脚本之前, 请在 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()); }

参考:

暂无
暂无

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

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