Home > Back-end >  Simple Apps Script timing out on execution
Simple Apps Script timing out on execution

Time:12-05

I am only beginner level at C and I'm trying to execute what I think is a simple script in Apps Script for this spreadsheet: https://docs.google.com/spreadsheets/d/1lu0_B1OdjU6Y5KF4zomCpQNiGCGklVoZDF8rQ8HQBhQ/edit?usp=sharing The execution is timing out and it doesn't give me much information as to why but it has to be something to do with the parts of the template I have changed. Namely just the for loop in task 3. Maybe the way i have concatenated too? I am not familiar with Java and may be missing something very obvious. I'm not familiar with how it should be set out either so please be kind.

function routineActivator() {
  /** 
  Task 1) Open the Event  Calendar.
  **/
  
  var spreadsheet = SpreadsheetApp.getActiveSheet();
  var calendarId = 'myEmail';
  var eventCal = CalendarApp.getCalendarById(calendarId);
  
 /** 
  Task 2) Pull each shift information into the code, in a form that the code can understand.
 **/
 
  var routine = spreadsheet.getRange('A4:D74').getValues();
  

  /**
  Task 3) Do the work!
 **/
  
  for (x=0; x<routine.length; x  ) {
       
       
       var task = routine[x];
       var des = task[3]
       if(des=1){
         var label = task[2];
         var startTime = task[0]   task[1];
         while (des != 2){
           var endTime = task[0]   task[1];
           task  ;
          }
         eventCal.createEvent(label, startTime, endTime);
        }       
      }
     
       
       /**
       Task 4) Make it easy to use.
       **/
       
       function onOpen() {
          var ui = SpreadsheetApp.getUi();
          ui.createMenu('Sync to Calendar')
               .addItem('Schedule tasks now', 'routineActivator')
       }
}

I have tried to run it but it loops and times out.

CodePudding user response:

The problem is that the following will loop forever and never exit:

  if (des = 1) {
    while (des != 2) {
    }
  }

des will always be 1 because you are using the assignment operator = instead of the comparison operator ===.

Changing the operator is not enough to fix the problem, though, because when des is 1, it is not 2, and the while loop will never exit. You need to modify des in the while loop.

  • Related