web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Append Excel received ...
Power Automate
Unanswered

Append Excel received from an email as an attach to an existing Excel sheet in one drive business

(0) ShareShare
ReportReport
Posted on by
I'm currently working on a Power Automate flow that automates the process of appending data from an Excel attachment (sent from my ERP system) to an existing Excel file stored in OneDrive for Business.
 

Here's a breakdown of how the flow works:

  1. Trigger: The flow is triggered when I receive an email with the Excel file as an attachment (let's call this the Source file).

  2. Save Attachment: The attachment is automatically saved in OneDrive for Business.

  3. Step 1 - Create Table in Source: The first Office Script converts the data in the Source file into a table if it isn't already structured as one.

    • function main(workbook: ExcelScript.Workbook) {
          // Get the active worksheet
          let selectedSheet = workbook.getActiveWorksheet();
      
          // Get the used range in the sheet
          let usedRange = selectedSheet.getUsedRange();
      
          // Add a new table at the used range
          let newTable = selectedSheet.addTable(usedRange, true);
      }
       
  4. Step 2 - Add Export Date: The second Office Script adds a new column called Export_Date to the far right of the table in the Source file. This column is populated with the date the file was processed.

    • function main(workbook: ExcelScript.Workbook) {
        // Get the file name of the active workbook
        let fileName = workbook.getName(); // This will return the file name with the extension, e.g., "WPs_Updates__13092024.xlsx"
      
        // Extract the date part from the file name (assuming the last 8 characters before .xlsx)
        let dateFromFileName = fileName.slice(fileName.indexOf('__') + 2, fileName.indexOf('.xlsx'));
      
        // Format the date as DD/MM/YYYY
        let exportDate = `${dateFromFileName.slice(0, 2)}/${dateFromFileName.slice(2, 4)}/${dateFromFileName.slice(4, 8)}`;
      
        // Get the active worksheet
        let selectedSheet = workbook.getActiveWorksheet();
      
        // Get the used range in the sheet
        let usedRange = selectedSheet.getUsedRange();
      
        // Get the total number of columns in the used range
        let lastColumn = usedRange.getColumnCount();
      
        // Calculate the column where "Export_Date" will be written (one column to the right of the last column)
        let exportDateColumn = lastColumn + 1;
      
        // Set the header "Export_Date" in the first row of the export date column (Row 1, Column to the right)
        let exportDateHeaderCell = selectedSheet.getCell(0, exportDateColumn - 1); // Row 1 is index 0, columns are 0-indexed.
        exportDateHeaderCell.setValue("Export_Date");
      
        // Get the total number of rows in the used range
        let rowCount = usedRange.getRowCount();
      
        // Populate the "Export_Date" column with the extracted date for each row
        for (let i = 1; i < rowCount; i++) { // Starting from the second row (index 1)
          let exportDateCell = selectedSheet.getCell(i, exportDateColumn - 1); // Row i, same column
          exportDateCell.setValue(exportDate);
        }
      }
       
  5. Step 3 - Capture Data: The third Office Script extracts all the data from the Source table as a String[][] (2D array of strings), making it ready to be appended to the target file.

    • function main(workbook: ExcelScript.Workbook) {
        // Get the active worksheet
        let selectedSheet = workbook.getActiveWorksheet();
      
        // Get the table named "Table1"
        let sourceTable = selectedSheet.getTable("Table1");
      
        // Get the values of the entire table (excluding headers)
        let tableData = sourceTable.getRangeBetweenHeaderAndTotal().getValues();
      
        // Return the table data as output
        return tableData;  // This returns a 2D array (array of arrays)
      }
      
       
  6. Step 4 - Append to Target: The final Office Script appends this data in a for each loop from the Source to an existing Excel file in OneDrive (let's call this the Target file). The Target table contains the same column headers as the Source, ensuring consistency. The data is stacked vertically, row by row.

    • function main(workbook: ExcelScript.Workbook, data: string[][]) {
        // Get the active worksheet
        let selectedSheet = workbook.getActiveWorksheet();
      
        // Get the destination table named "Table1"
        let targetTable = selectedSheet.getTable("Table1");
      
        // Get the number of columns in the target table by checking the header row range
        let targetColumnCount: number = targetTable.getHeaderRowRange().getColumnCount();
      
        // Append each row of data to the target table, adjusting the number of columns if needed
        data.forEach(row => {
          // Trim the row to match the target column count if it has more columns
          let adjustedRow = row.slice(0, targetColumnCount);
      
          // Append the row to the table
          targetTable.addRow(-1, adjustedRow); // -1 adds the row at the end of the table
        });
      }
      
 

Issue: Flow Timeout and Performance Bottleneck

However, I am facing issues regarding the time it takes for the flow to finish, especially with larger files. Sometimes, the flow times out around the 17-minute mark, causing delays and incomplete executions. This is especially problematic when processing bigger files with a significant number of rows.

Categories:
I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 538 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 405 Moderator

#3
abm abm Profile Picture

abm abm 252 Most Valuable Professional

Last 30 days Overall leaderboard