How do I restrict a trigger to a specific sheet/tab?

前端 未结 2 786
眼角桃花
眼角桃花 2020-12-20 05:13

I have written a script in google sheets that will change the time stamp in the selected cell anytime the entire spreadsheet is updated at all. However, I need the timestamp

相关标签:
2条回答
  • 2020-12-20 05:43

    The onEdit trigger triggers whenever any sheet/tab is edited. It cannot be restricted to a single sheet. However, even if it is triggered, it is possible to make it do nothing with a simple if and testing the event object that is sent on each edit, which contains the circumstances of the edit made.

    Sample:

    /**
     * @param {GoogleAppsScript.Events.SheetsOnEdit} param1
     */
    function onEdit({ range: /*edited range*/ eRange }) {
      const restrictCell = 'A1',
        restrictToSheet = 'Input';
      if (
        eRange.getA1Notation() !== restrictCell ||
        eRange.getSheet().getName() !== restrictToSheet
      )
        return;
      SpreadsheetApp.getActiveSpreadsheet()
        .getSheetByName('Board')
        .getRange('A28')
        .setValue(new Date());
    }
    
    
    0 讨论(0)
  • 2020-12-20 05:45

    This is what you are looking for :

    function onEdit(e) {
    
      var row = e.range.getRow();
      var col = e.range.getColumn();
      if ( e.source.getActiveSheet().getName() === "Input" && (row==1 && col==1) ){
     e.source.getSheetByName('Board').getRange('A28').setValue(new Date())      
      }
    }
    
    0 讨论(0)
提交回复
热议问题