Question

function getColIndexByName(colName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
for (i in row[0]) {
var name = row[0][i];
if (name == colName) {
  return parseInt(i) + 1;
    }
 }
    return -1;
   }

function sendtoContacts () {

 var source = SpreadsheetApp.getActiveSpreadsheet();
 var ss = source.getActiveSheet();
 var row = ss.getActiveRange().getRowIndex();

 var group = ContactsApp.getContactGroup('From Googlesheet');
 var givenName = ss.getRange(row, getColIndexByName("First")).getValue();
 var familyName = ss.getRange(row, getColIndexByName("Last")).getValue();
 var email = ss.getRange(row, getColIndexByName("Work Gmail")).getValue();
 var Homeemail = ss.getRange(row, getColIndexByName("Personal Email")).getValue();
 var company = ss.getRange(row, getColIndexByName("Company")).getValue();
 var title = ss.getRange(row, getColIndexByName("Title")).getValue();
 var phone = ss.getRange(row, getColIndexByName("Phone")).getValue();
 var mobile = ss.getRange(row, getColIndexByName("Mobile")).getValue();
 var newContact =  ContactsApp.createContact(givenName, familyName, email);
 var contactid = newContact.getId();
 var addy = ss.getRange(row, getColIndexByName("Address")).getValue();
 var city = ss.getRange(row, getColIndexByName("City")).getValue();
 var prov = ss.getRange(row, getColIndexByName("Prov")).getValue();
 var pc = ss.getRange(row, getColIndexByName("Postal Code")).getValue();
 var address = addy + ", " + city + ", " + prov + ", " + pc


 var AltContact = ss.getRange(row, getColIndexByName("Alt Contact Name")).getValue();
 var AltRelation = ss.getRange(row, getColIndexByName("Alt ContactRelation")).getValue();
 var AltPhone = ss.getRange(row, getColIndexByName("Alt Contact Phone")).getValue();
 var AltWork = ss.getRange(row, getColIndexByName("Alt Contact Wk No")).getValue();
 var AltMobile = ss.getRange(row, getColIndexByName("Alt Contact Mobile")).getValue();

   newContact.addToGroup(group);
   newContact.addAddress("Home", address);
   newContact.addCompany(company, title);
   newContact.addEmail("Home", Homeemail);
   newContact.addCustomField("Emergency Contact", AltContact);
   newContact.addCustomField("Emergency Contact Relation", AltRelation);
   newContact.addCustomField("Emergency Contact Work", AltWork);
   newContact.addCustomField("Emergency Contact Mobile", AltMobile);

      for ( var i = 0; i < phone.length ; i++){
         if (phone[i][3] != ''){ newContact.addPhone("HOME", phone); return}};   

      for ( var i = 0; i < mobile.length ; i++){
         if (mobile[i][44] != ''){ newContact.addPhone("Mobile", mobile); return}};

        }


  function MakeAllContacts() {
   var source = SpreadsheetApp.getActiveSpreadsheet();
   var ss = source.getActiveSheet();
   var startRow = 2;  // First row of data to process
   var numRows = 100;   // Number of rows to process


   for (row = 2; row < 6; row++)
   {

      sendtoContacts();

       }
    return
      }

Here I am duplicating the entries with MakeAllContacts() but I want to make the RowIndex change to every row in the sheet so it add all the contacts in the sheet. Here is at Video I made explaining it Video and here is a link to my actual sheet Google Sheet. I have a fair bit of code I would like to start sharing if I could just get my head are around looping instead of the one row to be All the rows in the sheet. Thanks for any help is appreciated.

Était-ce utile?

La solution

Your sendtoContacts () function is using ss.getActiveRange().getRowIndex(); to determine which row to use but nowhere in your script you set any row to active so you keep using the same data all the way through the main loop in MakeAllContacts().

There are 2 possible solutions :

  • use activate() in the MakeAllContacts() function loop so that the active row changes for each iteration (ss.getRange(row,1).activate())
  • use a rowIndex parameter in the sendtoContacts () function like below :

function MakeAllContacts() {
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var ss = source.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 100;   // Number of rows to process
  for (row = 2; row < numRows; row++){
    sendtoContacts(row);
  }
}

and then change the function sendtoContacts () function like this:

function sendtoContacts (row) { // row as parameter
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var ss = source.getActiveSheet();
  ...

That said, this approach is not very efficient since each data is read in the spreadsheet using a single getRange/getValue which is particularly slow... please read the best practice to get inspiration on how to handle your data more efficiently using a single getValues() and iterate the array content instead.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top