Ensure last column of csv export has terminator

Last shown column can be different from the last exported column

Fixes https://gitlab.com/kicad/code/kicad/issues/11981
This commit is contained in:
Seth Hillbrand 2022-07-07 11:22:30 -07:00
parent 6fef054c51
commit 428bbc201b
1 changed files with 14 additions and 2 deletions

View File

@ -1331,6 +1331,8 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnSaveAndContinue( wxCommandEvent& aEvent )
void DIALOG_SYMBOL_FIELDS_TABLE::OnExport( wxCommandEvent& aEvent ) void DIALOG_SYMBOL_FIELDS_TABLE::OnExport( wxCommandEvent& aEvent )
{ {
int last_col = m_grid->GetNumberCols() - 1;
if( m_dataModel->IsEdited() ) if( m_dataModel->IsEdited() )
if( OKOrCancelDialog( nullptr, _( "Unsaved data" ), if( OKOrCancelDialog( nullptr, _( "Unsaved data" ),
_( "Changes are unsaved. Export unsaved data?" ), "", _( "OK" ), _( "Changes are unsaved. Export unsaved data?" ), "", _( "OK" ),
@ -1354,6 +1356,16 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnExport( wxCommandEvent& aEvent )
if( !out.IsOpened() ) if( !out.IsOpened() )
return; return;
// Find the location for the line terminator
for( int col = m_grid->GetNumberCols() - 1; col <=0 ; --col )
{
if( m_grid->IsColShown( col ) )
{
last_col = col;
break;
}
}
// Column names // Column names
for( int col = 0; col < m_grid->GetNumberCols(); col++ ) for( int col = 0; col < m_grid->GetNumberCols(); col++ )
{ {
@ -1363,7 +1375,7 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnExport( wxCommandEvent& aEvent )
wxString escapedValue = m_grid->GetColLabelValue( col ); wxString escapedValue = m_grid->GetColLabelValue( col );
escapedValue.Replace( "\"", "\"\"" ); escapedValue.Replace( "\"", "\"\"" );
wxString format = col == m_grid->GetNumberCols() - 1 ? "\"%s\"\r\n" : "\"%s\","; wxString format = col == last_col ? "\"%s\"\r\n" : "\"%s\",";
out.Write( wxString::Format( format, escapedValue ) ); out.Write( wxString::Format( format, escapedValue ) );
} }
@ -1384,7 +1396,7 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnExport( wxCommandEvent& aEvent )
wxString escapedValue = m_dataModel->GetRawValue( row, col ); wxString escapedValue = m_dataModel->GetRawValue( row, col );
escapedValue.Replace( "\"", "\"\"" ); escapedValue.Replace( "\"", "\"\"" );
wxString format = col == m_grid->GetNumberCols() - 1 ? "\"%s\"\r\n" : "\"%s\","; wxString format = col == last_col ? "\"%s\"\r\n" : "\"%s\",";
out.Write( wxString::Format( format, escapedValue ) ); out.Write( wxString::Format( format, escapedValue ) );
} }