al-codaio / events-from-gmail

Create Google Calendar events from sending yourself a Gmail on a mobile device using Google Apps Script.
12 stars 2 forks source link

Not Sure what went wrong (TypeError: Cannot read properties of undefined (reading 'indexOf')) #8

Open HarveyCraft opened 5 months ago

HarveyCraft commented 5 months ago

I haven't touched coding since now but I followed this tutorial on how to scrape emails from gmail and make them calendar events to a tee and it spat out this error message, idk what to do

Error
TypeError: Cannot read properties of undefined (reading 'indexOf') calcDateTime @ Code.gs:88 parseEmail @ Code.gs:38 getEmail @ Code.gs:23

EDIT: I'm pretty sure it's having trouble figuring out stuff with the date but that's the extent of my guessing....

al-codaio commented 5 months ago

@HarveyCraft Hi, can you give me an example of the email you are sending? The format should look like what I wrote here.

HarveyCraft commented 5 months ago

It's for a business and it's for making bookings through the website that get sent to gmail into google calendar entries automatically.

The email looks something like this in the subject line. Booking Mail - ID 14411 - (Store: [Business name] ) 19-04-2024 at 08:30 am for example.

And then the email would have the price breakdown for the services offered image_2024-04-18_132652037

and after that the contact info of who booked, our contact info and a breakdown of the services that will be performed.

HarveyCraft commented 5 months ago

Also it's not written with CCs or anything like that, it just filters emails from one inbound email address to another email address which worked fine on gmail's end when I made labels.

I just left the time format as ROW because that's how the email formats it and just changed the Gmail Label field GMAILLABEL ' ' with the label I made, ran it and it gave me the errors. I did the gmail filtering just fine idk...

al-codaio commented 5 months ago

Yeah I think that email subject won't work because it has multiple dashes and my script only accounts for one dash (for adding multi-day events). Didn't plan for edge cases like this.

HarveyCraft commented 5 months ago

What if I change it to slashes, is there any way to add a line of code to make an exception for multiple dashes?

al-codaio commented 5 months ago

You could try slashes, not sure if it will work. Probably could expand this script further to account for multiple dashes. If there were more features do you think you would pay for a service like this?

HarveyCraft commented 5 months ago

If there was more features I'd consider it, I haven't seen it work yet if you do make a fix for this let me know and I'll try it out.

bonelifer commented 5 months ago

Not sure if I'm understanding correctly, but changed the multi day delimiter to "|" using chatGPT, possibly this may help. :

function calcDateTime(rawDate, rawTime, isAllDay) {
  // Splitting the rawDate by pipe character to handle multi-day events
  var pipeModifierIndex = rawDate.indexOf("|");
  var isMultiDay = pipeModifierIndex != -1;

  // Get time if event is not an all-day event
  if (!isAllDay && !isMultiDay) {
    var [month, day, year] = parseDate(rawDate);
    var [hours, minutes] = convertTime12to24(rawTime);
    var newDateStart = new Date(year, month - 1, day, hours, minutes);
    var newDateEnd = new Date(newDateStart);
    newDateEnd.setMinutes(newDateStart.getMinutes() + DEFAULT_EVENT_TIME);
  }  else {
    // Set start and end date if event is multi-day
    if (isMultiDay) {
      var startDate = rawDate.substring(0, pipeModifierIndex);
      var endDate = rawDate.substring(pipeModifierIndex + 1);
      var [startMonth, startDay, startYear] = parseDate(startDate);
      var [endMonth, endDay, endYear] = parseDate(endDate); 
      var newDateStart = new Date(startYear, startMonth - 1, startDay);
      var newDateEnd = new Date(endYear, endMonth - 1, endDay);
      isAllDay = true;
    } else {
      var [month, day, year] = parseDate(rawDate);
      var newDateStart = newDateEnd = new Date(year, month - 1, day);
    }    
  }
  return [newDateStart, newDateEnd, isAllDay];
}
al-codaio commented 5 months ago

Thanks for suggesting this change @bonelifer !

bonelifer commented 5 months ago

Needs tested, as I'm not a programmer, even in my dreams in my living room. :)

al-codaio commented 5 months ago

Makes two of us!