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.
- PhishdawgJun 08, 2023Brass Contributor
NikolinoDE
I'm getting close, but I believe I'm chasing my tail at this point,
I've edited your example (I wasn't able to get you a shot of mine, yours shown below), and I'm getting errors at all the spots circled in red.
I don't believe, as I wasn't sure how, that I 'activated' the destination workbook.
Everything else in accordance with your instructions, I have edited, with no errors occurring.
Code Editor
Output (1)
See Line 4, column7: Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.
See Line 5, column7: Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.
Problems (8)
[4, 41] Property 'Workbook' does not exist on type 'typeof ExcelScript'.
[8, 171] Property 'copyTo' does not exist on type 'Range'.
[9, 32] Property 'PasteType' does not exist on type 'typeof ExcelScript'.
[10, 37] Property 'PasteOperation' does not exist on type 'typeof ExcelScript'.
[16, 51] Property 'save' does not exist on type '"My Destination Workbook Name Here"'.
[17, 49] Property 'close' does not exist on type '"My Destination Workbook Name Here"'.
[4, 7] Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.
[5, 7] Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.- NikolinoDEJun 09, 2023Gold Contributor
unplugged...not tested.
function main(workbook: ExcelScript.Workbook) { // Define the source and destination ranges const sourceWorksheet = workbook.getWorksheet('Sheet1'); const sourceRange = sourceWorksheet.getRange('A5:K10'); const destinationWorkbook = ExcelScript.Workbook.open('<destination-workbook-path>'); const destinationWorksheet = destinationWorkbook.getWorksheet('<destination-worksheet-name>'); const destinationRange = destinationWorksheet.getRange('<destination-cell-range>'); // Copy data from source to destination sourceRange.copyFrom(sourceRange, { completeFormats: true, skipBlanks: false, transpose: false }); destinationRange.copyFrom(sourceRange, { completeFormats: true, skipBlanks: false, transpose: false }); // Save and close the destination workbook destinationWorkbook.save(); destinationWorkbook.close(); }
You may need to adjust the code accordingly based on the available Excel Office Scripts capabilities.
- PhishdawgJun 09, 2023Brass Contributor
Each workbook has three sheets that I need to perform the same actions across the same cell range.
I found guidance about ""collections" -
let sheets = workbook.getWorksheets()
How would I incorporate this additional task of 'collecting' all data from all three sheets of the workbook?
Also, is the script 'dynamic'? There are new versions of the workbook several times a week - the only difference in the title being the date, does the script account for the this anomaly?
I got it to this; Still getting errors at the end.
Output 1 -Line 24: destinationWorkbook.save is not a function
[24, 25] Property 'save' does not exist on type '"https://......my.sharepoint.com/:x:/g/personal/....._onmicrosoft_com/EfykGyBjVwhMhJO1voXK4qoB42a2i-bgPabQWuQZWIfELA?e=9RvJNy"'.
[25, 25] Property 'close' does not exist on type '"https://......-my.sharepoint.com/:x:/g/personal/....._onmicrosoft_com/EfykGyBjVwhMhJO1voXK4qoB42a2i-bgPabQWuQZWIfELA?e=9RvJNy"'.
- PhishdawgJun 08, 2023Brass ContributorFantastic detail and content, thank you so much.
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?