العمل مع خلايا GridWeb

الوصول إلى الخلايا في ورقة العمل

تناول هذا الموضوع الخلايا، مع التركيز على الميزة الأساسية لـ GridWeb: الوصول إلى الخلايا.

تحتوي كل ورقة عمل على كائن GridCells، مجموعة من أجهزة GridCell. يمثل كائن GridCell خلية في Aspose.Cells.GridWeb. من الممكن الوصول إلى أي خلية باستخدام GridWeb. هناك طريقتان مفضلتان:

أدناه موضوع كل نهج.

استخدام اسم الخلية

تحتوي جميع الخلايا على اسم فريد. على سبيل المثال، A1، A2، B1، B2، إلخ. يسمح Aspose.Cells.GridWeb للمطورين بالوصول إلى أي خلية مطلوبة باستخدام اسم الخلية. ما عليك سوى تمرير اسم الخلية (كفهرس) إلى مجموعة GridCells لـ GridWorksheet.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Accessing "B1" cell of the worksheet
GridCell cell = sheet.getCells().get("B1");

استخدام فهرس الصف والعمود

يمكن أيضًا التعرف على خلية من خلال موقعها من حيث فهارس الصف والعمود. ما عليك سوى تمرير فهارات الصف والعمود لخلية إلى مجموعة GridCells لـ GridWorksheet. هذا النهج أسرع من النهج الأول.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Accessing "B1" cell of the worksheet using its row and column indices
GridCell cell = sheet.getCells().get(0, 1);

الوصول وتعديل قيمة الخلية

وصول الخلايا في ورقة العمل ناقش وصول الخلايا. يوسع هذا الموضوع تلك النقاش ليظهر كيفية الوصول وتعديل قيم الخلايا باستخدام واجهة برمجة التطبيقات لـ GridWeb.

الوصول وتعديل قيمة الخلية

قيم السلاسل

قبل الوصول وتعديل قيمة الخلية، تحتاج إلى معرفة كيفية الوصول إلى الخلايا. للحصول على تفاصيل حول النهج المختلفة للوصول إلى الخلايا، راجع الوصول إلى الخلايا في ورقة العمل.

لكل خلية خاصية تسمى getStringValue(). بمجرد الوصول إلى خلية، يمكن للمطورين الوصول إلى طريقة getStringValue() للوصول إلى قيمة الخلية النصية.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Accessing "B1" cell of the worksheet
GridCell cell = sheet.getCells().get("B1");
//Inserting & modifying the string value of "B1" cell
cell.putValue("Hello Aspose.Grid");

جميع أنواع القيم

توفر Aspose.Cells.GridWeb أيضًا طريقة خاصة، putValue، لكل خلية. باستخدام هذه الطريقة، من الممكن إدخال أو تعديل أي نوع من القيم (Boolean, int, double, DateTime, and string) في خلية.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Accessing "B1" cell of the worksheet
GridCell cell = sheet.getCells().get("B1");
//Putting a value in "B1" cell
cell.putValue(Calendar.getInstance());

هناك أيضًا نسخة معبأة زائدة من طريقة putValue يمكنها أخذ أي نوع من القيم في تنسيق نصي وتحويلها إلى نوع بيانات صحيح تلقائيًا. للقيام بذلك، قم بتمرير قيمة Boolean true إلى معلمة أخرى من طريقة putValue كما هو موضح أدناه في المثال.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Accessing "B1" cell of the worksheet
GridCell cell = sheet.getCells().get("B1");
//Putting a numeric value as string in "B1" cell that will be converted to a suitable data type automatically
cell.putValue("19.4", true);

إضافة صيغ إلى الخلايا

أهم الميزات التي يوفرها Aspose.Cells.GridWeb هي دعم الصيغ أو الوظائف. تحتوي Aspose.Cells.GridWeb على محرك الصيغ الخاص بها الذي يقوم بحساب الصيغ في أوراق العمل. يدعم Aspose.Cells.GridWeb الوظائف أو الصيغ الداخلية والمحددة من قبل المستخدمين. يناقش هذا الموضوع إضافة الصيغ إلى الخلايا باستخدام واجهة برمجة التطبيقات Aspose.Cells.GridWeb بالتفصيل.

