0

enter image description here

The image (attached) is a simple "AppScript" adapted from a popular YouTube ...that allows "Values" (in a column) to be increased by 1 - by clicking an "Arrow" (button) - that I've linked to the script

enter image description here

By Clicking the "Up" Arrow, all the values will change from 6 ...to 7 (ie: go "Up" by one value)

enter image description here

QUESTION(s): How do I 'adjust' the Script to:

  1. Assess (or Evaluate) the Values in the column
  2. Ignore any values that are "Zero" (0)
  3. Increment (by 1) ...all the other "Non-Zero" values

And finally, if a Value is 'different' than the other values (eg: 5 ...instead of 7)

  1. Increment a value of 5 to 6 ...and the 'other' values of 7 to 8

enter image description here

Currently, the Script will change 5 to an 8 (along with all the 7's)

enter image description here

I'd like the Script to change the 5 to a 6 (and the 7's can increment to 8's)

1 Answer 1

0

Suggestion:

You can have the script iterate through each of the value of the range and increment it if the value isn't equal to 0.

Do try this script:

function incrementA2A5() {
  var sourceSheet = SpreadsheetApp.getActiveSheet();
  var range = sourceSheet.getRange("A2:A5");
  var values = range.getValues();

  //The forEach loop below iterates through each value of the range.
  //If the value isn't equal to zero, the loop will increment it by one.

  values.forEach(x => x[0] != 0 ? x[0] += 1 : x[0]);

  range.setValues(values); //Sets the updated values on the range
}

.GIF demo of the script: enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

The Script works (like a charm) - Wow 😲 I'm only familiar Excel's < or > logic (algorithms) ...so, any advice on how: x[0] ...and !=0 ...and =1 : x[0] ...works, would be greatly appreciated 🎓 Thanks again for your help 👍
You're welcome! The statement you're referring to's utilizing Javascript's Ternary operator The statement x[0] != 0 ? translates to if x[0] is not equal to zero The next part, x[0] += 1, adds 1 to the value if the if statement is true. : x[0] is essentially an else statement and means that the value is returned without any modification if the if statement is false. x[0] is just a placeholder referring to the first and, in the example, only value of the row being evaluated.
Follow-up ...re: adding multiple columns (or ranges) to increment. I'd like to increase the values in Columns/Rows A2:A5 & C2:C5 & E2:E5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.