简体   繁体   English

Google Apps 脚本排序选项卡/工作表 - 按最新日期优先且不排序/显示隐藏工作表?

[英]Google Apps Script Sort Tabs/Sheets - by Latest Date First and don't sort/show hidden sheets?

I am trying to sort tabs by date, putting the newest date first;我正在尝试按日期对选项卡进行排序,将最新日期放在首位; and I found this code that is helpful but seems to reverse when I use it.我发现这段代码很有帮助,但在我使用它时似乎反转了。 I'm not sure how to add to the code so that it doesn't open the hidden tabs;我不知道如何添加到代码中,这样它就不会打开隐藏的选项卡; and I'd like to NOT have it sort a tab called 'Students' and leave it as the first tab.而且我不想让它对一个名为“学生”的标签进行排序,并将其保留为第一个标签。

The expected (hopeful) behavior is that it it leaves the 'Student' tab as is and sorts the remaining open tabs with the newest date first (consistently);预期的(有希望的)行为是它按原样离开“学生”选项卡,并将剩余打开的选项卡与最新日期排在最前面(一致地); and does not unhide the hidden tabs.并且不会取消隐藏隐藏的选项卡。

The actual behavior is it sorts all tabs, unhides those that are hidden - putting the latest tab first the first time I run it;实际行为是它对所有选项卡进行排序,取消隐藏那些隐藏的选项卡 - 第一次运行时将最新选项卡放在首位; then when I run it again, it reverses the order and the latest date is on the end (and it includes the 'Student' tab in the sort.然后当我再次运行它时,它会颠倒顺序,最后日期是最后(它包括排序中的“学生”选项卡。

Here's a link If you would like to see an example file.这是一个链接如果您想查看示例文件。 Thank you.谢谢你。

Sample File 示例文件

The code for the function: function 的代码:

   function sortSheets () {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var obj = sheets.map(function(s) {
    var sheetName = s.getSheetName();
    var date = 0;
    if (sheetName != "Students") {
      var ar = sheetName.split("/");
      date = new Date(ar[4], ar[2], ar[2]).getTime();
    }
    return {sheet: s, converted: date};
  });
  obj.sort(function(a, b) {return a.converted > b.converted ? 1 : -1});
  obj.forEach(function(s, i) {
    ss.setActiveSheet(s.sheet);
    ss.moveActiveSheet(i + 1);
  });
}

I believe your goal is as follows.我相信你的目标如下。

  • There are the following sheets in the Spreadsheet.电子表格中有以下表格。

     Pre-KF <--- hidden Pre-K <--- hidden KG <--- hidden Students 2022/01/19 - Suzie Q - G3 - T G3 2021/09/30 - Suzie Q - G3 - T G2 2021/05/10 - Suzie Q - G2 - T G2
  • You want to sort the sheets of 2022/01/19 - Suzie Q - G3 - T G3 , 2021/09/30 - Suzie Q - G3 - T G2 and 2021/05/10 - Suzie Q - G2 - T G2 as the date of top letter.您想将2022/01/19 - Suzie Q - G3 - T G32021/09/30 - Suzie Q - G3 - T G22021/05/10 - Suzie Q - G2 - T G2的工作表排序为首字母的日期。

  • In this case, you want to ignore the hidden sheets and "Students" sheet.在这种情况下,您要忽略隐藏的工作表和“学生”工作表。

In order to achieve your goal, how about the following modification?为了达到你的目的,下面的修改怎么样?

Modification points:修改点:

  • In your sheet name including the date is like 2022/01/19 - Suzie Q - G3 - T G3 .在包含日期的工作表名称中,例如2022/01/19 - Suzie Q - G3 - T G3 But in your script, var ar = sheetName.split("/"); date = new Date(ar[4], ar[2], ar[2]).getTime();但是在您的脚本中, var ar = sheetName.split("/"); date = new Date(ar[4], ar[2], ar[2]).getTime(); var ar = sheetName.split("/"); date = new Date(ar[4], ar[2], ar[2]).getTime(); is used for retrieving the date.用于检索日期。 In this case, new Date(ar[4], ar[2], ar[2]) returns Invalid Date .在这种情况下, new Date(ar[4], ar[2], ar[2])返回Invalid Date
  • In your script, the "Students" sheet and hidden sheets are also used for sorting the sheets.在您的脚本中,“学生”工作表和隐藏工作表也用于对工作表进行排序。

When these points are reflected in your script, it becomes as follows.当这些点反映在你的脚本中时,它变成如下。

Modified script:修改后的脚本:

function sortSheets () {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var obj = sheets.reduce(function(ar, s) {
    var sheetName = s.getSheetName();
    var hide = s.isSheetHidden();
    if (sheetName != "Students" && !hide) {
      var date = new Date(sheetName.split(" - ")[0]).getTime();
      ar.push({name: sheetName, sheet: s, converted: date, hide})
    }
    return ar;
  }, []);
  var diff = sheets.length - obj.length;
  obj.sort(function(a, b) {return a.converted < b.converted ? 1 : -1});
  obj.forEach(function(s, i) {
    ss.setActiveSheet(s.sheet);
    ss.moveActiveSheet(i + 1 + diff);
  });
}
  • In this modification, both the hidden sheets and "Students" sheet were ignored.在此修改中,隐藏的工作表和“学生”工作表都被忽略了。
  • The script for parsing the date from the sheet name was modified.修改了从工作表名称解析日期的脚本。

Note:笔记:

  • In this modified script, the sheets of top of sheet names are sorted as follows.在此修改后的脚本中,工作表名称顶部的工作表排序如下。

     Pre-KF <--- hidden Pre-K <--- hidden KG <--- hidden Students 2022/01/19 - Suzie Q - G3 - T G3 2021/09/30 - Suzie Q - G3 - T G2 2021/05/10 - Suzie Q - G2 - T G2
  • If you want to sort the following order,如果要按以下顺序排序,

     Pre-KF <--- hidden Pre-K <--- hidden KG <--- hidden Students 2021/05/10 - Suzie Q - G2 - T G2 2021/09/30 - Suzie Q - G3 - T G2 2022/01/19 - Suzie Q - G3 - T G3
    • Please modify obj.sort(function(a, b) {return a.converted < b.converted? 1: -1});请修改obj.sort(function(a, b) {return a.converted < b.converted? 1: -1}); to obj.sort(function(a, b) {return a.converted > b.converted? 1: -1}); to obj.sort(function(a, b) {return a.converted > b.converted? 1: -1}); . .

Reference:参考:

I have been harassment by St. John County The whole complete the United States and the other trying to be mean she followed me across state line including St. Johns County Using a car so it's whipping into Social Security the veterans is there an Stealing checks我一直受到圣约翰县的骚扰整个美国和另一个试图卑鄙的人她跟着我穿过 state 线,包括圣约翰县使用汽车所以它被鞭打到社会保障退伍军人那里偷支票

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

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