简体   繁体   中英

looping google sheets into multiple calendar events

I've created(found and slightly edited) a script online that takes responses from a google form, sends the answers into a sheet, and then creates calendar events based on a static time stamp.

The code is working great. There is also a loop to prevent double booking that I didn't show.

The next step is to create a loop/if/and statement that will allow me to add multiple times to the form, allowing staff (I'm a teacher) to pick the period, or multiple periods when they would like to check out a piece of equipment.

I'd attach a picture of the script, but cannot b/c I am new to stackoverflow. So here is the best I can give without making the post too long.

The original...

  //For loop processes the entries one row at a time.


for (var i = 0; i < myData.length; ++i) 
      {
    var row = myData[i]; //ith row of data
    if(row[0]=="") //row[0] is the entry in the "Timestamp" column (i.e. the first, or index 0 column)
    {
      i=myData.length  //when the timestamp column is empty, you've reached the end of the data, so this jumps you out of the loop
    }
    else
    {
      var timestamp = row[0];  //timestamp of the entry (stored as a javascript Date object)
      var event_name = "Laptop - Cart 2";  // Second column (first column has timestamp), stores the name of the event as a string.
      var start_date = row[3]; // Date for the start of the event (stored as a javascript Date object)
      var startDt = new Date();// Time for the start of the event (stored as a javascript Date object)
      startDt.setHours(08);
      startDt.setMinutes(30);
      var end_date; 
      if(row[4]=="")  //if this column is blank, the end date is set to the start date, otherwise the end date is stored.
          {end_date=row[3];}
          else
          {end_date=row[4];} //if this column is blank, the end date is set to the start date, otherwise the end date is stored.  

      var end_time = new Date();
      end_time.setHours(15);
      end_time.setMinutes(30);

      var location = row[5] ;  // Location of the event stored as a string
       var description = row[7]; // Description of the event stored as a string
      var email_address = row[1]; //Email address of the user stored as a string

what I was hoping to change.. notice the change in the time stamps for row 3 and 4

  var start_time = row [3]
  var startDt = new Date();// Time for the start of the event (stored as a javascript Date object)
  startDt.setHours(08);
  startDt.setMinutes(30);    
  var end_time = new Date();
  end_time.setHours(11);
  end_time.setMinutes(30);


  var start_time = row [4]      
  var startDt = new Date();// Time for the start of the event (stored as a javascript Date object)
  startDt.setHours(11);
  startDt.setMinutes(45);    
  var end_time = new Date();
  end_time.setHours(15);
  end_time.setMinutes(30);

My problem is the script only reads the second set of values. The calendar event only creates an event using row 4 start and end time. what type of statement do I need to write to get both events on the calendar without creating a separate form for each time period available. There are 11 periods in a school day.

You are assigning as two different values for row[3] and row[4] in the two code blocks you added.

Assuming that row[3] and row[4] as time of a day(not date values), the problem in the second block of code is that after assigning row[3] value to start_time variable and overriding that value with row[4] immediately.

As per my understanding on your requirement, I see that you want to create multiple calender events for the same teacher for multiple periods. In that case if the start times of the periods are stored as values in the same row as row[3], row[4].. you should first create a calender event for row[3] first an then create another calender event for row[4] value.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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