34 const QStringList formattersAllowList{ QStringLiteral(
"KeyValue" ),
35 QStringLiteral(
"List" ),
36 QStringLiteral(
"ValueRelation" ),
37 QStringLiteral(
"ValueMap" ) };
39 for (
int i = 0; i < mLayer->fields().count(); ++i )
43 if ( formattersAllowList.contains( fieldFormatter->
id() ) )
45 mFormatters[i] = fieldFormatter;
46 mConfig[i] = setup.
config();
56 const int idx = mLayer->fields().indexFromName(
field.
name() );
57 if ( mFormatters.contains( idx ) )
64 QVariant
convert(
int i,
const QVariant &value )
override
71 if ( mCaches.contains( i ) )
73 cache = mCaches.value( i );
77 cache =
formatter->createCache( mLayer.data(), i, mConfig.value( i ) );
81 return formatter->representValue( mLayer.data(), i, mConfig.value( i ), cache, value );
84 FieldValueConverter *
clone()
const override
90 QPointer< QgsVectorLayer >
mLayer;
91 QMap< int, const QgsFieldFormatter * > mFormatters;
92 QMap< int, QVariantMap > mConfig;
93 QMap< int, QVariant > mCaches;
96 QString QgsExportToSpreadsheetAlgorithm::name()
const
98 return QStringLiteral(
"exporttospreadsheet" );
101 QString QgsExportToSpreadsheetAlgorithm::displayName()
const
103 return QObject::tr(
"Export to spreadsheet" );
106 QStringList QgsExportToSpreadsheetAlgorithm::tags()
const
108 return QObject::tr(
"microsoft,excel,xls,xlsx,calc,open,office,libre,ods" ).split(
',' );
111 QString QgsExportToSpreadsheetAlgorithm::group()
const
113 return QObject::tr(
"Layer tools" );
116 QString QgsExportToSpreadsheetAlgorithm::groupId()
const
118 return QStringLiteral(
"layertools" );
121 void QgsExportToSpreadsheetAlgorithm::initAlgorithm(
const QVariantMap & )
124 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"USE_ALIAS" ), QObject::tr(
"Use field aliases as column headings" ),
false ) );
125 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"FORMATTED_VALUES" ), QObject::tr(
"Export formatted values instead of raw values" ),
false ) );
127 outputParameter->
setMetadata( QVariantMap( {{QStringLiteral(
"widget_wrapper" ), QVariantMap( {{QStringLiteral(
"dontconfirmoverwrite" ),
true }} ) }} ) );
128 addParameter( outputParameter );
133 QString QgsExportToSpreadsheetAlgorithm::shortHelpString()
const
135 return QObject::tr(
"This algorithm collects a number of existing layers and exports them into a spreadsheet document.\n\n"
136 "Optionally the layers can be appended to an existing spreadsheet as additional sheets.\n\n" );
139 QgsExportToSpreadsheetAlgorithm *QgsExportToSpreadsheetAlgorithm::createInstance()
const
141 return new QgsExportToSpreadsheetAlgorithm();
146 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
149 mLayers.emplace_back( layer->clone() );
152 if ( mLayers.empty() )
153 feedback->
reportError( QObject::tr(
"No layers selected, spreadsheet will be empty" ),
false );
160 const bool overwrite = parameterAsBoolean( parameters, QStringLiteral(
"OVERWRITE" ), context );
161 const QString outputPath = parameterAsString( parameters, QStringLiteral(
"OUTPUT" ), context );
162 if ( outputPath.isEmpty() )
165 const bool useAlias = parameterAsBoolean( parameters, QStringLiteral(
"USE_ALIAS" ), context );
166 const bool formattedValues = parameterAsBoolean( parameters, QStringLiteral(
"FORMATTED_VALUES" ), context );
167 bool createNew =
true;
169 if ( overwrite && QFile::exists( outputPath ) )
171 feedback->
pushInfo( QObject::tr(
"Removing existing file '%1'" ).arg( outputPath ) );
172 if ( !QFile( outputPath ).remove() )
177 else if ( QFile::exists( outputPath ) )
182 const QFileInfo fi( outputPath );
185 OGRSFDriverH hDriver = OGRGetDriverByName( driverName.toLocal8Bit().constData() );
188 if ( driverName == QLatin1String(
"ods" ) )
196 if ( !QFile::exists( outputPath ) )
200 throw QgsProcessingException( QObject::tr(
"Creation of spreadsheet %1 failed (OGR error: %2)" ).arg( outputPath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
203 bool errored =
false;
207 QStringList outputLayers;
209 for (
const auto &layer : mLayers )
214 multiStepFeedback.setCurrentStep( i );
220 feedback->
pushDebugInfo( QObject::tr(
"Error retrieving map layer." ) );
225 feedback->
pushInfo( QObject::tr(
"Exporting layer %1/%2: %3" ).arg( i ).arg( mLayers.size() ).arg( layer ? layer->name() : QString() ) );
227 FieldValueConverter converter( qobject_cast< QgsVectorLayer * >( layer.get() ) );
229 if ( !exportVectorLayer( qobject_cast< QgsVectorLayer * >( layer.get() ), outputPath,
230 context, &multiStepFeedback, driverName, createNew, useAlias, formattedValues ? &converter :
nullptr ) )
234 outputLayers.append( QStringLiteral(
"%1|layername=%2" ).arg( outputPath, layer->name() ) );
243 outputs.insert( QStringLiteral(
"OUTPUT" ), outputPath );
244 outputs.insert( QStringLiteral(
"OUTPUT_LAYERS" ), outputLayers );
266 feedback->
reportError( QObject::tr(
"Exporting layer failed: %1" ).arg( error ) );