javascript - 从 Google Apps 脚本中的用户名获取姓名和姓氏

标签 javascript google-apps-script google-sheets getdate

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

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');
  }
}

最佳答案

看起来您在此处存储用户名 -

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

关于javascript - 从 Google Apps 脚本中的用户名获取姓名和姓氏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64238252/

相关文章:

php - 只选择 Javascript 中的一些复选框

javascript - 动态设置谷歌脚本时间驱动计时器和gmail中计时器错误太多

javascript - 尽管对象在数组中,indexOf 仍返回 -1 - Google 电子表格脚本中的 Javascript

javascript - 将 google 脚本值传递给 javascript datepicker

google-apps-script - Google Apps 脚本 - 是否可以通过脚本更新 protected 范围?

javascript - 创建 jQuery Accordion 插件的困难( super 整洁!)

javascript - 在 for 中同步进行异步调用

javascript - 将溢出的 div 滚动到 Angular 元素的垂直中心(5)

google-apps-script - 无法让 Google 脚本运行

google-sheets - 有条件导入范围