Skip to content

Conversation

@david-roper
Copy link
Collaborator

@david-roper david-roper commented Oct 23, 2025

Adds the subject ID as the page name for the subject table excel download

Summary by CodeRabbit

  • New Features

    • Excel export now creates two worksheets per export: a sanitized sheet name and the original name for clearer navigation.
  • Bug Fixes

    • Excel export correctly respects subject scope when exporting, preventing incorrect or ambiguous worksheet naming and content.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 23, 2025

Walkthrough

Excel export was updated to accept a worksheet name: downloadSubjectTableExcel gained a third parameter and export callers pass a subject-id–derived name for Excel and Excel Long paths; other export formats unchanged.

Changes

Cohort / File(s) Summary
Export Hook Updates
apps/web/src/hooks/useInstrumentVisualization.ts
Excel and Excel Long export branches now call downloadSubjectTableExcel(filename, rows, removeSubjectIdScope(params.subjectId)); CSV/JSON/TSV flows unchanged
Excel Utility
apps/web/src/utils/excel.ts
downloadSubjectTableExcel signature extended to accept a name argument; worksheet naming uses a sanitized version of name (plus original name as second sheet) instead of fixed 'ULTRA_LONG'

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant UI as UI / Hook
  participant Excel as utils/excel.downloadSubjectTableExcel
  note over UI,Excel #D6EAF8: Excel/Excel Long path
  UI->>Excel: downloadSubjectTableExcel(filename, rows, removeSubjectIdScope(subjectId))
  Excel-->>UI: writes .xlsx with sanitized sheet name + original name sheet

  note over UI,Excel #F8F3D4: Other export paths (CSV/JSON/TSV)
  UI->>UI: exportCSV/exportJSON/exportTSV(rows) 
  UI-->>User: triggers download
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • joshunrau

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "feat: add subjectID as page name for excel download" accurately describes the primary change in the pull request. The modifications add a third parameter to the downloadSubjectTableExcel function that accepts a subject identifier as the sheet name for Excel exports. The title is specific, concise, and directly reflects what was implemented: using the subjectID to generate sanitized sheet names in place of hard-coded values. It avoids vague terminology and clearly conveys the feature being added.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 47b7479 and 78c8ef2.

📒 Files selected for processing (2)
  • apps/web/src/hooks/useInstrumentVisualization.ts (1 hunks)
  • apps/web/src/utils/excel.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
apps/web/src/hooks/useInstrumentVisualization.ts (2)
apps/web/src/utils/excel.ts (1)
  • downloadSubjectTableExcel (10-14)
packages/subject-utils/src/index.ts (1)
  • removeSubjectIdScope (34-39)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: lint-and-test

