Google日历列表事件,输出未显示在Google表格中

时间:2019-05-21 23:08:33

标签: google-apps-script google-sheets google-calendar-api

我正在尝试生成报告以列出日历中的当前事件,但不知何故我在Google表格中看不到输出。有人可以帮我提供下面的代码吗?我一直在寻找和调整,但没有运气(脑死了的ATM)

function listevents(){

var ss, range, ui, rcalendarId, ask, askDate, askEnd,cal, start, end, events, num, dateString, eStart,sDate, sTime, string, eEnd, eDate, eTime, data;
ss = SpreadsheetApp.openById('SheetIDHERE').getSheetByName('SHEETNAMEHERE');
range = ss.getRange(1,1,1,8).setValues([ ["Date Created","Title","Description","Location","Start Date","End Date", "Id", "Creator"] ]);

ui = SpreadsheetApp.getUi();
ask = ui.prompt('Search for resource calendar list. Enter the resource calendar id');

askDate = ui.prompt('Enter the Start Date. Date must be "Month Date, Year" format (i.e. May 1, 1970)');
start = new Date(askDate.getResponseText());
askEnd = ui.prompt('Enter the End Date. Date must be "Month Date, Year" format (i.e. May 1, 1970)');

end = new Date (askEnd.getResponseText()); 
cal = CalendarApp.getCalendarById(ask.getResponseText());

events = cal.getEvents(start,end); 
num = events.length;

for (var i = 0; i < num; i ++) {

// Column: Start Date
eStart = events[i].getStartTime();
sDate = eStart.toDateString();
sTime = sDate.toTimeString();
// Column: End Date
eEnd = events[i].getEndTime();
eDate = eEnd.toDateString();
eTime = eDate.toTimeString();
ss.getRange(i+2,1,1,8).setValues[ [events[i].getDateCreated(),
                                events[i].getTitle(),
                                events[i].getDescription(),
                                events[i].getLocation(),
                                sDate + " " + sTime,
                                eDate + " " + eTime,
                                events[i].getId(),
                                events[i].getCreators()] ];

} }

我希望这些值会在Google表格中发布。

2 个答案:

答案 0 :(得分:0)

此修改如何?

修改点:

  1. 请对日期对象使用toTimeString()
  2. 在您的脚本中,需要修改setValues[ [events[i].getDateCreated(),,,中的ss.getRange(i+2,1,1,8).setValues[ [events[i].getDateCreated(),,,

当以上几点反映到您的脚本中时,它如下所示。

修改后的脚本:

从:
eStart = events[i].getStartTime();
sDate = eStart.toDateString();
sTime = sDate.toTimeString();
// Column: End Date
eEnd = events[i].getEndTime();
eDate = eEnd.toDateString();
eTime = eDate.toTimeString();
ss.getRange(i+2,1,1,8).setValues[ [events[i].getDateCreated(),
                                events[i].getTitle(),
                                events[i].getDescription(),
                                events[i].getLocation(),
                                sDate + " " + sTime,
                                eDate + " " + eTime,
                                events[i].getId(),
                                events[i].getCreators()] ];
至:
var eStart = events[i].getStartTime();
var sDate = eStart.toDateString();
var sTime = eStart.toTimeString(); // Modified
    // Column: End Date
var eEnd = events[i].getEndTime();
var eDate = eEnd.toDateString();
var eTime = eEnd.toTimeString(); // Modified
ss.getRange(i+2,1,1,8).setValues([ [events[i].getDateCreated(), // Modified
                                events[i].getTitle(),
                                events[i].getDescription(),
                                events[i].getLocation(),
                                sDate + " " + sTime,
                                eDate + " " + eTime,
                                events[i].getId(),
                                events[i].getCreators()] ]); // Modified

或者,我认为您也可以如下使用toString()

var eStart = events[i].getStartTime();
var sDate = eStart.toString(); // Modified
var eEnd = events[i].getEndTime();
var eDate = eEnd.toString(); // Modified
ss.getRange(i+2,1,1,8).setValues([ [events[i].getDateCreated(),
                                events[i].getTitle(),
                                events[i].getDescription(),
                                events[i].getLocation(),
                                sDate, // Modified
                                eDate, // Modified
                                events[i].getId(),
                                events[i].getCreators()] ]);

参考文献:

如果我误解了你的问题,我表示歉意。

答案 1 :(得分:0)

如果任何人有兴趣或可能能够帮助您检索自己的日历事件,则代码如下:

function onOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
options = [
{name:"MENU NAME1",functionName:"YOUR FUNCTION NAME HERE"}
];
ss.addMenu("CUSTOM NAME", options);
}

function calEvents(){
var ss, sheet,range, ui, rcalendarId, ask, askDate,askEnd,cal, start, end, events, num, output, startTime,startDate, startTime1, endTime, endDate, endTime1, dataRow;
ss = SpreadsheetApp.openById('GOOGLE SHEET ID HERE');
sheet = ss.getSheetByName('SHEET NAME HERE');
sheet.clear(); 
range = [];
range.push([ "Date Created","Title","Description","Location","Start Date","End Date", "Id", "Creator" ]);

//ENTER CALENDAR ID HERE
ui = SpreadsheetApp.getUi();
ask = ui.prompt('Search for resource calendar list. Enter the resource calendar id');

//ENTER START DATE HERE
askDate = ui.prompt('Enter the Start Date. Date must be "Month Date, Year" format (i.e. May 1, 1970)');
start = new Date(askDate.getResponseText());
askEnd = ui.prompt('Enter the End Date. Date must be "Month Date, Year" format (i.e. May 1, 1970)');

end = new Date (askEnd.getResponseText()); 
cal = CalendarApp.getCalendarById(ask.getResponseText());

events = cal.getEvents(start,end); 
num = events.length;

for (var i = 0; i < events.length; i++) {

// Column: Start Date
startTime = events[i].getStartTime();
startDate = startTime.toDateString();
startTime1 = startTime.toTimeString(); 
// Column: End Date
endTime = events[i].getEndTime();
endDate = endTime.toDateString();
endTime1 = endTime.toTimeString(); 

dataRow = []
dataRow.push(events[i].getDateCreated(),
    events[i].getTitle(),
    events[i].getDescription(),
    events[i].getLocation(),
    startDate + " " + startTime1,
    endDate + " " + endTime1,
    events[i].getId(),
    events[i].getCreators())

range.push(dataRow);
}
sheet.getRange(1,1,range.length, range[0].length).setValues(range);
}