Assegna Macro al Controllo modulo con JavaScript tramite C++
Contents
[
Hide
]
Aspose.Cells ti consente di assegnare un codice Macro a un Controllo Modulo come un Pulsante. Si prega di utilizzare la proprietà Shape.macroName per assegnare un nuovo codice Macro a un Controllo Modulo all’interno del workbook.
Il seguente esempio di codice crea un nuovo workbook, assegna un Codice Macro a un Pulsante di Modulo e salva l’output in formato XLSM. Una volta aperto il file XLSM in Microsoft Excel, vedrai il seguente codice macro.
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End SubAssegna Macro al Controllo modulo in JavaScript
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Add VBA Module and Button</title>
</head>
<body>
<h1>Add VBA Module and Button Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.xlsm,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (fileInput.files.length && fileInput.files[0]) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
var workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
var workbook = new Workbook();
}
// Accessing the first worksheet in the workbook
const sheet = workbook.worksheets.get(0);
// Adding a VBA module to the workbook tied to the sheet
const moduleIdx = workbook.vbaProject.modules.add(sheet);
const module = workbook.vbaProject.modules.get(moduleIdx);
module.codes = "Sub ShowMessage()\r\n" +
" MsgBox \"Welcome to Aspose!\"\r\n" +
"End Sub";
// Adding a button shape to the sheet
const button = sheet.shapes.addButton(2, 0, 2, 0, 28, 80);
button.placement = AsposeCells.PlacementType.FreeFloating;
button.font.name = "Tahoma";
button.font.isBold = true;
button.font.color = AsposeCells.Color.Blue;
button.text = "Aspose";
// Assigning the macro name to the button
button.macroName = sheet.name + ".ShowMessage";
// Saving the modified workbook as an XLSM file and providing a download link
const outputData = workbook.save(SaveFormat.Xlsm);
const blob = new Blob([outputData], { type: 'application/vnd.ms-excel.sheet.macroEnabled.12' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Output.out.xlsm';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified XLSM File';
document.getElementById('result').innerHTML = '<p style="color: green;">VBA module and button added successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>