Làm việc với Lịch Gmail
Thêm, Chỉnh sửa và Xóa một Lịch
Aspose.Email cho phép các ứng dụng quản lý các lịch Gmail bằng cách sử dụng IGmailClient cung cấp các tính năng như thêm, xóa và cập nhật các lịch Gmail. Lớp khách hàng này trả về danh sách các đối tượng kiểu ExtendedCalendar chứa thông tin về các mục lịch Gmail. IGmailClient lớp này công khai các hàm sau cho lịch:
- createCalendar Để chèn lịch mới
- listCalendars
Lấy danh sách tất cả các lịch của một khách hàng
- deleteCalendar Nó có thể được dùng để xóa một lịch
- fetchCalendar Nó có thể được dùng để lấy lịch cụ thể của một khách hàng
- updateCalendar Hàm này được dùng để chèn lại một lịch đã chỉnh sửa của khách hàng
Để truy cập các lịch, GoogleTestUser được khởi tạo bằng thông tin đăng nhập tài khoản Gmail. GoogleOAuthHelper được dùng để lấy token truy cập cho người dùng, sau đó được dùng để khởi tạo IGmailClient.
Chèn, Lấy và Cập nhật
Để chèn một lịch, khởi tạo một Calendar đối tượng kiểu và chèn nó bằng cách sử dụng createCalendar hàm. createCalendar trả về id của lịch mới được chèn. Id này có thể được dùng để lấy lịch từ máy chủ. Đoạn mã sau cho bạn thấy cách chèn, lấy và cập nhật lịch.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Insert, get and update calendar
Calendar calendar = new Calendar("Summary", "Description", "Location", "America/Los_Angeles");
// Insert calendar and Retrieve same calendar using id
String id = client.createCalendar(calendar);
Calendar cal = client.fetchCalendar(id);
// Change information in the fetched calendar and Update calendar
cal.setDescription("New Description");
cal.setLocation("New Location");
client.updateCalendar(cal);
}
Xóa Lịch Cụ Thể
Để xóa một lịch cụ thể, chúng ta cần lấy danh sách tất cả các lịch của một khách hàng và sau đó xóa theo yêu cầu. listCalendars trả về danh sách của ExtendedCalendar chứa các lịch Gmail. Đoạn mã sau cho bạn thấy cách xóa một lịch cụ thể.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Access and delete calendar with summary starting from "Calendar summary"
String summary = "Calendar summary";
// Get calendars list
ExtendedCalendar[] lst = client.listCalendars();
for (ExtendedCalendar extCal : lst) {
// Delete selected calendars
if (extCal.getSummary().startsWith(summary))
client.deleteCalendar(extCal.getId());
}
}
Làm việc với Kiểm soát Truy cập Lịch
Aspose.Email cung cấp kiểm soát đầy đủ đối với quyền truy cập vào các mục lịch. listAccessRules hàm được công khai bởi IGmailClient trả về danh sách của AccessControlRule. Thông tin quy tắc cá nhân có thể được lấy, chỉnh sửa và lưu lại cho lịch của một khách hàng. IGmailClient chứa các hàm sau để quản lý các quy tắc kiểm soát truy cập.
- listAccessRules Hàm này cung cấp danh sách của AccessControlRule
- createAccessRule Hàm này tạo một quy tắc truy cập mới cho lịch.
- updateAccessRule Hàm này được dùng để cập nhật một quy tắc truy cập.
- fetchAccessRule Nó có thể được dùng để lấy quy tắc truy cập cụ thể cho lịch của một khách hàng
- deleteAccessRule Hàm này được dùng để xóa một quy tắc truy cập.
Đoạn mã sau cho bạn thấy cách các hàm được sử dụng để quản lý các quy tắc truy cập:
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Retrieve list of calendars for the current client
ExtendedCalendar[] calendarList = client.listCalendars();
// Get first calendar id and retrieve list of AccessControlRule for the first calendar
String calendarId = calendarList[0].getId();
AccessControlRule[] roles1 = client.listAccessRules(calendarId);
// Create a local access control rule and Set rule properties
AccessControlRule rule = new AccessControlRule();
rule.setRole(AccessRole.reader);
rule.setScope(new AclScope(AclScopeType.user, email2));
// Insert new rule for the calendar. It returns the newly created rule
AccessControlRule createdRule = client.createAccessRule(calendarId, rule);
// Get list of rules
AccessControlRule[] roles2 = client.listAccessRules(calendarId);
// Current list length should be 1 more than the earlier one
if (roles1.length + 1 == roles2.length) {
System.out.println("List lengths are ok");
} else {
System.out.println("List lengths are not ok");
return;
}
// Change rule and Update the rule for the selected calendar
createdRule.setRole(AccessRole.writer);
AccessControlRule updatedRule = client.updateAccessRule(calendarId, createdRule);
// Retrieve individaul rule against a calendar
AccessControlRule fetchedRule = client.fetchAccessRule(calendarId, createdRule.getId());
// Delete particular rule against a given calendar and Retrieve the all rules list for the same calendar
client.deleteAccessRule(calendarId, createdRule.getId());
AccessControlRule[] roles3 = client.listAccessRules(calendarId);
// Check that current rules list length should be equal to the original list length before adding and deleting the rule
if (roles1.length == roles3.length) {
System.out.println("List lengths are same");
} else {
System.out.println("List lengths are not equal");
return;
}
}
Làm việc với Cài đặt Khách hàng và Thông tin Màu
Aspose.Email hỗ trợ truy cập cài đặt Khách hàng bằng cách sử dụng IGmailClient.getSettings. Nó trả về danh sách các cài đặt như dưới đây:
- dateFieldOrder
- displayAllTimezones
- hideInvitations
- format24HourTime
- defaultCalendarMode
- defaultEventLength
- locale
- remindOnRespondedEventsOnly
- alternateCalendar
- userLocation
- hideWeekends
- showDeclinedEvents
- weekStart
- weather
- customCalendarMode
- timezoneLabel
- timezone
- useKeyboardShortcuts
- quốc gia
Tương tự, thông tin màu cho khách hàng cũng có thể được lấy bằng cách sử dụng IGmailClient.getColors. Đối tượng thông tin màu này trả về danh sách các màu tiền cảnh, màu nền và ngày giờ cập nhật.
Truy cập Cài đặt Khách hàng
Đoạn mã sau cho bạn thấy cách các hàm được sử dụng để truy cập cài đặt của khách hàng:
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Retrieve client settings
Dictionary<String, String> settings = client.getSettings();
if (settings.size() < 1) {
System.out.println("No settings are available.");
return;
}
// Traverse the settings list
for (KeyValuePair<String, String> pair : settings) {
// Get the setting value and test if settings are ok
String value = client.getSetting(pair.getKey());
if (pair.getValue().equals(value)) {
System.out.println("Key = " + pair.getKey() + ", Value = " + pair.getValue());
} else {
System.out.println("Settings could not be retrieved");
}
}
}
Truy cập Thông tin Màu
Đoạn mã sau cho bạn thấy cách các hàm được sử dụng để truy cập cài đặt màu của khách hàng.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
ColorsInfo colors = client.getColors();
Dictionary<String, Colors> palettes = colors.getCalendar();
// Traverse the settings list
for (KeyValuePair<String, Colors> pair : palettes) {
System.out.println("Key = " + pair.getKey() + ", Color = " + pair.getValue());
}
System.out.println("Update Date = " + colors.getUpdated());
}
Làm việc với Cuộc hẹn
Aspose.Email cung cấp các tính năng để làm việc với Cuộc hẹn trong lịch Google. Dưới đây là danh sách các tác vụ có thể thực hiện trên cuộc hẹn trong lịch Google:
- Thêm cuộc hẹn - createAppointment, importAppointment
- Lấy danh sách các cuộc hẹn - listAppointments
- Lấy thông tin cuộc hẹn cụ thể - fetchAppointment, listAppointmentInstances
- Cập nhật một cuộc hẹn - updateAppointment
- Di chuyển cuộc hẹn từ lịch này sang lịch khác - moveAppointment
- Xóa cuộc hẹn - deleteAppointment
Thêm Cuộc hẹn
Đoạn mẫu mã sau đây trình bày tính năng thêm cuộc hẹn vào lịch. Trong mẫu này các bước sau được thực hiện:
- Tạo và chèn một lịch.
- Lấy danh sách các cuộc hẹn từ một lịch mới.
- Tạo một cuộc hẹn.
- Chèn cuộc hẹn.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Create local calendar
Calendar calendar1 = new Calendar("Summary", null, null, "Europe/Kiev");
// Insert calendar and get id of inserted calendar and Get back calendar using an id
String id = client.createCalendar(calendar1);
Calendar cal1 = client.fetchCalendar(id);
String calendarId1 = cal1.getId();
try {
// Retrieve list of appointments from the first calendar
Appointment[] appointments = client.listAppointments(calendarId1);
if (appointments.length > 0) {
System.out.println("Wrong number of appointments");
return;
}
// Get current time and Calculate time after an hour from now
java.util.Calendar c = java.util.Calendar.getInstance();
Date startDate = c.getTime();
c.add(java.util.Calendar.HOUR_OF_DAY, 1);
Date endDate = c.getTime();
// Initialize a mail address collection and set attendees mail address
MailAddressCollection attendees = new MailAddressCollection();
attendees.add("User1.EMail@domain.com");
attendees.add("User3.EMail@domain.com");
// Create an appointment with above attendees
Appointment app1 = new Appointment("Location", startDate, endDate, MailAddress.to_MailAddress(email2), attendees);
// Set appointment summary, description, start/end time zone
app1.setSummary("New Summary");
app1.setDescription("New Description");
app1.setStartTimeZone("Europe/Kiev");
app1.setEndTimeZone("Europe/Kiev");
// Insert appointment in the first calendar inserted above and get back inserted appointment
Appointment app2 = client.createAppointment(calendarId1, app1);
// Retrieve appointment using unique id
Appointment app3 = client.fetchAppointment(calendarId1, app2.getUniqueId());
} catch (Exception ex) {
System.err.println(ex);
}
}
Lấy và Cập nhật Cuộc hẹn
Ở đây việc lấy và cập nhật lịch được minh họa như sau:
- Lấy cuộc hẹn cụ thể.
- Sửa đổi cuộc hẹn.
- Cập nhật cuộc hẹn trong lịch.
Giả sử rằng một lịch có id "calendarId" và cuộc hẹn có ID duy nhất "AppointmentUniqueId" đã được trích xuất. Đoạn mã sau đây cho bạn thấy cách lấy và cập nhật cuộc hẹn.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
String calendarId = client.listCalendars()[0].getId();
String AppointmentUniqueId = client.listAppointments(calendarId)[0].getUniqueId();
// Retrieve Appointment
Appointment app3 = client.fetchAppointment(calendarId, AppointmentUniqueId);
// Change the appointment information
app3.setSummary("New Summary");
app3.setDescription("New Description");
app3.setLocation("New Location");
app3.setFlags(AppointmentFlags.AllDayEvent);
java.util.Calendar c = java.util.Calendar.getInstance();
c.add(java.util.Calendar.HOUR_OF_DAY, 2);
app3.setStartDate(c.getTime());
c.add(java.util.Calendar.HOUR_OF_DAY, 1);
app3.setEndDate(c.getTime());
app3.setStartTimeZone("Europe/Kiev");
app3.setEndTimeZone("Europe/Kiev");
// Update the appointment and get back updated appointment
Appointment app4 = client.updateAppointment(calendarId, app3);
}
Di chuyển và Xóa Cuộc hẹn
Appointment có thể được di chuyển bằng cách cung cấp lịch nguồn, lịch đích và ID duy nhất của cuộc hẹn trong lịch nguồn. Đoạn mã sau đây cho bạn thấy cách di chuyển và xóa cuộc hẹn.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
String SourceCalendarId = client.listCalendars()[0].getId();
String DestinationCalendarId = client.listCalendars()[1].getId();
String TargetAppUniqueId = client.listAppointments(SourceCalendarId)[0].getUniqueId();
// Retrieve the list of appointments in the destination calendar before moving the appointment
Appointment[] appointments = client.listAppointments(DestinationCalendarId);
System.out.println("Before moving count = " + appointments.length);
Appointment Movedapp = client.moveAppointment(SourceCalendarId, DestinationCalendarId, TargetAppUniqueId);
// Retrieve the list of appointments in the destination calendar after moving the appointment
appointments = client.listAppointments(DestinationCalendarId);
System.out.println("After moving count = " + appointments.length);
// Delete particular appointment from a calendar using unique id
client.deleteAppointment(DestinationCalendarId, Movedapp.getUniqueId());
// Retrieve the list of appointments. It should be one less than the earlier appointments in the destination calendar
appointments = client.listAppointments(DestinationCalendarId);
System.out.println("After deleting count = " + appointments.length);
}