繁体   English   中英

谷歌应用程序脚本 - 一个月后无法隐藏工作表

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

我正在尝试在 google 应用程序脚本中制作一个脚本,该脚本允许我将模板复制到新工作表中,在文件中附加日期以及标题和自动隐藏超过一个月的工作表。 到目前为止,这是我的代码,除了自动隐藏工作表(活动主工作表除外)外,一切正常。 另外,我不是专业的编码员,我检查旧月份的方式可能不是最好的代码,如果您有任何建议分享将不胜感激!

如果它更容易,我也可以接受,如果它在脚本运行时自动隐藏除主工作表之外的所有其他工作表。

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

插入新工作表

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

暂无
暂无

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

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