title: How can I make a cycle in Google apps script?
body: <p>The goal is to connect one of my Google Sheet documents with Google Calendar and create events by the data of the sheet. There are two sheets included in the document:</p>
  <ul>
  <li>The first one is the main sheet with all the data. Most of them are not needed to create an event.</li>
  <li>The second is a copy of the important information from the first sheet using this formula: <code>=Sheet1!H$5</code> and the following 3 cells under it. The formula is copied to Sheet2, so if I update sheet1 they will appear there as well.</li>
  </ul>
  <p>The main problem is, if I trigger the code, it will update my calendar every time I edit Sheet1 and the same event is added a thousand times. The other problem is if I copy down the formula - to fill sheet2 with data automatically from sheet1, if I add a new one - for the code the last row isn't the most recent one with data in it, however it is the last one with just the formula in it, which is usually blank.</p>
  <p><strong>Sheet1:</strong></p>
  <p><a href="https://i.stack.imgur.com/V3x2t.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V3x2t.png" alt="Sheet1" /></a></p>
  <p><strong>Sheet2:</strong></p>
  <p><a href="https://i.stack.imgur.com/wirNn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wirNn.png" alt="Sheet2" /></a></p>
  <p>Here's my code:</p>
  <pre><code>function simpleSheetsToCalendar() {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();   
  var sheet = ss.getSheetByName('Sheet2');
  var data = sheet.getRange(sheet.getLastRow(),1,1,4).getValues();
  var title = data[0][0];   
  var eventDesc = data[0][1];   
  var eventLoca = data[0][2];   
  var date = data[0][3];
  var masterCal = CalendarApp.getCalendarById(‘CalendarID)
  
  masterCal.createAllDayEvent(title, date,
                      {location:eventLoca,description:eventDesc});}
  </code></pre>