[英]Delete cells in range if less than current date
I have a range of dates in plain text format (must stay this way) and I need to clear them at 1am if the date in each range cell is less than the current date. 我有一个纯文本格式的日期范围(必须保持这种方式),如果每个范围单元格中的日期都小于当前日期,则需要在凌晨1点清除它们。 I can set up the run schedule later, but can anyone tell me why this isn't working? 我可以稍后设置运行时间表,但是有人可以告诉我为什么它不起作用吗?
I'm using =TEXT(TODAY(),"m/dd") to insert the current date in the correct format in cell AE3. 我正在使用= TEXT(TODAY(),“ m / dd”)以正确的格式在单元AE3中插入当前日期。
Thank you! 谢谢!
function clearOldDate()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Loads");
var cells = sheet.getRange('AC9:AE33').getValues();
var data = [cells];
var date = sheet.getRange('AE3').getValues();
//var Sdate = Utilities.formatDate(date,'GMT+0900','MM/dd');
for(i=0; i<76; i++)
{
if (date > data[i])
{
data[i].clear();
};
};
};
This should do the trick 这应该可以解决问题
function clearOldDate()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("LogSheet");
//getValues gives a 2D array.
var data = sheet.getRange('AC9:AE33').getValues();
var date = sheet.getRange('AE3').getValues();
//var Sdate = Utilities.formatDate(date,'GMT+0900','MM/dd');
//This for loop with loop through each row
for(i=0; i<data.length; i++)
{
//This for loop with loop through each column
for (var j = 0; j < data[i].length ; j ++){
//This assumes Column AC has the dates you are comparing aganist
if (date > data[i][j])
{
//sets that the Row with index i to empty
data[i] = ["","",""]
};
}
};
//finally set the value back into sheet
sheet.getRange('AC9:AE33').setValues(data)
};
Basically, you need to setValues once data array is modified. 基本上,修改数据数组后,您需要设置setValues。
Note: This Statement (date > data[i][0]) doesn't work as expected when the values are stored as text. 注意:当值以文本形式存储时,此语句(日期>数据[i] [0])无法按预期工作。
Edit: Modified the code and added a new for loop to go through each column, based on comments 编辑:修改了代码,并基于注释添加了新的for循环以遍历每一列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.