-2

I am trying to create a spreadsheet to keep track of event attendance of a group of people.
Column 1 contains the attendee's name.
Column 2 contains a dropdown menu with 3 options: Confirmed,Maybe,Unable.
Column 3 contains a value of the number of events the person has attended.

I am trying to create a function that I can attach to a button. By clicking the button, I would like to increase the value of column 3 by 1, IF the value in column 2 is "Confirmed"

I found a similar question and somebody answered to use the following script, but this was just for 1 specific cell, and there were no additional variables:

function increment() {
  SpreadsheetApp.getActiveSheet().getRange('A1').setValue(SpreadsheetApp.getActiveSheet().getRange('A1').getValue() + 1);
}

How can I modify the above script to increase the value of all my cells in Column 3 by 1 if the value of column 2 in the same row is "Confirmed"?

Here's the post I mentioned above

Here's an example of the 3 columns

isherwood
  • 58,414
  • 16
  • 114
  • 157

2 Answers2

0

You should use a loop to iterate through all your rows with data. For each row, you check if the value in the second column is "Confirmed", and if that's the case you increment the value in the third column. If could be something like this:

function increment() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var firstRow = 2;
  var firstCol = 1;
  var numRows = sheet.getLastRow() - firstRow + 1;
  var numCols = 3;
  var values = sheet.getRange(firstRow, firstCol, numRows, numCols).getValues();
  for (var i = 0; i < values.length; i++) {
    var row = values[i];
    var status = row[1];
    var count = row[2];
    if (status == "Confirmed") count++;
    sheet.getRange(i + firstRow, 3).setValue(count);
  }
}

In this example data starts at row 2 and spans column A to C. If that's not your case, please change this accordingly.

Reference:

I hope this is of any help.

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
-1
function increment() {
  var ss=SpreadsheetApp.getActive();
  var rg=ss.getActiveSheet().getRange('A1');
  if(rg.offset(0,1).getValue()=="you enter your string here") {
    rg.setValue(rg.getValue() + 1);
}
Cooper
  • 59,616
  • 6
  • 23
  • 54