0

I have two sheets, one is updated from time to time, one is the backup which it may contain some the same values of the first one, the first one needs to be updated in some columns with recorded data already saved in the backup, I have written this script which is working fine some times but other times completely wrong (!) is taking right value form wrong row! second sheet is sorted descendant way so that inedxOf will match the right value for sure as first one.

        var M = data.length;
  for ( var r = 0; r < M ;r++){
    var L = datiArchivio.length;
    var row = data[r];
    var nave = row[4];
    var imo1 = data [r][23]

    if ( imo1 == "" && nave!= ""){

      for (var j = 0;  j < L;j++){
      var roww = datiArchivio[j];
      var IMO = roww[23];
      var naveArchivio = roww[4];
      var GT = roww[25]; 

        if ( IMO == ""){continue;} else {


          if ( naveArchivio == nave) {

            row.splice(23,1,IMO);  
            row.splice(25,1,GT);    

      }
    }
  }
  }
  }

I guess you meant something like this, but it is not working, in the archivio sheet there could be matching row with empty value at the reference column as well as fulfil values, it is ordered so that it should match first one which fullfil the condition, hence I added roww[23]=="" condition in order to skip that j row. it is challenging, keep on work on the logic. This last version is working, and also quickly, I was stucked in the old "rubbish in rubbish out".

user3858166
  • 61
  • 1
  • 8
  • don't use `for ... in` on Arrays (if they are arrays, that is) – Jaromanda X Nov 07 '15 at 14:44
  • Can you show how the arrays are structured and example input for this function? – Tobias Beuving Nov 07 '15 at 14:44
  • I agree with @JaromandaX Check out this thread http://stackoverflow.com/questions/5263847/javascript-loops-for-in-vs-for – Tobias Beuving Nov 07 '15 at 14:45
  • checking out the link... here is mine https://docs.google.com/spreadsheets/d/1yRsdK9pxhv8tFgBdrLyWXlWcQJhhyZRb35Ixr5Jc7NU/edit#gid=370677769 backup sheet is archivio, while ongoing file is Giornale del porto – user3858166 Nov 07 '15 at 15:14
  • To skip one iteration of a loop you can use the 'continue' keyword: if ( roww[23]==""){continue;} – Tobias Beuving Nov 07 '15 at 18:28
  • I have been staring at this for some minutes now and I can't figure out what it should do. You might be aware of it, but in general: do not use indexes as a reference since this kind of data tend to change. If you can use a keyword instead of a rownumber. Also use length caching and declare your variables like so: for (var j = 0; L= datiArchivio.length; j < L;j++){ – Tobias Beuving Nov 07 '15 at 18:36
  • many thanks, may be is not working because is not a smart idea to deal with, should work on it in another way. Some array formula in a different column where insert a look for a certain value if missing in another sheet and then copy it to the right column. – user3858166 Nov 07 '15 at 21:06

0 Answers0