Forum Discussion
Use Script to Migrate Excel Date
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.
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 10, 2023Diamond Contributor
IMHO, if we speak about combining sheets within same workbook it doesn't matter what is the name of the workbook. Script is applied to opened workbook or one is initiated in Power Automate.
- 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 :).- SergeiBaklanJun 10, 2023Diamond Contributor
Hi Nikolino and thank you for the feedback. I'm not very deep in scripting, but perhaps we need to adjust terminology to avoid confusing.
We have Office Scripts API and access to it in Office Scripts in Excel through ExcelScript package to work with Excel file. The latest always requires
function main( myworkbook: ExcelScript.workbook)
at least which returns to the script opened workbook object. Thus with such script we may work only with current workbook. Within such script there is no access to other workbooks.
We speak about Automate. Script Lab is different story. Another way to work with different workbooks is Power Automate which could perform ExcelScript on currently defined workbook.
Thus to pass data from one workbook to another we shall to use Power Automate or, perhaps, Script Lab, or other tools like Power Shell.