Skip to content

Commit

Permalink
preserve field alias when exporting formatted values in Export to
Browse files Browse the repository at this point in the history
spreadsheet algorithm (fix #59403)
  • Loading branch information
alexbruy authored and lbartoletti committed Feb 11, 2025
1 parent fedd040 commit fd8b3b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/analysis/processing/qgsalgorithmexporttospreadsheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class FieldValueConverter : public QgsVectorFileWriter::FieldValueConverter
const int idx = mLayer->fields().indexFromName( field.name() );
if ( mFormatters.contains( idx ) )
{
return QgsField( field.name(), QMetaType::Type::QString );
QgsField newField( field.name(), QMetaType::Type::QString );
newField.setAlias( field.alias() );
return newField;
}
return field;
}
Expand Down
14 changes: 10 additions & 4 deletions tests/src/analysis/testqgsprocessingalgspt1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ void TestQgsProcessingAlgsPt1::exportToSpreadsheetOptions()
bool ok = false;

mPointsLayer->setFieldAlias( 1, QStringLiteral( "my heading" ) );
mPointsLayer->setFieldAlias( 2, QStringLiteral( "my importance" ) );

const QgsProcessingAlgorithm *alg( QgsApplication::processingRegistry()->algorithmById( QStringLiteral( "native:exporttospreadsheet" ) ) );

Expand All @@ -572,7 +573,6 @@ void TestQgsProcessingAlgsPt1::exportToSpreadsheetOptions()

pointLayer.reset();


mPointsLayer->setEditorWidgetSetup( 2, QgsEditorWidgetSetup( QStringLiteral( "ValueMap" ), QVariantMap { { "map", QVariantMap { { "High", "1" }, { "Medium", "10" }, { "Low", "20" }, { "VLow", "3" }, { "VHigh", "4" } } } } ) );

parameters.insert( QStringLiteral( "USE_ALIAS" ), true );
Expand All @@ -584,15 +584,15 @@ void TestQgsProcessingAlgsPt1::exportToSpreadsheetOptions()
pointLayer = std::make_unique<QgsVectorLayer>( outputPath + "|layername=points", "points", "ogr" );
QCOMPARE( pointLayer->fields().at( 0 ).name(), QStringLiteral( "Class" ) );
QCOMPARE( pointLayer->fields().at( 1 ).name(), QStringLiteral( "my heading" ) );
QCOMPARE( pointLayer->fields().at( 2 ).name(), QStringLiteral( "Importance" ) );
QCOMPARE( pointLayer->fields().at( 2 ).name(), QStringLiteral( "my importance" ) );
QCOMPARE( pointLayer->fields().at( 3 ).name(), QStringLiteral( "Pilots" ) );
QCOMPARE( pointLayer->fields().at( 4 ).name(), QStringLiteral( "Cabin Crew" ) );

QSet<QString> values;
QgsFeature f;
QgsFeatureIterator it = pointLayer->getFeatures();
while ( it.nextFeature( f ) )
values.insert( f.attribute( QStringLiteral( "Importance" ) ).toString() );
values.insert( f.attribute( QStringLiteral( "my importance" ) ).toString() );

QCOMPARE( values.size(), 5 );
QVERIFY( values.contains( "1" ) );
Expand All @@ -607,10 +607,16 @@ void TestQgsProcessingAlgsPt1::exportToSpreadsheetOptions()

QVERIFY( !results.value( QStringLiteral( "OUTPUT" ) ).toString().isEmpty() );
pointLayer = std::make_unique<QgsVectorLayer>( outputPath + "|layername=points", "points", "ogr" );
QCOMPARE( pointLayer->fields().at( 0 ).name(), QStringLiteral( "Class" ) );
QCOMPARE( pointLayer->fields().at( 1 ).name(), QStringLiteral( "my heading" ) );
QCOMPARE( pointLayer->fields().at( 2 ).name(), QStringLiteral( "my importance" ) );
QCOMPARE( pointLayer->fields().at( 3 ).name(), QStringLiteral( "Pilots" ) );
QCOMPARE( pointLayer->fields().at( 4 ).name(), QStringLiteral( "Cabin Crew" ) );

values.clear();
it = pointLayer->getFeatures();
while ( it.nextFeature( f ) )
values.insert( f.attribute( QStringLiteral( "Importance" ) ).toString() );
values.insert( f.attribute( QStringLiteral( "my importance" ) ).toString() );

QCOMPARE( values.size(), 5 );
QVERIFY( values.contains( "High" ) );
Expand Down

0 comments on commit fd8b3b3

Please sign in to comment.