VBA kodlarını Yönetme — Excel Makro Etkin çalışma kitabı

Node.js kullanarak VBA Modülü ekleyin

Aşağıdaki örnek kod, yeni bir çalışma kitabı oluşturur ve yeni bir VBA Modülü ve Makro Kodu ekler ve çıktıyı XLSM formatında kaydeder. Çıktı XLSM dosyasını Microsoft Excel’de açıp Geliştirici > Görsel Basic menü komutlarına tıklarsanız, “TestModule” adlı bir modül göreceksiniz ve içinde aşağıdaki makro kodunu göreceksiniz.

Sub ShowMessage() {
    MsgBox "Welcome to Aspose!"
}

İşte VBA Modülü ve Makro Kodu ile çıktı XLSM dosyasını oluşturmak için örnek kod.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Create new workbook
const workbook = new AsposeCells.Workbook();

// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);

// Add VBA Module
const idx = workbook.getVbaProject().getModules().add(worksheet);

// Access the VBA Module, set its name and codes
const module = workbook.getVbaProject().getModules().get(idx);
module.setName("TestModule");

module.setCodes("Sub ShowMessage()" + "\r\n" +
"    MsgBox \"Welcome to Aspose!\"" + "\r\n" +
"End Sub");

// Save the workbook
workbook.save(path.join(dataDir, "output_out.xlsm"), AsposeCells.SaveFormat.Xlsm);

VBA veya Makro düzenle Node.js kullanarak

Aşağıdaki örnek kod, içeriğinde VBA veya Makro kodu bulunan kaynak Excel dosyasını yükler.

Sub Button1_Click() {
    MsgBox "This is test message."
}

Aspose.Cells örnek kodunun yürütülmesinden sonra, VBA veya Makro kodu bu şekilde değiştirilmiş olacaktır

Sub Button1_Click() {
    MsgBox "This is Aspose.Cells message."
}

Verilen bağlantılardan kaynak Excel dosyasını ve çıktı Excel dosyasını indirebilirsiniz.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create workbook object from source Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sample.xlsm"));

// Change the VBA Module Code
const modules = workbook.getVbaProject().getModules();
const moduleCount = modules.getCount();
for (let i = 0; i < moduleCount; i++) {
const module = modules.get(i);
const code = module.getCodes();
if (code.includes("This is test message.")) 
{
code = code.replace("This is test message.", "This is Aspose.Cells message.");
module.setCodes(code);
}
}


// Save the output Excel file
workbook.save(path.join(dataDir, "output_out.xlsm"));

Gelişmiş Konular