我在电子表格的脚本编辑器中编写了一个功能,旨在关注包含当前日期的单元格。通常它在菜单输入触发功能时起作用,但在加载电子表格后,它不会专注于任何单元格。
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Go to Today",
functionName : "goToToday" // goToToday() contains same code as last 3 lines of onOpen()
}];
spreadsheet.addMenu("Script", entries); // works on manual run and on startup
var s = spreadsheet.getSheetByName("Sheet1");
var r = 25 + s.getRange("I4").getValue();
s.setActiveSelection(s.getRange("A" + r )); // works on manual run, doesn't work on startup
};
答案 0 :(得分:1)
可以在打开...
上显示自定义对话框function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Go to Today",
functionName : "goToToday" // goToToday() contains same code as last 3 lines of onOpen()
}];
ss.addMenu("Script", entries); // works on manual run and on startup
var ui = SpreadsheetApp.getUi();
var app = UiApp.createApplication();
var btn = app.createButton("Go to Today", app.createServerHandler("goToToday"))
app.add(btn);
ui.showModalDialog(app, "Start");
};
function goToToday() {
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var r = 25 + s.getRange("I4").getValue();
s.setActiveSelection(s.getRange("A" + r ));
}
只需让用户点击按钮即可设置活动单元格。
答案 1 :(得分:1)
根据Bryan的回答,我尝试了另一种可能会自动关闭对话框的代码。
我调用UiApp弹出窗口的方式稍有改动。
function onOpen() {
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var ui = SpreadsheetApp.getUi();
ui.createMenu('Goto today').addItem('go !','goToToday').addToUi();
var app = UiApp.createApplication().setHeight(40).setWidth(180).setTitle('Start');
var btn = app.createButton('Go to Today in cell A'+(25 + (s.getRange("I4").getValue())), app.createServerHandler("goToToday"))
.setStyleAttributes({'fontFamily':'arial','fontSize':'10pt','fontWeight':'bold','color':'white','background':'#3870ff'}).setPixelSize(175,35);
app.add(btn);
SpreadsheetApp.getActive().show(app);
}
function goToToday() {
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var r = 25 + s.getRange("I4").getValue();
s.setActiveSelection(s.getRange("A" + r ));
return UiApp.getActiveApplication().close();
}