Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

Google Script logger not showing what sheet edit was made on from event data


I am trying to detect what sheet an edit was made on during an installed onEdit function.

The logger correctly records the data of the cell being edited however does not tell me what sheet this edit was made on. I need specifically to ID what sheet the edit took place on in order to complete a function I am building.

Logger:

[19-08-01 08:37:27:714 CDT] {"authMode":{},"range":{"columnStart":4,"rowStart":4,"rowEnd":4,"columnEnd":4},"source":{},"oldValue":"2.0","triggerUid":"1111111","user":{"nickname":"Mike-SMT","email":"[email protected]"},"value":"222"}

Code used to log event:

function raw_data_edited(e) {
  Logger.log(JSON.stringify(e));
}

I do not see anything in the above log that would indicate specific sheet.

Is there a way to ID sheet from event?


Solution

  • It's strange, source always seems to show as empty when logging the event object, but can still be used.

    I wasn't sure if you meant spreadsheet ID or sheet ID, so here's how to get both using event objects:

    //get spreadsheet id using "source" object
    e.source.getId();
    
    //get spreadsheet id using "range" object
    e.range.getSheet().getParent().getId();
    
    //get individual sheet id using "source" object
    e.source.getActiveSheet().getSheetId();
    
    //get individual sheet id using "range" object
    e.range.getSheet().getSheetId();
    

    References: