File size: 788 Bytes
4c36df5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { google } = require('googleapis');

async function saveOrderToSheet(orderData) {
    const auth = new google.auth.GoogleAuth({
        keyFile: 'credentials.json', // File key của Google Cloud
        scopes: ['https://www.googleapis.com/auth/spreadsheets'],
    });
    const sheets = google.sheets({ version: 'v4', auth });
    
    const values = [[
        new Date().toLocaleString(),
        `DH${Date.now()}`,
        orderData.phone,
        orderData.address,
        orderData.items.join(", "),
        orderData.totalPrice,
        orderData.weight
    ]];

    await sheets.spreadsheets.values.append({
        spreadsheetId: process.env.SPREADSHEET_ID,
        range: 'khachhang!A:G',
        valueInputOption: 'USER_ENTERED',
        resource: { values },
    });
}