What is the right way to use OR and AND in appscript? - google-apps-script

Is this the right way to write OR condition to combine the 2 if statements into 1?
if (activeSheet.getName() == ("2 Grade" || "3 Grade") && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
supplierSeal2G.copyTo(supplierSeal2GData, SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
surveyorSeal2G.copyTo(surveyorSeal2GData, SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
remarks2G.copyTo(remarks2GData, SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
}

How about this modification? Please think of this as just one of several answers.
Pattern 1:
In this pattern, sheetName is declared and used for the if statement.
From:
if (activeSheet.getName() == ("2 Grade" || "3 Grade") && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
To:
var sheetName = activeSheet.getName();
if ((sheetName == "2 Grade" || sheetName == "3 Grade") && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
Pattern 2:
In this pattern, ["2 Grade", "3 Grade"] is prepared as an array and used for the if statement with indexOf().
From:
if (activeSheet.getName() == ("2 Grade" || "3 Grade") && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
To:
var sheetNames = ["2 Grade", "3 Grade"];
if (sheetNames.indexOf(activeSheet.getName()) > -1 && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
References:
if...else
indexOf()
If this was not the result you want, I apologize.

Related

Date Stamp in multiple Columns Google Sheets

I have started using this script to auto populate the date when data is entered into the column next to it for Google Sheets. It needs to do this on multiple columns as new data is entered. This script works on all of the columns but the last paired: 13-14 and 15-16. It was working before but now it has stopped. Any help with what I need to do? I'm not versed in code and I learned this much from YouTube! Thanks!
function onEdit(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
if(col == 5 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" ) {
e.source.getActiveSheet().getRange(row,6).setValue(new Date())
if(col == 7 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,8).setValue(new Date())
if(col == 9 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,10).setValue(new Date())
if(col == 11 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,12).setValue(new Date())
if(col == 13 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,14).setValue(new Date())
if(col == 15 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,16).setValue(new Date())
}
}
Try it this way
function onEdit(e) {
const sh = e.range.getSheet();
const idx = [5,7,9,11,13,15].indexOf(e.range.columnStart);
if(sh.getName() == "IRLA Level" && e.range.rowStart > 1 && ~idx) {
e.range.offset(0,1).setValue(new Date());
}
}
Bitwise Not

Number of columns in the data does not match the number of columns in the range

I know this issue has been posted about quite a few times but I'm still struggling to make it work. I have the code below:
function conditionalFormat (subGradeRange, colourRange, colourRangeVals) {
subGradeRange.setBorder(true, true, true, true, true, true);
subDash.setColumnWidths(11, 11, 40)
subDash.setColumnWidth(10, 70);
subDash.setColumnWidths(22, 4, 80);
subGradeRange.setHorizontalAlignment("center");
var colours = [];
for (var i = 1; i < colourRangeVals.length; i++) {
colours[i] = [];
for (var j = 0; j < colourRangeVals[i].length; j++) {
var avgTarget = subDash.getRange("O1").getValue();
var gradeCell = subDash.getRange(3, j + 12).getValue();
var avgTargetCell = subSheetPP.getRange(i + 1, 17).getValue();
var mockGradeCell = subDash.getRange(i + 3, j + 12);
var mockGradeCellVal = mockGradeCell.getValue();
if (avgTarget == 0 && gradeCell > avgTargetCell && mockGradeCellVal >= 0) {
colours[i][j] = "#dabeec";
} else if (avgTarget == -1 && gradeCell > avgTargetCell -1 && mockGradeCellVal >= 0) {
colours[i][j] = "#dabeec";
} else if (avgTarget == -2 && gradeCell > avgTargetCell - 2 && mockGradeCellVal >= 0) {
colours[i][j] = "#dabeec";
} else if (avgTarget == 0 && gradeCell == avgTargetCell && mockGradeCellVal >= 0) {
colours [i][j] = "#b8dac1";
} else if (avgTarget == -1 && gradeCell == avgTargetCell -1 && mockGradeCellVal >= 0) {
colours [i][j] = "#b8dac1";
} else if (avgTarget == -2 && gradeCell == avgTargetCell - 2 && mockGradeCellVal >= 0) {
colours [i][j] = "#b8dac1";
} else if (avgTarget == 0 && gradeCell < avgTargetCell && mockGradeCellVal >= 0) {
colours [i][j] = "#e7b3b3";
} else if (avgTarget == -1 && gradeCell < avgTargetCell - 1 && mockGradeCellVal >= 0) {
colours [i][j] = "#e7b3b3";
} else if (avgTarget == -2 && gradeCell < avgTargetCell - 2 && mockGradeCellVal >= 0) {
colours [i][j] = "#e7b3b3";
} else {
colours [i][j] = 'white';
}
}
}
Logger.log("done");
colourRange.setBackgrounds(colours);
}
When debugging my colourRangeVals has an array of 31 with 10 columns. My colours array has an array of 31 with 10 columns but I still get the above error.
Any ideas??
Thanks
The issue may be caused by the first for loop that starts iteration from 1 instead of 0.
To see the results in the spreadsheet and so make debugging easier, try replacing the last line of code with this:
colourRange.offset(0, 0, colours.length, colours[0].length).setBackgrounds(colours);

(f) script works (e) doesnt

i have these two scripts that do the same thing just on different pages, but the f script works and the e script doesnt, any help??
function onEdit(e) {
if (e.source.getActiveSheet().getName() !== "RS3 Points" ||
e.range.columnStart < 9 || e.range.columnStart > 35 ||
e.range.rowStart < 4 || e.range
.columnStart < e.range.columnEnd) return;
e.range.offset(0, 37 - e.range.columnStart).setValue(new Date());
}
function onEdit(f) {
if (f.source.getActiveSheet().getName() !== "OS Points" ||
f.range.columnStart < 6 || f.range.columnStart > 37 ||
f.range.rowStart < 4 || f.range
.columnStart < f.range.columnEnd) return;
f.range.offset(0, 39 - f.range.columnStart).setValue(new Date());
}
Munkey's intuition is right. You can't have more than one onEdit() in the same project. Moreover 'e' is not a random letter (that can be replaced by any other letter) but stands for 'event object'. You can read more about that here. The solution would indeed consist in 'merging' the two scripts into one. Give this a try:
function onEdit(e) {
var ind = ["RS3 Points", "OS Points"].indexOf(e.source.getActiveSheet()
.getName()),
startCol = [9, 6],
endCol = [35, 37],
offset = [37, 39];
if (ind === -1 || e.range.columnStart < startCol[ind] || e.range.columnStart > endCol[ind] ||
e.range.rowStart < 4 || e.range.columnStart < e.range.columnEnd) return;
e.range.offset(0, offset[ind] - e.range.columnStart)
.setValue(new Date());
}
I don't think you can have 2 onEdits in google scripts.
If you comment out the f function, does the e one work?
cant you put them in one onedit function as a solution.
function onEdit(e) {
if (e.source.getActiveSheet().getName() !== "RS3 Points" ||
e.range.columnStart < 9 || e.range.columnStart > 35 ||
e.range.rowStart < 4 || e.range
.columnStart < e.range.columnEnd) return;
e.range.offset(0, 37 - e.range.columnStart).setValue(new Date());
if (e.source.getActiveSheet().getName() !== "OS Points" ||
e.range.columnStart < 6 || e.range.columnStart > 37 ||
e.range.rowStart < 4 || e.range
.columnStart < e.range.columnEnd) return;
e.range.offset(0, 39 - e.range.columnStart).setValue(new Date());
}
Something like that, but its untested.
I think If i'm reading your code correctly, you're looking for Not equals to sheet name, it might be better to change it to equals instead.

Why does e.preventDefault() and return false not work on keydown event?

I just want to cancel the keydown event when I type the key except a number,
for this, I did the following :
function handleNum(obj){
var e = window.event;
var flag = true;
if( ( e.keyCode >= 48 && e.keyCode <= 57 ) ||
( e.keyCode >= 96 && e.keyCode <= 105 ) ||
e.keyCode == 8 ||
e.keyCode == 46 ||
//e.keyCode == 110 ||
//e.keyCode == 190 ||
e.keyCode == 37 ||
e.keyCode == 39 ||
e.keyCode == 35 ||
e.keyCode == 36 ||
e.keyCode == 9
) {
flag = true;
} else {
alert('You can type only a number!');
console.log("doesn't return?");
e.preventDefault();
flag = false;
}
console.log(flag);
return flag;
}
html :
<input type="text" onkeydown="return handleNum(this);" />
but It doesn't work, how can I solve this problems?
I have no idea why e.preventDefault() don't work in any browser.
Event is undefined, You can pass it like this
<script>
function handleNum(e,obj){
if (!e) var e = window.event;
var flag = true;
if( ( e.keyCode >= 48 && e.keyCode <= 57 ) ||
( e.keyCode >= 96 && e.keyCode <= 105 ) ||
e.keyCode == 8 ||
e.keyCode == 46 ||
//e.keyCode == 110 ||
//e.keyCode == 190 ||
e.keyCode == 37 ||
e.keyCode == 39 ||
e.keyCode == 35 ||
e.keyCode == 36 ||
e.keyCode == 9
) {
flag = true;
} else {
alert('You can type only a number!');
console.log("doesn't return?");
e.preventDefault();
flag = false;
obj.value = obj.value.replace(/\D/g, '');
}
console.log(flag);
return flag;
}
</script>
<input type="text" onKeyDown="return handleNum(event,this);" />
Fiddle
Here is a code that will return false if the key pressed is not a number.
Use the keypress event instead of keydown.
$(fieldid).keypress(function(e) {
if (e.which != 8 && e.which != 0 && (e.which<48 || e.which>57)) {
return false;
}
});

How to implement the AND command in a Google apps function correctly?

I have a function that checks if a cell in column 7 is checked.
if (r.getColumn() == 7 && r.getValue() == "x")
How do I write this to check if EITHER column 7 OR Column 8 are checked?
if (r.getColumn() == 7 && r.getValue() == "x") OR if (r.getColumn() == 8 && r.getValue() == "x")
doesn't seem to work.
OR is represented with ||
So if you want (7 OR 8) AND x , just write :
if ((r.getColumn() == 7 || r.getColumn() == 8) && r.getValue() == "x")