Search code examples
csvemeditor

Command for deleting multiple columns based on header name with EmEditor?


I am looking for a way to use macros to remove certain columns from multiple csv files based on header names.

I found this previous answered question: How can you delete the first and fifth columns from 100 CSV files with EmEditor?

But that only work with the column number.

I tried creating a macro like this:

document.DeleteColumn( Example1 );  // Delete Column With Header Example1
document.DeleteColumn( Example2 );  // Delete Column With Header Example2``` 


But I then get an error saying the example string is unspecified.

Is there a way to do this? Thank you in advance.

Solution

  • The parameter of DeleteColumn() must be an integer value. Here is a JavaScript macro to delete two columns specified by the header names.

    asHeader = ["Example1", "Example2"];
    
    function DeleteColumnByString( sHeader )
    {
        document.selection.StartOfDocument();
        nMaxCol = document.GetColumns();
        document.selection.SetActivePoint( eePosCellLogical, nMaxCol, 1, true );
        if( document.selection.Find( sHeader,eeFindNext | eeFindReplaceCase | eeFindReplaceSelOnly,0) ) {
            iCol = document.selection.GetActivePointX( eePosCellLogical );
            document.selection.Collapse();
            document.DeleteColumn( iCol );
        }
    }
    
    for( i = 0; i < asHeader.length; ++i ) {
        DeleteColumnByString( asHeader[i] );
    }
    

    To run a macro, save the code below as, for instance, RemoveColumn.jsee, and then select this file from Select... in the Macros menu. Finally, select Run RemoveColumn.jsee in the Macros menu.