简体   繁体   English

React Native:如何解析ics / ical文件

[英]React Native: How to parse an ics / ical file

What is the best way to parse an ical / ics file? 解析ical / ics文件的最佳方法是什么? I have it all in a string, but I cant find a package or library that is compatible with RN. 我将其全部放在一个字符串中,但找不到与RN兼容的软件包或库。 All the general JS or Node ones throw errors about the FS package missing. 所有常规的JS或Node抛出有关FS包丢失的错误。 I am reading it in from a URL and have it all in memory so I don't even need file system access. 我正在从URL中读取它,并将其全部存储在内存中,所以我什至不需要文件系统访问。

I was not able to find any such module. 我找不到任何这样的模块。 Here is the basic code I used to solve this issue: 这是我用来解决此问题的基本代码:

var request = new XMLHttpRequest();
    request.open(
      "GET",
      "https://calendar.google.com/calendar/ical/en.usa%23holiday%40group.v.calendar.google.com/public/basic.ics",
      true
    );
    request.send(null);
    request.onreadystatechange = function() {
      if (request.readyState === 4 && request.status === 200) {
        var type = request.getResponseHeader("Content-Type");
        if (type.indexOf("text") !== 1) {
          var lines = request.responseText.split("\n");
          var events = {}
          var events_i = 0;
          for (i = 0; i < lines.length; i++) {
            if (lines[i].includes('DTSTART')) {
              var date = lines[i].split(":");
              events[events_i] = {date: date[1]};
            }
            else if (lines[i].includes('SUMMARY')) {
              var title = lines[i].split(":");
              events[events_i]["title"] = title[1];
            }
            else if (lines[i].includes('END:VEVENT')) {
              events_i++;
            }
          }
          console.log(events);
        }
      }
    };

Anyone looking for the answer to this question in 2019+, I've written an npm module to solve this problem! 任何想要在2019年以上找到该问题的答案的人,我已经编写了一个npm模块来解决此问题! You can find it here ! 你可以在这里找到它! ( https://github.com/Christop406/ical-parser ). https://github.com/Christop406/ical-parser )。

Or, if you're lazy, just run npm i cal-parser ( NOT ical-parser ). 或者,如果您很懒,只需运行npm i cal-parser不是 ical-parser )。

You can use it like this: 您可以像这样使用它:

const ical = require('cal-parser');

var parsedCal = ical.parseString(MY_ICAL_STRING);

console.log(parsedCal.calendarData); // for calendar metadata
console.log(parsedCal.events);       // for calendar events

Happy parsing! 解析愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM