1

I am trying to write scirpt to add events from a spreadsheet to my google calendar. This is the script that I am using.

function addEvents() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr = ss.getLastRow();
  var cal = CalendarApp.getCalendarById("c_fe882662e583725f15fd4faa8c8fdf5124f3affe543778ecdcf0838d6eb17f26@group.calendar.google.com")

  var data = ss.getRange("A3:D"+lr).getValues();

    for(var i = 0;i<data.length;i++){ 

      cal.createEvent(data[i][2],data[i][4],data[i][5],{location: data[i][6], description: data[i][7]});

}
}

When I run the script I am getting the following error. Error Exception: Invalid argument: startTime addEvents @ Code.gs:10

This is the sheet that I am using with my dates.

https://docs.google.com/spreadsheets/d/1qG68-NLnq9LscPPzlnzRLCfHFIsN3v7V5zvWiVsG0qU/edit?usp=sharing

I want the title of the event to be column C, the start time to be Column E, the endtime to be Column F, Location G, and Description H.

James Fay
  • 13
  • 2

2 Answers2

0

Try changing this: cal.createEvent(data[i][2],data[i][4],data[i][5],{location: data[i][6], description: data[i][7]}); to this cal.createEvent(data[i][2],new Date(data[i][4]),new Date(data[i][5]),{location: data[i][6], description: data[i][7]});

Try changing this var data = ss.getRange("A3:D"+lr).getValues(); to this var data = ss.getRange("A3:H"+lr).getValues();

Try this:

