尝试比较两行,然后使用Google脚本在每行中添加单元格

时间:2019-03-28 20:14:47

标签: google-apps-script google-sheets compare rows

我有一个包含新零件和翻新零件的库存清单。它们都具有相同的零件号,但翻新零件的末尾带有R。我想找到两行,然后添加每行有多少。即

  • 项目#手上的说明分钟
  • 172077 CAT 6跳线-4 2
  • 172077R 172079 CAT 6 11跳线

所以我想把两只手都放在一起,并将它们添加到脚本中,这样一来,它将在选项卡中打印出一行,告诉我要订购多少

我有在个别基础上工作的脚本,但它没有像零件一样进行比较,因此它告诉我订购不需要的零件。我只需要保留该物品的存货,无论新旧都可以。

//@@@@@@@@@@@@@@@@@@@@@@@ Needs Ordered @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  function needsOrderedSheet() {
  // source spreadsheets
  var sourceSpreadSheet = SpreadsheetApp.getActive();
  var ss = sourceSpreadSheet.getSheetByName('Products'); 
 //destination spreadsheet
 var destinationSpreadSheet = sourceSpreadSheet.getSheetByName('Needs Ordered'); 
//  destinationSpreadSheet.appendRow(['Item #','Description','Type','Min Stock','When To Order','On Order','Portal On Hands','Total In Stock','How Many to Order']);

  var startRow=2; // First row of data to process
  var numRows =2; // Number of rows to process
  var startColumn = 1;  // A=1 B=2
  var numColumns = 9;  // Number of columns to process

  var dataRange = ss.getRange(startRow, startColumn, numRows, numColumns);
  var data = dataRange.getValues();

  for (var i = 0; i < data.length; ++i) {
     var column = data[i];
    var itemnum = column[0]; // a column
    var desc = column[1]; // b column
    var type =column[2]; // c column
    var minstock = column[3]; // d column
    var whentoorder=column[4];// e column
    var onorder=column[5];// f column
    var portal = column[6]; // g column
    var compare= column
    var newitem = itemnum.split("R");
    var nitem=newitem[0];

  var dataRange2 = ss.getRange(startRow, startColumn, numRows, numColumns);
  var data2 = dataRange2.getValues();    
    for (var c = 0; c < data2.length; ++c) {
   var compare = data2[c];
   var itemnum2 = compare[0]; // a column
   var desc2 = compare[1]; // b column
   var type2 =compare[2]; // c column
   var minstock2 = compare[3]; // d column
   var whentoorder2=compare[4];// e column
   var onorder2=compare[5];// f column
   var portal2 = compare[6]; // g column
   var addp=portal + portal2;   
      var totalwhentoorder=whentoorder+whentoorder2;
      var totalminstock=minstock+minstock2;
      var instock = addp; // h column
      var totalonorder=onorder + onorder2;
      var howmanytoorder=totalminstock-(totalonorder+instock);

 if (nitem == itemnum2 && itemnum2 != '' && nitem != itemnum){
  if(instock <= whentoorder && whentoorder >= '1' && totalonorder < totalwhentoorder || instock < totalminstock && instock < whentoorder){

 destinationSpreadSheet.appendRow([itemnum,desc,type,totalminstock,totalwhentoorder,totalonorder,portal,instock,howmanytoorder,nitem]);
 }

    }    

  if (nitem == itemnum && itemnum != '' && nitem != itemnum2){
   if(instock <= whentoorder && whentoorder >= '1' && totalonorder < totalwhentoorder || instock < totalminstock && instock < whentoorder){
   destinationSpreadSheet.appendRow([itemnum,desc,type,totalminstock,totalwhentoorder,totalonorder,portal,instock,howmanytoorder,itemnum2]);
 }
     }    
    }
 //   }
 }
}

我更新了代码。.这实际上按照我想要的前两行的方式工作,但是如果我将NumRows更改为100,则基本上只需再次处理前两行即可。

2 个答案:

答案 0 :(得分:0)

比较新库存和翻新库存

function search1() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet105');//sheet where I copied your data
  var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());//Data Range after Header
  var vA=rg.getValues();//all data
  var vB=[];
  var s=0;
  for(var i=0;i-s<vA.length;i++) {
    if(vA[i-s][0].toString().slice(-1)=='R') {
      vB.push(vA[i - s]);//push Rs into vB
      vA.splice(i - s,1);//removing Rs from vA 
      s+=1;
    }
  }
  for(var i=0;i<vA.length;i++) {//looking for vB in vA
    for(var j=0;j<vB.length;j++) {
      if(vA[i][0]==vB[j][0].toString().slice(0,-1)) {
        vB[j][7]=vA[i][6]+vB[j][6];//put sum in added column
      }  
    }
  }
  var d=0;
  for(var i=0;i-d<vB.length;i++) {//removing vB rows that are not long enough
    if(!vB[i-d][7]) {
      vB.splice(i-d,1);
      d++;
    }
  }
  //Putting vBs found in vA at bottom of spreadsheet.
  //The last column vB[j][7] is the sum of vA[i][6] and vB[j][6] from above
  sh.getRange(sh.getLastRow() + 2,1,vB.length,vB[0].length).setValues(vB);
}

这些是我找到的线。

enter image description here

此版本将找到匹配项,将R版本中的两者相加,然后删除非R版本,并创建新名单。它还利用了在每次运行之前清除的输出页面。您有两个分配页面的名称。输入页面必须仅包含数据(该页面上没有其他内容),并且输出页面将在再次写入之前被清除。

