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

Code before:
      };

      const document = writeToServerDocuments({
        data: combinedData,
        filename: slugify(path.basename(filename)),
        destinationOverride: null,
        options: { parseOnly: true },
      });
      documents.push(document);
      console.log(`[SUCCESS]: ${filename} converted & ready for embedding.`);
    } else {
      const folderName = slugify(
        `${path.basename(filename)}-${v4().slice(0, 4)}`,
        {
          lower: true,
          trim: true,
        }
      );
      const outFolderPath = path.resolve(documentsFolder, folderName);
      if (!fs.existsSync(outFolderPath))
        fs.mkdirSync(outFolderPath, { recursive: true });

      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;
          }

          console.log(`-- Processing sheet: ${name} --`);
          const sheetData = {
            id: v4(),
            url: `file://${path.join(outFolderPath, `${slugify(name)}.csv`)}`,
            title: metadata.title || `${filename} - Sheet:${name}`,
            docAuthor: metadata.docAuthor || "Unknown",
            description:
              metadata.description || `Spreadsheet data from sheet: ${name}`,
            docSource:
              metadata.docSource || "an xlsx file uploaded by the user.",
            chunkSource: metadata.chunkSource || "",
            published: createdDate(fullFilePath),
            wordCount: content.split(/\s+/).length,
            pageContent: content,
            token_count_estimate: tokenizeString(content),
          };

          const document = writeToServerDocuments({
            data: sheetData,

Review comment (refactor):
Maybe abstract to a function to keep code DRY?

Example:
```ts
/**
 * Processes a single sheet and returns its content and metadata
 * @param {Object} sheet - The sheet object from xlsx.parse()
 * @returns {Object|null} - Object with { name, content, wordCount } or null if sheet is empty
 */
function processSheet(sheet) {
  try {
    const { name, data } = sheet;
    const content = convertToCSV(data);

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

    console.log(`-- Processing sheet: ${name} --`);
    return {
      name,
      content,
      wordCount: content.split(/\s+/).length,
    };
  } catch (err) {
    console.error(`Error processing sheet "${sheet.name}":`, err);
    return null;
  }
}
```

Code after:
        filename: `${slugify(path.basename(filename))}-${combinedData.id}`,
        destinationOverride: null,
        options: { parseOnly: true },
      });
      documents.push(document);
      console.log(`[SUCCESS]: ${filename} converted & ready for embedding.`);
    } else {
      const folderName = slugify(
        `${path.basename(filename)}-${v4().slice(0, 4)}`,
        {
          lower: true,
          trim: true,
        }
      );
      const outFolderPath = path.resolve(documentsFolder, folderName);
      if (!fs.existsSync(outFolderPath))
        fs.mkdirSync(outFolderPath, { recursive: true });

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

        const { name, content, wordCount } = processed;
        const sheetData = {
          id: v4(),
          url: `file://${path.join(outFolderPath, `${slugify(name)}.csv`)}`,
          title: metadata.title || `${filename} - Sheet:${name}`,
          docAuthor: metadata.docAuthor || "Unknown",
          description:
            metadata.description || `Spreadsheet data from sheet: ${name}`,
          docSource: metadata.docSource || "an xlsx file uploaded by the user.",
          chunkSource: metadata.chunkSource || "",
          published: createdDate(fullFilePath),
          wordCount: wordCount,
          pageContent: content,
          token_count_estimate: tokenizeString(content),
        };

        const document = writeToServerDocuments({
          data: sheetData,
          filename: `sheet-${slugify(name)}`,
          destinationOverride: outFolderPath,
          options: { parseOnly: options.parseOnly },
        });
        documents.push(document);
        console.log(
          `[SUCCESS]: Sheet "${name}" converted & ready for embedding.`
        );
      }
    }
  } catch (err) {