首页 > 解决方案 > 将脚本运行到特定工作表 - Google 工作表

问题描述

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

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

标签: google-apps-scriptgoogle-sheets

解决方案


如果您想在编辑文档中的任何工作表时获得对“检查”工作表的引用,则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);
        }
     }
}

推荐阅读