Сохранить префикс одиночной кавычки для значения или диапазона Cell

Возможные сценарии использования

Когда вы помещаете какое-либо значение в ячейку с начальным апострофом или одинарной кавычкой, Microsoft Excel скрывает его, но когда вы выбираете ячейку, он отображает начальный апостроф или одинарную кавычку в строке формул, как показано на следующем снимке экрана.

дело:изображение_альтернативный_текст

Aspose.Cells также скрывает начальный апостроф или одинарную кавычку, как Microsoft Excel, но устанавливаетStyle.QuotePrefix какистинный для этой ячейки. Если задать пустой стиль ячейки, тоStyle.QuotePrefix становитсяЛОЖЬ еще раз. Чтобы решить эту проблему, Aspose.Cells предоставляетStyleFlag.QuotePrefix свойство, когда оно установленоЛОЖЬ, тогдаStyleFlag.QuotePrefixвообще не обновляется и сохраняется его старое значение. Это означает, что если старое значениеStyle.QuotePrefixимущество былоистинный, оно останется истинным, а если старое значение было ложным, оно останется ложным.

Сохранить префикс одиночной кавычки для значения или диапазона Cell

В следующем примере кода объясняется использованиеStyleFlag.QuotePrefixимущество, как описано ранее. Пожалуйста, прочтите комментарии внутри кода и просмотрите консольный вывод кода, приведенного ниже, для получения дополнительной помощи.

Образец кода

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Create workbook
Workbook wb = new Workbook();
//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
//Access cell A1
Cell cell = ws.getCells().get("A1");
//Put some text in cell, it does not have Single Quote at the beginning
cell.putValue("Text");
//Access style of cell A1
Style st = cell.getStyle();
//Print the value of Style.QuotePrefix of cell A1
System.out.println("Quote Prefix of Cell A1: " + st.getQuotePrefix());
//Put some text in cell, it has Single Quote at the beginning
cell.putValue("'Text");
//Access style of cell A1
st = cell.getStyle();
//Print the value of Style.QuotePrefix of cell A1
System.out.println("Quote Prefix of Cell A1: " + st.getQuotePrefix());
//Print information about StyleFlag.QuotePrefix property
System.out.println();
System.out.println("When StyleFlag.QuotePrefix is False, it means, do not update the value of Cell.Style.QuotePrefix.");
System.out.println("Similarly, when StyleFlag.QuotePrefix is True, it means, update the value of Cell.Style.QuotePrefix.");
System.out.println();
//Create an empty style
st = wb.createStyle();
//Create style flag - set StyleFlag.QuotePrefix as false
//It means, we do not want to update the Style.QuotePrefix property of cell A1's style.
StyleFlag flag = new StyleFlag();
flag.setQuotePrefix(false);
//Create a range consisting of single cell A1
Range rng = ws.getCells().createRange("A1");
//Apply the style to the range
rng.applyStyle(st, flag);
//Access the style of cell A1
st = cell.getStyle();
//Print the value of Style.QuotePrefix of cell A1
//It will print True, because we have not updated the Style.QuotePrefix property of cell A1's style.
System.out.println("Quote Prefix of Cell A1: " + st.getQuotePrefix());
//Create an empty style
st = wb.createStyle();
//Create style flag - set StyleFlag.QuotePrefix as true
//It means, we want to update the Style.QuotePrefix property of cell A1's style.
flag = new StyleFlag();
flag.setQuotePrefix(true);
//Apply the style to the range
rng.applyStyle(st, flag);
//Access the style of cell A1
st = cell.getStyle();
//Print the value of Style.QuotePrefix of cell A1
//It will print False, because we have updated the Style.QuotePrefix property of cell A1's style.
System.out.println("Quote Prefix of Cell A1: " + st.getQuotePrefix());

Консольный вывод

Quote Prefix of Cell A1: false

Quote Prefix of Cell A1: true

When StyleFlag.QuotePrefix is False, it means, do not update the value of Cell.Style.QuotePrefix.

Similarly, when StyleFlag.QuotePrefix is True, it means, update the value of Cell.Style.QuotePrefix.

Quote Prefix of Cell A1: true

Quote Prefix of Cell A1: false