0
votes

I'm having a terrible time getting this code to work. I'm learning a lot, but I'm very much a beginner.

Here's the script:

//push new events to calendar function pushToCalendar() { //spreadsheet variables
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(1,1,5,25);
var values = range.getValues();

//calendar variables   
var calendar = CalendarApp.getCalendarById('xxxx')

var numValues = 0;   for (var i = 0; i < values.length; i++) {     
//check to see if name and type are filled out - date is left off because length is "undefined"
if ((values[i][0].length > 0) && (values[i][2].length > 0)) {

  //check if it's been entered before          
  if (values[i][16]!= 'y') {                       

    //create event https://developers.google.com/apps-script/class_calendarapp#createEvent
    var newEventTitle = 'values[i][1]' + ' ' + values[i][15] + ' - ' + values[i][3] + ' - ' + values[i][10] + ' Driver: Needed';
    var newEvent = calendar.createAllDayEvent(newEventTitle, values[i][8]);

    //get ID
    var newEventId = newEvent.getId();

    //mark as entered, enter ID
    sheet.getRange(i+2,16).setValue('y');
    sheet.getRange(i+2,17).setValue(newEventId);

  } //could edit here with an else statement
}
numValues++;   }

And here's a link to a dummy:

https://docs.google.com/spreadsheets/d/1gBXtHjEhnNSEkJYq2upCWtwkj1ZLWiUYv6KSuMH8Nds/edit?usp=sharing

1

1 Answers

0
votes

There are few errors with code:

1.//push new events to calendar function pushToCalendar(). As you included the function statement in the comments, the whole statment was commented out.

  1. For loop should start from i=1. This is because, when you take i=0, this is referred to as the name of columns row.

  2. sheet.getRange(i+2,16) should be changed to sheet.getRange(i+1,16).

  3. In var values = range.sheet.getRange(1, 1, sheet.getLastRow(), 25).getValues();

Here the code:

 function pushToCalendar() { 
  //spreadsheet variables
 var sheet = SpreadsheetApp.getActiveSheet();
 var range = sheet.getRange(1,1,5,25);
 var values = range.getValues();

 Logger.log(values.length);

 //calendar variables   
   var calendar = CalendarApp.getCalendarById('your calendar ID')

    var numValues = 0;   

   for (var i = 1; i < values.length; i++) {     

     var check = values[i][0];
  //check to see if name and type are filled out - date is left off because length is     "undefined"
     if ((values[i][0]) && (values[i][2].length > 0)) {

     //check if it's been entered before          
     if (values[i][16]!= 'y') {                       

     //create event https://developers.google.com/apps  script/class_calendarapp#createEvent
     var newEventTitle = 'values[i][1]' + ' ' + values[i][15] + ' - ' + values[i][3] + '     - ' + values[i][10] + ' Driver: Needed';
     Logger.log(i);

     Logger.log(values[i][8]);

     var newEvent = calendar.createAllDayEvent(newEventTitle, values[i][8]);

      //get ID
     var newEventId = newEvent.getId();

     //mark as entered, enter ID
    sheet.getRange(i+1,16).setValue('y');
    sheet.getRange(i+1,17).setValue(newEventId);

} //could edit here with an else statement
}
 }
}

I tried the above code and was able to create events in my calendar.Let me know if you have any questions