我有这个简单的脚本:
function myFunction() {
var ssh = SpreadsheetApp.getActiveSpreadsheet();
var ss = ssh.getActiveSheet();
var s = ss.getActiveRange();
var rowIndex = s.getRowIndex();
var colIndex = s.getColumnIndex();
// Get the number of columns in the active sheet.
var colNumber = ss.getLastColumn();
if (colIndex == 1 && rowIndex != 1) {
//Get User Name from inputBox
var name = Browser.inputBox('Owner', 'Enter your Name', Browser.Buttons.OK_CANCEL);
var r1 = ss.getActiveRange().getRow();
//Insert the Name in the active row in the column 6
ss.getRange(r1, 6).setValue(name)
//Here I have other part of the code but is for copy rows to other sheet.
}
}
在第6列中进行有效的数据验证(不允许插入不在项目列表中的值)。例如:查尔斯,奥斯卡,保罗和其他。如果我输入的名称全部为小写,请不要输入。如果输入的值与“查尔斯”相同,则输入该值。
现在,这些值可能会在输入框中显示为下拉列表吗?我的意思是,用户可以从该列表中进行选择,然后按Enter或Ok,然后将值插入到单元格中。
注意:这不是表格。这是电子表格。
谢谢,
function Historic(e) { //CopyRows
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
if (s.getName() == 'ISP1'){
var r = SpreadsheetApp.getActiveRange();
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
var colNumber = s.getLastColumn();
// Move row based on criteria in column 1, and if row is not the header.
if (colIndex == 1 && rowIndex != 1) { // 1 Is the Comment Column
//Call the Function Menu List
showNamePicker();
//Get values for the active row bases on the criteria.
var status = s.getRange(rowIndex, colIndex).getValue();
// --------------- Copy ROW only when someone modify the Column1 --------------------------
// Do nothing if criteria value is not actually changed to something else.
if (s.getName() != 'Historic') {
// The target sheet is the one with the same name as the criteria value.
var targetSheet = ss.getSheetByName('Historic');
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
//=====Copy the Row from ISP1 to Historic=======
s.getRange(rowIndex, 1, 1, colNumber).copyTo(target);
}
}
}
}
function showNamePicker() {
var app = UiApp.createApplication().setHeight(100).setWidth(180);
var handler = app.createServerHandler('setName');
var list = app.createListBox().setName('list').addChangeHandler(handler);
var names = ['Choose a name here','Charles', 'Oscar', 'Paul', 'Other'];
for(var n in names){
list.addItem(names[n]);
}
handler.addCallbackElement(list);
app.add(list);
SpreadsheetApp.getActive().show(app);
}
function setName(e){
var ssh = SpreadsheetApp.getActiveSpreadsheet();
var ss = ssh.getActiveSheet();
var r1 = ss.getActiveRange().getRow();
ss.getRange(r1, 6).setValue(e.parameter.list);
}
历史功能:如果有人修改了列1,脚本将复制已修改为历史的行中的所有数据。
需要:
在脚本将行复制到历史记录之前,应在该行的第6列中插入名称。
问题:
当有人修改第1列时,用户按ENTER键,新的活动行会因为不相同而发生更改,并且脚本不会在修改的行中插入名称。例如:我修改A2并按ENTER,新行是A3,脚本会将信息插入F3,而不是F2。
我尝试在此行之前调用该函数:
//根据条件获取活动行的值。
var status = s.getRange(rowIndex,colIndex).getValue();
但是我仍然是新手...我很简单,但是我无法解决问题。
如果您能帮助我,我将不胜感激。
例子:
更改单元格A2 = ID#12578脚本应在F2列中插入名称,然后将所有包含F2的行复制到历史记录表中。(脚本未在F2中插入名称,而是在F3中插入
您将必须用一个对话框替换Browser.inputBox,该对话框将保留一个具有所需值的列表框。
您可以使用UiApp或HTML服务来构建此对话框。
问题将是,没有什么可以阻止直接在单元格中输入值,但是在您的代码中已经是如此。
您可以在任何地方将列表值存储在隐藏的列,另一个工作表,另一个电子表格中或ScriptProperties中,也可以存储在代码本身中……这是选择的问题:-)
编辑:使用UiApp的一个小示例:
function showNamePicker() {
var app = UiApp.createApplication().setHeight(100).setWidth(120);
var handler = app.createServerHandler('setName');
var list = app.createListBox().setName('list').addChangeHandler(handler);
var names = ['Choose a name here','Charles', 'Oscar', 'Paul', 'Other'];
for(var n in names){
list.addItem(names[n]);
}
handler.addCallbackElement(list);
app.add(list);
SpreadsheetApp.getActive().show(app);
}
function setName(e){
var ssh = SpreadsheetApp.getActiveSpreadsheet();
var ss = ssh.getActiveSheet();
var r1 = ss.getActiveRange().getRow();
ss.getRange(r1-1, 6).setValue(e.parameter.list);
return UiApp.getActiveApplication().close();// close the Ui
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句