给窗体控件分配宏
Contents
[
Hide
]
Aspose.Cells for Python via .NET允许您将宏代码指定给表单控件(如按钮)。请使用Shape.macro_name属性为工作簿中的表单控件分配新的宏代码。
以下示例代码创建一个新的工作簿,为一个表单按钮分配宏代码,并将输出保存为XLSM格式。一旦你在Microsoft Excel中打开输出的XLSM文件,你将看到以下宏代码。
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End Sub
在Python中为表单控件赋予宏
以下是生成输出XLSM文件的示例代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import Workbook | |
from aspose.cells.drawing import PlacementType | |
from aspose.pydrawing import Color | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
if notpath.isdir(dataDir): | |
os.makedirs(dataDir) | |
workbook = Workbook() | |
sheet = workbook.worksheets[0] | |
moduleIdx = workbook.vba_project.modules.add(sheet) | |
module = workbook.vba_project.modules[moduleIdx] | |
module.codes = "Sub ShowMessage()" + "\r\n" + " MsgBox \"Welcome to Aspose!\"" + "\r\n" + "End Sub" | |
button = sheet.shapes.add_button(2, 0, 2, 0, 28, 80) | |
button.placement = PlacementType.FREE_FLOATING | |
button.font.name = "Tahoma" | |
button.font.is_bold = True | |
button.font.color = Color.blue | |
button.text = "Aspose" | |
button.macro_name = sheet.name + ".ShowMessage" | |
dataDir = dataDir + "Output.out.xlsm" | |
workbook.save(dataDir) |