كيفية إضافة وحساب صيغة

من الممكن إضافة، الوصول، وتعديل الصيغ في الخلايا باستخدام خاصية الصيغة للخلية. يدعم Aspose.Cells.GridWeb صيغ المعرفة من قبل المستخدم تتراوح بين البسيطة والمعقدة. ومع ذلك، يتم توفير عدد كبير من الدوال أو الصيغ المدمجة (مشابهة ل Microsoft Excel) أيضًا مع Aspose.Cells.GridWeb. لرؤية القائمة الكاملة للدوال المدمجة، يرجى الرجوع إلى قائمة الدوال المدعمة.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Putting some values to cells
sheet.getCells().get("A1").putValue("1st Value");
sheet.getCells().get("A2").putValue("2nd Value");
sheet.getCells().get("A3").putValue("Sum");
sheet.getCells().get("B1").putValue(125.56);
sheet.getCells().get("B2").putValue(23.93);
//Calculating all formulas added in worksheets
gridweb.getWorkSheets().calculateFormula();
//Adding a simple formula to "B3" cell
sheet.getCells().get("B3").setFormula("=SUM(B1:B2)");

تمت إضافة الصيغة إلى الخلية B3 ولكن لم يتم حسابها بواسطة GridWeb

todo:image_alt_text

في اللقطة المتحركة أعلاه، يمكنك رؤية أن الصيغة قد تمت إضافتها إلى الخلية B3 ولم يتم حسابها بعد. لحساب جميع الصيغ، قم بنداء طريقة الحساب calculateFormula من مجموعة GridWorksheetCollection لعنصر تحكم GridWeb بعد إضافة الصيغ إلى ورق العمل كما هو موضح أدناه.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Calculating all formulas added in worksheets
gridweb.getWorkSheets().calculateFormula();

يمكن للمستخدمين أيضًا حساب الصيغ عن طريق النقر على تقديم.

النقر على زر تقديم من GridWeb

todo:image_alt_text

مهم: إذا قام المستخدم بالنقر على حفظ أو تراجع, أو علامات علامات التبويب للصفحات، سيتم حساب جميع الصيغ تلقائيًا بواسطة GridWeb.

نتيجة الصيغة بعد الحساب

todo:image_alt_text

الإشارة إلى الخلايا من أوراق عمل أخرى

من خلال Aspose.Cells.GridWeb، يمكن الإشارة إلى القيم المخزنة في أوراق عمل مختلفة في صيغهم، مما يخلق صيغ معقدة.

الصيغة للإشارة إلى قيمة الخلية من ورقة عمل مختلفة هي اسم الورقة! اسم الخلية.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Adding a bit complex formula to "A1" cell
sheet.getCells().get("A1").setFormula("=SUM(F1:F7)/ AVERAGE (E1:E7)-Sheet1!C6");

إنشاء التحقق من البيانات في خلية من GridWeb

يتيح Aspose.Cells.GridWeb لك إضافة ‘التحقق من البيانات’ باستخدام طريقة .add() GridWorksheet.getValidations(). باستخدام هذه الطريقة، يجب عليك تحديد ‘نطاق الخلية’. ولكن إذا كنت ترغب في إنشاء التحقق من البيانات في خلية GridCell واحدة فقط يمكنك فعل ذلك مباشرة باستخدام طريقة GridCell.createValidation(). بالمثل، يمكنك إزالة ‘التحقق من البيانات’ من خلية الشبكة باستخدام طريقة GridCell.removeValidation().

الكود النموذجي التالي ينشئ التحقق من البيانات في خلية B3. إذا قمت بإدخال أي قيمة ليست بين 20 و 40، ستظهر خلية B3 خطأ في التحقق على شكل XXXX أحمر كما هو موضح في هذا اللقطة الشاشة.

