【问题标题】:Edit cell value from filtered row in app script for google sheets编辑谷歌表格应用脚本中过滤行中的单元格值
【发布时间】:2021-05-12 12:50:08
【问题描述】:

问题与this answer有关。

我正在过滤一些具有特定单元格值的行:

.filter(row => row[position_1] == 'value')

我想更新所选行中的另一个单元格:

row[position_2] = 'new_value'

我尝试包含类似上一行代码的内容,但它不起作用。

所以,如果我们有这样的代码:

function selectRecords() {
  const ss = SpreadsheetApp.getActiveSheet();
  const dataRange = ss.getDataRange();
  const headers = 2;
  const dataValues = dataRange
    .offset(headers, 0, dataRange.getNumRows() - headers)//offsetting the headers from the whole range
    .getValues();

  dataValues
    .filter(row => row[position_1] == 'value') 
    .forEach(row => {
           //update the value of a specific cell within the row.
           //extract some cell's values: row[pos_x], row[pos_y], row[pos_z],.. to use it inside the loop
    });
}

如何更新每个过滤行的单元格?

【问题讨论】:

    标签: javascript google-apps-script google-sheets


    【解决方案1】:

    在其他方法中,我想建议使用 TextFinder 和 RangeList 来实现您的目标。示例脚本如下。

    示例脚本:

    请设置position_1position_2searchTextreplaceText的变量。

    function selectRecords() {
      const position_1 = 1; // From your script, 1st index is 0. So, 1 is the column "B".
      const position_2 = 2; // From your script, 1st index is 0. So, 2 is the column "C".
      const searchText = "value"; // Please set the search text.
      const replaceText = "new_value"; // Please set the replace text.
    
      // 1. Retrieve the search range.
      const ss = SpreadsheetApp.getActiveSheet();
      const headers = 2;
      const range = ss.getRange(3, position_1 + 1, ss.getLastRow() - headers);
    
      // 2. Search the searchText and retrieve the range list.
      const rangeList = range.createTextFinder(searchText).matchEntireCell(true).findAll().map(r => ss.getRange(r.getRow(), position_2 + 1).getA1Notation());
    
      // 3. Replace the cell values using the range list.
      if (rangeList.length > 0) ss.getRangeList(rangeList).setValue(replaceText);
    }
    
    • 此脚本的流程如下。
      1. 检索搜索范围。
      2. 搜索 searchText 并检索范围列表。
      3. 使用范围列表替换单元格值。
    • 在这种情况下,只有要替换的单元格值是替换的。因此,即使被替换单元格以外的单元格有公式,也可以使用此脚本。

    参考资料:

    【讨论】:

    • 非常清晰且解释清楚的答案。在我的 forEach 中,我需要提取一些单元格的值(因为我使用它来构建 BigQuery 的更新)。例如:行[x]、行[y]、行[z]。有没有办法使用您的代码提取这些单元格的值?因为现在我需要保留这两个功能,并且我想同时提高性能。
    • @IoT 用户感谢您的回复。我很高兴你的问题得到了解决。关于你的新问题,我想支持你。但我必须为我糟糕的英语水平道歉。来自For example: row[x], row[y], row[z]. Is there any way to extract these cell's values using your code?,我无法理解您的新问题。为了正确理解你的新问题,我可以问你关于你的新问题的细节吗?您能否提供您期望的示例输入和输出值?通过这个,我想尝试理解你的新问题。
    【解决方案2】:

    您必须改用 setValues

    我会在箭头函数内使用带有条件的 .map() 函数,因为电子表格的性质与 Apps 脚本有关,它需要二维值数组才能设置值。这是我的方法:

    function selectRecords() {
      const ss = SpreadsheetApp.getActiveSheet();
      const dataRange = ss.getDataRange();
      const headers = 2;
      const dataValues = dataRange
        .offset(headers, 0, dataRange.getNumRows() - headers)//offsetting the headers from the whole range
        .getValues();
    
      dataValues = dataValues.map(row => {
                     if(row[position_1] !== 'value') return row
                     
                     // Update the value and return row
                     row[position_2] = 'new_value'
                     return row
                   });
    
      dataRange
        .offset(headers, 0, dataRange.getNumRows() - headers)
        .setValues(dataValues);
    }
    

    【讨论】:

      【解决方案3】:

      假设我们要查找第一个单元格中包含“a1”的所有行,并将这些行的第二个单元格更改为“zzz”:

      var dataValues = [
          ["a1", "b1", "c1"],
          ["a2", "b2", "c2"],
          ["a3", "b3", "c3"],
          ["a1", "b1", "c1"],
          ["a2", "b2", "c2"],
          ["a3", "b3", "c3"]
      ];
      
      var value      = "a1";
      var position_1 = 0;
      var new_value  = "zzz";
      var position_2 = 1;
      
      var filtered_dataValues = dataValues.filter(row => {
          if (row[position_1] == value) { row[position_2] = new_value; return row; }
          else { return false; }
      });
      
      console.log(filtered_dataValues); // [ [ 'a1', 'zzz', 'c1' ], [ 'a1', 'zzz', 'c1' ] ]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多