questo codice funziona ma è lento nell'esecuzione: function startTraversing() { const…
- User
questo codice funziona ma è lento nell'esecuzione:
function startTraversing() { const folderId = ""; // Inserisci l'ID della cartella const folder = DriveApp.getFolderById(folderId); const files = folder.getFiles(); const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const structureSheet = spreadsheet.getSheetByName("Struttura_fe"); const structureRows = structureSheet.getDataRange().getValues();
// Creare una mappa per il rapido accesso ai dati const pathToStructureRowMap = structureRows.reduce((acc, row, index) => { const fullPath = "FatturaElettronica/" + row[2]; acc[fullPath] = index; return acc; }, {});
if (!files.hasNext()) { // La cartella è vuota, esci dalla funzione return; }
while (files.hasNext()) { const file = files.next(); const xml = file.getBlob().getDataAsString(); var document = XmlService.parse(xml); var rootElement = document.getRootElement(); traverseElements(rootElement, '', pathToStructureRowMap, structureRows, spreadsheet); // Start with the root element and an empty path } }
function traverseElements(element, path, pathToStructureRowMap, structureRows, spreadsheet) { // Append the current element's name to the path var currentPath = path + (path ? '/' : '') + element.getName();
// Verificare se il percorso corrente è mappato e ottenere i dati corrispondenti const rowIndex = pathToStructureRowMap[currentPath]; if (rowIndex !== undefined) { const structureRow = structureRows[rowIndex]; const targetSheetName = structureRow[3]; const targetCampo = structureRow[4]; // Questa dovrebbe essere la colonna che identifica il tipo di campo, ad esempio "ID" const targetSheet = spreadsheet.getSheetByName(targetSheetName); const targetColumn = structureRow[11]; // Assuming column L is index 11 (0-based index) cercaCampi(targetSheetName,"Id")
codeif (targetSheet) { appendDataToSheet(targetSheet, element.getText(), targetColumn, targetCampo); }}
// Get all child elements var children = element.getChildren(); // Iterate through the child elements and call the function recursively children.forEach(child => traverseElements(child, currentPath, pathToStructureRowMap, structureRows, spreadsheet)); }
function appendDataToSheet(targetSheet, data, targetColumn, targetCampo) { // Trova la prima riga vuota nella colonna target const lastRow = targetSheet.getLastRow(); const columnValues = targetSheet.getRange(1, targetColumn, lastRow).getValues();
// Trova la prima cella vuota nella colonna let emptyRowIndex = columnValues.findIndex(row => !row[0]) + 1; // +1 poiché gli indici degli array iniziano da 0
// Se non viene trovata alcuna riga vuota, usa la riga successiva all'ultima if (emptyRowIndex === 0) { emptyRowIndex = lastRow + 1; }
// Se il campo è "ID", genera un ID progressivo if (targetCampo === "ID") { let idValue = 1; // Inizia da 1 se la cella nella seconda riga è vuota if (emptyRowIndex > 2) { // La prima riga è per le intestazioni, quindi inizia a contare dalla seconda const lastId = parseInt(targetSheet.getRange(emptyRowIndex - 1, targetColumn).getValue()); idValue = isNaN(lastId) ? 1 : lastId + 1; // Incrementa l'ID del rigo precedente } data = idValue; // Sovrascrive i dati con il nuovo ID progressivo }
- Assistant
- User
mi dai tutto il codice completo
- Assistant