通过Google电子表格创建联系人组

时间:2013-07-16 12:51:14

标签: google-apps-script google-sheets contacts google-contacts

我正在尝试从Google表单创建电子邮件分发列表。然后我想把这些人放在一个特定的联系小组中。这是我到目前为止编译的代码。请帮忙。提前谢谢。

 function addContact() {
   var sheet = SpreadsheetApp.getActiveSheet();
   var startRow = 3;
   var numRows = 10;   // I want the rows to be infinite
   var dataRange = sheet.getRange(startRow, 2, numRows, 10)
   var data = dataRange.getValues();
   for (i in data) {
    var row = data[i];
    var firstName = row[1]; 
    var lastName = row[2];      
    var emailAdd = row[3];
  }
  // The code below creates a new contact and adds it to the "Work Friends" contact group
  var contact = ContactsApp.createContact(firstName, lastName, emailAdd);
  var group = ContactsApp.getContactGroup('Work Friends');
  group.addContact(contact);
  }

1 个答案:

答案 0 :(得分:1)

你很亲密,只需要一些调整。请参阅在线注释,解释更改。

function addContact() {
  var sheet = SpreadsheetApp.getActiveSheet();
  // var startRow = 3;  // This implies you have two header rows...
  var headerRows = 2;                   // Number of header rows to skip
  // var numRows = 10;   // I want the rows to be infinite (so use getDataRange)
  var dataRange = sheet.getDataRange(); // Range containing all non-blank rows & cols
  var data = dataRange.getValues();     // Read all data
  data.splice(0,headerRows);            // Remove header rows
  for (i =0; i<data.length; i++) {      // Avoid using for..in with arrays
    var row = data[i];
    var firstName = row[1]; 
    var lastName = row[2];      
    var emailAdd = row[3];

    // Do this IN the loop vvv

    // The code below creates a new contact and adds it to the "Work Friends" contact group
    var contact = ContactsApp.createContact(firstName, lastName, emailAdd);
    var group = ContactsApp.getContactGroup('Work Friends');
    debugger;  // Pause if running in debugger, to examine state
    group.addContact(contact);
  }
}
相关问题