简体   繁体   English

如何运行多个onEdit函数

[英]How to run multiple onEdit functions

I have two onEdit functions that work individually, but I cannot seem to combine them properly to run at the same time. 我有两个分别可运行的onEdit函数,但似乎无法正确组合它们以同时运行。

My goals is to timestamp the cell in column 9 AND copy the whole row to a new sheet when the checkbox is checked in column 1. 我的目标是为第9列中的单元格添加时间戳,并在第1列中选中此复选框时,将整行复制到新工作表中。

I have followed the advice posted elsewhere on Stackoverflow regarding triggers, renaming the functions, etc, but at most I can only get the first function to run. 我遵循了在Stackoverflow上其他地方发布的有关触发器,重命名函数等的建议,但是至多我只能让第一个函数运行。

function onEdit(e) {

var colToWatch = 1, colToStamp = 9;
var valueToWatch = "TRUE"; 
if (e.range.columnStart === colToWatch && (e.value === valueToWatch || 
typeof e.value == 'object'))
e.source.getActiveSheet()
    .getRange(e.range.rowStart, colToStamp)
    .setValue(typeof e.value === 'object' ? null : new Date());
}

function onEdit(event) {
// assumes source data in sheet named Active
// target sheet of move to named Found 
// 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() == "Jessica" && r.getColumn() == 1 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Done");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}

}

I expected both functions to run, but I fear I've taken too many disparate pieces of advice, and now neither function runs on edit. 我希望两个函数都可以运行,但是我担心我已经接受了太多不同的建议,现在两个函数都不能在编辑时运行。

You cannot have two functions sharing the same name, and therefore will encounter issues trying to have two separate onEdit() functions. 您不能有两个共享相同名称的函数,因此在尝试使用两个单独的onEdit()函数时会遇到问题。 Rewrite your script so that you have a single function; 重写脚本,以便只有一个功能; and also give a read through the guide on triggers 并通读触发器指南

In the timestamp function, you are using two undefined properties of Range : e.range.columnStart and e.range.rowStart . 在时间戳功能中,您使用的是Range的两个未定义属性: e.range.columnStarte.range.rowStart Your code works when you replace them with e.range.getColumn() and e.range.getRow() . 将代码替换为e.range.getColumn()e.range.getRow()时,您的代码将起作用。

I suggest renaming the functions to describe their function, and then make a new onEdit that calls these functions in the correct order. 我建议重命名功能以描述它们的功能,然后创建一个新的onEdit ,以正确的顺序调用这些功能。

Here is a working example: 这是一个工作示例:

function onEdit(e) {
  timestamp(e);
  copyRow(e);
}

function timestamp(e) {
  var colToWatch = 1, colToStamp = 9;
  var valueToWatch = "TRUE"; 
  if (e.range.getColumn() === colToWatch &&
     (e.value === valueToWatch || typeof e.value == 'object')) {
    e.source.getActiveSheet()
      .getRange(e.range.getRow(), colToStamp)
      .setValue(typeof e.value === 'object' ? null : new Date());
  }
}

function copyRow(event) {
  // assumes source data in sheet named Active
  // target sheet of move to named Found 
  // 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() == "Jessica" &&
      r.getColumn() == 1 &&
      r.getValue() == true) {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Done");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).copyTo(target);
  }
}

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

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