قياس العرض والارتفاع لقيمة الخلية بوحدات البكسل
في بعض الأحيان تحتاج إلى حساب العرض والارتفاع لقيمة الخلية لتناسب قيمة الخلية داخل الخلية. توفر Aspose.Cells for Python via .NET الأساليب Cell.get_width_of_value() و Cell.get_height_of_value() لهذا الغرض. من خلال استخدام هذه الأساليب يمكنك حساب عرض وارتفاع قيمة الخلية ومن ثم تعيين عرض العمود وارتفاع الصف الخاص بتلك الخلية على التوالي ، وسيقوم ذلك بعد ذلك بضبط أو تناسب قيمة الخلية داخل الخلية.
بالإضافة إلى ذلك ، يمكنك أيضًا تلقائيًا تناسب الصفوف والأعمدة الخاصة بالخلية أو نطاق الخلايا الخاصة بك باستخدام واجهات برمجة التطبيقات Aspose.Cells for Python via .NET.
الشفرة التالية تشرح استخدام الأساليب Cell.get_width_of_value() و Cell.get_height_of_value().
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create workbook object | |
workbook = Workbook() | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
# Access cell B2 and add some value inside it | |
cell = worksheet.cells.get("B2") | |
cell.put_value("Welcome to Aspose!") | |
# Enlarge its font to size 16 | |
style = cell.get_style() | |
style.font.size = 16 | |
cell.set_style(style) | |
# Calculate the width and height of the cell value in unit of pixels | |
widthOfValue = cell.get_width_of_value() | |
heightOfValue = cell.get_height_of_value() | |
# Print both values | |
print("Width of Cell Value: " + str(widthOfValue)) | |
print("Height of Cell Value: " + str(heightOfValue)) | |
# Set the row height and column width to adjust/fit the cell value inside cell | |
worksheet.cells.set_column_width_pixel(1, widthOfValue) | |
worksheet.cells.set_row_height_pixel(1, heightOfValue) | |
# Save the output excel file | |
workbook.save(dataDir + "output_out.xlsx") |