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 / Using Script to copy t...
Power Automate
Unanswered

Using Script to copy to multiple files in a folder

(0) ShareShare
ReportReport
Posted on by 15

Sorry if this is the wrong forum I got redirected to post my question on Scripts here by the Excel team

 

I have a large number of files for individual clients that periodically need to be updated with the same new content. I am trying to create a script which loops through and opens each file within a specific folder and copies the content from a masterfile into each file held in the folder.

 

The files are saved in a sharepoint folder.

 

I am getting the following error

 

See line 8, column 7: Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.

 

This is the line of code I have used

 

 let folder: ExcelScript.DirectoryObject = workbook.getDirectoryObjectFromUrl(folderUrl);

Categories:
  • MichelleRanMSFT Profile Picture
    Microsoft Employee on at

    Hi @MFM0870, thanks for your question! If you're writing an Office Script, then one thing you should be aware of is that a script can only modify the workbook that it's being run in - it can't modify multiple files. To do what you're describing, you'd probably want to use an Office Script together with a Power Automate flow. Something along the lines of: list files in folder -> apply to each -> run script on each file. If you can describe your scenario in more detail, I'd be happy to give more guidance on how to set up such a flow.

  • MFM0870 Profile Picture
    15 on at

    Hi Michelle 

    I have around 90 files that have an identical format. I would like to copy the same data from a range of cells in a masterfile to each file. If Power Automate can open each file that would be great. 

     

  • MichelleRanMSFT Profile Picture
    Microsoft Employee on at

    You can try a flow like this (derived from the "Run an Office Script on every Excel workbook in a SharePoint folder" template):

    MichelleRanMSFT_0-1694030330378.png

    MichelleRanMSFT_1-1694030356525.png

     

    This is the first script to get the range from the masterfile:

    function main(workbook: ExcelScript.Workbook, worksheet: string, rangeToCopy: string) {
     return JSON.stringify(workbook.getWorksheet(worksheet).getRange(rangeToCopy).getValues());
    } 

    This is the second script to copy the range:

    function main(workbook: ExcelScript.Workbook, rangeValues: string, worksheet: string, rangeToCopyTo: string) {
     let values: (string | number | boolean)[][] = JSON.parse(rangeValues);
     workbook.getWorksheet(worksheet).getRange(rangeToCopyTo).setValues(values);
    }

     Let me know if that helps!

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 Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 445

#2
11manish Profile Picture

11manish 345 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 300

Last 30 days Overall leaderboard