指定要在Google脚本上启动的行

时间:2012-07-06 00:46:03

标签: google-apps-script triggers google-sheets

我希望google电子表格上的这个onEdit脚本从第2行开始,它会跳过我的标题行,但是我无法使用现有代码? 有人可以帮助一个菜鸟吗?

function onEdit(e)
{
  var ss = e.source.getActiveSheet();
  var rr  = e.source.getActiveRange();


//comment 2 lines below if you want it working on all sheets, not just on 2nd one
  if(ss.getIndex()!= 1)
    if(ss.getIndex()!= 2) 
      if(ss.getIndex()!= 3) 
  return;
 ///

  var firstRow = rr.getRow();
  var lastRow = rr.getLastRow();

//the last modified date will appear in the 43th column which is the Last Update Column
  for(var r=firstRow; r<=lastRow; r++)
    ss.getRange(r, 43).setValue(new Date());
}

1 个答案:

答案 0 :(得分:1)

只需检查当前行是否在标题内,然后退出触发器:

function onEdit(e)
{
  var rr = e.range;
  var ss = e.range.getSheet();

  var headerRows = 1;  // # header rows to ignore

  if (rr.getRow() <= headerRows) return;
  ...