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.
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"'.
- NikolinoDEJun 10, 2023Gold Contributor
In Excel Office Scripts, the save and close methods are not available for workbooks. Instead, the changes made to the workbook are automatically saved.
Regarding your question about performing the same actions across multiple sheets, you can use the getWorksheets method to retrieve all the worksheets in a workbook. You can then iterate over the collection of worksheets and perform the desired actions on each sheet.
Here is an updated example that incorporates iterating over worksheets:
function main(workbook: ExcelScript.Workbook) { const sheets = workbook.getWorksheets(); // Iterate over each worksheet for (const sheet of sheets) { const sourceRange = sheet.getRange('A5:K10'); const destinationWorkbook = ExcelScript.Workbook.getCurrent(); const destinationSheet = destinationWorkbook.getWorksheet('<destination-worksheet-name>'); const destinationRange = destinationSheet.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 }); } }
Regarding the dynamic nature of the workbook's title, if the only difference is the date, you can use a wildcard character (*) in the file path to accommodate different versions of the workbook. For example:
const destinationWorkbook = ExcelScript.Workbook.open('https://.../Workbook_*');
Make sure to replace Workbook_* with the appropriate wildcard pattern, that matches the title of your workbook.
I apologize for any confusion caused. I hope that I did not end up turning everything into potato salad :).
That's what happens when you want to excel with VBA, Office Script and Java Script at the same time.... the result is potato salad :)).
- SergeiBaklanJun 09, 2023Diamond Contributor
NikolinoDE , how did you do that? I was sure it's not possible to open another Excel file using ExcelScript package in Office Script.
- NikolinoDEJun 10, 2023Gold ContributorYou are correct that the ExcelScript.Workbook.open method is not available in Excel Office Scripts.
In Excel Office Scripts, you can only access and manipulate the workbook that the script is running on. It does not have the capability to directly open or interact with other Excel workbooks.
If you need to migrate data from one workbook to another using Excel Office Scripts, you would typically have to read the data from the source workbook, perform any necessary transformations or processing, and then write the data to the destination workbook within the same script.
I apologize for the incorrect information in my previous response.
Thank you for pointing it out :).