function search1() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet109');
  var osh=ss.getSheetByName('Sheet110');
  osh.clearContents();
  var vH=sh.getRange(1,1,1,sh.getLastColumn()).getValues();
  var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
  var vA=rg.getValues();
  var vB=[];
  var s=0;
  //rg.sort({column:1,ascending:true});
  for(var i=0;i-s<vA.length;i++) {
    if(vA[i-s][0].toString().slice(-1)=='R') {
      vB.push(vA[i - s]);
      vA.splice(i - s,1);
      s+=1;
    }
  }
  s=0;
  for(var i=0;i-s<vA.length;i++) {
    for(var j=0;j<vB.length;j++) {
      if(vA[i-s][0]==vB[j][0].toString().slice(0,-1)) {
        vB[j][7]=vA[i-s][6]+vB[j][6];
        vA.splice(i-s,1);
        s+=1;
        break;
      }  
    }
  }
  var vBk=[];
  var d=0;
  for(var i=0;i-d<vB.length;i++) {
    if(!vB[i-d][7]) {
      vBk.push(vB[i-d]);
      vB.splice(i-d,1);
      d++;
    }
  }
  osh.getRange(1,1,vH.length,vH[0].length).setValues(vH);
  osh.getRange(osh.getLastRow()+1,1,vA.length,vA[0].length).setValues(vA);
  osh.getRange(osh.getLastRow()+1,1,vBk.length,vBk[0].length).setValues(vBk);
  osh.getRange(osh.getLastRow() + 1,1,vB.length,vB[0].length).setValues(vB);
}

答案 1 :(得分:0)

这是我所寻找的工作版本。感谢库珀的大部分代码。它查找翻新的零件和新零件,并将它们连接在一起。然后,它仅打印需要订购的物品。

 function needsOrderedSheet() {
      var ss=SpreadsheetApp.getActive();
      var sh=ss.getSheetByName('Stock Items');
      var osh=ss.getSheetByName('Needs Ordered');
       osh.clearContents();
      var vH=(['Item #','Description','Type','Min Stock','On Order','Portal On Hands','Total In Stock','How Many to Order','New Part #']);
      var rg=sh.getRange(2,1,sh.getLastRow()-1,6);
      var vA=rg.getValues();
      var vB=[];
      var s=0;
      //rg.sort({column:1,ascending:true});
      for(var i=0;i-s<vA.length;i++) {
         vA[i-s][6]=vA[i-s][5];                                 // Total In Stock
         vA[i-s][7]=vA[i-s][3] - (vA[i-s][6] + vA[i-s][4]);     // How many to Order
      if(vA[i-s][0].toString().slice(-1)=='R') {

        vB.push(vA[i - s]);

          vA.splice(i - s,1);  
          s+=1;
        }
      }

      s=0;
      for(var i=0;i-s<vA.length;i++) {
         for(var j=0;j<vB.length;j++) {
           if(vA[i-s][0]==vB[j][0].toString().slice(0,-1)) {
            vB[j][6]=vA[i-s][5]+vB[j][5];                              //Total In Stock  vB
            vB[j][7]=vB[j][3] - (vA[i-s][4]+vB[j][6] + vB[j][4]);      //How many to Order  vB
            vB[j][8]=vA[i-s][0];                                      // Add New Part Number vB
            vA.splice(i-s,1);
             s+=1;
            break;
          }  
        }
      }
      var vBk=[];
      var d=0;
      for(var i=0;i-d<vB.length;i++) {
        if(!vB[i-d][8]) {

          vBk.push(vB[i-d]);
          vB.splice(i-d,1);
          d++;
        }
      }


    //  osh.getRange(1,1,vH.length,vH[0].length).setValues(vH)
     osh.appendRow(['Item #','Description','Type','Min Stock','On Order','Portal On Hands','Total In Stock','How Many to Order','Part # For New Items']);

     for(var c=0;c<vA.length;c++) {
       var vC = vA[c]; 
        var itemnum = vC[0]; // a column
        var desc = vC[1]; // b column
        var type =vC[2]; // c column
        var minstock = vC[3]; // d column
        var onorder=vC[4];// f column
        var portal =vC[5]; // g column
        var totalinstock=vC[6];
        var howmanytoorder=vC[7];
       var newpart=vC[8];

       if (howmanytoorder > 0){
      osh.appendRow([itemnum,desc,type,minstock,onorder,portal,totalinstock,howmanytoorder,]);
       }
     }
    //   osh.getRange(osh.getLastRow()+1,1,vA.length,vA[0].length).setValues(vA);
      for(var d=0;d<vBk.length;d++) {
       var vD = vBk[d]; 
        var itemnum = vD[0]; // a column
        var desc = vD[1]; // b column
        var type =vD[2]; // c column
        var minstock = vD[3]; // d column
        var onorder=vD[4];// f column
        var portal =vD[5]; // g column
        var totalinstock=vD[6];
        var howmanytoorder=vD[7];
       var newpart=vD[8];

       if (howmanytoorder > 0){
      osh.appendRow([itemnum,desc,type,minstock,onorder,portal,totalinstock,howmanytoorder,]);
       }
     }
    //  osh.getRange(osh.getLastRow()+1,1,vBk.length,vBk[0].length).setValues(vBk);

      for(var e=0;e<vB.length;e++) {
         var vE = vB[e]; 
        var itemnum = vE[0]; // a column
        var desc = vE[1]; // b column
        var type =vE[2]; // c column
        var minstock = vE[3]; // d column
        var onorder=vE[4];// f column
        var portal =vE[5]; // g column
        var totalinstock=vE[6];
        var howmanytoorder=vE[7];
       var newpart=vE[8];

       if (howmanytoorder > 0){
      osh.appendRow([itemnum,desc,type,minstock,onorder,portal,totalinstock,howmanytoorder,newpart]);
       }
     }
        //  osh.getRange(osh.getLastRow()+1,1,vB.length,vB[0].length).setValues(vB);

    }