0

我正在尝试从 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);
  }
4

1 回答 1

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);
  }
}
于 2013-07-16T13:47:35.370 回答