Get your API key

Lesson 3 mein se 5

Menus, buttons aur checkboxes

Custom menu, button bani drawing, simple onEdit email kyun nahi bhej sakta, aur woh columns protect karna jo sirf script likhe.

8 min Login karke kholo
Ek baar mein ek step, page palatne jaisa.

Is lesson ke end tak aapki sheet ka apna Leave app menu aur ek bada Approve selected button hoga, dono ek hi safe decide function use karte hue, aur status columns galti se edit hone se protected honge.

Aapko chahiye: lesson 2 ka chalta hua form aur Requests tab, aapka SPEC.md, aur kuch dummy Pending requests · Time: 8 minute

  1. Approve karna kai jagah se shuru ho sakta hai: menu, button, agle lesson ka phone page. Sabko same rules follow karne hain, isliye sab ek hi function ko bulaate hain jo asli kaam aur checks karta hai. Pehle wahi AI se maango:

    Prompt · ise apne AI agent mein paste karo
    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();
      }
    }
    Kyun?

    Har button wahi checks bulaata hai. Agar "sirf Pending badal sakta hai" wala rule teen jagah hota, toh kabhi na kabhi ek jagah bhool jaata, aur rejected request chupchaap approved ban sakti thi.

Baaki lesson padhne ke liye free login karo

Har lesson free hai. Free account se saare lessons khul jaate hain aur aapki progress save hoti hai.

Is lesson mein aage:

  1. Custom menu jodo
  2. Bada button jodo (sirf computer par)
  3. Checkbox ka jaal: simple onEdit email nahi bhej sakta
  4. Woh columns protect karo jo sirf app likhe
  5. Recap