Simple gantt example not exporting

Hello

Im very new to dhtmlx gantt, and I was wondering why this example code does not export anything?
I get an empty excel sheet

When i set the “visual” property to “true”, i get some date headers.

Any help is appreciated.

gantt.config.xml_date = “%d-%m-%Y %H:%i”;

    gantt.plugins({
        export_api: true
    });

    const data = {
        tasks: [
            {
                id: 1, text: "Task #2", start_date: "03-04-2018 00:00", type: "project",
                render: "split"
            },
            {
                id: 2, text: "Task #2.1", start_date: "03-04-2018 00:00", duration: 1,
                parent: 1
            },
            {
                id: 3, text: "Task #2.2", start_date: "05-04-2018 00:00", duration: 2,
                parent: 1
            },
            {
                id: 4, text: "Task #2.3", start_date: "08-04-2018 00:00", duration: 1,
                parent: 1
            }
        ],
        links: []
    }

    gantt.init("gantt_here");
    gantt.parse(data)

    setTimeout(() => {

        gantt.exportToExcel({
            name: "document.xlsx",
            visual: false,
            cellColors: true,
            data: data,
            date_format: "d/m/yyyy"
        });

    }, 1000)

Hello @KoenMorren ,

The reason why you get an empty Excel sheet is that you set the custom data in an incorrect format. In your code, you are passing the custom data (in the data parameter), which is an object containing both tasks and links. The export expects an array of tasks, so you need to pass only the tasks array:

gantt.exportToExcel({
    name: "document.xlsx",
    visual: false,
    data: data.tasks, // <-- pass the array of tasks here
    cellColors: true,
    date_format: "d/m/yyyy"
});

With this change, the tasks should be correctly exported to Excel.
Please check the working example: DHTMLX Snippet Tool.

You can omit the data parameter entirely if you want to export the data presented in the initial Gantt chart:

gantt.exportToExcel({
    name: "document.xlsx",
    visual: false,
    cellColors: true,
    date_format: "d/m/yyyy"
});

Best regards,
Valeria Ivashkevich
DHTMLX Support Engineer

Related to this, why does the following example (DHTMLX Snippet Tool) give me this error?

Internal server error. Error: Gantt to Excel. TypeError: Cannot read properties of undefined (reading ‘forEach’)

The issue is related to the render: "split" property in your tasks.
Unfortunately, currently, there is no support for split tasks in Excel export.

A possible workaround is to temporarily switch split tasks to a regular rendering mode before export, and then restore the split rendering after the export is finished:

function exportData() {
    const splitTasks = [];

    gantt.eachTask(task => {
        if (task.render === "split") {
            task.render = "";
            splitTasks.push(task);
            gantt.refreshTask(task.id);
        }
    });

    gantt.exportToExcel({
        visual: "base-colors",
        cellColors: true
    });

    // Restore split tasks after export
    if (splitTasks.length) {
        splitTasks.forEach(task => {
            task.render = "split";
            gantt.refreshTask(task.id);
        });
    }
}

This way, the export module processes them as standard tasks and generates the Excel file correctly.

Please check the full working sample based on the example you provided: DHTMLX Snippet Tool.

We’ve passed this case to our dev team as well. The dev team will add support for split tasks in Excel export in the future. However, I am unable to give you any ETA. I will notify you on any updates on this case.

Best regards,
Valeria Ivashkevich
DHTMLX Support Engineer