Im learning JS and Google apps script. Im just working on examples to help learn. Something Im sure is simple that Im stuck on. Is there a way to print out Dell and HP count using the same variable? I know I could add a second variable counter2 and use that but just looking understand preferred methods . I tried to comment out what I have tried. The sheet Im referencing has 100 Dell and 400 HP.
var ss= SpreadsheetApp.getActiveSpreadsheet();
var es = ss.getSheetByName("School1");
var ms = ss.getSheetByName("School2");
var hs = ss.getSheetByName("School3");
var esValues = es.getDataRange().getValues();
var counter = 0;
//var esValues = es.getRange('A:C').getValues();
Logger.log(esValues.length);
for(var i = 0; i < esValues.length; i ){
var data= esValues[i];
var first = data[1];
var last = data[0];
var middle = data[2];
var studentid = data[3];
var device = data[4];
if(device.includes("Dell")){
counter ;
//Logger.log(`Dell count is ${counter}`); //When add here it prints same line 100 times
} else if(device.includes("HP")){
counter ;
}
}
Logger.log(`Dell count is ${counter}`)//Printing same count 500
Logger.log(`Hp count is ${counter}`)//Printing same count 500
}```
CodePudding user response:
So when logging the total of two different items using one variable isn't the way to do it. In your case both of your conditions will increase counter so while your for loop goes through it will always bump counter so it will hit 500 which is the total number of devices. Try something like this.
var ss= SpreadsheetApp.getActiveSpreadsheet();
var es = ss.getSheetByName("School1");
var ms = ss.getSheetByName("School2");
var hs = ss.getSheetByName("School3");
var esValues = es.getDataRange().getValues();
var counterDell = 0;
var counterHP = 0; // New for just HP
//var esValues = es.getRange('A:C').getValues();
Logger.log(esValues.length);
for(var i = 0; i < esValues.length; i ){
var data= esValues[i];
var first = data[1];
var last = data[0];
var middle = data[2];
var studentid = data[3];
var device = data[4];
if(device.includes("Dell")){
counterDell ;
//Logger.log(`Dell count is ${counter}`); //When add here it prints same line 100 times
} else if(device.includes("HP")){
counterHP ;
}
}
Logger.log(`Dell count is ${counterDell}`)//Printing same count 500
Logger.log(`Hp count is ${counterHP}`)//Printing same count 500
}
CodePudding user response:
When there are only HP
and Dells
in device
field, it could be resolved.
But if there are more values can be used in device
field, there is no solution rather than using two counters.
If only HD
and Dell
s are used, try this one.
var ss= SpreadsheetApp.getActiveSpreadsheet();
var es = ss.getSheetByName("School1");
var ms = ss.getSheetByName("School2");
var hs = ss.getSheetByName("School3");
var esValues = es.getDataRange().getValues();
var counter = 0;
//var esValues = es.getRange('A:C').getValues();
Logger.log(esValues.length);
for(var i = 0; i < esValues.length; i ){
var data= esValues[i];
var first = data[1];
var last = data[0];
var middle = data[2];
var studentid = data[3];
var device = data[4];
if(device.includes("Dell")){
counter ;
}
}
Logger.log(`Dell count is ${counter}`)
Logger.log(`Hp count is ${esValues.length - counter}`)
}```