2013-10-20 38 views
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更改爲表格中的每一行,以便將表格中的所有聯繫人添加。這裏是視頻我做了解釋它Video這裏是我的實際工作表Google Sheet的鏈接。我有一段代碼,我想開始共享,如果我可以讓我的頭在循環而不是一行成爲工作表中的所有行。感謝您的任何幫助表示讚賞。如何在Google表格中使用.getRowIndex()循環命令?

回答

0

sendtoContacts()功能是使用ss.getActiveRange().getRowIndex();以確定哪一行使用,但無處在你的腳本設置任何行至有效狀態,以便繼續使用相同的數據一路通過MakeAllContacts()主循環。

有2級可能的解決方案:

  • 使用activate()MakeAllContacts()功能循環,使得對於每次迭代(ss.getRange(row,1).activate()
  • 在像下面的sendtoContacts()功能使用的rowIndex參數活動行更改:

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這是特別慢讀這種方法不是很有效......請閱讀best practice以獲取靈感,以便如何使用單個getValues()更有效地處理數據,並反覆更新數組內容。

+0

在一個測試,我沒有使用的聯絡服務(僅讀出電子表格)花了[2.297秒總運行時間] 6行...非常非常非常緩慢的過程的確: -/ –

+0

非常感謝你塞爾希望能幫助我理解並花時間指出如何改進代碼!非常感謝。 –