Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google脚本仅在某些日期添加带有日期的行_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript Google脚本仅在某些日期添加带有日期的行

Javascript Google脚本仅在某些日期添加带有日期的行,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我正在尝试制作一个Google工作表,自动在第一列中添加新行,并在相应日期前两天(星期六添加星期一,星期天添加星期二,等等)添加工作日日期。为此,我编写了以下函数: function addRowsWithDate() { var ss = SpreadsheetApp.getActiveSheet(); var Today = new Date(); Today.setDate(Today.getDate()+0); // I am using this to experim

我正在尝试制作一个Google工作表,自动在第一列中添加新行,并在相应日期前两天(星期六添加星期一,星期天添加星期二,等等)添加工作日日期。为此,我编写了以下函数:

function addRowsWithDate() {
  var ss = SpreadsheetApp.getActiveSheet();
  
  var Today = new Date();
  Today.setDate(Today.getDate()+0); // I am using this to experiment with different dates
  var newDate = new Date();
  var dayOfWeek = Today.getDay();
  
  if (dayOfWeek<=3 || dayOfWeek==6){ // On Saturday through Wednesday
    newDate.setDate(Today.getDate()+2);
    ss.insertRowsBefore(1, 1); // Add new rows
    ss.getRange(1, 1).setValue(newDate); // Set date to the day after tomorrow 
  }
}
函数addRowsWithDate(){
var ss=SpreadsheetApp.getActiveSheet();
var Today=新日期();
Today.setDate(Today.getDate()+0);//我用它来试验不同的日期
var newDate=新日期();
var dayOfWeek=Today.getDay();

if(dayOfWeek不是为
newDate
创建一个
new Date()
,而是将
newDate
定义为
new Date(Today)
,然后在if循环中添加2天

解决方案:

function addRowsWithDate() {
  var ss = SpreadsheetApp.getActiveSheet();
  
  var Today = new Date();
  Today.setDate(Today.getDate()+3); // I am using this to experiment with different dates
  var newDate = new Date(Today);
  var dayOfWeek = Today.getDay();
  
  
  if (dayOfWeek<=3 || dayOfWeek==6){ // On Saturday through Wednesday
    newDate.setDate(newDate.getDate()+2);
    ss.insertRowsBefore(1, 1); // Add new rows
    ss.getRange(1, 1).setValue(newDate); // Set date to the day after tomorrow 
    ss.getRange(1, 2).setValue(Today); 
  }
}
函数addRowsWithDate(){
var ss=SpreadsheetApp.getActiveSheet();
var Today=新日期();
Today.setDate(Today.getDate()+3);//我用它来试验不同的日期
var newDate=新日期(今天);
var dayOfWeek=Today.getDay();

if(dayOfWeekTry使用getDay()函数获取日期,然后使用if()进行处理)函数相应地递增。@Harsh我不明白。这不是我已经在做的吗?今天是星期三。如果你加上+1,那就是星期四。后者是一周中的第四天。记住星期天是0,因此星期四是4。你的If条件将跳过+1或+2。@Marios是的,这就是我想要的。我的想法是,我可以每日取消此脚本,而不向电子表格中添加周末。问题是,如果我设置+3(假设是星期六),它将返回9月5日,而不是10月5日星期一。非常好。谢谢。当我添加
ss.getRange(1,2).setValue(今天)时
若要同时打印假装今天的日期,它将返回与
newDate
相同的日期。编辑
newDate
时,这些更改是否也应用于
今天的日期?