I have a Google Sheet which I gave Edit permission for few users. first raw is protected with the titles. Column A is a date that I want to have a format of dd/mm/yyyy
only. I managed to force the format if the user manually enters a date with another format, but I have a problem if someone pastes a value to the cell, and then it can get any format they paste. How can I make sure it is always with format dd/yy/yyyy
? I don't want to use an apps script that formats the date to my dd/MM/yyyy
format, but I want to reject it if the format isn't dd/MM/yyyy
.
I tried the solution from this old question: Custom date validation in google sheets with popup alert but this isn't what I am looking for as I don't want to popup also if this is a valid date but not with my format. I have a simple way to reject it if the user manually enters the date (I use for the format of the cell to dd/MM/yyyy
). My problem is only with users who paste values into it.
function onEdit(e) {
let range = e.range;
let col = range.getColumn();
if (col == 1) {
let value = range.getValue();
let newDate = new Date(value);
let timeCheck = newDate.getTime();
let isTime = (timeCheck === timeCheck);
if (!isTime) {
let message = "'" + value + "' is not a date";
let ui = SpreadsheetApp.getUi();
ui.alert(message);
range.setValue("");
} else {
range.setValue(newDate).setNumberFormat("dd/MM/yyyy");
}
}
}