Copy content of any table to clipboard to paste it in Excel (already coded)

Hello,
I wrote some time ago an handy feature that add a little button on any dolibarr table (list of customers, list of invoices, etc.). When you click on it, it copy the table content to clipboard. Then you can paste it to Excel.

The file modified is :
htdocs\core\class\html.form.class.php

And the function :slight_smile:

public function showFilterButtons($pos = '')
	{
		$out = '<div class="nowraponall">';
		$out .= '<a class="liste_titre button_copycb" style="cursor: pointer"><span class="fa fa-copy"></span></a>';
		
		if ($pos == 'left') {
			$out .= '<button type="submit" class="liste_titre button_search reposition" name="button_search_x" value="x"><span class="fas fa-search"></span></button>';
			$out .= '<button type="submit" class="liste_titre button_removefilter reposition" name="button_removefilter_x" value="x"><span class="fas fa-times"></span></button>';
		} else {
			$out .= '<button type="submit" class="liste_titre button_search reposition" name="button_search_x" value="x"><span class="fas fa-search"></span></button>';
			$out .= '<button type="submit" class="liste_titre button_removefilter reposition" name="button_removefilter_x" value="x"><span class="fas fa-times"></span></button>';
		}
		$out .= '</div>';
		
		$out .= '<script>
			$(document).ready(function() {
            	$(".button_copycb").click(function() { 
					var TableContent = $(this).closest("table").clone();
					TableContent.find("a[href^=mailto]").each(function() {
						$(this).html($(this).attr("href").substr(7));
					});
					TableContent.find("a").contents().unwrap();
					TableContent.find("tr.liste_titre_filter").remove();
					TableContent.find("input").remove();
					TableContent.find("script").remove();
					TableContent.find("img").remove();
					TableContent.find(".dropdown").remove();
															
					navigator.clipboard.writeText(TableContent[0].outerHTML).then(function() {
					  console.log("Copied to clipboard successfully!");
					}, function() {
					  console.error("Unable to write to clipboard. :-(");
					});
				});
			});
			</script>';

		return $out;
	}
1 Like

Would you be willing to submit a PR in GitHub with this?

Hi,
I’m sorry. I tried but I didn’t understant how it works and then got crazzy because I don’t have too much time.

Hi, not a programmer here, I tried to embed your code into my dolibarr setup but it didn’t work. when I edit the php file I end up with a fatal error message saying there is problem with the html.form.class.php.

In which part should I inject your code?

Hi.
I think there is a problem with quotes.

I attach the file html.form.class.php then with it, you will be able to copy the part of code. Be carful to not replace your file but just get the peace of code (because we probalbly does’t have the same version).

Regards
David
html.form.class.php (461.3 KB)