事件的错误结束日期

问题描述 投票:0回答:1

我已经在Google工作表中实现了一个脚本,该脚本可以获取gcal日历信息,并将其显示在工作表的各个标签中。但是,工作表中显示的每个事件的结束日期比日历本身显示的日期晚一天。下面的代码。通过日志记录,我无法弄清楚它为什么会发生。与日期格式有关吗?任何帮助,将不胜感激!


function populateAllTabs() {
  var id = "[MY CAL ID HERE]";   // id is currently set to bookings calendar. NB: Takes a string!
  var cal = CalendarApp.getCalendarById(id);
  var startPeriod = new Date('January 1, 2020');
  startPeriod.setHours(0, 0, 0, 0);
  var endPeriod = new Date(startPeriod);
  endPeriod.setDate(endPeriod.getDate() + 365); // looks for all events in the range one year
  var ss = SpreadsheetApp.getActive();
  for(var n in ss.getSheets()){// loop over all tabs in the spreadsheet
    var sheet = ss.getSheets()[n];// look at every sheet in spreadsheet
    var name = sheet.getName();//get name

    if(name != 'List'){ 

        var gig = sheet.getRange(1,1);
        var gigName = gig.getValue();
        var events = cal.getEvents(startPeriod, endPeriod, {search:gigName});


        // find the title of each event in the calendar
        var eventTitles = [];
         for (var i = 0; i < events.length; i++) { 
           eventTitles.push([events[i].getTitle()]);    
         }

        // find the start date of each event in the calendar
       var starttime = [];
         for (var i = 0; i < events.length; i++) { 
           starttime.push([Utilities.formatDate(events[i].getStartTime(), "GMT", "MM/dd/yy")]);    
         }

        // find the end date of each event in the calendar
       var endtime = [];
         for (var i = 0; i < events.length; i++) { 
           endtime.push([Utilities.formatDate(events[i].getEndTime(), "GMT", "MM/dd/yy")]);    
         }

       var cell = sheet.getRange("B3");

       cell.setValue(starttime + ' - ' + endtime);
    }
  }
}
google-apps-script google-sheets google-calendar-api
1个回答
1
投票

这对我有用。日期没问题

function populateAllTabs() {
  var cal = CalendarApp.getCalendarById('id');
  var startyear=2019;
  var startPeriod = new Date(startyear,0,1);
  var endPeriod = new Date(startyear+1,1,1);
  var ss=SpreadsheetApp.getActive();
  var shts=ss.getSheets();
  for(var n=0;n<shts.length;n++){
    var sheet=shts[n];
    var name=sheet.getName();
    if(name!='List'){ 
      var gigName = sheet.getRange(1,1).getValue();
      var ev=cal.getEvents(startPeriod, endPeriod, {search:gigName});
      var gigs=[]
      for (var i=0;i< ev.length;i++) { 
        gigs.push([ev[i].getTitle(),Utilities.formatDate(new Date(ev[i].getStartTime()),Session.getScriptTimeZone(),"MM/dd/yy"),Utilities.formatDate(new Date(ev[i].getEndTime()),Session.getScriptTimeZone(),"MM/dd/yy")]);    
      }
      if(gigs) {
        sheet.getRange(3,2,gigs.length,gigs[0].length).setValues(gigs);
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.