如何生成将 gmail 和 googlemail 视为同义词的代码,就像 Google 本身一样?

问题描述 投票:0回答:1

我正在根据电子表格中的条目管理一组电子邮件地址。电子邮件地址列表由用户提供。在一种情况下,我的用户的电子邮件地址为“[电子邮件受保护]”。但是,当我下载电子邮件列表时:

group = AdminDirectory.Members.list('[email protected]').members.email; 

它返回“[电子邮件受保护]”。因此,代码当前决定“[email protected]”是新成员,并尝试使用以下方式添加它们:

AdminDirectory.Member.Insert('abc.gmail.com, GROUP_EMAIL);

由于“用户已存在”而失败。

因此,我的问题是其他人如何处理这种情况 - 是否有标准方法或只是捕获错误并重试?

我尝试创建可重现的代码,但它依赖于对现有域和电子表格的访问,而我无法匿名共享这两者(如果有办法,请告诉我)。代码如下所示:

function InsertNewUser() {
  var page;
  var rqdUsers=[];
  var usersEmail=[];
  var testSheet=SpreadsheetApp.getActive().getSheetByName("Required Users");

  page = AdminDirectory.Members.list('[email protected]').members;
  usersEmail=page.map((user)=>user.email); //'usersEmail[0]' then has the value [email protected]

  rqdUsers.push(testSheet.getRange(1,4).getValue()); //'rqdUsers[0]' then has the value [email protected]
  
  //usersEmail[0] <> rqdUsers[0] and hence is assumed to be a new user
  var member = {
    email: rqdUsers[0], //rqdUsers[0][email protected]
    role: "MEMBER"
    };
  AdminDirectory.Members.insert(member, '[email protected]');  //this line fails because the member already exists
}

感谢您的帮助。

google-sheets google-apps-script
1个回答
0
投票

这不会执行您所要求的操作,但它确实会用适当的替换项替换当前域

function replacingDomains() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet1");//Emails
  const sr = 2;//data start row
  const es = sh.getRange(sr, 1, sh.getLastRow() - sr + 1).getDisplayValues().flat();
  const ds = es.map(e => e.split("@")[1]);
  const xrObj = { "domain8.com": "gmail.com", "domain16.com": "gmail.com" };//Data Entry
  Object.keys(xrObj).forEach(k => {
    idx = -1;
    var from = '';
    do {
      var idx = ds.indexOf(k, from)
      if (~idx) {
        es[idx] = es[idx].toString().replace(ds[idx], xrObj[ds[idx]]);
        from = idx + 1;
      }
    } while (~idx)
  })
  Logger.log(es.join('\n'));
  return es;
}

表1:

Execution log
8:46:42 AM  Notice  Execution started
8:46:40 AM  Info    [email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
8:46:44 AM  Notice  Execution completed
columnOffset: Integer, The number of columns right from the range's top-left cell; negative values represent columns left from the range's top-left cell., Returns a new range that is offset from this range by the given number of rows and columns (which can be negative). The new range is the same size as the original range. ``` var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("A1"); // newCell references B2 var newCell = cell.offset(1, 1); ```, hint
© www.soinside.com 2019 - 2024. All rights reserved.