Repository: Mintplex-Labs/anything-llm
File: collector/processSingleFile/convert/asXlsx.js (JavaScript)

Code before:
          return cell;
        })
        .join(",")
    )
    .join("\n");
}

async function asXlsx({
  fullFilePath = "",
  filename = "",
  options = {},
  metadata = {},
}) {
  const documents = [];

  try {
    const workSheetsFromFile = xlsx.parse(fullFilePath);

    if (options.parseOnly) {
      const allSheetContents = [];
      let totalWordCount = 0;
      const sheetNames = [];

      for (const sheet of workSheetsFromFile) {
        const { name, data } = sheet;
        const content = convertToCSV(data);

        if (!content?.length) {
          console.warn(`Sheet "${name}" is empty. Skipping.`);
          continue;
        }

        console.log(`-- Processing sheet: ${name} --`);
        sheetNames.push(name);
        allSheetContents.push(`\n\n=== Sheet: ${name} ===\n\n${content}`);
        totalWordCount += content.split(/\s+/).length;
      }

      if (allSheetContents.length === 0) {
        console.log(`No valid sheets found in ${filename}.`);
        return {
          success: false,
          reason: `No valid sheets found in ${filename}.`,
          documents: [],
        };
      }

      const combinedContent = allSheetContents.join("\n");
      const sheetListText =
        sheetNames.length > 1
          ? ` (Sheets: ${sheetNames.join(", ")})`

Review comment (suggestion):
Shouldn't the csv conversion be wrapped in a try/catch like how it is in the `else` block?

Line 112
```ts
for (const sheet of workSheetsFromFile) {
  try { 
    const { name, data } = sheet;
    const content = convertToCSV(data);
    
    if (!content?.length) {
      console.log(`Sheet "${name}" is empty. Skipping.`);
      continue;
    }
    // ... rest of processing
  } catch (err) {
    console.error(`Error processing sheet "${name}":`, err);
    continue; 
  }
}
```

Code after:
        })
        .join(",")
    )
    .join("\n");
}

async function asXlsx({
  fullFilePath = "",
  filename = "",
  options = {},
  metadata = {},
}) {
  const documents = [];

  try {
    const workSheetsFromFile = xlsx.parse(fullFilePath);

    if (options.parseOnly) {
      const allSheetContents = [];
      let totalWordCount = 0;
      const sheetNames = [];

      for (const sheet of workSheetsFromFile) {
        const processed = processSheet(sheet);
        if (!processed) continue;

        const { name, content, wordCount } = processed;
        sheetNames.push(name);
        allSheetContents.push(`\nSheet: ${name}\n${content}`);
        totalWordCount += wordCount;
      }

      if (allSheetContents.length === 0) {
        console.log(`No valid sheets found in ${filename}.`);
        return {
          success: false,
          reason: `No valid sheets found in ${filename}.`,
          documents: [],
        };
      }

      const combinedContent = allSheetContents.join("\n");
      const sheetListText =
        sheetNames.length > 1
          ? ` (Sheets: ${sheetNames.join(", ")})`
          : ` (Sheet: ${sheetNames[0]})`;

      const combinedData = {
        id: v4(),
        url: `file://${fullFilePath}`,
        title: metadata.title || `${filename}${sheetListText}`,