تنسيق الخلايا باستخدام Node.js عبر C++
مقدمة
كيفية تنسيق الخلايا باستخدام أساليب GetStyle و SetStyle
تطبيق أنواع مختلفة من أنماط التنسيق على الخلايا لتعيين ألوان الخلفية أو الأمامية، الحدود، الخطوط، المحاور الأفقية والعمودية، مستوى المسافة البادئة، اتجاه النص، زاوية الدوران والمزيد.
كيفية استخدام أساليب GetStyle و SetStyle
إذا كان المطورون بحاجة إلى تطبيق أنماط تنسيق مختلفة على خلايا مختلفة، فمن الأفضل استرجاع Style للخلية عبر طريقة Cell.getStyle()، تحديد سمات النمط، ثم تطبيق التنسيق باستخدام طريقة Cell.setStyle(Style). يُعطى المثال أدناه ليوضح هذا النهج لتطبيق تنسيقات متنوعة على خلية.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");
// Defining a Style object
let style;
// Get the style from A1 cell
style = cell.getStyle();
// Setting the vertical alignment
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text
style.getFont().setColor(AsposeCells.Color.Green);
// Setting to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border color to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Applying the style to A1 cell
cell.setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
كيفية استخدام كائن النمط لتنسيق خلايا مختلفة
إذا كان المطورون بحاجة إلى تطبيق نمط تنسيق نفسه على خلايا مختلفة، يمكنهم استخدام كائن Style. يرجى اتباع الخطوات أدناه لاستخدام كائن Style:
- أضف كائن Style عن طريق استدعاء طريقة createStyle() الخاصة بفئة Workbook
- الوصول إلى كائن Style المضاف حديثًا
- تعيين الخصائص/السمات المرغوبة لكائن Style لتطبيق الإعدادات التنسيقية المطلوبة
- تعيين كائن Style المُعدّل للخلايا المرغوبة
يمكن أن يحسن هذا النهج بشكل كبير كفاءة تطبيقاتك ويوفر ذاكرة أيضًا.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Adding a new worksheet to the Excel object
const i = workbook.getWorksheets().add();
// Obtaining the reference of the first worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(i);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");
// Adding a new Style
const style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Assigning the Style object to the "A1" cell
cell.setStyle(style);
// Apply the same style to some other cells
worksheet.getCells().get("B1").setStyle(style);
worksheet.getCells().get("C1").setStyle(style);
worksheet.getCells().get("D1").setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
كيفية استخدام أنماط Microsoft Excel 2007 المحددة مسبقًا
إذا كنت بحاجة لتطبيق أنماط تنسيق مختلفة لـ Microsoft Excel 2007، فقم بتطبيق الأنماط باستخدام واجهة برمجة التطبيقات Aspose.Cells. يُظهر المثال التالي هذا النهج لتطبيق نمط محدد مسبقًا على خلية.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiate a new Workbook.
const workbook = new AsposeCells.Workbook();
// Create a style object.
const style = workbook.createStyle();
// Input a value to A1 cell.
workbook.getWorksheets().get(0).getCells().get("A1").putValue("Test");
// Apply the style to the cell.
workbook.getWorksheets().get(0).getCells().get("A1").setStyle(style);
// Save the Excel 2007 file.
workbook.save(path.join(dataDir, "book1.out.xlsx"));
كيفية تنسيق الأحرف المحددة في خلية
تشرح التعامل مع إعدادات الخط كيفية تنسيق النص في الخلايا، لكنها تشرح فقط كيفية تنسيق كل مضمون الخلية. ماذا إذا كنت ترغب في تنسيق الأحرف المحددة فقط؟
يدعم Aspose.Cells هذه الميزة أيضًا. يوضح هذا الموضوع كيفية استخدام هذه الميزة بشكل فعال.
كيفية تنسيق الأحرف المحددة
توفر Aspose.Cells فئة، Workbook التي تمثل ملف إكسل من Microsoft. تحتوي فئة Workbook على مجموعة getWorksheets() التي تتيح الوصول إلى كل ورقة عمل في ملف إكسل. تمثل الورقة بواسطة فئة Worksheet. توفر فئة Worksheet مجموعة getCells(). يمثل كل عنصر في مجموعة getCells() كائن من فئة Cell.
تقدم فئة Cell طريقة characters(number, number) التي تستقبل المعلمات التالية لاختيار نطاق من الأحرف داخل خلية:
- فهرس البداية, وهو فهرس الحرف الذي يبدأ منه التحديد.
- عدد الحروف, عدد الأحرف المراد تحديدها.
تعيد طريقة characters(number, number) نسخة من فئة FontSetting التي تسمح للمطورين بتنسيق الأحرف بنفس طريقة تنسيق خلية كما هو موضح أدناه في مثال التعليمات البرمجية. في الملف الناتج، في خلية A1، سيتم تنسيق كلمة ‘Visit’ باستخدام الخط الافتراضي ولكن ‘Aspose!’ بخط عريض وأزرق.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the first(default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the font of selected characters to bold
const font = cell.characters(6, 7).getFont();
font.isBold = true;
// Setting the font color of selected characters to blue
font.color = AsposeCells.Color.Blue;
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
كيفية تنسيق الصفوف والأعمدة
في بعض الأحيان، يحتاج المطورون إلى تطبيق نفس التنسيق على الصفوف أو الأعمدة. تطبيق التنسيق على الخلايا واحدة تلو الأخرى غالبًا ما يستغرق وقتا أطول وليس حلا جيدًا. لحل هذه المشكلة، يوفر Aspose.Cells طريقة بسيطة وسريعة يتم مناقشتها بالتفصيل في هذا المقال.
تنسيق الصفوف والأعمدة
توفر Aspose.Cells فئة، Workbook التي تمثل ملف إكسل من Microsoft. تحتوي فئة Workbook على مجموعة getWorksheets() تتيح الوصول إلى كل ورقة عمل في ملف إكسل. تمثل ورقة العمل بواسطة فئة Worksheet. تقدم فئة Worksheet مجموعة getCells(). توفر مجموعة getCells() مجموعة getRows().
كيفية تنسيق صف
كل عنصر في مجموعة getRows() يمثل كائن Row. يوفر كائن Row طريقة applyStyle(Style, StyleFlag) المستخدمة لضبط تنسيق الصف. لتطبيق التنسيق نفسه على صف، استخدم كائن Style. تظهر الخطوات أدناه كيفية استخدامه.
- أضف كائن Style إلى فئة Workbook عن طريق استدعاء طريقة createStyle().
- تعيين خصائص كائن Style لتطبيق إعدادات التنسيق.
- تفعيل السمات ذات الصلة على كائن StyleFlag.
- تعيين كائن Style المُعدّل إلى كائن Row.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles
const style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Creating StyleFlag
const styleFlag = new AsposeCells.StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBorders(true);
styleFlag.setFontColor(true);
// Accessing a row from the Rows collection
const row = worksheet.getCells().getRows().get(0);
// Assigning the Style object to the Style property of the row
row.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
كيفية تنسيق عمود
توفر مجموعة getCells() أيضًا مجموعة getColumns(). يمثل كل عنصر في مجموعة getColumns() كائن Column. مماثل لكائن Row، توفر كائن Column أيضًا طريقة applyStyle(Style, StyleFlag) لتنسيق عمود.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles
const style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Creating StyleFlag
const styleFlag = new AsposeCells.StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBorders(true);
styleFlag.setFontColor(true);
// Accessing a column from the Columns collection
const column = worksheet.getCells().getColumns().get(0);
// Applying the style to the column
column.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));