📄 dvparser.java
字号:
condition = BETWEEN;
// the options
stringListGiven = true;
emptyCellsAllowed = true;
suppressArrow = false;
showPrompt = true;
showError = true;
promptTitle = "\0";
errorTitle = "\0";
promptText = "\0";
errorText = "\0";
if (strings.size() == 0)
{
logger.warn("no validation strings - ignoring");
}
Iterator i = strings.iterator();
StringBuffer formulaString = new StringBuffer();
formulaString.append('\"');
formulaString.append(i.next().toString());
while (i.hasNext())
{
formulaString.append('\0');
formulaString.append(' ');
formulaString.append(i.next().toString());
}
formulaString.append('\"');
formula1String = formulaString.toString();
}
/**
* Constructor called when creating a data validation from the API
*/
public DVParser(int c1, int r1, int c2, int r2)
{
type = LIST;
errorStyle = STOP;
condition = BETWEEN;
// the options
stringListGiven = false;
emptyCellsAllowed = true;
suppressArrow = false;
showPrompt = true;
showError = true;
promptTitle = "\0";
errorTitle = "\0";
promptText = "\0";
errorText = "\0";
StringBuffer formulaString = new StringBuffer();
CellReferenceHelper.getCellReference(c1,r1,formulaString);
formulaString.append(':');
CellReferenceHelper.getCellReference(c2,r2,formulaString);
formula1String = formulaString.toString();
}
/**
* Constructor called when creating a data validation from the API
*/
public DVParser(double val1, double val2, Condition c)
{
type = DECIMAL;
errorStyle = STOP;
condition = c;
// the options
stringListGiven = false;
emptyCellsAllowed = true;
suppressArrow = false;
showPrompt = true;
showError = true;
promptTitle = "\0";
errorTitle = "\0";
promptText = "\0";
errorText = "\0";
formula1String = Double.toString(val1);
if (!Double.isNaN(val2))
{
formula2String = Double.toString(val2);
}
}
/**
* Constructor called when doing a cell deep copy
*/
public DVParser(DVParser copy)
{
type = copy.type;
errorStyle = copy.errorStyle;
condition = copy.condition;
stringListGiven = copy.stringListGiven;
emptyCellsAllowed = copy.emptyCellsAllowed;
suppressArrow = copy.suppressArrow;
showPrompt = copy.showPrompt;
showError = copy.showError;
promptTitle = copy.promptTitle;
promptText = copy.promptText;
errorTitle = copy.errorTitle;
errorText = copy.errorText;
// Don't copy the formula parsers - just take their string equivalents
try
{
formula1String = copy.formula1.getFormula();
formula2String = (copy.formula2 != null) ?
copy.formula2.getFormula() : null;
}
catch (FormulaException e)
{
logger.warn("Cannot parse validation formula: " + e.getMessage());
}
// Don't copy the cell references - these will be added later
}
/**
* Gets the data
*/
public byte[] getData()
{
// Compute the length of the data
byte[] f1Bytes = formula1 != null ? formula1.getBytes() : new byte[0];
byte[] f2Bytes = formula2 != null ? formula2.getBytes() : new byte[0];
int dataLength =
4 + // the options
promptTitle.length() * 2 + 2 + // the prompt title
errorTitle.length() * 2 + 2 + // the error title
promptText.length() * 2 + 2 + // the prompt text
errorText.length() * 2 + 2 + // the error text
f1Bytes.length + 2 + // first formula
f2Bytes.length + 2 + // second formula
+ 4 + // unused bytes
10; // cell range
byte[] data = new byte[dataLength];
// The position
int pos = 0;
// The options
int options = 0;
options |= type.getValue();
options |= errorStyle.getValue() << 4;
options |= condition.getValue() << 20;
if (stringListGiven)
{
options |= STRING_LIST_GIVEN_MASK;
}
if (emptyCellsAllowed)
{
options |= EMPTY_CELLS_ALLOWED_MASK;
}
if (suppressArrow)
{
options |= SUPPRESS_ARROW_MASK;
}
if (showPrompt)
{
options |= SHOW_PROMPT_MASK;
}
if (showError)
{
options |= SHOW_ERROR_MASK;
}
// The text
IntegerHelper.getFourBytes(options, data, pos);
pos += 4;
IntegerHelper.getTwoBytes(promptTitle.length(), data, pos);
pos += 2;
StringHelper.getUnicodeBytes(promptTitle, data, pos);
pos += promptTitle.length() * 2;
IntegerHelper.getTwoBytes(errorTitle.length(), data, pos);
pos += 2;
StringHelper.getUnicodeBytes(errorTitle, data, pos);
pos += errorTitle.length() * 2;
IntegerHelper.getTwoBytes(promptText.length(), data, pos);
pos += 2;
StringHelper.getUnicodeBytes(promptText, data, pos);
pos += promptText.length() * 2;
IntegerHelper.getTwoBytes(errorText.length(), data, pos);
pos += 2;
StringHelper.getUnicodeBytes(errorText, data, pos);
pos += errorText.length() * 2;
// Formula 1
IntegerHelper.getTwoBytes(f1Bytes.length, data, pos);
pos += 4;
System.arraycopy(f1Bytes, 0, data, pos, f1Bytes.length);
pos += f1Bytes.length;
// Formula 2
IntegerHelper.getTwoBytes(f2Bytes.length, data, pos);
pos += 4;
System.arraycopy(f2Bytes, 0, data, pos, f2Bytes.length);
pos += f2Bytes.length;
// The cell ranges
IntegerHelper.getTwoBytes(1, data, pos);
pos += 2;
IntegerHelper.getTwoBytes(row1, data, pos);
pos += 2;
IntegerHelper.getTwoBytes(row2, data, pos);
pos += 2;
IntegerHelper.getTwoBytes(column1, data, pos);
pos += 2;
IntegerHelper.getTwoBytes(column2, data, pos);
pos += 2;
return data;
}
/**
* Inserts a row
*
* @param row the row to insert
*/
public void insertRow(int row)
{
if (formula1 != null)
{
formula1.rowInserted(0, row, true);
}
if (formula2 != null)
{
formula2.rowInserted(0, row, true);
}
if (row1 >= row)
{
row1++;
}
if (row2 >= row)
{
row2++;
}
}
/**
* Inserts a column
*
* @param col the column to insert
*/
public void insertColumn(int col)
{
if (formula1 != null)
{
formula1.columnInserted(0, col, true);
}
if (formula2 != null)
{
formula2.columnInserted(0, col, true);
}
if (column1 >= col)
{
column1++;
}
if (column2 >= col)
{
column2++;
}
}
/**
* Removes a row
*
* @param row the row to insert
*/
public void removeRow(int row)
{
if (formula1 != null)
{
formula1.rowRemoved(0, row, true);
}
if (formula2 != null)
{
formula2.rowRemoved(0, row, true);
}
if (row1 > row)
{
row1--;
}
if (row2 >= row)
{
row2--;
}
}
/**
* Removes a column
*
* @param col the row to remove
*/
public void removeColumn(int col)
{
if (formula1 != null)
{
formula1.columnRemoved(0, col, true);
}
if (formula2 != null)
{
formula2.columnRemoved(0, col, true);
}
if (column1 > col)
{
column1--;
}
if (column2 >= col)
{
column2--;
}
}
/**
* Accessor for first column
*
* @return the first column
*/
public int getFirstColumn()
{
return column1;
}
/**
* Accessor for the last column
*
* @return the last column
*/
public int getLastColumn()
{
return column2;
}
/**
* Accessor for first row
*
* @return the first row
*/
public int getFirstRow()
{
return row1;
}
/**
* Accessor for the last row
*
* @return the last row
*/
public int getLastRow()
{
return row2;
}
/**
* Gets the formula present in the validation
*
* @return the validation formula as a string
* @exception FormulaException
*/
String getValidationFormula() throws FormulaException
{
if (type == LIST)
{
return formula1.getFormula();
}
String s1 = formula1.getFormula();
String s2 = formula2 != null ? formula2.getFormula() : null;
return condition.getConditionString(s1, s2) +
"; x " + type.getDescription();
}
/**
* Called by the cell value when the cell features are added to the sheet
*/
public void setCell(int col, int row,
ExternalSheet es,
WorkbookMethods nt,
WorkbookSettings ws) throws FormulaException
{
row1 = row;
row2 = row;
column1 = col;
column2 = col;
formula1 = new FormulaParser(formula1String,
es, nt, ws);
formula1.parse();
if (formula2String != null)
{
formula2 = new FormulaParser(formula2String,
es, nt, ws);
formula2.parse();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -