如果 ELSE 基于今天的日期,则将行附加到每月工作表

IF ELSE based on Todays date append rows to monthly Sheet

本文关键字:工作 于今天 ELSE 今天 日期 如果      更新时间:2023-09-26

我正在尝试为我的电子表格创建一个Google Apps脚本。它应该运行一个函数,并根据今天的日期将结果返回到电子表格中的特定工作表。当只是将行附加到活动工作表而不是特定工作表时,它可以正常工作。此外,代码在脚本编辑器中保存得很好,所以我假设没有重大错误。也许我在 Javascript 中布局错误,或者在我不理解的代码中存在根本错误。我倾向于我的 IF ELSE 或我如何设置活动电子表格然后工作表,但不确定。任何帮助将不胜感激。以下是代码基础知识:

function CheckDateInsertData() {
   function infoDate(){ this gets today's date}
 if (infoDate() >= 2016/01/01 && infoDate() <= 2016/01/31) {
   function GetData1{
      //this retrieves necessary data, working when not using IF ELSE statements and only single active sheet, without calling sheet by name.
      var Data =.......... ;
      appendData(Data);
      function appendData(Data){
         var sheet = SpreadsheetApp.getActiveSpreadsheet();
         sheet.setActiveSheet(sheet.getSheetByName("January"));
         sheet.appendRow(['Date', 'Price', 'Location']);
         }
    }
} else if (infoDate() >= 2016/02/01 && infoDate() <= 2016/02/29){
    function GetData2{
       var Data = ........;
       appendData(Data);
       function appendData(Data){
          var sheet = SpreadsheetApp.getActiveSpreadsheet();
          sheet.setActiveSheet(sheet.getSheetByName("February"));
          sheet.appendRow(['Date', 'Price', 'Location']);
          }
    }
}
}

这就是我将如何布置代码:

function CheckDateInsertData() {
  var todaysDate = infoDate();
  var startDate = new Date('2016/01/01');
  var endDate = new Date('2016/01/01');
  var startDateTwo = new Date('2016/02/01');
  var endDateTwo = new Date('2016/02/01');
  if (todaysDate >= startDate && todaysDate <= endDate) {
    GetData('1'); //Pass '1' as the argument
  } else if (todaysDate >= startDateTwo && todaysDate <= endDateTwo) {
    GetData('2');
  };
};
  function appendData(Data, oneOrTwo){
    var sheetToGet = oneOrTwo === '1'?"January":"February";//If, then, else
    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    sheet.setActiveSheet(sheet.getSheetByName(sheetToGet));
    sheet.appendRow(['Date', 'Price', 'Location']);
  };
  function GetData(oneOrTwo) {//this retrieves necessary data
    var Data;
    if (oneOrTwo === '1') {
      Data = ['one'];
      appendData(Data, oneOrTwo);
    } else {
      Data = ['two'];
      appendData(Data, oneOrTwo);
    };
  };
  function infoDate() {
    return new Date();// this gets today's date
  };

我不知道这是否会解决您的问题,但希望它能给您一些想法。