Determine current user in Apps Script

前端 未结 4 1565
深忆病人
深忆病人 2020-12-03 22:55

I\'m trying to identify current user\'s name to make notes of who edited what like this:

  r.setComment(\"Edit at \" + (new Date()) + \" by \" + Session.getA         


        
4条回答
  •  无人及你
    2020-12-03 23:15

    I had trouble with Wim den Herder's solution when I used scripts running from triggers. Any non script owner was unable to edit a protected cell. It worked fine if the script was run from a button. However I needed scripts to run periodically so this was my solution:

    When a user uses the sheet the first time he/she should click a button and run this:

    function identifyUser(){
       var input = Browser.inputBox('Enter User Id which will be used to save user to events (run once)');
      PropertiesService.getUserProperties().setProperty("ID", input);
    }
    

    This saves the user's input to a user property. It can be read back later at any time with this code:

    var user = PropertiesService.getUserProperties().getProperty("ID");

提交回复
热议问题