إدراج وحذف الصفوف والأعمدة من ملف إكسل
مقدمة
سواء كنت تقوم بإنشاء ورقة عمل جديدة من الصفر أو العمل في ورقة عمل موجودة، قد نحتاج إلى إضافة صفوف أو أعمدة إضافية لاستيعاب المزيد من البيانات. بالعكس، قد نحتاج أيضًا إلى حذف صفوف أو أعمدة من مواقع محددة في ورقة العمل. لتلبية هذه المتطلبات، يوفر Aspose.Cells for Node.js via C++ مجموعة بسيطة للغاية من الفئات والطرق، التي نوقشت أدناه.
إدارة الصفوف والأعمدة
يوفر Aspose.Cells for Node.js via C++ فئة Workbook، والتي تمثل ملف Microsoft Excel. تحتوي فئة Workbook على مجموعة Worksheets تتيح الوصول إلى كل ورقة عمل في ملف Excel. تمثل ورقة العمل بواسطة فئة Worksheet. توفر فئة Worksheet مجموعة getCells() التي تمثل جميع الخلايا في ورقة العمل.
توفر مجموعة getCells() طرقًا عدة لإدارة الصفوف والأعمدة في ورقة العمل. تم مناقشة بعض منها أدناه.
إدراج الصفوف والأعمدة
كيفية إدراج صف
قم بإدراج صف في ورقة العمل في أي موقع عن طريق استدعاء الinsertRow(number) من مجموعة الgetCells(). يأخذ الinsertRow(number) المؤشر للصف الذي سيتم إدراج الصف الجديد فيه.
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.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);
// Inserting a row into the worksheet at 3rd position
worksheet.getCells().insertRow(2);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
كيفية إدراج عدة صفوف
لإدراج عدة صفوف في ورقة العمل، اتصل بالطريقة insertRows(number, number, boolean) في تجميعة getCells(). طريقة insertRows(number, number, boolean) تأخذ محددتين:
- فهرس الصف، الفهرس للصف من حيث إن الصفوف الجديدة ستدرج.
- عدد الصفوف، إجمالي عدد الصفوف التي يجب إدراجها.
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "book1.xls");
const fileData = fs.readFileSync(filePath);
const workbook = new AsposeCells.Workbook(fileData);
const worksheet = workbook.getWorksheets().get(0);
worksheet.getCells().insertRows(2, 10);
workbook.save(path.join(dataDir, "output.out.xls"));
كيفية إدراج صف مع تنسيق
لإدراج صف بخيارات تنسيق، استخدم الحمولة insertRows(number, number, boolean) التي تأخذ InsertOptions كمعلمة. ثبّت خصية CopyFormatType لفئة InsertOptions بقيمة الترقيم CopyFormatType. الفئة CopyFormatType لها ثلاثة أعضاء كما هو مدرج أدناه.
- SameAsAbove: ينسق الصف نفسه كصف الذي في الأعلى.
- SameAsBelow: ينسق الصف نفسه كصف الذي في الأسفل.
- Clear: يُمسح التنسيق.
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");
// Creating a file stream containing the Excel file to be opened
const filePath = path.join(dataDir, "book1.xls");
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 Formatting options
const insertOptions = new AsposeCells.InsertOptions();
insertOptions.setCopyFormatType(AsposeCells.CopyFormatType.SameAsAbove);
// Inserting a row into the worksheet at 3rd position
worksheet.getCells().insertRows(2, 1, insertOptions);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "InsertingARowWithFormatting.out.xls"));
كيفية إدراج عمود
يمكن للمطورين أيضًا إدراج عمود في ورقة العمل في أي موقع عن طريق استدعاء الطريقة insertColumn(number, boolean) في تجميعة getCells(). الطريقة insertColumn(number, boolean) تأخذ مؤشر العمود حيث سيتم إدراج العمود الجديد.
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 fileStream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fileStream);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Inserting a column into the worksheet at 2nd position
worksheet.getCells().insertColumn(1);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
حذف الصفوف والأعمدة
كيفية حذف عدة صفوف
لحذف صفوف متعددة من ورقة العمل، اتصل بالطريقة deleteRows(number, number) في تجميعة getCells(). الطريقة deleteRows(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.xlsx");
// Read file contents as Uint8Array
const fileContent = fs.readFileSync(filePath);
const fileBuffer = new Uint8Array(fileContent);
// Instantiating a Workbook object with file buffer
const workbook = new AsposeCells.Workbook(fileBuffer);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Deleting 10 rows from the worksheet starting from 3rd row
worksheet.getCells().deleteRows(2, 10);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.xlsx"));
كيفية حذف عمود
لحذف عمود من ورقة العمل في أي موقع، اتصل بالطريقة deleteColumn(number, boolean) في تجميعة getCells(). الطريقة deleteColumn(number, boolean) تأخذ مؤشر العمود للحذف.
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");
// Creating a file stream containing the Excel file to be opened
const filePath = path.join(dataDir, "Book1.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fs.readFileSync(filePath));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Deleting a column from the worksheet at 5th position
worksheet.getCells().deleteColumn(4);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.xlsx"));
// Closing resources is handled automatically by Node.js, no specific close needed.