我如何在下周的日期自动打开工作表?

时间:2019-11-21 13:45:45

标签: google-apps-script google-sheets google-apps-script-editor

我使用了另一篇文章中的代码(非常感谢,请参阅下文),使表格在今天的日期打开。

请,您能建议我需要在哪里添加“ +7”(或其他名称)以使其在从现在起一周的某个日期(今天+ 7天)打开吗?

How do I get my google spreadsheet to open to today's date?

function onOpen() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getActiveSheet();
 var range = sheet.getRange("1:1");

 var values = range.getValues(); 
 values = values[0];
 var day = 24*3600*1000;  
 var today = parseInt((new Date().setHours(0,0,0,0))/day);  
 Logger.log(today);
 var ssdate; 
 for (var i=0; i<values.length; i++) {
   try {
     ssdate = values[i].getTime()/day;
   }
   catch(e) {
   }
   if (ssdate && Math.floor(ssdate) == today) {
     sheet.setActiveRange(range.offset(0,i,1,1));
     break;
   }    
 }

}

1 个答案:

答案 0 :(得分:0)

尝试一下:

function onOpen() {
 var ss=SpreadsheetApp.getActive();
 var sheet=ss.getActiveSheet();
 var range=sheet.getRange("1:1");
 var values=range.getValues()[0]; 
 var today=new Date();
 var nextweek=new Date(today.getFullYear(),today.getMonth(),today.getDate()+7); 
 for (var i=0;i<values.length;i++) {
   var ssdate = values[i].getTime()/86400000;   
   if (ssdate && Math.floor(ssdate) == nextweek) {
     sheet.setActiveRange(range.offset(0,i,1,1));
     break;
   }    
 }
}