skip to Main Content
const { app } = require('@azure/functions');
const ExcelJS = require('exceljs');
const { Pool } = require('pg');

app.storageBlob('storageBlobTrigger', {
    path: 'attachments/{name}',
    connection: 'MyStorageConnectionString',
    handler: async (context) => {
        // Obtain a reference to the blob stream
        const blobStream = context.bindingData.blobTrigger.openReadStream();

        try {
            context.log(`Processing blob "${context.bindingData.name}" with size ${context.bindingData.length} bytes`);

            // Create a new workbook
            const workbook = new ExcelJS.Workbook();
            await workbook.xlsx.read(blobStream);  // Read the Excel file from the blob stream

            // Assuming you're interested in the first worksheet
            const worksheet = workbook.getWorksheet(1);

           const pool = new Pool({
                user: '****',
                host: '***',
                database: '***',
                password: '****',
                port: 5432,
                ssl: {
                    rejectUnauthorized: false // Necessary for Azure connections but consider the security implications
                }
            });

            // Connect to the database
            const client = await pool.connect();

            try {
                // Start a transaction
                await client.query('BEGIN');

                // Process all rows asynchronously
                const updates = [];
                worksheet.eachRow((row, rowNumber) => {
                    const values = [
                        row.getCell(1).value, row.getCell(2).value, row.getCell(3).value,
                        row.getCell(4).value, row.getCell(5).value, row.getCell(6).value,
                        row.getCell(7).value, row.getCell(8).value
                    ];

                    const updatePromise = client.query(`
                        UPDATE stock_overview SET
                        material_code = $2, producer_item_code = $3, description_id = $4, serial_number = $5,
                        total_stock = $6, unit = $7, warehouse = $8
                        WHERE product_name = $1;
                    `, values);

                    updates.push(updatePromise);
                    updatePromise.then(result => {
                        context.log(`Updated database for Row ${rowNumber} with result: ${result.rowCount}`);
                    });
                });

                // Wait for all updates to complete
                await Promise.all(updates);

                // Commit the transaction
                await client.query('COMMIT');
            } catch (error) {
                await client.query('ROLLBACK'); // Roll back the transaction on error
                throw error;
            } finally {
                client.release(); // Release the client back to the pool
            }

        } catch (error) {
            context.log.error(`Failed to process Excel file: ${error}`);
            throw error;  // Ensure errors are logged in Azure monitoring
        } finally {
            pool.end(); // Close the pool
        }
    }
});

I am automating a process, in which:

  • I need to read email attachment files from Microsoft Outlook 365 account and update a database accordingly in the Microsoft Azure account.

  • I have created a logic app which triggers the email account and saves the attachment to a blob storage account. -beside that I have created a Azure Database for PostgreSQL flexible servers.

  • I have created a function which is triggered by the blob storage account and next step is to read and process the excel files and update the database. the code above I’ve written doesn’t work as intended and crashes. Could someone please help me with it.

Thank You for help in advance

2

Answers


  1. Chosen as BEST ANSWER

    Thanks Alot for your response. The function gets triggered by the storage account can read the excel files, it doesn’t update the Azure postgres database and my Function gets stuck here. I have to work with a vpn and the database is not found when connected with vpn.enter image description here


  2. This worked for me.

    I have used xlsx package for reading *.xlsx file.

    const { app } = require('@azure/functions');
    const {Client} = require('pg');
    const XLSX = require('xlsx');
    
    app.storageBlob('storageBlobTrigger', {
        path: 'test',
        connection: 'mystrg_conn',
        handler: async (blob,context) => {
            const connection = new Client(
                {
                    host: "xxxxxxxx",
                    user: "xxxxxxxxx",
                    password: "xxxxxxxxx",
                    database: "xxxxxxx",
                    port: 5432,
                    ssl:{
                        rejectUnauthorized: false
                    }
                });
                try{
                await connection.connect();
    
                context.log(`Storage blob function processed blob "${context.triggerMetadata.name}" with size ${blob.length} bytes`);
                console.log("excel data: ",blob)
    
                const workbook = XLSX.read(blob);
                const sheetName = workbook.SheetNames[0] // Assuming data is in the first sheet
                const sheet = workbook.Sheets[sheetName];
    
                const data = XLSX.utils.sheet_to_json(sheet);
    
                console.log(data);
                for (let row of data) {
                    await connection.query('INSERT INTO desserts (name, type, price) VALUES ($1, $2, $3)', [row.name, row.type, row.price]);
                }
                
    
                const result = await connection.query('Select * from desserts')
    
                console.log("Result: ",result.rows);
    
            }
            catch(error){
                console.log("Error: ",error);
            }
            finally{
                connection.end()
            }
    
                
        }
    });
    

    INPUT:

    name type price
    Butterscotch Ice Cream 9.50

    OUTPUT:

    [
      {
         dessert_id: 1,
         name: 'Chocolate Cake',
         type: 'Cake',
         price: '15.99'
       },
       {
         dessert_id: 2,
         name: 'Vanilla Ice Cream',
         type: 'Ice Cream',
       },
       {
         dessert_id: 3,
         name: 'Strawberry Cheesecake',
         type: 'Cheesecake',
         price: '20.75'
       },
       { dessert_id: 4, name: 'Apple Pie', type: 'Pie', price: '12.25' },
       {
         dessert_id: 5,
         name: 'Butterscoch',
         type: 'Ice Cream',
         price: '9.50'
       }
    ]
    

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search