Appsmith download issue.

mail@pastecode.io avatar
unknown
javascript
5 months ago
1.4 kB
7
Indexable
export default {
	
	progress: 0,
	running: false,
	
	generateExcel: async () => {
		
		this.progress = 0;
		this.running = true;
		
		// Initialise counting
		this.progress = 0;
		this.running = true;
		
		// Create workbook
		const wb = new exceljs.Workbook();
		
		// All products
		let all = await select_all_stock.run();
		this.progress++; // 1
		this.add_worksheet(wb, all, "All Products");
		this.progress++; // 2
		
		// Add cards
		let cards = await select_all_cards.run();
		this.progress++; // 3
		this.add_worksheet(wb, cards, "Cards");
		this.progress++; // 4
		
		// Generate the file
		const buffer = await wb.xlsx.writeBuffer();
		this.progress++;
		
		// Convert to base 64
		const base64Data = buffer.toString('base64');
		
		this.running = false;
		
		// // Download
		download(base64Data, "exported_data.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64");
		
	},

	add_worksheet(wb, data, title) {
		
		try {
		
			const ws = wb.addWorksheet(title);

			// Ensure there is data to add
			if (data.length > 0) {
				
				// Set columns
				ws.columns = Object.keys(data[0]).map(key => ({ header: key.toUpperCase().replace("_", " "), key: key }))
				
				// Insert data
				data.forEach(item => ws.addRow(item));
			}
			
		} catch (error) {
			showAlert(`Error creating sheet ${title}: ${error.message}`)
		}
		
	}
	
}
Leave a Comment