I wrote this Google Apps Script code meant to see if a car key was already in our safe or whether it is a new key. The first part works perfectly. It runs through the first spreadsheet with form submissions, gets the number plates, goes to the second spreadsheet with number plates of keys in our safe, compares to see if the key was here, and if the case, it performs a series of actions. If the key is not in the safe, then it should perform other actions. I thought I'd just need to use Else and it'd work, however, when I list the conditions for the else, it runs for both new keys and keys already in the safe. I don't know why it is not only running on what was left, which would be the keys that are not in the safe. Maybe it is something very obvious and I am missing.
var arrayVRMKeysOnsite = keysOnsite.getRange(1, 1, ct1, 1).getValues(); // creates an array of data
for (var nn = 1; nn < arrayVRMKeysOnsite.length; nn++) {
if (arrayVRMKeysOnsite[nn][0] == vrm) {
keysOnsite.getRange(nn + 1, 5).setValue("Yes");
//Keys Returned
var arrayKrtn = keysReturned.getRange('A:A').getValues();
var ct3 = 0;
while (arrayKrtn[ct3] && arrayKrtn[ct3][0] != "") {
ct3++;
}
var lkrtn = ct3 + 1;
var vrmKrtn = keysReturned.getRange(lkrtn, 1);
var timeKrtn = keysReturned.getRange(lkrtn, 2);
var allKrtn = keysReturned.getRange(lkrtn, 3);
var durationKrtn = keysReturned.getRange(lkrtn, 4);
var byKrtn = keysReturned.getRange(lkrtn, 5);
//end
vrmKrtn.setValue(vrm);
timeKrtn.setValue(date);
byKrtn.setValue(person);
// # Keys and duration calc
var arrayKbrrw = keysBorrowed.getRange('A:A').getValues();
var ct4 = 0;
while (arrayKbrrw[ct4] && arrayKbrrw[ct4][0] != "") {
ct4++;
}
var lkbrrw = ct4 + 1;
var arrayKeysBorrowed = keysBorrowed.getRange(2, 2, ct4, 3).getValues(); // creates an array of data
for (var mm = 1; mm < arrayKeysBorrowed.length; mm++) {
if (arrayKeysBorrowed[mm][1] == keysReturned1) {
var durationX = arrayKeysBorrowed[mm][2];
allKrtn.setValue("Yes");
durationKrtn.setValue(durationX);
for (var l = 1; l <= arrayKeysBorrowed.length; l++) {
var vrmBorrowed = keysBorrowed.getRange(l, 2).getValue()
if (vrmBorrowed == vrm) {
keysBorrowed.hideRows(l);
}
}
} else if (arrayKeysBorrowed[mm][1] > keysReturned1) {
var durationX = arrayKeysBorrowed[mm][2];
allKrtn.setValue("No");
durationKrtn.setValue(durationX);
for (var l = 1; l <= arrayKeysBorrowed.length; l++) {
var vrmBorrowed = keysBorrowed.getRange(l, 2).getValue();
if (vrmBorrowed == vrm) {
keysBorrowed.hideRows(l);
}
}
}
//end
}
checked1.setValue("Checked");
} else {
vrmKOS.setValue(vrm);
}
Aucun commentaire:
Enregistrer un commentaire