Convertir texto a columnas usando Aspose.Cells for JavaScript a través de C++

Escenarios de uso posibles

Puedes convertir tu texto a columnas usando Microsoft Excel. Esta característica está disponible desde Herramientas de Datos en la pestaña Datos. Para dividir el contenido de una columna en múltiples columnas, los datos deben contener un delimitador específico, como una coma (u otro carácter), según el cual Microsoft Excel divide el contenido de una celda en múltiples celdas. Aspose.Cells for JavaScript a través de C++ también proporciona esta función mediante Cells.textToColumns(number, number, number, TxtLoadOptions).

** Convertir texto a columnas usando Aspose.Cells for JavaScript a través de C++**

El código ejemplo siguiente explica el uso del método Cells.textToColumns(number, number, number, TxtLoadOptions). Primero añade algunos nombres de personas en la columna A de la primera hoja. El nombre y apellido están separados por un espacio. Luego aplica el método Cells.textToColumns(number, number, number, TxtLoadOptions) en la columna A y lo guarda como un archivo Excel de salida. Si abres el archivo Excel de salida, verás que los nombres están en la columna A mientras que los apellidos están en la columna B, como se muestra en esta captura.

todo:image_alt_text

Código de muestra

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example</title>
    </head>
    <body>
        <h1>Text to Columns Example</h1>
        <p>Select an optional Excel file to start from, or leave empty to create a new workbook.</p>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <button id="runExample">Run Example</button>
        <a id="downloadLink" style="display: none;">Download Result</a>
        <div id="result"></div>
    </body>

    <script src="aspose.cells.js.min.js"></script>
    <script type="text/javascript">
        const { Workbook, SaveFormat, TxtLoadOptions, Utils } = AsposeCells;

        AsposeCells.onReady({
            license: "/lic/aspose.cells.enc",
            fontPath: "/fonts/",
            fontList: [
                "arial.ttf",
                "NotoSansSC-Regular.ttf"
            ]
        }).then(() => {
            console.log("Aspose.Cells initialized");
        });

        document.getElementById('runExample').addEventListener('click', async () => {
            const fileInput = document.getElementById('fileInput');
            const resultDiv = document.getElementById('result');

            // Basic validation only: file is optional
            let workbook;
            if (fileInput.files && fileInput.files.length) {
                const file = fileInput.files[0];
                const arrayBuffer = await file.arrayBuffer();
                workbook = new Workbook(new Uint8Array(arrayBuffer));
            } else {
                // Create a new workbook if no file is provided
                workbook = new Workbook();
            }

            // Access first worksheet.
            const ws = workbook.worksheets.get(0);

            // Add people name in column A. First name and last name are separated by space.
            ws.cells.get("A1").value = "John Teal";
            ws.cells.get("A2").value = "Peter Graham";
            ws.cells.get("A3").value = "Brady Cortez";
            ws.cells.get("A4").value = "Mack Nick";
            ws.cells.get("A5").value = "Hsu Lee";

            // Create text load options with space as separator.
            const opts = new TxtLoadOptions();
            opts.separator = ' ';

            // Split the column A into two columns using TextToColumns() method.
            // Now column A will have first name and column B will have second name.
            ws.cells.textToColumns(0, 0, 5, opts);

            // Saving the modified Excel file
            const outputData = workbook.save(SaveFormat.Xlsx);
            const blob = new Blob([outputData]);
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'outputTextToColumns.xlsx';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download Modified Excel File';

            resultDiv.innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
        });
    </script>
</html>