简体   繁体   English

创建一个新的工作表,其名称基于下个月

[英]Create a new sheet with a name based on the next month

I have a script which creates a new sheet with a name based on month-year, like Oct-2017, automatically every new month. 我有一个脚本,该脚本会在每个新月自动基于月年份创建一个新工作表,例如2017年10月。

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. 但是现在我想要一个按钮,可以在其中创建具有相同结构,名称基于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. 现在代码需要使用工作表名称作为新名称的参考,增加日期并遵守实际日历,因此,如果当前工作表名称为Dec-2017,则按钮将创建一个名为Jan-2018的新工作表。 So no need anymore to be related to the new Date command like my previous code. 因此,不再需要像以前的代码那样与新的Date命令相关。

I started a new code, but I don't know how to create the var nameOfNextMonth : 我开始了一个新代码,但是我不知道如何创建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? 谁能告诉我如何进行?

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 我没有进行任何错误处理(因此请确保活动工作表的名称具有mmm-yyyy格式),但是请查看是否可行

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

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. 您可以使用标准的new Date()获得下个月的名称,它将处理递增到适当的年份,并修改成适当的月份。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM