Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide sheets if Data is empty - using template (SaveAsByTemplateAsync) #656

Open
1 of 4 tasks
Gilcemir opened this issue Aug 21, 2024 · 0 comments
Open
1 of 4 tasks

Comments

@Gilcemir
Copy link

Gilcemir commented Aug 21, 2024

Excel Type

  • XLSX
  • XLSM
  • CSV
  • OTHER

Upload Excel File

Template.xlsx

MiniExcel Version

1.32.1

Description

Please attach your issue file by dragging or droppng, selecting or pasting them.

Hi,
Im trying to hide sheets if the data is empty.
For example, for the sheet "Mensalidades", if there's nothing to fill the template sheet, I want to hide it.

I did something like this:

        var configuration = new OpenXmlConfiguration()
        {
            IgnoreTemplateParameterMissing = true
        };
        var sheets = HideSheetsIfEmpty(eventData);
        if(sheets.Any())
        {
            configuration.DynamicSheets = sheets;
        }
                await using var memoryStream = new MemoryStream();
        await memoryStream.SaveAsByTemplateAsync(template, value, configuration);
        memoryStream.Seek(0, SeekOrigin.Begin);

        return memoryStream.ToArray();

where:

    private DynamicExcelSheet[] HideSheetsIfEmpty(TransferReportCreatedArgs eventData)
    {
        var sheets = new List<DynamicExcelSheet>();
        if (!eventData.Adjusts.Any())
        {
            sheets.Add(new DynamicExcelSheet(sheetNames["adjust"]) { Name = sheetNames["adjust"], State = SheetState.Hidden });
        }
        if (!eventData.Mensals.Any())
        {
            sheets.Add(new DynamicExcelSheet(sheetNames["mensal"]) { Name = sheetNames["mensal"], State = SheetState.Hidden });
        }
        if (!eventData.Retentions.Any())
        {
            sheets.Add(new DynamicExcelSheet(sheetNames["retention"]) { Name = sheetNames["retention"], State = SheetState.Hidden });
        }
        if (!eventData.OtherProducts.Any())
        {
            sheets.Add(new DynamicExcelSheet(sheetNames["other"]) { Name = sheetNames["other"], State = SheetState.Hidden });
        }
        if (!eventData.Gateways.Any())
        {
            sheets.Add(new DynamicExcelSheet(sheetNames["educbankgateway"]) { Name = sheetNames["educbankgateway"], State = SheetState.Hidden });
        }
        return sheets.ToArray();
    }

Is it possible in any way? Or this configuration is only available for non-template methods?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant