Gantt Copy Tasks

Hello,

I am trying to copy & paste the tasks within and outside of the gantt.

  1. What can I use to copy & paste the whole of task data or selective task data?
  2. Would it be possible to copy & paste within different tabs?
  3. Is it possible to copy from external resource to gantt?

Thanks!

Hello Karanveer,
Gantt doesn’t have a built-in feature to copy and paste tasks. But you can use the Gantt API and implement a custom solution.

What can I use to copy & paste the whole of task data or selective task data?

You can use the copy method that creates a deep copy of the provided object:
https://docs.dhtmlx.com/gantt/api__gantt_copy.html

To iterate the selected tasks, you can use the eachSelectedTask method:
https://docs.dhtmlx.com/gantt/api__gantt_eachselectedtask.html

To iterate all tasks, you can use the eachTask method:
https://docs.dhtmlx.com/gantt/api__gantt_eachtask.html

To add a new task, you can use the addTask method and specify the task object:
https://docs.dhtmlx.com/gantt/api__gantt_addtask.html

However, you need to make sure that the new task doesn’t have the same value in the id parameter. Otherwise, it will replace the existing task.

Here is an example that can help you to start implementing your solution:
https://snippet.dhtmlx.com/kck3pnmh?text=gantt.%2Bcopy

Also, if you want to save all data from Gantt, you can use the serialize method:
https://docs.dhtmlx.com/gantt/api__gantt_serialize.html


Would it be possible to copy & paste within different tabs?

If you have several Gantts on different tabs, and you want to transfer some tasks between them, it means, you need to save the data you copy from Gantt1 before you can paste it in Gantt2. The object where the data is saved should be available for both Gantts.


Is it possible to copy from external resource to gantt?

It should be possible to do that. But Gantt expects the task object with the specific properties:
https://docs.dhtmlx.com/gantt/desktop__task_properties.html

This means before you add the data to Gantt, you need to make sure that the dataset has the properties and structure Gantt expects:
https://docs.dhtmlx.com/gantt/desktop__loading.html

Thank you for your reply.

I have another question, when I copy the task from external resource then its going to save on clipboard by default, then how will I be able to add it as a new task in gantt as its not locally available on gantt.

Only then the task properties can be added.

Please let me know if this is possible.

Thanks

Hello Karanveer,
It depends on how it is implemented in your case and in what form you have the task data.
If the task data is in the form of a text in the clipboard, you need to extract that information from the clipboard, then you can provide it to the Gantt API:

There is an example of the implementation that shows how to copy text from Excel and paste it to the grid columns:

In this example, the text data is extracted from the clipboard, then by using the Gantt API, the tasks are modified.

You can use the same approach for adding tasks.
Here is an example where you can press the Control+v hotkey after clicking in the grid, and Gantt will show a new dialog to add a task:
http://snippet.dhtmlx.com/5/d1b0cfe96

Thank you so much for your quick reply.

I’ve already tried this but because my gantt works inside a dynamic iframe, it gives me permission policy errors. I would really appreciate if you have an solution for this or if you can suggest another way.

Thanks!

Hello Karanveer,
If Gantt works inside an iframe, you need to add permissions for that iframe to read the clipboard:

Otherwise, you won’t be able to do that, and this is not a Gantt issue.

If you cannot change the policy, you need to use the workarounds. For example, you can paste the text contents in an input field, then you can copy the contents of the input field and add a new task basing on it.
Here is an example of how it can be implemented:
https://snippet.dhtmlx.com/kpzvf5t5

Thank you for answering all my questions.