Create a new sheet with a name based on the next month

Viewed 3608

I have a script which creates a new sheet with a name based on month-year, like Oct-2017, automatically every new month.

But now I'd like to have a button, where I can create new sheets with the same structure, with names based on the MMM-yyyy.

My first script creates a new sheet only when the month changes, but now my intention is to be able to create it when I judge necessary by pressing that button. How to create a button is not a problem.

So, if before the sheet name was based on the following code,

function checkSheetName() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = formatDate(); // load the current 'Month-Year'

  try {
    ss.setActiveSheet(ss.getSheetByName(sheetName)); // try to set 'sheetName' as active sheet
  } catch (e) { // if returns error,
    createNewMonthSheet(); // creates a new sheet
  }

function createNewMonthSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = formatDate(); // load the current 'Month-Year'  
  ss.insertSheet(sheetName, 2); // creates a new sheet on the left side, after 2 existing sheets
}

function formatDate() {
  var monthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
  var monthNumber = Utilities.formatDate(new Date(), 'GMT+12:00', "M");
  var yearNumber = Utilities.formatDate(new Date(), 'GMT+12:00', "yyyy");
  return monthNames[monthNumber-1]+'-'+yearNumber; 
}

now the code needs to use the Sheet Name as a reference for the new name, increasing the date and respecting a real calendar, so if the current sheet name is Dec-2017, the button will create a new sheet called Jan-2018. So no need anymore to be related to the new Date command like my previous code.

I started a new code, but I don't know how to create the var nameOfNextMonth:

function createNewSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var currentMonth = ss.getActiveSheet().getName();
  var nameOfNextMonth = **[currentMonth+1]**;
  ss.setActiveSheet(ss.getSheetByName("template"));
  var newSheet = ss.duplicateActiveSheet();
  newSheet.activate();
  ss.moveActiveSheet(0);
  newSheet.setName(nameOfNextMonth);
}

Is that making any sense? Could anyone give me any idea how to proceed?

2 Answers

You can get next Months name by using standard new Date(), it will handle incrementing to the appropriate year and moding to the appropriate month.

Also, make sure to check if the worksheet by that name already exists or not

function createWorksheet()
{
  var monthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

  var monthsToAdd = 1;
  var currentDate = new Date();
  currentDate.setMonth(currentDate.getMonth() + monthsToAdd);

  var sheetName = monthNames[currentDate.getMonth()]+"-"+currentDate.getFullYear();
  var sheetsArray = spreadsheet.getSheets();
  var creationFlag = false;
  //Logger.log(sheetsArray)
  for(var itr in sheetsArray)
  {
    if(sheetsArray[itr].getSheetName() == sheetName)
    {
      creationFlag = false;
      break;
    }
    else
      creationFlag = true;
  }

  if(creationFlag)
    spreadsheet.insertSheet(sheetName);

  if(!creationFlag)
    Logger.log("Worksheet Exists");
}//createWorksheet

I didn't include any error handling (so make sure the active sheet has a name with the format mmm-yyyy), but see if this works

function createNewSheet() {
var monthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var curMonth = ss.getActiveSheet().getName().split("-")
var nextMonth = new Date(curMonth[1], monthNames.indexOf(curMonth[0])+1, 1);
var name = Utilities.formatDate(nextMonth, 'GMT+12:00', "MMM-yyyy")
ss.setActiveSheet(ss.getSheetByName("template"));
if(!ss.getSheetByName(name)){    
ss.duplicateActiveSheet().activate().setName(name)
ss.moveActiveSheet(0);
}
}
Related