title: In Google Sheets Apps Script, how to check if a sheet exists, and if it doesn't add one
body: <p>I am creating a spreadsheet to sort events based on the date inputted. I have successfully gotten the program to add a new sheet with the contents of the event that I want to copy over, but I have not been able to add to the script to allow for two options. My goal is for it to go something like this:</p>
  
  <p>Option 1: The sheet already exists
  . A2:D2 cells will be copied from the template sheet to the sheet that is titled the same date as is in cell A2.
  . A2:D2 will be cleared from the template sheet as to leave a blank canvas.</p>
  
  <p>Option 2: The sheet does not exist
  . Using the template sheet as a template for the new sheet, all the information will be copied over, with the date in A2 as the name of this new sheet.
  . A2:D2 will be cleared from the template sheet as to leave a blank canvas.</p>
  
  <p>I have tried to use a try catch statement, trying to find the sheet, and if that doesn't work, the catch creating the sheet with the new name. I get the error "A sheet with the name "____" already exists." though I am confused as to why, since I am receiving this error, it is not going on with the catch.</p>
  
  <p>I have also tried an if else statement with a similar idea, stating that if getSheetByName didn't work, it would create a sheet, or else it would set a new Active Sheet. This one has the same error.</p>
  
  <p>As for my code, I will show it, but I have to warn it's a bit chunky (for whatever reason, it won't acknowledge the variables within the try catch statement unless I restate them).</p>
  
  <pre><code>  var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];
    ss.setActiveSheet(ss.getSheets()[0])
    var range = sheet.getRange("A2");
    var A2 = range.getValue();
    var templateSheet = ss.getActiveSheet();  
    var value = sheet.getRange("A2").getValue();
    var val = Utilities.formatDate(new Date(A2), "GMT+1", "MM/dd/yyyy");
    range.setValue(new Date(A2)).setNumberFormat("MM/dd/yyyy");
    A2 = SpreadsheetApp.getActiveSheet().getRange('A2').getValue();
     try {
          ss.setActiveSheet(ss.getSheetByName(A2));
          var sheet1 = ss.getSheetByName("GUI");
          var sheet2 = ss.getSheetByName(A2);
          sheet1.getRange("A2:D2").copyTo(sheet2.getRange(sheet2.getLastRow()+1,1,1,4), {contentsOnly:true});
          ss.setActiveSheet(ss.getSheets()[0]);
          sheet.getRange("A2:D2").clearContent();
      } catch (e) {
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var sheet = ss.getSheets()[0];
          var A2 = range.getValue();
          var templateSheet = ss.getActiveSheet();    
          var val = Utilities.formatDate(new Date(A2), "GMT+1", "MM/dd/yyyy");
          ss.insertSheet(val, ss.getSheets().length, {template: templateSheet});
          ss.setActiveSheet(ss.getSheets()[0]);
          sheet.getRange("A2:D2").clearContent();
      }
  }
  </code></pre>
  
  <p>If anyone has any ideas why this isn't working, I'd love to know. Thanks!</p>