我有我们公司的时间表电子表格,我需要按每个时间表块(15 行 x 20 列)对员工进行排序。我有以下代码,我得到了帮助,但是一旦遇到没有员工姓名的块,数组就会退出排序(我希望这些被洗牌到底部)。我遇到的另一个问题是这些单元格中有许多公式,当我按原样运行它时,它会删除它们。如果可能的话,我想保持这些完好无损。这是代码:
function sortSections()
{
var activeSheet = SpreadsheetApp.getActiveSheet();
//SETTINGS
var sheetName = activeSheet.getSheetName(); //name of sheet to be sorted
var headerRows = 53; //number of header rows
var pageHeaderRows = 5; //page totals to top of next emp section
var sortColumn = 11; //index of column to be sorted by; 1 = column A
var pageSize = 65;
var sectionSize = 15; //number of rows in each section
var col = sortColumn-1;
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
var data = sheet.getRange(headerRows+1, 1, sheet.getMaxRows()-headerRows, sheet.getLastColumn()).getValues();
var data3d = [];
var dataLength = data.length/sectionSize;
for (var i = 0; i < dataLength; i++) {
data3d[i] = data.splice(0, sectionSize);
}
data3d.sort(function(a,b){return(((a[0][col]<b[0][col])&&a[0][col])?-1:((a[0][col]>b[0][col])?1:0))});
var sortedData = [];
for (var k in data3d) {
for (var l in data3d[k]) {
sortedData.push(data3d[k][l]);
}
}
sheet.getRange(headerRows+1, 1, sortedData.length, sortedData[0].length).setValues(sortedData);