- You want to reduce the search cost using Google Apps Script.
- In your situation, the value of column "A" is searched, and the value of column "E" at the first found row is retrieved.
If my understanding is correct, how about this modification? In this sample script, your script is modified.
Modified script:
Please modify as follows.
From:
var last=ss.getLastRow();
var data=sh.getRange(1,1,last,5).getValues();
for(nn=0;nn<data.length;++nn){
if (data[nn][0]==stuID){break} ;
}
var StuName = data[nn][4]
To:
var StuName = "";
var f = sh.createTextFinder(stuID).findAll();
if (f.length > 0) {
for (var i = 0; i < f.length; i++) {
if (f[i].getColumn() == 1) {
StuName = sh.getRange(f[i].getRow(), 5).getValue();
break;
}
}
}
Note:
- In this modification, I used Class TextFinder. There are several methods for this situation. So please think of this as just one of several answers.
- In this script, when no values are found,
""
is returned.
References:
If I misunderstood your question and this was not the direction you want, I apologize.