function addEvents() {
  const ss = SpreadsheetApp.getActive();
  var sh = ss.getActiveSheet();
  var cal = CalendarApp.getCalendarById("jimesteban@jimesteban.com")
  var data = sh.getRange("A3:H" + sh.getLastRow()).getValues();
  for (var i = 0; i < data.length; i++) {
    cal.createEvent(data[i][2], data[i][4], data[i][5], { location: data[i][6], description: data[i][7] });
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
0

Modification points:

  • In your script, in the for loop, data[i][4],data[i][5] is used as the start and end time. And also, data[i][7] is used. But, atvar data = ss.getRange("A3:D"+lr).getValues();, 4 columns of "A" to "D" are retrieved. I thought that this might be the reason for your issue. In this case, it is required to be var data = ss.getRange("A3:H" + lr).getValues().

  • But, when I saw your Spreadsheet, the start and end times don't have the year and month. In this case, 1899 year is used. Please be careful about this. From your Spreadsheet, I guessed that you might have wanted to use the year, month, and date from column "A".

When my understanding of your current issue and your goal, how about the following modification?

Modified script:

function addEvents() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr = ss.getLastRow();
  var cal = CalendarApp.getCalendarById("c_fe882662e583725f15fd4faa8c8fdf5124f3affe543778ecdcf0838d6eb17f26@group.calendar.google.com");
  var data = ss.getRange("A3:I" + lr).getValues();
  while (data[data.length - 1][0] == '') data.pop();
  for (var i = 0; i < data.length; i++) {
    var year = data[i][8].getFullYear();
    var month = data[i][8].getMonth();
    var date = data[i][8].getDate();
    data[i][4].setFullYear(year);
    data[i][4].setMonth(month);
    data[i][4].setDate(date);
    data[i][5].setFullYear(year);
    data[i][5].setMonth(month);
    data[i][5].setDate(date);
    cal.createEvent(data[i][2], data[i][4], data[i][5], { location: data[i][6], description: data[i][7] });
  }
}
  • When this script is run, the start and end times are retrieved from the columns "E" and "F", respectively. And also, the year, month, and date are retrieved from column "A". Using these values, the start and end date are created and they are used with createEvent.

  • When you want to use other values of year, month, and date instead of column "A", please tell me.

Note:

  • From your reply of This sounds promising, the sheet that I am using is actually setting up a mail merge as well and the date in column A is for the mail merge and not for the calendar. I would actually like column I to be the date for the calendar events. , I modified the above script.

  • From your reply of If I run this script twice (or multiple times) as I will continue to add events, it seems to duplicate the events that are already added. Any idea how to eliminate that?, I updated the above script as follows.

    function addEvents() {
      var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var lr = ss.getLastRow();
      var cal = CalendarApp.getCalendarById("c_fe882662e583725f15fd4faa8c8fdf5124f3affe543778ecdcf0838d6eb17f26@group.calendar.google.com");
      var data = ss.getRange("A3:R" + lr).getValues();
      while (data[data.length - 1][0] == '') data.pop();
      var rangeList = [];
      for (var i = 0; i < data.length; i++) {
        if (data[i][17] == "created") continue;
        var year = data[i][8].getFullYear();
        var month = data[i][8].getMonth();
        var date = data[i][8].getDate();
        data[i][4].setFullYear(year);
        data[i][4].setMonth(month);
        data[i][4].setDate(date);
        data[i][5].setFullYear(year);
        data[i][5].setMonth(month);
        data[i][5].setDate(date);
        cal.createEvent(data[i][2], data[i][4], data[i][5], { location: data[i][6], description: data[i][7] });
        rangeList.push(`R${i + 3}`);
      }
      if (rangeList.length == 0) return;
      ss.getRangeList(rangeList).setValue("created");
    }
    
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This sounds promising, the sheet that I am using is actually setting up a mail merge as well and the date in column A is for the mail merge and not for the calendar. I would actually like column I to be the date for the calendar events. – James Fay Nov 03 '22 at 23:32
  • @James Fay Thank you for replying. From your reply, I updated my proposed script. Could you please confirm it? – Tanaike Nov 03 '22 at 23:36
  • This is beautiful! Exactly what I was trying to do. Thank you so much!!!!!! – James Fay Nov 03 '22 at 23:40
  • @James Fay Thank you for replying and testing it again. I'm glad your issue was resolved. Thank you, too. – Tanaike Nov 03 '22 at 23:40
  • If I run this script twice (or multiple times) as I will continue to add events, it seems to duplicate the events that are already added. Any idea how to eliminate that? – James Fay Nov 03 '22 at 23:47
  • @James Fay About your new question of `If I run this script twice (or multiple times) as I will continue to add events, it seems to duplicate the events that are already added. Any idea how to eliminate that?`, I think that it's yes. In this case, when your provided Spreadsheet is used, how about putting a value like `created` to the column "R" and checking the column "R"? – Tanaike Nov 03 '22 at 23:54
  • I understand the logic of the script checking to see if column R is true (checkbox marked) and not running the script for those events, but my skills are too limited to figure the actual script on my own. Thank you for all of your help, godsend! – James Fay Nov 03 '22 at 23:58
  • @James Fay Thank you for replying. From your reply, I added one more sample script to my answer. The added script creates the events by checking the column "R". Could you please confirm it? – Tanaike Nov 04 '22 at 00:01
  • @James Fay Thank you for replying and testing it. I'm glad your new question was resolved. Thank you, too. – Tanaike Nov 04 '22 at 00:38
  • I appreciate your help. I am now attempting to augment your script that you helped me with to complete a similar task with a different sheet that I use to organize meetings. I want column D to be the date of the event to be added and the Teacher Name to be the name of the event. I added the script in the below comments, sorry for the formatting. – James Fay Nov 04 '22 at 03:13
  • `function addEvents() { var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var lr = ss.getLastRow(); var cal = CalendarApp.getCalendarById("c_6c69891ab79d1ec6b556faa974b821e6a0c60c59e282b7a398faabbdc67e02cc@group.calendar.google.com"); var data = ss.getRange("A3:L" + lr).getValues(); while (data[data.length - 1][0] == '') data.pop(); var rangeList = []; for (var i = 0; i < data.length; i++) { if (data[i][11] == "created") continue; var year = data[i][3].getFullYear(); var month = data[i][3].getMonth(); var date = data[i][3].getDate();` – James Fay Nov 04 '22 at 03:14
  • `data[i][4].setFullYear(year); data[i][4].setMonth(month); data[i][4].setDate(date); data[i][5].setFullYear(year); data[i][5].setMonth(month); data[i][5].setDate(date); cal.createEvent(data[i][0], data[i][4], data[i][5], { location: data[i][6], description: data[i][1] }); rangeList.push(`L${i + 3}`); } if (rangeList.length == 0) return; ss.getRangeList(rangeList).setValue("created"); }` – James Fay Nov 04 '22 at 03:15