简体   繁体   中英

Search for matching cells then copy one cell to a different location in Google Sheets

I have two different sheets in a spreadsheet that both contain emails. I am trying to move 3 cells from the row that an email is on in sheet "y" to the row that that same email is on in sheet "x".

Example: An email is on sheet "y" at D3, that same email is on sheet "x" at F7; I then want to copy the cells from H3:K3 on sheet "y" (because the email was in row 3 on sheet "y") to L7:P7 on sheet "x" (because the same email was in row 7 on sheet "x").

Also on I have this formula in R2 on sheet "x" and N2 on sheet "y" to tell what is the last row that has data in it so the script only scans for matching emails up until that row. I have variables "finalRowV" and "maxEmail" referring to those cells respectively:

=COUNTIF(A1:A, "<>")

I'm not the best at JavaScript but I feel like I compiled a coherent script (to me...) but when I ran it nothing happened: there was no error but the data was not moved.

function onOpen(e){

  function runIt(){
    var mainSheet = SpreadsheetApp.getActiveSpreadsheet();
    var workoutSheet = mainSheet.getSheetByName("x");
    var customerSheet = mainSheet.getSheetByName("y");

    var searchRow = 2;
    var searchLoc = "C" + searchRow;
    var copyLoc = "D" + searchRow + ":F" + searchRow;
    
    var maxEmail = customerSheet.getRange("N2").getValue();
    var found = 0;
    
    if (searchRow > maxEmail){
      
      if(found = 0){
        Logger.log("No match found");
      }
      else{
        endSearch();
      }
      
    }

    var finalRowV = workoutSheet.getRange("R2").getValue();
    var targetColumnV = "C2:C" + finalRowV;
    var targetColumn = workoutSheet.getRange(targetColumnV).getValues();
  
    var email = customerSheet.getRange(searchLoc).getValue();

    for (var i = 2; i < targetColumn[2].length; i++){
      
      if (targetColumn["C"][i].toString() == email.toString()){
        Logger.log("found the email " + targetColumn["C"][i] + " at cell" + searchRow + i);
        found++;
        searchRow++;
        
        var pasteLoc = "M" + i + ":O" + i;
        customerSheet.getRange(copyLoc).copyTo(workoutSheet.getRange(pasteLoc));
        
        runIt();
      }
      else {
        customerSheet.getRange(searchLoc).setBackground("red");
        i++;
        searchRow++;
        runIt();
      }
  
    }
    
  }
  
  function endSearch(){
  }
  
}

I found it hard to reproduce your code, therefore I just made a similar scenario based on your question which you can use for your case as well:

  function moveEmails() {
  
  ss = SpreadsheetApp.getActive();
  sx = ss.getSheetByName("x");
  sy = ss.getSheetByName("y");
  sx_size = sx.getLastRow();
  sy_size = sy.getLastRow();
  x_email = sx.getRange("F2:F"+sx_size).getValues().flat([1]);
  y_email = sy.getRange("D2:D"+sy_size).getValues().flat([1]);

  for (var i = 0 ; i < y_email.length ; i++ ) {  
  pos_found = x_email.indexOf(y_email[i]);
  if (pos_found > -1) {
  y_data = sy.getRange("H"+(i+2)+":K"+(i+2)).getValues();
  sx.getRange("L"+(pos_found+2)+":O"+(pos_found+2)).setValues(y_data);
  }
  }  
}

Explanation:

  • I have two sheets: x,y,

  • emails in x are in column F and emails in y are in column D,

  • columns in both sheets have a header, this is why the ranges start from the second row,

  • check if an email from y list matches email from x list,

  • if there is a match copy the relevant data from y(H:K) to x(L:0) since you copy data from 4 columns.

Concepts you can use:

Instead of having the count formula =COUNTIF(A1:A, "<>") to calculate the last row with content, you can use the google script method: sheet.getLastRow() .

Finally, pos_found = x_email.indexOf(y_email[i]) gives the position of the element in list x_email where the element y_email[i] was matched. If there is no match, it returns -1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM