Search code examples
google-sheetsgoogle-apps-script

How to trigger function if no empty rows are left?


Currently, I use the following code to trigger a function if the user inputs a value into cell A1.

I would like to automate this process by triggering the function if no more empty rows are left on the active sheet tab.

Is this possible?

My current code:

if (spreadsheet.getActiveSheet().getSheetName() == "Vocabulary") {
    if(spreadsheet.getActiveSheet().getRange("A1").getValue() !== "") {
        triggerFunction();
}

Solution

  • The following code will trigger triggerFunction whenever a value is entered in any column of the last empty row in any sheet:

    function onEdit(e) {
      const sheet = e.range.getSheet();
      if (sheet.getLastRow() !== sheet.getMaxRows()) return;
      const data = sheet.getDataRange().getValues();
      if (data.every(row => row.some(cell => cell))) {
        triggerFunction();
      }
    }