简体   繁体   中英

google app scripts - Cant get hide sheets after a month to function

i am trying to make a script in google app scripts that would allow me to copy a template into a new sheet, append a date in the file as well as the title and auto hide sheets older than a month. Here is my code so far and everything works except the auto hide of sheets(with the exception of the active main sheet). Also, im not a professional coder, the way that i am checking against an older month probably isnt the best code, if you have any advice sharing would be appreciated!

If its easier, i would also be ok with if it just auto hid every other sheet except the main sheet when the script is run.

function createNewStandupSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet()

  //Sets date
  var options = { day: '2-digit', month: 'short', year: 'numeric' };
  var todaysDate = new Date().toLocaleDateString("en-GB", options);
  var data = Utilities.formatDate(new Date(), "GMT", "'Week'w");

  //set month variable
  
  var today = new Date();
  var m = today.getMonth(); 

  //copy template as a new sheet
  var newSheet = ss.getSheetByName('DAILY STANDUP TEMPLATE').copyTo(ss);

  //change the new sheet's name
  SpreadsheetApp.flush();
  newSheet.setName(data + "-" + todaysDate);
  var sheetname = newSheet.getName();

  //Set line one value to date
  var cell = newSheet.getRange("C1:G1");
  cell.setValue(todaysDate);

//check if month is the same as current month, if its less, hide the other sheets. Then change the value to the current month after checking.
  var monthcell = newSheet.getRange("A50:B50");
  if (monthcell < m){
    hideAllSheetsExcept(sheetname)
  }
  monthcell.setValue(m)

function hideAllSheetsExcept(sheetName) {
  var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();

  for(var i =0;i<sheets.length;i++){
    Logger.log(i);
    if(sheets[i].getName()!=sheetName){
      sheets[i].hideSheet();
    }
  }
}

Insert New Sheet

function createNewStandupSheet() {
  const ss = SpreadsheetApp.getActive();
  const todaysDate = new Date().toLocaleDateString("en-GB", { day: '2-digit', month: 'short', year: 'numeric' });
  const data = Utilities.formatDate(new Date(), "GMT", "'Week'w");
  const today = new Date();
  const m = today.getMonth();
  const nsh = ss.insertSheet(0,{template:ss.getSheetByName('DAILY STANDUP TEMPLATE')});
  nsh.setName(data + "-" + todaysDate);
  const sheetname = nsh.getName();
  nsh.getRange("C1").setValue(todaysDate);
  let monthcell = nsh.getRange("A50");
  if (monthcell < m) {
    ss.getSheets().filter(sh => sh.getName() == sheetname).forEach(sh => sh.hideSheet());
  }
  monthcell.setValue(m);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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