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

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

问题描述

我在Google电子表格中获得了两个 onEdit()函数脚本。但是看起来像是一个函数在一次工作。



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



以下是脚本。



function colorAll()
{
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 4;
var endRow = sheet.getLastRow();
for(var r = startRow; r <= endRow; r ++){
colorRow(r);
}
}
SpreadsheetApp.flush();
函数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);



函数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);


$ / code>

日期函数正在工作,但 colorRow 函数不是,如果我删除了日期脚本,那么 colorRow 就可以工作。



任何一个人都可以指向正确的方向吗?好像我缺少一些东西



谢谢

解决方案

Barry Smith的评论关于两个同名的函数是正确的;您可以只有一个名为 onEdit()的电子表格包含的函数 。如果您想将另一个函数用作 onEdit 触发器,则需要将其设置为可安装触发器。



您可以使用资源 - >当前项目的触发器来安装第二个触发器。





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



背景:触发指南


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

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.

Here are the scripts.

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();

And the second function.

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);
  }
}

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

Thanks

解决方案

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

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.

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

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.

Background: Guide to Triggers.

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

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