2017-08-04 115 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);} 

} 
相關問題