Satır ve Sütunları Otomatik Boyutlandırma Node.js ve C++ ile
Otomatik Uydurma
Aspose.Cells, Microsoft Excel dosyasını temsil eden bir Workbook sınıfı sağlar. Workbook sınıfı, bir Excel dosyasındaki her çalışma sayfasına erişim sağlayan bir Workbook.getWorksheets() koleksiyonu içerir. Bir çalışma sayfası Worksheet sınıfı ile temsil edilir. Worksheet sınıfı, bir çalışma sayfasını yönetmek için geniş bir özellik ve yöntem yelpazesi sunar. Bu makale, Worksheet sınıfını kullanarak satır veya sütunları otomatik sığdırmaya bakıyor.
Satırı Otomatik Uydurma - Basit
Bir satırın genişliği ve yüksekliğini otomatik boyutlandırmanın en basit yolu, Worksheet sınıfı autoFitRow metodunu çağırmaktır. autoFitRow yöntemi, yeniden boyutlandırılacak satırın satır indeksini parametre olarak alır.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");
// Reading the Excel file into a buffer
const fs = require("fs");
const fileBuffer = fs.readFileSync(inputPath);
// Opening the Excel file through the buffer
const workbook = new AsposeCells.Workbook(fileBuffer);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the 3rd row of the worksheet
worksheet.autoFitRow(1);
// Saving the modified Excel file
const outputPath = path.join(dataDir, "output.xlsx");
workbook.save(outputPath);
Hücre Aralığında Satır Otomatik Sığdırma
Bir satır, birçok sütundan oluşur. Aspose.Cells, satırdaki hücre aralığındaki içeriğe göre satırı otomatik sığdırmak için autoFitRow metodunun aşırı yüklenmiş bir versiyonunu çağırmaya izin verir. Aşağıdaki parametreleri alır:
- Satır dizini, otomatik olarak uyarlama yapılacak satırın dizini.
- İlk sütun dizini, satırın ilk sütununun dizini.
- Son sütun dizini, satırın son sütununun dizini.
autoFitRow metodu, satırdaki tüm sütunların içeriğini kontrol eder ve ardından satırı otomatik sığdırır.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");
// Reading the Excel file into a buffer
const fs = require("fs");
const fileData = fs.readFileSync(inputPath);
// Opening the Excel file through the buffer
const workbook = new AsposeCells.Workbook(fileData);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the 3rd row of the worksheet
worksheet.autoFitRow(1, 0, 5);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.xlsx"));
Hücre Aralığında Sütun Otomatik Sığdırma
Bir sütun, birçok satırdan oluşur. Bir sütunu, sütun içindeki hücre aralığındaki içeriğe göre otomatik sığdırmak mümkündür; bunun için autoFitColumn metodunun aşırı yüklenmiş bir versiyonu çağrılır ve aşağıdaki parametreleri alır:
- Sütun dizini, otomatik olarak sığdırılacak sütunun dizini.
- İlk satır indeksi, sütunun ilk satırının indeksi.
- Son satır indeksi, sütunun son satırının indeksi.
autoFitColumn metodu, sütundaki tüm satırların içeriğini kontrol eder ve sonra sütunu otomatik sığdırır.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");
// Creating a file stream containing the Excel file to be opened
const fs = require("fs");
const workbook = new AsposeCells.Workbook(fs.readFileSync(inputPath));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the Column of the worksheet
worksheet.autoFitColumn(4);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.xlsx"));
Birleştirilmiş Hücreler İçin Satırları Otomatik Uydurma
Aspose.Cells kullanılarak, AutoFitterOptions API’si kullanılarak birleştirilmiş hücreler için bile satırları otomatik sığdırmak mümkündür. AutoFitterOptions sınıfı, birleştirilmiş hücreler için satırları otomatik sığdırmak amacıyla kullanılabilecek AutoFitterOptions.getAutoFitMergedCellsType() özelliği sağlar. AutoFitterOptions.getAutoFitMergedCellsType(), aşağıdaki üyeleri içeren AutoFitMergedCellsType yinelemeli nesnesini kabul eder.
- Hiçbiri: Birleştirilmiş hücreleri yoksay.
- FirstLine: Sadece ilk satırın yüksekliğini artırır.
- LastLine: Sadece son satırın yüksekliğini artırır.
- EachLine: Her satırın yüksekliğini artırır.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const outputDir = path.join(dataDir, "output");
// Instantiate a new Workbook
const wb = new AsposeCells.Workbook();
// Get the first (default) worksheet
const worksheet = wb.getWorksheets().get(0);
// Create a range A1:B1
const range = worksheet.getCells().createRange(0, 0, 1, 2);
// Merge the cells
range.merge();
// Insert value to the merged cell A1
worksheet.getCells().get(0, 0).setValue("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");
// Create a style object
const style = worksheet.getCells().get(0, 0).getStyle();
// Set wrapping text on
style.setIsTextWrapped(true);
// Apply the style to the cell
worksheet.getCells().get(0, 0).setStyle(style);
// Create an object for AutoFitterOptions
const options = new AsposeCells.AutoFitterOptions();
// Set auto-fit for merged cells
options.setAutoFitMergedCellsType(AsposeCells.AutoFitMergedCellsType.EachLine);
// Autofit rows in the sheet (including the merged cells)
worksheet.autoFitRows(options);
// Save the Excel file
wb.save(path.join(outputDir, "AutofitRowsforMergedCells.xlsx"));
Ayrıca, seçimli satır/sütun aralığını ve AutoFitterOptions örneğini kabul eden autoFitRows ve autoFitColumns metodlarının aşırı yüklenmiş versiyonlarını kullanarak istenilen AutoFitterOptions ile seçili satır/sütunları otomatik sığdırmak için deneyebilirsiniz.
Yukarıdaki metodların imzaları aşağıdaki gibidir:
- autoFitRows(int startRow, int endRow, AutoFitterOptions options)
- autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)