Google Sheets报错‘Script function not found’问题排查求助
First, let's tackle the immediate error you're facing: the "Script function not found" message when clicking your custom menu. This is a straightforward fix related to how you're registering the menu item.
The Main Issue
When using addItem to create a menu entry, the second parameter needs to be a string matching your function's exact name—not the function reference itself. In your original code, you passed mergeDuplicates directly instead of wrapping it in quotes as a string.
Corrected onOpen Function
function onOpen() { var ui = SpreadsheetApp.getUi(); var menu = ui.createMenu('Custom Menu'); // Pass the function name as a quoted string here menu.addItem('Merge Duplicates', 'mergeDuplicates') .addToUi(); }
Bonus Fix: Duplicate Notes Not Combining
Once you fix the menu error, you'll notice your script still doesn't merge notes correctly. That's because concat() returns a new string but doesn't update the value stored in your notesByName object. You need to assign the concatenated result back to the object key. I also added a separator (comma + space) to make combined notes more readable:
Updated mergeDuplicates Function
function mergeDuplicates() { var target = SpreadsheetApp.getActiveSpreadsheet(); var sheet = target.getSheetByName("Test"); var lastRow = sheet.getLastRow(); var dataRange = sheet.getRange(2, 1, lastRow - 1, 2); var data = dataRange.getValues(); var notesByName = {}; for (var i = 0; i < data.length; i++) { var row = data[i]; var curName = row[0]; var curNote = row[1]; // Skip rows with empty names if (!curName.trim()) { continue; } // Initialize note for first occurrence of the name if (!notesByName[curName]) { notesByName[curName] = String(curNote); } // Append note for duplicate names (with a separator) else { notesByName[curName] = notesByName[curName].concat(', ', curNote); } } // Format data for output var outputData = Object.keys(notesByName).map(function(name){ return [name, notesByName[name]]; }); // Clear old data and write merged results dataRange.clearContent(); var newDataRange = sheet.getRange(2, 1, outputData.length, 2); newDataRange.setValues(outputData); }
How to Apply the Fixes
- Replace your existing functions with the updated versions above.
- Save your script, then refresh your Google Sheet.
- The "Custom Menu" will now appear, and clicking "Merge Duplicates" will run the function without errors—correctly combining notes for duplicate names.
内容的提问来源于stack exchange,提问作者IWI




