How would I edit this script to only return odd columns in the range?
function onEdit() {
var s = SpreadsheetApp.getActiveSheet(); if (s.getName() == "2025 Attendance") {
var r = s.getActiveCell(); if (r.getColumn() >= 3 && r.getColumn() <= 20) {
var nextCell = r.offset(0, 1); if (nextCell.getValue() === '') //checks whether the adjacent cell is empty or not
nextCell.setValue(new Date());
}
}
}
I've tried changing the range but I can't figure out how to set the range to only odd numbers.
How would I edit this script to only return odd columns in the range?
function onEdit() {
var s = SpreadsheetApp.getActiveSheet(); if (s.getName() == "2025 Attendance") {
var r = s.getActiveCell(); if (r.getColumn() >= 3 && r.getColumn() <= 20) {
var nextCell = r.offset(0, 1); if (nextCell.getValue() === '') //checks whether the adjacent cell is empty or not
nextCell.setValue(new Date());
}
}
}
I've tried changing the range but I can't figure out how to set the range to only odd numbers.
You can try this modified version of your code:
Code
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if (s.getName() == "2025 Attendance") {
var r = s.getActiveCell();
if (r.getColumn() >= 3 && r.getColumn() <= 20 && r.getColumn() % 2 !== 0) {
var nextCell = r.offset(0, 1);
if (nextCell.getValue() === '') { // Checks whether the adjacent cell is empty or not
nextCell.setValue(new Date());
}
}
}
}
I added r.getColumn() % 2 !== 0
which effectively checks if the column being edited is an odd-numbered column and adds it to your other conditions.
Sample Output
Edited Cell | Cell to populate with date |
---|---|
Test edit | 1/15/2025 |
References:
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == '2025 Attendance' && ~[3,5,7,9,11,13,15,17,19].indexOf(e.range.columnStart)) {
if(e.range.offset(0,1).getValue() == "") {
e.range.offset(0,1).setValue(new Date())
}
}
}