如何使onEdit()触发器功能适用于多张纸 [英] How to make onEdit() trigger function apply to multiple sheets

查看:162
本文介绍了如何使onEdit()触发器功能适用于多张纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个脚本可以填写列B 列A 时,它带有时间戳。不过,我需要它在第二个选项卡上做同样的事情,但我无法在那里工作。我需要改变什么?



我使用的当前脚本是:

  function onEdit(e){

var sheetToWatch ='Wrong Grading',
columnToWatch = 1,
columnToStamp = 2; //将所有这些改为你的需求

if(e.range.columnStart!== columnToWatch
|| e.source.getActiveSheet()。getName()!== sheetToWatch
||!e.value)
return;
e.source.getActiveSheet()
.getRange(e.range.rowStart,columnToStamp)
.setValue(new Date());


解决方案

保罗问题中的代码片段来自 Google文档帮助论坛上的代码,其中包括详细的逐行解释。



该函数使用变量 sheetToWatch 来标识一个工作表(又名选项卡), onEdit()函数关心。这是通过比较验证的:

  || e.source.getActiveSheet()。getName()!== sheetToWatch 

...如果当前触发事件的来源不匹配,函数将不做任何事情退出。



我需要更改什么?如果您想要函数在电子表格中的 all 工作表上工作,那么您可以完全取消这个检查:

 函数onEdit(e){

var columnToWatch = 1,
columnToStamp = 2; //将所有这些更改为您的需要

if(e.range.columnStart!== columnToWatch
||!e.value)
return;
e.source.getActiveSheet()
.getRange(e.range.rowStart,columnToStamp)
.setValue(new Date());

$ / code>

如果你想让 onEdit()在一组工作表上操作,然后您可以更改上面的比较,以检查当前工作表的名称是否在表格数组中找到。这将改变比较:

  || sheetsToWatch.indexOf(e.source.getActiveSheet()。getName())=== -1 

您可以了解更多关于 indexOf()方法的信息这里。它做的是获取事件触发器源表的名称,在 sheetsToWatch 数组中找到它,并返回找到的索引。如果该表未出现在数组中, indexOf()返回 -1



结果函数是:

$ p $ function onEdit(e){

var sheetsToWatch = ['Wrong Grading',
'Something Else'],
columnToWatch = 1,
columnToStamp = 2; (e.range.columnStart!== columnToWatch
|| sheetsToWatch.indexOf(e.source.getActiveSheet()。getName()); //将所有这些更改为您的需要

if === -1
||!e.value)
return;
e.source.getActiveSheet()
.getRange(e.range.rowStart,columnToStamp)
.setValue(new Date());
}


I have a Google Sheet I'm working on.

I have a script that fills in the column B with a timestamp when I update column A on the first tab. However I need it to do the same on the second tab, but I can't get it to work there. What do I need to change?

The current script I'm using is:

function onEdit(e) {

    var sheetToWatch= 'Wrong Grading',
        columnToWatch = 1,
        columnToStamp = 2;            //change all of these to your needs

    if (e.range.columnStart !== columnToWatch
      || e.source.getActiveSheet().getName() !== sheetToWatch
      || !e.value)
        return;
    e.source.getActiveSheet()
        .getRange(e.range.rowStart, columnToStamp)
        .setValue(new Date());
}

解决方案

For future readers, the code snippet in Paul's question is derived from code on the Google Docs help forum, which includes a detailed line-by-line explanation.

The function uses the variable sheetToWatch to identify one sheet (aka "tab") that the onEdit() function cares about. That is validated by this comparison:

|| e.source.getActiveSheet().getName() !== sheetToWatch

...and if the source of the current trigger event is not matched, the function exits without doing anything.

What do I need to change? If you want this function to work on all sheets in the Spreadsheet, then you can just eliminate this check altogether:

function onEdit(e) {

    var columnToWatch = 1,
        columnToStamp = 2;               //change all of these to your needs

    if (e.range.columnStart !== columnToWatch
      || !e.value)
        return;
    e.source.getActiveSheet()
        .getRange(e.range.rowStart, columnToStamp)
        .setValue(new Date());
}

If you want to have the onEdit() operate on a set of sheets, then you can change that above comparison to check if the current sheet's name is found in an array of sheet names. That will change the comparison to this:

  || sheetsToWatch.indexOf( e.source.getActiveSheet().getName() ) === -1 

You can learn more about the indexOf() method here. What it's doing though, is getting the name of the event trigger source sheet, finding it in the sheetsToWatch array, and returning the found index. If the sheet does not appear in the array, indexOf() returns -1.

The resulting function is:

function onEdit(e) {

    var sheetsToWatch= ['Wrong Grading',
                        'Something Else'],
        columnToWatch = 1,
        columnToStamp = 2;            //change all of these to your needs

    if (e.range.columnStart !== columnToWatch
      || sheetsToWatch.indexOf( e.source.getActiveSheet().getName() ) === -1 
      || !e.value)
        return;
    e.source.getActiveSheet()
        .getRange(e.range.rowStart, columnToStamp)
        .setValue(new Date());
}

这篇关于如何使onEdit()触发器功能适用于多张纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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