## When to use Use when the user runs /course-sort-semester, or wants their course-schedule tabs (e.g. "2026 Fall", "2027 Spring", combined sheets like "Springs") sorted like the "courseSort Macro" with Semester as an extra final level. Applies to every sheet in the workbook unless the user names specific sheets. ## Expected sheet layout - Row 7 is the header: Row# or Semester | Rubric | Title | Status | Campus | Room | Day | Time | Instructors | Instructional Format | Delivery Mode | Enrolled | Capacity (columns A:M). - Column A header (A7) is either "Row#" (single-term sheets) or "Semester" (values like "2025 Spring"; used on combined sheets like "Springs"). - Data starts in row 8. The last data row is the last row where column B (Rubric) is not blank. Blank rows inside the data block (e.g. inserted spacer rows) are included in the sort range and will sort to the bottom — mention this in the report if any are found. - 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. 5. Semester (column A), oldest first — ONLY if A7 = "Semester" (case-insensitive, trimmed). - If A7 is "Row#" or anything else, do NOT add this level; the sheet sorts with levels 1–4 only. - If the Semester column has a single value (or blanks), the level is still safe: it is the last tiebreaker, so it changes nothing. No need to check for variation. 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, read A7 and accept the sheet if A7 is "Row#" or "Semester"; record whether it is a Semester sheet. Count data rows from row 8 down, using column B, and note any fully blank rows inside the block. 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 and Semester yes/no for each. ### 2. Sort with temporary helper columns In one execute_office_js call, for each qualifying sheet: - Compute a day index per data row: regex-match the first of sunday…saturday (case-insensitive) in column G → 0–6, else 99. Compute start minutes from the first `h:mm AM/PM` in column H, else 9999. Write these as values to N8:O{last}. - Build sort keys: 4 (Campus), 5 (Room), 13 (day index), 14 (start minutes), all ascending. If it is a Semester sheet, append key 0 (Semester), ascending. - Apply Excel's native sort to A7:O{last} with hasHeaders = true (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 const keys=[{key:4,ascending:true},{key:5,ascending:true},{key:13,ascending:true},{key:14,ascending:true}]; if (String(a7).trim().toLowerCase()==="semester") keys.push({key:0,ascending:true}); // sheet.getRange(`A7:O${7+n}`).sort.apply(keys,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: one with multi-day entries (usually a Summer term) and, if present, one Semester sheet with multiple semesters (e.g. "Springs"). Confirm Campus → Room → Day → Time (→ Semester) order and that N:O are empty. Note: Excel sorts blank Campus/Room values last, so a naive JS string compare may flag those rows as false positives. Done when: the sheets you re-read are in the correct order. ### 4. Report Briefly list which sheets you sorted (and which got the Semester level), which you skipped and why, any blank rows that moved to the bottom, and which sheets you re-read to verify. Offer to renumber Row# if the user wants.