提交 c718fdef 编写于 作者: S serge-rider

#1178 Advanced paste fix (parse grid + quoted cells)

上级 fd7b8afb
......@@ -486,10 +486,10 @@ public class SpreadsheetPresentation extends AbstractPresentation implements IRe
return;
}
try (DBCSession session = DBUtils.openUtilSession(VoidProgressMonitor.INSTANCE, dataSource, "Advanced paste")) {
for (String line : strValue.split("\n")) {
for (String[] line : parseGridLines(strValue)) {
int colNum = focusPos.col;
Object rowElement = spreadsheet.getRowElement(rowNum);
for (String value : line.split("\t")) {
for (String value : line) {
if (colNum >= spreadsheet.getColumnCount()) {
break;
}
......@@ -545,6 +545,80 @@ public class SpreadsheetPresentation extends AbstractPresentation implements IRe
}
}
private String[][] parseGridLines(String strValue) {
final char columnDelimiter = '\t';
final char rowDelimiter = '\n';
final char trashDelimiter = '\r';
final char quote = '"';
final List<String[]> lines = new ArrayList<>();
final StringBuilder cellValue = new StringBuilder();
final List<String> curLine = new ArrayList<>();
boolean inQuote = false;
int length = strValue.length();
for (int i = 0; i < length; i++) {
char c = strValue.charAt(i);
if (inQuote && c != quote) {
cellValue.append(c);
} else {
switch (c) {
case columnDelimiter:
curLine.add(cellValue.toString());
cellValue.setLength(0);
break;
case rowDelimiter:
curLine.add(cellValue.toString());
lines.add(curLine.toArray(new String[curLine.size()]));
curLine.clear();
cellValue.setLength(0);
break;
case trashDelimiter:
// Ignore
continue;
case quote:
if (inQuote) {
if (i == length - 1 ||
strValue.charAt(i + 1) == columnDelimiter ||
strValue.charAt(i + 1) == trashDelimiter ||
strValue.charAt(i + 1) == rowDelimiter)
{
inQuote = false;
continue;
}
} else if (cellValue.length() == 0) {
// Search for end quote
for (int k = i + 1; k < length; k++) {
if (strValue.charAt(k) == quote &&
(k == length - 1 ||
strValue.charAt(k + 1) == columnDelimiter ||
strValue.charAt(k + 1) == trashDelimiter ||
strValue.charAt(k + 1) == rowDelimiter))
{
inQuote = true;
break;
}
}
if (inQuote) {
continue;
}
}
default:
cellValue.append(c);
break;
}
}
}
if (cellValue.length() > 0) {
curLine.add(cellValue.toString());
}
if (!curLine.isEmpty()) {
lines.add(curLine.toArray(new String[curLine.size()]));
}
return lines.toArray(new String[lines.size()][]);
}
@Override
public Control getControl() {
return spreadsheet;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册