Dynamic header other than Field Label in model.exportData()

Is there any way to replace field label to dynamic label while exporting. 
For Example,

var modelE = skuid.model.getModel(‘Event’);
Model.exportData({
    fileName: ’ Report-’ +rowE.Name,
    doNotAppendRowIdColumn: true,
    fields: 
    [
        model.getField(‘Account__r.Name’),
]
});

In above case,  how do we replace the label with dynamic label to " Company Name" in header while exporting ?

I’m also very interested in this. The exports done in this way often have confusing names in the columns, or no names at all. How can this be accomplished?

There’s a setting on the exportData function “useAPINamesForHeaders” which you can set to false, that helps a lot and may be all you need in some instances.

The following also allows you to change the value for certain fields:

/* include out all your fields one by one, it may be possible to instead pull in all model fields via model.getFields() */

var fieldsToUse = [

            'Id',

            'Account__r.Name',

            'First_Name__c',

            'Date_of_Birth__c'

]

/* bring in your model */

var model = skuid.$M(‘yourModelName’);

/* map all the fields you listed in the above array to actual field objects from your model. you could probably skip the above set fieldsToUse with model.getFields() or something */

var fieldsWithCorrectLabels = $.map(fieldsToUse, function (v) {

        var actualField = model.getField(v);

/* helpful to make sure you know what field ids you’re getting */

console.log(actualField.id);

console.log(actualField.label);

/* for any field that you want to change, use an if statement based on the field id*/

if (actualField.id === ‘Account__r.Name’){

return {

    id: actualField.id,

    name: actualField.name,

    label: 'Company Name'

};

}

/* for all other fields, use the Salesforce label as your column header */

else {

    return {

    id: actualField.id,

    name: actualField.name,

    label: actualField.label

}; 

}

});

/* export data, your fields array is the result of the above */

model.exportData({

        fields: fieldsWithCorrectLabels,

        doNotAppendRowIdColumn: true,

        useAPINamesForHeaders: false

    });

@Mark_L were you able to test this out in your env?

I’ve actually abandoned the standard export feature due to this and am using SheetJS as a static resource instead ( https://github.com/SheetJS/sheetjs ); this also allows export of XLSX files instead of CSVs. Needed to translate model data to a form that SheetJS can handle; created a custom javascript function for that, but it works. I’d post more here but I don’t know how to embed code in this newer forum system.

Ahh I see, and thanks for sharing @Mark_L . for code snippets you can enter the tic ( ` ) three times or highlight the syntax after you paste it and you’ll see a box:

Screen Shot 2021-03-18 at 10.52.41 AM.png

Custom function for translating model fields over to a form usable by SheetJS’s XLSX.utils.aoa_to_sheet function

skuid.custom.sheetJSData = function (d, options) {

if(d===undefined || d.length === 0){

return undefined;

}

let retArr = ;

let usingFields = false;

if(options !== undefined && options.fields !== undefined){

usingFields=true;

}

let fieldsToSkip = {};

fieldsToSkip[‘skuid_record’] = true;

fieldsToSkip[‘Id’] = true;

if(options !== undefined && options.includeId === true){

fieldsToSkip[‘Id’] = false;

}

//construct first row

let firstRow = ;

if(!usingFields){

for (const [key, value] of Object.entries(d[0])) {

if(fieldsToSkip[key] === true){

continue;

}

firstRow.push(key);

}

}

else{

for (let i=0; i < options.fields.length; i++){

let field = options.fields[i];

let fieldName = field.id;

if(field.name !== undefined){

fieldName = field.name;

}

firstRow.push(fieldName);

}

}

retArr.push(firstRow);

for(let i=0; i < d.length; i++){

let o = d[i];

let thisRow = ;

if(!usingFields){

for (const [key, value] of Object.entries(o)) {

if(fieldsToSkip[key] === true){

continue;

}

thisRow.push(value);

}

}

else{

for (let j=0; j < options.fields.length; j++){

let fieldId = options.fields[j].id;

thisRow.push(o[fieldId]);

}

}

retArr.push(thisRow);

}

return retArr;

};

Example usage in a snippet:

var params = arguments[0], $ = skuid.$;

// use specific fields on the model for export

let fieldsToUse = ;

fieldsToUse.push({id:‘Field1’});

fieldsToUse.push({id:‘Field2’});

// will use field ID as the column header by default, override this using “name”

fieldsToUse.push({id:‘Field3’,name:‘Field3Label’});

let fn = ‘FileNameToUse’;

// make sure this is a valid file name

fn = fn.replace(/[ &/\#,+()$~%.'":*?<>{}]/g, “”);

//create a workbook

var wb = XLSX.utils.book_new();

//create a worksheet

//use our skuid.sheetJSData to translate model.data to a proper export using specific fields

var ws = XLSX.utils.aoa_to_sheet(skuid.custom.sheetJSData(skuid.$M(‘MyModel’).data,{fields: fieldsToUse}));

//append the worksheet

XLSX.utils.book_append_sheet(wb, ws, fn.substring(0,30));

//export

XLSX.writeFile(wb, fn+‘.xlsx’);