I think this would work faster because I tested both codes:
function sortSheets() {
const ss = SpreadsheetApp.getActive();
const allSheets = ss.getSheets();
const sheetsAr = allSheets.map(sh=>[sh,sh.getName()]);
sheetsAr.sort((a,b) => a[1].charCodeAt(0)-b[1].charCodeAt(0));
sheetsAr.forEach((v,i)=>{
ss.setActiveSheet(v[0]);
ss.moveActiveSheet(i+1);
});
}
You will gain some performance because I got rid of the for
loop.
If this does not work, I am afraid you have to use a time-driven trigger which can run from 6 minutes (if you have a consumer account) or 30 minutes (if you have a business account). You can set up the time driven trigger to run every 1 minute or longer so you can have a sort of "live" adjustments in the orders of the sheet.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…