用C++保存单元格或区域前导单引号前缀
Contents
[
Hide
]
可能的使用场景
当你在单元格中放入带前导撇号或单引号的值时,Excel会隐藏它,但在选中该单元格时,公式栏会显示出前导撇号或单引号,如下截图所示。
Aspose.Cells 同样会像Excel一样隐藏前导撇号,但会将Style.GetQuotePrefix()设置为true以示该单元格已保存此属性。如果将单元格设置为空样式,则Style.GetQuotePrefix()变为false。为处理此问题,Aspose.Cells提供了StyleFlag.GetQuotePrefix()属性。当它设为false时,Style.GetQuotePrefix()不会被更新,原有值会被保留。这意味着,如果Style.GetQuotePrefix()属性的旧值为true,它会保持为true;如果旧值为false,则保持不变。
保留单引号前缀的单元格值或范围
下面的示例代码演示了前面描述的StyleFlag.GetQuotePrefix()属性的用法。请阅读代码中的注释,并查看以下代码的控制台输出以获得更多帮助。
示例代码
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Create workbook
Workbook wb;
// Access first worksheet
Worksheet ws = wb.GetWorksheets().Get(0);
// Access cell A1
Cell cell = ws.GetCells().Get(u"A1");
// Put some text in cell, it does not have Single Quote at the beginning
cell.PutValue(u"Text");
// Access style of cell A1
Style st = cell.GetStyle();
// Print the value of Style.QuotePrefix of cell A1
std::cout << "Quote Prefix of Cell A1: " << st.GetQuotePrefix() << std::endl;
// Put some text in cell, it has Single Quote at the beginning
cell.PutValue(u"'Text");
// Access style of cell A1
st = cell.GetStyle();
// Print the value of Style.QuotePrefix of cell A1
std::cout << "Quote Prefix of Cell A1: " << st.GetQuotePrefix() << std::endl;
// Print information about StyleFlag.QuotePrefix property
std::cout << std::endl;
std::cout << "When StyleFlag.QuotePrefix is False, it means, do not update the value of Cell.Style.QuotePrefix." << std::endl;
std::cout << "Similarly, when StyleFlag.QuotePrefix is True, it means, update the value of Cell.Style.QuotePrefix." << std::endl;
std::cout << std::endl;
// 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;
flag.SetQuotePrefix(false);
// Create a range consisting of single cell A1
Range rng = ws.GetCells().CreateRange(u"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.
std::cout << "Quote Prefix of Cell A1: " << st.GetQuotePrefix() << std::endl;
// 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.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.
std::cout << "Quote Prefix of Cell A1: " << st.GetQuotePrefix() << std::endl;
Aspose::Cells::Cleanup();
}
控制台输出
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