使用Java将事件另存为ICalendar [英] Save event as ICalendar using Javascript

查看:81
本文介绍了使用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:

示例代码: https://jsfiddle.net/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": "http://www.ebay.com",
  "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;
  });
});

引用:

  1. 在内存中创建文件供用户下载,而不是通过服务器下载
  2. 如何创建动态文件+可以下载Java链接吗?
  1. Create a file in memory for user to download, not through server
  2. How to create a dynamic file + link for download in Javascript?

我还从这里收获了一些功能:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