Inserting Images in XML and Exporting to Excel

I want to insert the company logo in the exported excel. How to do that in NetSuite Suitescript suitelet

 

xml code

<Row ss:Index="3" ss:Height="13.5">

<Cell ss:Index="2" ss:StyleID="S35" />

<Cell ss:MergeAcross="1" ss:MergeDown="5" ss:StyleID="S215"><Data ss:Type="String">{comp_logo}</Data>

</Cell>

<Cell ss:Index="5" ss:StyleID="S25" />

<Cell ss:Index="11" ss:StyleID="S36" />

</Row>

suitelet

var imageId = '10983871'; // Replace with actual image file ID from the File Cabinet

var imageFile = file.load({

id: imageId

});
// Encode image to base64

var imageUrl = imageFile.getContents();
var dynamicData = {

image_url: imageUrl,

}
var xmlContent = generateExcelContent(dynamicData);
context.response.writeFile({

file: xmlContent

});
function generateExcelContent(data) {

var xmlStr = file.load({

id: '10972644' // Replace with the actual file path or file ID

});
var xmlContent = xmlStr.getContents();
//Insert Company logo

xmlContent = xmlContent.replace('{comp_logo}', '<![CDATA[' + data.image_url + ']]>');//, '<![CDATA[' + data.image_url + ']]>'
var binaryData = xmlContent; // Replace with actual binary data if needed
// Convert the binary data to Base64 encoded string

var base64String = encode.convert({

string: binaryData,

inputEncoding: encode.Encoding.UTF_8,

outputEncoding: encode.Encoding.BASE_64

});
var xlsFile = file.create({

name: 'PSC - ' + data.opp_num + '.xls',

fileType: file.Type.EXCEL, // PLAINTEXT since the content is XML

// encoding: file.Encoding.UTF8,

contents: base64String

});
log.debug("Data object:", JSON.stringify(data));

log.debug("XML content before replacement:", xmlContent);
return xlsFile;//Put it after testing xlsFile

}

Beginner Asked on November 12, 2024 in SuiteScript.
Add Comment
1 Answer(s)

The result is long text, I want the actual image inside of specific cell of the excel

Beginner Answered on November 12, 2024.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   SuiteMigration   Become a Sponsor