0
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
      }

在这里,我使用 MakeAllContacts() 复制条目,但我想让 RowIndex 更改工作表中的每一行,以便添加工作表中的所有联系人。这是我制作的视频解释它视频,这里是我的实际工作表Google Sheet的链接。我有相当多的代码我想开始分享,如果我能让我的头在循环而不是一行成为工作表中的所有行。感谢您的帮助。

4

1 回答 1

0

您的sendtoContacts ()函数ss.getActiveRange().getRowIndex();用于确定要使用的行,但在脚本中没有将任何行设置为活动的,因此您在MakeAllContacts().

有两种可能的解决方案:

  • activate()在函数循环中使用,MakeAllContacts()以便每次迭代时活动行都会更改 ( ss.getRange(row,1).activate())
  • 在函数中使用 rowIndex 参数,sendtoContacts ()如下所示:

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);
  }
}

然后sendtoContacts ()像这样更改函数函数:

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

也就是说,这种方法效率不高,因为使用单个 getRange/getValue 在电子表格中读取每个数据,这特别慢...请阅读最佳实践以获取有关如何使用单个getValues()和而是迭代数组内容。

于 2013-10-20T07:32:07.347 回答