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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Dynamic sort inside ne...
Power Automate
Unanswered

Dynamic sort inside new excel sheet that get created using Script

(0) ShareShare
ReportReport
Posted on by 2,015 Season of Giving Solutions 2025
I have this Excel script which get called from power automate:-
 
function main(workbook: ExcelScript.Workbook) {
  const templateName = 'TemplateDoNotDelete';
  const template = workbook.getWorksheet(templateName);
  if (!template) {
    throw new Error(`Template sheet was not found.`);
  }
  // Get current date in Pacific Time
  const pacificDate = new Date(
    new Date().toLocaleString("en-US", { timeZone: "America/Los_Angeles" })
  );
  const day = pacificDate.getDate().toString().padStart(2, '0');
  const month = (pacificDate.getMonth() + 1).toString().padStart(2, '0');
  const year = pacificDate.getFullYear().toString();
  const newSheetName = `${day}.${month}.${year}`;
  // Delete existing sheet with same name, if found
  const existingSheet = workbook.getWorksheet(newSheetName);
  if (existingSheet) {
    existingSheet.delete();
  }
  // Copy template
  const copiedSheet = template.copy(
    ExcelScript.WorksheetPositionType.after,
    template
  );
  // Rename copied sheet
  copiedSheet.setName(newSheetName);
  // Get tables from new sheet
  const tables = copiedSheet.getTables();
  const tableNames = tables.map(t => t.getName());
  return {
    worksheetName: copiedSheet.getName(),
    tableNames: tableNames
  };
}
the original sheet named "TemplateDoNotDelete" has 3 tables with columns been sorted, but when the script creates the new sheet and i added some data using powerautomate flow, the column will not be sorted dynamically as the data get added,, instead i need to manually click on sort.. can this be fixed? to allow the new sheet to respect the sort setting and apply dynamic sorting?
 
Thanks
 
 
I have the same question (0)
  • Vish WR Profile Picture
    3,748 on at
    Hi 
    The issue is that Excel's AutoFilter copies the configuration but doesn't actively apply sort rules to newly added data. You need to reapply the sort settings after copying the template sheet.
     
    Try adding this code to your script after renaming the copied sheet:
     

    ----------------------*****----------------------
    // Reapply sort settings from template to copied tables
    const templateTables = template.getTables();
    templateTables.forEach((templateTable, index) => {
      const copiedTable = tables[index];
      if (copiedTable && templateTable) {
        const templateSort = templateTable.getSort();
        const copiedSort = copiedTable.getSort();
        const sortFields = templateSort.getFields();
        if (sortFields.length > 0) {
          copiedSort.apply(sortFields);
        }
      }
    });
    ----------------------*****----------------------
     
    function main(workbook: ExcelScript.Workbook) {
    const templateName = 'TemplateDoNotDelete';
    const template = workbook.getWorksheet(templateName);
    if (!template) {
    throw new Error(`Template sheet was not found.`);
    }
    // Get current date in Pacific Time
    const pacificDate = new Date(
    new Date().toLocaleString("en-US", { timeZone: "America/Los_Angeles" })
    );
    const day = pacificDate.getDate().toString().padStart(2, '0');
    const month = (pacificDate.getMonth() + 1).toString().padStart(2, '0');
    const year = pacificDate.getFullYear().toString();
    const newSheetName = `${day}.${month}.${year}`;
    // Delete existing sheet with same name, if found
    const existingSheet = workbook.getWorksheet(newSheetName);
    if (existingSheet) {
    existingSheet.delete();
    }
    // Copy template
    const copiedSheet = template.copy(
    ExcelScript.WorksheetPositionType.after,
    template
    );
    // Rename copied sheet
    copiedSheet.setName(newSheetName);
    // Get tables from new sheet
    const tables = copiedSheet.getTables();
    const tableNames = tables.map(t => t.getName());
     
    // ⭐ NEW CODE ADDED BELOW ⭐
    // Reapply sort settings from template to copied tables
    const templateTables = template.getTables();
    templateTables.forEach((templateTable, index) => {
    const copiedTable = tables[index];
    if (copiedTable && templateTable) {
    const templateSort = templateTable.getSort();
    const copiedSort = copiedTable.getSort();
     
    // Copy sort fields from template
    const sortFields = templateSort.getFields();
    if (sortFields.length > 0) {
    copiedSort.apply(sortFields);
    }
    }
    });
    // ⭐ NEW CODE ADDED ABOVE ⭐
     
    return {
    worksheetName: copiedSheet.getName(),
    tableNames: tableNames
    };
    }

    Note
    If your Power Automate flow adds data to the tables, create a second script that runs after data insertion and reapply the sorts again using the same code above. This ensures new rows respect the sort rules dynamically
     
     
     
      Vishnu WR
     
    Please  Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like 
     
     
     

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Valantis Profile Picture

Valantis 377

#2
11manish Profile Picture

11manish 279

#3
David_MA Profile Picture

David_MA 234 Super User 2026 Season 1

Last 30 days Overall leaderboard