Google Sheets单个onEdit类函数可用,多函数失效问题咨询
Hey there! Let's clear up what's going on here and get your timestamp functionality working for all sheets.
First off, this isn't about function order at all—here's the core issue: Google Sheets only automatically triggers specifically named simple trigger functions like onEdit() or onOpen(). When you copied one of those functions and renamed it (say, to onNewSheetEdit()), Sheets has no idea to run that function when an edit happens. Simple triggers rely on exact naming conventions to be detected automatically.
You have two solid solutions here, depending on your preference:
1. Consolidate All Logic into a Single onEdit() Function (Recommended)
Since your only difference is the target column for timestamps across sheets, combining everything into one function makes maintenance way easier (no duplicate code to update later). Here's how you can structure it:
function onEdit(e) { // Get the active sheet and edited range from the event object const activeSheet = e.source.getActiveSheet(); const editedRange = e.range; const sheetName = activeSheet.getName(); // Map sheet names to their respective timestamp columns (adjust these to match your setup) const timestampColumnMap = { "FirstSheet": 4, // Example: Column D for "FirstSheet" "CommsSheet": 6, // Example: Column F for "CommsSheet" "NewSheet": 2 // Example: Column B for your new sheet }; // Check if the current sheet is in our map—if not, exit early if (!timestampColumnMap[sheetName]) return; // Skip header rows (adjust the row number if your header is on a different row) const editedRow = editedRange.getRow(); if (editedRow <= 1) return; // Set the timestamp in the correct column for this sheet activeSheet.getRange(editedRow, timestampColumnMap[sheetName]).setValue(new Date()); }
This way, every edit triggers the same function, which checks which sheet was edited and uses the right column for the timestamp.
2. Set Up an Installable Trigger for Your Custom Function
If you really want to keep separate functions, you can create an installable trigger for your new function. Unlike simple triggers, installable triggers can run any function you specify when an edit occurs. Here's how to set it up:
- Open the script editor (Extensions > Apps Script)
- Click the clock icon (Triggers) in the left sidebar
- Click "+ Add Trigger" at the bottom right
- For "Choose which function to run", select your custom-named function
- For "Choose which deployment to run", select "Head"
- For "Select event source", choose "From spreadsheet"
- For "Select event type", choose "On edit"
- Click "Save" and follow the prompts to authorize the trigger
Note: Installable triggers require authorization (unlike simple triggers) and can get messy if you end up with multiple triggers for similar events. That's why consolidating into one onEdit() is usually the cleaner approach.
Either way, you'll get your timestamp functionality working for all your sheets without issues.
内容的提问来源于stack exchange,提问作者J.E.T

