【问题标题】:What is the most efficient way of finding and replacing multiple strings of a cell in a range?在一个范围内查找和替换多个单元格字符串的最有效方法是什么?
【发布时间】:2021-04-23 21:03:57
【问题描述】:

**背景**

  • 我正在编写用于在格斗游戏中制作连击的代码
  • 我需要取一个范围,然后将某些数字和/或字母转换为其他数字/和/或带有附加字符/间距的字母,以便程序注册组合。

现在我有一些代码可以完成工作,但我不认为这是“正确”或有效的方法,因为总共将设置大约 40 个“开关”:

 function printCombo()  {
//general set up
  const ss=SpreadsheetApp.getActive();
  const psh=ss.getSheetByName('Print Sheet');
  const scanrange= psh.getRange("Range to be scanned");

//set constants and text they are being replaced with
  const 1A = scanrange.createTextFinder('1A');
  1A.replaceAllWith('1 A');
  const 2B = scanrange.createTextFinder('2B');
  2B..replaceAllWith('2   B');
  const 3C = scanrange.createTextFinder('3C');
  3C.replaceAllWith('');
  const 4D = scanrange.createTextFinder('4D');
  4D.replaceAllWith('4 ]D[ 4');
  const xy = scanrange.createTextFinder('xy');
  4D.replaceAllWith('xy's replacement');
}

另外,如果有关于此类主题的初学者指南,请告诉我,因为在脚本编码的逻辑和操作顺序方面我仍然是初学者

谢谢!!

奖金问题: 有没有办法做到这一点,如果一个单元格是“粗体”/某种其他形式的样式,它会在该单元格的任何字符周围放置一个 ][ 加粗?例如 "3B" 将变为 "3 ]B[" 而不是 "3 B"

【问题讨论】:

    标签: google-apps-script google-sheets replace range replaceall


    【解决方案1】:

    我相信你的目标如下。

    • 您希望降低脚本的处理成本。
    • 在您的情况下,您想用大约 40 种模式替换文本。

    修改点:

    • 我认为const 1A =const 2B =等变量不能使用。请注意这一点。
    • 关于4D.replaceAllWith('xy's replacement');,在这种情况下,xy's replacement 没有被包围。请注意这一点。
    • 关于2B..replaceAllWith('2 B');..不正确。
    • 为了减少脚本的处理成本,在这个答案中,我想建议使用 Sheets API。使用 Sheets API 时,只需一次 API 调用即可将文本替换为大约 40 种模式。我认为这样可以降低工艺成本。

    当以上几点反映到你的脚本中时,它变成如下。

    修改脚本:

    在使用此脚本之前,please enable Sheets API at Advanced Google services

    function printCombo() {
      //general set up
      const ss = SpreadsheetApp.getActive();
      const psh=ss.getSheetByName('Print Sheet');
      const scanrange= psh.getRange("Range to be scanned");
    
      // I modified below script.
      // Please set the replace patterns for this object. This object uses your patterns.
      const replacePatterns = [
        { search: '1A', replace: '1 A' },
        { search: '2B', replace: '2   B' },
        { search: '3C', replace: '' },
        { search: '4D', replace: '4 ]D[ 4' },
        { search: 'xy', replace: "xy's replacement" }
      ];
      const sheetId = psh.getSheetId();
      const startRowIndex = scanrange.getRow() - 1;
      const endRowIndex = startRowIndex + scanrange.getNumRows();
      const startColumnIndex = scanrange.getColumn() - 1;
      const endColumnIndex = startColumnIndex + scanrange.getNumColumns();
      const requests = replacePatterns.map(({ search, replace }) => ({ findReplace: { range: { sheetId: sheetId, startRowIndex: startRowIndex, endRowIndex: endRowIndex, startColumnIndex: startColumnIndex, endColumnIndex: endColumnIndex }, find: search, replacement: replace } }));
      Sheets.Spreadsheets.batchUpdate({ requests: requests }, ss.getId());
    }
    
    • 运行上述脚本时,scanrange 的单元格值将使用replacePatterns 的替换模式进行替换。

    注意:

    • 关于您的Bonus Question,在这种情况下,我认为可能需要使用上述修改的其他过程。因此,在这种情况下,我建议将其作为新问题发布。

    参考资料:

    补充:

    根据您的以下回复,

    这是一个很棒的解决方案,但是由于这是一个我需要能够让人们快速理解和使用的脚本,有没有办法在不激活高级工作表 API 情况的情况下进行这种设置?

    下面的示例脚本怎么样?

    function printCombo() {
      //general set up
      const ss = SpreadsheetApp.getActive();
      const psh=ss.getSheetByName('Print Sheet');
      const scanrange= psh.getRange("Range to be scanned");
    
      // I modified below script.
      // Please set the replace patterns for this object. This object uses your patterns.
      const replacePatterns = [
        { search: '1A', replace: '1 A' },
        { search: '2B', replace: '2   B' },
        { search: '3C', replace: '' },
        { search: '4D', replace: '4 ]D[ 4' },
        { search: 'xy', replace: "xy's replacement" }
      ];
      replacePatterns.forEach(({ search, replace }) => scanrange.createTextFinder(search).replaceAllWith(replace));
    }
    

    【讨论】:

    • 这是一个很棒的解决方案,但是由于这是一个脚本,我需要能够让人们快速理解和使用,有没有办法在不激活高级工作表的情况下进行这种设置API情况?
    • @Kuukuu Omoi 感谢您的回复。我带来的不便表示歉意。为了您的回复,我在回答中又添加了一个示例脚本。你能确认一下吗?如果这不是您期望的方向,我再次道歉。
    • 完美。我很感激。
    • @Kuukuu Omoi 感谢您的回复。很高兴您的问题得到解决。
    • 据我所知,这两种方法都涉及创建一个数组,然后运行该数组以查找将要更改的内容,然后用什么替换它,对吗? .map 用于设置第一个替换,而第二个使用文本查找和替换。 (顺便说一句,这是我能理解的对 Foreach 最清晰的理解)
    猜你喜欢
    • 2010-10-24
    • 1970-01-01
    • 2011-07-25
    • 2011-10-22
    • 2021-02-11
    • 2018-03-28
    • 2014-06-23
    • 2015-01-11
    相关资源
    最近更新 更多