我可以根据特定单元格的变化添加触发器吗? [英] Can I add a Trigger based on change of a particular cell?

查看:46
本文介绍了我可以根据特定单元格的变化添加触发器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个当前设置为运行onEdit的脚本.理想情况下,我希望脚本仅在有人更改相关工作表的单元格B26时才运行.

I have a script that is currently set to run onEdit. Ideally, I'd like the script to run ONLY when someone makes a change to cell B26 of the sheet in question.

这可能吗?

推荐答案

下面的代码直接来自文档:

This is code taken directly from the documentation:

function onEdit(e){
  // Set a comment on the edited cell to indicate when it was changed.
  var range = e.range;
  range.setNote('Last modified: ' + new Date());
}

它已经有e.range了.您所需要做的就是添加和if语句,并获取范围的单元格地址:

It already has e.range in it. All you need to do is add and if statement, and get the cell address of the range:

function onEdit(e){
  var cellAddress,cellAddressToTestFor;

  cellAddressToTestFor = 'B26';

  // Get cell edited - If it's B6 then do something
  cellAddress = e.range.getA1Notation();
  Logger.log('cellAddress: ' + cellAddress);

  if (cellAddress === cellAddressToTestFor) {
    //To Do - Code here if cell edited is correct
    Logger.log('the check worked!');
  };
}

添加该代码.编辑一个单元格,然后查看日志.

Add that code. Edit a cell, then VIEW the LOGS.

这篇关于我可以根据特定单元格的变化添加触发器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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