![](/img/trans.png)
[英]How to check if the user is idle and switch tabs? via script (Google Spreadsheet - GAS)
[英]how to check if the input data in google spreadsheet is a valid Date type by GAS
我正在研究 Google Apps 腳本。
如果值是有效日期,我想檢查單元格值。 用戶輸入或編輯應以有效日期格式輸入的數據(例如 2020/05/25),然后在執行 onEdit 時觸發可安裝觸發器以檢查單元格值。
function checkData_colA(e){
//there are some other codes.
...
var str = e.value;
var range = e.range;
//ignore check when str is empty.
if(str !== "" && !isValidDate(date)){
//pop up alert
}
}
// From http://stackoverflow.com/questions/1353684
// Returns 'true' if variable d is a date object.
function isValidDate(d) {
if ( Object.prototype.toString.call(d) !== "[object Date]" )
return false;
return !isNaN(d.getTime());
}
但是,它沒有按預期工作。 它有什么問題?
根據答案,我嘗試了這個。
function checkData_colA(e){
//there are some other codes.
...
var str = e.value;
var range = e.range;
Logger.log(str);//output: 43956
//ignore check when str is empty.
if(str !== "" && !isDate(date)){
//pop up alert
}
}
function isDate(date){
return(Object.prototype.toString.call(date) === '[object Date]');
}
但它不起作用。 當輸入“2020/5/5”時,會彈出警報。 而且, Logger.log(str);//output: 43956
。
嘗試像這樣進行 onEdit() :
使用 e.range.getValue() 代替 e.value。
function onEdit(e){
const sh=e.range.getSheet();
if(sh.getName()=='Sheet1'&& e.range.columnStart==1 && e.value) {
if(isDate(e.range.getValue())) {
e.source.toast('Yip');
}else{
e.source.toast('Nope');
}
}
}
function isDate(date){
return(Object.prototype.toString.call(date) === '[object Date]');
}
我有時注意到 e.value 和 e.range.getValue() 之間的區別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.