Forum Discussion
Use Script to Migrate Excel Date
You can use Excel Office Scripts to capture and move data from one workbook to another or to a SharePoint list. Excel Office Scripts provide automation capabilities within Excel, allowing you to perform tasks like data manipulation, data transfer, and more.
To migrate data from range A5:K10 in one workbook to another workbook or SharePoint list, you can follow these general steps:
- Open the source workbook containing the data you want to migrate.
- Go to the "Automate" tab in the Excel ribbon and click on "Record Script" to start recording your actions.
- Select the range A5:K10 that you want to capture and move.
- Copy the selected range to the clipboard (Ctrl+C).
- Open the destination workbook or SharePoint list where you want to paste the data.
- Activate the target worksheet or list where you want to paste the data.
- Paste the data from the clipboard (Ctrl+V).
- Stop recording the script.
Once you have recorded the script, you can modify and enhance it as needed. For example, you can add error handling, specify specific locations or formats for the target data, or perform additional data transformations.
Keep in mind that Excel Office Scripts require an Office 365 subscription and may have variations in functionality depending on the version and updates of Excel. Also, SharePoint list integration might have specific requirements and configuration.
Here's a ready-made script using Excel Office Scripts to migrate data from range A5:K10 in one workbook to another workbook:
function main(workbook: ExcelScript.Workbook) {
// Define the source and destination ranges
const sourceRange = workbook.getWorksheet('Sheet1').getRange('A5:K10');
const destinationWorkbook = ExcelScript.Workbook.open('<destination-workbook-path>');
const destinationRange = destinationWorkbook.getWorksheet('<destination-worksheet-name>').getRange('<destination-cell-range>');
// Copy data from source to destination
sourceRange.copyTo(destinationRange, {
pasteType: ExcelScript.PasteType.all,
pasteOperation: ExcelScript.PasteOperation.none,
skipBlanks: false,
transpose: false
});
// Save and close the destination workbook
destinationWorkbook.save();
destinationWorkbook.close();
}
Replace <destination-workbook-path>, <destination-worksheet-name>, and <destination-cell-range> with the appropriate values for your scenario.
To run this script, follow these steps:
- Open the source workbook that contains the data you want to migrate.
- Click on the "Automate" tab in the Excel ribbon and select "Script Lab" (if you have it installed) or "Record a Macro" to open the Office Scripts editor.
- In the Office Scripts editor, create a new script and replace the default content with the provided script.
- Update the placeholders in the script with the appropriate values.
- Click the "Run" button to execute the script and migrate the data.
Please note that you need to have the necessary permissions and access to both the source and destination workbooks for the script to run successfully.
Remember to test the script with sample data before using it on important files, and make sure to backup your files before performing any automated operations.
Two unique concerns. One, the source file comes in an email a few times a week and is moved from that inbox to the source OneDrive folder (or SharePoint library) manually by another.
The same folder/library is used each time, however there is a variation (the date) of the file name each time (e.a. 'Test_File (6-7-2023) one day and 'Test_File (6-8-2023)'.
The second, the desired data maybe two lines (header row and one new item) to many items, up to 20 rows. Can the script account for the two unique issues?