العمل مع Cells GridWeb

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

يناقش هذا الموضوع الخلايا ، ويبحث في الميزة الأساسية لـ GridWeb: الوصول إلى الخلايا.

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

أدناه ، تتم مناقشة كل نهج.

باستخدام Cell الاسم

جميع الخلايا لها اسم فريد. على سبيل المثال ، 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);

الوصول إلى وتعديل قيمة Cell

الوصول إلى Cells في ورقة العمل ناقش الوصول إلى الخلايا. يوسع هذا الموضوع المناقشة لإظهار كيفية الوصول إلى قيم الخلايا وتعديلها باستخدام GridWeb API.

الوصول إلى قيمة Cell وتعديلها

قيم السلسلة

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

تحتوي كل خلية على خاصية تسمى 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 و 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 يمكن أن تأخذ أي نوع من القيمة في تنسيق سلسلة وتحويلها إلى نوع بيانات مناسب تلقائيًا. لتحقيق ذلك ، مرر القيمة المنطقية “صواب” إلى معلمة أخرى لطريقة 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);

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

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

كيف تضيف وتحسب صيغة؟

من الممكن إضافة الصيغ والوصول إليها وتعديلها في الخلايا باستخدام خاصية الصيغة للخلية. 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

ما يجب القيام به: image_بديل_نص

في لقطة الشاشة أعلاه ، يمكنك أن ترى أنه تمت إضافة صيغة إلى B3 ولكن لم يتم حسابها بعد. لحساب جميع الصيغ ، قم باستدعاء أسلوب GridWorksheetCollection’s calculateFormula الخاص بعنصر التحكم 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

ما يجب القيام به: image_بديل_نص

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

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

ما يجب القيام به: image_بديل_نص

الرجوع إلى Cells من أوراق العمل الأخرى

باستخدام 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");

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

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

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

ما يجب القيام به: image_بديل_نص

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. من الممكن أيضًا إضافة أزرار مخصصة تؤدي مهامًا مخصصة. يشرح هذا الموضوع كيفية استخدام هذه الميزة.

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

ما يجب القيام به: image_بديل_نص

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

ما يجب القيام به: image_بديل_نص

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 a 9 أو 10. سيتم تنسيق الرقم 9 3٪ كـ 3٪ ولكن الرقم 10 سيتم تنسيقه 3٪ كـ 3.00٪.

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

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

ما يجب القيام به: image_بديل_نص

عينة من الرموز

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