Skip to content

Copy Worksheet and copy + rename the original named ranges in there #2427

@unimatrix27

Description

@unimatrix27

Read and complete the full issue template

Do not randomly delete sections. They are here for a reason.

Do you want to request a feature or report a bug?

  • [] Bug
  • Feature
  • Question

Did you test against the latest CI build?

  • Yes
  • No

Version of ClosedXML

0.104.1

What is the expected behavior or new feature?

I spend some hours playing with ideas trying to read the code inline documentation. I am trying to copy a worksheet from a handmade template that I later want to use for the Report extension.

Since the template eventually has to contain 2 sheets which are very similar, I want to copy 1 sheet and then manyally just change a view cells. Since the template system uses named ranges, I relalize I would have to copy them as well and give them both a different name.

Is there any way to do this without completely recreating them from scratch, identifying the original cell range from the original sheet?

my example shows just the basic idea. The original sheet "Anlagenspiegel" does have a named range "positions"

Code to reproduce problem:

        private string prepareTemplate(string templatepath)
        {
            var newTemplatePath = Path.Join(rootPathProvider.GetProjectRootPath(), "templates", "tempready.xlsx");
            using (var workbook = new XLWorkbook(templatepath))
            {
                workbook.Worksheets.TryGetWorksheet("Anlagenspiegel", out IXLWorksheet anlagenspiegel);
                anlagenspiegel!.CopyTo("Anlagen Handelsrecht", 2);
                anlagenspiegel.CopyTo("Anlagen Steuerrecht", 3);
                //anlagenspiegel.Delete();
                workbook.SaveAs(newTemplatePath);
            }

            return newTemplatePath;
        }
  • I attached a sample spreadsheet. (You can drag files on to this issue)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions