## When to use
Use when the user runs /course-sort, asks to "re-run the course sort", or wants their semester course-schedule tabs (e.g. "2026 Fall", "2027 Spring") sorted like the "courseSort Macro". Applies to every sheet in the workbook unless the user names specific sheets.
## Expected sheet layout
- Row 7 is the header: Row# | Rubric | Title | Status | Campus | Room | Day | Time | Instructors | Instructional Format | Delivery Mode | Enrolled | Capacity (columns A:M).
- Data starts in row 8. The last data row is the last row where column B (Rubric) is not blank.
- Day values may be a single day ("Monday"), a list ("Monday, Tuesday, Wednesday, Thursday"), a pair ("Tuesday & Thursday"), or blank (online sections).
- Time values are text like "9:00 AM - 1:50 PM" or blank.
## Sort order
1. Campus (column E), A→Z
2. Room (column F), A→Z
3. Day (column G), Sunday→Saturday. For multi-day values, use the FIRST day mentioned. Blank goes last.
4. Time (column H), by start time (the part before " - "), earliest first. Blank goes last.
Do NOT renumber Row# (column A) unless the user asks; it moves with its row.
## Steps
### 1. Inspect
Use execute_office_js to list all worksheets. For each one, check that A7 = "Row#". Count data rows from row 8 down, using column B. Confirm columns N:O are empty, since they'll hold temporary helper values. Skip sheets with no data rows or no matching header, and note them for the final report.
Done when: you have a list of sheets to sort, with the row count for each.
### 2. Sort with temporary helper columns
In one execute_office_js call, do the following for each qualifying sheet:
- Compute a day index for each data row. Regex-match the first of sunday…saturday (case-insensitive) in column G and map it to 0–6, or 99 if there's no match. Compute a start-time value in minutes from the first `h:mm AM/PM` in column H, or 9999 if there's no match. Write these as values to N8:O{last}.
- Apply Excel's native sort to A7:O{last} with hasHeaders = true and these keys: 4 (Campus), 5 (Room), 13 (day index), 14 (start minutes), all ascending. The native sort keeps row formatting intact.
- Clear columns N:O with `clear(Excel.ClearApplyTo.all)`.
Reference code:
```javascript
const dayIdx={sunday:0,monday:1,tuesday:2,wednesday:3,thursday:4,friday:5,saturday:6};
const d=s=>{const m=String(s).toLowerCase().match(/sunday|monday|tuesday|wednesday|thursday|friday|saturday/);return m?dayIdx[m[0]]:99;};
const t=s=>{const m=String(s).match(/(\d{1,2}):(\d{2})\s*([AP]M)/i);if(!m)return 9999;let h=+m[1]%12;if(m[3].toUpperCase()==="PM")h+=12;return h*60+ +m[2];};
// per sheet: write [[d(G),t(H)],...] to N8:O{7+n}, then
// sheet.getRange(`A7:O${7+n}`).sort.apply([{key:4,ascending:true},{key:5,ascending:true},{key:13,ascending:true},{key:14,ascending:true}],false,true);
// sheet.getRange("N:O").clear(Excel.ClearApplyTo.all);
```
Done when: every qualifying sheet has been sorted and its columns N:O are empty.
### 3. Verify
Re-read A7:O{last} on at least two sheets, including one with multi-day entries (usually a Summer term). Confirm the Campus → Room → Day → Time order and that columns N:O are empty.
Done when: the sheets you re-read are in the correct order.
### 4. Report
Briefly list which sheets you sorted, which you skipped and why, and which you re-read to verify. Offer to renumber Row# if the user wants.