Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Google Sheets Apps Script, how to check if a sheet exists, and if it doesn't add one

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:

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.

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.

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.

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.

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).

  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();
    }
}

If anyone has any ideas why this isn't working, I'd love to know. Thanks!

like image 468
SorryI8YourPi Avatar asked Sep 02 '25 05:09

SorryI8YourPi


1 Answers

The function getSheetByName returns null when it doesn't exist a sheet with that name. If you implement the code that way I think It will be easier.

Code would be something like this:

var sheet2 = ss.getSheetByName(A2);
if( ss.getSheetByName(A2) == null)
{
  //if returned null means the sheet doesn't exist, so create it
  var val = Utilities.formatDate(new Date(A2), "GMT+1", "MM/dd/yyyy");
  ss.insertSheet(val, ss.getSheets().length, {template: templateSheet});
  sheet2 = ss.insertSheet(A2);
}
//Rest of the behavior...
like image 116
Jonatas Grandini Avatar answered Sep 04 '25 17:09

Jonatas Grandini