Get your API key

Lesson 3 of 5

Menus, buttons and checkboxes

A custom menu, a drawing used as a button, why simple onEdit cannot send email, and protecting the columns only the script should write.

8 min Log in to open
One step at a time, like flipping pages.

By the end of this lesson your sheet will have its own Leave app menu and a big Approve selected button, both using one safe decide function, and the status columns will be protected from accidental edits.

You need: the working form and Requests tab from lesson 2, your SPEC.md, and a few dummy Pending requests · Time: 8 minutes

  1. Approving can start from many places: a menu, a button, a phone page in the next lesson. All of them must follow the same rules, so they all call one function that does the real work and the checks. Ask the AI for it first:

    Prompt · copy it into your AI agent
    Here is the spec of my Google Sheets leave app:
    [paste SPEC.md]
    
    My Apps Script project already has handleLeaveForm(e) and findApprover(email).
    Write one function decide(row, status, note) that approves or rejects one row of the Requests tab (columns A:M as in the spec).
    Checks, in this order, and stop with a clear error message if one fails, changing nothing:
    1. The person running it (Session.getActiveUser().getEmail()) is the approver of that request (findApprover of the row's Email). If the email is blank, treat it as "not allowed".
    2. The row's Status is exactly "Pending".
    Then write Status, Decided by (the approver's email), Decided at (now) and Note into J:M, and email the requester: subject "Your leave LR-0007 is Approved" (or Rejected), with the note if there is one.
    Use LockService so two clicks at once cannot both decide the same row. Short comments in simple English.
    JavaScript
    // Approves or rejects one row of Requests. The menu, the button and the phone page all call this.
    function decide(row, status, note) {
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Requests');
      const lock = LockService.getScriptLock();
      lock.waitLock(30000); // two clicks at once are handled one after the other
      try {
        const r = sheet.getRange(row, 1, 1, 13).getValues()[0]; // columns A to M of that row
        const user = Session.getActiveUser().getEmail();
        const approver = String(findApprover(r[2])).toLowerCase();
        if (!user || user.toLowerCase() !== approver) {
          throw new Error('Only the approver of ' + r[0] + ' can decide it. Nothing changed.');
        }
        if (r[9] !== 'Pending') {
          throw new Error(r[0] + ' is already ' + r[9] + '. Nothing changed.');
        }
        sheet.getRange(row, 10, 1, 4).setValues([[status, user, new Date(), note || '']]);
        MailApp.sendEmail(r[2], 'Your leave ' + r[0] + ' is ' + status,
          'Hi ' + r[3] + ',\n\nYour ' + r[4] + ' leave request ' + r[0] + ' is ' + status + '.' +
          (note ? '\nNote: ' + note : ''));
      } finally {
        lock.releaseLock();
      }
    }
    Why?

    Every button calls the same checks. If the "only Pending can change" rule lived in three places, one of them would eventually be forgotten, and a rejected request could quietly turn into an approved one.

Log in free to read the rest of this lesson

Every lesson is free. A free account unlocks all of them and saves your progress.

Still to come in this lesson:

  1. Add a custom menu
  2. Add a big button (computer only)
  3. The checkbox trap: simple onEdit cannot send email
  4. Protect the columns only the app should write
  5. Recap