تنسيق الصفوف والأعمدة باستخدام Node.js عبر C++
العمل مع الصفوف
كيفية ضبط ارتفاع الصف
تقدم Aspose.Cells فئة، Workbook، تمثل ملف Excel من Microsoft. تحتوي فئة Workbook على WorksheetCollection تتيح الوصول إلى كل ورقة عمل في ملف Excel. يتم تمثيل ورقة العمل بواسطة فئة Worksheet. توفر فئة Worksheet مجموعة Cells التي تمثل جميع الخلايا في ورقة العمل.
توفر مجموعة Cells العديد من الطرق لإدارة الصفوف أو الأعمدة في ورقة العمل. سيتم مناقشة بعضها بمزيد من التفصيل أدناه.
كيفية ضبط ارتفاع الصف
من الممكن تعيين ارتفاع صف واحد من خلال استدعاء طريقة setRowHeight(number, number) لمجموعة Cells. تأخذ طريقة setRowHeight(number, number) المعلمات التالية كما يلي:
- مؤشر الصف, مؤشر الصف الذي كنت تغير ارتفاعه.
- ارتفاع الصف, ارتفاع الصف المطبق على الصف.
try {
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.createReadStream(filePath);
// Reading the file stream into a buffer
const chunks = [];
fstream.on('data', chunk => chunks.push(chunk));
fstream.on('end', () => {
const buffer = Buffer.concat(chunks);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(buffer);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the height of the second row to 13
worksheet.getCells().setRowHeight(1, 13);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
fstream.close();
كيفية ضبط ارتفاع كل الصفوف في ورقة عمل
إذا احتاج المطورون إلى تعيين ارتفاع الصف نفسه لجميع الصفوف في ورقة العمل، يمكنهم القيام بذلك باستخدام خاصية getStandardHeight() لمجموعة Cells.
مثال:
const fs = require("fs");
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, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the height of all rows in the worksheet to 15
worksheet.getCells().setStandardHeight(15);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
// (Note: Closing the file stream is unnecessary in this context as it's a
// synchronous read performed using fs.readFileSync, which does not require
// explicit closure, but if using fs.createReadStream, you would handle it accordingly)
العمل مع الأعمدة
كيفية ضبط عرض العمود
حدد عرض العمود من خلال استدعاء مجموعة Cells وطريقة setColumnWidth(number, number). تتطلب طريقة setColumnWidth(number, number) المعلمات التالية:
- فهرس العمود, فهرس العمود الذي تريد تغيير عرضه.
- عرض العمود, العرض المطلوب للعمود.
const fs = require("fs");
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, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the width of the second column to 17.5
worksheet.getCells().setColumnWidth(1, 17.5);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
fstream; // Note: No explicit close needed for fs.readFileSync
كيفية تعيين عرض العمود بالبكسل
حدد عرض العمود من خلال استدعاء مجموعة Cells وطريقة setColumnWidthPixel(number, number). تتطلب طريقة setColumnWidthPixel(number, number) المعلمات التالية:
- فهرس العمود, فهرس العمود الذي تريد تغيير عرضه.
- عرض العمود, عرض العمود المطلوب بالبكسل.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// Source directory
const sourceDir = path.join(__dirname, "data");
const outDir = path.join(__dirname, "output");
// Load source Excel file
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "Book1.xlsx"));
// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Set the width of the column in pixels
worksheet.getCells().setColumnWidthPixel(7, 200);
workbook.save(path.join(outDir, "SetColumnWidthInPixels_Out.xlsx"));
كيفية ضبط عرض جميع الأعمدة في ورقة العمل
لضبط عرض العمود نفسه لجميع الأعمدة في ورقة العمل، استخدم خاصية getStandardWidth() لمجموعة Cells.
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Creating a file stream containing the Excel file to be opened
const filePath = path.join(dataDir, "book1.xls");
// Instantiating a Workbook object
// Opening the Excel file
const workbook = new AsposeCells.Workbook(filePath);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the width of all columns in the worksheet to 20.5
worksheet.getCells().setStandardWidth(20.5);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
// No explicit close needed in Node.js
مواضيع متقدمة
- تكييف صفوف وأعمدة تلقائيًا
- تحويل النص إلى أعمدة باستخدام Aspose.Cells
- نسخ الصفوف والأعمدة
- حذف الصفوف والأعمدة الفارغة في ورقة العمل
- تجميع وفك تجميع الصفوف والأعمدة
- إخفاء وإظهار الصفوف والأعمدة
- إدراج أو حذف الصفوف في ورقة عمل Excel
- إدراج وحذف الصفوف والأعمدة من ملف Excel
- إزالة الصفوف المكررة في ورقة العمل
- تحديث المراجع في ورقات العمل الأخرى أثناء حذف الأعمدة والصفوف الفارغة في ورقة العمل