Merge pull request #21785 from 3x380V/sheet_fixes

Sheet: random fixes
This commit is contained in:
Chris Hennes
2025-06-05 10:16:32 -05:00
committed by GitHub
6 changed files with 57 additions and 30 deletions

View File

@@ -287,20 +287,21 @@ void Cell::setContent(const char* value)
clearException();
if (value) {
if (owner->sheet()->isRestoring()) {
Sheet* sheet = owner->sheet();
if (sheet && sheet->isRestoring()) {
if (value[0] == '\0' || (value[0] == '\'' && value[1] == '\0')) {
return;
}
expression = std::make_unique<App::StringExpression>(owner->sheet(), value);
expression = std::make_unique<App::StringExpression>(sheet, value);
setUsed(EXPRESSION_SET, true);
return;
}
if (*value == '=') {
try {
newExpr = ExpressionPtr(App::ExpressionParser::parse(owner->sheet(), value + 1));
newExpr = ExpressionPtr(App::ExpressionParser::parse(sheet, value + 1));
}
catch (Base::Exception& e) {
newExpr = std::make_unique<App::StringExpression>(owner->sheet(), value);
newExpr = std::make_unique<App::StringExpression>(sheet, value);
setParseException(e.what());
}
}
@@ -309,7 +310,7 @@ void Cell::setContent(const char* value)
value = nullptr;
}
else {
newExpr = std::make_unique<App::StringExpression>(owner->sheet(), value + 1);
newExpr = std::make_unique<App::StringExpression>(sheet, value + 1);
}
}
else if (*value != '\0') {
@@ -320,8 +321,7 @@ void Cell::setContent(const char* value)
if (errno == 0) {
const bool isEndEmpty = *end == '\0' || strspn(end, " \t\n\r") == strlen(end);
if (isEndEmpty) {
newExpr = std::make_unique<App::NumberExpression>(owner->sheet(),
Quantity(float_value));
newExpr = std::make_unique<App::NumberExpression>(sheet, Quantity(float_value));
}
}
@@ -329,7 +329,7 @@ void Cell::setContent(const char* value)
const bool isStartingWithNumber = value != end;
if (!newExpr && isStartingWithNumber) {
try {
ExpressionPtr parsedExpr(App::ExpressionParser::parse(owner->sheet(), value));
ExpressionPtr parsedExpr(App::ExpressionParser::parse(sheet, value));
if (const auto fraction = freecad_cast<OperatorExpression*>(parsedExpr.get())) {
if (fraction->getOperator() == OperatorExpression::UNIT) {
@@ -384,7 +384,7 @@ void Cell::setContent(const char* value)
}
if (!newExpr && value && *value != '\0') {
newExpr = std::make_unique<App::StringExpression>(owner->sheet(), value);
newExpr = std::make_unique<App::StringExpression>(sheet, value);
}
// trying to add an empty string will make newExpr = nullptr

View File

@@ -146,11 +146,6 @@ void Sheet::clearAll()
cellErrors.clear();
columnWidths.clear();
rowHeights.clear();
for (auto& observer : observers) {
delete observer.second;
}
observers.clear();
}
// validate import/export parameters

View File

@@ -290,10 +290,6 @@ protected:
/* Row heights */
PropertyRowHeights rowHeights;
/* Document observers to track changes to external properties */
using ObserverMap = std::map<std::string, SheetObserver*>;
ObserverMap observers;
int currentRow = -1;
int currentCol = -1;

View File

@@ -37,6 +37,7 @@ DlgSettingsImp::DlgSettingsImp(QWidget* parent)
, ui(new Ui_DlgSettings)
{
ui->setupUi(this);
ui->dZLSpinBox->setDisabled(true);
}
/**

View File

@@ -74,9 +74,15 @@ SheetView::SheetView(Gui::Document* pcDocument, App::DocumentObject* docObj, QWi
ui = new Ui::Sheet();
QWidget* w = new QWidget(this);
ui->setupUi(w);
ui->zoomMinus->hide();
ui->zoomPlus->hide();
ui->zoomSlider->hide();
ui->zoomTB->hide();
ui->realSB_h->hide();
ui->realSB_v->hide();
setCentralWidget(w);
new ZoomableView(ui);
// new ZoomableView(ui);
delegate = new SpreadsheetDelegate(sheet);
ui->cells->setModel(model);

View File

@@ -346,9 +346,18 @@ def handleCells(cellList, actCellSheet, sList):
formulaRef = cell.getElementsByTagName("f")
if len(formulaRef) == 1:
theFormula = getText(formulaRef[0].childNodes)
# print("theFormula: ", theFormula)
fTrans = FormulaTranslator()
actCellSheet.set(ref, fTrans.translateForm(theFormula))
if theFormula:
# print("theFormula: ", theFormula)
fTrans = FormulaTranslator()
actCellSheet.set(ref, fTrans.translateForm(theFormula))
else:
attrs = formulaRef[0].attributes
attrRef = attrs.getNamedItem("t")
attrName = getText(attrRef.childNodes)
indexRef = attrs.getNamedItem("si")
indexName = getText(indexRef.childNodes)
content = "<f t='{}' si='{}'/>".format(attrName, indexName)
print(f"Unsupported formula in cell {ref}: {content}")
else:
valueRef = cell.getElementsByTagName("v")
@@ -364,8 +373,22 @@ def handleCells(cellList, actCellSheet, sList):
actCellSheet.set(ref, (sList[int(theValue)]))
def handleWorkBook(theBook, sheetDict, Doc):
def handleWorkBookRels(theBookRels):
theRels = theBookRels.getElementsByTagName("Relationship")
idTarget = {}
for rel in theRels:
relAtts = rel.attributes
idRef = relAtts.getNamedItem("Id")
relRef = getText(idRef.childNodes)
targetRef = relAtts.getNamedItem("Target")
relTarget = getText(targetRef.childNodes)
idTarget[relRef] = relTarget
return idTarget
def handleWorkBook(theBook, theBookRels, sheetDict, Doc):
theSheets = theBook.getElementsByTagName("sheet")
theIdTargetMap = handleWorkBookRels(theBookRels)
# print("theSheets: ", theSheets)
for sheet in theSheets:
sheetAtts = sheet.attributes
@@ -373,7 +396,7 @@ def handleWorkBook(theBook, sheetDict, Doc):
sheetName = getText(nameRef.childNodes)
# print("table name: ", sheetName)
idRef = sheetAtts.getNamedItem("r:id")
sheetFile = "sheet" + getText(idRef.childNodes)[3:] + ".xml"
sheetFile = theIdTargetMap[getText(idRef.childNodes)]
# print("sheetFile: ", sheetFile)
# add FreeCAD-spreadsheet
sheetDict[sheetName] = (Doc.addObject("Spreadsheet::Sheet", sheetName), sheetFile)
@@ -397,10 +420,10 @@ def handleWorkBook(theBook, sheetDict, Doc):
def handleStrings(theStr, sList):
print("process Strings: ")
# print("process Strings: ")
stringElements = theStr.getElementsByTagName("t")
for sElem in stringElements:
print("string: ", getText(sElem.childNodes))
# print("string: ", getText(sElem.childNodes))
sList.append(getText(sElem.childNodes))
@@ -416,8 +439,11 @@ def open(nameXLSX):
theBookFile = z.open("xl/workbook.xml")
theBook = xml.dom.minidom.parse(theBookFile)
handleWorkBook(theBook, sheetDict, theDoc)
theBookRelsFile = z.open("xl/_rels/workbook.xml.rels")
theBookRels = xml.dom.minidom.parse(theBookRelsFile)
handleWorkBook(theBook, theBookRels, sheetDict, theDoc)
theBook.unlink()
theBookRels.unlink()
if "xl/sharedStrings.xml" in z.namelist():
theStringFile = z.open("xl/sharedStrings.xml")
@@ -428,7 +454,7 @@ def open(nameXLSX):
for sheetSpec in sheetDict:
# print("sheetSpec: ", sheetSpec)
theSheet, sheetFile = sheetDict[sheetSpec]
f = z.open("xl/worksheets/" + sheetFile)
f = z.open("xl/" + sheetFile)
myDom = xml.dom.minidom.parse(f)
handleWorkSheet(myDom, theSheet, stringList)
@@ -455,8 +481,11 @@ def insert(nameXLSX, docname):
z = zipfile.ZipFile(nameXLSX)
theBookFile = z.open("xl/workbook.xml")
theBook = xml.dom.minidom.parse(theBookFile)
handleWorkBook(theBook, sheetDict, theDoc)
theBookRelsFile = z.open("xl/_rels/workbook.xml.rels")
theBookRels = xml.dom.minidom.parse(theBookRelsFile)
handleWorkBook(theBook, theBookRels, sheetDict, theDoc)
theBook.unlink()
theBookRels.unlink()
if "xl/sharedStrings.xml" in z.namelist():
theStringFile = z.open("xl/sharedStrings.xml")
@@ -467,7 +496,7 @@ def insert(nameXLSX, docname):
for sheetSpec in sheetDict:
# print("sheetSpec: ", sheetSpec)
theSheet, sheetFile = sheetDict[sheetSpec]
f = z.open("xl/worksheets/" + sheetFile)
f = z.open("xl/" + sheetFile)
myDom = xml.dom.minidom.parse(f)
handleWorkSheet(myDom, theSheet, stringList)