Using of a simple If then statement

Rolf Beck 20 Reputation points
2023-08-28T17:19:44.56+00:00

Hello

I am new to office script and would like to program a simple condition:

If the cell C6 contains the value 1, the value 2 should be inserted. If the cell C6 contains the value 2, the value 1 should be inserted. So every time the script is executed, the value of C6 changes between 1 and 2.

I have tried it this way, but the value does not change as desired. It seems that the "else" statement is not executed:


let myvalue = "C6";
	if (myvalue = "1") {
		selectedSheet.getRange("C6").setValue("2");
	} else {
		selectedSheet.getRange("C6").setValue("1");
	}
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
1,027 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michelle Ran 346 Reputation points Microsoft Employee
    2023-09-01T21:04:32.9533333+00:00

    Hi @Rolf Beck - when comparing values, you should use either the double or triple equals sign rather than the singular. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

    Hopefully that helps!

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.