两个 OnEdit 函数不能一起工作 [英] Two OnEdit functions not working together

查看:23
本文介绍了两个 OnEdit 函数不能一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Google 电子表格中得到了两个 onEdit() 函数脚本.但似乎一次只能运行一个功能.

I got two onEdit() function scrips on a Google spreadsheet. But seems like one function is working at a time.

第一个函数是为所有行着色的脚本,第二个函数是基于列编辑在 2 个单元格上添加日期的日期函数.

First function is a script to color all Rows, and second one is date function for adding dates on 2 cells based on column edit.

这是脚本.

function colorAll() 
{
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 4;
  var endRow = sheet.getLastRow();
  for (var r = startRow; r <= endRow; r++) {
    colorRow(r);
  }
}
SpreadsheetApp.flush();
function colorRow(r)
{
  var sheet = SpreadsheetApp.getActiveSheet();
  var dataRange = sheet.getRange(r, 1, 1, 32);
  var data = dataRange.getValues();
  var row = data[0];
  SpreadsheetApp.flush();
  if(row[14] === ""){
    dataRange.setBackgroundRGB(255, 255, 255);
    dataRange.setFontColor("#000000");
  }
  else if(row[14] === "BEING USED") {
    dataRange.setBackgroundRGB(150, 185, 255);
    dataRange.setFontColor("#004BE1");
  }
}
function onEdit(event)
{
  var r = event.source.getActiveRange().getRowIndex();
  if (r >= 2) {
    colorRow(r);
  }
}

function onOpen(){
  colorAll();

还有第二个函数.

function onEdit(e) {
  var aCell = e.source.getActiveCell(), col = aCell.getColumn(); 
  if(col == 19 || col == 21) {
    var adjacentCell = aCell.offset(0, 1);  
    var newDate = Utilities.formatDate(new Date(), 
      "GMT+1", "dd/MM/yyyy");
    adjacentCell.setValue(newDate);
  }
}

日期函数正在运行,但 colorRow 函数不起作用,如果我删除日期脚本,则 colorRow 将起作用.

The date function is working but the colorRow function is not, if I remove the date script then the colorRow will work.

有人能指出我正确的方向吗?好像遗漏了什么

Can any one point me in the right direction? Seems like I am missing something

谢谢

推荐答案

Barry Smith 关于两个同名函数"的评论是对的;在这种情况下,只有第二个会执行.

Barry Smith's comment about "two functions with same name" is right; only the second would execute in that case.

您只能拥有一个名为 onEdit() 的包含电子表格的函数.如果您想将另一个函数用作 onEdit 触发器,则需要将其设置为 可安装 触发器.

You can have just one spreadsheet-contained function named onEdit(). If you want to use another function as an onEdit trigger, you need to set it up as an installable trigger.

您可以使用 Resources -> 中的对话框当前项目的触发器安装第二个触发器.

You can use the dialog from Resources -> Current Project's Triggers to install the second trigger.

或者,您可以只有一个 onEdit() 简单触发器,但让它调用子触发器"函数,将事件对象 e 传递给每个触发器

Alternatively, you could have just one onEdit() simple trigger, but have it call the "sub-trigger" functions, passing the event object e to each of them.

背景:触发器指南.

这篇关于两个 OnEdit 函数不能一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