温馨提示:本文翻译自stackoverflow.com,查看原文请点击:google apps script - How to display name of the user who ran a macro in a specific cell?
google-apps-script google-sheets google-sheets-macros

google apps script - 如何显示在特定单元格中运行宏的用户的名称?

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

抱歉,我不太懂技术,所以我问这个问题,但是,谁知道如何显示在Google表格中特定单元格中运行过宏的用户?不用担心匿名用户,我希望记录谁运行某个宏来进行记录。

查看更多

提问者
Anton Quintos
被浏览
237
Rafa Guillermo 2019-07-04 17:35

您可以通过调用App Script Session.getActiveUser方法来获取当前用户的电子邮件地址,然后使用arguments.callee.name可以获取使用了宏的名称。

您可以将此代码附加到Macro函数的末尾:

  // 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);

您只需要指定要将信息写入哪个单元格即可。