未找到脚本功能 - Google表格问题

时间:2018-01-16 15:56:02

标签: google-sheets google-spreadsheet-api

我已将自定义菜单和脚本添加到我的Google工作表页面。我收到一条错误说Script function not found,然后向我展示了我的整个脚本!我做错了什么?

错误:

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)`

代码:

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();
}

1 个答案:

答案 0 :(得分:1)

您的菜单代码是什么样的?我猜你在其中一个菜单中使用addItem的方式存在问题。也许您的additem看起来像addItem('Description','mergeDuplicates()')而不是addItem('Description','mergeDuplicates')