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

ace
ace ✭✭✭
edited October 23, 2018 in Questions
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 ?

Comments

  • Mark L
    Mark L ✭✭✭✭

    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?

  • Jack Sanford
    Jack Sanford San Antonio, TX 💎💎
    edited February 26, 2021

    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
    
            });
    
  • Germany3
    Germany3 🛠️ 

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

  • Germany3
    Germany3 🛠️ 

    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:


  • Mark L
    Mark L ✭✭✭✭
    edited March 26, 2021

    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');
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!