如何将Google表格单元格中的日期与今天进行比较? – 应用脚本

huangapple go评论75阅读模式
英文:

How can I compare a date in a Google Sheets cell to today? - Apps Script

问题

I am very new to Apps Script, so forgive me if this is super simple, but here's a rundown of what I'm trying to do: (please keep in mind I've tried so many different ways to do this, and have been reading and trying this for a week lol)

I have a sheet with an "end date" column (G:G). (MMDDYYY) The next column over is the "status" column (H:H) with a dropdown, and one of the options is "Update Status."

I'm trying to get the "status" column to say "Update Status" when the end date is in the past.

Unfortunately, I can't link the sheet because it has confidential information, but here's a screenshot of the columns: (https://i.stack.imgur.com/xW1o9.png)

Here's the latest script I've tried:

function updateStatus()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet(); //get the spreadsheet
  var sheet = ss.getSheetByName('February 2023'); //get the Feb sheet
  var rangeG = sheet.getRange('G:G'); // get the end date row
  var endDates = rangeG.getValues(); // get the values
  var status = sheet.getRange('H:H');
  var day = 24*3600*1000
  var today = parseInt((new Date().setHours(0,0,0,0))/day); // get date today
  
  for (var i = 0; i < endDates.length; i++) { // repeat loop
    var dataday = parseInt(endDates[i][0].getTime()/day)
    
      if (dataday < today){
        status.setValue('Update Status');
    }
  }
}

Literally nothing happens. For this one, I referenced: https://stackoverflow.com/questions/14350671/how-do-i-compare-dates-with-a-spreadsheet-using-google-apps-script

英文:

I am very new to Apps Script, so forgive me if this is super simple, but here's a rundown of what I'm trying to do:
(please keep in mind I've tried so many different ways to do this, and have been reading and trying this for a week lol)

I have a sheet with an "end date" column (G:G). (MMDDYYY)
The next column over is the "status" column (H:H) with a dropdown, and one of the options is "Update Status."

I'm trying to get the "status" column to say "Update Status" when the end date is in the past.

Unfortunately, I can't link the sheet because it has confidential information, but here's a screenshot of the columns:
(https://i.stack.imgur.com/xW1o9.png)

Here's the latest script I've tried:

function updateStatus()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet(); //get the spreadsheet
  var sheet = ss.getSheetByName(&#39;February 2023&#39;); //get the Feb sheet
  var rangeG = sheet.getRange(&#39;G:G&#39;); // get the end date row
  var endDates = rangeG.getValues(); // get the values
  var status = sheet.getRange(&#39;H:H&#39;);
  var day = 24*3600*1000
  var today = parseInt((new Date().setHours(0,0,0,0))/day); // get date today
  
  for (var i = 0; i &lt; endDates.length; i++) { // repeat loop
    var dataday = parseInt(endDates[i][0].getTime()/day)
    
      if (dataday &lt; today){
        status.setValue(&#39;Update Status&#39;);
    }
  }
}

Literally nothing happens. For this one, I referenced: https://stackoverflow.com/questions/14350671/how-do-i-compare-dates-with-a-spreadsheet-using-google-apps-script

答案1

得分: 2

请看:

function updateStatus() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName('February 2023');
  const rg = sh.getRange('G1:G' + sh.getLastRow());
  const ds = rg.getValues().flat();
  const status = sh.getRange('H1:H' + sh.getLastRow()).getDisplayValues();
  const dt = new Date();
  const today = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()).valueOf();
  ds.forEach((e, i) => {
    if (new Date(e).valueOf() < today) {
      sh.getRange(i + 1, 8).setValue('Update Status');
    }
  });
}

这是提供的JavaScript代码的翻译。

英文:

Try this:

function updateStatus() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName(&#39;February 2023&#39;);
  const rg = sh.getRange(&#39;G1:G&#39; + sh.getLastRow());
  const ds = rg.getValues().flat();
  const status = sh.getRange(&#39;H1:H&#39; + sh.getLastRow()).getDisplayValues();
  const dt = new Date();
  const today = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()).valueOf();
  ds.forEach((e, i) =&gt; {
    if (new Date(e).valueOf() &lt; today) {
      sh.getRange(i + 1, 8).setValue(&#39;Update Status&#39;);
    }
  });
}

答案2

得分: 1

从您提供的示例图像来看,第1行似乎是标题行。并且在您的脚本中,值是从第1行检索的。因此,我认为第1行的错误发生在endDates[i][0].getTime()这一行。但是,您说“实际上什么都没有发生”。所以,我担心您可能已经误复制了您的脚本。

因此,在这个答案中,我想提供一个实现您目标的示例脚本。示例脚本如下。

function updateStatus() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('February 2023');
  var range = sheet.getRange('G2:H' + sheet.getLastRow());
  var now = new Date();
  now.setHours(0, 0, 0, 0);
  var today = now.getTime();
  var values = range.getValues().map(([g, h]) => [g.getTime() < today ? 'Update Status' : h]);
  range.offset(0, 1, values.length, 1).setValues(values);
}
  • 运行此脚本时,将从“G”和“H”列检索值,并检查“G”列的日期。当日期早于今天时,在“H”列中放入“Update Status”。当日期不早于今天时,保留“H”列的当前值。

  • 如果要在日期不早于今天时删除值,请将g.getTime() < today ? 'Update Status' : h修改为g.getTime() < today ? 'Update Status' : null

参考链接:

英文:

From your provided sample image, it seems that the 1st row is a header row. And, in your script, the values are retrieved from the 1st row. By this, I think that an error occurs at endDates[i][0].getTime() for the 1st row. But, you say Literally nothing happens.. So, I'm worried that you might have miscopied your script.

So, in this answer, I would like to propose a sample script for achieving your goal. The sample script is as follows.

Sample script:

function updateStatus() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(&#39;February 2023&#39;);
  var range = sheet.getRange(&#39;G2:H&#39; + sheet.getLastRow());
  var now = new Date();
  now.setHours(0, 0, 0, 0);
  var today = now.getTime();
  var values = range.getValues().map(([g, h]) =&gt; [g.getTime() &lt; today ? &#39;Update Status&#39; : h]);
  range.offset(0, 1, values.length, 1).setValues(values);
}
  • When this script is run, the values are retrieved from the columns "G" and "H", and check the date of column "G". When the date is the past from today, "Update Status" is put to the column "H". And, when the date is not the past from today, the current value of column "H" is left.

  • If you want to remove the value when the date is not the past from today, please modify g.getTime() &lt; today ? &#39;Update Status&#39; : h to g.getTime() &lt; today ? &#39;Update Status&#39; : null.

Reference:

答案3

得分: 0

尝试:

function updateStatus() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); //获取电子表格
  var sheet = ss.getSheetByName('February 2023'); //获取2月份工作表
  var rangeG = sheet.getRange('G2:G' + sheet.getLastRow()); // 获取截止日期行
  var endDates = rangeG.getValues(); // 获取数值
  var day = 24 * 3600 * 1000;
  var today = parseInt((new Date().setHours(0, 0, 0, 0)) / day);

  for (var i = 0; i < endDates.length; i++) { // 重复循环
    var dataday = parseInt(endDates[i][0].getTime() / day);
    if (dataday < today) {
      sheet.getRange(i + 2, 8).setValue('更新状态');
    }
  }
}

结果:

使用您的代码,您可以进行以下更改:

从:

var rangeG = sheet.getRange('G:G'); // 获取截止日期行

到:

var rangeG = sheet.getRange('G2:G' + sheet.getLastRow()); // 获取截止日期行

这将消除错误:TypeError: endDates[i][0].getTime is not a function。您之所以会收到此错误是因为包括了标题,而标题不是日期,所以将其调整为从第2行开始,这是实际日期。

从:

status.setValue('更新状态');

这将更改整个"status"(第H列)为更新状态

到:

sheet.getRange(i + 2, 8).setValue('更新状态');

i的值是行号。您从0开始,因此需要加2以从第2行开始。

英文:

Try:

function updateStatus() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); //get the spreadsheet
  var sheet = ss.getSheetByName(&#39;February 2023&#39;); //get the Feb sheet
  var rangeG = sheet.getRange(&#39;G2:G&#39; + sheet.getLastRow()); // get the end date row
  var endDates = rangeG.getValues(); // get the values
  var day = 24 * 3600 * 1000
  var today = parseInt((new Date().setHours(0, 0, 0, 0)) / day)

  for (var i = 0; i &lt; endDates.length; i++) { // repeat loop
    var dataday = parseInt(endDates[i][0].getTime() / day)
    if (dataday &lt; today) {
      sheet.getRange(i + 2, 8).setValue(&#39;Update Status&#39;);
    }
  }
}

Result:

如何将Google表格单元格中的日期与今天进行比较? – 应用脚本


Using your code you may change the following:

From:

var rangeG = sheet.getRange(&#39;G:G&#39;); // get the end date row

To:

var rangeG = sheet.getRange(&#39;G2:G&#39; + sheet.getLastRow()); // get the end date row

This will get rid of the error: TypeError: endDates[i][0].getTime is not a function. You are getting this error because you are including the header which is not a date, so adjust this to start on row 2 which is the actual date.

And

From:

status.setValue(&#39;Update Status&#39;);

This will change the whole "status" (Column H) to Update Status.
To:

sheet.getRange(i + 2, 8).setValue(&#39;Update Status&#39;);

The value i is the row number. You started with 0 so you add 2 to start on row 2.

huangapple
  • 本文由 发表于 2023年2月14日 07:30:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定