为什么这些纸张没有正确排序?

时间:2017-10-27 02:41:19

标签: javascript google-apps-script

因此,基于ADAM的工作表分拣程序(下面链接),我稍微修改了一下,以便按订单类型和日期来获取工作表名称。例如:生日订单的“B:10/23/2017”或月末报告的“月末:10:2017”。

https://productforums.google.com/forum/#!topic/docs/eP7kHR6L7ZI/discussion

    //function to sort tabs by date
function sortTabs() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();

  var ignored = 5;      //last 5 sheets to be ignored

  var sheetDateMatrix = [];
  var sheetDateRow = [];

  var MONTH_END = 'Month End:';
  var monthArray = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']     //Easy conversion of 0-11 format to 1-12 format

  var tabOrder = []   //Final array of tab names in order

  //Loops through all sheets seperates order types from date. Sends to matrix.
  for (var i = 0; i < sheets.length - ignored; i++) {

    sheetDateRow = sheets[i].getName().split(" ");

    if(sheetDateRow.length <= 2) {
      sheetDateRow[1] = new Date(sheetDateRow[1])
      sheetDateMatrix.push(sheetDateRow);
    }
    else {                       
      sheetDateRow.shift();
      sheetDateRow[0] = MONTH_END;
      sheetDateRow[1] = getLastDayOfMonth(sheetDateRow[1]);

      sheetDateMatrix.push(sheetDateRow);   
    }
  }

  //sorts matrix by date
  sheetDateMatrix.sort(function(a, b) {
    return b[1] - a[1];
  });

  //Reforms dates into correct format. Rejoins order type with date. Sets spreadsheet tabs' indices following the sheetDateMatrix' order
  for(var j = 0; j < sheets.length - ignored; j++ ) {
    if(sheetDateMatrix[j][0] == MONTH_END) {
      sheetDateMatrix[j][1] = monthArray[sheetDateMatrix[j][1].getMonth()] + '/' + sheetDateMatrix[j][1].getFullYear();
    }
    else {
      sheetDateMatrix[j][1] = monthArray[sheetDateMatrix[j][1].getMonth()] + '/' + ('0' + sheetDateMatrix[j][1].getDate()).slice(-2) + '/' + sheetDateMatrix[j][1].getFullYear();
    }

    tabOrder.push(sheetDateMatrix[j].join(' '));

    ss.setActiveSheet(ss.getSheetByName(tabOrder[j]));
    ss.moveActiveSheet(j + 1);
  }
  return tabOrder;
}

tabOrder数组列出了按日期正确排序的数组。

  

[17-10-26 22:24:12:411 EDT] [O:10/21/2017,B:10/21/2017,B:10/14/2017,B:10/07/2017 ,B:10:01/2017,月末:09:2017,B:09/23/2017,B:09/16/2017,B:09/09/2017,B: 2017年2月9日,B:08/19/2017,B:08/11/2017,B:08/02/2017]

问题在于我的电子表格中标签的实际顺序与数组不匹配。

如果我目前有19个标签。模板将忽略最后5个选项卡。表格09/02/2017和09/09/17交换。

如果你们中的任何一个人能帮助我解决这个小小的头痛,那就很好奇。我打算自动填充月末报告,这会让事情变得棘手。

2 个答案:

答案 0 :(得分:0)

排序标签

  

我只是对sort函数做了一些小改动,我注意到在月末:表中,月份必须是两位数。之后,它似乎找到并排序最左边的日期和左边的最旧日期,并将忽略表单独留在右边。

以下是代码:

function sortTabs() 
{
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sheets=ss.getSheets();
  var ignored = 1;      //last 5 sheets to be ignored
  var sheetDateMatrix = [];//each row is String,Date
  var sheetDateRow = [];
  var MONTH_END = 'Month End:';
  var monthArray = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']     //Easy conversion of 0-11 format to 1-12 format
  var tabOrder = []   //Final array of tab names in order
  //Loops through all sheets separates order types from date. Sends to matrix.
  for (var i=0;i<sheets.length-ignored;i++) 
  {
    sheetDateRow=sheets[i].getName().split(" ");//B: 10/23/2017 or MonthEnd: 10/23/2017
    if(sheetDateRow.length <= 2) 
    {
      sheetDateRow[1] = new Date(sheetDateRow[1])
      sheetDateMatrix.push(sheetDateRow);
    }
    else 
    {                       
      sheetDateRow.shift();
      sheetDateRow[0] = MONTH_END;
      sheetDateRow[1] = getLastDayOfMonth(sheetDateRow[1]);
      sheetDateMatrix.push(sheetDateRow);   
    }
  }
  sheetDateMatrix.sort(function(a, b) 
  {
    return b[1].valueOf() - a[1].valueOf();//I changed this line to make sure were working with numbers.
  });

  //Reforms dates into correct format. Rejoins order type with date. Sets spreadsheet tabs' indices following the sheetDateMatrix' order
  for(var j = 0; j < sheets.length - ignored; j++ ) 
  {
    if(sheetDateMatrix[j][0] == MONTH_END) 
    {
      sheetDateMatrix[j][1] = monthArray[sheetDateMatrix[j][1].getMonth()] + '/' + sheetDateMatrix[j][1].getFullYear();
    }
    else 
    {
      sheetDateMatrix[j][1] = monthArray[sheetDateMatrix[j][1].getMonth()] + '/' + ('0' + sheetDateMatrix[j][1].getDate()).slice(-2) + '/' + sheetDateMatrix[j][1].getFullYear();
    }
    tabOrder.push(sheetDateMatrix[j].join(' '));
    ss.setActiveSheet(ss.getSheetByName(tabOrder[j]));
    ss.moveActiveSheet(j + 1);
  }
  return tabOrder;
}

function getLastDayOfMonth(s) 
{
  var myA=s.split('/');
  return new Date( (new Date(myA[1], myA[0],1))-1 );
}

我从here获得了getLastDayOfMonth()函数。

最终标签排序顺序

enter image description here

答案 1 :(得分:0)

数组按顺序显示日期。但是在运行sortTab函数之后,它会混合一些标签。

Tabs after sort