I'm attempting to make a grocery cost dividing system using google sheets and google apps scripts in order to create it. I'm having a problem when trying to use a switch statement that is nested within a for loop that goes through all of the people that're associated with a certain purchase and enters into the case based on their name, then adds the value to another sheet that has total purchases.
for (var i = 1; i < 6; i ) {
people.push(data[i][0]);
values.push(data[i][1]);
if(values[i-1] != 0.0){
var splits = (data[i][2]);
splits = splits.split(',');
var splitprice = values[i-1].toFixed(2);
Logger.log(`Values: ${values[i-1]} splits: ${splits} split length: ${Object.keys(splits).length} Split Price: ${splitprice}`);
switch (people[i-1]) {
case 'Aiden':
Logger.log('In Aiden case: ' splits);
Aiden(splits,splitprice);
return;
case 'Ben':
Logger.log('In Ben case: ' splits);
Ben(splits,splitprice);
break;
case 'Joe':
Logger.log('In Joe case: ' splits);
Joe(splits,splitprice);
break;
case 'Malcolm':
Logger.log('In Macolm case: ' splits);
Malcolm(splits,splitprice);
break;
case 'Marcel':
Logger.log('In Marcel case: ' splits);
Marcel(splits,splitprice);
return;
}
}
}
Within this code it checks the cell that corresponds to who made the purchase and goes through a switch statement to enter the correct function based on the person.
function Aiden(splitbtwn,total){
for(var j=0; j <= splitbtwn.length 1; j ) {
var splitprice = (total/(splitbtwn.length 1)).toFixed(2);
switch (splitbtwn[j]) {
case "All":
splitbtwn = "Ben,Joe,Malcolm,Marcel";
splitprice = (total/5);
case "Ben":
for(var k = 3; k < 1000; k ) {
var Ben2Aid = ('Payables!F' k)
var checker = sheet.getRange(Ben2Aid).isBlank();
if (checker) {
SpreadsheetApp.getActiveSpreadsheet().getRange(Ben2Aid).setValue(splitprice);
break;
}
}
case "Joe":
for(var k = 3; k < 1000; k ) {
var Joe2Aid = ('Payables!K' k)
var checker = sheet.getRange(Joe2Aid).isBlank();
if (checker) {
SpreadsheetApp.getActiveSpreadsheet().getRange(Joe2Aid).setValue(splitprice);
break;
}
}
case "Malcolm":
for(var k = 3; k < 1000; k ) {
var Mal2Aid = ('Payables!P' k)
var checker = sheet.getRange(Mal2Aid).isBlank();
if (checker) {
SpreadsheetApp.getActiveSpreadsheet().getRange(Mal2Aid).setValue(splitprice);
break;
}
}
case "Marcel":
for(var k = 3; k < 1000; k ) {
var Mar2Aid = ('Payables!U' k)
var checker = sheet.getRange(Mar2Aid).isBlank();
if (checker) {
SpreadsheetApp.getActiveSpreadsheet().getRange(Mar2Aid).setValue(splitprice);
return;
}
}
}
}
}
Each person has their own function that is called with the parameters to see who it is split between. My problem is that it is executing every single switch case within this second function instead of doing the ones that correspond to the case based on the splitbtwn variable that is an array that possesses the names of who its split between. Any help would be greatly appreciated.
CodePudding user response:
Please move break
at the end of case
statement
function Aiden(splitbtwn,total){
for(var j=0; j <= splitbtwn.length 1; j ) {
var splitprice = (total/(splitbtwn.length 1)).toFixed(2);
switch (splitbtwn[j]) {
case "All":
splitbtwn = "Ben,Joe,Malcolm,Marcel";
splitprice = (total/5);
case "Ben":
for(var k = 3; k < 1000; k ) {
var Ben2Aid = ('Payables!F' k)
var checker = sheet.getRange(Ben2Aid).isBlank();
if (checker) {
SpreadsheetApp.getActiveSpreadsheet().getRange(Ben2Aid).setValue(splitprice);
}
}
break;
case "Joe":
for(var k = 3; k < 1000; k ) {
var Joe2Aid = ('Payables!K' k)
var checker = sheet.getRange(Joe2Aid).isBlank();
if (checker) {
SpreadsheetApp.getActiveSpreadsheet().getRange(Joe2Aid).setValue(splitprice);
}
}
break;
case "Malcolm":
for(var k = 3; k < 1000; k ) {
var Mal2Aid = ('Payables!P' k)
var checker = sheet.getRange(Mal2Aid).isBlank();
if (checker) {
SpreadsheetApp.getActiveSpreadsheet().getRange(Mal2Aid).setValue(splitprice);
}
}
break;
case "Marcel":
for(var k = 3; k < 1000; k ) {
var Mar2Aid = ('Payables!U' k)
var checker = sheet.getRange(Mar2Aid).isBlank();
if (checker) {
SpreadsheetApp.getActiveSpreadsheet().getRange(Mar2Aid).setValue(splitprice);
}
}
}
}
}
CodePudding user response:
Try to put break
outside of the loop:
case "Ben":
for(var k = 3; k < 1000; k ) {
var Ben2Aid = ('Payables!F' k)
var checker = sheet.getRange(Ben2Aid).isBlank();
if (checker) {
SpreadsheetApp.getActiveSpreadsheet().getRange(Ben2Aid).setValue(splitprice);
}
}
break;
...