简体   繁体   English

在Google Apps脚本中使用If / Then函数进行日期验证

[英]Date Validation with If/Then Function in Google Apps Script

Thanks already to Serge insas for his insight both here and here , which have been a godsend for me already. 已经感谢Serge insas在这里这里的洞见,这已经成为我的天赐之物。 But...I'm having trouble tying everything together with date validation. 但是...我很难将所有内容与日期验证捆绑在一起。

To clarify, I have a GAS intended to verify that the date in Column A is (a) more than seven days old and (b) not null. 为了明确起见,我有一个GAS旨在验证A列中的日期是(a)超过7天,并且(b)不为空。 If both pass, the script determines the first empty row in Column G, and then pauses before completing various functions. 如果两者都通过,则脚本将确定G列中的第一个空行,然后在完成各种功能之前暂停。 The beginning of the script looks like... 脚本的开始看起来像...

function getStats() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("Main");
var TITLE_ROW = 1;
var DATE_COL = 1;
var URL_COL = 4;
var sevendaysBefore = new Date(new Date().getTime()-7*24*60*60*1000);
if (DATE_COL != ''||(DATE_COL != null || DATE_COL< sevendaysBefore)) {
var end = sheet.getLastRow();
for( var i = 1; i < end; i++) {
var Gvals = sheet.getRange("G1:G").getValues();
var Glast = Gvals.filter(String).length;
var rowNum = TITLE_ROW+Glast;
var itemurl = sheet.getRange(rowNum,URL_COL).getValues();
Utilities.sleep(500);

... ...

I've clearly implemented something wrong, though, because the date validation doesn't work—the script appears to function as though the data in Column A doesn't matter. 但是,我显然实现了一些错误,因为日期验证不起作用-该脚本似乎可以正常运行,好像A列中的数据无关紧要。 I'm sure I've done something incredibly idiotic, but I'm too ignorant to spot it on my own. 我敢肯定,我做了一些令人难以置信的愚蠢行为,但是我太无知了,无法独自发现它。 So...anyone know what I've overlooked? 所以...有人知道我忽略了什么吗?

Mistake : You are hard coding DATE_COL = 1 and you are using this in if statement. 错误:您正在硬编码DATE_COL = 1,并且在if语句中使用它。 It doesn't get the value of the cell. 它无法获取单元格的值。 Also I am not getting your statement "date in Column A is (a) more than seven days old". 另外,我没有收到您的声明“ A列中的日期(a)已超过7天”。 Is that date is from a cell or you are iterating through all the cells in column A ?. 该日期是来自某个单元格,还是您要遍历A列中的所有单元格?

Below code will satisfy your need and I tested. 下面的代码将满足您的需求,我进行了测试。 Here as example I am checking date validation for cell R1C1(A1). 作为示例,我在检查单元格R1C1(A1)的日期验证。

1)Get the date from cell. 1)从单元格获取日期。 You can change it or Iterate the cells in column for date. 您可以更改它或迭代日期列中的单元格。
2) We have date.valueOf() method which returns the number of milliseconds since midnight 1970-01-01. 2)我们有date.valueOf()方法,该方法返回自1970-01-01午夜以来的毫秒数。
3) Validation : check the cell data is date and greater than 7 days 3)验证:检查单元格数据是否为日期且大于7天

function compDate()
{

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getRange("A1"); //point1
  var date01 = new Date();
  var date02 = cell.getValue(); //point2
  var dateDiff = (date01.valueOf()-date02.valueOf())/(24*60*60*1000);

  if((isValidDate(date02)) == true && dateDiff > 7) //point3
  Logger.log("success");
}

//below function will return true if the arg is valid date and false if not.
function isValidDate(d) {
  if ( Object.prototype.toString.call(d) !== "[object Date]" )
    return false;
  return !isNaN(d.getTime());
}

While the other answer is probably working (didn't test), its approach is very different from yours. 尽管其他答案可能有效(未经测试),但其方法与您的方法大不相同。

Below is code that follows the same logic as yours but works at the array level (to follow recommendations in Best practices ). 下面的代码遵循与您相同的逻辑,但是在数组级别上有效(遵循最佳实践中的建议)。

I added a few comments to show the differences, hoping it will help you to understand how it works. 我添加了一些注释以显示差异,希望它可以帮助您了解其工作原理。

function getStats() {
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = doc.getSheetByName("Main");
  var Glast; // define the variable for later use
  var vals = sheet.getDataRange().getValues();// get all data in an array (do that before loop)
  var TITLE_ROW = 0;// use array index instead of real row numbers
  var DATE_COL = 0;// use array index instead of real column numbers
  var URL_COL = 3;// use array index instead of real column numbers
  var sevendaysBefore = new Date(new Date().getTime()-7*24*60*60*1000).getTime();// get native value in milliseconds to make comparison easier below
  for( var i = 1; i < vals.length; i++) { // start loop from Row 2 (=array index 1)
    if(vals[i][0]!='' && vals[i][0]!=null&&vals[i][0].getTime()<sevendaysBefore){continue};// use && instead of ||, we want ALL conditions to be true ( see !='' and !=null) 
    Glast = i; break ;// first occurrence of data meeting above condition (non null and date < 7 days before)
  } 
  var itemurl = vals[Glast][URL_COL];// get the value from the array
  Utilities.sleep(500);
  //...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM