IF statement in AMPScript - if-statement

I have a little problem with if statement as it populates only last elseif
%%[IF #Add3 == "Y" AND #Add4 == "Y" AND #Add5 == "N" THEN
SET #WhichNom1 = "1"
SET #WhichNom2 = "2"
ELSEIF #Add3 == "Y" AND #Add4 == "N" AND #Add5 == "Y" THEN
SET #WhichNom1 = "1"
SET #WhichNom2 = "3"
ELSEIF #Add3 == "N" AND #Add4 == "Y" AND #Add5 == "Y" THEN
SET #WhichNom1 = "1"
SET #WhichNom2 = "3"
]%%
#WhichNom1
#WhichNom2
%%[ENDIF]%%
The code above will display two variables when the last ELSEIF is TRUE. What do I need to do to check 3 statements and display WhichNom1 and WhichNom2 for every scenario?

You're only printing the variables if the last elseif is true.
Move them like this:
...
ELSEIF #Add3 == "N" AND #Add4 == "Y" AND #Add5 == "Y" THEN
SET #WhichNom1 = "1"
SET #WhichNom2 = "3"
]%%
%%[ENDIF]%%
#WhichNom1
#WhichNom2
So they're outside the IF/ELSEIF. That way they'll always be printed, but the variables will be set differently based on the clauses.
EDIT:
If you only want the variables printed if one of the statements is true, then you would (as you have mentioned) need to print them within the if statement, OR you could:
SET #WhichNom1 = ""
SET #WhichNom2 = ""
before the if statements, then after them do:
IF #WhichNom1 != "" THEN #WhichNom1
IF #WhichNom2 != "" THEN #WhichNom2
so you only print them if they've been set to something other than "".

