0

I have added a custom menu and script to my google sheets page. I get an error saying Script function not found, followed by showing me my entire script! What have I done wrong?

Error:

Script function not found: 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]; if (!curName.trim()) { continue; } if (!notesByName[curName]) 
   { notesByName[curName] = String(curNote); } else { 
    notesByName[curName].concat(curNote); } } var outputData = 
    Object.keys(notesByName).map(function (name) { return [name, 
notesByName[name]]; }); dataRange.clearContent(); var newDataRange = 
sheet.getRange(2, 1, outputData.length, 2); 
newDataRange.setValues(outputData); } For more information, see 
https://developers.google.com/apps-
script/reference/base/menu#addItem(String,String)`

Code:

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];
    // empty name
    if (!curName.trim())
    {
      continue;
    }
    // if name found first time, save it to object
    if (!notesByName[curName])
    {
      notesByName[curName] = String(curNote);
    }
    // if duplicate, sum numbers
    else
    {
      notesByName[curName].concat(curNote);
    }
  }
  // prepare data for output
  var outputData = Object.keys(notesByName).map(function(name){
    return [name, notesByName[name]];
  });
  // clear old data
  dataRange.clearContent();
  // write calculated data
  var newDataRange = sheet.getRange(2, 1, outputData.length, 2);
  newDataRange.setValues(outputData);
}

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  var menu = ui.createMenu('Custom Menu');
  var item = menu.addItem('Merge Duplicates', mergeDuplicates);
  item.addToUi();
}
0

1 Answer 1

1

What does your menu code look like? I'm guessing it's a problem with the way you did the addItem in one of your menus. Perhaps your additem looks like this addItem('Description','mergeDuplicates()') rather than this addItem('Description','mergeDuplicates')

Sign up to request clarification or add additional context in comments.

2 Comments

My menu code is var item = menu.addItem('Merge Duplicates', mergeDuplicates); => I had seen a few stack questions where that was the issue
Is that 'mergeDuplicates' or mergeDuplicates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.