Can I add a Trigger based on change of a particular cell?

我只是一个虾纸丫 提交于 2020-04-16 02:56:36

问题


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.

Is this possible?


回答1:


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

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.



来源:https://stackoverflow.com/questions/30426124/can-i-add-a-trigger-based-on-change-of-a-particular-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!