简体   繁体   English

如果小于当前日期,则删除范围内的单元格

[英]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.

相关问题 如何使用jquery检查日期不能小于当前日期? - how to check date can not be less than current date using jquery? 如果日期比当前日期少 15 分钟,那么做一些事情 - If Date is Less Than Current Date by 15mins then do something VueJS,Javascript - 防止输入 slider 范围小于当前值 - VueJS, Javascript - Prevent Input slider range less than current value 如何使用angularjs检查所选日期和所选时间小于当前日期或大于当前日期和时间? - How to check selected date and selected time is less than current or greater than current date and time using angularjs? 使日期字段仅显示过去日期(小于当前日期)的方法 - Way to make date field to show only past date(less than current date) 检查当前日期是否不少于dd / MM / yyyy格式的当前日期 - To check whether current date is not less than present date in dd/MM/yyyy format 如果日期值小于当前日期,如何更改横条的背景颜色? - How can I change the background color of horizontal bar if date value is less than to current date? 如果日期小于当前日期,则禁用ui bootstrap datepicker-AngularJS ngRepeat - Disable ui bootstrap datepicker if the date is less than the current date - Angularjs ngRepeat 以 mm/dd/yyyy 为单位的日期验证正则表达式,应小于当前日期 - Regex for Date Validation in mm/dd/yyyy and should be less than current date Google Analytics(分析)-较宽的日期范围显示的访问次数少于较短的日期范围。 可能的尺寸和指标不匹配? - Google Analytics - wider date range shows less visits than a shorter date range. Possible dimensions and metrics mismatch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM