有没有一种方法可以更新此脚本,使其仅在编辑特定列中的单元格而不是任何单元格时才运行? [英] Is there a way to update this script so it only runs when a cell in a specific column is edited instead of any cell?

查看:45
本文介绍了有没有一种方法可以更新此脚本,使其仅在编辑特定列中的单元格而不是任何单元格时才运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google表格问题:我需要显示单元格的新值与其以前的值之间的差异,以设置TRUE值(如果差异大于x).此刻,我正在使用此脚本(对不起,我无法正确格式化该脚本!):

Google Sheets problem: I need to show the difference between a cell's new value and its previous value to set a TRUE value if the difference is greater than x. At the moment, I'm using this script (sorry, I can't get this to format correctly!):

function previous() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

    var source = ss.getRange('sheet1!B2:B');
  source.copyTo(ss.getRange('sheet2!C2:C'), {contentsOnly: true});
}

使用触发器,我将其设置为在进行编辑时运行,但是,我只希望它在B列中的单元格被编辑时运行.

Using a trigger, I set this up to run whenever an edit occurs, however, I only want it to run when a cell in column B is edited.

这可能吗?

我尝试在脚本中添加range.GetColumn()部分,但是,我是脚本的新手,并且不确定所需的语法(甚至可以使用).

I've tried adding in a range.GetColumn() section to the script, however, I'm new to scripts and I'm not sure of the syntax required (or even if this will work).

推荐答案

为此,您可以使用 onEdit(e)触发器,并使用默认添加的参数( e ).

To do this you can use the onEdit(e) trigger and use the parameter that is added by default (e).

该对象具有一个称为范围(e.range)的属性,其类型为 Range .

That object has a property called range (e.range) that is of type Range.

然后,您可以在函数内部检查范围是否在要检查的列之内.

Inside the function you can then check if that Range is within the column you want to check.

function onEdit(e) {
  if (e.range.getColumn()==3) {//for column C, as an example
     //execute here
  }
}

希望这会有所帮助!

这篇关于有没有一种方法可以更新此脚本,使其仅在编辑特定列中的单元格而不是任何单元格时才运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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