improvements in attachPdfToAccount function and splitExcel function

This commit is contained in:
2026-07-30 12:23:09 +02:00
parent 406d519a64
commit 3fbc08b054
+10 -37
View File
@@ -1,6 +1,8 @@
const ExcelJS = require('exceljs');
const { PDFParse } = require('pdf-parse');
const { PDFDocument } = require('pdf-lib');
const readline = require('node:readline/promises');
const { stdin: input, stdout: output } = require('node:process');
const fs = require('fs');
const os = require('os');
const path = require('path');
@@ -52,9 +54,10 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
let parser;
try {
let contentVersion;
//for (let i = 0; i < tempFiles.length; i++) {
const rl = readline.createInterface({ input, output });
for (let i = 0; i < tempFiles.length; i++) {
// 1. Read the local PDF file and convert it to Base64
const pdfBuffer = fs.readFileSync(tempFiles[0]);
const pdfBuffer = fs.readFileSync(tempFiles[i]);
const base64Pdf = pdfBuffer.toString('base64');
parser = new PDFParse({ data: pdfBuffer });
try {
@@ -79,7 +82,6 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
const fullName = parts.join(' ');
const reversedName = [...parts.slice(1), parts[0]].join(' ');
//conditions.push(`Name='${fullName}'`);
if (fullName !== reversedName) {
conditions.push(`Name='${ escapeSoql(reversedName) }'`);
}
@@ -100,10 +102,11 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
console.log('DRY RUN: non creato documento');
}
else {
const title = await rl.question('Choose a title for the document: ');
const nowIsoString = new Date().toISOString(); // current timestamp in UTC
const newDocument = await sfdc.create('Documento__c', {
Account__c: `${ result_soql.records[0].Id }`,
Name: `Attestato - Sicurezza e Salute sul Lavoro -`,
Name: `${ title }`,
Data_Caricamento__c: nowIsoString,
Tipo_Documento__c: "Stage doc",
Stato__c: 'Valido'
@@ -131,9 +134,10 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
await parser.destroy();
}
}
//}
}
console.log(`PDFs successfully attached!`);
console.log('Dry run completed!')
console.log('Dry run completed!');
rl.close();
} catch (error) {
console.error(`Error uploading PDF: `, error);
@@ -159,7 +163,6 @@ async function processPage(pdfDoc, pageIndex, tempFiles) {
async function splitExcelFile(inputFile) {
let numRows; // this parameter is needed when we need to analyze the pdf file in case it has duplicate pages
const columnNames = []; // contains the names of the columns (header names)
try {
console.log(`Loading excel file: ${ inputFile }\n`);
@@ -172,36 +175,6 @@ async function splitExcelFile(inputFile) {
numRows = (worksheet.actualRowCount) - 1; // since it counts the row that represents the header, I need to subtract -1
// to get the actual number of rows that contains the data
worksheet.getRow(1) // to convert every title of the first row to upper case
.eachCell((col, col_num) => {
if (typeof col.value === 'string') {
col.value = col.value.toUpperCase().trim();
}
})
// to store the names of the columns in columnNames
worksheet.getRow(1).eachCell({ includeEmpty: true }, (cell, cell_num) => {
columnNames.push(cell.value);
})
// now we need to store the data of each user
for (let i = 2; i <= numRows + 1; i++) {
let dataUser = {};
worksheet.getRow(i)
.eachCell({ includeEmpty: true }, (cell, cell_num) => {
const rawColumnName = columnNames[cell_num - 1] || `column_${cell_num}`;
// 1. Trim whitespace and replace spaces with underscores
const cleanKey = rawColumnName
.toString()
.trim()
.replace(/\s+/g, '_'); // \s => matches any whitespaces
// + => groups multiple consecutive spaces together into a
// single match so you don't end up with multiple underscores.
dataUser[cleanKey] = cell.value;
})
}
return numRows;
}
catch (error) {