保留单引号前缀的单元格值或范围
可能的使用场景
当您在单元格中放入具有前导撇号或单引号标记的值时,Microsoft Excel会隐藏它,但当您选择单元格时,它会在公式栏中显示前导撇号或单引号,如下面的屏幕截图所示。
Aspose.Cells for Python via .NET也隐藏前导撇号或单引号,就像Microsoft Excel一样,但它将Style.quote_prefix设置为true以用于该单元格。如果您设置一个空的单元格样式,则Style.quote_prefix再次变为false。为了解决这个问题,Aspose.Cells for Python via .NET提供了Style.quote_prefix属性,当它设置为false时,Style.quote_prefix根本不被更新,它的旧值被保留。这意味着如果Style.quote_prefix属性的旧值为true,它将保持为true,如果旧值为false,它将保持为false。
保留单引号前缀的单元格值或范围
以下示例代码解释了如何使用先前描述的Style.quote_prefix属性。请阅读代码中的注释,并查看下面给定的代码的控制台输出以获取更多帮助。
示例代码
from aspose.cells import StyleFlag, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create workbook | |
wb = Workbook() | |
# Access first worksheet | |
ws = wb.worksheets[0] | |
# Access cell A1 | |
cell = ws.cells.get("A1") | |
# Put some text in cell, it does not have Single Quote at the beginning | |
cell.put_value("Text") | |
# Access style of cell A1 | |
st = cell.get_style() | |
# Print the value of Style.quote_prefix of cell A1 | |
print("Quote Prefix of Cell A1: " + str(st.quote_prefix)) | |
# Put some text in cell, it has Single Quote at the beginning | |
cell.put_value("'Text") | |
# Access style of cell A1 | |
st = cell.get_style() | |
# Print the value of Style.quote_prefix of cell A1 | |
print("Quote Prefix of Cell A1: " + str(st.quote_prefix)) | |
# Print information about StyleFlag.quote_prefix property | |
print() | |
print("When StyleFlag.quote_prefix is False, it means, do not update the value of Cell.Style.quote_prefix.") | |
print("Similarly, when StyleFlag.quote_prefix is True, it means, update the value of Cell.Style.quote_prefix.") | |
print() | |
# Create an empty style | |
st = wb.create_style() | |
# Create style flag - set StyleFlag.quote_prefix as false | |
# It means, we do not want to update the Style.quote_prefix property of cell A1's style. | |
flag = StyleFlag() | |
flag.quote_prefix = False | |
# Create a range consisting of single cell A1 | |
rng = ws.cells.create_range("A1") | |
# Apply the style to the range | |
rng.apply_style(st, flag) | |
# Access the style of cell A1 | |
st = cell.get_style() | |
# Print the value of Style.quote_prefix of cell A1 | |
# It will print True, because we have not updated the Style.quote_prefix property of cell A1's style. | |
print("Quote Prefix of Cell A1: " + str(st.quote_prefix)) | |
# Create an empty style | |
st = wb.create_style() | |
# Create style flag - set StyleFlag.quote_prefix as true | |
# It means, we want to update the Style.quote_prefix property of cell A1's style. | |
flag = StyleFlag() | |
flag.quote_prefix = True | |
# Apply the style to the range | |
rng.apply_style(st, flag) | |
# Access the style of cell A1 | |
st = cell.get_style() | |
# Print the value of Style.quote_prefix of cell A1 | |
# It will print False, because we have updated the Style.quote_prefix property of cell A1's style. | |
print("Quote Prefix of Cell A1: " + str(st.quote_prefix)) |
控制台输出
Quote Prefix of Cell A1: False
Quote Prefix of Cell A1: True
When StyleFlag.quote_prefix is False, it means, do not update the value of Cell.Style.quote_prefix.
Similarly, when StyleFlag.quote_prefix is True, it means, update the value of Cell.Style.quote_prefix.
Quote Prefix of Cell A1: True
Quote Prefix of Cell A1: False