コード例 #1
0
        public AddIn(InteropApplication application, IProgram program, IRibbon ribbon)
        {
            this.Application = application;
            this.Program     = program;
            this.Ribbon      = ribbon;

            this.workbookByInteropWorkbook = new Dictionary <InteropWorkbook, Workbook>();

            ((InteropAppEvents_Event)this.Application).NewWorkbook += async interopWorkbook =>
            {
                if (!string.IsNullOrWhiteSpace(this.ExistentialAttribute))
                {
                    var customProperties = new CustomProperties(interopWorkbook.CustomDocumentProperties);
                    if (!customProperties.Exist(this.ExistentialAttribute))
                    {
                        return;
                    }
                }

                var workbook = this.New(interopWorkbook);
                for (var i = 1; i <= interopWorkbook.Worksheets.Count; i++)
                {
                    var interopWorksheet = (InteropWorksheet)interopWorkbook.Worksheets[i];
                    workbook.New(interopWorksheet);
                }

                var worksheets = workbook.Worksheets;
                await this.Program.OnNew(workbook);

                foreach (var worksheet in worksheets)
                {
                    await program.OnNew(worksheet);
                }
            };

            this.Application.WorkbookOpen += async interopWorkbook =>
            {
                if (!string.IsNullOrWhiteSpace(this.ExistentialAttribute))
                {
                    var customProperties = new CustomProperties(interopWorkbook.CustomDocumentProperties);
                    if (!customProperties.Exist(this.ExistentialAttribute))
                    {
                        return;
                    }
                }

                var workbook = this.New(interopWorkbook);
                for (var i = 1; i <= interopWorkbook.Worksheets.Count; i++)
                {
                    var interopWorksheet = (InteropWorksheet)interopWorkbook.Worksheets[i];
                    workbook.New(interopWorksheet);
                }

                var worksheets = workbook.Worksheets;
                await this.Program.OnNew(workbook);

                foreach (var worksheet in worksheets)
                {
                    await program.OnNew(worksheet);
                }
            };

            this.Application.WorkbookActivate += interopWorkbook =>
            {
                if (!string.IsNullOrWhiteSpace(this.ExistentialAttribute))
                {
                    var customProperties = new CustomProperties(interopWorkbook.CustomDocumentProperties);
                    if (!customProperties.Exist(this.ExistentialAttribute))
                    {
                        return;
                    }
                }

                if (!this.WorkbookByInteropWorkbook.TryGetValue(interopWorkbook, out var workbook))
                {
                    workbook = this.New(interopWorkbook);
                }

                workbook.IsActive = true;
            };

            this.Application.WorkbookDeactivate += wb =>
            {
                // Could already be gone by the WorkbookBeforeClose event
                if (this.WorkbookByInteropWorkbook.TryGetValue(wb, out _))
                {
                    this.WorkbookByInteropWorkbook[wb].IsActive = false;
                }
            };

            void WorkbookBeforeClose(InteropWorkbook interopWorkbook, ref bool cancel)
            {
                if (this.WorkbookByInteropWorkbook.TryGetValue(interopWorkbook, out var workbook))
                {
                    this.Program.OnClose(workbook, ref cancel);
                    if (!cancel)
                    {
                        this.Close(interopWorkbook);
                    }
                }
            }

            this.Application.WorkbookBeforeClose += WorkbookBeforeClose;
        }