2017-08-04 121 views
2

您好,感谢您的帮助。如何将日期选择器添加到下拉列表中

我试图在Google表格中执行的操作是将数据验证日期选择器添加到数据验证下拉选择中。

我想这样做是为了在员工开始和结束日期的情况下使用。特别为结束日期。这个想法是,从下拉选择中,我可以选择“选择一个”,“现在”或“日期”选项,其中“日期”选项启用数据验证日期选择器。

更好的是,如果选择“日期”会立即触发日期选择器。

下面是我一直在试图让此功能工作的代码:

function listCalendar() { 

// Set the data validation for cells in column H6 thru H1000 to require "Choose One", "Present", or "Date", with a dropdown menu. 
var cell = SpreadsheetApp.getActive().getSheetByName('Employees').getRange('H6:H100'); 
var rule1 = SpreadsheetApp.newDataValidation().requireValueInList(['Choose One', 'Present', 'Date']).build(); 

// Set the "Date" option in the dropdown menu to activate a date picker. 
var rule2 = SpreadsheetApp.newDataValidation().requireDate().build(); 
    if (cell == 'Date') {cell.setDataValidation(rule2);} 
    else {cell.setDataValidation(rule1);} 

} 

再次感谢您的帮助!

回答

2

添加下面的代码并设置电子表格onEdit触发器。希望这给你一个开始。

function onEdit() { 
// Set the data validation for cells in column H6 thru H1000 to require "Choose One", "Present", or "Date", with a dropdown menu. 
var cell = SpreadsheetApp.getActive().getActiveSheet().getRange("H6:H100"); 
var rule1 = SpreadsheetApp.newDataValidation().requireValueInList(['Choose One', 'Present', 'Date']).build(); 
var rule2 = SpreadsheetApp.newDataValidation().requireDate().build(); 
     if (SpreadsheetApp.getActive().getActiveSheet().getActiveCell().getValue() == 'Date') { 
     SpreadsheetApp.getActive().getActiveSheet().getActiveCell().setDataValidation(rule2); 
     } 
    else { 
     cell.setDataValidation(rule1); 
     } 
} 
+0

谢谢丽兹!我会鼓励你的建议,并回到这个帖子的结果。 – Azualend

0

巨大的感谢丽思^^
看代码之后丽思贴我意识到,我是在我应该使用的getValue并允许该功能的onEdit部分去过的地方使用getRange规则在彼此之间来回切换。所以,以防万一这对别人来说很有用,下面是我结束的代码:

function onEdit() { 

var ss = SpreadsheetApp.getActive(); 
var employees = ss.getSheetByName('Employees'); 
var cellrange = employees.getRange('H6'); 
var cellvalue = cellrange.getValue(); 

// Set the data validation to require "Choose One", "Present", or "Date", with a dropdown menu. 
var rule1 = SpreadsheetApp.newDataValidation().requireValueInList(['Choose One', 'Present', 'Date']).build(); 

// Set the "Date" option in the dropdown menu to activate a date picker. 
var rule2 = SpreadsheetApp.newDataValidation().requireDate().build(); 

if (cellvalue == 'Date') {cellrange.setDataValidation(rule2);} 
else {cellrange.setDataValidation(rule1);} 

} 
相关问题