使用 C++ 在工作表中检测合并单元格 Node.js 通过 C++
Contents
[
Hide
]
本文介绍如何在工作表中获取合并的单元区域。
Aspose.Cells for Node.js via C++ 允许你获取工作表中的合并区域,还可以取消合并(拆分)它们。本文展示了使用 Aspose.Cells API 执行此任务的最简代码。
该组件提供Cells.getMergedAreas()方法,可以获取所有合并的单元格。以下示例代码演示了如何检测工作表中的合并单元格。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "SampleInput.xlsx");
// Instantiate a new Workbook
// Open an existing excel file
const wkBook = new AsposeCells.Workbook(filePath);
// Get a worksheet in the workbook
const wkSheet = wkBook.getWorksheets().get("Sheet2");
// Clear its contents
wkSheet.getCells().clear();
// Get merged areas
const areas = wkSheet.getCells().getMergedAreas();
// Check if areas is null or not
if (!areas || areas.length === 0) {
console.warn("No merged areas to unmerge.");
return;
}
// Define some variables
let frow, fcol, erow, ecol, trows, tcols;
// Loop through the arraylist and get each cellarea
// To unmerge it
for (let i = 0; i < areas.length; i++) {
frow = areas[i].startRow;
fcol = areas[i].startColumn;
erow = areas[i].endRow;
ecol = areas[i].endColumn;
trows = erow - frow + 1;
tcols = ecol - fcol + 1;
wkSheet.getCells().unMerge(frow, fcol, trows, tcols);
}
const outputFilePath = path.join(dataDir, "MergeTrial.out.xlsx");
// Save the excel file
wkBook.save(outputFilePath);