Determine current user in Apps Script

前端 未结 4 1563
深忆病人
深忆病人 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:30

    In this code you can use a cell for input. Authorising scripts are not required.

    function onEdit(e){
      checkUsername(e);
    }
    
    function checkUsername(e){
      var sheet = e.source.getActiveSheet();
      var sheetToCheck = 'Your profile';
      var sheetName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetToCheck);
      var CellInputUsername = 'B4';
      var ActiveCell = SpreadsheetApp.getActive().getActiveRange().getA1Notation();
    
      if (sheet.getName() !== sheetToCheck || ActiveCell !== CellInputUsername){return;}
    
      var cellInput = sheetName.getRange(CellInputUsername).getValue();
      PropertiesService.getUserProperties().setProperty("Name", cellInput);
    
      // Make cell empty again for new user
      sheetName.getRange(CellInputUsername).setValue("");
    
      var Username = PropertiesService.getUserProperties().getProperty("Name");      
      SpreadsheetApp.getUi().alert("Hello " + Username);
    }
    

提交回复
热议问题