Comment on lines 10 to 12
export function downloadSubjectTableExcel(filename: string, records: { [key: string]: any }[], name: string) {
const workbook = utils.book_new();
utils.book_append_sheet(workbook, utils.json_to_sheet(records), 'ULTRA_LONG');
utils.book_append_sheet(workbook, utils.json_to_sheet(records), name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Validate and sanitize the worksheet name.

Excel worksheet names have strict constraints: max 31 characters, cannot contain \ / ? * [ ] :, cannot be empty, and cannot start/end with apostrophe. Subject IDs may violate these restrictions, causing export failures.

Add validation before using the name:

 export function downloadSubjectTableExcel(filename: string, records: { [key: string]: any }[], name: string) {
+  // Sanitize worksheet name for Excel constraints
+  const sanitizedName = name
+    .replace(/[\\\/\?\*\[\]:]/g, '_')  // Replace invalid chars
+    .slice(0, 31)                        // Max 31 chars
+    .replace(/^'|'$/g, '')               // Remove leading/trailing apostrophes
+    .trim() || 'Sheet1';                 // Fallback if empty
+
   const workbook = utils.book_new();
-  utils.book_append_sheet(workbook, utils.json_to_sheet(records), name);
+  utils.book_append_sheet(workbook, utils.json_to_sheet(records), sanitizedName);
   writeFileXLSX(workbook, filename);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function downloadSubjectTableExcel(filename: string, records: { [key: string]: any }[], name: string) {
const workbook = utils.book_new();
utils.book_append_sheet(workbook, utils.json_to_sheet(records), 'ULTRA_LONG');
utils.book_append_sheet(workbook, utils.json_to_sheet(records), name);
export function downloadSubjectTableExcel(filename: string, records: { [key: string]: any }[], name: string) {
// Sanitize worksheet name for Excel constraints
const sanitizedName = name
.replace(/[\\\/\?\*\[\]:]/g, '_') // Replace invalid chars
.slice(0, 31) // Max 31 chars
.replace(/^'|'$/g, '') // Remove leading/trailing apostrophes
.trim() || 'Sheet1'; // Fallback if empty
const workbook = utils.book_new();
utils.book_append_sheet(workbook, utils.json_to_sheet(records), sanitizedName);

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 78c8ef2 and b4afa29.

📒 Files selected for processing (1)
  • apps/web/src/utils/excel.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: lint-and-test

export function downloadSubjectTableExcel(filename: string, records: { [key: string]: any }[], name: string) {
const sanitizedName =
name
.replace(/[\\/?*[\]:]/g, '_') // Replace invalid chars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Escape brackets in the regex.

The [ and ] characters inside the character class are not properly escaped. This may cause the regex to malfunction.

Apply this diff:

-      .replace(/[\\/?*[\]:]/g, '_') // Replace invalid chars
+      .replace(/[\\/?*\[\]:]/g, '_') // Replace invalid chars
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.replace(/[\\/?*[\]:]/g, '_') // Replace invalid chars
const sanitizedName = name
.replace(/[\\/?*\[\]:]/g, '_') // Replace invalid chars
.slice(0, 31) // Max 31 chars
.replace(/^'|'$/g, '') // Remove leading/trailing apostrophes
.trim() || 'Sheet1'; // Fallback if empty
🤖 Prompt for AI Agents
In apps/web/src/utils/excel.ts around line 13, the regex used in
.replace(/[\\/?*[\]:]/g, '_') contains unescaped square brackets inside the
character class which can break the pattern; update the character class so the
literal brackets are escaped (for example replace with /[\\/?*\\[\\]:]/g or
place the ] immediately after the opening bracket) so the regex correctly
matches and replaces literal '[' and ']' along with the other invalid filename
characters.

Comment on lines +18 to +19
utils.book_append_sheet(workbook, utils.json_to_sheet(records), sanitizedName);
utils.book_append_sheet(workbook, utils.json_to_sheet(records), name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Remove the second worksheet with unsanitized name.

Creating a worksheet with the unsanitized name (line 19) defeats the purpose of sanitization and will cause Excel export failures when subject IDs contain invalid characters or exceed 31 characters.

Apply this diff:

   utils.book_append_sheet(workbook, utils.json_to_sheet(records), sanitizedName);
-  utils.book_append_sheet(workbook, utils.json_to_sheet(records), name);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
utils.book_append_sheet(workbook, utils.json_to_sheet(records), sanitizedName);
utils.book_append_sheet(workbook, utils.json_to_sheet(records), name);
utils.book_append_sheet(workbook, utils.json_to_sheet(records), sanitizedName);
🤖 Prompt for AI Agents
In apps/web/src/utils/excel.ts around lines 18 to 19, a second worksheet is
being appended using the unsanitized `name`, which contradicts the sanitization
and can cause Excel failures; remove the second utils.book_append_sheet call
that uses `name` so only the sanitizedName is appended (keep the existing
sanitized append, delete or omit the unsanitized one).

@joshunrau joshunrau merged commit 86621f4 into DouglasNeuroInformatics:main Oct 23, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants