1

I'm looking to pull email addresses from one column and put them in another column. The only thing I am struggling with at the moment is that this needs to be done based on whether the data in two other columns is the same.

I have the names of the companies in one column and the email addresses in the next. I am copying in the company names from a separate spreadsheet where it is not in the same order. I would like to be able to populate Column A with the email addresses based on if the company name is the same but I'm currently struggling to work out how this is possible.

Here is a link as to an example of what I mean.

I think I am way, way off with my code at the moment but here is what I have.

function pullEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;   var numRows = 2;

  var dataRange = sheet.getRange(startRow, 1, numRows, 5)   
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var showname1 = row[1];
    var showname2 = row[3];
    var values1 = sheet.getRange("B2:B100").getValues();
    var values2 = sheet.getRange("D2:D100").getValues();

    if (showname1=showname2){
      sheet.getRange("A2").setValue(sheet.getRange("E2").getValues());     
    }     
  } 
}

Which doesn't do what I need. Any help or pointers would be much appreciated! A very basic user at the moment so still sort of working in the dark!

4

1 回答 1

3

只是一个小错误......您需要相等运算符“==”来比较值:

if (showname1 == showname2)

注意:在遍历数组时,也不建议使用该for (... in ...)构造,因为这也会访问对象的可枚举属性,而不仅仅是可迭代的数据数组。相反,当使用for (start, limit, increment)阅读更多:为什么在数组迭代中使用“for...in”是一个坏主意?

于 2013-09-10T12:40:52.933 回答