google-sheets-uploader 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +5 -0
- package/lib/excelHandler.js +19 -0
- package/lib/googleAuth.js +18 -0
- package/lib/sheetsUploader.js +68 -0
- package/package.json +24 -0
package/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const ExcelJS = require('exceljs');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
|
|
4
|
+
async function convertJsonToExcel(jsonData, outputFilePath) {
|
|
5
|
+
const workbook = new ExcelJS.Workbook();
|
|
6
|
+
const worksheet = workbook.addWorksheet('Sheet1');
|
|
7
|
+
|
|
8
|
+
const headers = Object.keys(jsonData[0]);
|
|
9
|
+
worksheet.addRow(headers); // Añadir encabezados
|
|
10
|
+
|
|
11
|
+
jsonData.forEach(row => {
|
|
12
|
+
worksheet.addRow(Object.values(row)); // Añadir datos fila por fila
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
await workbook.xlsx.writeFile(outputFilePath);
|
|
16
|
+
console.log(`Excel file created from JSON at ${outputFilePath}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = { convertJsonToExcel };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const { google } = require('googleapis');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
async function authenticate(credentialsPath) {
|
|
6
|
+
if (!fs.existsSync(credentialsPath)) {
|
|
7
|
+
throw new Error(`The credentials file does not exist at ${credentialsPath}.`);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const auth = new google.auth.GoogleAuth({
|
|
11
|
+
keyFile: credentialsPath,
|
|
12
|
+
scopes: ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return await auth.getClient();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = { authenticate };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const { google } = require('googleapis');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const ExcelJS = require('exceljs');
|
|
4
|
+
const { authenticate } = require('./googleAuth');
|
|
5
|
+
|
|
6
|
+
async function clearSheet(sheets, spreadsheetId, sheetName) {
|
|
7
|
+
try {
|
|
8
|
+
const response = await sheets.spreadsheets.get({ spreadsheetId });
|
|
9
|
+
const sheet = response.data.sheets.find(sheet => sheet.properties.title === sheetName);
|
|
10
|
+
const sheetId = sheet ? sheet.properties.sheetId : null;
|
|
11
|
+
|
|
12
|
+
if (!sheetId) {
|
|
13
|
+
throw new Error(`Sheet "${sheetName}" not found.`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
await sheets.spreadsheets.batchUpdate({
|
|
17
|
+
spreadsheetId,
|
|
18
|
+
resource: {
|
|
19
|
+
requests: [{
|
|
20
|
+
updateCells: {
|
|
21
|
+
range: { sheetId, startRowIndex: 0, startColumnIndex: 0 },
|
|
22
|
+
fields: 'userEnteredValue'
|
|
23
|
+
}
|
|
24
|
+
}]
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
console.log(`Cleared data from sheet: ${sheetName}`);
|
|
29
|
+
} catch (error) {
|
|
30
|
+
throw new Error(`Error clearing sheet: ${error.message}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function uploadToGoogleSheets(credentialsPath, spreadsheetId, sheetName, filePathOrJson) {
|
|
35
|
+
const authClient = await authenticate(credentialsPath);
|
|
36
|
+
const sheets = google.sheets({ version: 'v4', auth: authClient });
|
|
37
|
+
|
|
38
|
+
let filePath = filePathOrJson;
|
|
39
|
+
|
|
40
|
+
// Si filePathOrJson es un JSON, convertirlo a un archivo Excel
|
|
41
|
+
if (typeof filePathOrJson !== 'string') {
|
|
42
|
+
const jsonData = filePathOrJson;
|
|
43
|
+
filePath = './output/temp_json_to_excel.xlsx';
|
|
44
|
+
await convertJsonToExcel(jsonData, filePath);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const workbook = new ExcelJS.Workbook();
|
|
48
|
+
await workbook.xlsx.readFile(filePath);
|
|
49
|
+
const worksheet = workbook.getWorksheet(1);
|
|
50
|
+
|
|
51
|
+
// Limpiar la hoja existente
|
|
52
|
+
await clearSheet(sheets, spreadsheetId, sheetName);
|
|
53
|
+
|
|
54
|
+
const rows = worksheet.getSheetValues().slice(1); // Excluye la fila de encabezado
|
|
55
|
+
const values = rows.map(row => row.slice(1)); // Ajustar el rango
|
|
56
|
+
const range = `${sheetName}!A1:${String.fromCharCode(64 + rows[0].length)}${rows.length + 1}`;
|
|
57
|
+
|
|
58
|
+
await sheets.spreadsheets.values.update({
|
|
59
|
+
spreadsheetId,
|
|
60
|
+
range,
|
|
61
|
+
valueInputOption: 'RAW',
|
|
62
|
+
resource: { values }
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
console.log(`Data uploaded to Google Sheets (${spreadsheetId}) in sheet ${sheetName}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = { uploadToGoogleSheets };
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "google-sheets-uploader",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A library for uploading data to Google Sheets from Excel or JSON.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"google",
|
|
11
|
+
"sheets",
|
|
12
|
+
"excel",
|
|
13
|
+
"json",
|
|
14
|
+
"uploader",
|
|
15
|
+
"api",
|
|
16
|
+
"nodejs"
|
|
17
|
+
],
|
|
18
|
+
"author": "Carlos Escorcia cmescorcia5@icloud.com",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"exceljs": "^4.4.0",
|
|
22
|
+
"googleapis": "^144.0.0"
|
|
23
|
+
}
|
|
24
|
+
}
|