Create addin file. - customize ribbon tab and associated VBA callback according to ui file - include VBA modules, e.g., general VBA subroutines for data transferring. Args: vba_only (bool, optional): Whether simple VBA addin (without Python related module
(self, vba_only:bool=False)
| 53 | |
| 54 | |
| 55 | def create(self, vba_only:bool=False): |
| 56 | '''Create addin file. |
| 57 | - customize ribbon tab and associated VBA callback according to ui file |
| 58 | - include VBA modules, e.g., general VBA subroutines for data transferring. |
| 59 | |
| 60 | Args: |
| 61 | vba_only (bool, optional): Whether simple VBA addin (without Python related modules). |
| 62 | Defaults to False. |
| 63 | ''' |
| 64 | N = 2 if vba_only else 3 |
| 65 | |
| 66 | # 1 create addin file |
| 67 | logging.info('(1/%d) Creating add-in structure...', N) |
| 68 | ui = UI(self.xlam_file) |
| 69 | template = os.path.join(RESOURCE_PATH, RESOURCE_ADDIN) |
| 70 | custom_ui = os.path.join(self.path, CUSTOM_UI) |
| 71 | ui.create(template, custom_ui) |
| 72 | |
| 73 | if not os.path.exists(self.xlam_file): |
| 74 | raise AddInException('Create add-in structures failed.') |
| 75 | |
| 76 | # 2 update VBA modules |
| 77 | vba = VBA(xlam_file=self.xlam_file, excel_app=self.excel_app) |
| 78 | |
| 79 | # 2.1 import ribbon module |
| 80 | logging.info('(2/%d) Creating menu callback subroutines...', N) |
| 81 | base_menu = os.path.join(RESOURCE_PATH, RESOURCE_VBA, f'{VBA_MENU}.bas') |
| 82 | user_menu = os.path.join(RESOURCE_PATH, RESOURCE_VBA, f'{VBA_USER_MENU}.bas') |
| 83 | vba.import_module(base_menu) |
| 84 | vba.import_module(user_menu) |
| 85 | |
| 86 | # extra steps for VBA-Python combined addin |
| 87 | if not vba_only: |
| 88 | logging.info('(3/%d) Creating Python-VBA interaction modules...', N) |
| 89 | |
| 90 | # 2. import general module |
| 91 | general_module = os.path.join(RESOURCE_PATH, RESOURCE_VBA, f'{VBA_GENERAL}.bas') |
| 92 | vba.import_module(general_module) |
| 93 | |
| 94 | # 3. copy main python scripts |
| 95 | if RESOURCE_PATH.upper()!=self.path.upper(): |
| 96 | python_scripts = os.path.join(RESOURCE_PATH, RESOURCE_PYTHON) |
| 97 | target_scripts = os.path.join(self.path, RESOURCE_PYTHON) |
| 98 | shutil.copytree(python_scripts, target_scripts) |
| 99 | |
| 100 | python_main = os.path.join(RESOURCE_PATH, PYTHON_MAIN) |
| 101 | python_config = os.path.join(RESOURCE_PATH, PYTHON_CONFIG) |
| 102 | shutil.copy(python_main, self.path) |
| 103 | shutil.copy(python_config, self.path) |
| 104 | |
| 105 | # save vba modules |
| 106 | vba.save() |
| 107 | |
| 108 | |
| 109 | def update(self): |
no test coverage detected