简体   繁体   English

来自 AD 的 Google Apps 脚本电话号码

[英]Google Apps Script phone numbers from AD

I'm trying to create a script in Google Apps Script that gets users' phone numbers from AD and copies them into Google Sheets.我正在尝试在 Google Apps 脚本中创建一个脚本,从 AD 获取用户的电话号码并将它们复制到 Google 表格中。

try {
      if(user.phones[0].type = 'home'){
        prop.push(user.phones[0].value);
      }
      if (user.phones[1].type = 'mobile'){
        prop.push(user.phones[1].value );
      }
      if(user.phones[2].type = 'work'){
        prop.push(user.phones[2].value);
      }
      else{
        prop.push('');
      }
      
    }
      catch(err){
      prop.push('');
    }

Now that does work, but it puts numbers in unwanted cell order.现在确实有效,但它会将数字置于不需要的单元格顺序中。 It seems to type: 'work' 'home' 'mobile'.它似乎在输入:“工作”“家庭”“移动”。 Also, if the user does not have 3 of those numbers, so fe Only 'mobile', this script puts it first where 'work' should be.此外,如果用户没有这些号码中的 3 个,那么 fe Only 'mobile',此脚本将其放在第一个应该是 'work' 的位置。 I was hoping this code would put them in a specific cell order: 'home', 'mobile', 'work'.我希望这段代码会将它们置于特定的单元格顺序中:“家庭”、“移动”、“工作”。 Any ideas?有任何想法吗?

  1. Instead of = use ===而不是=使用===
  2. The first two if hasn't and else .前两个ifelse If you don't want misplaced data, then you should add else{ prop.push(''); }如果你不想要错放的数据,那么你应该添加else{ prop.push(''); } else{ prop.push(''); } after each of them.在他们每个else{ prop.push(''); }之后。
  3. Regarding the order, you might have to create an Array of empty strings and replace the one that corresponds to each phone type by the corresponding value.关于订单,您可能必须创建一个空字符串数组,并用相应的值替换与每种电话类型对应的字符串。

The following example uses Array.prototype.forEach and switch instead of several if下面的例子使用Array.prototype.forEachswitch而不是几个if

const prop = new Array(4).fill('');
user.phones.forEach(phone => {

  switch(phone.type){
     case 'home':
       prop[0] = phone.value;
     break;
     case 'work':
       prop[1] = phone.value;
     break;
     case 'mobile':
       prop[2] = phone.value;
     break;
     default: // This will manage other phone types
       prop[3] = prop[3].length === 0 ? phone.value : [prop[3], phone.value].join(',');
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM