skip to Main Content

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


  1. Try this function:

    function isEmpty(value) {
      return value === null || 
             value === undefined || 
             (typeof value === 'string' && value.trim() === '') ||
             (typeof value === 'string' && value.trim() === '0') ||
             (typeof value === 'number' && value === 0);
    }
    

    Changes:

    1. You can directly compare to undefined.
    2. value !== 0 to value === 0, since, if I understand your situation correctly, 0 should be treated as empty.
    3. Optional: Added a condition for 0 passed as a string.

    These are the test cases you can run to check:

    const testIsEmpty = () => {
      console.log(1, isEmpty(1))  // false
      console.log(" Text ", isEmpty(" Text "))  // false
      console.log(0, isEmpty(0))  // true
      console.log("0", isEmpty("0"))  // true
      console.log(null, isEmpty(null))  // true
      console.log(undefined, isEmpty(undefined))  // true
      console.log("", isEmpty(""))  // true
      console.log(" ", isEmpty(" "))  // true
    }
    
    Login or Signup to reply.
  2. Or using an onEdit trigger:

    function onEdit(e) {
       if(!e.value) {
        e.source.toast(`You edited the cell ${e.range.getA1Notation()} and now the cell is empty`);
      }
    

    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."

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search