简体   繁体   English

如何检查谷歌电子表格中的输入数据是否是 GAS 的有效日期类型

[英]how to check if the input data in google spreadsheet is a valid Date type by GAS

I am working on Google Apps Script.我正在研究 Google Apps 脚本。

I want to check the cell value if the value is valid date.如果值是有效日期,我想检查单元格值。 User inputs or edits data that should be input in valid date format(eg. 2020/05/25), and then an installable trigger that is triggered when onEdit is executed to check the cell value.用户输入或编辑应以有效日期格式输入的数据(例如 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());
}

but, it doesn't work as expected.但是,它没有按预期工作。 What is wrong with it?它有什么问题?

According to the answer, I tried this.根据答案,我尝试了这个。

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]');
}

but it doesn't work.但它不起作用。 When input "2020/5/5", alert pops up.当输入“2020/5/5”时,会弹出警报。 And, Logger.log(str);//output: 43956 .而且, Logger.log(str);//output: 43956

Try doing your onEdit() like this:尝试像这样进行 onEdit() :

Use e.range.getValue() instead of e.value.使用 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]');
}

I've noticed a difference between e.value and e.range.getValue() sometimes.我有时注意到 e.value 和 e.range.getValue() 之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查用户是否空闲并切换标签页? 通过脚本(谷歌电子表格 - GAS) - How to check if the user is idle and switch tabs? via script (Google Spreadsheet - GAS) (GAS) Email 基于单元格中输入日期的电子表格范围 - (GAS) Email Spreadsheet range based on date input in the cell 如何基于单个单元格编辑(Google Script / GAS)通过电子邮件发送电子表格中的整行数据 - How to send whole row of data in my spreadsheet via email based on single cell edit (Google Script / GAS) 将数据推送到不同的元素,具体取决于使用 GAS 的谷歌电子表格中的列? - Push data to different elements, depending on column in google spreadsheet with GAS? Google App Script GAS-如何获得 <input type=“file”> 从表格 - Google App Script GAS - How can I get an <input type=“file”> from a form 如何检查 input type=&quot;date&quot; 是否有值 - How to check if input type=“date” has value 在有效输入上更改输入类型日期的颜色 - Changing color of input type date on valid input 通过GAS将数字符号(#)插入Google电子表格 - Insertion of number sign (#) into a Google Spreadsheet via GAS GAS:从电子表格数据填充下拉列表 - GAS: Populating a dropdown list from Spreadsheet data 使用日期选择器输入Google Apps脚本的电子表格日期 - Google Apps Script for Spreadsheet date input using date picker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM