【问题标题】:How to loop command using the .getRowIndex() in Google Sheets?如何在 Google 表格中使用 .getRowIndex() 循环命令?
【发布时间】:2013-10-20 15:50:49
【问题描述】:
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-apps-script google-contacts-api


    【解决方案1】:

    您的 sendtoContacts () 函数正在使用 ss.getActiveRange().getRowIndex(); 来确定要使用的行,但在脚本中没有将任何行设置为活动的,因此您在 MakeAllContacts() 的主循环中一直使用相同的数据。

    有两种可能的解决方案:

    • MakeAllContacts() 函数循环中使用activate(),这样每次迭代的活动行都会改变(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() 并迭代数组内容。

    【讨论】:

    • 在我没有使用联系服务(仅读取电子表格)的测试中,它花费了 [2.297 秒总运行时间] 6 行......确实非常非常慢的过程:-/
    • 非常感谢 Serge insas 帮助我理解并花时间指出如何改进代码!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多