英文:
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('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
答案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('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');
}
});
}
答案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('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);
}
-
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() < today ? 'Update Status' : h
tog.getTime() < today ? 'Update Status' : 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('February 2023'); //get the Feb sheet
var rangeG = sheet.getRange('G2:G' + 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 < endDates.length; i++) { // repeat loop
var dataday = parseInt(endDates[i][0].getTime() / day)
if (dataday < today) {
sheet.getRange(i + 2, 8).setValue('Update Status');
}
}
}
Result:
Using your code you may change the following:
From:
var rangeG = sheet.getRange('G:G'); // get the end date row
To:
var rangeG = sheet.getRange('G2:G' + 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('Update Status');
This will change the whole "status" (Column H) to Update Status.
To:
sheet.getRange(i + 2, 8).setValue('Update Status');
The value i is the row number. You started with 0 so you add 2 to start on row 2.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论