繁体   English   中英

编辑谷歌表格应用脚本中过滤行中的单元格值

[英]Edit cell value from filtered row in app script for google sheets

这个问题与这个答案有关。

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

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

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

您必须改用 setValues

我会改用.map() function 和箭头 function 内的条件,因为电子表格的性质与 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);
}

在其他方法中,我想建议使用 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. 使用范围列表替换单元格值。
  • 在这种情况下,只有要替换的单元格值才会被替换。 因此,即使被替换的单元格以外的单元格具有公式,也可以使用此脚本。

参考:

假设我们要查找第一个单元格中包含“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' ] ]

暂无
暂无

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

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