首页 > 解决方案 > 从 Google Apps 脚本中的用户名获取姓名和姓氏

问题描述

我使用脚本自动插入日期和用户名来填写字符串。几个人填表。如何修改脚本以便插入名字和姓氏而不是用户名?

function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  var idCol = e.range.getColumn();
  var idRow = e.range.getRow();
  
  if ( idCol == 4 && sheet.getName() =='LIST1' ) {
    var Value = e.range.offset(0, -2).getValues();
    if ( Value == "" ) {
      var vartoday = getDate();  //Gets the current date//
      sheet.getRange(idRow, 2).setValue( vartoday ); //Inserts into column 2 of the current row//
      var u = Session.getEffectiveUser().getUsername();  //Gets the username of the editor//
      sheet.getRange(idRow, 7).setValue(u); //Inserts into column 7 of the current row//
    }
  }
  function getDate() {
    var today = new Date();
    today.setDate(today.getDate());
    return Utilities.formatDate(today, 'GMT+03:00', 'dd/MM/yy');
  }
}

标签: javascriptgoogle-apps-scriptgoogle-sheetsgetdate

解决方案


看起来您将用户名存储在这里 -

var u = Session.getEffectiveUser().getUsername();  //Gets the username of the editor//

然后在此处插入用户名 -

sheet.getRange(idRow, 7).setValue(u); //Inserts into column 7 of the current row//

您需要在存储之前拆分名称。像这样的东西-

var first_name = // Split by your delimeter here and store first name 
var last_name = // Split by your delimeter here and store last name 
sheet.getRange(idRow, 7).setValue(first_name); //Inserts into column 7 of the current row//
sheet.getRange(idRow, 8).setValue(last_name); //Inserts into column 8 of the current row//

推荐阅读