title: Google Apps Script - Changing values in a range of google sheets cells
body: <p>In the following example I use Apps Script to schedule Google calendar events based on spreadsheet input. This code works just fine however I need to tweak it to do a small manipulation on the source sheet as well.
  You can see here that I filter the range to only include rows that have &quot;Pending&quot; value in column D (r[3]). However I need to include a line of code in the loop so that after the filtered rows are synced to my Google Calendar the same cell value in column D changes to &quot;Scheduled&quot; for the respective cell. I have tried following this solution but could not implement it since I am new to JS.</p>
  <p><a href="https://stackoverflow.com/questions/22496610/google-apps-script-setvalue-in-cell-based-on-for-loop-matching">Google Apps Script - .setValue in cell based on for loop matching</a></p>
  <pre><code>
  function calendarSync() {
  var ss = SpreadsheetApp.getActiveSheet();
  var calendarId = &quot;My Calendar ID&quot;;
  var eventCal = CalendarApp.getCalendarById(calendarId);
  var eventArray = ss.getRange('A2:I500').getValues().filter(r =&gt; r[3] == &quot;Pending Schedule&quot;);
  
  for (x=0; x&lt;eventArray.length; x++) {
    var event = eventArray[x];
    var eventName = event[0];
    var startTime = event[1];
    var endTime = event[2];
    
    var exisEvents = eventCal.getEvents(startTime, endTime, {search: eventName}) //prevents creating duplicate events;
    if (exisEvents.length == 0) {
    eventCal.createEvent(eventName, startTime, endTime);
    }
  }
  }
  </code></pre>