Warm tip: This article is reproduced from stackoverflow.com, please click
google-apps-script google-sheets google-sheets-macros

How to display name of the user who ran a macro in a specific cell?

发布于 2020-03-27 10:15:18

I'm not too tech savvy, I apologize, hence me asking this but, would anyone know how to display the user who ran a macro in a specific cell in google sheets? No anonymous users to worry about, I'm hoping to log who runs a certain macro for records purposes.

Questioner
Anton Quintos
Viewed
118
Rafa Guillermo 2019-07-04 17:35

You can get the current user’s email address by calling the App Script Session.getActiveUser method and then using arguments.callee.name you can get the name of which Macro was used.

You can append this code to the end of your Macro function:

  // get email of user that ran the macro
  var user = Session.getActiveUser().getEmail()

  //Set cell to add value to, in this case Sheet2:A1
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2")
  var cell = sheet.getRange(1, 1, 1, 1);

  cell.setValue(user + ' ran macro ' + arguments.callee.name);

You just need to specify which Cell to write the information to.