我已经在我的谷歌表格页面中添加了一个自定义菜单和脚本。 我收到一个错误,说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();
}
你的菜单代码是什么样的? 我猜这是您在其中一个菜单中执行 addItem 的方式有问题。 也许您的添加项看起来像这样addItem('Description','mergeDuplicates()')
而不是这个addItem('Description','mergeDuplicates')