Skip to content

Instantly share code, notes, and snippets.

@duynhm
Created November 28, 2023 05:09
Show Gist options
  • Save duynhm/ccdcd418b951665815792c78a2e13fd1 to your computer and use it in GitHub Desktop.
Save duynhm/ccdcd418b951665815792c78a2e13fd1 to your computer and use it in GitHub Desktop.
Create bot for L&D include Trip bot and Send notify to email for attendees
//https://docs.google.com/spreadsheets/d/1VHaSalwgxWg7gCgQPr-lIGVsH9QebU4RO4of9d-ubTs/edit#gid=1626974869 link backend
//cần 1 hàm để tạo ra nhiều events tương ứng với từng sheet events
function createListEvent(){
//bước 1: đã có: menu danh sách các lớp - danh sách từng lớp theo từng sheet
//đọc menu lấy ra danh sách các lớp học
var sheetConf = getSheet("conf");
var countClass = getValueNotationBySheet(sheetConf,"B2");
var countReminder = getValueNotationBySheet(sheetConf,"B2");
var classList= getValuesBySheet(sheetConf,2,3,1,countClass); //lấy từ ô C2 đi tiếp theo chiều ngang
var reminderList = getValuesBySheet(sheetConf,3,3,1,countReminder); //lấy từ ô C3 đi tiếp theo chiều ngang
var overrides = [];//danh sách reminder
for(var reminder in reminderList){
overrides.push({
method:"popup",
minutes:reminder
});
}
for (var i =0; i<classList.length; i++){
//đọc thông tin lớp học
var sheet= getSheet(classList[0][i]);
var xacDinhDuocCoBaoNhieuCot =sheet.getRange("C1").getValue(); //các khung giờ học trong cùng 1 lớp học, ví dụ: ca 1: 10h - 12h ngày 8/6, ca2: 10h-12h ngày 9/6
var data = sheet.getRange(1,2,5,xacDinhDuocCoBaoNhieuCot).getValues();
var trainingCourse = data[0][0]; //code bắt đầu từ 0 cho nên trong gg sheet hàng 1 = 0 và cột A=0
var startDate= data[1];
var endDate= data[2];
var location= data[3][0];
var description= data[4][0];
Logger.log(location);
var count= 0;
var attendeeList=[];
var xacDinhDuoccoBaoNhieuHangEmail= sheet.getRange("D1").getValue();
var danhSach= sheet.getRange(7,4,xacDinhDuoccoBaoNhieuHangEmail,1).getValues();
while (count<xacDinhDuoccoBaoNhieuHangEmail){
attendeeList.push({email:danhSach[count][0]});
count++;
}
count=0;
while (count< xacDinhDuocCoBaoNhieuCot){
//count là tính bắt đầu từ hàng 1=0 và cột A =0 đếm các giá trị trong data cần dò đến khi chổ XÁc định được có bao nhiêu cột thì sẽ dừng lại
createEvent(trainingCourse,location,description,new Date(startDate[count]), new Date(endDate[count]),attendeeList, overrides);
count++;
}
}
}
//tạo ra 1 event google calendar với title địa điểm, mô tả, ngày giờ bắt đầu, ngày giờ kết thúc, danh sách mail người tham dự
function createEvent(title,loc, des, start, end, attendeeList, reminderList) {
var calendarId = 'primary';
//var start = getRelativeDate(1, 12);
//var end = getRelativeDate(1, 13);
var event = {
summary: title, //Lớp excel
location: loc,// '523 Tô Hiến Thành',
description:des,// 'Mang theo laptop đến lớp',
start: {
dateTime: start.toISOString()
},
end: {
dateTime: end.toISOString()
},
reminders:{
useDefault:false,
overrides:reminderList
},
attendees: attendeeList/*[
{email: '[email protected]'},
]*/,
// Red background. Use Calendar.Colors.get() for the full list.
colorId: 11
};
event = Calendar.Events.insert(event, calendarId);
Logger.log('Event ID: ' + event.getId());
}
/**
* Helper function to get a new Date object relative to the current date.
* @param {number} daysOffset The number of days in the future for the new date.
* @param {number} hour The hour of the day for the new date, in the time zone
* of the script.
* @return {Date} The new date.
*/
function getRelativeDate(daysOffset, hour) {
var date = new Date();
date.setDate(date.getDate() + daysOffset);
date.setHours(hour);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
return date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment