I have a script report. Just like any other report I can export its data (rows/columns) in excel or csv format.
I have a custom button in this script report called “Non Redundant Export”. How can I trigger the original export button’s functionality when I click on my custom button.
Any help is appreciated.
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
"""build query for doclistview and return results"""
import json
import frappe
import frappe.permissions
from frappe import _
from frappe.core.doctype.access_log.access_log import make_access_log
from frappe.model import child_table_fields, default_fields, optional_fields
from frappe.model.base_document import get_controller
from frappe.model.db_query import DatabaseQuery
from frappe.model.utils import is_virtual_doctype
from frappe.utils import add_user_info, format_duration
@frappe.whitelist()
@frappe.read_only()
This file has been truncated. show original
Use Reference of Frappe Team Code to achieve your Customisation.
Find this function def export_query(): and look the code
1 Like
@pra17shant Thanks for your help.
Hello Prashant…
I added button “Export” using client script.
Now I am not able to add this function to that Button.
def export_query():
"""export from report builder"""
from frappe.desk.utils import get_csv_bytes, pop_csv_params, provide_binary_file
form_params = get_form_params()
form_params["limit_page_length"] = None
form_params["as_list"] = True
doctype = form_params.pop("doctype")
file_format_type = form_params.pop("file_format_type")
title = form_params.pop("title", doctype)
csv_params = pop_csv_params(form_params)
add_totals_row = 1 if form_params.pop("add_totals_row", None) == "1" else None
frappe.permissions.can_export(doctype, raise_exception=True)
if selection := form_params.pop("selected_items", None):
form_params["filters"] = {"name": ("in", json.loads(selection))}
make_access_log(
doctype=doctype,
file_type=file_format_type,
report_name=form_params.report_name,
filters=form_params.filters,
)
db_query = DatabaseQuery(doctype)
ret = db_query.execute(**form_params)
if add_totals_row:
ret = append_totals_row(ret)
data = [[_("Sr")] + get_labels(db_query.fields, doctype)]
data.extend([i + 1] + list(row) for i, row in enumerate(ret))
data = handle_duration_fieldtype_values(doctype, data, db_query.fields)
if file_format_type == "CSV":
from frappe.utils.xlsxutils import handle_html
file_extension = "csv"
content = get_csv_bytes(
[[handle_html(frappe.as_unicode(v)) if isinstance(v, str) else v for v in r] for r in data],
csv_params,
)
elif file_format_type == "Excel":
from frappe.utils.xlsxutils import make_xlsx
file_extension = "xlsx"
content = make_xlsx(data, doctype).getvalue()
provide_binary_file(title, file_extension, content)
Also, after adding button Export to ListView Actual Export Button in dropdown gets hide as below.
Sujay
November 14, 2024, 1:18pm
6
Hi, @umarless
Even I am trying to achieve the same. Did you find the solution?