Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

javascript - Take values ?from one sheet and save them on the other sheet based on the ID

I tried to create a script that takes values ??from one sheet and saves them on the other sheet based on the ID. Unfortunately, the script only saves the first value. What's wrong?

enter image description hereenter image description here

function script1() 
{

  // get first sheet 
  var Kwoty = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Kwoty");
  var FirstRow = 10;
  var LastRow = Kwoty.getLastRow();
  var RowRange = LastRow - FirstRow + 1;
  var WholeRange = Kwoty.getRange(FirstRow,6,RowRange,8);
  var AllValues = WholeRange.getValues();
  //Logger.log(AllValues); 
  
  //get 2 sheet
  var rozliczenie = Kwoty.getRange('G6').getValue();//Get sheet name
  var docelowysheet = SpreadsheetApp.openById('1_QgbLn9gKowDhCOwZ8lY9XBe5JmN107xhGDJ2kaGJIE').getSheetByName(rozliczenie);
  var FirstRow2 = 3;
  var LastRow2 = docelowysheet.getLastRow();
  var RowRange2 = LastRow2 - FirstRow2 + 1;
  var WholeRange2 = docelowysheet.getRange(FirstRow2,1,RowRange2,13);
  var AllValues2 = WholeRange2.getValues();
  //Logger.log(AllValues2); 
  
  
  for (var i=0;i<AllValues.length;i++){
     var CurrentRow = AllValues[i];
     var Id1 = CurrentRow[0]; //col with ID Sheet1 
     var kwota = CurrentRow[7]; //col with rate Sheet 1
    Logger.log(CurrentRow); 
     
  for (var i=0;i<AllValues2.length;i++){
     var CurrentRow2 = AllValues2[i];
     var Id2 = CurrentRow2[0]; //Col with ID Sheet2  
     var kwota2 = CurrentRow2[12]; //Col with rate Sheet2
   //Logger.log(Id2);  
     
  //Set Values   
    if (Id1 == Id2){
     var setRow2 = i + FirstRow2;
    docelowysheet.getRange(setRow2, 13).setValue(kwota);
    //Logger.log(CurrentRow[7]);
 
  } 
  }
  }
}
question from:https://stackoverflow.com/questions/65863444/take-values-from-one-sheet-and-save-them-on-the-other-sheet-based-on-the-id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Most likely the inconsistency arises in the for loop as you are using the same variable i as iterator for both the main and inner loops, as a result the values overwrite. Change the second loop variable, for example to j (and its references in the internal loop), for example:

for (var i=0;i<AllValues.length;i++){
   var CurrentRow = AllValues[i];
   var Id1 = CurrentRow[0]; //col with ID Sheet1 
   var kwota = CurrentRow[7]; //col with rate Sheet 1
   Logger.log(CurrentRow); 
     
   for (var j=0;j<AllValues2.length;j++){
      var CurrentRow2 = AllValues2[j];
      var Id2 = CurrentRow2[0]; //Col with ID Sheet2  
      var kwota2 = CurrentRow2[12]; //Col with rate Sheet2
      //Logger.log(Id2);  
     
      //Set Values   
      if (Id1 == Id2){
        var setRow2 = j + FirstRow2;
        docelowysheet.getRange(setRow2, 13).setValue(kwota);
        //Logger.log(CurrentRow[7]);
      } 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...