Skip to content

Instantly share code, notes, and snippets.

@TurkerTunali
Created April 2, 2024 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TurkerTunali/e82af8fcb2b2d2e64ab358b3979fe90d to your computer and use it in GitHub Desktop.
Save TurkerTunali/e82af8fcb2b2d2e64ab358b3979fe90d to your computer and use it in GitHub Desktop.
Excel Create and Download Frappe (ERPNext)
frappe.ui.form.on('Sevkiyat', {
refresh: function(frm) {
frm.add_custom_button('Çeki Listesi Oluştur', async function() {
let columns = ["Batch", "Item Code", "Roll ID", "Customer", "Sales Order", "Net Weight [Kg]", "Gross Weight [Kg]", "Meter [cm]", "Width [cm]", "Diameter [cm]"];
let data = [];
for(let dItemIndex = 0; dItemIndex < frm.doc.shipment_detail.length; dItemIndex++) {
let docBatch = await frappe.db.get_doc("Batch", frm.doc.shipment_detail[dItemIndex].batch);
console.log(docBatch.reference_name);
let docWO = await frappe.db.get_value("Stock Entry", docBatch.reference_name, "work_order");
console.log(docWO);
data.push([
docWO.message.work_order, frm.doc.shipment_detail[dItemIndex].item_code, frm.doc.shipment_detail[dItemIndex].batch,
frm.doc.customer, frm.doc.shipment_detail[dItemIndex].sales_order,
docBatch.ld_net_weight, docBatch.gross_weight, docBatch.meter, docBatch.length, docBatch.diameter
]
);
}
open_url_post(
'/api/method/jetorme.jo_utils.download_excel_file',
{
columns: JSON.stringify(columns),
data: JSON.stringify(data)
}
);
});
}
});
@frappe.whitelist()
def download_excel_file(columns, data, excel_filename = "test.xlsx", sheet_name = "Generated by Fox"):
#This method will be used to generate excel files from data
from frappe.utils.xlsxutils import make_xlsx
from frappe.desk.query_report import build_xlsx_data
from frappe.utils.csvutils import UnicodeWriter
import csv
import os
columns = json.loads(columns)
data = json.loads(data)
writer = UnicodeWriter()
writer.writerow(columns)
for item in data:
writer.writerow(item)
filename = frappe.generate_hash("", 10)
with open(filename, "wb") as f:
f.write(cstr(writer.getvalue()).encode("utf-8"))
f = open(filename)
reader = csv.reader(f)
xlsx_file = make_xlsx(reader, sheet_name)
f.close()
os.remove(filename)
# write out response as a xlsx type
frappe.response["filename"] = excel_filename#"coa_importer_template.xlsx"
frappe.response["filecontent"] = xlsx_file.getvalue()
frappe.response["type"] = "binary"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment