I use a Google Sheets spreadsheet to manage the files that I am working on. I have roughly 60-70 files at any given time, each of which has a unique file number. Each file is represented in my spreadsheet by a unique sheet named by its file number. File numbers fit the following format: [#]-[##]-[####] (e.g. 5-18-0040). I also have a sheet named "Files" which is a list of all of those sheets and a few other hidden sheets with operations contained in them.
I have cobbled together a script that I had thought would sort those sheets by increasing file number (e.g. 1-15-0023, 2-16-1924, 3-14-2012, 5-17-0040) and then pushes the Files sheet and those hidden sheets to the start. However, it doesn't work. It definitely moves sheets around somewhat, but I still end up with sheets out of order.
Can anyone take a look at this code and tell me what could be failing? For instance, do I need to specify the names as integers (and if so, how?) or is there something else that could be preventing this script from working?
function sortSheets () {
var sheetNameArray = [];
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
sheetNameArray.push(sheets[i].getName());
}
sheetNameArray.sort();
for( var j = 0; j < sheets.length; j++ ) {
ss.setActiveSheet(ss.getSheetByName(sheetNameArray[j]));
ss.moveActiveSheet(j + 1);
}
ss.setActiveSheet(ss.getSheetByName("Files"));
ss.moveActiveSheet(1);
ss.setActiveSheet(ss.getSheetByName("Court Information"));
ss.moveActiveSheet(2);
ss.setActiveSheet(ss.getSheetByName("Timelines"));
ss.moveActiveSheet(3);
ss.setActiveSheet(ss.getSheetByName("Template"));
ss.moveActiveSheet(4);
ss.setActiveSheet(ss.getSheetByName("Sheet Operators"));
ss.moveActiveSheet(5);
}
sheetNameArrayand verified that it has the order you want?console.log(sheetNameArray);will send it to Stackdriver, where you can more easily interact with multiple logs. - tehhowch["Files", "Court Information" "Timelines", "Template", "Sheet Operators"].forEach(function (name, index) { ss.setActiveSheet(ss.getSheetByName(name)); ss.moveActiveSheet(1 + index); });- tehhowch"Court Information". There's a reason code doesn't belong in comments ;) - tehhowch