替换特殊字符的不同变量

时间:2014-11-11 15:42:23

标签: javascript google-apps-script

我试图将firstname + lastname放在一个电子邮件地址中,但它确实有效,但我需要替换firstnamelastname中的特殊字符或已完成的变量emailad

function CreateEmailAddress(){

 var ss = SpreadsheetApp.getActiveSpreadsheet(),
     sheet = ss.getActiveSheet(),
     range = sheet.getDataRange(),
     values = range.getValues();
  for (var r=1; r<values.length; r++) {
   var row = values[r],
       firstname = row[0],
       lastname = row[1],
       email = row[2];

       if(email != "nodata" ){

       var emailad = (firstname + "." + lastname + "@superenterprisecompany.com");

      Logger.log(emailad);

    sheet.getRange(1 + r, 3).setValue(emailad);

    }

  }
}

我找到了这个,但不知道如何让它与我的变量一起使用

function convert_accented_characters(str){
    var conversions = new Object();
    conversions['ae'] = 'ä|æ|ǽ';
    conversions['oe'] = 'ö|œ';
    conversions['ue'] = 'ü';
    conversions['Ae'] = 'Ä';
    conversions['Ue'] = 'Ü';
    conversions['Oe'] = 'Ö';
    conversions['A'] = 'À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ';
    conversions['a'] = 'à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª';
    conversions['C'] = 'Ç|Ć|Ĉ|Ċ|Č';
    conversions['c'] = 'ç|ć|ĉ|ċ|č';
    conversions['D'] = 'Ð|Ď|Đ';
    conversions['d'] = 'ð|ď|đ';
    conversions['E'] = 'È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě';
    conversions['e'] = 'è|é|ê|ë|ē|ĕ|ė|ę|ě';
    conversions['G'] = 'Ĝ|Ğ|Ġ|Ģ';
    conversions['g'] = 'ĝ|ğ|ġ|ģ';
    conversions['H'] = 'Ĥ|Ħ';
    conversions['h'] = 'ĥ|ħ';
    conversions['I'] = 'Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ';
    conversions['i'] = 'ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı';
    conversions['J'] = 'Ĵ';
    conversions['j'] = 'ĵ';
    conversions['K'] = 'Ķ';
    conversions['k'] = 'ķ';
    conversions['L'] = 'Ĺ|Ļ|Ľ|Ŀ|Ł';
    conversions['l'] = 'ĺ|ļ|ľ|ŀ|ł';
    conversions['N'] = 'Ñ|Ń|Ņ|Ň';
    conversions['n'] = 'ñ|ń|ņ|ň|ʼn';
    conversions['O'] = 'Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ';
    conversions['o'] = 'ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º';
    conversions['R'] = 'Ŕ|Ŗ|Ř';
    conversions['r'] = 'ŕ|ŗ|ř';
    conversions['S'] = 'Ś|Ŝ|Ş|Š';
    conversions['s'] = 'ś|ŝ|ş|š|ſ';
    conversions['T'] = 'Ţ|Ť|Ŧ';
    conversions['t'] = 'ţ|ť|ŧ';
    conversions['U'] = 'Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ';
    conversions['u'] = 'ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ';
    conversions['Y'] = 'Ý|Ÿ|Ŷ';
    conversions['y'] = 'ý|ÿ|ŷ';
    conversions['W'] = 'Ŵ';
    conversions['w'] = 'ŵ';
    conversions['Z'] = 'Ź|Ż|Ž';
    conversions['z'] = 'ź|ż|ž';
    conversions['AE'] = 'Æ|Ǽ';
    conversions['ss'] = 'ß';
    conversions['IJ'] = 'IJ';
    conversions['ij'] = 'ij';
    conversions['OE'] = 'Œ';
    conversions['f'] = 'ƒ';

    for(var i in conversions){
        var re = new RegExp(conversions[i],"g");
        str = str.replace(re,i);
    }

    return str;
}

3 个答案:

答案 0 :(得分:2)

你可以使用这样的功能:

function testReplaceAccents() {
  Logger.log(convert_accented_characters("Jean-Noël Esèvéçà"));  
}

结果:

enter image description here

所以,在你的代码中:

function CreateEmailAddress(){
  var ss = SpreadsheetApp.getActiveSpreadsheet(),
      sheet = ss.getActiveSheet(),
      range = sheet.getDataRange(),
      values = range.getValues();
  for (var r=1; r<values.length; r++) {
    var row = values[r],
        firstname = row[0],
        lastname = row[1],
        email = row[2];
    if(email != "nodata" ){
      var emailad = (firstname + "." + lastname + "@superenterprisecompany.com");
      Logger.log(convert_accented_characters(emailad));
      sheet.getRange(1 + r, 3).setValue(convert_accented_characters(emailad)); 
    }
  }
}

答案 1 :(得分:1)

你能尝试一下吗?我认为它应该有效。请告诉我。如果没有,那么你必须改变convert_accented_characters函数。

var emailad =(convert_accented_characters(firstname)+“。”+ convert_accented_characters(lastname)+“@ superenterprisecompany.com”)

答案 2 :(得分:0)

您可以尝试使用正则表达式删除特殊字符。

例如:

var firstName = "test'name &&^^%%%#s";
var newFirstName=str.replace(/[^a-zA-Z]/g, "");