我正在使用Google表格,并试图创建一个菜单,允许我运行我选择的几个脚本,我做错了什么,不太确定是什么,有人可以阅读这个,让我知道是什么似乎是错的?提前致谢。
function onOpen() {
var menu = DocumentApp.getUi().createMenu('Menu');
menu.addItem('Compare sheets', 'compare');
menu.addItem('Clear Formats', 'clear');
menu.addToUi();
}
function compare() {
var ss = SpreadsheetApp.getActive(),
master = ss.getSheetByName('NewData'),
newData = ss.getSheetByName('Master'),
dif = ss.getSheetByName('Comparison'),
rows = 500,
columns = 52,
bg = [],
dateCols = [14, 24, 29, 36, 37],
masterDataRange = master.getRange(1, 1, rows, columns),
masterValues = masterDataRange.getValues(),
newDataValues = newData.getRange(1, 1, rows, columns)
.getValues();
dif.getDataRange()
.clear()
.setBackground('White');
for (var i = 0, rl = masterValues.length; i < rl; i++) {
var r = [];
for (var j = 0, cl = masterValues[0].length; j < cl; j++) {
if (dateCols.indexOf(j) > -1 && i > 1) {
if (masterValues[i][j] ) masterValues[i][j] = Utilities.formatDate(new Date(masterValues[i][j]), Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm");
if (newDataValues[i][j] ) newDataValues[i][j] = Utilities.formatDate(new Date(newDataValues[i][j]), Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm");
}
r.push(i < 2 ? "white" : masterValues[i][j] !== newDataValues[i][j] ? "green" : "white");
}
bg.push(r);
}
dif.getRange(1, 1, masterValues.length, masterValues[0].length)
.setValues(masterValues)
.setBackgrounds(bg);
}
function clear(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Master");
first.clearFormats();
}
答案 0 :(得分:0)
您在电子表格中使用DocumentApp
,应该使用SpreadsheetApp
代替!
代码:
function onOpen() {
var menu = SpreadsheetApp.getUi().createMenu('Menu');
menu.addItem('Compare sheets', 'compare');
menu.addItem('Clear Formats', 'clear');
menu.addToUi();
}