It appears you're trying to create clickable images in Google Sheets that trigger the sendEmail function when clicked. However, the approach you're using—embedding a javascript: call within the HYPERLINK function—is not supported in Google Sheets due to security restrictions. Google Sheets doesn't allow executing scripts directly from cell formulas or hyperlinks.
Why Your Current Approach Doesn't Work:
- Security Limitations: Google Sheets prevents the execution of JavaScript code from within cell formulas to protect users from potential security risks.
- Formula Limitations: The
HYPERLINK function is designed to navigate to URLs, not to execute Apps Script functions.
- Script Triggers: Apps Script functions can't be directly triggered by clicking on a cell or an image inserted via a formula.
Alternative Solution: Using Checkboxes and an onEdit Trigger
A reliable way to achieve the functionality you want is to use checkboxes in your sheet and an onEdit(e) function that sends the email when a checkbox is checked. Here's how you can modify your code:
Step 1: Modify Your createButtons Function to Insert Checkboxes
Replace your createButtons function with one that inserts checkboxes in column C:
function createCheckboxes() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
// Clear existing checkboxes in column C
var checkboxesRange = sheet.getRange(2, 3, values.length - 1); // Assuming row 1 is headers
checkboxesRange.clearContent();
checkboxesRange.uncheck();
checkboxesRange.clearDataValidations();
// Insert checkboxes in column C starting from row 2
checkboxesRange.insertCheckboxes();
}
- Explanation:
- Clearing Existing Checkboxes: Ensures that old checkboxes and their states are removed.
- Inserting New Checkboxes: Adds a checkbox to each cell in column C for each row that contains data.
Step 2: Create an onEdit(e) Function to Handle Checkbox Changes
Implement an onEdit(e) function that sends an email when a checkbox is checked:
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
// Check if the edit was in column C (column index 3) and from row 2 onwards
if (range.getColumn() == 3 && range.getRow() >= 2) {
var checkboxValue = range.getValue();
if (checkboxValue === true) { // Checkbox checked
var row = range.getRow();
sendEmail(row);
// Mark as sent
sheet.getRange(row, 4).setValue("Email Sent"); // Column D
// Uncheck the box
range.setValue(false);
}
}
}
- Explanation:
- Trigger on Checkbox Edit: The function checks if the edited cell is in the checkbox column.
- Send Email: Calls the
sendEmail function with the specific row number.
- Mark as Sent: Writes "Email Sent" in column D.
- Reset Checkbox: Unchecks the checkbox after sending the email.
Step 3: Adjust Your sendEmail Function
Ensure your sendEmail function uses the correct indices:
function sendEmail(row) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Get the subject and body from the specified row
var subject = sheet.getRange(row, 1).getValue(); // Column A
var body = sheet.getRange(row, 2).getValue(); // Column B
// Replace with your recipient email
var recipient = "recipient@example.com"; // Change to the desired email
// Send the email
MailApp.sendEmail({
to: recipient,
subject: subject,
body: body
});
}
- Note: Replace
"recipient@example.com" with the actual recipient's email address.
Step 4: Run the createCheckboxes Function
After updating your script, run the createCheckboxes function manually to set up the checkboxes in your sheet.
How It Works in Google Sheets
- Checkbox Interaction: Users can click the checkbox in column C next to the row containing the email data they want to send.
- Automatic Script Execution: The
onEdit(e) trigger automatically detects when a checkbox is checked and runs the sendEmail function.
- Feedback in the Sheet: After sending the email, the script writes "Email Sent" in column D and resets the checkbox.
Important Notes
- Authorization Required: The first time you run the script, you'll need to authorize it to access your Gmail account to send emails.
- Single Recipient: The script sends emails to a single recipient. If you want to send to different recipients per row, add a recipient column and modify the
sendEmail function accordingly.
- Error Handling: For production use, consider adding error handling to manage cases where subject or body is missing.
Limitations and Considerations
- Apps Script Quotas: Be aware of Google Apps Script quotas for sending emails.
- Concurrent Edits: If multiple users are editing the sheet simultaneously, you may need to account for concurrent modifications.
Summary:
To achieve the functionality of clicking something in Google Sheets to run a script, you can't use a hyperlink with a JavaScript call. Instead, use checkboxes with an onEdit(e) trigger. This method is reliable and adheres to Google Sheets' security model.