Google Apps Script getDateCreated, getStartTime, g

2019-08-27 03:16发布

问题:

Trying to write a function to export Google Calendar entries into a Google Sheet. I've created two calendar entries in the date range 2019/02/01 - 2019/03/01 with the text #outreach. The search works and finds the two events I created, and only the two events I created. However, the getStartTime, getEndTime, and getDateCreated functions all return the date/time that I run the function:

function myFunction() {

    var defaultCalendar = CalendarApp.getDefaultCalendar();
    var lower = new Date(2019, 2, 1);
    var upper = new Date(2019, 3, 1);
    var events = CalendarApp.getDefaultCalendar().getEvents(lower, upper, {search: '#outreach'});

    var startTime = Date(events[1].getStartTime());
    Logger.log('Start time: %s', startTime);

    var endTime = Date(events[1].getEndTime());
    Logger.log('End time: %s', endTime);

    var createDate = Date(events[1].getDateCreated());
    Logger.log('Create date: %s', createDate);

}

Any ideas as to why?

回答1:

Some Calendar Events to Spreadsheet

This function also launches a modeless dialog with table of the events found by the search. You'll probably need to change sheet name and search string

function myFunction() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet83');//Sheet Name
  var cal = CalendarApp.getDefaultCalendar();
  var start = new Date(2019, 0, 1);
  var end = new Date(2019, 5, 1);
  var events = cal.getEvents(start, end, {search: 'search string'});//search string
  sh.appendRow(['Title','Start','End']);
  var html='<style>th,td{border:1px solid black;}</style><table><tr><th>Title</th><th>Start Time</th><th>EndTimer</th></tr>';
  for(var i=0;i<events.length;i++) {
    html+=Utilities.formatString('<tr><td>%s</td><td>%s</td><td>%s</td></tr>', events[i].getTitle(),events[i].getStartTime(),events[i].getEndTime());
    sh.appendRow([events[i].getTitle(),events[i].getStartTime(),events[i].getEndTime()])
  }
  html+='</table>';
  var userInterface=HtmlService.createHtmlOutput(html).setWidth(1000);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Event Information for ' + cal.getName() );
}
  • Calendar.getEvents()


回答2:

Comment by https://stackoverflow.com/users/9337071/tehhowch was the solution, i.e., I needed to add the new keyword, as follows var startTime = new Date(events[1].getStartTime()); Logger.log('Start time: %s', startTime);

var endTime = new Date(events[1].getEndTime());
Logger.log('End time: %s', endTime);

var createDate = new Date(events[1].getDateCreated());
Logger.log('Create date: %s', createDate);