I managed to get it sorted in slightly different way using two IF statements
%%[IF #Add3 == "Y" AND #Add4 == "Y" AND #Add5 == "N" THEN
SET #WhichNom1 = "1"
SET #WhichNom2 = "2"
SET #Status = "T"
ELSEIF #Add3 == "Y" AND #Add4 == "N" AND #Add5 == "Y" THEN
SET #WhichNom1 = "1"
SET #WhichNom2 = "3"
SET #Status = "T"
ELSEIF #Add3 == "N" AND #Add4 == "Y" AND #Add5 == "Y" THEN
SET #WhichNom1 = "2"
SET #WhichNom2 = "3"
SET #Status = "T"
ELSE
SET #Status = "F"
ENDIF]%%
%%[IF #Status == "T" THEN]%%
#WhichNom1
#WhichNom2
%%[ENDIF]%%

Rob is correct. Also if you want to print #WhichNom1 and #WhichNom2 outside of the AMPscript block, you'll probably need to code as:
%%[
IF #Add3 == "Y" AND #Add4 == "Y" AND #Add5 == "N" THEN
SET #WhichNom1 = "1"
SET #WhichNom2 = "2"
SET #Status = "T"
ELSEIF #Add3 == "Y" AND #Add4 == "N" AND #Add5 == "Y" THEN
SET #WhichNom1 = "1"
SET #WhichNom2 = "3"
SET #Status = "T"
ELSEIF #Add3 == "N" AND #Add4 == "Y" AND #Add5 == "Y" THEN
SET #WhichNom1 = "2"
SET #WhichNom2 = "3"
SET #Status = "T"
ELSE
SET #Status = "F"
ENDIF
]%%
%%[ IF #Status == "T" THEN ]%%
%%= v(#WhichNom1) =%%<br />
%%= v(#WhichNom2) =%%
%%[ ENDIF ]%%

Related

Exception based on condition on Apps Script in Google Sheet

With this script I can exclude to insert the same value column in Google Sheet for maximum 100 times.
But I am trying to exclude (with if statement) some values from this script, in particular the date "25/12/2022" and the date "12/01/2012".
How could I proceed?
function onEdit(e) {
var r = e.range;
var s = r.getSheet();
if (s.getName() === 'New Rise 2022' && r.getColumn() === 27) {
var newValue = r.getDisplayValue();
if (newValue == "") return;
var count = s.getRange('AA1:AA').getDisplayValues().filter(([a]) => a === newValue).length;
if (count > 99) {
r.setValue(e.oldValue);
SpreadsheetApp.flush();
SpreadsheetApp.getUi().alert('Questa data è stata già inserita 100 volte');
}
}
}
Update:
function onEdit(e) {
var r = e.range;
var s = r.getSheet();
if (s.getName() === 'New Rise 2022' && r.getColumn() === 27) {
var newValue = r.getDisplayValue();
if (newValue == "") return;
var count = s.getRange('AA1:AA').getDisplayValues().filter(([a]) => a === newValue).length;
if (count > 99 || e.range.getDisplayValue() == "25/12/2012" || e.range.getDisplayValue() == "12/01/2012") {
r.setValue(e.oldValue);
r.setNumberFormat('dd/mm/YYYY');
SpreadsheetApp.flush();
SpreadsheetApp.getUi().alert('Questa data è stata già inserita 100 volte');
}
}
}
How about this?
function onEdit(e) {
const sh = e.range.getSheet();
const x = ["25/12/2022","12/01/2012"];
const idx = x.indexOf(e.value);
if (sh.getName() === 'New Rise 2022' && e.range.columnStart == 27 && e.value && !~idx) {
var count = sh.getRange('AA1:AA' + sh.getLastRow()).getDisplayValues().flat().filter(e => e == e.value).length;
if (count > 99) {
e.range.setValue(e.oldValue);
}
}
}
You can get the newly entered display value and compare it against the "forbidden" values
Therefore, retrieve the latest modified cell with e.range:
...
if (count > 99 || e.range.getDisplayValue() == "25/12/2022" || e.range.getDisplayValue() == "12/01/2012") {
...
}
...
Note:
I understood that what you are interested in is the displayed value (date in this case), but depending on your date formatting the display value will be different from the value you typed in.
If it is the typed in value you are after, you can retrieve it with e.value:
...
console.log("e.value: " + e.value)
console.log("e.range.getDisplayValue(): " + e.range.getDisplayValue())
if (count > 99 || e.value == "25/12/2022" || e.value == "12/01/2012") {
...
}
...
References:
Event Objects
getDisplayValue()
UPDATE:
If you have problems with number formatting you can use the method setNumberFormat().
Modify your code block in the if statement to
r.setValue(e.oldValue);
r.setNumberFormat('dd/mm/YYYY');

Coffeescript syntax issue with for statement

Using Coffeescript, I am trying to set conditionals for setting a fillColor on a graph plot for two variables; lastVisibility and curr_visibility. Something is not right with my syntax. Any suggestions
for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: if lastVisibilty == 0 & curr_visibility == 0
then "#C90E30"
else lastVisibilty == 0 & curr_visibility == 1
then "#439C32"
else lastVisibilty == 1 & curr_visibility == 0
then "#C90E30"
else
"#439C32"
There are lots of problems here:
You only use then when you're using a one-liner such as a = if b then c else d, if you're using the multi-line form of if then you don't use thens.
You have multiple else branches where you mean else if.
& and && are different things, & is a bit-wise operator, && is the logical conjunction you're looking for.
You need to be very careful and consistent with your indentation.
Applying those adjustments to your code you get:
for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
However, the conditional logic inside your loop doesn't depend on set (i.e. is loop invariant) so you should factor that out:
fillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: fillColor
Or better, push all the invariant stuff outside the loop to further clarify the code:
fillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
lines =
show: true
fill: true
opacity: 0.7
fillColor: fillColor
for set in datasets
line.push
data: set,
lines: lines
Or, since a for-loop is an expression, you could say:
fillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
lines =
show: true
fill: true
opacity: 0.7
fillColor: fillColor
line = ({ data: set, lines: line } for set in datasets)
Assuming that line is empty before your loop of course; if it isn't then you could use Array.prototype.concat:
line = line.concat({data: set, lines: line } for set in datasets)
to append the loop's data to line.

Split String not Meeting Conditions [Swift]

I'm trying to take a series of numbers from a text file and based on the numbers define a new variable. When I print SettingsArray i get the following:
[10, 25, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0.02, 0.002, 0, 1, 0, 0, 0, 1, 1, 1, 25, 500, 0, 1, 250, 250, 250, 500, 500, 500, 10, 10, 10, 200, 200, 200]
Which are the numbers I'm looking for, but for some reason, when checking the If statements, all of the SettingsArray elements look something like this:
[2] String class name = _TtC10Foundation15_NSOpaqueString
Which obviously don't meet any of the conditions, why are the array elements garbled when checking the If statements?
let file = String(contentsOfFile: "/Users/UserGoesHere/Documents/Settings.txt", encoding: NSUTF8StringEncoding, error: nil)!
var SettingsArray = split(file) {$0 == ","}
var StepPortInvert = ""
if SettingsArray[2] == "1" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
var StepPortInvert = "00000000"
}
if SettingsArray[2] == "0" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
var StepPortInvert = "00000001"
}
if SettingsArray[2] == "1" && SettingsArray[3] == "0" && SettingsArray[4] == "1" {
var StepPortInvert = "00000010"
}
You just have to declare StepPortInvert once. BTW 2,3,4 = "1, 0, 0"
let file = String(contentsOfFile: "/Users/UserGoesHere/Documents/Settings.txt", encoding: NSUTF8StringEncoding, error: nil)!
var SettingsArray = split(file) {$0 == ","}
var StepPortInvert = ""
if SettingsArray[2] == "1" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
StepPortInvert = "00000000"
}
if SettingsArray[2] == "0" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
StepPortInvert = "00000001"
}
if SettingsArray[2] == "1" && SettingsArray[3] == "0" && SettingsArray[4] == "1" {
StepPortInvert = "00000010"
}

c++ Why is this true and not false?

I'm trying to understand why ! ( ( true || false ) && false ) is true and not false but I can't seem to figure it out.
true || false == true
true && false == false
!false == true
! ( ( true || false ) && false )
is equal to
! ( ( true ) && false )
which is
! ( false )
which is
true
Try to go through it one-by-one:
! ( ( true || false ) && false )
3 ( ( 1 ) 2 )
1) true || false => true because it is true if at least either of them is true.
2) true && false => false because it is only true if both are true, i.e. if at least either of them is false, it evaluates to false.
3) !(false) => true because '!' means negation, the negation of false is true, and the negation of true is false.
For these kind of Boolean logic issues I always try to break it into steps.
So for this the first condition
( true || false )
This is equal to true as you're saying true OR false
The next condition can now be read as
( true && false )
Which is false
The final bit that makes it true as oppssed to false is the !
The final part can be equated to
!( false )
The ! flips the value so the final statement is true

In Sass, how to compare two or more values in if statement?

I know that in PHP I can do something like this:
if ($foo == true && $bar == true) {
// Both true
} elseif ($foo == false && $bar == false) {
// Both false
}
How could I do that in Sass? Or can I... The docs are sparse on this topic http://sass-lang.com/documentation/file.SASS_REFERENCE.html#_6
I tried something like this:
#if $foo == false $bar == false {
// Both false
}
Doesn't give error but it only evaluates the $foo.
This also wont work:
#if $foo == false && $bar == false {
// Both false
}
Nor this:
#if $foo == false AND $bar == false {
// Both false
}
Thanks!
From §6.4.4 - Boolean Operations:
SassScript supports and, or, and not operators for boolean values.