我有这个小脚本,目前使用简单的"编辑"触发器,但我想从菜单中运行它,因此您可以批量移动行而不是一次移动一行。似乎"编辑"触发功能需要几秒钟的时间来更新,当你重新洗牌时,你可以在列表中失去它的位置。
我的目标:
虽然我对Apps Script非常陌生,但我可以看到它为什么不起作用 - 我只是不知道如何改变它!当前脚本正在查看已编辑的行,但我希望它能有效地扫描工作表以查找具有该状态的任何行。
原始代码:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
var Colno = 8 //Column to be checked
if (s.getName() == "Sheet1" && r.getColumn() == Colno && r.getValue() == "C") {
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);
} else if (s.getName() == "Sheet1" && r.getColumn() == Colno && r.getValue() == "A") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet3");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
答案 0 :(得分:0)
您需要分离程序的操作
function mover(s, r) {
var ss = s.getParent();
if (s.getName() == "Sheet1" && r.getColumn() == Colno && r.getValue() == "C") {
SpreadsheetApp.getActiveSpreadsheet().toast('if');
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);
} else if (s.getName() == "Sheet1" && r.getColumn() == Colno && r.getValue() == "A") {
SpreadsheetApp.getActiveSpreadsheet().toast('elseif');
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet3");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
我必须重命名。
function onEditTrg(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
mover(s, r);
}
function menuCall() {
var s = SpreadsheetApp.getActiveSheet();
var r = SpreadsheetApp.getActiveRange();
mover(s, r);
}
作为补充,我建议使用onEdit触发器的开关
function onOpen() {
SpreadsheetApp.getUi().createMenu('Mover')
.addItem('Move current', 'menuCall')
.addItem('Switch onEdit()', 'switchOnEdit')
.addToUi();
}
function switchOnEdit() {
var isExist = false;
var tgrs = ScriptApp.getUserTriggers(SpreadsheetApp.getActiveSpreadsheet());
for (var i = 0; i < tgrs.length; i++) {
if (tgrs[i].getHandlerFunction() == 'onEditTrg' && tgrs[i].getEventType() == ScriptApp.EventType.ON_EDIT) {
isExist = true;
ScriptApp.deleteTrigger(tgrs[i]);
}
}
if (isExist) {
SpreadsheetApp.getActiveSpreadsheet().toast('onEdit() is off', 'Switcher onEdit()')
} else {
ScriptApp.newTrigger('onEditTrg')
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onEdit()
.create();
SpreadsheetApp.getActiveSpreadsheet().toast('onEdit() is on', 'Switcher onEdit()')
}
}
Colno
应该是一个全局变量。
对我来说很好。完整代码here。