How to concatenate strings to a array in a loop?

48 Views Asked by At

I have a table with food ingredients, suppliers, dates, and article numbers, and need a script to automatically create calendar events when the specifications need to be renewed (dates). I had a working script but as we have more and more ingredients by the same suppliers my calendar is getting too crowded. Some ingredients are by the same supplier and need renewing on the same date so I want to concatenate their article numbers into an array and use that as a calendar event description. I struggle to set up the loops and the array. My thinking was to check if the next supplier matches and the next date matches and if so concatenate the description into the array.

The following code would only append one description and I think I'd need another loop.

function createEvents() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("name of sheet"); 
var lastrow = sheet.getLastRow();
var range = sheet.getRange("A2:D"+lastrow);
var cal = calendarApp.getCalendarById("calId");

range.sort({column: 2, ascending: true});
var data = range.getValues()

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

var name = data[i][0] //event name
var supplier = data[i][1] //supplier
var nextsupplier = data[i+1][1]
var date = new Date (data[i][2]) //event date
var nextdate = new Date (data[i+1][2]);
var desc = data [i][3] //event description

if (supplier == nextsupplier){
     if (nextdate == date){
      append desc from i+ to i
        } else {
      create event from i with appended desc 
      }
  } else {
  var event = cal.createAllDayEvent(name, date, {description: desc});
  Logger.log('Event ID: ' + event.getTitle()); 
  }
}

}
0

There are 0 best solutions below