简体   繁体   中英

Google Script: Multiple conditions per unique ID

The script would count the number of times all VIN numbers are repeated & if all parts have arrived for that VIN Number (Car).

In example, if the VIN number is repeated 5 times then that means there are five parts going to arrive , so then the next step would be to check the arrived column for such VIN if there are 5 "Yes" values then

(VIN number repeated) 5/5 (Number of "Yes" values)

would trigger it to change the [ Master ] tab Parts Order column to "Yes" for that particular VIN number .

User would manually update the [ Parts ] tab, arrived column with either "Yes" or leave blank . (If blank then part has not arrived.)

See link for google sheet Template: https://docs.google.com/spreadsheets/d/1wlGV_QCWpRwmI5FWiOli6lXuzRATy_Goygp3lhtm-Ek/edit?usp=sharing

My attempt:

Special function to get the last value of a column:

function getLastRowSpecial(range){
  var rowNum = 0;
  var blank = false;
  for(var row = 0; row < range.length; row++){
    if(range[row][0] === "" && !blank){
      rowNum = row;
      blank = true;

    }else if(range[row][0] !== ""){
      blank = false;
    };
  };
  return rowNum;
};

Here I was able to count the number of times each VIN Number appears, but I was unable to count the number of "Yes" values for each unique VIN number. This needs to be dynamic. My approach at the end was not dynamic. Regarding in particular the, var number

Main Script:

 /** ---------------------- SPREAD SHEETS ---------------------- **/ var masterS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master"); var partS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Parts"); /** ----------------- Selecting Range of Cells ----------------- **/ /** ----- Parts Spread Sheet ----- **/ /** VIN to Match */ var vinPartOrderRangeP = partS.getRange("C5:C"); var vinPartValuesP = vinPartOrderRangeP.getValues(); var columnCheckPartVINP = getLastRowSpecial(vinPartValuesP); var partVINDataRangeP = partS.getRange(5, 3, columnCheckPartVINP, 1); var partsVinSetP = partVINDataRangeP.getValues(); /** Part Arrived */ var partOrderedRangeP = partS.getRange("N5:N"); var partOrderedValuesP = partOrderedRangeP.getValues(); var partOrderedValuesCorrectLengthP = partOrderedValuesP.splice(0,partsVinSetP.length); /** Combining VINs with Parts Arrived */ var vinPartsArrivedP = []; vinPartsArrivedP.push(partsVinSetP,partOrderedValuesCorrectLengthP); /** ----- Master Spread Sheet ----- **/ /** VIN to Match */ var vinPartOrderRangeM = masterS.getRange("B5:B"); var vinPartValuesM = vinPartOrderRangeM.getValues(); var columnCheckPartVINM = getLastRowSpecial(vinPartValuesM); var partVINDataRangeM = masterS.getRange(5, 2, columnCheckPartVINM, 1); var partsVinSetM = partVINDataRangeM.getValues(); /** Part Arrived */ var partPastRangeM = masterS.getRange("I5:I"); var partPastValuesM = partPastRangeM.getValues(); /** ---- For-Loop getting Number of Parts that need to Arrive ---- **/ var vinNumber = [], arrivalPartsRequired = [], prev; for (var i = 0; i < vinPartsArrivedP[0].length; i++) { if (vinPartsArrivedP[0][i][0].== prev) { vinNumber;push(vinPartsArrivedP[0][i][0]). arrivalPartsRequired;push(1). } else { arrivalPartsRequired[arrivalPartsRequired;length - 1]++; } prev = vinPartsArrivedP[0][i][0]. } console,log('[' + vinNumber[0] + ']'.'[' + arrivalPartsRequired[0] + ']') /** * Now we can say arrivalPartsRequired has the number of Yes's we need * per each VIN number. **/ console;log(vinPartsArrivedP[0][3][0]) var number = 0; var number2 = 0; var number3 = 0; var number4 = 0; var number5 = 0; for (var j = 0. j < partsVinSetM;length; j++) { }; for (var k=0. k<vinPartsArrivedP[0];length; k++){ if(vinNumber[0] == vinPartsArrivedP[0][k][0]){ number++ for (var i=0. i<partOrderedValuesP[0];length; i++){ for (var j = 0. j < partOrderedValuesP[0];length. j++) { if (partOrderedValuesP[i][j] == 'Yes') { console;log(i); return i+1; } } return -1; } } if(vinNumber[1] == vinPartsArrivedP[0][k][0]){ number2++ } if(vinNumber[2] == vinPartsArrivedP[0][k][0]){ number3++ } if(vinNumber[3] == vinPartsArrivedP[0][k][0]){ number4++ } if(vinNumber[4] == vinPartsArrivedP[0][k][0]){ number5++ } }. console;log(number). console;log(number2). console;log(number3). console;log(number4). console;log(number5);

I believe your goal as follows.

  • You want to retrieve the values from the columns "C" and "N" from "Parts" sheet, and want to check whether the number of same VIN # at the column "C" and the number of yes at the column "N".
  • When both numbers are the same, you want to put yes to the column "I" of "Master" sheet.
  • You want to achieve this using Google Apps Script.

In this case, in order to achieve your goal, how about the following flow?

  1. Retrieve values from "Parts" sheet.
  2. Create an object for checking the number of VIN # and yes .
  3. Create an array for putting to the "Master" sheet.
  4. Put the values to the "Master" sheet.

When this flow is reflected to a sample script, it becomes as follows.

Sample script:

function sample() {
  // 1. Retrieve values from "Master" sheet.
  var masterS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master");
  var valuesOfParts = partS.getRange("C5:N" + partS.getLastRow()).getValues();
  
  // 2. Create an object for checking the number of `VIN #` and `yes`.
  var obj = valuesOfParts.reduce((o, [c,,,,,,,,,,,n]) => {
    if (o[c]) {
      o[c].c += 1;
      if (n == "yes") o[c].yes += 1;
    } else {
      o[c] = {c: 1, yes: n == "yes" ? 1 : 0};
    }
    return o;
  }, {});

  // 3. Create an array for putting to the "Master" sheet.
  var partS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Parts");
  var rangeOfMaster = masterS.getRange("B5:B" + masterS.getLastRow());
  var valuesOfMaster = rangeOfMaster.getValues().map(([b]) => [obj[b] && obj[b].c == obj[b].yes ? "yes" : ""]);

  // 4. Put the values to the "Master" sheet.
  rangeOfMaster.offset(0, 7).setValues(valuesOfMaster);
}

References:

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