todo:image_alt_text

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Access first worksheet
GridWorksheet sheet = gridweb.getWorkSheets().get(0);
//Access cell B3
GridCell cell = sheet.getCells().get("B3");
//Add validation inside the gridcell
//Any value which is not between 20 and 40 will cause error in a gridcell
GridValidation val = cell.createValidation(GridValidationType.WHOLE_NUMBER, true);
val.setFormula1("=20");
val.setFormula2("=40");
val.setOperator(OperatorType.BETWEEN);
val.setShowError(true);
val.setShowInput(true);

إنشاء أزرار أوامر مخصصة

يحتوي Aspose.Cells.GridWeb على أزرار خاصة مثل ‘إرسال’، ‘حفظ’، و’تراجع'. كل هذه الأزرار تقوم بأداء مهام محددة لـ Aspose.Cells.GridWeb. كما أنه من الممكن إضافة أزرار مخصصة تقوم بأداء مهام مخصصة. يشرح هذا الموضوع كيفية استخدام هذه الميزة.

يشرح الكود المحدد أدناه كيفية إنشاء زر أمر مخصص وكيفية التعامل مع حدث النقر عليه. يمكنك استخدام أي رمز لأيقونتك المخصصة. لأغراض التوضيح، استخدمنا أيقونة الصورة هذه.

todo:image_alt_text

كما ترون في اللقطة المتحركة التالية، عندما يقوم المستخدم بالنقر على زر الأمر المخصص، يتم إضافة نص في الخلية A1 قائلاً “لقد تم النقر على زر الأمر المخصص الخاص بي.”

todo:image_alt_text

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Instantiating a CustomCommandButton object
CustomCommandButton button = new CustomCommandButton();
//Setting the command for button
button.setCommand("MyButton");
//Setting text of the button
button.setText("MyButton");
//Setting tooltip of the button
button.setToolTip("My Custom Command Button");
//Setting image URL of the button
button.setImageUrl("icon.png");
//Adding button to CustomCommandButtons collection of GridWeb
gridweb.getCustomCommandButtons().add(button);

معالجة الأحداث لزر أمر مخصص

يشرح الكود المحدد أدناه كيفية معالجة حدث زر الأمر المخصص.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Create custom command event handler to handle the click event
CustomCommandEventHandler cceh=new CustomCommandEventHandler(){
public void handleCellEvent(Object sender, String command){
//Identifying a specific button by checking its command
if (command.equals("MyButton"))
{
//Accessing the cells collection of the worksheet that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Putting value to "A1" cell
sheet.getCells().get("A1").putValue("My Custom Command Button is Clicked.");
sheet.getCells().setColumnWidth(0, 50);
}
}
};
//Assign the custom command event handler created above to gridweb
gridweb.CustomCommand = cceh;

تهيئة الخلايا لـ GridWeb

سيناريوهات الاستخدام المحتملة

يدعم GridWeb الآن المستخدمين في إدخال بيانات الخلية بتنسيق النسبة مثل 3٪ وسيتم تنسيق البيانات في الخلية تلقائيًا كـ 3.00٪. ومع ذلك ، سيتعين عليك تعيين نمط الخلية إلى تنسيق النسبة وهو إما GridTableItemStyle.NumberType 9 أو 10. سيقوم الرقم 9 بتنسيق 3٪ كـ 3٪ ولكن الرقم 10 سيقوم بتنسيق 3٪ كـ 3.00٪.

إدخال بيانات الخلية لورقة العمل GridWeb بتنسيق النسبة

الكود النموذجي التالي يضبط الخلية A1 GridTableItemStyle.NumberType كرقم 10، وبالتالي سيتم تنسيق بيانات الإدخال 3٪ تلقائياً كـ 3.00٪ كما هو موضح في اللقطة.

todo:image_alt_text

الكود المثالي

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Access cell A1 of first gridweb worksheet
GridCell cellA1 = gridweb.getWorkSheets().get(0).getCells().get("A1");
//Access cell style and set its number format to 10 which is a Percentage 0.00% format
GridTableItemStyle st = cellA1.getStyle();
st.setNumberType(10);
cellA1.setStyle(st);