Search code examples
google-sheetsgoogle-groups

Creating a dynamic Google group update from a Google form


Just looking for the final piece of code for this. I currently use the code below to pull contact info from a form responses sheet and auto add to an existing group.

I'll be adding a trigger to run every time the form is completed, however I need to change the code to only add info from the LAST ROW. Otherwise it will re-add all the contacts every time the form is completed, instead of just the most recent entry.

function addContact() {
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
for (i =0; i<data.length; i++) { 
var column = data[i];
var firstName = column[1]; 
var lastName = column[2];      
var emailAdd = column[3];
var contact = ContactsApp.createContact(firstName, lastName, emailAdd);
var group = ContactsApp.getContactGroup('Group Name');
debugger;
group.addContact(contact);
}
}

Any and all help appreciated.


Solution

  • use this code, I guess:

    function addContact() {
       var sheet = SpreadsheetApp.getActiveSheet();
       var dataRange = sheet.getDataRange();
       var data = dataRange.getValues();
       var LastDataRow = data[data.length - 1];
       var firstName = LastDataRow[1]; 
       var lastName = LastDataRow[2];      
       var emailAdd = LastDataRow[3];
    
       // and so on...
    }
    

    This part of code: var LastDataRow = data[data.length - 1]; gives you the last row data.