Skip to main content
Power Automate
    • Robotic process automation
    • Power Automate Desktop
    • Process advisor
    • AI Builder
    • Connectors
    • Templates
  • Pricing
    • Blog
    • Documentation
    • Roadmap
    • Self-paced learning
    • Webinar
    • Overview
    • Issues
    • Give feedback
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • Register
    • ·
    • Sign In
    • ·
    • Help
    Go To
    • Microsoft Power Automate Community
    • Community News
    • News & Announcements
    • Get Help with Power Automate
    • General Power Automate Discussion
    • Connecting To Data
    • Building Flows
    • Using Flows
    • Power Automate Desktop
    • AI Builder
    • Power Automate Mobile App
    • Translation Quality Feedback
    • Share, Chat, Discuss
    • Community Chat & Announcements
    • Power Automate Cookbook
    • Off Topic Discussion
    • Power Platform Integration - Better Together!
    • Power Platform Integrations
    • Power Platform and Dynamics 365 Integrations
    • Watch & Share Videos
    • Webinars and Video Gallery
    • MBAS Gallery 2019
    • MBAS Gallery 2020
    • Give Feedback & Share Ideas
    • Power Automate Ideas
    • Report Issues
    • I Found A Bug
    • Find & Join Local User Groups
    • Power Automate User Groups
    • Read & Share Blogs
    • Power Automate Community Blog
    • Community Blog Topic Suggestions
    • Community Support
    • Community Accounts & Registration
    • Using the Community
    • Community Feedback
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power Automate Community
    • Forums
    • Share, Chat, Discuss
    • Power Automate Cookbook
    • Cross Reference and formatting two Excel files usi...

    Cross Reference and formatting two Excel files using Office Scripts

    10-25-2020 17:42 PM

    Microsoft sumurthy
    Microsoft
    417 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    sumurthy
    Microsoft sumurthy
    Microsoft
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    Cross Reference and formatting two Excel files using Office Scripts

    ‎10-25-2020 05:42 PM

    This article describes how two Excel files can be used for cross-refenced and formatted using Office Scripts. 

     

    The flow achieves this - 

     

    1. Extracts event master (key) data from Event.xlsx using one Run script action. 

    2. Passes that data to second Excel file containing event transaction data and uses that data to do basic validation of data and formatting of missing or incorrect data using Office Scripts. 

    3. Passes the result to an email. 

     

    See the flow in action below. 

    Download attachment to get Excel files and Office Scripts used. Construct PA flow on your own. 

     

    PA 2 Excel files.pngOS.png

     

     

    Return Unique Values 

    function main(workbook: ExcelScript.Workbook): EventData[] {
        let table = workbook.getWorksheet('Keys').getTables()[0];
        let range = table.getRangeBetweenHeaderAndTotal();
        let rows = range.getValues();
        let records: EventData[] = [];
        for (let row of rows) {
            let [event, date, location, capacity] = row;
            records.push({
                event: event as string,
                date: date as number, 
                location: location as string,
                capacity: capacity as number
            })
        }
        console.log(JSON.stringify(records))
        return records;
    }
    
    interface EventData {
        event: string
        date: number
        location: string
        capacity: number
    }

     

    Format missing data

    function main(workbook: ExcelScript.Workbook, keys: string): string {
    
      // Needed for testing purpose. Override the keys
      // keys = `[{"event":"E123","date":43892,"location":"Montgomery","capacity":10},{"event":"E124","date":43892,"location":"Juneau","capacity":15},{"event":"E125","date":43897,"location":"Phoenix","capacity":15},{"event":"E126","date":43914,"location":"Boise","capacity":25},{"event":"E127","date":43918,"location":"Salt Lake City","capacity":20},{"event":"E128","date":43938,"location":"Fremont","capacity":3},{"event":"E129","date":43938,"location":"Vancouver","capacity":50}]`;
    
      let table = workbook.getWorksheet('Transactions').getTables()[0];
      let range = table.getRangeBetweenHeaderAndTotal();
      range.clear(ExcelScript.ClearApplyTo.formats);
    
      let overallMatch = true;
    
      table.getColumnByName('Date').getRangeBetweenHeaderAndTotal().setNumberFormatLocal("yyyy-mm-dd;@");
      table.getColumnByName('Capacity').getRangeBetweenHeaderAndTotal().getFormat()
        .setHorizontalAlignment(ExcelScript.HorizontalAlignment.center);
      let rows = range.getValues();
      let keysObject = JSON.parse(keys) as EventData[];
      for (let i=0; i < rows.length; i++){
        let row = rows[i];
        let [event, date, location, capacity] = row;
        let match = false;
        // console.log("Currently on event ID " + event + " row: " + i );
        for (let keyObject of keysObject){
          // console.log("Comparing: " + event + " with master event record: " + keyObject.event);
          if (keyObject.event === event) {
            match = true;
            if (keyObject.date !== date) {
              overallMatch = false;
              range.getCell(i, 1).getFormat()
                .getFill()
                .setColor("FFFF00");
            }
            if (keyObject.location !== location) {
              overallMatch = false;
              range.getCell(i, 2).getFormat()
                .getFill()
                .setColor("FFFF00");
            }
            if (keyObject.capacity !== capacity) {
              overallMatch = false;
              range.getCell(i, 3).getFormat()
                .getFill()
                .setColor("FFFF00");
            }   
            break;             
          }
        }
        if (!match) {
          overallMatch = false;
          range.getCell(i, 0).getFormat()
            .getFill()
            .setColor("FFFF00");      
        }
    
      }
      let returnString = "All the data is in the right order.";
      if (overallMatch === false) {
        returnString = "Mismatch found. Data requires your review.";
      }
      console.log("Returning: " + returnString);
      return returnString;
    }
    
    interface EventData {
      event: string
      date: number
      location: string
      capacity: number
    }

    watch?v=dVwqBf483qo

    2 Excel Files in PA.zip
    Labels:
    • Labels:
    • Business process flows
    • Solutions
    Message 1 of 2
    417 Views
    2 Kudos
    Reply
    • All posts
    • Previous Topic
    • Next Topic
    Hongbo-Miao
    Hongbo-Miao
    New Member
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎10-26-2020 02:05 PM

    Nice work! Thanks for sharing : )

    Message 2 of 2
    349 Views
    1 Kudo
    Reply

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Automate
    • Power Virtual Agents
    • Sign up free
    • Sign in

    Browse

    • Templates
    • Connectors
    • Partners

    Downloads

    • Mobile
    • Gateway

    Learn

    • Documentation
    • Learn
    • Support
    • Community
    • Give feedback
    • Blog
    • Pricing
    • © 2021 Microsoft
    • Twitter
    • RSS Feed
    • YouTube
    • Contact us
    • Trademarks
    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Terms & conditions