google-apps-script - 如何在保留条件格式的同时移动 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);
}
}
这是样本表。
解决方案
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:B
etc。
https://www.goskills.com/Excel/Resources/Absolute-reference-Excel
推荐阅读
- swift - 如何按值的升序对字典进行排序?
- python - 使用 Python 检查用户名是否存在于数据库中
- jquery - 如果少于 8 个帖子,如何使加载更多按钮不显示
- google-chrome-devtools - 使用 Puppeteer 和 Chrome DevTools 协议,如何获得 4 个性能计时指标?
- r - 如何在具有多个条件的 r 中使用 ifelse 将另一个变量更改为 NA
- java - Eclipse 上的 JavaFX 11 和 Oracle JDK 11 - 执行 JAR 文件时出错
- xml - 需要使用 powershell 更新特定的 XML 节点值
- r - 如何计算列名中具有相似模式的不同数据框中的列之间的差异?
- r - 使用ggplot2将日期显示为特定月份或日/月的唯一日期?
- java - 在 Firestore 中获取服务器时间戳时出错