I’m using the spreadsheet from OnlyOffice (via the kDrive infomaniak), and I would like to know how to duplicate a whole worksheet using a macro.
The use case is very basic : I have a template worksheet, which contains a lot of elements, shapes, formulas, and so on, and once a month, I would like to create a new worksheet from this template sheet.
I’ve only managed to create a blank sheet and copy/paste its content in the new sheet, but it lack of the column sizing, the objects, etc.
I didn’t find any solution in the docs.
Did I miss something, or is this impossible to do for now ?
Unfortunately, there is no separate method to implement your scenario similar to Worksheet.Copy in Excel.
We need some time to discuss it internally. I will notify you when we have something to share.
We would be delighted if you could share your efforts in creating the macro with us.
I believe many others who have the same question would be grateful for your code.
I didn’t go very far in this, but I can share with you the beginning :
(function()
{
var oTechnicalSheet = Api.GetSheet("Technical");
// Get Sheet name from a technical sheet that contains month names (in my locale)
var sNewSheetName = oTechnicalSheet.GetRange("B5").GetValue2() + " " + oTechnicalSheet.GetRange("B6").GetValue2();
var oCurrentMonth = Api.GetSheet(sNewSheetName);
// If sheet don't exists, then create it
if(oCurrentMonth === null){
//Create sheet don't return the new sheet, it's a bit weird to me, but that's how it works
Api.AddSheet(sNewSheetName);
oCurrentMonth = Api.GetSheet(sNewSheetName);
// "Duplicate" templates
var oTemplateSheet = Api.GetSheet("Template");
oTemplateSheet.GetRange("A1:L72").Copy(oCurrentMonth.GetRange("A1:L72"));
oCurrentMonth.GetRange("A1").SetValue(oTechnicalSheet.GetRange("B3").GetValue2());
}
})();
I was about to try to recover the objects from the templates using GetAllShapes(), but then I realized that I needed to create a new of each kind using AddShape(), and that’s where I left my work : translate the shapes were to much of work regarding the “right clic → Copy” on the sheet once a month
I also planned to set the column width manually using SetColumnWidth() (no link for this one, I’m not allowed ), but as for above, it was a lot of work, for something that would break every time I update the template sheet.
We have submitted a proposal for the implementation of the Api.CopySheet method.
Unfortunately, the timeline for this implementation is currently unknown.
I tried to implement something similar to @NainzguL above, but found some very strange behavior. Api.AddSheet() does actually return a sheet, but it is not the sheet that is created, but the already-existing one directly preceding in the list of sheets. What’s more, if I try to reference the newly-added sheet by index + 1, the result returns the sheet following the newly-added sheet (i.e., the index of the existing sheet, had the new sheet not been created). Unlike in the example above, attempting to reference the newly-created sheet by name returns a null result. In short, it appears that it is not possible to work with the newly-created sheet within the same script.
I was pursuing this to try to resolve another error I had come across: it appears that if OnlyOffice attempts to open a workbook to a sheet that has been hidden, it produces an unrecoverable error and the sheet will have to be copied and the copy opened to return to a prior version of the workbook.
We checked this behavior using a simple scenario: created a new worksheet with Api.AddSheet(), then accessed it within the same macro run using the returned object, the sheet name, and its index. In our test, the issue was not reproduced — the newly created worksheet was available immediately and could be modified in the same script.
Could you please provide the following information so that we can investigate the issue further?
The complete macro code you are using.
The exact version of ONLYOFFICE Desktop Editors.
Please also check whether the behavior is reproduced in a newly created blank .xlsx file.
If the issue does not occur in a new file, please attach the original file where it occurs.