Forum Discussion
Use Script to Migrate Excel Date
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"'.
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.
- PhishdawgJun 10, 2023Brass ContributorJust so I don't spin my wheels needlessly, as I am the ignorant one in this conversation...
My assigned task is to develope an automatic and recurring way to copy multiple rows of data - that are not formatted as a table, from one Excel workbook and save it in another as a table.
Can I achieve this task using Excel Scripts and Power Automate, without having to physically open and close the source workbook - everything should happen without manual assistance or intervention, is this possible.
Thank You, both, for your assistance.- SergeiBaklanJun 11, 2023Diamond Contributor
That's an idea.
1) Using any file create ExcelScript which combines data within workbook as required
2) In Power Automate create the flow which is triggered when new file is appeared in SharePoint folder or like, which trigger is more close to your business process. Next flow steps are
3) Delete existing master file
4) Copy that new file (we shall have it's ID on previous step) on another folder a master file
5) Run prepared script on that file
6) Remove appeared new file
If master file shall be updated with new information, not replaced, perhaps create two ExcelScripts, one as above and another one updates excel with csv text.
Again, above is only idea, evil could be in details.