首页 > 解决方案 > 如何在保留条件格式的同时移动 Google 表格行?

问题描述

我有一种情况,当状态更改为“全部选中”时,我需要移动整行,如 COL 所示A3,在这里我想提一下,我在 COLA3中有数组论坛,它与条件格式一起计算状态。

到目前为止,我发现下面的代码可以帮助我将整行移动到工作表的底部,只有当我在 COL 中手动输入“All Picked”时A,但实际上 COLA是通过数组公式自动计算状态:

=arrayformula(if(B3:B="",,if(E3:E = 0,"All Picked",if((E3:E <> 0) * (D3:D < C3:C) * not((D3:D = 0) + (D3:D = "")),"partial qty picked",if((E3:E <> 0) * ((D3:D = 0) + (D3:D = "")),"Not yet picked","")))))

我试图弄清楚如何将状态 A 列值更改为“全部选中”后将行移至底部,从而保持公式不变。我不想手动插入状态。

工作表名称为:Sheet1,显示状态为 COLA 的列 状态变为“All Picked”时需要触发

这是我在 stackoverflow 上找到的代码,我不需要 HOLD 的条件,但由于我不知道它,我不知道如何更改它。

        function onEdit(e) {
          const row = e.range.getRow();
          const col = e.range.getColumn();
          const as = e.source.getActiveSheet();
          const lc = as.getLastColumn();
          if(as.getName() == "Sheet1" && col == 1  && row > 1 && as.getRange(row,col).getValue() == 'All Picked') {
            const row_new = as.getRange(row, 1, 1, lc);
            row_new.copyTo(as.getRange(as.getLastRow() + 1, 1, 1, lc));
            as.deleteRow(row);
          } else if(as.getName() == "Sheet1" && col == 2  && row > 1 && as.getRange(row,col).getValue() == 'HOLD'){
            as.insertRowAfter(1);
            const row_new = as.getRange(row + 1, 1, 1, lc);
            row_new.copyTo(as.getRange(2, 1, 1, lc));
            as.deleteRow(row + 1);
          }
       }

这是样本表

标签: google-apps-scriptgoogle-sheets

解决方案


C每当您更改列中的单元格或D列中的单元格A包含“All Picked”时,将当前行移动到表格底部的代码如下:

function onEdit(e) {

  // get sheet where edit was happen
  var as = e.source.getActiveSheet();
  if (as.getName() != "Sheet1") return; 

  // get current row
  var row = e.range.getRow();
  if (row == 1) return; // stop it's first row
 
  // get column
  var col = e.range.getColumn();
  if (col != 3 && col != 4 ) return; // stop if it's not 3 and not 4 column

  // get a first cell or the row
  var value = as.getRange("A"+row).getDisplayValue();
  if (value != 'All Picked') return; // stop if it doesn't contain 'All Picked'
  
  // move current row to the bottom of the table
  var lc = as.getLastColumn();
  var row_new = as.getRange(row, 1, 1, lc);
  row_new.copyTo(as.getRange(as.getLastRow() + 1, 1, 1, lc));
  as.deleteRow(row);
 
}

但我不明白你的公式。我不确定它的作用。代码将按原样复制公式(如果它在单元格中,表中的大多数行都不是这种情况)。

如果您需要公式中的行索引在复制期间不发生变化,则可能需要使用绝对引用B$3:B而不是B3:Betc。

https://www.goskills.com/Excel/Resources/Absolute-reference-Excel


推荐阅读