'Run script depending on cell selected in dropdown

So this might be very easy, but this is my first time using javascript in google sheets and im already stuck. I made a simple counter adding +1 every time you click a button. Problem is that id like to change the cell that the +1 is added to with a dropdown in my sheet. This is the Code for my counter:

function Cell1 (Cellname, increase){
var range = SpreadsheetApp.getActiveSheet().getRange(Cellname);
 var toAdd = -1;
if (increase){
toAdd = 1;
 }
range.setValue(range.getValue() + toAdd);
}
function Seminareinladung() {
Cell1("E5", true);
}

Where it now says "E5" i need to know how to select the source of the selection from my dropdown. EG:

City1  =  4
City2  =  4
City3  =  5
City4  =  9

When i click on my button it should run the script on the cell next to the one selected in the dropdown. Therefore when City1 is selected in the Dropdown in E5 it should add +1 making it City1 = 5 Sorry if my description is a bit confusing... Thanks in advance for your help



Solution 1:[1]

Incrementing or Decrementing cells selected by a dropdown data validation

This is the code for my example and I have images to follow:

function incrementCity (){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var selected=sh.getRange(2,3).getValue();//in my example
  switch(selected) {
    case 'City1':
      sh.getRange(2,2).setValue(sh.getRange(2,2).getValue() + 1);
      break;
    case 'City2':
      sh.getRange(3,2).setValue(sh.getRange(3,2).getValue() + 1);
      break;
    case 'City3':
      sh.getRange(4,2).setValue(sh.getRange(4,2).getValue() + 1);
      break;
    case 'City4':
      sh.getRange(5,2).setValue(sh.getRange(5,2).getValue() + 1);
      break;
    default:
      break;
  }
}

function decrementCity (){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var selected=sh.getRange(2,3).getValue();//in my example
  switch(selected) {
    case 'City1':
      sh.getRange(2,2).setValue(sh.getRange(2,2).getValue() - 1);
      break;
    case 'City2':
      sh.getRange(3,2).setValue(sh.getRange(3,2).getValue() - 1);
      break;
    case 'City3':
      sh.getRange(4,2).setValue(sh.getRange(4,2).getValue() - 1);
      break;
    case 'City4':
      sh.getRange(5,2).setValue(sh.getRange(5,2).getValue() - 1);
      break;
    default:
      break;
  }
}

The image below describes how I set up the Data Validation and where the increment and decrement buttons are connected to the two functions. So you can select which city with the data validation cell in C2 and then press increment or decrement and the cell next to the city selection will increment or decrement.

enter image description here

This is simply how to setup the data validation.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 halfer