简体   繁体   中英

Break if value from range on sheet1 is found in range on sheet2 not working

Basically, the user will input data on a new item on sheet NovoItem. When the Save Button (to be added) is pressed, the code is supposed to check on sheet2 (ArquivoItens) if the item is already there. If so, then it should break. The code is not breaking if value is found on sheet2 (ArquivoItens):

function copyrange() { 
  var sourceSheet = 'Novo Item';
  var destinationSheet = 'ArquivoItens';
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getSheetByName(sourceSheet);
  var ActiveUser = Session.getActiveUser();

  //it add current user and a timestamp to the last 2 columns on sheet1.
  var val = sheet.getRange("Q12:Q")
      .getValues();
  for (var i = 0; i < val.length; i++) {
      if (val[i] > 0) {
        sheet.getRange(12, 34, i + 1, 1)
               .setValue(new Date());
       sheet.getRange(12, 35, i + 1, 1)
               .setValue(ActiveUser)
      }
    }

  var LastRowSource = sheet.getLastRow();
  var LastColumnSource = sheet.getLastColumn();
  var values = sheet.getRange(11,1,LastRowSource,LastColumnSource).getValues();
  var csh = ss.getSheetByName(destinationSheet);
  var data = []; 

  for (var i = 1; i < values.length; i++) {    
    if ( values[i][0] != '') { 
      data.push(values[i]);      
      //sheet.deleteRow(i+1) 
    } 
  }

  var dataNovoItem = sheet.getRange("B12:B").getValues(); // gets data (item number) in the sheet where data is input
  var dataArquivoItens = csh.getRange("B2:B").getValues(); //gets the item number on the datawarehouse sheet 

  for(var n=1; n < dataNovoItem.length ; n++){
    for(var j=1; j< dataArquivoItens.length ; j++){
      if (dataNovoItem[n] != 0 && dataArquivoItens[j] != 0) {
        if(dataNovoItem[n] == dataArquivoItens[j]) {
        break;
        }
      }
  }
    }

  Logger.log("Novo Item" + dataNovoItem);
  Logger.log("ArquivoItens" + dataArquivoItens);

  //Copy data array to destination sheet  
 csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);

}

Here's the log I'm getting: 在此处输入图像描述

Any help is appreiated.

You aren't using the iterator to access the index that you're iterating through.

It should be

  for(var n=1; n < dataNovoItem.length ; n++){
    for(var j=1; j< dataArquivoItens.length ; j++){
      if (dataNovoItem[n] != 0 && dataArquivoItens[j] != 0) { // this is where I try to eliminate blank rows in both ranges. I've tried != '', but it gets me the same result on the print below.
        if(dataNovoItem[n] == dataArquivoItens[j]) {
        break; //not working
        }
      }
  }
}
  Logger.log("Novo Item" + dataNovoItem);
  Logger.log("ArquivoItens" + dataArquivoItens);

I ended up solving this by iterating only through the existing list and comparing each row with the value I had set on the current sheet. This is probably too logic to yur programmers, but for a beginner like me, this can be a pain.

This is how the working is now:

var newItemID = sheet.getRange("W5").getValue(); //get the destination cell, where the item will be set
  var itemIDArquivoItens = csh.getRange(2, 1, csh.getLastRow(),1).getValues(); //gets the data range where the existing ID's to be compared against are

  var message = "Item já cadastrado!"; //Message to be displayed in case the ID already exists in the list;
  for(j = 1; j < itemIDArquivoItens.length; j++) { //loops through existing ID
    if(itemIDArquivoItens[j][0] == newItemID) { //if the ID already exists, pop up a message and stop the code from running
      Browser.msgBox(message)
      return;
    }
  }

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