问题描述
我正在为我的老板的电子表格,我一直在试图增加在那里从Sheet1整行移动到Sheet2时COLUMN1的复选框被选中的特征.我正在使用的代码可以工作,除了有时即使一次只选中一个框,它也会一次移动两行.这是我的代码:
I'm working on a spreadsheet for my boss and I've been trying to add a feature where the entire row moves from Sheet1 to Sheet2 when the checkbox in column1 is checked. The code I'm using works, except sometimes it moves two rows at once even though only one box is checked. Here's my code:
function onEdit(event) {
// assumes source data in sheet named Sheet1
// target sheet of move to named Sheet2
// test column with yes/no is col 1 or A
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Sheet1" && r.getColumn() == 1 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet2");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
我完全没有代码经验.我是从约书亚·多恩(Joshua Doan)回答其他人的问题中得到的:用于根据复选框移动行的Google脚本-Google工作表
I am NOT experienced with code at all. I got this from Joshua Doan's reply to someone else's question: Google script for move rows depending on checkbox - Google sheet
所以我的问题是,如何使它停止一次移动两行?
So my question is, how do I get it to stop moving two rows at once??
非常感谢!
推荐答案
function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()!='Sheet1')return;
if(e.range.columnStart==1 && e.value=="TRUE") {
var row1=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn());
var tsh=e.source.getSheetByName('Sheet2');
row1.moveTo(tsh.getRange(tsh.getLastRow()+1,1));
sh.deleteRow(e.range.rowStart);
}
}
这篇关于复选框为true时,如何阻止代码一次移动两行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!