I would like to implement a function that allows characters and numbers to be entered into a spreadsheet, and if nothing is entered using GAS, a message box will be displayed.
Therefore, I would like to use the following function to determine whether information has been entered in the cell. However, if the number 0
is entered in the cell, the number 0
will be determined as an empty character. As a result, it is determined that nothing has been entered even though 0
has been entered.
function isEmpty(value) {
return value == null;
}
I would like to return false
when the number 0
is input, but how can I do that?
By the way, when I asked ChatGPT, they presented the following code, but with this code, even if a number is entered in the cell, it is determined that it is not, and true
is returned.
function isEmpty(value) {
return value === null ||
typeof value === 'undefined' ||
(typeof value === 'string' && value.trim() === '') ||
(typeof value === 'number' && value !== 0);
}
2
Answers
Try this function:
Changes:
undefined
.value !== 0
tovalue === 0
, since, if I understand your situation correctly,0
should be treated as empty.0
passed as a string.These are the test cases you can run to check:
Or using an onEdit trigger:
You cannot run these functions without supplying the event object. If you attempt to run from the script editor without supplying the event object you will get an error. The onEdit trigger is a simple trigger meaning that it cannot perform functions that require permission. If you need to perform commands that require permission you must create an installable trigger either programmatically or through the editors trigger panel.
There are several limitations running scripts this way. You will have difficulty using alert and prompt boxes. They are fairly difficult to debug because the onEdit is not easily run in debug mode so you are left having to edit the appropriate elements on the desired sheet."