ATTENTION.. READ THIS FIRST:
While testing and debugging things I suddenly noticed that PHPR 8 began automatically mapping column headers with and without spaces. I am not sure why it did not work for me earlier, but I suspect that my phpr project table structure was out of sync with my actual db table structure. When I started this forum thread I was originally dealing with a db table that contained spaces in column names (yes, I know - terrible, but necessary for client) and also spaces in the excel columns.
Anyhow, you can probably ignore this forum thread. However, I am keeping it here in case someone finds a different use for the snippet I provided. I can envision a use for extracting the first column text and manipulating it.
===============================================================
Original Thread Entry:
The new column mapping feature in PHPR is great! In previous versions we had to handle this mapping manually and it was somewhat clunky and did not allow end user to choose the map themselves. I even wrote about it in this forum post here
However, the new feature in PHPR 8 requires that we map each column every single time we do an import... this is true when our column names in our excel file contain spaces! Ex: "Student Last Name" instead of "Student_Last_Name". The latter will automatically map when you import with the correct column name because PHPR interprets the underscores as equivalent to spaces and will therefore match your database column whether it has spaces or underscores. In my case my excel import file contained actual spaces in the column headers, and hence they did not map automatically. The reason for this is because I prefer to provide my clients an import spreadsheet template without the use of "geeky" underscores in column headers.
Mapping manually can be a tedious waste of time if we constantly import excel files using an existing template. We are now forced to select each column header to map to one-by-one.
So here is a simple solution that will automatically populate the column header map to match the first line of your import file:
Step 1:
In your import page under visual editor insert a new button (using the built in PHPR feature which is found righ next to insert php snippet). I placed this button on the same line as "Column headers in the first line" checkbox that is by default on an import page.
Step 2:
Then in either the "Client Before" or "Client After" tab of the newly inserted button you can add the following javascript code:
var totalColumnsFound = $('#importPreviewHeadTable1 th').length;
//console.log(totalColumnsFound); use this to check how many column headers were created from your import file
//Loop through the column headers, grab text then set value of the column header dropdown mapping control with it :-)
for(i = 0; i <= totalColumnsFound; i++){
var firstRowHeaderVal = $('#importPreviewBodyTable1 .rnr-import-disabled td.prvcell.import-column-' + i).html();
$('#importColumn'+i).val(firstRowHeaderVal).change();;
//console.log(firstRowHeaderVal + i); //to debug things
}
Step 3: Done!
Note that the column headers text on first line must match your database table column names verbatim.
Also note that the "Column headers in the first line" checkbox must be ticked (this adds the "rnr-import-disabled" class to the first row) which we use in the loop above