Let's get this script working for you! This Google Apps Script is designed to automate sending raffle codes when a row is marked as "Verified". Here's a breakdown of common issues and how to troubleshoot them, along with some improvements.
This script runs automatically when a cell is edited in your Google Sheet. Specifically, it checks if:
The edit is made on the "Electronic Raffle Ticket" sheet.
The edit is in Column K (the "Verified" column).
If these conditions are met, it then checks if:
The "Verified" column (K) for that row is "Yes".
The "Codes Sent" column (M) is not "Yes".
The "Confirmed Tickets" column (L) has a value greater than 0.
The "Email" column (J) has an email address.
If all these conditions are true, the script will:
Generate a number of unique codes based on the "Confirmed Tickets" value.
Write these codes into the "Unique Codes" column (N) of that row.
Send an email with these codes to the email address in Column J.
Mark the "Codes Sent" column (M) as "Yes" to prevent duplicate emails.
Here are the most common reasons why your onEdit function might not be working as expected, along with how to fix them:
1. Authorization Issues 🔒The most frequent problem with Google Apps Script, especially when sending emails or accessing external services, is lack of authorization. When you run a script that performs actions like sending emails for the first time, Google requires you to grant it permission.
How to fix:
Open your Google Sheet.
Go to Extensions > Apps Script.
In the Apps Script editor, find any function (you can just select onEdit from the dropdown, though you can't run it directly) and click the "Run" button (looks like a play icon ▶️).
A pop-up will appear asking you to "Review permissions". Click on it.
Select your Google account.
You'll likely see a warning that "Google hasn't verified this app." This is normal for personal scripts. Click on "Go to [Project Name] (unsafe)" at the bottom.
Finally, click "Allow" to grant the necessary permissions.
Once authorized, the onEdit function should run automatically when triggered by an edit.
The onEdit(e) function is a special simple trigger that runs automatically when a user manually changes a value in a spreadsheet. However, simple triggers have limitations (e.g., they cannot send emails directly without prior authorization or cannot run if the edit was made by another script).
Consider an Installable Trigger: If the simple onEdit trigger isn't consistently working (e.g., you're sharing the sheet, or other scripts are making changes), you might need an installable onEdit trigger. This type of trigger runs under your authorization and has fewer restrictions.
In the Apps Script editor, on the left sidebar, click on "Triggers" (the clock icon ⏰).
Click "+ Add Trigger" in the bottom right.
For "Choose which function to run", select onEdit.
For "Choose event source", select "From spreadsheet".
For "Choose event type", select "On edit".
Click "Save". You will likely need to re-authorize the script as described in point 1.
Google Apps Script is case-sensitive, and even small typos can prevent your script from working.
sheetName: Ensure the sheetName variable ("Electronic Raffle Ticket") exactly matches the name of your sheet in Google Sheets, including spaces and capitalization.
Column Headers/Values: Double-check that the values you're comparing (e.g., "Yes") exactly match what's in your sheet. For instance, if your sheet has "yes" (lowercase) but your script checks for "Yes" (uppercase), it won't match.
Your script already uses Logger.log(), which is excellent for debugging!
How to use: After making an edit in your sheet that should trigger the script:
Go to Extensions > Apps Script.
In the Apps Script editor, click on "Executions" (the list icon 📋) on the left sidebar.
You'll see a list of script runs. Click on the most recent onEdit execution.
The "Log" section will show you the output of your Logger.log() statements. This will tell you if the script is running, what values it's reading (e.g., Verified, Tickets, CodesSent, Email), and which if conditions are being met or skipped.
The log Logger.log("Row: " + row + ", Verified: " + verified + ", Tickets: " + confirmedTickets + ", CodesSent: " + codesSent + ", Email: " + email); is particularly useful. Check the values displayed here to ensure they are what you expect.
Confirm that your columns are correctly set up as expected by the script:
Column K: "Verified" (where you'll be typing "Yes")
Column L: "Confirmed Tickets" (should contain a number like 1, 2, 3...)
Column M: "Codes Sent" (will be marked "Yes" by the script)
Column J: "Email"
Column N: "Unique Codes" (where generated codes will appear)
Here are a few suggestions to make your script more robust and readable:
Early Exit: The script now checks the sheet name at the very beginning and uses return; if it's not the target sheet. This makes the code more efficient by avoiding unnecessary operations.
Constants for Columns: Using const for column numbers (VERIFIED_COL, etc.) makes the code more readable and easier to maintain.
Batch Reading: sheet.getRange(editedRow, 1, 1, sheet.getLastColumn()).getValues()[0]; reads the entire row's data in one go, then you access specific values from that array. This is much more efficient than calling sheet.getRange().getValue() multiple times, reducing the number of calls to the Google Sheets API.
Batch Writing (setValues): Instead of setValue twice, the improved code attempts to update both the UNIQUE_CODES_COL and CODES_SENT_COL in a single setValues call. This is a significant performance improvement. It involves figuring out the range that encompasses both columns and constructing a 2D array for setValues.
Error Handling for Email: A try...catch block around MailApp.sendEmail will help you identify if email sending fails and why, logging the error.
Strict Equality (===): Using === instead of == is generally a good practice in JavaScript as it checks both value and type, preventing unexpected type coercion issues.
Try implementing these suggestions, and use the Executions log in Apps Script to pinpoint exactly where the script might be stopping or behaving unexpectedly. Let me know what you find in the logs!
Thanks for the breakdown—super helpful! Just one quick thing: could you clarify who all the people mentioned in the message are? I want to make sure I fully understand the context before moving forward.
Appreciate your help!
Best,
Cayden
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/934c4cf5-092f-4f65-8320-42b7cfbc9235n%40googlegroups.com.