Transient errors, Skipping blank cells with if clause

喜欢而已 提交于 2019-12-12 01:13:47

问题


This program is supposed to iterate through a range of spreadsheet cells containing email addresses and then assign them to contact groups. It is very slow and keeps giving me a "transient error. Try again later". Many of the cells are empty and I figure that is the cause. Can you help me speed this up so I stop getting transient errors (after 20-30 cells). I tried to implement this if clause but It either creates an blank contact (wasting my quota) or throws a null error.

If clause to skip over empty cells:

if(cell != "" && cell != null){

My script:

function addContactstoGroup() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName('NewEmails');

 var lastRow = sheet.getLastRow();
 var lastColumn = sheet.getLastColumn();



 var dataRange = sheet.getRange(2, 9, lastRow , lastColumn); //rows and columns start at 1, arrays start at 0.
 var data = dataRange.getValues();






 for (var i=1; i < lastRow; ++i){ //rows
 var groupHeaderRange = sheet.getRange(i+1,9);
 var groupHeader = groupHeaderRange.getValue();

 for(var j=1; j < lastColumn; ++j){ //columns

 var cell = data[i][j].getRange;

 if(cell != "" && cell != null){

 var groups = data[i][9];
 var cell = sheet.getRange(i+2,j+9);

 var contactGroup = data[i][0];
 var emailAddress = data[i][j];


 var contact = ContactsApp.createContact(null,null, emailAddress); // insert null   http://stackoverflow.com/questions/20600852/how-to-add-a-contact-without-a-name-in-gas
 var group = ContactsApp.getContactGroup(contactGroup);

 group.addContact(contact);//or should this just be emailAddress

 cell.clear();



};

groupHeaderRange.clear();

};





//}else{break;};



};

};

You would be my hero if you could help me fix:

1) Transient errors

2) If clause so it skips empty cells and does not run "ContactsApp.createContact"

var contact = ContactsApp.createContact(null,null, emailAddress); // insert null  

Thank you!!! I've only been coding a few months and I don't understand why this keeps coming up.

Thanks!


回答1:


  1. Try changing var cell to var cell = data[i][j]; then your if statment should work.

  2. Your transient error could be caused by an invalid email address. i.e. an email address that does not exist or some whitespace in an email address that you can't see.

You can find and remove whitespace by selecting the range containing the email addresses go to find replace. In the find field, put in 1 space and click replace all.

If you could provide a dummy spreadsheet with dummy email addresses it would be much easier to see if we could make your code more efficient.



来源:https://stackoverflow.com/questions/42615392/transient-errors-skipping-blank-cells-with-if-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!