繁体   English   中英

将脚本运行到特定工作表 - Google 工作表

[英]Running script to a specific sheet - Google sheet

我想知道如何修改我的脚本以针对特定选项卡(检查)而不是所有选项卡。 下面是我的脚本:

function onEdit(e) {    
  var sheet, cols, colInd, offset, format;   
  sheet = e.source.getActiveSheet();
  cols = [7,13,14,16];
  colInd = cols.indexOf(e.range.columnStart);
  if (colInd == -1) return;
  if (colInd < 3) {
    offset = [4,-1,1,1];
    format = "MM/dd/yyyy HH:mm:ss";
    e.range.offset(0, offset[colInd])
    .setValue(Utilities.formatDate(new Date(), "GMT+8", format));
  } else {
    setValidation(e);
  }
}

这是我的样本表: https ://docs.google.com/spreadsheets/d/1hQhh3UlWm38ILADMqlCuv08GipMBDWVOF4l4gzM5tjE/edit#gid=2039637864

如果您想在编辑文档中的任何工作表时获得对“检查”工作表的引用, Raserhin的回答是有效的。

如果您只想在编辑“检查”表时运行代码,请使用以下代码。

function onEdit(e) {

    var sheet, cols, colInd, offset, format;   
    sheet = e.source.getActiveSheet();
    if (sheet.getName() === 'Check') {  // Condition to check edited Sheet
        cols = [7,13,14,16];
        colInd = cols.indexOf(e.range.columnStart);
        if (colInd == -1) return;
        if (colInd < 3) {
            offset = [4,-1,1,1]
            format = "MM/dd/yyyy HH:mm:ss";
            e.range.offset(0, offset[colInd])
            .setValue(Utilities.formatDate(new Date(), "GMT+8", format));
        } else {
            setValidation(e);
        }
     }
}

重新阅读您的问题后,我认为您的意思是仅在修改工作表Check时才执行onEdit

如果是这种情况,您应该只放置一个if语句以确保编辑发生在正确的工作表中。 因此,使用您的原始代码可以像这样工作:

function onEdit(e) {    
  var cols, colInd, offset, format;   
  if(e.range.getSheet().getName() = 'Check'){
    cols = [7,13,14,16];
    colInd = cols.indexOf(e.range.columnStart);
    if (colInd == -1) return;
    if (colInd < 3) {
      offset = [4,-1,1,1];
      format = "MM/dd/yyyy HH:mm:ss";
      e.range.offset(0, offset[colInd])
      .setValue(Utilities.formatDate(new Date(), "GMT+8", format));
    } else {
      setValidation(e);
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM