-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadexceltodrive.js
More file actions
63 lines (54 loc) · 2.24 KB
/
Copy pathUploadexceltodrive.js
File metadata and controls
63 lines (54 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const ExcelJS = require('exceljs');
const orderModel = require("./models/PurchaseOrder")
const { uploadBufferToCloud } = require("./googlecloudstorage.service");
// Function to export and upload Excel file to Google Cloud Storage
const exportToExcelAndUpload = async (Id) => {
try {
// Fetch your data (replace with actual MongoDB query)
const orders = await orderModel.find({}).populate("staff","name email").lean();
//console.log("the orders",orders)
// Process the orders to create your Excel data
const formattedData = orders.map((order)=>{
return{orderNumber: order.orderNumber || "N/A",
supplier: order.supplier || "N/A",
email: order.staff.email || "N/A",
status: order.status || "N/A",
orderedBy: order.staff.name || "N/A",
}
});
console.log("formatted data products:",formattedData.products)
const productData = orders.flatMap(order =>
order.products.map(item => ({
orderNumber: order.orderNumber || "N/A", // Include orderNumber for reference
name: item.name || "N/A",
quantity: item.quantity || "N/A",
price: item.price || "N/A"
}))
);
// Create the Excel file in memory
const workbook = new ExcelJS.Workbook();
const ws = workbook.addWorksheet('orders');
if (formattedData.length > 0) {
ws.columns = Object.keys(formattedData[0]).map(key => ({ header: key, key }));
ws.addRows(formattedData);
}
const ordersworksheet = workbook.addWorksheet('Request_data');
if (productData.length > 0) {
ordersworksheet.columns = Object.keys(productData[0]).map(key => ({ header: key, key }));
ordersworksheet.addRows(productData);
}
// Write the workbook to a buffer (not to a file)
const excelBuffer = await workbook.xlsx.writeBuffer();
// Upload the file to Google Cloud Storage
const cloudFile = await uploadBufferToCloud(
excelBuffer,
'orders.xlsx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
);
console.log('File uploaded successfully! Object name:', cloudFile.objectName);
} catch (error) {
console.error('Error exporting and uploading Excel file:', error);
}
};
// Run the function
module.exports=exportToExcelAndUpload;