-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
105 lines (85 loc) · 2.85 KB
/
api.js
File metadata and controls
105 lines (85 loc) · 2.85 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const { google } = require('googleapis');
const credentials = require('./exportToGoogleSheet/keys.json');
const mysql = require('mysql');
const util = require('util');
const dotenv = require('dotenv');
dotenv.config();
let jsonData = null;
const pool = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
database: process.env.DB_DATABASE,
});
const query = util.promisify(pool.query).bind(pool);
module.exports = {
submitToGoogleSheet: async (req, res) => {
const name = req.body.name;
const box = req.body.box;
try {
const result = await query('SELECT * FROM u2hdb.ItemBox WHERE GroupName = ? AND BoxNumber = ?', [name, box]);
console.log('Row Details:', JSON.stringify(result));
jsonData = result;
console.log(jsonData);
console.log(result);
} catch (err) {
console.error(err);
res.status(500).send('Error processing the request');
return;
}
const sheets = google.sheets({
version: 'v4',
auth: new google.auth.JWT(
credentials.client_email,
null,
credentials.private_key,
['https://www.googleapis.com/auth/spreadsheets'],
),
});
try {
// Get the spreadsheet information
const spreadsheetInfo = await sheets.spreadsheets.get({
spreadsheetId: '1vk9U8D8WY3EvQcqSVFPE9mzg5Wz1XjWd8vh5HsUMva8',
});
const newSheetTitle = `Box${box}_Group${name}`;
// Create a new sheet
await sheets.spreadsheets.batchUpdate({
spreadsheetId: '1vk9U8D8WY3EvQcqSVFPE9mzg5Wz1XjWd8vh5HsUMva8',
resource: {
requests: [
{
addSheet: {
properties: {
title: newSheetTitle,
},
},
},
],
},
});
console.log(jsonData);
// Extract column names from the first row of the result
const headers = Object.keys(jsonData[0]);
// Combine headers with data
const values = [headers, ...jsonData.map(row => Object.values(row))];
// Append data to the new sheet
const appendResult = await sheets.spreadsheets.values.append({
spreadsheetId: '1vk9U8D8WY3EvQcqSVFPE9mzg5Wz1XjWd8vh5HsUMva8',
range: `${newSheetTitle}!A1`,
valueInputOption: 'USER_ENTERED',
resource: {
values: values,
},
});
console.log('Data submitted to Google Sheet:', appendResult.data);
// Redirect to the Google Sheets document
const googleSheetsUrl = `https://docs.google.com/spreadsheets/d/1vk9U8D8WY3EvQcqSVFPE9mzg5Wz1XjWd8vh5HsUMva8/edit`;
res.redirect(googleSheetsUrl);
} catch (error) {
console.error(error);
res.status(500).send('Error submitting data to Google Sheet');
}
},
};