Make 'onEdit' work only on one of the shee

2019-08-27 15:50发布

问题:

I have an 'onEdit' script for a Google Sheets but only want it to work on one sheet:

    function onEdit(event)
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var CellRow = SpreadsheetApp.getActiveRange().getRow();
  var CellColumn = SpreadsheetApp.getActiveRange().getColumn();

  if (CellColumn == 2 && CellRow == 3){
    sheet.getRange(CellRow, CellColumn).setFormula( "=ArrayFormula(QUERY(ImportRange(\"0AmH7M5970cwZdHFRa3NITkdBSFhxd3JzYUZwbFRId3c\";\"Table!B2:J200\");\"select Col1 where Col1 = \'\"&$K$13&\"\' limit 1\"))");
    Browser.msgBox("DON'T MESS WITH THE FORMULAS!");
  }
  }

How to I change the script so that it only works on one of the sheets in the worksheet?

回答1:

function onEdit(event){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var CellRow = SpreadsheetApp.getActiveRange().getRow();
  var CellColumn = SpreadsheetApp.getActiveRange().getColumn();

  if (sheet.GetSheetName() == "MYSHEETNAME"){
    if (CellColumn == 2 && CellRow == 3){
    sheet.getRange(CellRow, CellColumn).setFormula( "=ArrayFormula(QUERY(ImportRange(\"0AmH7M5970cwZdHFRa3NITkdBSFhxd3JzYUZwbFRId3c\";\"Table!B2:J200\");\"select Col1 where Col1 = \'\"&$K$13&\"\' limit 1\"))");
    Browser.msgBox("DON'T MESS WITH THE FORMULAS!");
    }
  }
}

Change MYSHEETNAME to the name of the sheet you want onedit to work for.