Stop Script From Duplicating Events

519 views
Skip to first unread message

Jamie Marshall

unread,
Nov 13, 2019, 10:55:50 AM11/13/19
to Google Apps Script Community

Hello All,

I have a very basic understanding of scripts and I'm looking for a bit of assistance. 

I have a script I'm using to add events from sheets to a public google calendar. The intent of the calendar is to show all approved travel to a particular site. Despite an error I get ("Cannot find method createEvent(string,string,string,object). (line 26, file "CalendarEvent") the script does seem to work (I can at least see the events on the calendar). One issue I'm having is it's creating duplicate events. 


Here's my script so far:

function CreateBPEvent() {
  var spreadsheet = SpreadsheetApp.getActiveSheet();
  var calendarId = spreadsheet.getRange('AB1').getValue();
  var eventCal = CalendarApp.getCalendarById("###");
  var lr = spreadsheet.getLastRow();
  Logger.log(lr);
  var count = spreadsheet.getRange("Z3:AC"+lr+"").getValues(); 
  for (x=0; x<count.length; x++) {
    var shift = count[x];
    var title = shift[0];
    var startTime = shift[1];
    var endTime = shift[2];
    var description = shift[3];   
    var options = {
      'description': description,
    }
    eventCal.createEvent(title, startTime, endTime, options);
  }
}

I appreciate your help!

Jorge Forero

unread,
Nov 13, 2019, 3:23:35 PM11/13/19
to google-apps-sc...@googlegroups.com
Hi Jamie,

You must include the calendarId value when you get the eventCal on the line 4:

var eventCal = CalendarApp.getCalendarById(calendarId);

I have tested the script with this change and it works... the event is created with the values from the spreadsheet.

Another thing, be sure you get the right values in the line:
var count = spreadsheet.getRange("Z3:AC"+lr+"").getValues(); 

I did change this line (only for testing purposes)
var count = spreadsheet.getRange('B' + lr + ':E'+ lr).getValues(); 

Greetings
Jorge.

Confidentiality Notice:

The information contained in the communication and any attachments may contain confidential and legally protected information that is intended only for the use of the addressee.  It is the property of Relish Labs, LLC.  Unauthorized use, disclosure, dissemination or copying of this communication is strictly prohibited and may be unlawful.  If you have received this message in error please notify the sender immediately and destroy this communication and all copies, including attachments.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f9529deb-3a79-4e96-89b5-146636740605%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages