تحجيم تلقائي للصفوف والأعمدة باستخدام Node.js عبر C++

ضبط تلقائي

توفر Aspose.Cells فئة Workbook التي تمثل ملف Microsoft Excel. تحتوي فئة Workbook على مجموعة Workbook.getWorksheets() التي تتيح الوصول إلى كل ورقة عمل في ملف إكسل. تمثل ورقة العمل بالفئة Worksheet. توفر فئة Worksheet مجموعة واسعة من الخصائص والأساليب لإدارة ورقة العمل. يستعرض هذا المقال استخدام الفئة Worksheet للتحجيم التلقائي للصفوف أو الأعمدة.

ضبط تلقائي للصف - بسيط

أبسط طريقة لضبط عرض وارتفاع الصف تلقائيًا هي استدعاء طريقة autoFitRow لفئة Worksheet. تأخذ طريقة autoFitRow مؤشر صف (للصف الذي سيتم تغيير حجمه) كمعامل.

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);

كيفية ضبط صف تلقائيًا في مجموعة من الخلايا

يتكون الصف من العديد من الأعمدة. يسمح Aspose.Cells للمطورين بضبط صف تلقائيًا استنادًا إلى المحتوى في نطاق خلايا داخل الصف عن طريق استدعاء إصدار محمل زائد من طريقة autoFitRow. وهي تأخذ المعاملات التالية:

  • فهرس الصف, فهرس الصف المراد ضبطه تلقائياً.
  • فهرس العمود الأول, فهرس العمود الأول للصف.
  • فهرس العمود الأخير, فهرس العمود الأخير للصف.

تتحقق طريقة autoFitRow من محتويات جميع الأعمدة في الصف ثم تضبط الصف تلقائيًا.

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"));

كيفية ضبط العمود تلقائيًا في مجموعة من الخلايا

العمود يتكون من العديد من الصفوف. من الممكن التحقق من التحجيم التلقائي لعمود استنادًا إلى المحتوى في نطاق من الخلايا في العمود من خلال استدعاء نسخة مفرطة التحميل من أسلوب autoFitColumn الذي يأخذ المعلمات التالية:

  • فهرس العمود: فهرس العمود الذي سيتم تلائم حجمه تلقائياً.
  • فهرس الصف الأول: فهرس أول صف في العمود.
  • فهرس الصف الأخير: فهرس آخر صف في العمود.

تتحقق طريقة autoFitColumn من محتويات جميع الصفوف في العمود ثم تضبط العمود تلقائيًا.

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"));

كيفية تلائم حجم الصفوف للخلايا المدمجة

مع Aspose.Cells، من الممكن التحجيم التلقائي للصفوف حتى للخلايا المدمجة باستخدام واجهة برمجة التطبيقات AutoFitterOptions. توفر فئة AutoFitterOptions الخاصية AutoFitterOptions.getAutoFitMergedCellsType() التي يمكن استخدامها لتحجيم الصفوف للخلايا المدمجة. يقبل AutoFitterOptions.getAutoFitMergedCellsType() مجموعة AutoFitMergedCellsType القابلة للعد التي تحتوي على الأعضاء التالية.

  • لا شيء: تجاهل الخلايا المدمجة.
  • السطر الأول: فقط يوسع ارتفاع الصف الأول.
  • السطر الأخير: فقط يوسع ارتفاع الصف الأخير.
  • كل سطر: يوسع ارتفاع كل صف.
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"));

مهم معرفته

مواضيع متقدمة