使用Java将事件另存为ICalendar

编程入门 行业动态 更新时间:2024-10-26 23:38:25
本文介绍了使用Java将事件另存为ICalendar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好,大家知道如何将活动保存为Google日历或ICal吗?场景是我有活动的开始和结束日期以及标题.如果我单击按钮,它应该保存或通过Google日历打开浏览器并保存该事件.我尝试了 jquery.icalendar ,但仅Jquery V1.1.1运气不好.现在,我正在使用jQuery v3.2.1,你们当中的每个人都对如何做到这一点有所了解,如果可能的话,不要使用jquery.icalendar.

Hi guys anyone of you know how to save an event as google calendar or ICal? Scenario is I have the start and end date and the title of the event. If I click on the button it should save or open a browser via google calendar and save that event. I tried the jquery.icalendar but no luck its only for Jquery V1.1.1. Right now I'm using jQuery v3.2.1 anyone of you have an idea on how to do this without using the jquery.icalendar if possible as its outdated already.

想要实现类似于这个我现在正在使用它,但是没有这种插件,有没有办法做我自己的代码

Would like to achieve similar to this I'm using it right now but is there a way to do my own code without this plugin

推荐答案

这似乎是一个有趣的挑战,所以我花了很多时间看自己能做什么.我强烈建议您使用库或PHP,以便正确保留所有格式,例如.

This seemed like an interesting challenge, so I spend much of the day seeing what I could do. I would strongly suggest using a library or PHP so that all formats and such as retained properly.

这是您可以尝试的一种方法:

Here is one way you might try:

示例代码: jsfiddle/Twisty/6sye1f75/

JavaScript

var eventData = { "title": "ebay live auction test", "desc": "this is a live auction test \n testing new line.", "location": "my house", "url": "www.ebay", "time": { "start": "March 12, 2014 14:00:00", "end": "march 13, 2014 15:30:00", "zone": "-07:00", "allday": false }, }; $(function() { function adjustToUTC(dateObj, zone) { var dateOut = new Date(dateObj), hours, mins; if (isNaN(dateOut.getTime())) { return new Date(); } // adjust to UTC hours = zone.substring(1, 3); mins = zone.substring(4, 6); if (zone.substring(0, 1) === '-') { dateOut.setHours(dateOut.getHours() + (hours - 0)); dateOut.setMinutes(dateOut.getMinutes() + (mins - 0)); } else { dateOut.setHours(dateOut.getHours() - hours); dateOut.setMinutes(dateOut.getMinutes() - mins); } return dateOut; } function getDatePart(part, digits) { part = part.toString(); while (part.length < digits) { part = '0' + part; } return part; } function getUTCTime(dateObj) { var newDateObj = adjustToUTC(dateObj, eventData.time.zone); return getDatePart(newDateObj.getFullYear(), 4) + getDatePart(newDateObj.getMonth() + 1, 2) + getDatePart(newDateObj.getDate(), 2) + 'T' + getDatePart(newDateObj.getHours(), 2) + getDatePart(newDateObj.getMinutes(), 2) + getDatePart(newDateObj.getSeconds(), 2) + 'Z'; } function prepareEvent(data) { var tData = ""; tData += "BEGIN:VCALANDER\r\n"; tData += "VERSION:2.0\r\n"; tData += "METHOD:PUBLISH\r\n"; tData += "BEGIN:VEVENT\r\n"; tData += "SUMMARY:" + data.title + "\r\n"; tData += "DESCRIPTION:" + data.desc + "\r\n"; tData += "LOCATION:" + data.location + "\r\n"; tData += "URL:" + data.url + "\r\n"; tData += "UID:00" + Math.floor(Math.random() * 10000000) + "-Custom@test\r\n"; tData += "SEQUENCE:0\r\n"; tData += "DTSTAMP:" + getUTCTime(data.time.start) + "\r\n"; tData += "DTSTART:" + getUTCTime(data.time.start) + "\r\n"; tData += "DTEND:" + getUTCTime(data.time.end) + "\r\n"; tData += "END:VEVENT\r\n"; tData += "END:VCALENDAR\r\n"; return tData; } $(".show-event.ics pre").html(prepareEvent(eventData)); $.each(eventData, function(k, v) { var item = $("<li>"); if (k !== "time") { item.append($("<label>").html(k + ":")); item.append($("<span>").html(v)); } else { item.append($("<label>").html(k + ":")); item.append($("<ul>")); item.find("ul").append($("<li>").append($("<label>").html("start:")).append($("<span>").html(v.start + " (" + v.zone + ")"))); item.find("ul").append($("<li>").append($("<label>").html("end:")).append($("<span>").html(v.end + " (" + v.zone + ")"))); } $(".show-event.html ul").append(item); }); $(".controlgroup").controlgroup(); $("#save-event").click(function(e) { var contents = prepareEvent(eventData); var name = eventData.title.replace(/ /g, "_") + ".ics"; $(this)[0].download = name; $(this).attr("href", "data:text/calendar;" + encodeURIComponent(content)); console.log($(this)); return true; }); });

引用:

  • 在内存中创建文件供用户下载,而不是通过服务器下载
  • 如何创建动态文件+可以下载Java链接吗?
  • Create a file in memory for user to download, not through server
  • How to create a dynamic file + link for download in Javascript?
  • 我还从这里收获了一些功能: jQuery AddCalEvent插件演示.

    I also harvested a few of the functions out of here: jQuery AddCalEvent Plugin Demos.

    由于它的工作方式,因此无法在jsfiddle中进行测试.我可以将其移至plnkr或codepen,以查看其是否可以正常工作.

    This is not been able to be tested in jsfiddle due to the way it works. I may move this to a plnkr or codepen to see if it works there.

    更多推荐

    使用Java将事件另存为ICalendar

    本文发布于:2023-10-26 11:43:33,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1530035.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:另存为   事件   Java   ICalendar

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!